|
| 1 | +package notification |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestNotificationUnmarshaJSON(t *testing.T) { |
| 12 | + t.Parallel() |
| 13 | + |
| 14 | + for _, tc := range []struct { |
| 15 | + name string |
| 16 | + input string |
| 17 | + expect *Notification |
| 18 | + errVal string |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "no type value defined", |
| 22 | + input: `{}`, |
| 23 | + expect: nil, |
| 24 | + errVal: "unknown notification type \"\"", |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "AWS Event Bridge", |
| 28 | + input: `{"type":"AmazonEventBridge"}`, |
| 29 | + expect: &Notification{Type: "AmazonEventBridge", Value: &AmazonEventBrigeNotification{ |
| 30 | + Type: "AmazonEventBridge", |
| 31 | + }}, |
| 32 | + errVal: "", |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "BigPanda", |
| 36 | + input: `{"type":"BigPanda"}`, |
| 37 | + expect: &Notification{Type: "BigPanda", Value: &BigPandaNotification{ |
| 38 | + Type: "BigPanda", |
| 39 | + }}, |
| 40 | + errVal: "", |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "Email", |
| 44 | + input: `{"type":"Email"}`, |
| 45 | + expect: &Notification{Type: "Email", Value: &EmailNotification{ |
| 46 | + Type: "Email", |
| 47 | + }}, |
| 48 | + errVal: "", |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "Jira", |
| 52 | + input: `{"type":"Jira"}`, |
| 53 | + expect: &Notification{Type: "Jira", Value: &JiraNotification{ |
| 54 | + Type: "Jira", |
| 55 | + }}, |
| 56 | + errVal: "", |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "Office365", |
| 60 | + input: `{"type":"Office365"}`, |
| 61 | + expect: &Notification{Type: "Office365", Value: &Office365Notification{ |
| 62 | + Type: "Office365", |
| 63 | + }}, |
| 64 | + errVal: "", |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "Opsgenie", |
| 68 | + input: `{"type":"Opsgenie"}`, |
| 69 | + expect: &Notification{Type: "Opsgenie", Value: &OpsgenieNotification{ |
| 70 | + Type: "Opsgenie", |
| 71 | + }}, |
| 72 | + errVal: "", |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "PagerDuty", |
| 76 | + input: `{"type":"PagerDuty"}`, |
| 77 | + expect: &Notification{Type: "PagerDuty", Value: &PagerDutyNotification{ |
| 78 | + Type: "PagerDuty", |
| 79 | + }}, |
| 80 | + errVal: "", |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "ServiceNow", |
| 84 | + input: `{"type":"ServiceNow"}`, |
| 85 | + expect: &Notification{Type: "ServiceNow", Value: &ServiceNowNotification{ |
| 86 | + Type: "ServiceNow", |
| 87 | + }}, |
| 88 | + errVal: "", |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "Slack", |
| 92 | + input: `{"type":"Slack"}`, |
| 93 | + expect: &Notification{Type: "Slack", Value: &SlackNotification{ |
| 94 | + Type: "Slack", |
| 95 | + }}, |
| 96 | + errVal: "", |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "Team", |
| 100 | + input: `{"type":"Team"}`, |
| 101 | + expect: &Notification{Type: "Team", Value: &TeamNotification{ |
| 102 | + Type: "Team", |
| 103 | + }}, |
| 104 | + errVal: "", |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "TeamEmail", |
| 108 | + input: `{"type":"TeamEmail"}`, |
| 109 | + expect: &Notification{Type: "TeamEmail", Value: &TeamEmailNotification{ |
| 110 | + Type: "TeamEmail", |
| 111 | + }}, |
| 112 | + errVal: "", |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "VictorOps", |
| 116 | + input: `{"type":"VictorOps"}`, |
| 117 | + expect: &Notification{Type: "VictorOps", Value: &VictorOpsNotification{ |
| 118 | + Type: "VictorOps", |
| 119 | + }}, |
| 120 | + errVal: "", |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "Webhook", |
| 124 | + input: `{"type":"Webhook"}`, |
| 125 | + expect: &Notification{Type: "Webhook", Value: &WebhookNotification{ |
| 126 | + Type: "Webhook", |
| 127 | + }}, |
| 128 | + errVal: "", |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "XMatters", |
| 132 | + input: `{"type":"XMatters"}`, |
| 133 | + expect: &Notification{Type: "XMatters", Value: &XMattersNotification{ |
| 134 | + Type: "XMatters", |
| 135 | + }}, |
| 136 | + errVal: "", |
| 137 | + }, |
| 138 | + { |
| 139 | + name: "SplunkPlatform", |
| 140 | + input: `{"type":"SplunkPlatform"}`, |
| 141 | + expect: &Notification{Type: "SplunkPlatform", Value: &SplunkPlatformNotification{ |
| 142 | + Type: "SplunkPlatform", |
| 143 | + }}, |
| 144 | + errVal: "", |
| 145 | + }, |
| 146 | + } { |
| 147 | + t.Run(tc.name, func(t *testing.T) { |
| 148 | + t.Parallel() |
| 149 | + |
| 150 | + var actual Notification |
| 151 | + err := json.NewDecoder(strings.NewReader(tc.input)).Decode(&actual) |
| 152 | + if tc.errVal != "" { |
| 153 | + assert.EqualError(t, err, tc.errVal, "Must match the expected error value") |
| 154 | + } else { |
| 155 | + assert.Equal(t, tc.expect, &actual, "Must match the expected notification value") |
| 156 | + assert.NoError(t, err, "Must not error") |
| 157 | + } |
| 158 | + }) |
| 159 | + } |
| 160 | +} |
0 commit comments