4a23e80feb8896901d7605015b4de5573f32aece
[so.git] /
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
24 import org.junit.Before
25 import org.junit.Test
26 import org.onap.aai.domain.yang.ServiceInstance
27 import org.onap.aai.domain.yang.SliceProfile
28 import org.onap.aai.domain.yang.SliceProfiles
29 import org.onap.aaiclient.client.aai.AAIObjectType
30 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
31 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
32 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
33
34 import static org.junit.Assert.assertNotNull
35 import static org.junit.Assert.assertTrue
36 import static org.mockito.Mockito.*
37
38 class DoDeallocateCoreNSSITest extends MsoGroovyTest {
39
40     @Before
41     void init() throws IOException {
42         super.init("DoDeallocateNSSITest")
43     }
44
45
46     @Test
47     void testExecuteTerminateNSSIQuery() {
48
49         def currentNSSI = [:]
50         currentNSSI.put("nssiId","5G-999")
51
52         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
53
54         when(mockExecution.getVariable("mso.oof.endpoint")).thenReturn("http://oof.onap:8088")
55         when(mockExecution.getVariable("mso.oof.auth")).thenReturn("mso.oof.auth")
56         when(mockExecution.getVariable("mso.msoKey")).thenReturn("mso.msoKey")
57         when(mockExecution.getVariable("mso-request-id")).thenReturn("mso-request-id")
58
59         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
60         when(spy.getAAIClient()).thenReturn(client)
61
62         when(spy.encryptBasicAuth("mso.oof.auth", "mso.msoKey")).thenReturn("auth-value")
63
64         String authHeaderResponse =  "auth-header"
65
66       /*  String authHeaderResponse =  "{\n" +
67                 " \"errorCode\": \"401\",\n" +
68                 " \"errorMessage\": \"Bad request\"\n" +
69                 "}" */
70
71         when(spy.getAuthHeader(mockExecution, "auth-value", "mso.msoKey")).thenReturn(authHeaderResponse)
72
73         String urlString = "http://oof.onap:8088"
74
75         String httpRequest =    "{\n" +
76                 "  \"type\": \"NSSI\",\n" +
77                 "  \"NxIId\": \"5G-999\",\n" +
78                 "  \"requestInfo\": {\n" +
79                 "    \"transactionId\": \"mso-request-id\",\n" +
80                 "    \"requestId\": \"mso-request-id\",\n" +
81                 "    \"sourceId\": \"so\",\n" +
82                 "    }\n" +
83                 "}"
84
85         boolean terminateResponse = true
86
87         String oofResponse =   "{\n" +
88                 " \"requestId\": \"mso-request-id\",\n" +
89                 " \"transactionId\": \"mso-request-id\",\n" +
90                 " \"statusMessage\": \"\",\n" +
91                 " \"requestStatus\": \"accepted\",\n" +
92                 " \"terminateResponse\": \"${terminateResponse}\",\n" +
93                 " \"reason\": \"\"\n" +
94                 " }\n"
95
96         String oofCallResponse = oofResponse
97
98       /*  String oofCallResponse =  "{\n" +
99                 " \"errorCode\": \"401\",\n" +
100                 " \"errorMessage\": \"Exception during the call\"\n" +
101                 "}" */
102
103         when(spy.callOOF(urlString, "auth-header", httpRequest)).thenReturn(oofCallResponse)
104
105         spy.executeTerminateNSSIQuery(mockExecution)
106
107         verify(mockExecution).setVariable("isTerminateNSSI", terminateResponse)
108
109     }
110
111
112     @Test
113     void testDeleteServiceOrder() {
114         def currentNSSI = [:]
115         currentNSSI.put("nssiId","5G-999")
116
117         ServiceInstance networkServiceInstance = new ServiceInstance()
118         networkServiceInstance.setServiceInstanceId("NS-777")
119         networkServiceInstance.setServiceRole("Network Service")
120
121         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
122
123         currentNSSI.put("networkServiceInstance", networkServiceInstance)
124
125         when(mockExecution.getVariable("nbi.endpoint.url")).thenReturn("http://nbi.onap:8088")
126         when(mockExecution.getVariable("mso.msoKey")).thenReturn("mso.msoKey")
127         when(mockExecution.getVariable("mso.infra.endpoint.auth")).thenReturn("mso.infra.endpoint.auth")
128
129         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
130         when(spy.getAAIClient()).thenReturn(client)
131
132         when(spy.encryptBasicAuth("mso.infra.endpoint.auth", "mso.msoKey")).thenReturn("auth-value")
133
134         String authHeaderResponse =  "auth-header"
135
136         /*  String authHeaderResponse =  "{\n" +
137                   " \"errorCode\": \"401\",\n" +
138                   " \"errorMessage\": \"Bad request\"\n" +
139                   "}" */
140
141         when(spy.getAuthHeader(mockExecution, "auth-value", "mso.msoKey")).thenReturn(authHeaderResponse)
142
143         String urlString = String.format("http://nbi.onap:8088/api/v4/serviceOrder/%s", networkServiceInstance.getServiceInstanceId())
144
145         String callDeleteServiceOrderResponse = "deleted"
146
147         when(spy.callDeleteServiceOrder(mockExecution, urlString, "auth-header")).thenReturn(callDeleteServiceOrderResponse)
148
149         spy.deleteServiceOrder(mockExecution)
150     }
151
152
153     @Test
154     void testGetNSSIAssociatedProfiles() {
155         def currentNSSI = [:]
156         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
157
158         ServiceInstance nssi = new ServiceInstance()
159         nssi.setServiceInstanceId("5G-999")
160
161         SliceProfiles sliceProfiles = new SliceProfiles()
162
163         List<SliceProfile> slProfiles = sliceProfiles.getSliceProfile()
164         slProfiles.add(new SliceProfile())
165         slProfiles.add(new SliceProfile())
166
167         nssi.setSliceProfiles(sliceProfiles)
168         currentNSSI.put("nssi", nssi)
169
170         DoDeallocateCoreNSSI obj = new DoDeallocateCoreNSSI()
171         obj.getNSSIAssociatedProfiles(mockExecution)
172
173         List<SliceProfile> associatedProfiles = (List<SliceProfile>)currentNSSI.get("associatedProfiles")
174         assertTrue("Either associatedProfiles doesn't exist or size is incorrect", (associatedProfiles != null && associatedProfiles.size() == 2))
175     }
176
177
178     @Test
179     void testCalculateSNSSAI() {
180         def currentNSSI = [:]
181         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
182
183         String theSNSSAI = "theS-NSSAI"
184
185         currentNSSI.put("S-NSSAI", theSNSSAI)
186
187         List<SliceProfile> associatedProfiles = new ArrayList<>()
188         SliceProfile sliceProfile1 = new SliceProfile()
189         sliceProfile1.setSNssai("snssai1")
190
191         SliceProfile sliceProfile2 = new SliceProfile()
192         sliceProfile2.setSNssai(theSNSSAI)
193
194         SliceProfile sliceProfile3 = new SliceProfile()
195         sliceProfile3.setSNssai("snssai3")
196
197         associatedProfiles.add(sliceProfile1)
198         associatedProfiles.add(sliceProfile2)
199         associatedProfiles.add(sliceProfile3)
200
201         int sizeBefore = associatedProfiles.size()
202
203         currentNSSI.put("associatedProfiles", associatedProfiles)
204
205         DoDeallocateCoreNSSI obj = new DoDeallocateCoreNSSI()
206         obj.calculateSNSSAI(mockExecution)
207
208         List<SliceProfile> snssais = (List<SliceProfile>)currentNSSI.get("S-NSSAIs")
209         SliceProfile sliceProfileContainsSNSSAI = (SliceProfile)currentNSSI.get("sliceProfileS-NSSAI")
210
211         assertTrue("Either snssais doesn't exist or size is incorrect", (snssais != null && snssais.size() == (sizeBefore - 1)))
212         assertNotNull("Slice Profile which contains given S-NSSAI not found", sliceProfileContainsSNSSAI)
213         assertTrue("Wrong Slice Profile", sliceProfileContainsSNSSAI.getSNssai().equals(theSNSSAI))
214     }
215
216
217     @Test
218     void testRemoveNSSIAssociationWithNSI() {
219         def currentNSSI = [:]
220
221         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
222
223         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
224
225         when(spy.getAAIClient()).thenReturn(client)
226
227         String nssiId = "5G-999"
228         String nsiId = "5G-99"
229         currentNSSI.put("nssiId", nssiId)
230         currentNSSI.put("nsiId", nsiId)
231
232         AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, nssiId)
233         AAIResourceUri nsiUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, nsiId)
234
235         doNothing().when(client).disconnect(nssiUri, nsiUri)
236
237         spy.removeNSSIAssociationWithNSI(mockExecution)
238
239     }
240
241
242     @Test
243     void testDeleteNSSIServiceInstance() {
244         def currentNSSI = [:]
245
246         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
247
248         String nssiId = "5G-999"
249
250         currentNSSI.put("nssiId", nssiId)
251
252         AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, nssiId)
253
254         DoDeallocateCoreNSSI spy = spy(DoDeallocateCoreNSSI.class)
255
256         when(spy.getAAIClient()).thenReturn(client)
257
258         doNothing().when(client).delete(nssiUri)
259
260         spy.deleteNSSIServiceInstance(mockExecution)
261     }
262
263
264 }