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