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 org.junit.Before
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
42 import javax.ws.rs.core.Response
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.*
49 class DoDeallocateCoreNSSITest extends MsoGroovyTest {
52 void init() throws IOException {
53 super.init("DoDeallocateNSSITest")
58 void testPreProcessRequest() {
60 String nssiId = "5G-999"
61 when(mockExecution.getVariable("serviceInstanceID")).thenReturn(nssiId)
63 String nsiId = "5G-777"
64 when(mockExecution.getVariable("nsiId")).thenReturn(nsiId)
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" +
73 when(mockExecution.getVariable("sliceParams")).thenReturn(sliceParams)
75 DoDeallocateCoreNSSI obj = new DoDeallocateCoreNSSI()
76 obj.preProcessRequest(mockExecution)
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)
90 void testExecuteTerminateNSSIQuery() {
94 String nssiId = "5G-999"
95 currentNSSI.put("nssiId", nssiId)
97 String nsiId = "ns-777"
98 currentNSSI.put("nsiId", nsiId)
100 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
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")
107 DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
108 when(spy.getAAIClient()).thenReturn(client)
110 OofUtils oofUtilsMock = mock(OofUtils.class)
111 when(spy.getOofUtils()).thenReturn(oofUtilsMock)
113 when(spy.encryptBasicAuth("mso.oof.auth", "mso.msoKey")).thenReturn("auth-value")
115 String authHeaderResponse = "auth-header"
117 when(spy.getAuthHeader(mockExecution, "auth-value", "mso.msoKey")).thenReturn(authHeaderResponse)
119 AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
121 ServiceInstance nssi = new ServiceInstance()
122 nssi.setServiceInstanceId("5G-999")
123 Optional<ServiceInstance> nssiOpt = Optional.of(nssi)
125 when(client.get(ServiceInstance.class, nssiUri)).thenReturn(nssiOpt)
127 AAIResourceUri nsiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nsiId))
129 ServiceInstance nsi = new ServiceInstance()
130 nsi.setServiceInstanceId("5G-777")
131 Optional<ServiceInstance> nsiOpt = Optional.of(nsi)
133 when(client.get(ServiceInstance.class, nsiUri)).thenReturn(nsiOpt)
135 String urlString = "http://oof.onap:8088"
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" +
147 String requestId = "request-id"
148 String nxlId = nssi.getServiceInstanceId()
149 String nxlType = "NSSI"
150 String messageType = "cn"
151 String serviceInstanceId = nsi.getServiceInstanceId()
153 when(mockExecution.getVariable("msoRequestId")).thenReturn(requestId)
154 when(oofUtilsMock.buildTerminateNxiRequest(requestId, nxlId, nxlType, messageType, serviceInstanceId)).thenReturn(httpRequest)
156 String terminateResponse = "false"
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" +
167 String apiPath = "/api/oof/terminate/nxi/v1"
169 urlString = urlString + apiPath
171 HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class)
172 when(spy.getHttpClientFactory()).thenReturn(httpClientFactoryMock)
173 Response responseMock = mock(Response.class)
175 HttpClient httpClientMock = mock(HttpClient.class)
177 when(httpClientFactoryMock.newJsonClient(any(), any())).thenReturn(httpClientMock)
179 when(httpClientMock.post(httpRequest)).thenReturn(responseMock)
181 when(responseMock.getStatus()).thenReturn(200)
182 when(responseMock.hasEntity()).thenReturn(true)
184 when(responseMock.readEntity(String.class)).thenReturn(oofResponse)
186 spy.executeTerminateNSSIQuery(mockExecution)
188 Mockito.verify(mockExecution,times(1)).setVariable("isTerminateNSSI", terminateResponse)
194 void testDeleteServiceOrder() {
195 def currentNSSI = [:]
196 currentNSSI.put("nssiId","5G-999")
198 ServiceInstance networkServiceInstance = new ServiceInstance()
199 networkServiceInstance.setServiceInstanceId("NS-777")
200 networkServiceInstance.setServiceRole("Network Service")
202 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
204 currentNSSI.put("networkServiceInstance", networkServiceInstance)
206 String externalAPIURL = "https://nbi.onap:8443/nbi/api/v4/serviceOrder"
207 when(mockExecution.getVariable("ExternalAPIURL")).thenReturn(externalAPIURL)
209 DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
210 when(spy.getAAIClient()).thenReturn(client)
212 String serviceOrderRequest = ""
214 when(mockExecution.getVariable("serviceOrderRequest")).thenReturn(serviceOrderRequest)
216 Response responseMock = mock(Response.class)
218 ExternalAPIUtil externalAPIUtil = mock(ExternalAPIUtil.class)
219 when(spy.getExternalAPIUtil()).thenReturn(externalAPIUtil)
220 when(externalAPIUtil.executeExternalAPIPostCall(mockExecution, externalAPIURL, serviceOrderRequest)).thenReturn(responseMock)
222 when(responseMock.getStatus()).thenReturn(200)
224 String id = UUID.randomUUID().toString()
225 String entity = "{\"id\":\"${id}\"}"
226 when(responseMock.readEntity(String.class)).thenReturn(entity)
228 spy.deleteServiceOrder(mockExecution)
230 verify(mockExecution,times(1)).setVariable("ServiceOrderId", id)
236 void testCalculateSNSSAITerminateNSSI() {
237 invokeCalculateSNSSAI("true")
241 void testCalculateSNSSAINotTerminateNSSI() {
242 invokeCalculateSNSSAI("false")
245 void invokeCalculateSNSSAI(String isTerminateNSSI) {
246 def currentNSSI = [:]
247 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
249 when(mockExecution.getVariable("isTerminateNSSI")).thenReturn(isTerminateNSSI)
251 String theSNSSAI = "theS-NSSAI"
253 currentNSSI.put("S-NSSAI", theSNSSAI)
255 String theSliceProfileId = "the-slice-profile-id"
256 currentNSSI['sliceProfileId'] = theSliceProfileId
258 List<SliceProfile> associatedProfiles = new ArrayList<>()
259 SliceProfile sliceProfile1 = new SliceProfile()
260 sliceProfile1.setProfileId(theSliceProfileId)
261 sliceProfile1.setSNssai(theSNSSAI)
263 SliceProfile sliceProfile2 = new SliceProfile()
264 sliceProfile2.setSNssai("snssai2")
266 SliceProfile sliceProfile3 = new SliceProfile()
267 sliceProfile3.setSNssai("snssai3")
269 if(isTerminateNSSI.equals("false")) {
270 associatedProfiles.add(sliceProfile1)
271 associatedProfiles.add(sliceProfile2)
272 associatedProfiles.add(sliceProfile3)
275 int sizeBefore = associatedProfiles.size()
277 currentNSSI.put("associatedProfiles", associatedProfiles)
279 DoDeallocateCoreNSSI obj = new DoDeallocateCoreNSSI()
280 obj.calculateSNSSAI(mockExecution)
282 List<SliceProfile> snssais = (List<SliceProfile>)currentNSSI.get("S-NSSAIs")
283 SliceProfile sliceProfileContainsSNSSAI = (SliceProfile)currentNSSI.get("sliceProfileS-NSSAI")
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))
291 assertTrue("Either snssais doesn't exist or size is incorrect", (snssais != null && snssais.size() == 0))
297 void testRemoveNSSIAssociationWithNSI() {
298 def currentNSSI = [:]
300 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
302 DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
304 when(spy.getAAIClient()).thenReturn(client)
306 String nssiId = "5G-999"
307 String nsiId = "5G-99"
308 currentNSSI.put("nssiId", nssiId)
309 currentNSSI.put("nsiId", nsiId)
311 AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
313 ServiceInstance nssi = new ServiceInstance()
314 nssi.setServiceInstanceId(nssiId)
316 ServiceInstance nsi = new ServiceInstance()
317 nsi.setServiceInstanceId(nsiId)
318 nsi.setServiceRole("nsi")
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)
326 currentNSSI.put("nssi", nssi)
328 AAIResourceUri nsiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nsiId))
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)
336 List<AAIResourceUri> allottedUris = new ArrayList<>()
337 AAIResourceUri allottedUri = AAIUriFactory.createResourceUri(Types.ALLOTTED_RESOURCE.getFragment("allotted-id"))
338 allottedUris.add(allottedUri)
340 when(rsMock.getRelatedUris(Types.ALLOTTED_RESOURCE)).thenReturn(allottedUris)
342 List<AAIResourceUri> nsiUris = new ArrayList<>()
345 Optional<ServiceInstance> nsiOpt = Optional.of(nsi)
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)
351 String globalSubscriberId = "globalSubscriberId"
352 String subscriptionServiceType = "subscription-service-type"
353 when(mockExecution.getVariable("globalSubscriberId")).thenReturn(globalSubscriberId)
354 when(mockExecution.getVariable("subscriptionServiceType")).thenReturn(subscriptionServiceType)
356 AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(nssiId).allottedResource(allottedResource.getId()))
358 doNothing().when(client).disconnect(nssiUri, nsiUri)
360 spy.removeNSSIAssociationWithNSI(mockExecution)
366 void testDeleteNSSIServiceInstance() {
367 def currentNSSI = [:]
369 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
371 String nssiId = "5G-999"
373 currentNSSI.put("nssiId", nssiId)
375 AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
377 DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
379 when(spy.getAAIClient()).thenReturn(client)
381 doNothing().when(client).delete(nssiUri)
383 spy.deleteNSSIServiceInstance(mockExecution)
388 void testDeleteServiceOrderProgressAcknowledged() {
390 executeDeleteServiceOrderProgress("ACKNOWLEDGED")
391 Mockito.verify(mockExecution,times(1)).setVariable("deleteStatus", "processing")
395 void testDeleteServiceOrderProgressInProgress() {
397 executeDeleteServiceOrderProgress("IN_PROGRESS")
398 Mockito.verify(mockExecution,times(1)).setVariable("deleteStatus", "processing")
403 void testDeleteServiceOrderProgressCompleted() {
405 executeDeleteServiceOrderProgress("COMPLETED")
406 Mockito.verify(mockExecution,times(1)).setVariable("deleteStatus", "completed")
410 void executeDeleteServiceOrderProgress(String state) {
411 def currentNSSI = [:]
412 currentNSSI.put("nssiId","5G-999")
414 ServiceInstance networkServiceInstance = new ServiceInstance()
415 networkServiceInstance.setServiceInstanceId("NS-777")
416 networkServiceInstance.setServiceRole("Network Service")
418 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
420 currentNSSI.put("networkServiceInstance", networkServiceInstance)
422 String serviceOrder = UUID.randomUUID().toString()
424 when(mockExecution.getVariable("ServiceOrderId")).thenReturn(serviceOrder)
426 String externalAPIURL = "https://nbi.onap:8443/nbi/api/v4/serviceOrder"
427 when(mockExecution.getVariable("ExternalAPIURL")).thenReturn(externalAPIURL)
429 DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
431 Response responseMock = mock(Response.class)
433 ExternalAPIUtil externalAPIUtil = mock(ExternalAPIUtil.class)
434 when(spy.getExternalAPIUtil()).thenReturn(externalAPIUtil)
436 externalAPIURL += "/" + serviceOrder
437 when(externalAPIUtil.executeExternalAPIGetCall(mockExecution, externalAPIURL)).thenReturn(responseMock)
439 when(responseMock.getStatus()).thenReturn(200)
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)
445 spy.getDeleteServiceOrderProgress(mockExecution)