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