26b96a0a4a07a9c996954438af22b92b471d703f
[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.*
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
34
35 import javax.ws.rs.core.Response
36
37 import static org.junit.Assert.assertNotNull
38 import static org.junit.Assert.assertTrue
39 import static org.mockito.Mockito.*
40
41 class DoDeallocateCoreNSSITest extends MsoGroovyTest {
42
43     @Before
44     void init() throws IOException {
45         super.init("DoDeallocateNSSITest")
46     }
47
48
49     @Test
50     void testExecuteTerminateNSSIQuery() {
51
52         def currentNSSI = [:]
53         currentNSSI.put("nssiId","5G-999")
54
55         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
56
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")
61
62         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
63         when(spy.getAAIClient()).thenReturn(client)
64
65         when(spy.encryptBasicAuth("mso.oof.auth", "mso.msoKey")).thenReturn("auth-value")
66
67         String authHeaderResponse =  "auth-header"
68
69       /*  String authHeaderResponse =  "{\n" +
70                 " \"errorCode\": \"401\",\n" +
71                 " \"errorMessage\": \"Bad request\"\n" +
72                 "}" */
73
74         when(spy.getAuthHeader(mockExecution, "auth-value", "mso.msoKey")).thenReturn(authHeaderResponse)
75
76         String urlString = "http://oof.onap:8088"
77
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" +
85                 "    }\n" +
86                 "}"
87
88         boolean terminateResponse = true
89
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" +
97                 " }\n"
98
99         String oofCallResponse = oofResponse
100
101       /*  String oofCallResponse =  "{\n" +
102                 " \"errorCode\": \"401\",\n" +
103                 " \"errorMessage\": \"Exception during the call\"\n" +
104                 "}" */
105
106         when(spy.callOOF(urlString, "auth-header", httpRequest)).thenReturn(oofCallResponse)
107
108         spy.executeTerminateNSSIQuery(mockExecution)
109
110         verify(mockExecution).setVariable("isTerminateNSSI", terminateResponse)
111
112     }
113
114
115     @Test
116     void testDeleteServiceOrder() {
117         def currentNSSI = [:]
118         currentNSSI.put("nssiId","5G-999")
119
120         ServiceInstance networkServiceInstance = new ServiceInstance()
121         networkServiceInstance.setServiceInstanceId("NS-777")
122         networkServiceInstance.setServiceRole("Network Service")
123
124         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
125
126         currentNSSI.put("networkServiceInstance", networkServiceInstance)
127
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")
131
132         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
133         when(spy.getAAIClient()).thenReturn(client)
134
135         when(spy.encryptBasicAuth("mso.infra.endpoint.auth", "mso.msoKey")).thenReturn("auth-value")
136
137         String authHeaderResponse =  "auth-header"
138
139         /*  String authHeaderResponse =  "{\n" +
140                   " \"errorCode\": \"401\",\n" +
141                   " \"errorMessage\": \"Bad request\"\n" +
142                   "}" */
143
144         when(spy.getAuthHeader(mockExecution, "auth-value", "mso.msoKey")).thenReturn(authHeaderResponse)
145
146         String urlString = String.format("http://nbi.onap:8088/api/v4/serviceOrder/%s", networkServiceInstance.getServiceInstanceId())
147
148         String callDeleteServiceOrderResponse = "deleted"
149
150         when(spy.callDeleteServiceOrder(mockExecution, urlString, "auth-header")).thenReturn(callDeleteServiceOrderResponse)
151
152         spy.deleteServiceOrder(mockExecution)
153     }
154
155
156     @Test
157     void testCalculateSNSSAI() {
158         def currentNSSI = [:]
159         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
160
161         String theSNSSAI = "theS-NSSAI"
162
163         currentNSSI.put("S-NSSAI", theSNSSAI)
164
165         List<SliceProfile> associatedProfiles = new ArrayList<>()
166         SliceProfile sliceProfile1 = new SliceProfile()
167         sliceProfile1.setSNssai("snssai1")
168
169         SliceProfile sliceProfile2 = new SliceProfile()
170         sliceProfile2.setSNssai(theSNSSAI)
171
172         SliceProfile sliceProfile3 = new SliceProfile()
173         sliceProfile3.setSNssai("snssai3")
174
175         associatedProfiles.add(sliceProfile1)
176         associatedProfiles.add(sliceProfile2)
177         associatedProfiles.add(sliceProfile3)
178
179         int sizeBefore = associatedProfiles.size()
180
181         currentNSSI.put("associatedProfiles", associatedProfiles)
182
183         DoDeallocateCoreNSSI obj = new DoDeallocateCoreNSSI()
184         obj.calculateSNSSAI(mockExecution)
185
186         List<SliceProfile> snssais = (List<SliceProfile>)currentNSSI.get("S-NSSAIs")
187         SliceProfile sliceProfileContainsSNSSAI = (SliceProfile)currentNSSI.get("sliceProfileS-NSSAI")
188
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))
192     }
193
194
195     @Test
196     void testRemoveNSSIAssociationWithNSI() {
197         def currentNSSI = [:]
198
199         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
200
201         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
202
203         when(spy.getAAIClient()).thenReturn(client)
204
205         String nssiId = "5G-999"
206         String nsiId = "5G-99"
207         currentNSSI.put("nssiId", nssiId)
208         currentNSSI.put("nsiId", nsiId)
209
210         AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
211
212         ServiceInstance nssi = new ServiceInstance()
213         nssi.setServiceInstanceId(nssiId)
214
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)
220
221         currentNSSI.put("nssi", nssi)
222
223         AAIResourceUri nsiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nsiId))
224
225         doNothing().when(client).update(nssiUri, nssi)
226
227         String globalSubscriberId = "globalSubscriberId"
228         String subscriptionServiceType = "subscription-service-type"
229         when(mockExecution.getVariable("globalSubscriberId")).thenReturn(globalSubscriberId)
230         when(mockExecution.getVariable("subscriptionServiceType")).thenReturn(subscriptionServiceType)
231
232         AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(nssiId).allottedResource(allottedResource.getId()))
233
234         doNothing().when(client).disconnect(nsiUri, allottedResourceUri)
235
236         spy.removeNSSIAssociationWithNSI(mockExecution)
237
238     }
239
240
241     @Test
242     void testDeleteNSSIServiceInstance() {
243         def currentNSSI = [:]
244
245         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
246
247         String nssiId = "5G-999"
248
249         currentNSSI.put("nssiId", nssiId)
250
251         AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
252
253         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
254
255         when(spy.getAAIClient()).thenReturn(client)
256
257         doNothing().when(client).delete(nssiUri)
258
259         spy.deleteNSSIServiceInstance(mockExecution)
260     }
261
262
263     @Test
264     void testDeleteServiceOrderProgressAcknowledged() {
265
266         executeDeleteServiceOrderProgress("ACKNOWLEDGED")
267         Mockito.verify(mockExecution,times(1)).setVariable("deleteStatus", "processing")
268     }
269
270     @Test
271     void testDeleteServiceOrderProgressInProgress() {
272
273         executeDeleteServiceOrderProgress("INPROGRESS")
274         Mockito.verify(mockExecution,times(1)).setVariable("deleteStatus", "processing")
275     }
276
277
278     @Test
279     void testDeleteServiceOrderProgressCompleted() {
280
281         executeDeleteServiceOrderProgress("COMPLETED")
282         Mockito.verify(mockExecution,times(1)).setVariable("deleteStatus", "completed")
283     }
284
285
286     void executeDeleteServiceOrderProgress(String state) {
287         def currentNSSI = [:]
288
289         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
290
291         String url = "http://nbi.onap:8088/api/v4/serviceOrder/NS-777"
292
293         currentNSSI.put("deleteServiceOrderURL", url)
294
295         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
296
297         ExternalAPIUtilFactory externalAPIUtilFactoryMock = mock(ExternalAPIUtilFactory.class)
298         when(spy.getExternalAPIUtilFactory()).thenReturn(externalAPIUtilFactoryMock)
299
300         ExternalAPIUtil externalAPIUtilMock = mock(ExternalAPIUtil.class)
301
302         when(externalAPIUtilFactoryMock.create()).thenReturn(externalAPIUtilMock)
303
304         Response responseMock = mock(Response.class)
305         when(externalAPIUtilMock.executeExternalAPIGetCall(mockExecution, url)).thenReturn(responseMock)
306
307         when(responseMock.getStatus()).thenReturn(200)
308
309         String entity = "{\"state\":\"ACCEPTED\",\"orderItem\":[{\"service\":{\"id\":\"5G-999\"},\"state\":\"${state}\"}]}"
310         when(responseMock.readEntity(String.class)).thenReturn(entity)
311
312         spy.getDeleteServiceOrderProgress(mockExecution)
313     }
314
315 }