github.com/newrelic/newrelic-client-go@v1.1.0/pkg/workflows/workflows_unit_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package workflows 5 6 import ( 7 "fmt" 8 "net/http" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 13 "github.com/newrelic/newrelic-client-go/pkg/ai" 14 "github.com/newrelic/newrelic-client-go/pkg/nrtime" 15 16 mock "github.com/newrelic/newrelic-client-go/pkg/testhelpers" 17 ) 18 19 var ( 20 timestampString = "2022-07-25T12:08:07.179638Z" 21 timestamp = nrtime.DateTime(timestampString) 22 user = "test-user" 23 accountId = 10867072 24 channelId = "0d11fd42-5919-4767-8cf5-e07cb71c1b04" 25 id = "03bd4929-3d86-4447-a077-a901b5d511ff" 26 27 testCreateWorkflowResponseJSON = `{ 28 "aiWorkflowsCreateWorkflow": { 29 "errors": [], 30 "workflow": { 31 "accountId": 10867072, 32 "createdAt": "2022-07-25T12:08:07.179638Z", 33 "destinationConfigurations": [ 34 { 35 "channelId": "0d11fd42-5919-4767-8cf5-e07cb71c1b04", 36 "name": "EMPTY", 37 "type": "EMAIL" 38 } 39 ], 40 "destinationsEnabled": false, 41 "enrichments": [ 42 { 43 "accountId": 10867072, 44 "configurations": [ 45 { 46 "query": "SELECT * FROM Logs" 47 } 48 ], 49 "createdAt": "2022-07-25T12:08:07.179638Z", 50 "id": "79ce0157-9c20-4d95-839f-daeb070bebb1", 51 "name": "Logs", 52 "type": "NRQL", 53 "updatedAt": "2022-07-25T12:08:07.179638Z" 54 } 55 ], 56 "enrichmentsEnabled": false, 57 "id": "03bd4929-3d86-4447-a077-a901b5d511ff", 58 "issuesFilter": { 59 "accountId": 10867072, 60 "id": "1a7f6caf-5afa-4bd5-841c-56108c3b244d", 61 "name": "source", 62 "predicates": [ 63 { 64 "attribute": "source", 65 "operator": "CONTAINS", 66 "values": [ 67 "newrelic" 68 ] 69 } 70 ], 71 "type": "FILTER" 72 }, 73 "lastRun": null, 74 "mutingRulesHandling": "DONT_NOTIFY_FULLY_MUTED_ISSUES", 75 "name": "workflow-test", 76 "updatedAt": "2022-07-25T12:08:07.179638Z", 77 "workflowEnabled": false 78 } 79 } 80 }` 81 82 testDeleteWorkflowResponseJSON = `{ 83 "aiWorkflowsDeleteWorkflow": { 84 "errors": [], 85 "id": "03bd4929-3d86-4447-a077-a901b5d511ff" 86 } 87 }` 88 89 testGetWorkflowResponseJSON = `{ 90 "actor": { 91 "account": { 92 "aiWorkflows": { 93 "workflows": { 94 "entities": [ 95 { 96 "accountId": 10867072, 97 "createdAt": "2022-07-25T12:08:07.179638Z", 98 "destinationConfigurations": [ 99 { 100 "channelId": "0d11fd42-5919-4767-8cf5-e07cb71c1b04", 101 "name": "EMPTY", 102 "type": "EMAIL" 103 } 104 ], 105 "destinationsEnabled": false, 106 "enrichments": [ 107 { 108 "accountId": 10867072, 109 "configurations": [ 110 { 111 "query": "SELECT * FROM Logs" 112 } 113 ], 114 "createdAt": "2022-07-25T12:08:07.179638Z", 115 "id": "79ce0157-9c20-4d95-839f-daeb070bebb1", 116 "name": "Logs", 117 "type": "NRQL", 118 "updatedAt": "2022-07-25T12:08:07.179638Z" 119 } 120 ], 121 "enrichmentsEnabled": false, 122 "id": "03bd4929-3d86-4447-a077-a901b5d511ff", 123 "issuesFilter": { 124 "accountId": 10867072, 125 "id": "1a7f6caf-5afa-4bd5-841c-56108c3b244d", 126 "name": "source", 127 "predicates": [ 128 { 129 "attribute": "source", 130 "operator": "CONTAINS", 131 "values": [ 132 "newrelic" 133 ] 134 } 135 ], 136 "type": "FILTER" 137 }, 138 "lastRun": null, 139 "mutingRulesHandling": "DONT_NOTIFY_FULLY_MUTED_ISSUES", 140 "name": "workflow-test", 141 "updatedAt": "2022-07-25T12:08:07.179638Z", 142 "workflowEnabled": false 143 } 144 ], 145 "nextCursor": null, 146 "totalCount": 1 147 } 148 } 149 } 150 }}` 151 ) 152 153 func newMockResponse(t *testing.T, mockJSONResponse string, statusCode int) Workflows { 154 ts := mock.NewMockServer(t, mockJSONResponse, statusCode) 155 tc := mock.NewTestConfig(t, ts) 156 157 return New(tc) 158 } 159 160 func TestCreateWorkflow(t *testing.T) { 161 t.Parallel() 162 respJSON := fmt.Sprintf(`{ "data":%s }`, testCreateWorkflowResponseJSON) 163 workflows := newMockResponse(t, respJSON, http.StatusCreated) 164 165 enrichmentsInput := &AiWorkflowsEnrichmentsInput{ 166 NRQL: []AiWorkflowsNRQLEnrichmentInput{{ 167 Name: "Logs", 168 Configuration: []AiWorkflowsNRQLConfigurationInput{{Query: "SELECT * FROM Logs"}}, 169 }}, 170 } 171 issuesFilterInput := AiWorkflowsFilterInput{ 172 Name: "source", 173 Type: AiWorkflowsFilterTypeTypes.FILTER, 174 Predicates: []AiWorkflowsPredicateInput{{ 175 Attribute: "source", 176 Operator: "CONTAINS", 177 Values: []string{"newrelic"}, 178 }}, 179 } 180 workflowInput := AiWorkflowsCreateWorkflowInput{ 181 Name: "workflow-test", 182 MutingRulesHandling: AiWorkflowsMutingRulesHandlingTypes.DONT_NOTIFY_FULLY_MUTED_ISSUES, 183 WorkflowEnabled: false, 184 DestinationsEnabled: false, 185 EnrichmentsEnabled: false, 186 DestinationConfigurations: []AiWorkflowsDestinationConfigurationInput{{ 187 ChannelId: channelId, 188 }}, 189 Enrichments: enrichmentsInput, 190 IssuesFilter: issuesFilterInput, 191 } 192 193 expectedDestinationConfiguration := []AiWorkflowsDestinationConfiguration{{ 194 ChannelId: channelId, 195 Name: "EMPTY", 196 Type: "EMAIL", 197 }} 198 expectedIssuedFilter := AiWorkflowsFilter{ 199 AccountID: accountId, 200 ID: "1a7f6caf-5afa-4bd5-841c-56108c3b244d", 201 Name: "source", 202 Type: AiWorkflowsFilterTypeTypes.FILTER, 203 Predicates: []AiWorkflowsPredicate{{ 204 Attribute: "source", 205 Operator: "CONTAINS", 206 Values: []string{"newrelic"}, 207 }}, 208 } 209 expectedEnrichmentConfigurations := []ai.AiWorkflowsConfiguration{ 210 {Query: "SELECT * FROM Logs"}, 211 } 212 for _, config := range expectedEnrichmentConfigurations { 213 config.ImplementsAiWorkflowsConfiguration() 214 } 215 216 expectedEnrichments := []AiWorkflowsEnrichment{{ 217 AccountID: accountId, 218 CreatedAt: timestamp, 219 UpdatedAt: timestamp, 220 ID: "79ce0157-9c20-4d95-839f-daeb070bebb1", 221 Name: "Logs", 222 Type: AiWorkflowsEnrichmentTypeTypes.NRQL, 223 Configurations: expectedEnrichmentConfigurations, 224 }} 225 expected := &AiWorkflowsCreateWorkflowResponse{ 226 Workflow: AiWorkflowsWorkflow{ 227 AccountID: accountId, 228 CreatedAt: timestamp, 229 UpdatedAt: timestamp, 230 DestinationConfigurations: expectedDestinationConfiguration, 231 Enrichments: expectedEnrichments, 232 IssuesFilter: expectedIssuedFilter, 233 MutingRulesHandling: AiWorkflowsMutingRulesHandlingTypes.DONT_NOTIFY_FULLY_MUTED_ISSUES, 234 ID: id, 235 Name: "workflow-test", 236 LastRun: "", 237 WorkflowEnabled: false, 238 EnrichmentsEnabled: false, 239 DestinationsEnabled: false, 240 }, 241 Errors: []AiWorkflowsCreateResponseError{}, 242 } 243 244 actual, err := workflows.AiWorkflowsCreateWorkflow(accountId, workflowInput) 245 246 assert.NoError(t, err) 247 assert.NotNil(t, actual) 248 assert.Equal(t, expected, actual) 249 } 250 251 func TestGetWorkflow(t *testing.T) { 252 t.Parallel() 253 respJSON := fmt.Sprintf(`{ "data":%s }`, testGetWorkflowResponseJSON) 254 workflows := newMockResponse(t, respJSON, http.StatusOK) 255 256 expectedDestinationConfiguration := []AiWorkflowsDestinationConfiguration{{ 257 ChannelId: channelId, 258 Name: "EMPTY", 259 Type: "EMAIL", 260 }} 261 expectedEnrichments := []AiWorkflowsEnrichment{{ 262 AccountID: accountId, 263 CreatedAt: timestamp, 264 UpdatedAt: timestamp, 265 ID: "79ce0157-9c20-4d95-839f-daeb070bebb1", 266 Name: "Logs", 267 Type: AiWorkflowsEnrichmentTypeTypes.NRQL, 268 Configurations: []ai.AiWorkflowsConfiguration{ 269 {Query: "SELECT * FROM Logs"}, 270 }, 271 }} 272 expectedIssuedFilter := AiWorkflowsFilter{ 273 AccountID: accountId, 274 ID: "1a7f6caf-5afa-4bd5-841c-56108c3b244d", 275 Name: "source", 276 Type: AiWorkflowsFilterTypeTypes.FILTER, 277 Predicates: []AiWorkflowsPredicate{{ 278 Attribute: "source", 279 Operator: "CONTAINS", 280 Values: []string{"newrelic"}, 281 }}, 282 } 283 expected := &AiWorkflowsWorkflows{ 284 Entities: []AiWorkflowsWorkflow{ 285 { 286 AccountID: accountId, 287 CreatedAt: timestamp, 288 UpdatedAt: timestamp, 289 DestinationConfigurations: expectedDestinationConfiguration, 290 Enrichments: expectedEnrichments, 291 IssuesFilter: expectedIssuedFilter, 292 MutingRulesHandling: AiWorkflowsMutingRulesHandlingTypes.DONT_NOTIFY_FULLY_MUTED_ISSUES, 293 ID: id, 294 Name: "workflow-test", 295 LastRun: "", 296 WorkflowEnabled: false, 297 EnrichmentsEnabled: false, 298 DestinationsEnabled: false, 299 }, 300 }, 301 NextCursor: "", 302 TotalCount: 1, 303 } 304 305 filters := ai.AiWorkflowsFilters{ 306 ID: id, 307 } 308 309 actual, err := workflows.GetWorkflows(accountId, "", filters) 310 311 assert.NoError(t, err) 312 assert.NotNil(t, actual) 313 assert.Equal(t, expected, actual) 314 } 315 316 func TestDeleteWorkflow(t *testing.T) { 317 t.Parallel() 318 respJSON := fmt.Sprintf(`{ "data":%s }`, testDeleteWorkflowResponseJSON) 319 workflows := newMockResponse(t, respJSON, http.StatusOK) 320 321 expected := &AiWorkflowsDeleteWorkflowResponse{ 322 ID: id, 323 Errors: []AiWorkflowsDeleteResponseError{}, 324 } 325 326 actual, err := workflows.AiWorkflowsDeleteWorkflow(accountId, id) 327 328 assert.NoError(t, err) 329 assert.NotNil(t, actual) 330 assert.Equal(t, expected, actual) 331 }