a3851435c0118e8d4c00fb8bed102a3b56162ac2
[so.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.vcpe.scripts
24
25 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse
26 import static com.github.tomakehurst.wiremock.client.WireMock.put
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching
28 import static org.junit.Assert.*
29 import static org.mockito.Mockito.*
30 import static org.onap.so.bpmn.mock.StubResponseAAI.*
31 import javax.ws.rs.core.UriBuilder
32 import org.camunda.bpm.engine.delegate.BpmnError
33 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
34 import org.junit.*
35 import org.mockito.MockitoAnnotations
36 import org.onap.aaiclient.client.aai.AAIResourcesClient
37 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
38 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
39 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
40 import org.onap.so.bpmn.core.RollbackData
41 import org.onap.so.bpmn.core.UrnPropertiesReader
42 import org.onap.so.bpmn.core.WorkflowException
43 import org.onap.so.bpmn.mock.FileUtil
44 import com.github.tomakehurst.wiremock.junit.WireMockRule
45
46 class DoCreateAllottedResourceBRGTest extends GroovyTestBase {
47
48     @Rule
49     public WireMockRule wireMockRule = new WireMockRule(PORT)
50
51     String Prefix = "DCARBRG_"
52
53     @BeforeClass
54     public static void setUpBeforeClass() {
55         aaiUriPfx = UrnPropertiesReader.getVariable("aai.endpoint")
56     }
57
58     @Before
59     public void init() {
60         MockitoAnnotations.initMocks(this)
61     }
62
63     public DoCreateAllottedResourceBRGTest() {
64         super("DoCreateAllottedResourceBRG")
65     }
66
67     // ***** preProcessRequest *****
68
69     @Test
70     public void preProcessRequest() {
71         ExecutionEntity mex = setupMock()
72         initPreProcess(mex)
73
74         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
75         DoCreateAllottedResourceBRG.preProcessRequest(mex)
76
77         verify(mex).setVariable("prefix", Prefix)
78
79         assertTrue(checkMissingPreProcessRequest("mso.workflow.sdncadapter.callback"))
80         assertTrue(checkMissingPreProcessRequest("mso.workflow.sdnc.replication.delay"))
81         assertTrue(checkMissingPreProcessRequest("serviceInstanceId"))
82         assertTrue(checkMissingPreProcessRequest("parentServiceInstanceId"))
83         assertTrue(checkMissingPreProcessRequest("allottedResourceModelInfo"))
84         assertTrue(checkMissingPreProcessRequest("vni"))
85         assertTrue(checkMissingPreProcessRequest("vgmuxBearerIP"))
86         assertTrue(checkMissingPreProcessRequest("brgWanMacAddress"))
87         assertTrue(checkMissingPreProcessRequest("allottedResourceRole"))
88         assertTrue(checkMissingPreProcessRequest("allottedResourceType"))
89     }
90
91     // ***** getAaiAR *****
92
93     @Test
94     @Ignore
95     public void getAaiAR() {
96         MockGetAllottedResource(wireMockRule, CUST, SVC, INST, ARID, "VCPE/DoCreateAllottedResourceBRG/getArBrg.xml")
97
98         ExecutionEntity mex = setupMock()
99         initGetAaiAR(mex)
100
101         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
102         DoCreateAllottedResourceBRG.getAaiAR(mex)
103
104         verify(mex).setVariable("foundActiveAR", true)
105     }
106
107     @Test
108     public void getAaiAR_Duplicate() {
109         MockGetAllottedResource(wireMockRule, CUST, SVC, INST, ARID, "VCPE/DoCreateAllottedResourceBRG/getArBrg.xml")
110
111         ExecutionEntity mex = setupMock()
112         initGetAaiAR(mex)
113
114         // fail if duplicate
115         when(mex.getVariable("failExists")).thenReturn("true")
116
117         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
118
119         assertTrue(doBpmnError({ _ -> DoCreateAllottedResourceBRG.getAaiAR(mex) }))
120     }
121
122     @Test
123     public void getAaiAR_NotActive() {
124         MockGetAllottedResource(wireMockRule, CUST, SVC, INST, ARID, "VCPE/DoCreateAllottedResourceBRG/getArBrg.xml")
125
126         ExecutionEntity mex = setupMock()
127         initGetAaiAR(mex)
128
129         // not active
130         when(mex.getVariable("aaiAROrchStatus")).thenReturn("not-active")
131
132         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
133
134         assertTrue(doBpmnError({ _ -> DoCreateAllottedResourceBRG.getAaiAR(mex) }))
135     }
136
137     @Test
138     @Ignore
139     public void getAaiAR_NoStatus() {
140         MockGetAllottedResource(wireMockRule, CUST, SVC, INST, ARID, "VCPE/DoCreateAllottedResourceBRG/getArBrg.xml")
141
142         ExecutionEntity mex = setupMock()
143         initGetAaiAR(mex)
144
145         when(mex.getVariable("aaiAROrchStatus")).thenReturn(null)
146
147         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
148         DoCreateAllottedResourceBRG.getAaiAR(mex)
149
150         verify(mex, never()).setVariable("foundActiveAR", true)
151     }
152
153     // ***** createAaiAR *****
154
155     @Test
156     public void createAaiAR() {
157         ExecutionEntity mockExecution = setupMock()
158         AAIResourcesClient client = mock(AAIResourcesClient.class)
159         when(mockExecution.getVariable("PSI_resourceLink")).thenReturn(AAIUriFactory.createResourceFromExistingURI(Types.SERVICE_INSTANCE, UriBuilder.fromPath("/aai/v9/business/customers/customer/" + CUST + "/service-subscriptions/service-subscription/" + SVC + "/service-instances/service-instance/" + INST).build()))
160         when(mockExecution.getVariable("CSI_resourceLink")).thenReturn("/aai/v9/business/customers/customer/" + CUST + "/service-subscriptions/service-subscription/" + SVC + "/service-instances/service-instance/" + INST)
161         when(mockExecution.getVariable("allottedResourceModelInfo")).thenReturn("{\n" +
162                 "  \"modelInvariantUuid\":\"modelInvariantUuid\",\n" +
163                 "  \"modelUuid\" : \"modelUuid\"\n" +
164                 "}")
165         DoCreateAllottedResourceBRG doCreateAllottedResourceBRG = spy(DoCreateAllottedResourceBRG.class)
166         when(doCreateAllottedResourceBRG.getAAIClient()).thenReturn(client)
167         doCreateAllottedResourceBRG.createAaiAR(mockExecution)
168     }
169
170     @Test
171     @Ignore
172     public void createAaiAR_NoArid_NoModelUuids() {
173         ExecutionEntity mex = setupMock()
174         def map = setupMap(mex)
175         initCreateAaiAr(mex)
176
177         // no allottedResourceId - will be generated
178
179         when(mex.getVariable("allottedResourceId")).thenReturn(null)
180
181         wireMockRule
182                 .stubFor(put(urlMatching("/aai/.*/allotted-resource/.*"))
183                 .willReturn(aResponse()
184                 .withStatus(200)))
185
186         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
187         DoCreateAllottedResourceBRG.createAaiAR(mex)
188
189         def arid = map.get("allottedResourceId")
190         assertNotNull(arid)
191         assertFalse(arid.isEmpty())
192
193         def data = map.get("rollbackData")
194         assertNotNull(data)
195         assertTrue(data instanceof RollbackData)
196
197         assertEquals(arid, data.get(Prefix, "allottedResourceId"))
198     }
199
200     @Test
201     public void createAaiAR_MissingPsiLink() {
202         ExecutionEntity mex = setupMock()
203         initCreateAaiAr(mex)
204
205         when(mex.getVariable("PSI_resourceLink")).thenReturn(null)
206
207         MockPutAllottedResource(wireMockRule, CUST, SVC, INST, ARID)
208
209         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
210
211         assertTrue(doBpmnError({ _ -> DoCreateAllottedResourceBRG.createAaiAR(mex) }))
212     }
213
214     @Test
215     public void createAaiAR_HttpFailed() {
216         ExecutionEntity mex = setupMock()
217         initCreateAaiAr(mex)
218
219         MockPutAllottedResource_500(wireMockRule, CUST, SVC, INST, ARID)
220
221         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
222
223         assertTrue(doBpmnError({ _ -> DoCreateAllottedResourceBRG.createAaiAR(mex) }))
224     }
225
226     @Test
227     public void createAaiAR_BpmnError() {
228         ExecutionEntity mex = setupMock()
229         initCreateAaiAr(mex)
230
231         when(mex.getVariable("aai.endpoint")).thenThrow(new BpmnError("expected exception"))
232
233         MockPutAllottedResource(wireMockRule, CUST, SVC, INST, ARID)
234
235         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
236
237         assertTrue(doBpmnError({ _ -> DoCreateAllottedResourceBRG.createAaiAR(mex) }))
238     }
239
240     @Test
241     public void createAaiAR_Ex() {
242         ExecutionEntity mex = setupMock()
243         initCreateAaiAr(mex)
244
245         when(mex.getVariable("aai.endpoint")).thenThrow(new RuntimeException("expected exception"))
246
247         MockPutAllottedResource(wireMockRule, CUST, SVC, INST, ARID)
248
249         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
250
251         assertTrue(doBpmnError({ _ -> DoCreateAllottedResourceBRG.createAaiAR(mex) }))
252     }
253
254     // ***** buildSDNCRequest *****
255
256     @Test
257     public void buildSDNCRequest() {
258         ExecutionEntity mex = setupMock()
259         initBuildSDNCRequest(mex)
260
261         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
262
263         String result = DoCreateAllottedResourceBRG.buildSDNCRequest(mex, "myact", "myreq")
264
265         assertTrue(result.indexOf("<sdncadapter:RequestId>myreq</") >= 0)
266         assertTrue(result.indexOf("<sdncadapter:SvcAction>myact</") >= 0)
267         assertTrue(result.indexOf("<allotted-resource-id>ari</") >= 0)
268         assertTrue(result.indexOf("<sdncadapter:SvcInstanceId>sii</") >= 0)
269         assertTrue(result.indexOf("<service-instance-id>sii</") >= 0)
270         assertTrue(result.indexOf("<parent-service-instance-id>psii</") >= 0)
271         assertTrue(result.indexOf("<subscription-service-type>sst</") >= 0)
272         assertTrue(result.indexOf("<global-customer-id>gci</") >= 0)
273         assertTrue(result.indexOf("<sdncadapter:CallbackUrl>scu</") >= 0)
274         assertTrue(result.indexOf("<request-id>mri</") >= 0)
275         assertTrue(result.indexOf("<brg-wan-mac-address>bwma</") >= 0)
276         assertTrue(result.indexOf("<vni>myvni</") >= 0)
277         assertTrue(result.indexOf("<vgmux-bearer-ip>vbi</") >= 0)
278         assertTrue(result.indexOf("<model-invariant-uuid>miu</") >= 0)
279         assertTrue(result.indexOf("<model-uuid>mu</") >= 0)
280         assertTrue(result.indexOf("<model-customization-uuid>mcu</") >= 0)
281         assertTrue(result.indexOf("<model-version>mv</") >= 0)
282         assertTrue(result.indexOf("<model-name>mn</") >= 0)
283     }
284
285     @Test
286     public void buildSDNCRequest_EmptyModelInfo() {
287         ExecutionEntity mex = setupMock()
288         initBuildSDNCRequest(mex)
289
290         when(mex.getVariable("allottedResourceModelInfo")).thenReturn("{}")
291
292         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
293
294         String result = DoCreateAllottedResourceBRG.buildSDNCRequest(mex, "myact", "myreq")
295
296         assertTrue(result.indexOf("<sdncadapter:RequestId>myreq</") >= 0)
297         assertTrue(result.indexOf("<sdncadapter:SvcAction>myact</") >= 0)
298         assertTrue(result.indexOf("<allotted-resource-id>ari</") >= 0)
299         assertTrue(result.indexOf("<sdncadapter:SvcInstanceId>sii</") >= 0)
300         assertTrue(result.indexOf("<service-instance-id>sii</") >= 0)
301         assertTrue(result.indexOf("<parent-service-instance-id>psii</") >= 0)
302         assertTrue(result.indexOf("<subscription-service-type>sst</") >= 0)
303         assertTrue(result.indexOf("<global-customer-id>gci</") >= 0)
304         assertTrue(result.indexOf("<sdncadapter:CallbackUrl>scu</") >= 0)
305         assertTrue(result.indexOf("<request-id>mri</") >= 0)
306         assertTrue(result.indexOf("<brg-wan-mac-address>bwma</") >= 0)
307         assertTrue(result.indexOf("<vni>myvni</") >= 0)
308         assertTrue(result.indexOf("<vgmux-bearer-ip>vbi</") >= 0)
309         assertTrue(result.indexOf("<model-invariant-uuid/>") >= 0)
310         assertTrue(result.indexOf("<model-uuid/>") >= 0)
311         assertTrue(result.indexOf("<model-customization-uuid/>") >= 0)
312         assertTrue(result.indexOf("<model-version/>") >= 0)
313         assertTrue(result.indexOf("<model-name/>") >= 0)
314     }
315
316     @Test
317     public void buildSDNCRequest_Ex() {
318         ExecutionEntity mex = setupMock()
319         initBuildSDNCRequest(mex)
320
321         when(mex.getVariable("allottedResourceId")).thenThrow(new RuntimeException("expected exception"))
322
323         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
324
325         assertTrue(doBpmnError({ _ -> DoCreateAllottedResourceBRG.buildSDNCRequest(mex, "myact", "myreq") }))
326     }
327
328     // ***** preProcessSDNCAssign *****
329
330     @Test
331     public void preProcessSDNCAssign() {
332         ExecutionEntity mex = setupMock()
333         def map = setupMap(mex)
334         def data = initPreProcessSDNC(mex)
335
336         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
337         DoCreateAllottedResourceBRG.preProcessSDNCAssign(mex)
338
339         def req = map.get("sdncAssignRequest")
340         assertNotNull(req)
341
342         assertEquals(data, map.get("rollbackData"))
343
344         def rbreq = data.get(Prefix, "sdncAssignRollbackReq")
345
346         assertTrue(req.indexOf("<sdncadapter:SvcAction>assign</") >= 0)
347         assertTrue(req.indexOf("<request-action>CreateBRGInstance</") >= 0)
348         assertTrue(req.indexOf("<sdncadapter:RequestId>") >= 0)
349
350         assertTrue(rbreq.indexOf("<sdncadapter:SvcAction>unassign</") >= 0)
351         assertTrue(rbreq.indexOf("<request-action>DeleteBRGInstance</") >= 0)
352         assertTrue(rbreq.indexOf("<sdncadapter:RequestId>") >= 0)
353     }
354
355     @Test
356     public void preProcessSDNCAssign_BpmnError() {
357         ExecutionEntity mex = setupMock()
358         initPreProcessSDNC(mex)
359
360         when(mex.getVariable("rollbackData")).thenThrow(new BpmnError("expected exception"))
361
362         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
363
364         assertTrue(doBpmnError({ _ -> DoCreateAllottedResourceBRG.preProcessSDNCAssign(mex) }))
365     }
366
367     @Test
368     public void preProcessSDNCAssign_Ex() {
369         ExecutionEntity mex = setupMock()
370         initPreProcessSDNC(mex)
371
372         when(mex.getVariable("rollbackData")).thenThrow(new RuntimeException("expected exception"))
373
374         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
375
376         assertTrue(doBpmnError({ _ -> DoCreateAllottedResourceBRG.preProcessSDNCAssign(mex) }))
377     }
378
379     // ***** preProcessSDNCCreate *****
380
381     @Test
382     public void preProcessSDNCCreate() {
383         ExecutionEntity mex = setupMock()
384         def map = setupMap(mex)
385         def data = initPreProcessSDNC(mex)
386
387         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
388         DoCreateAllottedResourceBRG.preProcessSDNCCreate(mex)
389
390         def req = map.get("sdncCreateRequest")
391         assertNotNull(req)
392
393         assertEquals(data, map.get("rollbackData"))
394
395         def rbreq = data.get(Prefix, "sdncCreateRollbackReq")
396
397         assertTrue(req.indexOf("<sdncadapter:SvcAction>create</") >= 0)
398         assertTrue(req.indexOf("<request-action>CreateBRGInstance</") >= 0)
399         assertTrue(req.indexOf("<sdncadapter:RequestId>") >= 0)
400
401         assertTrue(rbreq.indexOf("<sdncadapter:SvcAction>delete</") >= 0)
402         assertTrue(rbreq.indexOf("<request-action>DeleteBRGInstance</") >= 0)
403         assertTrue(rbreq.indexOf("<sdncadapter:RequestId>") >= 0)
404
405     }
406
407     @Test
408     public void preProcessSDNCCreate_BpmnError() {
409         ExecutionEntity mex = setupMock()
410         initPreProcessSDNC(mex)
411
412         when(mex.getVariable("rollbackData")).thenThrow(new BpmnError("expected exception"))
413
414         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
415
416         assertTrue(doBpmnError({ _ -> DoCreateAllottedResourceBRG.preProcessSDNCCreate(mex) }))
417     }
418
419     @Test
420     public void preProcessSDNCCreate_Ex() {
421         ExecutionEntity mex = setupMock()
422         initPreProcessSDNC(mex)
423
424         when(mex.getVariable("rollbackData")).thenThrow(new RuntimeException("expected exception"))
425
426         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
427
428         assertTrue(doBpmnError({ _ -> DoCreateAllottedResourceBRG.preProcessSDNCCreate(mex) }))
429     }
430
431     // ***** preProcessSDNCActivate *****
432
433     @Test
434     public void preProcessSDNCActivate() {
435         ExecutionEntity mex = setupMock()
436         def map = setupMap(mex)
437         def data = initPreProcessSDNC(mex)
438
439         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
440         DoCreateAllottedResourceBRG.preProcessSDNCActivate(mex)
441
442         def req = map.get("sdncActivateRequest")
443         assertNotNull(req)
444
445         assertEquals(data, map.get("rollbackData"))
446
447         def rbreq = data.get(Prefix, "sdncActivateRollbackReq")
448
449         assertTrue(req.indexOf("<sdncadapter:SvcAction>activate</") >= 0)
450         assertTrue(req.indexOf("<request-action>CreateBRGInstance</") >= 0)
451         assertTrue(req.indexOf("<sdncadapter:RequestId>") >= 0)
452
453         assertTrue(rbreq.indexOf("<sdncadapter:SvcAction>deactivate</") >= 0)
454         assertTrue(rbreq.indexOf("<request-action>DeleteBRGInstance</") >= 0)
455         assertTrue(rbreq.indexOf("<sdncadapter:RequestId>") >= 0)
456
457     }
458
459     @Test
460     public void preProcessSDNCActivate_BpmnError() {
461         ExecutionEntity mex = setupMock()
462         initPreProcessSDNC(mex)
463
464         when(mex.getVariable("rollbackData")).thenThrow(new BpmnError("expected exception"))
465
466         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
467
468         assertTrue(doBpmnError({ _ -> DoCreateAllottedResourceBRG.preProcessSDNCActivate(mex) }))
469     }
470
471     @Test
472     public void preProcessSDNCActivate_Ex() {
473         ExecutionEntity mex = setupMock()
474         initPreProcessSDNC(mex)
475
476         when(mex.getVariable("rollbackData")).thenThrow(new RuntimeException("expected exception"))
477
478         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
479
480         assertTrue(doBpmnError({ _ -> DoCreateAllottedResourceBRG.preProcessSDNCActivate(mex) }))
481     }
482
483     // ***** validateSDNCResp *****
484
485     @Test
486     public void validateSDNCResp() {
487         ExecutionEntity mex = setupMock()
488         def map = setupMap(mex)
489         def data = initValidateSDNCResp(mex)
490         def resp = initValidateSDNCResp_Resp()
491
492         when(mex.getVariable(Prefix + "sdncResponseSuccess")).thenReturn(true)
493
494         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
495
496         DoCreateAllottedResourceBRG.validateSDNCResp(mex, resp, "create")
497
498         verify(mex).getVariable("WorkflowException")
499         verify(mex).getVariable("SDNCA_SuccessIndicator")
500         verify(mex).getVariable("rollbackData")
501
502         assertEquals(data, map.get("rollbackData"))
503
504         assertEquals("true", data.get(Prefix, "rollback" + "SDNCcreate"))
505
506     }
507
508     @Test
509     public void validateSDNCResp_Get() {
510         ExecutionEntity mex = setupMock()
511         def data = initValidateSDNCResp(mex)
512         def resp = initValidateSDNCResp_Resp()
513
514         when(mex.getVariable(Prefix + "sdncResponseSuccess")).thenReturn(true)
515
516         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
517
518         DoCreateAllottedResourceBRG.validateSDNCResp(mex, resp, "get")
519
520         verify(mex).getVariable("WorkflowException")
521         verify(mex).getVariable("SDNCA_SuccessIndicator")
522
523         verify(mex, never()).getVariable("rollbackData")
524     }
525
526     @Test
527     public void validateSDNCResp_Unsuccessful() {
528         ExecutionEntity mex = setupMock()
529         initValidateSDNCResp(mex)
530         def resp = initValidateSDNCResp_Resp()
531
532         // unsuccessful
533         when(mex.getVariable(Prefix + "sdncResponseSuccess")).thenReturn(false)
534
535         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
536
537         assertTrue(doBpmnError({ _ -> DoCreateAllottedResourceBRG.validateSDNCResp(mex, resp, "create") }))
538     }
539
540     @Test
541     public void validateSDNCResp_BpmnError() {
542         ExecutionEntity mex = setupMock()
543         initValidateSDNCResp(mex)
544         def resp = initValidateSDNCResp_Resp()
545
546         when(mex.getVariable("WorkflowException")).thenThrow(new BpmnError("expected exception"))
547
548         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
549
550         assertTrue(doBpmnError({ _ -> DoCreateAllottedResourceBRG.validateSDNCResp(mex, resp, "create") }))
551     }
552
553     @Test
554     public void validateSDNCResp_Ex() {
555         ExecutionEntity mex = setupMock()
556         initValidateSDNCResp(mex)
557         def resp = initValidateSDNCResp_Resp()
558
559         when(mex.getVariable("WorkflowException")).thenThrow(new RuntimeException("expected exception"))
560
561         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
562
563         assertTrue(doBpmnError({ _ -> DoCreateAllottedResourceBRG.validateSDNCResp(mex, resp, "create") }))
564     }
565
566     // ***** preProcessSDNCGet *****
567
568     @Test
569     public void preProcessSDNCGet_FoundAR() {
570         ExecutionEntity mex = setupMock()
571         def map = setupMap(mex)
572         initPreProcessSDNCGet(mex)
573
574         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
575         DoCreateAllottedResourceBRG.preProcessSDNCGet(mex)
576
577         String req = map.get("sdncGetRequest")
578
579         assertTrue(req.indexOf("<sdncadapter:RequestId>") >= 0)
580         assertTrue(req.indexOf("<sdncadapter:SvcInstanceId>sii</") >= 0)
581         assertTrue(req.indexOf("<sdncadapter:SvcOperation>arlink</") >= 0)
582         assertTrue(req.indexOf("<sdncadapter:CallbackUrl>myurl</") >= 0)
583
584     }
585
586     @Test
587     public void preProcessSDNCGet_NotFoundAR() {
588         ExecutionEntity mex = setupMock()
589         def map = setupMap(mex)
590         initPreProcessSDNCGet(mex)
591
592         when(mex.getVariable("foundActiveAR")).thenReturn(false)
593
594         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
595         DoCreateAllottedResourceBRG.preProcessSDNCGet(mex)
596
597         String req = map.get("sdncGetRequest")
598
599         assertTrue(req.indexOf("<sdncadapter:RequestId>") >= 0)
600         assertTrue(req.indexOf("<sdncadapter:SvcInstanceId>sii</") >= 0)
601         assertTrue(req.indexOf("<sdncadapter:SvcOperation>assignlink</") >= 0)
602         assertTrue(req.indexOf("<sdncadapter:CallbackUrl>myurl</") >= 0)
603
604     }
605
606     @Test
607     public void preProcessSDNCGet_Ex() {
608         ExecutionEntity mex = setupMock()
609         initPreProcessSDNCGet(mex)
610
611         when(mex.getVariable("foundActiveAR")).thenThrow(new RuntimeException("expected exception"))
612
613         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
614
615         assertTrue(doBpmnError({ _ -> DoCreateAllottedResourceBRG.preProcessSDNCGet(mex) }))
616     }
617
618     // ***** updateAaiAROrchStatus *****
619
620     @Test
621     @Ignore
622     public void updateAaiAROrchStatus() {
623         MockPatchAllottedResource(wireMockRule, CUST, SVC, INST, ARID)
624
625         ExecutionEntity mex = setupMock()
626         initUpdateAaiAROrchStatus(mex)
627
628         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
629         DoCreateAllottedResourceBRG.updateAaiAROrchStatus(mex, "success")
630     }
631
632     // ***** generateOutputs *****
633
634     @Test
635     public void generateOutputs() {
636         ExecutionEntity mex = setupMock()
637         def brgtop = FileUtil.readResourceFile("__files/VCPE/DoCreateAllottedResourceBRG/SDNCTopologyQueryCallback.xml")
638
639         when(mex.getVariable(DBGFLAG)).thenReturn("true")
640         when(mex.getVariable("enhancedCallbackRequestData")).thenReturn(brgtop)
641
642         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
643         DoCreateAllottedResourceBRG.generateOutputs(mex)
644
645         verify(mex).setVariable("allotedResourceName", "namefromrequest")
646
647     }
648
649     @Test
650     public void generateOutputs_BadXml() {
651         ExecutionEntity mex = setupMock()
652
653         when(mex.getVariable(DBGFLAG)).thenReturn("true")
654         when(mex.getVariable("enhancedCallbackRequestData")).thenReturn("invalid xml")
655
656         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
657         DoCreateAllottedResourceBRG.generateOutputs(mex)
658
659         verify(mex, never()).setVariable(anyString(), anyString())
660
661     }
662
663     @Test
664     public void generateOutputs_BpmnError() {
665         ExecutionEntity mex = setupMock()
666
667         when(mex.getVariable(DBGFLAG)).thenReturn("true")
668         when(mex.getVariable("enhancedCallbackRequestData")).thenThrow(new BpmnError("expected exception"))
669
670         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
671
672         DoCreateAllottedResourceBRG.generateOutputs(mex)
673         verify(mex, never()).setVariable(anyString(), anyString())
674
675     }
676
677     @Test
678     public void generateOutputs_Ex() {
679         ExecutionEntity mex = setupMock()
680
681         when(mex.getVariable(DBGFLAG)).thenReturn("true")
682         when(mex.getVariable("enhancedCallbackRequestData")).thenThrow(new RuntimeException("expected exception"))
683
684         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
685
686         DoCreateAllottedResourceBRG.generateOutputs(mex)
687         verify(mex, never()).setVariable(anyString(), anyString())
688
689     }
690
691     // ***** preProcessRollback *****
692
693     @Test
694     public void preProcessRollback() {
695         ExecutionEntity mex = setupMock()
696         WorkflowException wfe = mock(WorkflowException.class)
697
698         when(mex.getVariable(DBGFLAG)).thenReturn("true")
699         when(mex.getVariable("WorkflowException")).thenReturn(wfe)
700
701         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
702         DoCreateAllottedResourceBRG.preProcessRollback(mex)
703
704         verify(mex).setVariable("prevWorkflowException", wfe)
705     }
706
707     @Test
708     public void preProcessRollback_NotWFE() {
709         ExecutionEntity mex = setupMock()
710
711         when(mex.getVariable(DBGFLAG)).thenReturn("true")
712         when(mex.getVariable("WorkflowException")).thenReturn("I'm not a WFE")
713
714         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
715         DoCreateAllottedResourceBRG.preProcessRollback(mex)
716     }
717
718     @Test
719     public void preProcessRollback_BpmnError() {
720         ExecutionEntity mex = setupMock()
721
722         when(mex.getVariable(DBGFLAG)).thenReturn("true")
723         when(mex.getVariable("WorkflowException")).thenThrow(new BpmnError("expected exception"))
724
725         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
726
727         DoCreateAllottedResourceBRG.preProcessRollback(mex)
728     }
729
730     @Test
731     public void preProcessRollback_Ex() {
732         ExecutionEntity mex = setupMock()
733
734         when(mex.getVariable(DBGFLAG)).thenReturn("true")
735         when(mex.getVariable("WorkflowException")).thenThrow(new RuntimeException("expected exception"))
736
737         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
738
739         DoCreateAllottedResourceBRG.preProcessRollback(mex)
740
741     }
742
743     // ***** postProcessRollback *****
744
745     @Test
746     public void postProcessRollback() {
747         ExecutionEntity mex = setupMock()
748         WorkflowException wfe = mock(WorkflowException.class)
749
750         when(mex.getVariable(DBGFLAG)).thenReturn("true")
751         when(mex.getVariable("prevWorkflowException")).thenReturn(wfe)
752
753         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
754         DoCreateAllottedResourceBRG.postProcessRollback(mex)
755
756         verify(mex).setVariable("WorkflowException", wfe)
757         verify(mex).setVariable("rollbackData", null)
758
759     }
760
761     @Test
762     public void postProcessRollback_NotWFE() {
763         ExecutionEntity mex = setupMock()
764
765         when(mex.getVariable(DBGFLAG)).thenReturn("true")
766         when(mex.getVariable("prevWorkflowException")).thenReturn("I'm not a WFE")
767
768         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
769         DoCreateAllottedResourceBRG.postProcessRollback(mex)
770
771         verify(mex).setVariable("rollbackData", null)
772
773     }
774
775     @Test
776     public void postProcessRollback_BpmnError() {
777         ExecutionEntity mex = setupMock()
778
779         when(mex.getVariable(DBGFLAG)).thenReturn("true")
780         when(mex.getVariable("prevWorkflowException")).thenThrow(new BpmnError("expected exception"))
781
782         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
783
784         assertTrue(doBpmnError({ _ -> DoCreateAllottedResourceBRG.postProcessRollback(mex) }))
785         verify(mex, never()).setVariable("rollbackData", null)
786
787     }
788
789     @Test
790     public void postProcessRollback_Ex() {
791         ExecutionEntity mex = setupMock()
792
793         when(mex.getVariable(DBGFLAG)).thenReturn("true")
794         when(mex.getVariable("prevWorkflowException")).thenThrow(new RuntimeException("expected exception"))
795
796         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
797
798         DoCreateAllottedResourceBRG.postProcessRollback(mex)
799         verify(mex, never()).setVariable("rollbackData", null)
800
801     }
802
803     private boolean checkMissingPreProcessRequest(String fieldnm) {
804         ExecutionEntity mex = setupMock()
805         initPreProcess(mex)
806
807         DoCreateAllottedResourceBRG DoCreateAllottedResourceBRG = new DoCreateAllottedResourceBRG()
808
809         when(mex.getVariable(fieldnm)).thenReturn("")
810
811         return doBpmnError({ _ -> DoCreateAllottedResourceBRG.preProcessRequest(mex) })
812     }
813
814     private void initPreProcess(ExecutionEntity mex) {
815         when(mex.getVariable(DBGFLAG)).thenReturn("true")
816         when(mex.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("sdncurn")
817         when(mex.getVariable("mso.workflow.sdnc.replication.delay")).thenReturn("sdncdelay")
818         when(mex.getVariable("serviceInstanceId")).thenReturn("sii")
819         when(mex.getVariable("parentServiceInstanceId")).thenReturn("psii")
820         when(mex.getVariable("allottedResourceModelInfo")).thenReturn("armi")
821         when(mex.getVariable("vni")).thenReturn("myvni")
822         when(mex.getVariable("vgmuxBearerIP")).thenReturn("vbi")
823         when(mex.getVariable("brgWanMacAddress")).thenReturn("bwma")
824         when(mex.getVariable("allottedResourceRole")).thenReturn("arr")
825         when(mex.getVariable("allottedResourceType")).thenReturn("art")
826     }
827
828     private void initGetAaiAR(ExecutionEntity mex) {
829         when(mex.getVariable(DBGFLAG)).thenReturn("true")
830         when(mex.getVariable("allottedResourceType")).thenReturn("BRGt")
831         when(mex.getVariable("allottedResourceRole")).thenReturn("BRGr")
832         when(mex.getVariable("CSI_service")).thenReturn(FileUtil.readResourceFile("__files/VCPE/DoCreateAllottedResourceBRG/getAR.xml"))
833         when(mex.getVariable("aai.endpoint")).thenReturn(aaiUriPfx)
834         when(mex.getVariable("aaiAROrchStatus")).thenReturn("Active")
835     }
836
837     private initCreateAaiAr(ExecutionEntity mex) {
838         when(mex.getVariable("disableRollback")).thenReturn(45)
839         when(mex.getVariable("serviceInstanceId")).thenReturn("sii")
840         when(mex.getVariable("parentServiceInstanceId")).thenReturn("psii")
841         when(mex.getVariable(DBGFLAG)).thenReturn("true")
842         when(mex.getVariable("allottedResourceId")).thenReturn(ARID)
843         when(mex.getVariable("aai.endpoint")).thenReturn(aaiUriPfx)
844         when(mex.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn(UrnPropertiesReader.getVariable("mso.workflow.global.default.aai.namespace"))
845         when(mex.getVariable("PSI_resourceLink")).thenReturn(AAIUriFactory.createResourceFromExistingURI(Types.SERVICE_INSTANCE, UriBuilder.fromPath("/business/customers/customer/" + CUST + "/service-subscriptions/service-subscription/" + SVC + "/service-instances/service-instance/" + INST).build()))
846         when(mex.getVariable("allottedResourceType")).thenReturn("BRGt")
847         when(mex.getVariable("allottedResourceRole")).thenReturn("BRGr")
848         when(mex.getVariable("CSI_resourceLink")).thenReturn(aaiUriPfx + "/aai/v9/mycsi")
849         when(mex.getVariable("allottedResourceModelInfo")).thenReturn("""
850                 {
851                     "modelInvariantUuid":"modelinvuuid",
852                     "modelUuid":"modeluuid",
853                     "modelCustomizationUuid":"modelcustuuid"
854                 }
855             """)
856     }
857
858     private initBuildSDNCRequest(ExecutionEntity mex) {
859         when(mex.getVariable(DBGFLAG)).thenReturn("true")
860         when(mex.getVariable("allottedResourceId")).thenReturn("ari")
861         when(mex.getVariable("serviceInstanceId")).thenReturn("sii")
862         when(mex.getVariable("parentServiceInstanceId")).thenReturn("psii")
863         when(mex.getVariable("subscriptionServiceType")).thenReturn("sst")
864         when(mex.getVariable("globalCustomerId")).thenReturn("gci")
865         when(mex.getVariable("sdncCallbackUrl")).thenReturn("scu")
866         when(mex.getVariable("msoRequestId")).thenReturn("mri")
867         when(mex.getVariable("brgWanMacAddress")).thenReturn("bwma")
868         when(mex.getVariable("vni")).thenReturn("myvni")
869         when(mex.getVariable("vgmuxBearerIP")).thenReturn("vbi")
870         when(mex.getVariable("allottedResourceModelInfo")).thenReturn("""
871                 {
872                     "modelInvariantUuid":"miu",
873                     "modelUuid":"mu",
874                     "modelCustomizationUuid":"mcu",
875                     "modelVersion":"mv",
876                     "modelName":"mn"
877                 }
878             """)
879     }
880
881     private RollbackData initPreProcessSDNC(ExecutionEntity mex) {
882         def data = new RollbackData()
883
884         when(mex.getVariable(DBGFLAG)).thenReturn("true")
885         when(mex.getVariable("rollbackData")).thenReturn(data)
886
887         return data
888     }
889
890     private initPreProcessSDNCGet(ExecutionEntity mex) {
891         when(mex.getVariable(DBGFLAG)).thenReturn("true")
892         when(mex.getVariable("sdncCallbackUrl")).thenReturn("myurl")
893         when(mex.getVariable("foundActiveAR")).thenReturn(true)
894         when(mex.getVariable("aaiARGetResponse")).thenReturn("<selflink>arlink</selflink>")
895         when(mex.getVariable("sdncAssignResponse")).thenReturn("<response-data>&lt;object-path&gt;assignlink&lt;/object-path&gt;</response-data>")
896         when(mex.getVariable("serviceInstanceId")).thenReturn("sii")
897         when(mex.getVariable("sdncCallbackUrl")).thenReturn("myurl")
898     }
899
900     private RollbackData initValidateSDNCResp(ExecutionEntity mex) {
901         def data = new RollbackData()
902
903         when(mex.getVariable(DBGFLAG)).thenReturn("true")
904         when(mex.getVariable("prefix")).thenReturn(Prefix)
905         when(mex.getVariable("SDNCA_SuccessIndicator")).thenReturn(true)
906         when(mex.getVariable("rollbackData")).thenReturn(data)
907
908         return data
909     }
910
911     private String initValidateSDNCResp_Resp() {
912         return "<response-data>&lt;response-code&gt;200&lt;/response-code&gt;</response-data>"
913     }
914
915     private initUpdateAaiAROrchStatus(ExecutionEntity mex) {
916         when(mex.getVariable(DBGFLAG)).thenReturn("true")
917         when(mex.getVariable("aaiARPath")).thenReturn("/business/customers/customer/" + CUST + "/service-subscriptions/service-subscription/" + SVC + "/service-instances/service-instance/" + INST + "/allotted-resources/allotted-resource/" + ARID)
918     }
919
920 }