48b96fb60ef5fb3f0fd9b728dc7d3a2810db2248
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020  TIM
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.camunda.bpm.engine.ProcessEngineServices
25 import org.camunda.bpm.engine.RepositoryService
26 import org.camunda.bpm.engine.impl.cmmn.execution.CaseExecutionImpl
27 import org.camunda.bpm.engine.impl.core.variable.scope.AbstractVariableScope
28 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
29 import org.camunda.bpm.engine.repository.ProcessDefinition
30 import org.junit.Rule
31 import org.junit.rules.ExpectedException
32 import org.mockito.invocation.InvocationOnMock
33 import org.mockito.stubbing.Answer
34 import org.onap.aai.domain.yang.CloudRegion
35 import org.onap.aai.domain.yang.Customer
36 import org.onap.aai.domain.yang.GenericVnf
37 import org.onap.aai.domain.yang.ModelVer
38 import org.onap.aai.domain.yang.OwningEntity
39 import org.onap.aai.domain.yang.Project
40 import org.onap.aai.domain.yang.Relationship
41 import org.onap.aai.domain.yang.RelationshipList
42 import org.onap.aai.domain.yang.ServiceSubscription
43 import org.onap.aai.domain.yang.SliceProfile
44 import org.onap.aai.domain.yang.SliceProfiles
45 import org.onap.aai.domain.yang.Tenant
46 import org.onap.aai.domain.yang.Tenants
47 import org.onap.aai.domain.yang.VfModule
48 import org.onap.aai.domain.yang.VfModules
49 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
50 import org.onap.aaiclient.client.aai.entities.Relationships
51 import org.onap.aaiclient.client.aai.entities.uri.AAISimpleUri
52 import org.onap.aaiclient.client.aai.entities.uri.ServiceInstanceUri
53 import org.onap.logging.filter.base.ONAPComponents
54 import org.onap.so.beans.nsmf.NssiResponse
55 import org.onap.so.bpmn.common.scripts.ExceptionUtil
56 import org.onap.so.bpmn.common.scripts.MsoUtils
57 import org.onap.so.bpmn.mock.FileUtil
58 import org.onap.so.client.HttpClient
59 import org.onap.so.client.HttpClientFactory
60 import org.onap.so.serviceinstancebeans.RequestDetails
61 import org.onap.so.utils.CryptoUtils
62
63 import javax.ws.rs.core.Response
64
65 import static org.junit.Assert.*;
66 import org.junit.Before
67 import org.junit.Test
68 import org.mockito.Mockito
69 import org.onap.aai.domain.yang.ServiceInstance
70 import org.onap.aaiclient.client.aai.AAIObjectType
71 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
72 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
73 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
74
75 import static org.mockito.ArgumentMatchers.anyDouble
76 import static org.mockito.ArgumentMatchers.anyInt
77 import static org.mockito.ArgumentMatchers.anyString
78 import static org.mockito.ArgumentMatchers.eq
79 import static org.mockito.Mockito.doNothing
80 import static org.mockito.Mockito.doReturn
81 import static org.mockito.Mockito.doThrow
82 import static org.mockito.Mockito.mock
83 import static org.mockito.Mockito.mock
84 import static org.mockito.Mockito.mock
85 import static org.mockito.Mockito.spy
86 import static org.mockito.Mockito.times
87 import static org.mockito.Mockito.verify
88 import static org.mockito.Mockito.when
89
90 class DoDeallocateCoreNSSITest extends MsoGroovyTest {
91
92     @Before
93     void init() throws IOException {
94         super.init("DoDeallocateNSSITest")
95     }
96
97
98     @Test
99     void testPreProcessRequest() {
100         def currentNSSI = [:]
101         currentNSSI.put("nssiId","5G-999")
102         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
103
104         DoDeallocateCoreNSSI dcnssi = new DoDeallocateCoreNSSI()
105         dcnssi.preProcessRequest(mockExecution)
106         Mockito.verify(mockExecution,times(1)).getVariable("currentNSSI")
107     }
108
109
110     @Test
111     void testExecuteTerminateNSSIQuery() {
112         HttpClientFactory httpClientFactoryMock
113         HttpClient httpClientMock
114
115         def currentNSSI = [:]
116         currentNSSI.put("nssiId","5G-999")
117
118         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
119
120         when(mockExecution.getVariable("mso.oof.endpoint")).thenReturn("http://oof.onap:8088")
121         when(mockExecution.getVariable("mso.oof.auth")).thenReturn("mso.oof.auth")
122         when(mockExecution.getVariable("mso.msoKey")).thenReturn("mso.msoKey")
123         when(mockExecution.getVariable("mso-request-id")).thenReturn("mso-request-id")
124
125         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
126         when(spy.getAAIClient()).thenReturn(client)
127
128         when(spy.encryptBasicAuth("mso.oof.auth", "mso.msoKey")).thenReturn("auth-value")
129
130         String authHeaderResponse =  "auth-header"
131
132       /*  String authHeaderResponse =  "{\n" +
133                 " \"errorCode\": \"401\",\n" +
134                 " \"errorMessage\": \"Bad request\"\n" +
135                 "}" */
136
137         when(spy.getAuthHeader(mockExecution, "auth-value", "mso.msoKey")).thenReturn(authHeaderResponse)
138
139         String urlString = "http://oof.onap:8088"
140
141         String httpRequest =    "{\n" +
142                 "  \"type\": \"NSSI\",\n" +
143                 "  \"NxIId\": \"5G-999\",\n" +
144                 "  \"requestInfo\": {\n" +
145                 "    \"transactionId\": \"mso-request-id\",\n" +
146                 "    \"requestId\": \"mso-request-id\",\n" +
147                 "    \"sourceId\": \"so\",\n" +
148                 "    }\n" +
149                 "}"
150
151         boolean terminateResponse = true
152
153         String oofResponse =   "{\n" +
154                 " \"requestId\": \"mso-request-id\",\n" +
155                 " \"transactionId\": \"mso-request-id\",\n" +
156                 " \"statusMessage\": \"\",\n" +
157                 " \"requestStatus\": \"accepted\",\n" +
158                 " \"terminateResponse\": \"${terminateResponse}\",\n" +
159                 " \"reason\": \"\"\n" +
160                 " }\n"
161
162         String oofCallResponse = oofResponse
163
164       /*  String oofCallResponse =  "{\n" +
165                 " \"errorCode\": \"401\",\n" +
166                 " \"errorMessage\": \"Exception during the call\"\n" +
167                 "}" */
168
169         when(spy.callOOF(urlString, "auth-header", httpRequest)).thenReturn(oofCallResponse)
170
171         spy.executeTerminateNSSIQuery(mockExecution)
172
173         verify(mockExecution).setVariable("isTerminateNSSI", terminateResponse)
174
175     }
176
177
178     @Test
179     void testGetNetworkServiceInstance() {
180         def currentNSSI = [:]
181         currentNSSI.put("nssiId","5G-999")
182
183         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
184
185         AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "5G-999")
186         AAIResourceUri networkServiceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "NS-777")
187
188         ServiceInstance nssi = new ServiceInstance()
189         nssi.setServiceInstanceId("5G-999")
190
191         ServiceInstance networkServiceInstance = new ServiceInstance()
192         networkServiceInstance.setServiceInstanceId("NS-777")
193         networkServiceInstance.setServiceRole("Network Service")
194
195         Optional<ServiceInstance> nssiOpt = Optional.of(nssi)
196         Optional<ServiceInstance> networkServiceInstaneOpt = Optional.of(networkServiceInstance)
197
198         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
199         when(spy.getAAIClient()).thenReturn(client)
200
201         when(client.get(ServiceInstance.class, nssiUri)).thenReturn(nssiOpt)
202
203         //String json = FileUtil.readResourceFile("__files/AAI/ServiceInstanceWithAR.json")
204         AAIResultWrapper wrapperMock = mock(AAIResultWrapper.class) //new AAIResultWrapper(json)
205         Relationships rsMock = mock(Relationships.class)
206         Optional<Relationships> orsMock = Optional.of(rsMock)
207         List<AAIResourceUri> arus = new ArrayList<>()
208         AAIResourceUri aru = new ServiceInstanceUri(networkServiceInstanceUri)
209         arus.add(aru)
210
211         when(client.get(nssiUri)).thenReturn(wrapperMock)
212         when(wrapperMock.getRelationships()).thenReturn(orsMock)
213
214         when(rsMock.getRelatedAAIUris(AAIObjectType.SERVICE_INSTANCE)).thenReturn(arus)
215         when(client.get(ServiceInstance.class, aru)).thenReturn(networkServiceInstaneOpt)
216
217         spy.getNetworkServiceInstance(mockExecution)
218
219         assertTrue("Either NSSI doesn't exist or unexpected NSSI Service Instance ID",
220                     currentNSSI.get("nssi") != null && ((ServiceInstance)currentNSSI.get("nssi")).getServiceInstanceId().equals(nssi.getServiceInstanceId()))
221
222         assertTrue("Either Network Service Instance doesn't exist or unexpected Network Service Instance ID",
223                 currentNSSI.get("networkServiceInstance") != null && ((ServiceInstance)currentNSSI.get("networkServiceInstance")).getServiceInstanceId().equals(networkServiceInstance.getServiceInstanceId()))
224
225         assertNotNull("networkServiceInstanceUri doesn't exist", currentNSSI.get("networkServiceInstanceUri"))
226     }
227
228
229     @Test
230     void testDeleteServiceOrder() {
231         def currentNSSI = [:]
232         currentNSSI.put("nssiId","5G-999")
233
234         ServiceInstance networkServiceInstance = new ServiceInstance()
235         networkServiceInstance.setServiceInstanceId("NS-777")
236         networkServiceInstance.setServiceRole("Network Service")
237
238         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
239
240         currentNSSI.put("networkServiceInstance", networkServiceInstance)
241
242         when(mockExecution.getVariable("nbi.endpoint.url")).thenReturn("http://nbi.onap:8088")
243         when(mockExecution.getVariable("mso.msoKey")).thenReturn("mso.msoKey")
244         when(mockExecution.getVariable("mso.infra.endpoint.auth")).thenReturn("mso.infra.endpoint.auth")
245
246         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
247         when(spy.getAAIClient()).thenReturn(client)
248
249         when(spy.encryptBasicAuth("mso.infra.endpoint.auth", "mso.msoKey")).thenReturn("auth-value")
250
251         String authHeaderResponse =  "auth-header"
252
253         /*  String authHeaderResponse =  "{\n" +
254                   " \"errorCode\": \"401\",\n" +
255                   " \"errorMessage\": \"Bad request\"\n" +
256                   "}" */
257
258         when(spy.getAuthHeader(mockExecution, "auth-value", "mso.msoKey")).thenReturn(authHeaderResponse)
259
260         String urlString = String.format("http://nbi.onap:8088/api/v4/serviceOrder/%s", networkServiceInstance.getServiceInstanceId())
261
262         String callDeleteServiceOrderResponse = "deleted"
263
264         when(spy.callDeleteServiceOrder(mockExecution, urlString, "auth-header")).thenReturn(callDeleteServiceOrderResponse)
265
266         spy.deleteServiceOrder(mockExecution)
267     }
268
269
270
271     @Test
272     void getConstituteVNFFromNetworkServiceInst() {
273         def currentNSSI = [:]
274
275         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
276
277         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
278         when(spy.getAAIClient()).thenReturn(client)
279
280         ServiceInstance networkServiceInstance = new ServiceInstance()
281         networkServiceInstance.setServiceInstanceId("NS-777")
282         networkServiceInstance.setServiceRole("Network Service")
283
284         GenericVnf genericVNF = new GenericVnf()
285         genericVNF.setVnfId("VNF-1")
286
287         AAIResourceUri networkServiceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, networkServiceInstance.getServiceInstanceId())
288
289         Optional<GenericVnf> genericVnfOpt = Optional.of(genericVNF)
290         AAIResourceUri genericVNFUri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, genericVNF.getVnfId())
291
292         currentNSSI.put("networkServiceInstanceUri", networkServiceInstanceUri)
293         currentNSSI.put("networkServiceInstance", networkServiceInstance)
294
295         AAIResultWrapper wrapperMock = mock(AAIResultWrapper.class) //new AAIResultWrapper(json)
296         Relationships rsMock = mock(Relationships.class)
297         Optional<Relationships> orsMock = Optional.of(rsMock)
298         List<AAIResourceUri> arus = new ArrayList<>()
299         AAIResourceUri aru = new AAISimpleUri(genericVNFUri)
300         arus.add(aru)
301
302         when(client.get(networkServiceInstanceUri)).thenReturn(wrapperMock)
303         when(wrapperMock.getRelationships()).thenReturn(orsMock)
304
305         when(rsMock.getRelatedAAIUris(AAIObjectType.GENERIC_VNF)).thenReturn(arus)
306         when(client.get(GenericVnf.class, genericVNFUri)).thenReturn(genericVnfOpt)
307
308         spy.getConstituteVNFFromNetworkServiceInst(mockExecution)
309
310         assertNotNull("constituteVnfUri doesn't exist", currentNSSI.get("constituteVnfUri"))
311
312         assertTrue("Either Constitute VNF doesn't exist or unexpected VNF ID",
313                 currentNSSI.get("constituteVnf") != null && ((GenericVnf)currentNSSI.get("constituteVnf")).getVnfId().equals(genericVNF.getVnfId()))
314     }
315
316
317     @Test
318     void testGetNSSIAssociatedProfiles() {
319         def currentNSSI = [:]
320         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
321
322         ServiceInstance nssi = new ServiceInstance()
323         nssi.setServiceInstanceId("5G-999")
324
325         SliceProfiles sliceProfiles = new SliceProfiles()
326
327         List<SliceProfile> slProfiles = sliceProfiles.getSliceProfile()
328         slProfiles.add(new SliceProfile())
329         slProfiles.add(new SliceProfile())
330
331         nssi.setSliceProfiles(sliceProfiles)
332         currentNSSI.put("nssi", nssi)
333
334         DoDeallocateCoreNSSI obj = new DoDeallocateCoreNSSI()
335         obj.getNSSIAssociatedProfiles(mockExecution)
336
337         List<SliceProfile> associatedProfiles = (List<SliceProfile>)currentNSSI.get("associatedProfiles")
338         assertTrue("Either associatedProfiles doesn't exist or size is incorrect", (associatedProfiles != null && associatedProfiles.size() == 2))
339     }
340
341
342     @Test
343     void testCalculateSNSSAI() {
344         def currentNSSI = [:]
345         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
346
347         String theSNSSAI = "theS-NSSAI"
348
349         currentNSSI.put("S-NSSAI", theSNSSAI)
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(theSNSSAI)
357
358         SliceProfile sliceProfile3 = new SliceProfile()
359         sliceProfile3.setSNssai("snssai3")
360
361         associatedProfiles.add(sliceProfile1)
362         associatedProfiles.add(sliceProfile2)
363         associatedProfiles.add(sliceProfile3)
364
365         int sizeBefore = associatedProfiles.size()
366
367         currentNSSI.put("associatedProfiles", associatedProfiles)
368
369         DoDeallocateCoreNSSI obj = new DoDeallocateCoreNSSI()
370         obj.calculateSNSSAI(mockExecution)
371
372         List<SliceProfile> snssais = (List<SliceProfile>)currentNSSI.get("S-NSSAIs")
373         SliceProfile sliceProfileContainsSNSSAI = (SliceProfile)currentNSSI.get("sliceProfileS-NSSAI")
374
375         assertTrue("Either snssais doesn't exist or size is incorrect", (snssais != null && snssais.size() == (sizeBefore - 1)))
376         assertNotNull("Slice Profile which contains given S-NSSAI not found", sliceProfileContainsSNSSAI)
377         assertTrue("Wrong Slice Profile", sliceProfileContainsSNSSAI.getSNssai().equals(theSNSSAI))
378     }
379
380
381     @Test
382     void testInvokePUTServiceInstance() {
383         def currentNSSI = [:]
384
385         ServiceInstance networkServiceInstance = new ServiceInstance()
386         networkServiceInstance.setServiceInstanceId("NS-777")
387         networkServiceInstance.setServiceRole("Network Service")
388
389         GenericVnf constituteVnf = new GenericVnf()
390         constituteVnf.setVnfId("VNF-1")
391
392         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
393
394         currentNSSI.put("networkServiceInstance", networkServiceInstance)
395         currentNSSI.put("constituteVnf", constituteVnf)
396
397         when(mockExecution.getVariable("mso.infra.endpoint.url")).thenReturn("http://mso.onap:8088")
398         when(mockExecution.getVariable("mso.msoKey")).thenReturn("mso.msoKey")
399         when(mockExecution.getVariable("mso.infra.endpoint.auth")).thenReturn("mso.infra.endpoint.auth")
400
401         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
402         when(spy.getAAIClient()).thenReturn(client)
403
404         when(spy.encryptBasicAuth("mso.infra.endpoint.auth", "mso.msoKey")).thenReturn("auth-value")
405
406         String authHeaderResponse =  "auth-header"
407
408         when(spy.getAuthHeader(mockExecution, "auth-value", "mso.msoKey")).thenReturn(authHeaderResponse)
409
410         String urlString = String.format("http://mso.onap:8088/serviceInstantiation/v7/serviceInstances/%s/vnfs/%s", networkServiceInstance.getServiceInstanceId(), constituteVnf.getVnfId())
411
412         String callPUTServiceInstanceResponse = "put"
413
414         RequestDetails requestDetails = new RequestDetails()
415         ObjectMapper mapper = new ObjectMapper()
416         String requestDetailsStr = mapper.writeValueAsString(requestDetails)
417
418         when(spy.prepareRequestDetails(mockExecution)).thenReturn(requestDetailsStr)
419
420         when(spy.callPUTServiceInstance(urlString, "auth-header", requestDetailsStr)).thenReturn(callPUTServiceInstanceResponse)
421
422         spy.invokePUTServiceInstance(mockExecution)
423     }
424
425
426     @Test
427     void testRemoveNSSIAssociationWithNSI() {
428         def currentNSSI = [:]
429
430         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
431
432         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
433
434         when(spy.getAAIClient()).thenReturn(client)
435
436         String nssiId = "5G-999"
437         String nsiId = "5G-99"
438         currentNSSI.put("nssiId", nssiId)
439         currentNSSI.put("nsiId", nsiId)
440
441         AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, nssiId)
442         AAIResourceUri nsiUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, nsiId)
443
444         doNothing().when(client).disconnect(nssiUri, nsiUri)
445
446         spy.removeNSSIAssociationWithNSI(mockExecution)
447
448     }
449
450
451     @Test
452     void testRemoveSPAssociationWithNSSI() {
453         def currentNSSI = [:]
454
455         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
456
457         String nssiId = "5G-999"
458         currentNSSI.put("nssiId", nssiId)
459         ServiceInstance nssi = new ServiceInstance()
460         nssi.setServiceInstanceId(nssiId)
461         nssi.setSliceProfiles(new SliceProfiles())
462
463         currentNSSI.put("nssi", nssi)
464
465         AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, nssiId)
466
467         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
468
469         when(spy.getAAIClient()).thenReturn(client)
470
471         String theSNSSAI = "theS-NSSAI"
472         currentNSSI.put("S-NSSAI", theSNSSAI)
473
474         List<SliceProfile> associatedProfiles = nssi.getSliceProfiles().getSliceProfile()
475
476         SliceProfile sliceProfile1 = new SliceProfile()
477         sliceProfile1.setSNssai("snssai1")
478
479         SliceProfile sliceProfile2 = new SliceProfile()
480         sliceProfile2.setSNssai(theSNSSAI)
481
482         SliceProfile sliceProfile3 = new SliceProfile()
483         sliceProfile3.setSNssai("snssai3")
484
485         associatedProfiles.add(sliceProfile1)
486         associatedProfiles.add(sliceProfile2)
487         associatedProfiles.add(sliceProfile3)
488
489         int sizeBefore = associatedProfiles.size()
490
491         doNothing().when(client).update(nssiUri, nssi)
492
493         spy.removeSPAssociationWithNSSI(mockExecution)
494
495         assertTrue("Association between slice profile and NSSI wasn't removed", ((ServiceInstance)currentNSSI.get("nssi")).getSliceProfiles().getSliceProfile().size() == (sizeBefore - 1))
496     }
497
498
499     @Test
500     void testDeleteSliceProfileInstance() {
501         def currentNSSI = [:]
502
503         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
504
505         String globalSubscriberId = "global-id"
506         String serviceType = "service"
507         String nssiId = "5G-999"
508
509         currentNSSI.put("globalSubscriberId", nssiId)
510         currentNSSI.put("serviceType", nssiId)
511         currentNSSI.put("nssiId", nssiId)
512
513         String theSNSSAI = "theS-NSSAI"
514
515         SliceProfile sliceProfile = new SliceProfile()
516         sliceProfile.setSNssai(theSNSSAI)
517         sliceProfile.setProfileId("prof-id")
518
519         AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, nssiId)
520
521         currentNSSI.put("sliceProfileS-NSSAI", sliceProfile)
522
523         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
524
525         when(spy.getAAIClient()).thenReturn(client)
526
527         doNothing().when(client).delete(nssiUri)
528
529         spy.deleteSliceProfileInstance(mockExecution)
530
531     }
532
533
534     @Test
535     void testDeleteNSSIServiceInstance() {
536         def currentNSSI = [:]
537
538         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
539
540         String nssiId = "5G-999"
541
542         currentNSSI.put("nssiId", nssiId)
543
544         AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, nssiId)
545
546         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
547
548         when(spy.getAAIClient()).thenReturn(client)
549
550         doNothing().when(client).delete(nssiUri)
551
552         spy.deleteNSSIServiceInstance(mockExecution)
553     }
554
555
556     @Test
557     void testUpdateServiceOperationStatus() {
558         def currentNSSI = [:]
559
560         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
561
562         String nssiId = "5G-999"
563
564         currentNSSI.put("nssiId", nssiId)
565         currentNSSI.put("e2eServiceInstanceId", "e2eServiceInstanceId")
566         currentNSSI.put("operationId", "operationId")
567         currentNSSI.put("operationType", "operationType")
568
569         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
570
571         spy.updateServiceOperationStatus(mockExecution)
572
573     }
574
575
576     @Test
577     void testPrepareRequestDetails() {
578         def currentNSSI = [:]
579
580         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
581
582         ServiceInstance networkServiceInstance = new ServiceInstance()
583         networkServiceInstance.setServiceInstanceId("NS-777")
584         networkServiceInstance.setServiceRole("Network Service")
585         networkServiceInstance.setModelInvariantId("model-invariant-id")
586         networkServiceInstance.setServiceInstanceName("service-instance-name")
587
588         ServiceInstance nssi = new ServiceInstance()
589         nssi.setServiceInstanceId("5G-999")
590         nssi.setOrchestrationStatus("orchestration-status")
591
592         AAIResourceUri networkServiceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "networkServiceInstance.getServiceInstanceId()")
593
594         AAIResourceUri cloudRegionAAIUri = AAIUriFactory.createResourceUri(AAIObjectType.CLOUD_REGION, "cloud-owner", "cloud-region-id")
595
596         currentNSSI.put("networkServiceInstanceUri", networkServiceInstanceUri)
597
598         currentNSSI.put("networkServiceInstance", networkServiceInstance)
599
600         currentNSSI.put("globalSubscriberId", "globalSubscriberId")
601
602         currentNSSI.put("subscriberName", "subscriber-name")
603
604         currentNSSI.put("serviceId", "service-id")
605
606         currentNSSI.put("nssi", nssi)
607
608         List<SliceProfile> associatedProfiles = new ArrayList<>()
609         SliceProfile sliceProfile1 = new SliceProfile()
610         sliceProfile1.setSNssai("snssai1")
611
612         SliceProfile sliceProfile2 = new SliceProfile()
613         sliceProfile2.setSNssai("snssai2")
614
615         associatedProfiles.add(sliceProfile1)
616         associatedProfiles.add(sliceProfile2)
617
618         List<String> snssais = new ArrayList<>()
619         snssais.add(sliceProfile1.getSNssai())
620         snssais.add(sliceProfile2.getSNssai())
621
622         currentNSSI.put("S-NSSAIs", snssais)
623
624
625         ServiceSubscription serviceSubscription = new ServiceSubscription()
626         serviceSubscription.setServiceType("service-type")
627
628         currentNSSI.put("serviceSubscription", serviceSubscription)
629
630         GenericVnf genericVnf = new GenericVnf()
631         genericVnf.setServiceId("service-id")
632         genericVnf.setVnfName("vnf-name")
633         genericVnf.setModelInvariantId("model-invariant-id")
634         genericVnf.setModelCustomizationId("model-customization-id")
635         genericVnf.setVnfName("vnf-name")
636         genericVnf.setVnfId("vnf-id")
637
638         VfModule vfModule = new VfModule()
639         vfModule.setModelInvariantId("model-invariant-id")
640         vfModule.setModelCustomizationId("model-customization-id")
641         vfModule.setModelVersionId("model-version-id")
642         vfModule.setVfModuleName("vf-module-name")
643
644         VfModules vfModules = new VfModules()
645         vfModules.getVfModule().add(vfModule)
646         genericVnf.setVfModules(vfModules)
647
648         currentNSSI.put("constituteVnf", genericVnf)
649
650         AAIResourceUri constituteVNFURI = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, genericVnf.getVnfId())
651
652         currentNSSI.put("constituteVnfUri", constituteVNFURI)
653
654         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
655
656         when(spy.getAAIClient()).thenReturn(client)
657
658         prepareModelVer(networkServiceInstance)
659
660         //prepareSubscriberInfo(networkServiceInstanceUri)
661
662         prepareCloudConfiguration(constituteVNFURI, cloudRegionAAIUri)
663
664         prepareModelVer(genericVnf)
665
666         prepareModelVer(vfModule)
667
668         prepareOwningEntity(networkServiceInstanceUri)
669
670         prepareProject(cloudRegionAAIUri)
671
672         String requestDetails = spy.prepareRequestDetails(mockExecution)
673
674     }
675
676
677     void prepareProject(AAIResourceUri cloudRegionAAIUri) {
678         Project project = new Project()
679         project.setProjectName("project-name")
680
681         AAIResultWrapper wrapperMock = mock(AAIResultWrapper.class)
682         Relationships rsMock = mock(Relationships.class)
683         Optional<Relationships> orsMock = Optional.of(rsMock)
684
685         when(client.get(cloudRegionAAIUri)).thenReturn(wrapperMock)
686         when(wrapperMock.getRelationships()).thenReturn(orsMock)
687
688         List<AAIResourceUri> arus = new ArrayList<>()
689         AAIResourceUri aru = new AAISimpleUri(cloudRegionAAIUri)
690         arus.add(aru)
691
692         when(rsMock.getRelatedAAIUris(AAIObjectType.PROJECT)).thenReturn(arus)
693
694         Optional<Project> projectOpt = Optional.of(project)
695
696         when(client.get(Project.class, aru)).thenReturn(projectOpt)
697     }
698
699
700     void prepareOwningEntity(AAIResourceUri networkServiceInstanceUri) {
701         OwningEntity owningEntity = new OwningEntity()
702
703         owningEntity.setOwningEntityId("owning-entity-id")
704         owningEntity.setOwningEntityName("owning-entity-name")
705
706         AAIResultWrapper wrapperMock = mock(AAIResultWrapper.class)
707
708         Relationships rsMock = mock(Relationships.class)
709         Optional<Relationships> orsMock = Optional.of(rsMock)
710
711         when(client.get(networkServiceInstanceUri)).thenReturn(wrapperMock)
712         when(wrapperMock.getRelationships()).thenReturn(orsMock)
713
714         List<AAIResourceUri> arus = new ArrayList<>()
715         AAIResourceUri aru = new AAISimpleUri(networkServiceInstanceUri)
716         arus.add(aru)
717
718         when(rsMock.getRelatedAAIUris(AAIObjectType.OWNING_ENTITY)).thenReturn(arus)
719
720         Optional<OwningEntity> owningEntityOpt = Optional.of(owningEntity)
721
722         when(client.get(OwningEntity.class, aru)).thenReturn(owningEntityOpt)
723     }
724
725
726
727     void prepareCloudConfiguration(AAIResourceUri constituteVNFURI, cloudRegionAAIUri) {
728         AAIResultWrapper wrapperMock = mock(AAIResultWrapper.class)
729
730         Relationships rsMock = mock(Relationships.class)
731         Optional<Relationships> orsMock = Optional.of(rsMock)
732
733         when(client.get(constituteVNFURI)).thenReturn(wrapperMock)
734         when(wrapperMock.getRelationships()).thenReturn(orsMock)
735
736         List<AAIResourceUri> arus = new ArrayList<>()
737         AAIResourceUri aru = new AAISimpleUri(cloudRegionAAIUri)
738         arus.add(aru)
739
740         when(rsMock.getRelatedAAIUris(AAIObjectType.CLOUD_REGION)).thenReturn(arus)
741
742         CloudRegion cloudRegion = new CloudRegion()
743         cloudRegion.setCloudRegionId("cloud-region-id")
744         cloudRegion.setCloudOwner("cloud-owner")
745         Tenant tenant = new Tenant()
746         tenant.setTenantId("tenant-id")
747
748         Tenants tenants = new Tenants()
749         tenants.getTenant().add(tenant)
750         cloudRegion.setTenants(tenants)
751         Optional<CloudRegion> cloudRegionOpt = Optional.of(cloudRegion)
752
753         when(client.get(CloudRegion.class, aru)).thenReturn(cloudRegionOpt)
754     }
755
756
757     void prepareSubscriberInfo( AAIResourceUri networkServiceInstanceUri) {
758         AAIResultWrapper wrapperMock = mock(AAIResultWrapper.class)
759
760         Relationships rsMock = mock(Relationships.class)
761         Optional<Relationships> orsMock = Optional.of(rsMock)
762
763         when(client.get(networkServiceInstanceUri)).thenReturn(wrapperMock)
764         when(wrapperMock.getRelationships()).thenReturn(orsMock)
765
766         AAIResourceUri serviceSubscriptionUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_SUBSCRIPTION, "global-customer-id", "service-type")
767
768         AAIResourceUri customerUri = AAIUriFactory.createResourceUri(AAIObjectType.CUSTOMER, "global-customer-id")
769         List<AAIResourceUri> arus = new ArrayList<>()
770
771         arus.add(serviceSubscriptionUri)
772
773         when(rsMock.getRelatedAAIUris(AAIObjectType.SERVICE_SUBSCRIPTION)).thenReturn(arus)
774
775         ServiceSubscription serviceSubscription = new ServiceSubscription()
776         serviceSubscription.setServiceType("service-type")
777         Optional<ServiceSubscription> serviceSubscriptionOpt = Optional.of(serviceSubscription)
778
779         when(client.get(ServiceSubscription.class, serviceSubscriptionUri)).thenReturn(serviceSubscriptionOpt)
780
781         when(client.get(networkServiceInstanceUri)).thenReturn(wrapperMock)
782
783         when(rsMock.getRelatedAAIUris(AAIObjectType.CUSTOMER)).thenReturn(arus)
784
785         Customer customer = new Customer()
786         customer.setSubscriberName("subscriber-name")
787         Optional<Customer> customerOpt = Optional.of(customer)
788
789         when(client.get(Customer.class, customerUri)).thenReturn(customerOpt)
790     }
791
792
793     void prepareModelVer(ServiceInstance networkServiceInstance) {
794         ModelVer modelVer = new ModelVer()
795         modelVer.setModelVersionId("model-version-id")
796         modelVer.setModelName("model-name")
797         modelVer.setModelVersion("model-version")
798
799         Optional<ModelVer> modelVerOpt = Optional.of(modelVer)
800
801         AAIResourceUri modelVerUrl = AAIUriFactory.createResourceUri(AAIObjectType.MODEL_VER, networkServiceInstance.getModelInvariantId(), networkServiceInstance.getModelVersionId())
802         when(client.get(ModelVer.class, modelVerUrl)).thenReturn(modelVerOpt)
803     }
804
805     void prepareModelVer(GenericVnf genericVnf) {
806         ModelVer modelVer = new ModelVer()
807         modelVer.setModelVersionId("model-version-id")
808         modelVer.setModelName("model-name")
809         modelVer.setModelVersion("model-version")
810
811         Optional<ModelVer> modelVerOpt = Optional.of(modelVer)
812
813         AAIResourceUri modelVerUrl = AAIUriFactory.createResourceUri(AAIObjectType.MODEL_VER, genericVnf.getModelInvariantId(), genericVnf.getModelVersionId())
814         when(client.get(ModelVer.class, modelVerUrl)).thenReturn(modelVerOpt)
815     }
816
817     void prepareModelVer(VfModule vfModule) {
818         ModelVer modelVer = new ModelVer()
819         modelVer.setModelVersionId("model-version-id")
820         modelVer.setModelName("model-name")
821         modelVer.setModelVersion("model-version")
822
823         Optional<ModelVer> modelVerOpt = Optional.of(modelVer)
824
825         AAIResourceUri modelVerUrl = AAIUriFactory.createResourceUri(AAIObjectType.MODEL_VER, vfModule.getModelInvariantId(), vfModule.getModelVersionId())
826         when(client.get(ModelVer.class, modelVerUrl)).thenReturn(modelVerOpt)
827     }
828
829 }