Wrong additional parameter for OOF's terminateNxiRequest
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / groovy / org / onap / so / bpmn / infrastructure / scripts / DoDeallocateCoreNSSITest.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.AAIUriFactory
32 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
33 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
34 import org.onap.logging.filter.base.ONAPComponents
35 import org.onap.so.bpmn.common.scripts.ExternalAPIUtil
36 import org.onap.so.bpmn.common.scripts.ExternalAPIUtilFactory
37 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
38 import org.onap.so.bpmn.common.scripts.MsoUtils
39 import org.onap.so.bpmn.common.scripts.OofUtils
40 import org.onap.so.client.HttpClient
41 import org.onap.so.client.HttpClientFactory
42 import org.onap.so.serviceinstancebeans.RequestDetails
43
44 import javax.ws.rs.core.Response
45
46 import static org.junit.Assert.assertNotNull
47 import static org.junit.Assert.assertTrue
48 import static org.mockito.Mockito.*
49
50 class DoDeallocateCoreNSSITest extends MsoGroovyTest {
51
52     @Before
53     void init() throws IOException {
54         super.init("DoDeallocateNSSITest")
55     }
56
57
58     @Test
59     void testPreProcessRequest() {
60
61         String nssiId = "5G-999"
62         when(mockExecution.getVariable("serviceInstanceID")).thenReturn(nssiId)
63
64         String nsiId = "5G-777"
65         when(mockExecution.getVariable("nsiId")).thenReturn(nsiId)
66
67         String snssai = "S-NSSAI"
68         String snssaiList = "[ \"${snssai}\" ]"
69         String sliceProfileId = "slice-profile-id"
70         String modifyAction = "allocate"
71         String sliceParams =  "{\n" +
72                 "\"sliceProfileId\":\"${sliceProfileId}\",\"snssaiList\":${snssaiList}\n" +
73                 "}"
74         when(mockExecution.getVariable("sliceParams")).thenReturn(sliceParams)
75
76         DoDeallocateCoreNSSI obj = new DoDeallocateCoreNSSI()
77         obj.preProcessRequest(mockExecution)
78
79         def currentNSSI = [:]
80         currentNSSI.put("nssiId", nssiId)
81         currentNSSI.put("nsiId", nsiId)
82         currentNSSI.put("sliceProfile", sliceParams)
83         currentNSSI.put("S-NSSAI", snssai)
84         currentNSSI.put("sliceProfileId", sliceProfileId)
85         Mockito.verify(mockExecution,times(1)).setVariable("currentNSSI", currentNSSI)
86
87     }
88
89
90     @Test
91     void testExecuteTerminateNSSIQuery() {
92
93         def currentNSSI = [:]
94
95         String nssiId = "5G-999"
96         currentNSSI.put("nssiId", nssiId)
97
98         String nsiId = "ns-777"
99         currentNSSI.put("nsiId", nsiId)
100
101         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
102
103         when(mockExecution.getVariable("mso.oof.endpoint")).thenReturn("http://oof.onap:8088")
104         when(mockExecution.getVariable("mso.oof.auth")).thenReturn("mso.oof.auth")
105         when(mockExecution.getVariable("mso.msoKey")).thenReturn("mso.msoKey")
106         when(mockExecution.getVariable("mso-request-id")).thenReturn("mso-request-id")
107
108         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
109         when(spy.getAAIClient()).thenReturn(client)
110
111         OofUtils oofUtilsMock = mock(OofUtils.class)
112         when(spy.getOofUtils()).thenReturn(oofUtilsMock)
113
114         when(spy.encryptBasicAuth("mso.oof.auth", "mso.msoKey")).thenReturn("auth-value")
115
116         String authHeaderResponse =  "auth-header"
117
118       /*  String authHeaderResponse =  "{\n" +
119                 " \"errorCode\": \"401\",\n" +
120                 " \"errorMessage\": \"Bad request\"\n" +
121                 "}" */
122
123         when(spy.getAuthHeader(mockExecution, "auth-value", "mso.msoKey")).thenReturn(authHeaderResponse)
124
125         AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
126
127         ServiceInstance nssi = new ServiceInstance()
128         nssi.setServiceInstanceId("5G-999")
129         Optional<ServiceInstance> nssiOpt = Optional.of(nssi)
130
131         when(client.get(ServiceInstance.class, nssiUri)).thenReturn(nssiOpt)
132
133         AAIResourceUri nsiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nsiId))
134
135         ServiceInstance nsi = new ServiceInstance()
136         nsi.setServiceInstanceId("5G-777")
137         Optional<ServiceInstance> nsiOpt = Optional.of(nsi)
138
139         when(client.get(ServiceInstance.class, nsiUri)).thenReturn(nsiOpt)
140
141         String urlString = "http://oof.onap:8088"
142
143         String httpRequest =    "{\n" +
144                 "  \"type\": \"NSSI\",\n" +
145                 "  \"NxIId\": \"5G-999\",\n" +
146                 "  \"requestInfo\": {\n" +
147                 "    \"transactionId\": \"mso-request-id\",\n" +
148                 "    \"requestId\": \"mso-request-id\",\n" +
149                 "    \"sourceId\": \"so\",\n" +
150                 "    }\n" +
151                 "}"
152
153         String requestId = "request-id"
154         String nxlId = nssi.getServiceInstanceId()
155         String nxlType = "NSSI"
156         String messageType = "cn"
157         String serviceInstanceId = nsi.getServiceInstanceId()
158
159         when(mockExecution.getVariable("msoRequestId")).thenReturn(requestId)
160         when(oofUtilsMock.buildTerminateNxiRequest(requestId, nxlId, nxlType, messageType, serviceInstanceId)).thenReturn(httpRequest)
161
162         String terminateResponse = "false"
163
164         String oofResponse =   "{\n" +
165                 " \"requestId\": \"mso-request-id\",\n" +
166                 " \"transactionId\": \"mso-request-id\",\n" +
167                 " \"statusMessage\": \"\",\n" +
168                 " \"requestStatus\": \"accepted\",\n" +
169                 " \"terminateResponse\": \"${terminateResponse}\",\n" +
170                 " \"reason\": \"\"\n" +
171                 " }\n"
172
173         String apiPath =  "/api/oof/terminate/nxi/v1"
174
175         urlString = urlString + apiPath
176
177         HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class)
178         when(spy.getHttpClientFactory()).thenReturn(httpClientFactoryMock)
179         Response responseMock = mock(Response.class)
180
181         HttpClient httpClientMock = mock(HttpClient.class)
182
183         when(httpClientFactoryMock.newJsonClient(any(), any())).thenReturn(httpClientMock)
184
185         when(httpClientMock.post(httpRequest)).thenReturn(responseMock)
186
187         when(responseMock.getStatus()).thenReturn(200)
188         when(responseMock.hasEntity()).thenReturn(true)
189
190         when(responseMock.readEntity(String.class)).thenReturn(oofResponse)
191
192         spy.executeTerminateNSSIQuery(mockExecution)
193
194         Mockito.verify(mockExecution,times(1)).setVariable("isTerminateNSSI", terminateResponse)
195
196     }
197
198
199     @Test
200     void testDeleteServiceOrder() {
201         def currentNSSI = [:]
202         currentNSSI.put("nssiId","5G-999")
203
204         ServiceInstance networkServiceInstance = new ServiceInstance()
205         networkServiceInstance.setServiceInstanceId("NS-777")
206         networkServiceInstance.setServiceRole("Network Service")
207
208         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
209
210         currentNSSI.put("networkServiceInstance", networkServiceInstance)
211
212         when(mockExecution.getVariable("mso.infra.endpoint.url")).thenReturn("http://mso.onap:8088")
213         when(mockExecution.getVariable("mso.msoKey")).thenReturn("mso.msoKey")
214         when(mockExecution.getVariable("mso.infra.endpoint.auth")).thenReturn("mso.infra.endpoint.auth")
215
216         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
217         when(spy.getAAIClient()).thenReturn(client)
218
219         GenericVnf genericVnf = new GenericVnf()
220         genericVnf.setServiceId("service-id")
221         genericVnf.setVnfName("vnf-name")
222         genericVnf.setModelInvariantId("model-invariant-id")
223         genericVnf.setModelCustomizationId("model-customization-id")
224         genericVnf.setVnfName("vnf-name")
225         genericVnf.setVnfId("vnf-id")
226
227
228         currentNSSI.put("constituteVnf", genericVnf)
229
230         String urlString = String.format("http://mso.onap:8088/serviceInstantiation/v7/serviceInstances/%s/vnfs/%s", networkServiceInstance.getServiceInstanceId(), genericVnf.getVnfId())
231
232         RequestDetails requestDetails = new RequestDetails()
233         ObjectMapper mapper = new ObjectMapper()
234         String requestDetailsStr = mapper.writeValueAsString(requestDetails)
235
236         when(spy.prepareRequestDetails(mockExecution)).thenReturn(requestDetailsStr)
237
238         MsoUtils msoUtilsMock = mock(MsoUtils.class)
239         String basicAuth = "basicAuth"
240         when(msoUtilsMock.getBasicAuth(anyString(), anyString())).thenReturn(basicAuth)
241
242         HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class)
243         when(spy.getHttpClientFactory()).thenReturn(httpClientFactoryMock)
244         Response responseMock = mock(Response.class)
245
246         HttpClient httpClientMock = mock(HttpClient.class)
247
248         when(httpClientFactoryMock.newJsonClient(any(), any())).thenReturn(httpClientMock)
249
250         when(httpClientMock.delete(requestDetailsStr)).thenReturn(responseMock)
251
252         when(responseMock.getStatus()).thenReturn(200)
253         when(responseMock.hasEntity()).thenReturn(true)
254
255         String macroOperationId = "request-id"
256         String requestSelfLink = "request-self-link"
257         String entity = "{\"requestReferences\":{\"requestId\": \"${macroOperationId}\",\"requestSelfLink\":\"${requestSelfLink}\"}}"
258         when(responseMock.readEntity(String.class)).thenReturn(entity)
259
260         spy.deleteServiceOrder(mockExecution)
261
262         Mockito.verify(mockExecution,times(1)).setVariable("macroOperationId", macroOperationId)
263         Mockito.verify(mockExecution,times(1)).setVariable("requestSelfLink", requestSelfLink)
264
265         assertTrue(currentNSSI['requestSelfLink'].equals(requestSelfLink))
266     }
267
268
269     @Test
270     void testCalculateSNSSAITerminateNSSI() {
271         invokeCalculateSNSSAI("true")
272     }
273
274     @Test
275     void testCalculateSNSSAINotTerminateNSSI() {
276         invokeCalculateSNSSAI("false")
277     }
278
279     void invokeCalculateSNSSAI(String isTerminateNSSI) {
280         def currentNSSI = [:]
281         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
282
283         when(mockExecution.getVariable("isTerminateNSSI")).thenReturn(isTerminateNSSI)
284
285         String theSNSSAI = "theS-NSSAI"
286
287         currentNSSI.put("S-NSSAI", theSNSSAI)
288
289         String theSliceProfileId = "the-slice-profile-id"
290         currentNSSI['sliceProfileId'] = theSliceProfileId
291
292         List<SliceProfile> associatedProfiles = new ArrayList<>()
293         SliceProfile sliceProfile1 = new SliceProfile()
294         sliceProfile1.setProfileId(theSliceProfileId)
295         sliceProfile1.setSNssai(theSNSSAI)
296
297         SliceProfile sliceProfile2 = new SliceProfile()
298         sliceProfile2.setSNssai("snssai2")
299
300         SliceProfile sliceProfile3 = new SliceProfile()
301         sliceProfile3.setSNssai("snssai3")
302
303         if(isTerminateNSSI.equals("false")) {
304             associatedProfiles.add(sliceProfile1)
305             associatedProfiles.add(sliceProfile2)
306             associatedProfiles.add(sliceProfile3)
307         }
308
309         int sizeBefore = associatedProfiles.size()
310
311         currentNSSI.put("associatedProfiles", associatedProfiles)
312
313         DoDeallocateCoreNSSI obj = new DoDeallocateCoreNSSI()
314         obj.calculateSNSSAI(mockExecution)
315
316         List<SliceProfile> snssais = (List<SliceProfile>)currentNSSI.get("S-NSSAIs")
317         SliceProfile sliceProfileContainsSNSSAI = (SliceProfile)currentNSSI.get("sliceProfileS-NSSAI")
318
319         if(isTerminateNSSI.equals("false")) {
320             assertTrue("Either snssais doesn't exist or size is incorrect", (snssais != null && snssais.size() == (sizeBefore - 1)))
321             assertNotNull("Slice Profile which contains given S-NSSAI not found", sliceProfileContainsSNSSAI)
322             assertTrue("Wrong Slice Profile", sliceProfileContainsSNSSAI.getSNssai().equals(theSNSSAI))
323         }
324         else {
325             assertTrue("Either snssais doesn't exist or size is incorrect", (snssais != null && snssais.size() == 0))
326         }
327     }
328
329
330     @Test
331     void testRemoveNSSIAssociationWithNSI() {
332         def currentNSSI = [:]
333
334         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
335
336         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
337
338         when(spy.getAAIClient()).thenReturn(client)
339
340         String nssiId = "5G-999"
341         String nsiId = "5G-99"
342         currentNSSI.put("nssiId", nssiId)
343         currentNSSI.put("nsiId", nsiId)
344
345         AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
346
347         ServiceInstance nssi = new ServiceInstance()
348         nssi.setServiceInstanceId(nssiId)
349
350         ServiceInstance nsi = new ServiceInstance()
351         nsi.setServiceInstanceId(nsiId)
352         nsi.setServiceRole("nsi")
353
354         AllottedResources allottedResources = new AllottedResources()
355         AllottedResource allottedResource = new AllottedResource()
356         allottedResource.setId(UUID.randomUUID().toString())
357         allottedResources.getAllottedResource().add(allottedResource)
358         nssi.setAllottedResources(allottedResources)
359
360         currentNSSI.put("nssi", nssi)
361
362         AAIResourceUri nsiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nsiId))
363
364         AAIResultWrapper wrapperMock = mock(AAIResultWrapper.class)
365         when(client.get(nssiUri)).thenReturn(wrapperMock)
366         Relationships rsMock = mock(Relationships.class)
367         Optional<Relationships> orsMock = Optional.of(rsMock)
368         when(wrapperMock.getRelationships()).thenReturn(orsMock)
369
370         List<AAIResourceUri> allottedUris = new ArrayList<>()
371         AAIResourceUri allottedUri = AAIUriFactory.createResourceUri(Types.ALLOTTED_RESOURCE.getFragment("allotted-id"))
372         allottedUris.add(allottedUri)
373
374         when(rsMock.getRelatedUris(Types.ALLOTTED_RESOURCE)).thenReturn(allottedUris)
375
376         List<AAIResourceUri> nsiUris = new ArrayList<>()
377         nsiUris.add(nsiUri)
378
379         Optional<ServiceInstance> nsiOpt = Optional.of(nsi)
380
381         when(client.get(allottedUri)).thenReturn(wrapperMock)
382         when(rsMock.getRelatedUris(Types.SERVICE_INSTANCE)).thenReturn(nsiUris)
383         when(client.get(ServiceInstance.class, nsiUri)).thenReturn(nsiOpt)
384
385         String globalSubscriberId = "globalSubscriberId"
386         String subscriptionServiceType = "subscription-service-type"
387         when(mockExecution.getVariable("globalSubscriberId")).thenReturn(globalSubscriberId)
388         when(mockExecution.getVariable("subscriptionServiceType")).thenReturn(subscriptionServiceType)
389
390         AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(nssiId).allottedResource(allottedResource.getId()))
391
392         doNothing().when(client).disconnect(nssiUri, nsiUri)
393
394         spy.removeNSSIAssociationWithNSI(mockExecution)
395
396     }
397
398
399     @Test
400     void testDeleteNSSIServiceInstance() {
401         def currentNSSI = [:]
402
403         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
404
405         String nssiId = "5G-999"
406
407         currentNSSI.put("nssiId", nssiId)
408
409         AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
410
411         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
412
413         when(spy.getAAIClient()).thenReturn(client)
414
415         doNothing().when(client).delete(nssiUri)
416
417         spy.deleteNSSIServiceInstance(mockExecution)
418     }
419
420
421     @Test
422     void testDeleteServiceOrderProgressAcknowledged() {
423
424         executeDeleteServiceOrderProgress("ACKNOWLEDGED")
425         Mockito.verify(mockExecution,times(1)).setVariable("deleteStatus", "processing")
426     }
427
428     @Test
429     void testDeleteServiceOrderProgressInProgress() {
430
431         executeDeleteServiceOrderProgress("IN_PROGRESS")
432         Mockito.verify(mockExecution,times(1)).setVariable("deleteStatus", "processing")
433     }
434
435
436     @Test
437     void testDeleteServiceOrderProgressCompleted() {
438
439         executeDeleteServiceOrderProgress("COMPLETE")
440         Mockito.verify(mockExecution,times(1)).setVariable("deleteStatus", "completed")
441     }
442
443
444     void executeDeleteServiceOrderProgress(String state) {
445         def currentNSSI = [:]
446
447         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
448
449         String url = "http://nbi.onap:8088/api/v4/serviceOrder/NS-777"
450
451         currentNSSI['requestSelfLink'] =  url
452
453         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
454
455         /*ExternalAPIUtilFactory externalAPIUtilFactoryMock = mock(ExternalAPIUtilFactory.class)
456         when(spy.getExternalAPIUtilFactory()).thenReturn(externalAPIUtilFactoryMock)
457
458         ExternalAPIUtil externalAPIUtilMock = mock(ExternalAPIUtil.class)
459
460         when(externalAPIUtilFactoryMock.create()).thenReturn(externalAPIUtilMock) */
461
462         MsoUtils msoUtilsMock = mock(MsoUtils.class)
463         String basicAuth = "basicAuth"
464         when(msoUtilsMock.getBasicAuth(anyString(), anyString())).thenReturn(basicAuth)
465
466         HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class)
467         when(spy.getHttpClientFactory()).thenReturn(httpClientFactoryMock)
468         Response responseMock = mock(Response.class)
469
470         HttpClient httpClientMock = mock(HttpClient.class)
471
472
473         when(httpClientFactoryMock.newJsonClient(any(), any())).thenReturn(httpClientMock)
474
475         when(httpClientMock.get()).thenReturn(responseMock)
476 //        when(externalAPIUtilMock.executeExternalAPIGetCall(mockExecution, url)).thenReturn(responseMock)
477
478         when(responseMock.getStatus()).thenReturn(200)
479         when(responseMock.hasEntity()).thenReturn(true)
480
481         String entity = "{\"request\":{\"requestStatus\":{\"requestState\":\"${state}\"}},\"state\":\"ACCEPTED\"}"
482         when(responseMock.readEntity(String.class)).thenReturn(entity)
483
484         spy.getDeleteServiceOrderProgress(mockExecution)
485     }
486
487 }