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.Types
30 import org.onap.so.bpmn.common.scripts.ExternalAPIUtil
31 import org.onap.so.bpmn.common.scripts.ExternalAPIUtilFactory
32 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
34 import javax.ws.rs.core.Response
36 import static org.junit.Assert.assertNotNull
37 import static org.junit.Assert.assertTrue
38 import static org.mockito.Mockito.*
40 class DoDeallocateCoreNSSITest extends MsoGroovyTest {
43 void init() throws IOException {
44 super.init("DoDeallocateNSSITest")
49 void testExecuteTerminateNSSIQuery() {
52 currentNSSI.put("nssiId","5G-999")
54 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
56 when(mockExecution.getVariable("mso.oof.endpoint")).thenReturn("http://oof.onap:8088")
57 when(mockExecution.getVariable("mso.oof.auth")).thenReturn("mso.oof.auth")
58 when(mockExecution.getVariable("mso.msoKey")).thenReturn("mso.msoKey")
59 when(mockExecution.getVariable("mso-request-id")).thenReturn("mso-request-id")
61 DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
62 when(spy.getAAIClient()).thenReturn(client)
64 when(spy.encryptBasicAuth("mso.oof.auth", "mso.msoKey")).thenReturn("auth-value")
66 String authHeaderResponse = "auth-header"
68 /* String authHeaderResponse = "{\n" +
69 " \"errorCode\": \"401\",\n" +
70 " \"errorMessage\": \"Bad request\"\n" +
73 when(spy.getAuthHeader(mockExecution, "auth-value", "mso.msoKey")).thenReturn(authHeaderResponse)
75 String urlString = "http://oof.onap:8088"
77 String httpRequest = "{\n" +
78 " \"type\": \"NSSI\",\n" +
79 " \"NxIId\": \"5G-999\",\n" +
80 " \"requestInfo\": {\n" +
81 " \"transactionId\": \"mso-request-id\",\n" +
82 " \"requestId\": \"mso-request-id\",\n" +
83 " \"sourceId\": \"so\",\n" +
87 boolean terminateResponse = true
89 String oofResponse = "{\n" +
90 " \"requestId\": \"mso-request-id\",\n" +
91 " \"transactionId\": \"mso-request-id\",\n" +
92 " \"statusMessage\": \"\",\n" +
93 " \"requestStatus\": \"accepted\",\n" +
94 " \"terminateResponse\": \"${terminateResponse}\",\n" +
95 " \"reason\": \"\"\n" +
98 String oofCallResponse = oofResponse
100 /* String oofCallResponse = "{\n" +
101 " \"errorCode\": \"401\",\n" +
102 " \"errorMessage\": \"Exception during the call\"\n" +
105 when(spy.callOOF(urlString, "auth-header", httpRequest)).thenReturn(oofCallResponse)
107 spy.executeTerminateNSSIQuery(mockExecution)
109 verify(mockExecution).setVariable("isTerminateNSSI", terminateResponse)
115 void testDeleteServiceOrder() {
116 def currentNSSI = [:]
117 currentNSSI.put("nssiId","5G-999")
119 ServiceInstance networkServiceInstance = new ServiceInstance()
120 networkServiceInstance.setServiceInstanceId("NS-777")
121 networkServiceInstance.setServiceRole("Network Service")
123 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
125 currentNSSI.put("networkServiceInstance", networkServiceInstance)
127 when(mockExecution.getVariable("nbi.endpoint.url")).thenReturn("http://nbi.onap:8088")
128 when(mockExecution.getVariable("mso.msoKey")).thenReturn("mso.msoKey")
129 when(mockExecution.getVariable("mso.infra.endpoint.auth")).thenReturn("mso.infra.endpoint.auth")
131 DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
132 when(spy.getAAIClient()).thenReturn(client)
134 when(spy.encryptBasicAuth("mso.infra.endpoint.auth", "mso.msoKey")).thenReturn("auth-value")
136 String authHeaderResponse = "auth-header"
138 /* String authHeaderResponse = "{\n" +
139 " \"errorCode\": \"401\",\n" +
140 " \"errorMessage\": \"Bad request\"\n" +
143 when(spy.getAuthHeader(mockExecution, "auth-value", "mso.msoKey")).thenReturn(authHeaderResponse)
145 String urlString = String.format("http://nbi.onap:8088/api/v4/serviceOrder/%s", networkServiceInstance.getServiceInstanceId())
147 String callDeleteServiceOrderResponse = "deleted"
149 when(spy.callDeleteServiceOrder(mockExecution, urlString, "auth-header")).thenReturn(callDeleteServiceOrderResponse)
151 spy.deleteServiceOrder(mockExecution)
156 void testGetNSSIAssociatedProfiles() {
157 def currentNSSI = [:]
158 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
160 ServiceInstance nssi = new ServiceInstance()
161 nssi.setServiceInstanceId("5G-999")
163 SliceProfiles sliceProfiles = new SliceProfiles()
165 List<SliceProfile> slProfiles = sliceProfiles.getSliceProfile()
166 slProfiles.add(new SliceProfile())
167 slProfiles.add(new SliceProfile())
169 nssi.setSliceProfiles(sliceProfiles)
170 currentNSSI.put("nssi", nssi)
172 DoDeallocateCoreNSSI obj = new DoDeallocateCoreNSSI()
173 obj.getNSSIAssociatedProfiles(mockExecution)
175 List<SliceProfile> associatedProfiles = (List<SliceProfile>)currentNSSI.get("associatedProfiles")
176 assertTrue("Either associatedProfiles doesn't exist or size is incorrect", (associatedProfiles != null && associatedProfiles.size() == 2))
181 void testCalculateSNSSAI() {
182 def currentNSSI = [:]
183 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
185 String theSNSSAI = "theS-NSSAI"
187 currentNSSI.put("S-NSSAI", theSNSSAI)
189 List<SliceProfile> associatedProfiles = new ArrayList<>()
190 SliceProfile sliceProfile1 = new SliceProfile()
191 sliceProfile1.setSNssai("snssai1")
193 SliceProfile sliceProfile2 = new SliceProfile()
194 sliceProfile2.setSNssai(theSNSSAI)
196 SliceProfile sliceProfile3 = new SliceProfile()
197 sliceProfile3.setSNssai("snssai3")
199 associatedProfiles.add(sliceProfile1)
200 associatedProfiles.add(sliceProfile2)
201 associatedProfiles.add(sliceProfile3)
203 int sizeBefore = associatedProfiles.size()
205 currentNSSI.put("associatedProfiles", associatedProfiles)
207 DoDeallocateCoreNSSI obj = new DoDeallocateCoreNSSI()
208 obj.calculateSNSSAI(mockExecution)
210 List<SliceProfile> snssais = (List<SliceProfile>)currentNSSI.get("S-NSSAIs")
211 SliceProfile sliceProfileContainsSNSSAI = (SliceProfile)currentNSSI.get("sliceProfileS-NSSAI")
213 assertTrue("Either snssais doesn't exist or size is incorrect", (snssais != null && snssais.size() == (sizeBefore - 1)))
214 assertNotNull("Slice Profile which contains given S-NSSAI not found", sliceProfileContainsSNSSAI)
215 assertTrue("Wrong Slice Profile", sliceProfileContainsSNSSAI.getSNssai().equals(theSNSSAI))
220 void testRemoveNSSIAssociationWithNSI() {
221 def currentNSSI = [:]
223 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
225 DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
227 when(spy.getAAIClient()).thenReturn(client)
229 String nssiId = "5G-999"
230 String nsiId = "5G-99"
231 currentNSSI.put("nssiId", nssiId)
232 currentNSSI.put("nsiId", nsiId)
234 AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
235 AAIResourceUri nsiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nsiId))
237 doNothing().when(client).disconnect(nssiUri, nsiUri)
239 spy.removeNSSIAssociationWithNSI(mockExecution)
245 void testDeleteNSSIServiceInstance() {
246 def currentNSSI = [:]
248 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
250 String nssiId = "5G-999"
252 currentNSSI.put("nssiId", nssiId)
254 AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
256 DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
258 when(spy.getAAIClient()).thenReturn(client)
260 doNothing().when(client).delete(nssiUri)
262 spy.deleteNSSIServiceInstance(mockExecution)
267 void testDeleteServiceOrderProgressAcknowledged() {
269 executeDeleteServiceOrderProgress("ACKNOWLEDGED")
270 Mockito.verify(mockExecution,times(1)).setVariable("deleteStatus", "processing")
274 void testDeleteServiceOrderProgressInProgress() {
276 executeDeleteServiceOrderProgress("INPROGRESS")
277 Mockito.verify(mockExecution,times(1)).setVariable("deleteStatus", "processing")
282 void testDeleteServiceOrderProgressCompleted() {
284 executeDeleteServiceOrderProgress("COMPLETED")
285 Mockito.verify(mockExecution,times(1)).setVariable("deleteStatus", "completed")
289 void executeDeleteServiceOrderProgress(String state) {
290 def currentNSSI = [:]
292 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
294 String url = "http://nbi.onap:8088/api/v4/serviceOrder/NS-777"
296 currentNSSI.put("deleteServiceOrderURL", url)
298 DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
300 ExternalAPIUtilFactory externalAPIUtilFactoryMock = mock(ExternalAPIUtilFactory.class)
301 when(spy.getExternalAPIUtilFactory()).thenReturn(externalAPIUtilFactoryMock)
303 ExternalAPIUtil externalAPIUtilMock = mock(ExternalAPIUtil.class)
305 when(externalAPIUtilFactoryMock.create()).thenReturn(externalAPIUtilMock)
307 Response responseMock = mock(Response.class)
308 when(externalAPIUtilMock.executeExternalAPIGetCall(mockExecution, url)).thenReturn(responseMock)
310 when(responseMock.getStatus()).thenReturn(200)
312 String entity = "{\"state\":\"ACCEPTED\",\"orderItem\":[{\"service\":{\"id\":\"5G-999\"},\"state\":\"${state}\"}]}"
313 when(responseMock.readEntity(String.class)).thenReturn(entity)
315 spy.getDeleteServiceOrderProgress(mockExecution)