forked from signalfx/signalfx-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_now_integration_test.go
264 lines (212 loc) · 12.2 KB
/
service_now_integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package signalfx
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"testing"
"github.com/signalfx/signalfx-go/integration"
"github.com/stretchr/testify/assert"
)
const (
serviceNowId = "FFF-yyyyZZ"
serviceNowName = "SNOW integ"
serviceNowInstanceName = "inst.service-now.com"
serviceNowIssueTypeIncident = "Incident"
serviceNowCreated = int64(1647426008469)
serviceNowCreatedBy = "EEEzzzzYYY"
serviceNowLastUpdated = int64(1647426008953)
serviceNowLastUpdatedBy = "YY_zzzzyyy"
serviceNowAlertTriggeredTemplate = "{\"short_description\": \"{{{messageTitle}}} (customized)\"}"
serviceNowAlertResolvedTemplate = "{\"close_notes\": \"{{{messageTitle}}} (customized close msg)\"}"
serviceNowUsername = "i.am"
serviceNowPassword = "my#pass"
)
var serviceNowRequestData = integration.ServiceNowIntegration{
Enabled: true,
Type: integration.SERVICE_NOW,
Name: serviceNowName,
InstanceName: serviceNowInstanceName,
IssueType: serviceNowIssueTypeIncident,
AlertTriggeredPayloadTemplate: serviceNowAlertTriggeredTemplate,
AlertResolvedPayloadTemplate: serviceNowAlertResolvedTemplate,
Username: serviceNowUsername,
Password: serviceNowPassword,
}
func verifyGetServiceNowIntegrationRequest(t *testing.T, r *http.Request) {
verifyHeaders(t, r, true)
verifyParams(t, r, url.Values{})
assert.Equal(t, "GET", r.Method, "Incorrect HTTP method")
body, err := ioutil.ReadAll(r.Body)
assert.NoError(t, err, "Unexpected error getting request body")
assert.Empty(t, body, "Unexpected request body")
}
func TestGetServiceNowIntegration(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc(IntegrationAPIURL+"/"+serviceNowId,
createResponse(t, http.StatusOK, "integration/create_service_now_success.json", verifyGetServiceNowIntegrationRequest))
result, err := client.GetServiceNowIntegration(context.Background(), serviceNowId)
if assert.NoError(t, err, "Unexpected error getting the integration") {
assert.Equal(t, integration.SERVICE_NOW, result.Type, "Integration type does not match")
assert.Equal(t, true, result.Enabled, "Integration not enabled")
assert.Equal(t, serviceNowId, result.Id, "ID does not match")
assert.Equal(t, serviceNowName, result.Name, "Name does not match")
assert.Equal(t, serviceNowInstanceName, result.InstanceName, "Instance name does not match")
assert.Equal(t, serviceNowIssueTypeIncident, result.IssueType, "Issue type does not match")
assert.Equal(t, serviceNowCreated, result.Created, "Create time does not match")
assert.Equal(t, serviceNowCreatedBy, result.Creator, "Creator does not match")
assert.Equal(t, serviceNowLastUpdated, result.LastUpdated, "Update time does not match")
assert.Equal(t, serviceNowLastUpdatedBy, result.LastUpdatedBy, "Updater does not match")
assert.Equal(t, serviceNowAlertTriggeredTemplate, result.AlertTriggeredPayloadTemplate, "Alert triggered template does not match")
assert.Equal(t, serviceNowAlertResolvedTemplate, result.AlertResolvedPayloadTemplate, "Alert resolved template does not match")
assert.Empty(t, result.Username, "User name is not empty")
assert.Empty(t, result.Password, "Password is not empty")
}
}
func TestFailGetServiceNowIntegration(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc(IntegrationAPIURL+"/"+serviceNowId,
createResponse(t, http.StatusNotFound, "", verifyGetServiceNowIntegrationRequest))
result, err := client.GetServiceNowIntegration(context.Background(), serviceNowId)
assert.Error(t, err, "Error expected getting the integration")
assert.Nil(t, result, "No result expected")
}
func verifyCreateServiceNowIntegrationRequest(t *testing.T, r *http.Request) {
verifyHeaders(t, r, true)
verifyParams(t, r, url.Values{})
assert.Equal(t, "POST", r.Method, "Incorrect HTTP method")
body := make(map[string]interface{})
err := json.NewDecoder(r.Body).Decode(&body)
if assert.NoError(t, err, "Unexpected error getting request body") {
//goland:noinspection GoRedundantConversion
assert.Equal(t, string(integration.SERVICE_NOW), body["type"], "Sent integration type does not match")
assert.Equal(t, true, body["enabled"], "Sent integration not enabled")
assert.Empty(t, body["id"], "Sent unexpected ID")
assert.Equal(t, serviceNowName, body["name"], "Sent name does not match")
assert.Equal(t, serviceNowInstanceName, body["instanceName"], "Sent instance name does not match")
assert.Equal(t, serviceNowIssueTypeIncident, body["issueType"], "Sent issue type does not match")
assert.Empty(t, body["created"], "Sent unexpected create time")
assert.Empty(t, body["creator"], "Sent unexpected creator")
assert.Empty(t, body["lastUpdated"], "Sent unexpected update time")
assert.Empty(t, body["lastUpdatedBy"], "Sent unexpected updater")
assert.Equal(t, serviceNowAlertTriggeredTemplate, body["alertTriggeredPayloadTemplate"], "Sent alert triggered template does not match")
assert.Equal(t, serviceNowAlertResolvedTemplate, body["alertResolvedPayloadTemplate"], "Sent alert resolved template does not match")
assert.Equal(t, serviceNowUsername, body["username"], "Sent user name does not match")
assert.Equal(t, serviceNowPassword, body["password"], "Sent password does not match")
}
}
func TestCreateServiceNowIntegration(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc(IntegrationAPIURL,
createResponse(t, http.StatusOK, "integration/create_service_now_success.json", verifyCreateServiceNowIntegrationRequest))
result, err := client.CreateServiceNowIntegration(context.Background(), &serviceNowRequestData)
if assert.NoError(t, err, "Unexpected error creating an integration") {
assert.Equal(t, integration.SERVICE_NOW, result.Type, "Integration type does not match")
assert.Equal(t, true, result.Enabled, "Integration not enabled")
assert.Equal(t, serviceNowId, result.Id, "ID does not match")
assert.Equal(t, serviceNowName, result.Name, "Name does not match")
assert.Equal(t, serviceNowInstanceName, result.InstanceName, "Instance name does not match")
assert.Equal(t, serviceNowIssueTypeIncident, result.IssueType, "Issue type does not match")
assert.Equal(t, serviceNowCreated, result.Created, "Create time does not match")
assert.Equal(t, serviceNowCreatedBy, result.Creator, "Creator does not match")
assert.Equal(t, serviceNowLastUpdated, result.LastUpdated, "Update time does not match")
assert.Equal(t, serviceNowLastUpdatedBy, result.LastUpdatedBy, "Updater does not match")
assert.Equal(t, serviceNowAlertTriggeredTemplate, result.AlertTriggeredPayloadTemplate, "Alert triggered template does not match")
assert.Equal(t, serviceNowAlertResolvedTemplate, result.AlertResolvedPayloadTemplate, "Alert resolved template does not match")
assert.Empty(t, result.Username, "User name is not empty")
assert.Empty(t, result.Password, "Password is not empty")
}
}
func TestFailCreateServiceNowIntegration(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc(IntegrationAPIURL,
createResponse(t, http.StatusForbidden, "", verifyCreateServiceNowIntegrationRequest))
result, err := client.CreateServiceNowIntegration(context.Background(), &serviceNowRequestData)
assert.Error(t, err, "Error expected creating an integration")
assert.Nil(t, result, "No result expected")
}
func verifyUpdateServiceNowIntegrationRequest(t *testing.T, r *http.Request) {
verifyHeaders(t, r, true)
verifyParams(t, r, url.Values{})
assert.Equal(t, "PUT", r.Method, "Incorrect HTTP method")
body := make(map[string]interface{})
err := json.NewDecoder(r.Body).Decode(&body)
if assert.NoError(t, err, "Unexpected error getting request body") {
//goland:noinspection GoRedundantConversion
assert.Equal(t, string(integration.SERVICE_NOW), body["type"], "Sent integration type does not match")
assert.Equal(t, true, body["enabled"], "Sent integration not enabled")
assert.Empty(t, body["id"], "Sent unexpected ID")
assert.Equal(t, serviceNowName, body["name"], "Sent name does not match")
assert.Equal(t, serviceNowInstanceName, body["instanceName"], "Sent instance name does not match")
assert.Equal(t, serviceNowIssueTypeIncident, body["issueType"], "Sent issue type does not match")
assert.Empty(t, body["created"], "Sent unexpected create time")
assert.Empty(t, body["creator"], "Sent unexpected creator")
assert.Empty(t, body["lastUpdated"], "Sent unexpected update time")
assert.Empty(t, body["lastUpdatedBy"], "Sent unexpected updater")
assert.Equal(t, serviceNowAlertTriggeredTemplate, body["alertTriggeredPayloadTemplate"], "Sent alert triggered template does not match")
assert.Equal(t, serviceNowAlertResolvedTemplate, body["alertResolvedPayloadTemplate"], "Sent alert resolved template does not match")
assert.Equal(t, serviceNowUsername, body["username"], "Sent user name does not match")
assert.Equal(t, serviceNowPassword, body["password"], "Sent password does not match")
}
}
func TestUpdateServiceNowIntegration(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc(IntegrationAPIURL+"/"+serviceNowId,
createResponse(t, http.StatusOK, "integration/create_service_now_success.json", verifyUpdateServiceNowIntegrationRequest))
result, err := client.UpdateServiceNowIntegration(context.Background(), serviceNowId, &serviceNowRequestData)
if assert.NoError(t, err, "Unexpected error updating the integration") {
assert.Equal(t, integration.SERVICE_NOW, result.Type, "Integration type does not match")
assert.Equal(t, true, result.Enabled, "Integration not enabled")
assert.Equal(t, serviceNowId, result.Id, "ID does not match")
assert.Equal(t, serviceNowName, result.Name, "Name does not match")
assert.Equal(t, serviceNowInstanceName, result.InstanceName, "Instance name does not match")
assert.Equal(t, serviceNowIssueTypeIncident, result.IssueType, "Issue type does not match")
assert.Equal(t, serviceNowCreated, result.Created, "Create time does not match")
assert.Equal(t, serviceNowCreatedBy, result.Creator, "Creator does not match")
assert.Equal(t, serviceNowLastUpdated, result.LastUpdated, "Update time does not match")
assert.Equal(t, serviceNowLastUpdatedBy, result.LastUpdatedBy, "Updater does not match")
assert.Equal(t, serviceNowAlertTriggeredTemplate, result.AlertTriggeredPayloadTemplate, "Alert triggered template does not match")
assert.Equal(t, serviceNowAlertResolvedTemplate, result.AlertResolvedPayloadTemplate, "Alert resolved template does not match")
assert.Empty(t, result.Username, "User name is not empty")
assert.Empty(t, result.Password, "Password is not empty")
}
}
func TestFailUpdateServiceNowIntegration(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc(IntegrationAPIURL+"/"+serviceNowId,
createResponse(t, http.StatusForbidden, "", verifyUpdateServiceNowIntegrationRequest))
result, err := client.UpdateServiceNowIntegration(context.Background(), serviceNowId, &serviceNowRequestData)
assert.Error(t, err, "Error expected updating the integration")
assert.Nil(t, result, "No result expected")
}
func verifyDeleteServiceNowIntegrationRequest(t *testing.T, r *http.Request) {
verifyHeaders(t, r, true)
verifyParams(t, r, url.Values{})
assert.Equal(t, "DELETE", r.Method, "Incorrect HTTP method")
body, err := ioutil.ReadAll(r.Body)
assert.NoError(t, err, "Unexpected error getting request body")
assert.Empty(t, body, "Unexpected request body")
}
func TestDeleteServiceNowIntegration(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc(IntegrationAPIURL+"/"+serviceNowId,
createResponse(t, http.StatusNoContent, "", verifyDeleteServiceNowIntegrationRequest))
err := client.DeleteServiceNowIntegration(context.Background(), serviceNowId)
assert.NoError(t, err, "Unexpected error deleting the integration")
}
func TestFailDeleteServiceNowIntegration(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc(IntegrationAPIURL+"/"+serviceNowId,
createResponse(t, http.StatusNotFound, "", verifyDeleteServiceNowIntegrationRequest))
err := client.DeleteServiceNowIntegration(context.Background(), serviceNowId)
assert.Error(t, err, "Error expected deleting the integration")
}