9707dd2242e1afb5155ef043cb27d440a28f3d8a
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCommonCoreNSSITest.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020  Telecom Italia
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.scripts
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.junit.Before
25 import org.junit.Test
26 import org.mockito.Mockito
27 import org.onap.aai.domain.yang.v19.*
28 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
29 import org.onap.aaiclient.client.aai.entities.Relationships
30 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
31 import org.onap.aaiclient.client.aai.entities.uri.AAISimpleUri
32 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
33 import org.onap.aaiclient.client.aai.entities.uri.ServiceInstanceUri
34 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
35 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
36 import org.onap.so.bpmn.common.scripts.ExternalAPIUtil
37 import org.onap.so.bpmn.common.scripts.ExternalAPIUtilFactory
38 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
39 import org.onap.so.bpmn.core.json.JsonUtils
40 import org.onap.so.serviceinstancebeans.RequestDetails
41
42 import javax.ws.rs.core.Response
43 import java.time.Instant
44
45 import static org.junit.Assert.assertNotNull
46 import static org.junit.Assert.assertTrue
47 import static org.mockito.Mockito.*
48
49 class DoCommonCoreNSSITest extends MsoGroovyTest {
50     @Before
51     void init() throws IOException {
52         super.init("DoCommonCoreNSSITest")
53     }
54
55
56     @Test
57     void testPreProcessRequest() {
58
59         String nssiId = "5G-999"
60         when(mockExecution.getVariable("serviceInstanceID")).thenReturn(nssiId)
61
62         String nsiId = "5G-777"
63         when(mockExecution.getVariable("nsiId")).thenReturn(nsiId)
64
65         String snssai = "S-NSSAI"
66         String snssaiList = "[ \"${snssai}\" ]"
67         String sliceProfileId = "slice-profile-id"
68         String sliceParams =  "{\n" +
69                 "\"sliceProfile\":{\"sliceProfileId\":\"${sliceProfileId}\",\"snssaiList\":${snssaiList}}\n" +
70                 "}"
71         when(mockExecution.getVariable("sliceParams")).thenReturn(sliceParams)
72
73         DoCommonCoreNSSI dcnssi = new DoCommonCoreNSSI()
74         dcnssi.preProcessRequest(mockExecution)
75
76         def currentNSSI = [:]
77         currentNSSI.put("nssiId", nssiId)
78         currentNSSI.put("nsiId", nsiId)
79         currentNSSI.put("sliceProfile", "{\"sliceProfileId\":\"slice-profile-id\",\"snssaiList\":[\"S-NSSAI\"]}")
80         currentNSSI.put("S-NSSAI", snssai)
81         currentNSSI.put("sliceProfileId", sliceProfileId)
82         Mockito.verify(mockExecution,times(1)).setVariable("currentNSSI", currentNSSI)
83
84     }
85
86
87     @Test
88     void testGetNetworkServiceInstance() {
89         def currentNSSI = [:]
90         currentNSSI.put("nssiId","5G-999")
91
92         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
93
94         AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment("5G-999"))
95         AAIResourceUri networkServiceInstanceUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment("NS-777"))
96
97         ServiceInstance nssi = new ServiceInstance()
98         nssi.setServiceInstanceId("5G-999")
99
100         ServiceInstance networkServiceInstance = new ServiceInstance()
101         networkServiceInstance.setServiceInstanceId("NS-777")
102         networkServiceInstance.setServiceRole("Network Service")
103
104         Optional<ServiceInstance> nssiOpt = Optional.of(nssi)
105         Optional<ServiceInstance> networkServiceInstaneOpt = Optional.of(networkServiceInstance)
106
107         DoCommonCoreNSSI spy = spy(DoCommonCoreNSSI.class)
108         when(spy.getAAIClient()).thenReturn(client)
109
110         when(client.get(ServiceInstance.class, nssiUri)).thenReturn(nssiOpt)
111
112         //String json = FileUtil.readResourceFile("__files/AAI/ServiceInstanceWithAR.json")
113         AAIResultWrapper wrapperMock = mock(AAIResultWrapper.class) //new AAIResultWrapper(json)
114         Relationships rsMock = mock(Relationships.class)
115         Optional<Relationships> orsMock = Optional.of(rsMock)
116         List<AAIResourceUri> arus = new ArrayList<>()
117         AAIResourceUri aru = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(networkServiceInstance.getServiceInstanceId()))
118         arus.add(aru)
119
120         when(client.get(nssiUri)).thenReturn(wrapperMock)
121         when(wrapperMock.getRelationships()).thenReturn(orsMock)
122
123         when(rsMock.getRelatedUris(Types.SERVICE_INSTANCE)).thenReturn(arus)
124         when(client.get(ServiceInstance.class, aru)).thenReturn(networkServiceInstaneOpt)
125
126         spy.getNetworkServiceInstance(mockExecution)
127
128         assertTrue("Either NSSI doesn't exist or unexpected NSSI Service Instance ID",
129                 currentNSSI.get("nssi") != null && ((ServiceInstance)currentNSSI.get("nssi")).getServiceInstanceId().equals(nssi.getServiceInstanceId()))
130
131         assertTrue("Either Network Service Instance doesn't exist or unexpected Network Service Instance ID",
132                 currentNSSI.get("networkServiceInstance") != null && ((ServiceInstance)currentNSSI.get("networkServiceInstance")).getServiceInstanceId().equals(networkServiceInstance.getServiceInstanceId()))
133
134         assertNotNull("networkServiceInstanceUri doesn't exist", currentNSSI.get("networkServiceInstanceUri"))
135     }
136
137
138     @Test
139     void getConstituteVNFFromNetworkServiceInst() {
140         def currentNSSI = [:]
141
142         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
143
144         DoCommonCoreNSSI spy = spy(DoCommonCoreNSSI.class)
145         when(spy.getAAIClient()).thenReturn(client)
146
147         ServiceInstance networkServiceInstance = new ServiceInstance()
148         networkServiceInstance.setServiceInstanceId("NS-777")
149         networkServiceInstance.setServiceRole("Network Service")
150
151         GenericVnf genericVNF = new GenericVnf()
152         genericVNF.setVnfId("VNF-1")
153
154         AAIResourceUri networkServiceInstanceUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(networkServiceInstance.getServiceInstanceId()))
155
156         Optional<GenericVnf> genericVnfOpt = Optional.of(genericVNF)
157         AAIResourceUri genericVNFUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf(genericVNF.getVnfId()))
158
159         currentNSSI.put("networkServiceInstanceUri", networkServiceInstanceUri)
160         currentNSSI.put("networkServiceInstance", networkServiceInstance)
161
162         AAIResultWrapper wrapperMock = mock(AAIResultWrapper.class) //new AAIResultWrapper(json)
163         Relationships rsMock = mock(Relationships.class)
164         Optional<Relationships> orsMock = Optional.of(rsMock)
165         List<AAIResourceUri> arus = new ArrayList<>()
166         AAIResourceUri aru = new AAISimpleUri(genericVNFUri)
167         arus.add(aru)
168
169         when(client.get(networkServiceInstanceUri)).thenReturn(wrapperMock)
170         when(wrapperMock.getRelationships()).thenReturn(orsMock)
171
172         when(rsMock.getRelatedUris(Types.GENERIC_VNF)).thenReturn(arus)
173         when(client.get(GenericVnf.class, genericVNFUri)).thenReturn(genericVnfOpt)
174
175         spy.getConstituteVNFFromNetworkServiceInst(mockExecution)
176
177         assertNotNull("constituteVnfUri doesn't exist", currentNSSI.get("constituteVnfUri"))
178
179         assertTrue("Either Constitute VNF doesn't exist or unexpected VNF ID",
180                 currentNSSI.get("constituteVnf") != null && ((GenericVnf)currentNSSI.get("constituteVnf")).getVnfId().equals(genericVNF.getVnfId()))
181     }
182
183
184     @Test
185     void testGetNSSIAssociatedProfiles() {
186         def currentNSSI = [:]
187         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
188
189         String nssiId = "5G-999"
190         ServiceInstance nssi = new ServiceInstance()
191         nssi.setServiceInstanceId(nssiId)
192         currentNSSI.put("nssiId", nssiId)
193
194         SliceProfiles sliceProfiles = new SliceProfiles()
195
196         List<SliceProfile> slProfiles = sliceProfiles.getSliceProfile()
197         slProfiles.add(new SliceProfile())
198         slProfiles.add(new SliceProfile())
199
200         //nssi.setSliceProfiles(sliceProfiles)
201         currentNSSI.put("nssi", nssi)
202
203         DoCommonCoreNSSI spy = spy(DoCommonCoreNSSI.class)
204         when(spy.getAAIClient()).thenReturn(client)
205
206         AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
207
208         AAIResultWrapper wrapperMock = mock(AAIResultWrapper.class) //new AAIResultWrapper(json)
209         Relationships rsMock = mock(Relationships.class)
210         Optional<Relationships> orsMock = Optional.of(rsMock)
211         List<AAIResourceUri> allottedUris = new ArrayList<>()
212         AAIResourceUri allottedUri = AAIUriFactory.createResourceUri(Types.ALLOTTED_RESOURCE.getFragment("allotted-id"))
213         allottedUris.add(allottedUri)
214
215         when(client.get(nssiUri)).thenReturn(wrapperMock)
216         when(wrapperMock.getRelationships()).thenReturn(orsMock)
217         when(rsMock.getRelatedUris(Types.ALLOTTED_RESOURCE)).thenReturn(allottedUris)
218
219         String sliceProfileInstanceId = "slice-profile-instance-id"
220         ServiceInstance sliceProfileInstance = new ServiceInstance()
221         sliceProfileInstance.setServiceInstanceId(sliceProfileInstanceId)
222         sliceProfileInstance.setServiceRole("slice-profile-instance")
223
224         List<AAIResourceUri> sliceProfileInstanceUris = new ArrayList<>()
225         AAIResourceUri sliceProfileInstanceUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(sliceProfileInstance.getServiceInstanceId()))
226         sliceProfileInstanceUris.add(sliceProfileInstanceUri)
227
228         Optional<ServiceInstance> sliceProfileInstanceOpt = Optional.of(sliceProfileInstance)
229
230         when(client.get(allottedUri)).thenReturn(wrapperMock)
231         when(rsMock.getRelatedUris(Types.SERVICE_INSTANCE)).thenReturn(sliceProfileInstanceUris)
232         when(client.get(ServiceInstance.class, sliceProfileInstanceUri)).thenReturn(sliceProfileInstanceOpt)
233
234
235         SliceProfiles sps = new SliceProfiles()
236         sps.getSliceProfile().addAll(slProfiles)
237         sliceProfileInstance.setSliceProfiles(sps)
238
239         spy.getNSSIAssociatedProfiles(mockExecution)
240
241         List<SliceProfile> associatedProfiles = (List<SliceProfile>)currentNSSI.get("associatedProfiles")
242         assertTrue("sliceProfileInstanceUri not found in contect Map", currentNSSI.get("sliceProfileInstanceUri") != null)
243         assertTrue("Either associatedProfiles doesn't exist or size is incorrect", (associatedProfiles != null && associatedProfiles.size() == 2))
244     }
245
246
247     @Test
248     void testInvokePUTServiceInstance() {
249         def currentNSSI = [:]
250
251         ServiceInstance networkServiceInstance = new ServiceInstance()
252         networkServiceInstance.setServiceInstanceId("NS-777")
253         networkServiceInstance.setServiceRole("Network Service")
254
255         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
256
257         currentNSSI.put("networkServiceInstance", networkServiceInstance)
258
259         when(mockExecution.getVariable("mso.infra.endpoint.url")).thenReturn("http://mso.onap:8088")
260         when(mockExecution.getVariable("mso.msoKey")).thenReturn("mso.msoKey")
261         when(mockExecution.getVariable("mso.infra.endpoint.auth")).thenReturn("mso.infra.endpoint.auth")
262
263         DoCommonCoreNSSI spy = spy(DoCommonCoreNSSI.class)
264         when(spy.getAAIClient()).thenReturn(client)
265
266         when(spy.encryptBasicAuth("mso.infra.endpoint.auth", "mso.msoKey")).thenReturn("auth-value")
267
268         String authHeaderResponse =  "auth-header"
269
270         when(spy.getAuthHeader(mockExecution, "auth-value", "mso.msoKey")).thenReturn(authHeaderResponse)
271
272         String urlString = String.format("http://mso.onap:8088/serviceInstantiation/v7/serviceInstances/%s", networkServiceInstance.getServiceInstanceId())
273
274         String callPUTServiceInstanceResponse = "put"
275
276         RequestDetails requestDetails = new RequestDetails()
277         ObjectMapper mapper = new ObjectMapper()
278         String requestDetailsStr = mapper.writeValueAsString(requestDetails)
279
280         when(spy.prepareRequestDetails(mockExecution)).thenReturn(requestDetailsStr)
281
282         when(spy.callPUTServiceInstance(urlString, "auth-header", requestDetailsStr)).thenReturn(callPUTServiceInstanceResponse)
283
284         spy.invokePUTServiceInstance(mockExecution)
285     }
286
287
288     @Test
289     void testRemoveSPAssociationWithNSSI() {
290         def currentNSSI = [:]
291
292         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
293
294         String nssiId = "5G-999"
295         currentNSSI.put("nssiId", nssiId)
296         ServiceInstance nssi = new ServiceInstance()
297         nssi.setServiceInstanceId(nssiId)
298         nssi.setSliceProfiles(new SliceProfiles())
299
300         currentNSSI.put("nssi", nssi)
301
302         AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
303
304         AAIResourceUri sliceProfileInstanceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer("global-subscriber-id").serviceSubscription("subscription-service-type").
305                 serviceInstance("slice-profile-instance-id"))
306
307         String sliceProfileInstanceId = "slice-profile-instance-id"
308         ServiceInstance sliceProfileInstance = new ServiceInstance()
309         sliceProfileInstance.setServiceInstanceId(sliceProfileInstanceId)
310         sliceProfileInstance.setServiceRole("slice-profile-instance")
311
312         Optional<ServiceInstance> sliceProfileInstanceOpt = Optional.of(sliceProfileInstance)
313
314         when(client.get(ServiceInstance.class, sliceProfileInstanceUri)).thenReturn(sliceProfileInstanceOpt)
315
316         currentNSSI.put("sliceProfileInstanceUri", sliceProfileInstanceUri)
317
318         DoCommonCoreNSSI spy = spy(DoCommonCoreNSSI.class)
319
320         when(spy.getAAIClient()).thenReturn(client)
321
322         String theSNSSAI = "theS-NSSAI"
323         currentNSSI.put("S-NSSAI", theSNSSAI)
324
325         List<SliceProfile> associatedProfiles = nssi.getSliceProfiles().getSliceProfile()
326
327         SliceProfile sliceProfile1 = new SliceProfile()
328         sliceProfile1.setSNssai("snssai1")
329
330         SliceProfile sliceProfile2 = new SliceProfile()
331         sliceProfile2.setSNssai(theSNSSAI)
332
333         SliceProfile sliceProfile3 = new SliceProfile()
334         sliceProfile3.setSNssai("snssai3")
335
336         associatedProfiles.add(sliceProfile1)
337         associatedProfiles.add(sliceProfile2)
338         associatedProfiles.add(sliceProfile3)
339
340         SliceProfiles sps = new SliceProfiles()
341         sps.getSliceProfile().addAll(associatedProfiles)
342         sliceProfileInstance.setSliceProfiles(sps)
343
344         int sizeBefore = associatedProfiles.size()
345
346         doNothing().when(client).update(sliceProfileInstanceUri, sliceProfileInstance)
347
348         doNothing().when(client). disconnect(nssiUri, sliceProfileInstanceUri)
349
350         spy.removeSPAssociationWithNSSI(mockExecution)
351
352         assertTrue("Association between slice profile and NSSI wasn't removed", ((ServiceInstance)currentNSSI.get("sliceProfileInstance")).getSliceProfiles().getSliceProfile().size() == (sizeBefore - 1))
353     }
354
355
356     @Test
357     void testDeleteSliceProfileInstance() {
358         def currentNSSI = [:]
359
360         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
361
362         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer("global-subscriber-id").serviceSubscription("subscription-service-type").
363                 serviceInstance("slice-profile-instance-id"))
364
365         currentNSSI.put("sliceProfileInstanceUri", uri)
366
367         DoCommonCoreNSSI spy = spy(DoCommonCoreNSSI.class)
368
369         when(spy.getAAIClient()).thenReturn(client)
370
371         doNothing().when(client).delete(uri)
372
373         spy.deleteSliceProfileInstance(mockExecution)
374
375     }
376
377
378     @Test
379     void testPrepareRequestDetails() {
380         def currentNSSI = [:]
381
382         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
383
384         ServiceInstance networkServiceInstance = new ServiceInstance()
385         networkServiceInstance.setServiceInstanceId("NS-777")
386         networkServiceInstance.setServiceRole("Network Service")
387         networkServiceInstance.setModelInvariantId("model-invariant-id")
388         networkServiceInstance.setServiceInstanceName("service-instance-name")
389
390         ServiceInstance nssi = new ServiceInstance()
391         nssi.setServiceInstanceId("5G-999")
392         nssi.setOrchestrationStatus("orchestration-status")
393
394         AAIResourceUri networkServiceInstanceUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(networkServiceInstance.getServiceInstanceId()))
395
396         AAIResourceUri cloudRegionAAIUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion("cloud-owner", "cloud-region-id"))
397
398         currentNSSI.put("networkServiceInstanceUri", networkServiceInstanceUri)
399
400         currentNSSI.put("networkServiceInstance", networkServiceInstance)
401
402         currentNSSI.put("globalSubscriberId", "globalSubscriberId")
403
404         currentNSSI.put("subscriberName", "subscriber-name")
405
406         currentNSSI.put("serviceId", "service-id")
407
408         currentNSSI.put("nssi", nssi)
409
410         List<SliceProfile> associatedProfiles = new ArrayList<>()
411         SliceProfile sliceProfile1 = new SliceProfile()
412         sliceProfile1.setSNssai("snssai1")
413
414         SliceProfile sliceProfile2 = new SliceProfile()
415         sliceProfile2.setSNssai("snssai2")
416
417         associatedProfiles.add(sliceProfile1)
418         associatedProfiles.add(sliceProfile2)
419
420         List<String> snssais = new ArrayList<>()
421         snssais.add(sliceProfile1.getSNssai())
422         snssais.add(sliceProfile2.getSNssai())
423
424         currentNSSI.put("S-NSSAIs", snssais)
425
426
427         ServiceSubscription serviceSubscription = new ServiceSubscription()
428         serviceSubscription.setServiceType("service-type")
429
430         currentNSSI.put("serviceSubscription", serviceSubscription)
431
432         GenericVnf genericVnf = new GenericVnf()
433         genericVnf.setServiceId("service-id")
434         genericVnf.setVnfName("vnf-name")
435         genericVnf.setModelInvariantId("model-invariant-id")
436         genericVnf.setModelCustomizationId("model-customization-id")
437         genericVnf.setVnfName("vnf-name")
438         genericVnf.setVnfId("vnf-id")
439
440         VfModule vfModule = new VfModule()
441         vfModule.setModelInvariantId("model-invariant-id")
442         vfModule.setModelCustomizationId("model-customization-id")
443         vfModule.setModelVersionId("model-version-id")
444         vfModule.setVfModuleName("vf-module-name")
445
446         VfModules vfModules = new VfModules()
447         vfModules.getVfModule().add(vfModule)
448         genericVnf.setVfModules(vfModules)
449
450         currentNSSI.put("constituteVnf", genericVnf)
451
452         AAIResourceUri constituteVNFURI = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf(genericVnf.getVnfId()))
453
454         currentNSSI.put("constituteVnfUri", constituteVNFURI)
455
456         DoCommonCoreNSSI spy = spy(DoCommonCoreNSSI.class)
457
458         when(spy.getAAIClient()).thenReturn(client)
459
460         prepareModelVer(networkServiceInstance)
461
462         //prepareSubscriberInfo(networkServiceInstanceUri)
463
464         prepareCloudConfiguration(constituteVNFURI, cloudRegionAAIUri)
465
466         prepareModelVer(genericVnf)
467
468         prepareModelVer(vfModule)
469
470         prepareOwningEntity(networkServiceInstanceUri)
471
472         prepareProject(cloudRegionAAIUri)
473
474         String prepareRequestDetailsResponse = spy.prepareRequestDetails(mockExecution)
475
476         JsonUtils jsonUtil = new JsonUtils()
477         String errorCode = jsonUtil.getJsonValue(prepareRequestDetailsResponse, "errorCode")
478         String errMsg = jsonUtil.getJsonValue(prepareRequestDetailsResponse, "errorMessage")
479
480         assertTrue(errMsg, errorCode == null || errorCode.isEmpty())
481     }
482
483
484     @Test
485     void testPrepareFailedOperationStatusUpdate() {
486         def currentNSSI = [:]
487
488         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
489         when(mockExecution.getVariable("jobId")).thenReturn("job-id")
490         when(mockExecution.getVariable("operationType")).thenReturn("operation-type")
491
492         String nssiId = "5G-999"
493         String nsiId = "5G-777"
494
495         currentNSSI.put("nssiId", nssiId)
496         currentNSSI.put("nsiId", nsiId)
497         currentNSSI.put("e2eServiceInstanceId", "e2eServiceInstanceId")
498         currentNSSI.put("operationId", "operationId")
499         currentNSSI.put("operationType", "operationType")
500
501         DoCommonCoreNSSI spy = spy(DoCommonCoreNSSI.class)
502
503         spy.prepareFailedOperationStatusUpdate(mockExecution)
504     }
505
506
507     @Test
508     void testPrepareUpdateResourceOperationStatus() {
509         def currentNSSI = [:]
510
511         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
512         when(mockExecution.getVariable("jobId")).thenReturn("job-id")
513         when(mockExecution.getVariable("operationType")).thenReturn("operation-type")
514
515         String nssiId = "5G-999"
516         String nsiId = "5G-777"
517
518         currentNSSI.put("nssiId", nssiId)
519         currentNSSI.put("nsiId", nsiId)
520         currentNSSI.put("e2eServiceInstanceId", "e2eServiceInstanceId")
521         currentNSSI.put("operationId", "operationId")
522         currentNSSI.put("operationType", "operationType")
523
524         DoCommonCoreNSSI spy = spy(DoCommonCoreNSSI.class)
525
526         spy.prepareUpdateResourceOperationStatus(mockExecution)
527
528     }
529
530
531     @Test
532     void testGetPUTServiceInstanceProgressAcknowledged() {
533
534         executePUTServiceInstanceProgress("ACKNOWLEDGED")
535         Mockito.verify(mockExecution,times(1)).setVariable("putStatus", "processing")
536     }
537
538
539     @Test
540     void testGetPUTServiceInstanceProgressInProgress() {
541
542         executePUTServiceInstanceProgress("INPROGRESS")
543         Mockito.verify(mockExecution,times(1)).setVariable("putStatus", "processing")
544     }
545
546
547     @Test
548     void testGetPUTServiceInstanceProgressCompleted() {
549
550         executePUTServiceInstanceProgress("COMPLETED")
551         Mockito.verify(mockExecution,times(1)).setVariable("putStatus", "completed")
552     }
553
554
555     @Test
556     void testTimeDelay() {
557         DoCommonCoreNSSI obj = spy(DoCommonCoreNSSI.class)
558
559         long before = Instant.now().toEpochMilli()
560         obj.timeDelay(mockExecution)
561
562         long after = Instant.now().toEpochMilli()
563
564         long delay = 5L
565
566         assertTrue(String.format("Didn't wait %d sec", delay), ((after - before) >= delay))
567     }
568
569
570     void executePUTServiceInstanceProgress(String state) {
571
572         def currentNSSI = [:]
573
574         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
575
576         String url = "http://mso.onap:8088/serviceInstantiation/v7/serviceInstances/5G-777"
577
578         currentNSSI.put("putServiceInstanceURL", url)
579
580         DoCommonCoreNSSI spy = spy(DoCommonCoreNSSI.class)
581
582         ExternalAPIUtilFactory externalAPIUtilFactoryMock = mock(ExternalAPIUtilFactory.class)
583         when(spy.getExternalAPIUtilFactory()).thenReturn(externalAPIUtilFactoryMock)
584
585         ExternalAPIUtil externalAPIUtilMock = mock(ExternalAPIUtil.class)
586
587         when(externalAPIUtilFactoryMock.create()).thenReturn(externalAPIUtilMock)
588
589         Response responseMock = mock(Response.class)
590         when(externalAPIUtilMock.executeExternalAPIGetCall(mockExecution, url)).thenReturn(responseMock)
591
592         when(responseMock.getStatus()).thenReturn(200)
593
594         String entity = "{\"state\":\"ACCEPTED\",\"orderItem\":[{\"service\":{\"id\":\"5G-999\"},\"state\":\"${state}\"}]}"
595         when(responseMock.readEntity(String.class)).thenReturn(entity)
596
597         spy.getPUTServiceInstanceProgress(mockExecution)
598
599     }
600
601
602     void prepareProject(AAIResourceUri cloudRegionAAIUri) {
603         Project project = new Project()
604         project.setProjectName("project-name")
605
606         AAIResultWrapper wrapperMock = mock(AAIResultWrapper.class)
607         Relationships rsMock = mock(Relationships.class)
608         Optional<Relationships> orsMock = Optional.of(rsMock)
609
610         when(client.get(cloudRegionAAIUri)).thenReturn(wrapperMock)
611         when(wrapperMock.getRelationships()).thenReturn(orsMock)
612
613         List<AAIResourceUri> arus = new ArrayList<>()
614         AAIResourceUri aru = new AAISimpleUri(cloudRegionAAIUri)
615         arus.add(aru)
616
617         when(rsMock.getRelatedUris(Types.PROJECT)).thenReturn(arus)
618
619         Optional<Project> projectOpt = Optional.of(project)
620
621         when(client.get(Project.class, aru)).thenReturn(projectOpt)
622     }
623
624
625     void prepareOwningEntity(AAIResourceUri networkServiceInstanceUri) {
626         OwningEntity owningEntity = new OwningEntity()
627
628         owningEntity.setOwningEntityId("owning-entity-id")
629         owningEntity.setOwningEntityName("owning-entity-name")
630
631         AAIResultWrapper wrapperMock = mock(AAIResultWrapper.class)
632
633         Relationships rsMock = mock(Relationships.class)
634         Optional<Relationships> orsMock = Optional.of(rsMock)
635
636         when(client.get(networkServiceInstanceUri)).thenReturn(wrapperMock)
637         when(wrapperMock.getRelationships()).thenReturn(orsMock)
638
639         List<AAIResourceUri> arus = new ArrayList<>()
640         AAIResourceUri aru = new AAISimpleUri(networkServiceInstanceUri)
641         arus.add(aru)
642
643         when(rsMock.getRelatedUris(Types.OWNING_ENTITY)).thenReturn(arus)
644
645         Optional<OwningEntity> owningEntityOpt = Optional.of(owningEntity)
646
647         when(client.get(OwningEntity.class, aru)).thenReturn(owningEntityOpt)
648     }
649
650
651
652     void prepareCloudConfiguration(AAIResourceUri constituteVNFURI, cloudRegionAAIUri) {
653         AAIResultWrapper wrapperMock = mock(AAIResultWrapper.class)
654
655         Relationships rsMock = mock(Relationships.class)
656         Optional<Relationships> orsMock = Optional.of(rsMock)
657
658         when(client.get(constituteVNFURI)).thenReturn(wrapperMock)
659         when(wrapperMock.getRelationships()).thenReturn(orsMock)
660
661         List<AAIResourceUri> arus = new ArrayList<>()
662         AAIResourceUri aru = new AAISimpleUri(cloudRegionAAIUri)
663         arus.add(aru)
664
665         when(rsMock.getRelatedUris(Types.CLOUD_REGION)).thenReturn(arus)
666
667         CloudRegion cloudRegion = new CloudRegion()
668         cloudRegion.setCloudRegionId("cloud-region-id")
669         cloudRegion.setCloudOwner("cloud-owner")
670         Tenant tenant = new Tenant()
671         tenant.setTenantId("tenant-id")
672
673         Tenants tenants = new Tenants()
674         tenants.getTenant().add(tenant)
675         cloudRegion.setTenants(tenants)
676         Optional<CloudRegion> cloudRegionOpt = Optional.of(cloudRegion)
677
678         when(client.get(CloudRegion.class, aru)).thenReturn(cloudRegionOpt)
679     }
680
681
682     void prepareSubscriberInfo( AAIResourceUri networkServiceInstanceUri) {
683         AAIResultWrapper wrapperMock = mock(AAIResultWrapper.class)
684
685         Relationships rsMock = mock(Relationships.class)
686         Optional<Relationships> orsMock = Optional.of(rsMock)
687
688         when(client.get(networkServiceInstanceUri)).thenReturn(wrapperMock)
689         when(wrapperMock.getRelationships()).thenReturn(orsMock)
690
691         AAIResourceUri serviceSubscriptionUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer("global-customer-id").serviceSubscription("service-type"))
692
693         AAIResourceUri customerUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer("global-customer-id"))
694         List<AAIResourceUri> arus = new ArrayList<>()
695
696         arus.add(serviceSubscriptionUri)
697
698         when(rsMock.getRelatedUris(Types.SERVICE_SUBSCRIPTION)).thenReturn(arus)
699
700         ServiceSubscription serviceSubscription = new ServiceSubscription()
701         serviceSubscription.setServiceType("service-type")
702         Optional<ServiceSubscription> serviceSubscriptionOpt = Optional.of(serviceSubscription)
703
704         when(client.get(ServiceSubscription.class, serviceSubscriptionUri)).thenReturn(serviceSubscriptionOpt)
705
706         when(client.get(networkServiceInstanceUri)).thenReturn(wrapperMock)
707
708         when(rsMock.getRelatedUris(Types.CUSTOMER)).thenReturn(arus)
709
710         Customer customer = new Customer()
711         customer.setSubscriberName("subscriber-name")
712         Optional<Customer> customerOpt = Optional.of(customer)
713
714         when(client.get(Customer.class, customerUri)).thenReturn(customerOpt)
715     }
716
717
718     void prepareModelVer(ServiceInstance networkServiceInstance) {
719         ModelVer modelVer = new ModelVer()
720         modelVer.setModelVersionId("model-version-id")
721         modelVer.setModelName("model-name")
722         modelVer.setModelVersion("model-version")
723
724         Optional<ModelVer> modelVerOpt = Optional.of(modelVer)
725
726         AAIResourceUri modelVerUrl = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.serviceDesignAndCreation().model(networkServiceInstance.getModelInvariantId()).modelVer(networkServiceInstance.getModelVersionId()))
727         when(client.get(ModelVer.class, modelVerUrl)).thenReturn(modelVerOpt)
728     }
729
730     void prepareModelVer(GenericVnf genericVnf) {
731         ModelVer modelVer = new ModelVer()
732         modelVer.setModelVersionId("model-version-id")
733         modelVer.setModelName("model-name")
734         modelVer.setModelVersion("model-version")
735
736         Optional<ModelVer> modelVerOpt = Optional.of(modelVer)
737
738         AAIResourceUri modelVerUrl = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.serviceDesignAndCreation().model(genericVnf.getModelInvariantId()).modelVer(genericVnf.getModelVersionId()))
739         when(client.get(ModelVer.class, modelVerUrl)).thenReturn(modelVerOpt)
740     }
741
742     void prepareModelVer(VfModule vfModule) {
743         ModelVer modelVer = new ModelVer()
744         modelVer.setModelVersionId("model-version-id")
745         modelVer.setModelName("model-name")
746         modelVer.setModelVersion("model-version")
747
748         Optional<ModelVer> modelVerOpt = Optional.of(modelVer)
749
750         AAIResourceUri modelVerUrl = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.serviceDesignAndCreation().model(vfModule.getModelInvariantId()).modelVer(vfModule.getModelVersionId()))
751         when(client.get(ModelVer.class, modelVerUrl)).thenReturn(modelVerOpt)
752     }
753 }