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.*
27 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
28 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
29 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
30 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
31 import org.onap.so.bpmn.common.scripts.ExternalAPIUtil
32 import org.onap.so.bpmn.common.scripts.ExternalAPIUtilFactory
33 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
35 import javax.ws.rs.core.Response
37 import static org.junit.Assert.assertNotNull
38 import static org.junit.Assert.assertTrue
39 import static org.mockito.Mockito.*
41 class DoDeallocateCoreNSSITest extends MsoGroovyTest {
44 void init() throws IOException {
45 super.init("DoDeallocateNSSITest")
50 void testExecuteTerminateNSSIQuery() {
53 currentNSSI.put("nssiId","5G-999")
55 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
57 when(mockExecution.getVariable("mso.oof.endpoint")).thenReturn("http://oof.onap:8088")
58 when(mockExecution.getVariable("mso.oof.auth")).thenReturn("mso.oof.auth")
59 when(mockExecution.getVariable("mso.msoKey")).thenReturn("mso.msoKey")
60 when(mockExecution.getVariable("mso-request-id")).thenReturn("mso-request-id")
62 DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
63 when(spy.getAAIClient()).thenReturn(client)
65 when(spy.encryptBasicAuth("mso.oof.auth", "mso.msoKey")).thenReturn("auth-value")
67 String authHeaderResponse = "auth-header"
69 /* String authHeaderResponse = "{\n" +
70 " \"errorCode\": \"401\",\n" +
71 " \"errorMessage\": \"Bad request\"\n" +
74 when(spy.getAuthHeader(mockExecution, "auth-value", "mso.msoKey")).thenReturn(authHeaderResponse)
76 String urlString = "http://oof.onap:8088"
78 String httpRequest = "{\n" +
79 " \"type\": \"NSSI\",\n" +
80 " \"NxIId\": \"5G-999\",\n" +
81 " \"requestInfo\": {\n" +
82 " \"transactionId\": \"mso-request-id\",\n" +
83 " \"requestId\": \"mso-request-id\",\n" +
84 " \"sourceId\": \"so\",\n" +
88 boolean terminateResponse = true
90 String oofResponse = "{\n" +
91 " \"requestId\": \"mso-request-id\",\n" +
92 " \"transactionId\": \"mso-request-id\",\n" +
93 " \"statusMessage\": \"\",\n" +
94 " \"requestStatus\": \"accepted\",\n" +
95 " \"terminateResponse\": \"${terminateResponse}\",\n" +
96 " \"reason\": \"\"\n" +
99 String oofCallResponse = oofResponse
101 /* String oofCallResponse = "{\n" +
102 " \"errorCode\": \"401\",\n" +
103 " \"errorMessage\": \"Exception during the call\"\n" +
106 when(spy.callOOF(urlString, "auth-header", httpRequest)).thenReturn(oofCallResponse)
108 spy.executeTerminateNSSIQuery(mockExecution)
110 verify(mockExecution).setVariable("isTerminateNSSI", terminateResponse)
116 void testDeleteServiceOrder() {
117 def currentNSSI = [:]
118 currentNSSI.put("nssiId","5G-999")
120 ServiceInstance networkServiceInstance = new ServiceInstance()
121 networkServiceInstance.setServiceInstanceId("NS-777")
122 networkServiceInstance.setServiceRole("Network Service")
124 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
126 currentNSSI.put("networkServiceInstance", networkServiceInstance)
128 when(mockExecution.getVariable("nbi.endpoint.url")).thenReturn("http://nbi.onap:8088")
129 when(mockExecution.getVariable("mso.msoKey")).thenReturn("mso.msoKey")
130 when(mockExecution.getVariable("mso.infra.endpoint.auth")).thenReturn("mso.infra.endpoint.auth")
132 DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
133 when(spy.getAAIClient()).thenReturn(client)
135 when(spy.encryptBasicAuth("mso.infra.endpoint.auth", "mso.msoKey")).thenReturn("auth-value")
137 String authHeaderResponse = "auth-header"
139 /* String authHeaderResponse = "{\n" +
140 " \"errorCode\": \"401\",\n" +
141 " \"errorMessage\": \"Bad request\"\n" +
144 when(spy.getAuthHeader(mockExecution, "auth-value", "mso.msoKey")).thenReturn(authHeaderResponse)
146 String urlString = String.format("http://nbi.onap:8088/api/v4/serviceOrder/%s", networkServiceInstance.getServiceInstanceId())
148 String callDeleteServiceOrderResponse = "deleted"
150 when(spy.callDeleteServiceOrder(mockExecution, urlString, "auth-header")).thenReturn(callDeleteServiceOrderResponse)
152 spy.deleteServiceOrder(mockExecution)
157 void testCalculateSNSSAI() {
158 def currentNSSI = [:]
159 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
161 String theSNSSAI = "theS-NSSAI"
163 currentNSSI.put("S-NSSAI", theSNSSAI)
165 List<SliceProfile> associatedProfiles = new ArrayList<>()
166 SliceProfile sliceProfile1 = new SliceProfile()
167 sliceProfile1.setSNssai("snssai1")
169 SliceProfile sliceProfile2 = new SliceProfile()
170 sliceProfile2.setSNssai(theSNSSAI)
172 SliceProfile sliceProfile3 = new SliceProfile()
173 sliceProfile3.setSNssai("snssai3")
175 associatedProfiles.add(sliceProfile1)
176 associatedProfiles.add(sliceProfile2)
177 associatedProfiles.add(sliceProfile3)
179 int sizeBefore = associatedProfiles.size()
181 currentNSSI.put("associatedProfiles", associatedProfiles)
183 DoDeallocateCoreNSSI obj = new DoDeallocateCoreNSSI()
184 obj.calculateSNSSAI(mockExecution)
186 List<SliceProfile> snssais = (List<SliceProfile>)currentNSSI.get("S-NSSAIs")
187 SliceProfile sliceProfileContainsSNSSAI = (SliceProfile)currentNSSI.get("sliceProfileS-NSSAI")
189 assertTrue("Either snssais doesn't exist or size is incorrect", (snssais != null && snssais.size() == (sizeBefore - 1)))
190 assertNotNull("Slice Profile which contains given S-NSSAI not found", sliceProfileContainsSNSSAI)
191 assertTrue("Wrong Slice Profile", sliceProfileContainsSNSSAI.getSNssai().equals(theSNSSAI))
196 void testRemoveNSSIAssociationWithNSI() {
197 def currentNSSI = [:]
199 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
201 DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
203 when(spy.getAAIClient()).thenReturn(client)
205 String nssiId = "5G-999"
206 String nsiId = "5G-99"
207 currentNSSI.put("nssiId", nssiId)
208 currentNSSI.put("nsiId", nsiId)
210 AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
212 ServiceInstance nssi = new ServiceInstance()
213 nssi.setServiceInstanceId(nssiId)
215 AllottedResources allottedResources = new AllottedResources()
216 AllottedResource allottedResource = new AllottedResource()
217 allottedResource.setId(UUID.randomUUID().toString())
218 allottedResources.getAllottedResource().add(allottedResource)
219 nssi.setAllottedResources(allottedResources)
221 currentNSSI.put("nssi", nssi)
223 AAIResourceUri nsiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nsiId))
225 doNothing().when(client).update(nssiUri, nssi)
227 String globalSubscriberId = "globalSubscriberId"
228 String subscriptionServiceType = "subscription-service-type"
229 when(mockExecution.getVariable("globalSubscriberId")).thenReturn(globalSubscriberId)
230 when(mockExecution.getVariable("subscriptionServiceType")).thenReturn(subscriptionServiceType)
232 AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(nssiId).allottedResource(allottedResource.getId()))
234 doNothing().when(client).disconnect(nsiUri, allottedResourceUri)
236 spy.removeNSSIAssociationWithNSI(mockExecution)
242 void testDeleteNSSIServiceInstance() {
243 def currentNSSI = [:]
245 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
247 String nssiId = "5G-999"
249 currentNSSI.put("nssiId", nssiId)
251 AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
253 DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
255 when(spy.getAAIClient()).thenReturn(client)
257 doNothing().when(client).delete(nssiUri)
259 spy.deleteNSSIServiceInstance(mockExecution)
264 void testDeleteServiceOrderProgressAcknowledged() {
266 executeDeleteServiceOrderProgress("ACKNOWLEDGED")
267 Mockito.verify(mockExecution,times(1)).setVariable("deleteStatus", "processing")
271 void testDeleteServiceOrderProgressInProgress() {
273 executeDeleteServiceOrderProgress("INPROGRESS")
274 Mockito.verify(mockExecution,times(1)).setVariable("deleteStatus", "processing")
279 void testDeleteServiceOrderProgressCompleted() {
281 executeDeleteServiceOrderProgress("COMPLETED")
282 Mockito.verify(mockExecution,times(1)).setVariable("deleteStatus", "completed")
286 void executeDeleteServiceOrderProgress(String state) {
287 def currentNSSI = [:]
289 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
291 String url = "http://nbi.onap:8088/api/v4/serviceOrder/NS-777"
293 currentNSSI.put("deleteServiceOrderURL", url)
295 DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
297 ExternalAPIUtilFactory externalAPIUtilFactoryMock = mock(ExternalAPIUtilFactory.class)
298 when(spy.getExternalAPIUtilFactory()).thenReturn(externalAPIUtilFactoryMock)
300 ExternalAPIUtil externalAPIUtilMock = mock(ExternalAPIUtil.class)
302 when(externalAPIUtilFactoryMock.create()).thenReturn(externalAPIUtilMock)
304 Response responseMock = mock(Response.class)
305 when(externalAPIUtilMock.executeExternalAPIGetCall(mockExecution, url)).thenReturn(responseMock)
307 when(responseMock.getStatus()).thenReturn(200)
309 String entity = "{\"state\":\"ACCEPTED\",\"orderItem\":[{\"service\":{\"id\":\"5G-999\"},\"state\":\"${state}\"}]}"
310 when(responseMock.readEntity(String.class)).thenReturn(entity)
312 spy.getDeleteServiceOrderProgress(mockExecution)