2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.infrastructure.scripts
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.junit.Before
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
44 import javax.ws.rs.core.Response
46 import static org.junit.Assert.assertNotNull
47 import static org.junit.Assert.assertTrue
48 import static org.mockito.Mockito.*
50 class DoDeallocateCoreNSSITest extends MsoGroovyTest {
53 void init() throws IOException {
54 super.init("DoDeallocateNSSITest")
59 void testPreProcessRequest() {
61 String nssiId = "5G-999"
62 when(mockExecution.getVariable("serviceInstanceID")).thenReturn(nssiId)
64 String nsiId = "5G-777"
65 when(mockExecution.getVariable("nsiId")).thenReturn(nsiId)
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" +
74 when(mockExecution.getVariable("sliceParams")).thenReturn(sliceParams)
76 DoDeallocateCoreNSSI obj = new DoDeallocateCoreNSSI()
77 obj.preProcessRequest(mockExecution)
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)
91 void testExecuteTerminateNSSIQuery() {
95 String nssiId = "5G-999"
96 currentNSSI.put("nssiId", nssiId)
98 String nsiId = "ns-777"
99 currentNSSI.put("nsiId", nsiId)
101 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
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")
108 DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
109 when(spy.getAAIClient()).thenReturn(client)
111 OofUtils oofUtilsMock = mock(OofUtils.class)
112 when(spy.getOofUtils()).thenReturn(oofUtilsMock)
114 when(spy.encryptBasicAuth("mso.oof.auth", "mso.msoKey")).thenReturn("auth-value")
116 String authHeaderResponse = "auth-header"
118 /* String authHeaderResponse = "{\n" +
119 " \"errorCode\": \"401\",\n" +
120 " \"errorMessage\": \"Bad request\"\n" +
123 when(spy.getAuthHeader(mockExecution, "auth-value", "mso.msoKey")).thenReturn(authHeaderResponse)
125 AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
127 ServiceInstance nssi = new ServiceInstance()
128 nssi.setServiceInstanceId("5G-999")
129 Optional<ServiceInstance> nssiOpt = Optional.of(nssi)
131 when(client.get(ServiceInstance.class, nssiUri)).thenReturn(nssiOpt)
133 AAIResourceUri nsiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nsiId))
135 ServiceInstance nsi = new ServiceInstance()
136 nsi.setServiceInstanceId("5G-777")
137 Optional<ServiceInstance> nsiOpt = Optional.of(nsi)
139 when(client.get(ServiceInstance.class, nsiUri)).thenReturn(nsiOpt)
141 String urlString = "http://oof.onap:8088"
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" +
153 String requestId = "request-id"
154 String nxlId = nssi.getServiceInstanceId()
155 String nxlType = "NSSI"
156 String messageType = "cn"
157 String serviceInstanceId = nsi.getServiceInstanceId()
159 when(mockExecution.getVariable("msoRequestId")).thenReturn(requestId)
160 when(oofUtilsMock.buildTerminateNxiRequest(requestId, nxlId, nxlType, messageType, serviceInstanceId)).thenReturn(httpRequest)
162 String terminateResponse = "false"
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" +
173 String apiPath = "/api/oof/terminate/nxi/v1"
175 urlString = urlString + apiPath
177 HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class)
178 when(spy.getHttpClientFactory()).thenReturn(httpClientFactoryMock)
179 Response responseMock = mock(Response.class)
181 HttpClient httpClientMock = mock(HttpClient.class)
183 when(httpClientFactoryMock.newJsonClient(any(), any())).thenReturn(httpClientMock)
185 when(httpClientMock.post(httpRequest)).thenReturn(responseMock)
187 when(responseMock.getStatus()).thenReturn(200)
188 when(responseMock.hasEntity()).thenReturn(true)
190 when(responseMock.readEntity(String.class)).thenReturn(oofResponse)
192 spy.executeTerminateNSSIQuery(mockExecution)
194 Mockito.verify(mockExecution,times(1)).setVariable("isTerminateNSSI", terminateResponse)
200 void testDeleteServiceOrder() {
201 def currentNSSI = [:]
202 currentNSSI.put("nssiId","5G-999")
204 ServiceInstance networkServiceInstance = new ServiceInstance()
205 networkServiceInstance.setServiceInstanceId("NS-777")
206 networkServiceInstance.setServiceRole("Network Service")
208 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
210 currentNSSI.put("networkServiceInstance", networkServiceInstance)
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")
216 DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
217 when(spy.getAAIClient()).thenReturn(client)
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")
228 currentNSSI.put("constituteVnf", genericVnf)
230 String urlString = String.format("http://mso.onap:8088/serviceInstantiation/v7/serviceInstances/%s/vnfs/%s", networkServiceInstance.getServiceInstanceId(), genericVnf.getVnfId())
232 RequestDetails requestDetails = new RequestDetails()
233 ObjectMapper mapper = new ObjectMapper()
234 String requestDetailsStr = mapper.writeValueAsString(requestDetails)
236 when(spy.prepareRequestDetails(mockExecution)).thenReturn(requestDetailsStr)
238 MsoUtils msoUtilsMock = mock(MsoUtils.class)
239 String basicAuth = "basicAuth"
240 when(msoUtilsMock.getBasicAuth(anyString(), anyString())).thenReturn(basicAuth)
242 HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class)
243 when(spy.getHttpClientFactory()).thenReturn(httpClientFactoryMock)
244 Response responseMock = mock(Response.class)
246 HttpClient httpClientMock = mock(HttpClient.class)
248 when(httpClientFactoryMock.newJsonClient(any(), any())).thenReturn(httpClientMock)
250 when(httpClientMock.delete(requestDetailsStr)).thenReturn(responseMock)
252 when(responseMock.getStatus()).thenReturn(200)
253 when(responseMock.hasEntity()).thenReturn(true)
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)
260 spy.deleteServiceOrder(mockExecution)
262 Mockito.verify(mockExecution,times(1)).setVariable("macroOperationId", macroOperationId)
263 Mockito.verify(mockExecution,times(1)).setVariable("requestSelfLink", requestSelfLink)
265 assertTrue(currentNSSI['requestSelfLink'].equals(requestSelfLink))
270 void testCalculateSNSSAITerminateNSSI() {
271 invokeCalculateSNSSAI("true")
275 void testCalculateSNSSAINotTerminateNSSI() {
276 invokeCalculateSNSSAI("false")
279 void invokeCalculateSNSSAI(String isTerminateNSSI) {
280 def currentNSSI = [:]
281 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
283 when(mockExecution.getVariable("isTerminateNSSI")).thenReturn(isTerminateNSSI)
285 String theSNSSAI = "theS-NSSAI"
287 currentNSSI.put("S-NSSAI", theSNSSAI)
289 String theSliceProfileId = "the-slice-profile-id"
290 currentNSSI['sliceProfileId'] = theSliceProfileId
292 List<SliceProfile> associatedProfiles = new ArrayList<>()
293 SliceProfile sliceProfile1 = new SliceProfile()
294 sliceProfile1.setProfileId(theSliceProfileId)
295 sliceProfile1.setSNssai(theSNSSAI)
297 SliceProfile sliceProfile2 = new SliceProfile()
298 sliceProfile2.setSNssai("snssai2")
300 SliceProfile sliceProfile3 = new SliceProfile()
301 sliceProfile3.setSNssai("snssai3")
303 if(isTerminateNSSI.equals("false")) {
304 associatedProfiles.add(sliceProfile1)
305 associatedProfiles.add(sliceProfile2)
306 associatedProfiles.add(sliceProfile3)
309 int sizeBefore = associatedProfiles.size()
311 currentNSSI.put("associatedProfiles", associatedProfiles)
313 DoDeallocateCoreNSSI obj = new DoDeallocateCoreNSSI()
314 obj.calculateSNSSAI(mockExecution)
316 List<SliceProfile> snssais = (List<SliceProfile>)currentNSSI.get("S-NSSAIs")
317 SliceProfile sliceProfileContainsSNSSAI = (SliceProfile)currentNSSI.get("sliceProfileS-NSSAI")
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))
325 assertTrue("Either snssais doesn't exist or size is incorrect", (snssais != null && snssais.size() == 0))
331 void testRemoveNSSIAssociationWithNSI() {
332 def currentNSSI = [:]
334 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
336 DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
338 when(spy.getAAIClient()).thenReturn(client)
340 String nssiId = "5G-999"
341 String nsiId = "5G-99"
342 currentNSSI.put("nssiId", nssiId)
343 currentNSSI.put("nsiId", nsiId)
345 AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
347 ServiceInstance nssi = new ServiceInstance()
348 nssi.setServiceInstanceId(nssiId)
350 ServiceInstance nsi = new ServiceInstance()
351 nsi.setServiceInstanceId(nsiId)
352 nsi.setServiceRole("nsi")
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)
360 currentNSSI.put("nssi", nssi)
362 AAIResourceUri nsiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nsiId))
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)
370 List<AAIResourceUri> allottedUris = new ArrayList<>()
371 AAIResourceUri allottedUri = AAIUriFactory.createResourceUri(Types.ALLOTTED_RESOURCE.getFragment("allotted-id"))
372 allottedUris.add(allottedUri)
374 when(rsMock.getRelatedUris(Types.ALLOTTED_RESOURCE)).thenReturn(allottedUris)
376 List<AAIResourceUri> nsiUris = new ArrayList<>()
379 Optional<ServiceInstance> nsiOpt = Optional.of(nsi)
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)
385 String globalSubscriberId = "globalSubscriberId"
386 String subscriptionServiceType = "subscription-service-type"
387 when(mockExecution.getVariable("globalSubscriberId")).thenReturn(globalSubscriberId)
388 when(mockExecution.getVariable("subscriptionServiceType")).thenReturn(subscriptionServiceType)
390 AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(nssiId).allottedResource(allottedResource.getId()))
392 doNothing().when(client).disconnect(nssiUri, nsiUri)
394 spy.removeNSSIAssociationWithNSI(mockExecution)
400 void testDeleteNSSIServiceInstance() {
401 def currentNSSI = [:]
403 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
405 String nssiId = "5G-999"
407 currentNSSI.put("nssiId", nssiId)
409 AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
411 DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
413 when(spy.getAAIClient()).thenReturn(client)
415 doNothing().when(client).delete(nssiUri)
417 spy.deleteNSSIServiceInstance(mockExecution)
422 void testDeleteServiceOrderProgressAcknowledged() {
424 executeDeleteServiceOrderProgress("ACKNOWLEDGED")
425 Mockito.verify(mockExecution,times(1)).setVariable("deleteStatus", "processing")
429 void testDeleteServiceOrderProgressInProgress() {
431 executeDeleteServiceOrderProgress("IN_PROGRESS")
432 Mockito.verify(mockExecution,times(1)).setVariable("deleteStatus", "processing")
437 void testDeleteServiceOrderProgressCompleted() {
439 executeDeleteServiceOrderProgress("COMPLETE")
440 Mockito.verify(mockExecution,times(1)).setVariable("deleteStatus", "completed")
444 void executeDeleteServiceOrderProgress(String state) {
445 def currentNSSI = [:]
447 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
449 String url = "http://nbi.onap:8088/api/v4/serviceOrder/NS-777"
451 currentNSSI['requestSelfLink'] = url
453 DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
455 /*ExternalAPIUtilFactory externalAPIUtilFactoryMock = mock(ExternalAPIUtilFactory.class)
456 when(spy.getExternalAPIUtilFactory()).thenReturn(externalAPIUtilFactoryMock)
458 ExternalAPIUtil externalAPIUtilMock = mock(ExternalAPIUtil.class)
460 when(externalAPIUtilFactoryMock.create()).thenReturn(externalAPIUtilMock) */
462 MsoUtils msoUtilsMock = mock(MsoUtils.class)
463 String basicAuth = "basicAuth"
464 when(msoUtilsMock.getBasicAuth(anyString(), anyString())).thenReturn(basicAuth)
466 HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class)
467 when(spy.getHttpClientFactory()).thenReturn(httpClientFactoryMock)
468 Response responseMock = mock(Response.class)
470 HttpClient httpClientMock = mock(HttpClient.class)
473 when(httpClientFactoryMock.newJsonClient(any(), any())).thenReturn(httpClientMock)
475 when(httpClientMock.get()).thenReturn(responseMock)
476 // when(externalAPIUtilMock.executeExternalAPIGetCall(mockExecution, url)).thenReturn(responseMock)
478 when(responseMock.getStatus()).thenReturn(200)
479 when(responseMock.hasEntity()).thenReturn(true)
481 String entity = "{\"request\":{\"requestStatus\":{\"requestState\":\"${state}\"}},\"state\":\"ACCEPTED\"}"
482 when(responseMock.readEntity(String.class)).thenReturn(entity)
484 spy.getDeleteServiceOrderProgress(mockExecution)