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