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