|
| 1 | +package models |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestFlexInt_UnmarshalJSON(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name string |
| 11 | + input string |
| 12 | + want FlexInt |
| 13 | + wantErr bool |
| 14 | + }{ |
| 15 | + {"string 1", `"1"`, 1, false}, |
| 16 | + {"string 0", `"0"`, 0, false}, |
| 17 | + {"bare int 1", `1`, 1, false}, |
| 18 | + {"bare int 0", `0`, 0, false}, |
| 19 | + {"bool true", `true`, 1, false}, |
| 20 | + {"bool false", `false`, 0, false}, |
| 21 | + {"invalid string", `"abc"`, 0, true}, |
| 22 | + {"null", `null`, 0, true}, |
| 23 | + } |
| 24 | + for _, tt := range tests { |
| 25 | + t.Run(tt.name, func(t *testing.T) { |
| 26 | + var f FlexInt |
| 27 | + err := json.Unmarshal([]byte(tt.input), &f) |
| 28 | + if (err != nil) != tt.wantErr { |
| 29 | + t.Fatalf("UnmarshalJSON(%s) error = %v, wantErr %v", tt.input, err, tt.wantErr) |
| 30 | + } |
| 31 | + if !tt.wantErr && f != tt.want { |
| 32 | + t.Errorf("UnmarshalJSON(%s) = %d, want %d", tt.input, f, tt.want) |
| 33 | + } |
| 34 | + }) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// TestSavedSearchObject_CloudBooleanFields verifies that SavedSearchObject |
| 39 | +// correctly deserialises the boolean values Splunk Cloud returns for |
| 40 | +// integer fields that Splunk Enterprise returns as string-encoded integers. |
| 41 | +// Regression test for https://github.com/splunk/terraform-provider-splunk/issues/130 |
| 42 | +func TestSavedSearchObject_CloudBooleanFields(t *testing.T) { |
| 43 | + // Splunk Cloud returns booleans for these fields instead of "0"/"1" strings. |
| 44 | + cloudJSON := `{ |
| 45 | + "action.email.include.results_link": false, |
| 46 | + "action.email.include.search": false, |
| 47 | + "action.email.include.trigger": true, |
| 48 | + "action.email.include.trigger_time": true, |
| 49 | + "action.email.include.view_link": false, |
| 50 | + "action.email.sendcsv": false, |
| 51 | + "action.snow_event.param.severity": false |
| 52 | + }` |
| 53 | + |
| 54 | + var obj SavedSearchObject |
| 55 | + if err := json.Unmarshal([]byte(cloudJSON), &obj); err != nil { |
| 56 | + t.Fatalf("Unmarshal cloud response: %v", err) |
| 57 | + } |
| 58 | + |
| 59 | + if obj.ActionEmailIncludeResultsLink != 0 { |
| 60 | + t.Errorf("ActionEmailIncludeResultsLink: got %d, want 0", obj.ActionEmailIncludeResultsLink) |
| 61 | + } |
| 62 | + if obj.ActionEmailIncludeSearch != 0 { |
| 63 | + t.Errorf("ActionEmailIncludeSearch: got %d, want 0", obj.ActionEmailIncludeSearch) |
| 64 | + } |
| 65 | + if obj.ActionEmailIncludeTrigger != 1 { |
| 66 | + t.Errorf("ActionEmailIncludeTrigger: got %d, want 1", obj.ActionEmailIncludeTrigger) |
| 67 | + } |
| 68 | + if obj.ActionEmailIncludeTriggerTime != 1 { |
| 69 | + t.Errorf("ActionEmailIncludeTriggerTime: got %d, want 1", obj.ActionEmailIncludeTriggerTime) |
| 70 | + } |
| 71 | + if obj.ActionEmailIncludeViewLink != 0 { |
| 72 | + t.Errorf("ActionEmailIncludeViewLink: got %d, want 0", obj.ActionEmailIncludeViewLink) |
| 73 | + } |
| 74 | + if obj.ActionEmailSendCSV != 0 { |
| 75 | + t.Errorf("ActionEmailSendCSV: got %d, want 0", obj.ActionEmailSendCSV) |
| 76 | + } |
| 77 | + if obj.ActionSnowEventParamSeverity != 0 { |
| 78 | + t.Errorf("ActionSnowEventParamSeverity: got %d, want 0", obj.ActionSnowEventParamSeverity) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// TestSavedSearchObject_EnterpriseStringFields verifies that SavedSearchObject |
| 83 | +// correctly deserialises the string-encoded integers Splunk Enterprise returns. |
| 84 | +func TestSavedSearchObject_EnterpriseStringFields(t *testing.T) { |
| 85 | + enterpriseJSON := `{ |
| 86 | + "action.email.include.results_link": "1", |
| 87 | + "action.email.include.search": "1", |
| 88 | + "action.email.include.trigger": "1", |
| 89 | + "action.email.include.trigger_time": "1", |
| 90 | + "action.email.include.view_link": "1", |
| 91 | + "action.email.sendcsv": "0", |
| 92 | + "action.snow_event.param.severity": "3" |
| 93 | + }` |
| 94 | + |
| 95 | + var obj SavedSearchObject |
| 96 | + if err := json.Unmarshal([]byte(enterpriseJSON), &obj); err != nil { |
| 97 | + t.Fatalf("Unmarshal enterprise response: %v", err) |
| 98 | + } |
| 99 | + |
| 100 | + if obj.ActionEmailIncludeResultsLink != 1 { |
| 101 | + t.Errorf("ActionEmailIncludeResultsLink: got %d, want 1", obj.ActionEmailIncludeResultsLink) |
| 102 | + } |
| 103 | + if obj.ActionEmailIncludeSearch != 1 { |
| 104 | + t.Errorf("ActionEmailIncludeSearch: got %d, want 1", obj.ActionEmailIncludeSearch) |
| 105 | + } |
| 106 | + if obj.ActionEmailIncludeTrigger != 1 { |
| 107 | + t.Errorf("ActionEmailIncludeTrigger: got %d, want 1", obj.ActionEmailIncludeTrigger) |
| 108 | + } |
| 109 | + if obj.ActionEmailIncludeTriggerTime != 1 { |
| 110 | + t.Errorf("ActionEmailIncludeTriggerTime: got %d, want 1", obj.ActionEmailIncludeTriggerTime) |
| 111 | + } |
| 112 | + if obj.ActionEmailIncludeViewLink != 1 { |
| 113 | + t.Errorf("ActionEmailIncludeViewLink: got %d, want 1", obj.ActionEmailIncludeViewLink) |
| 114 | + } |
| 115 | + if obj.ActionEmailSendCSV != 0 { |
| 116 | + t.Errorf("ActionEmailSendCSV: got %d, want 0", obj.ActionEmailSendCSV) |
| 117 | + } |
| 118 | + if obj.ActionSnowEventParamSeverity != 3 { |
| 119 | + t.Errorf("ActionSnowEventParamSeverity: got %d, want 3", obj.ActionSnowEventParamSeverity) |
| 120 | + } |
| 121 | +} |
0 commit comments