Changes in Modify and Deallocate Core NSSI flows
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / groovy / org / onap / so / bpmn / infrastructure / scripts / DoModifyCoreNSSITest.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 com.fasterxml.jackson.databind.ObjectMapper
24 import org.junit.Before
25 import org.junit.Test
26 import org.mockito.Mockito
27 import org.onap.aai.domain.yang.v19.AllottedResource
28 import org.onap.aai.domain.yang.v19.ServiceInstance
29 import org.onap.aai.domain.yang.v19.SliceProfile
30 import org.onap.aai.domain.yang.v19.SliceProfiles
31 import org.onap.aaiclient.client.aai.entities.AAIEdgeLabel
32 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
33 import org.onap.aaiclient.client.aai.entities.Relationships
34 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
35 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
36 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
37 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
38 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
39 import org.springframework.data.domain.Slice
40
41 import static org.junit.Assert.assertNotNull
42 import static org.junit.Assert.assertTrue
43 import static org.mockito.Mockito.*
44
45 class DoModifyCoreNSSITest extends MsoGroovyTest  {
46
47     @Before
48     void init() throws IOException {
49         super.init("DoModifyCoreNSSITest")
50     }
51
52
53     @Test
54     void testPreProcessRequest() {
55
56         String nssiId = "5G-999"
57         when(mockExecution.getVariable("serviceInstanceID")).thenReturn(nssiId)
58
59         String nsiId = "5G-777"
60         when(mockExecution.getVariable("nsiId")).thenReturn(nsiId)
61
62         String snssai = "S-NSSAI"
63         String snssaiList = "[ \"${snssai}\" ]"
64         String sliceProfileId = "slice-profile-id"
65         String modifyAction = "allocate"
66         String sliceParams =  "{\n" +
67                 "\"sliceProfileId\":\"${sliceProfileId}\",\"snssaiList\":${snssaiList}\n" +
68                 ",\"modifyAction\":\"${modifyAction}\"\n" +
69                 "}"
70         when(mockExecution.getVariable("sliceParams")).thenReturn(sliceParams)
71
72         DoModifyCoreNSSI obj = new DoModifyCoreNSSI()
73         obj.preProcessRequest(mockExecution)
74
75         def currentNSSI = [:]
76         currentNSSI.put("nssiId", nssiId)
77         currentNSSI.put("nsiId", nsiId)
78         currentNSSI.put("sliceProfile", sliceParams)
79         currentNSSI.put("S-NSSAI", snssai)
80         currentNSSI.put("sliceProfileId", sliceProfileId)
81
82         Mockito.verify(mockExecution,times(1)).setVariable("currentNSSI", currentNSSI)
83
84     }
85
86
87     @Test
88     void testCalculateSNSSAISliceProfileInstanceHasToBeDeleted() {
89         def currentNSSI = [:]
90         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
91
92         when(mockExecution.getVariable("isCreateSliceProfileInstance")).thenReturn("false")
93
94         String theSNSSAI = "theS-NSSAI"
95
96         currentNSSI.put("S-NSSAI", theSNSSAI)
97
98         String sliceProfileId = "the-slice-profile-id"
99         currentNSSI['sliceProfileId'] = sliceProfileId
100
101         List<SliceProfile> associatedProfiles = new ArrayList<>()
102         SliceProfile sliceProfile1 = new SliceProfile()
103         sliceProfile1.setProfileId(sliceProfileId)
104         sliceProfile1.setSNssai(theSNSSAI)
105
106         SliceProfile sliceProfile2 = new SliceProfile()
107         sliceProfile2.setProfileId("sp-2")
108         sliceProfile2.setSNssai(theSNSSAI)
109
110         SliceProfile sliceProfile3 = new SliceProfile()
111         sliceProfile3.setProfileId("sp-3")
112         sliceProfile3.setSNssai("snssai2")
113
114         associatedProfiles.add(sliceProfile1)
115         associatedProfiles.add(sliceProfile2)
116         associatedProfiles.add(sliceProfile3)
117
118         currentNSSI.put("associatedProfiles", associatedProfiles)
119
120         when(mockExecution.getVariable("isCreateSliceProfileInstance" )).thenReturn(false)
121
122         DoModifyCoreNSSI obj = new DoModifyCoreNSSI()
123         obj.calculateSNSSAI(mockExecution)
124
125         List<SliceProfile> snssais = (List<SliceProfile>)currentNSSI.get("S-NSSAIs")
126         SliceProfile sliceProfileContainsSNSSAI = (SliceProfile)currentNSSI.get("sliceProfileS-NSSAI")
127
128         assertTrue("Either snssais doesn't exist or size is incorrect", (snssais != null && snssais.size() == (associatedProfiles.size() - 1)))
129         assertNotNull("Slice Profile which contains given S-NSSAI not found", sliceProfileContainsSNSSAI)
130         assertTrue("Wrong Slice Profile", sliceProfileContainsSNSSAI.getSNssai().equals(theSNSSAI))
131     }
132
133
134     @Test
135     void testCalculateSNSSAISliceProfileInstanceHasToBeCreated() {
136         def currentNSSI = [:]
137         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
138
139         when(mockExecution.getVariable("isCreateSliceProfileInstance")).thenReturn("true")
140
141         String theSNSSAI = "theS-NSSAI"
142
143         currentNSSI.put("S-NSSAI", theSNSSAI)
144
145         List<SliceProfile> associatedProfiles = new ArrayList<>()
146         SliceProfile sliceProfile1 = new SliceProfile()
147         sliceProfile1.setSNssai("snssai1")
148
149         SliceProfile sliceProfile2 = new SliceProfile()
150         sliceProfile2.setSNssai("snssai2")
151
152         associatedProfiles.add(sliceProfile1)
153         associatedProfiles.add(sliceProfile2)
154
155         int sizeBefore = associatedProfiles.size()
156
157         currentNSSI.put("associatedProfiles", associatedProfiles)
158
159         DoModifyCoreNSSI obj = new DoModifyCoreNSSI()
160         obj.calculateSNSSAI(mockExecution)
161
162         List<SliceProfile> snssais = (List<SliceProfile>)currentNSSI.get("S-NSSAIs")
163
164         assertTrue("Either snssais doesn't exist or size is incorrect", (snssais != null && snssais.size() == (sizeBefore + 1)))
165
166     }
167
168
169     @Test
170     void testCreateSliceProfileInstance() {
171         def currentNSSI = [:]
172
173         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
174
175         String sliceProfileId = "sliceProfileId"
176
177         currentNSSI.put("sliceProfile", "{\"sliceProfileId\":\"slice-profile-id\",\"snssaiList\":[\"S-NSSAI\"],\"expDataRateUL\":\"12\",\"expDataRateDL\":\"5\"," +
178                         "\"activityFactor\":\"2\",\"resourceSharingLevel\":\"resource-sharing-level\",\"uEMobilityLevel\":\"ue-mobility-level\",\"coverageAreaTAList\":\"coverage-area-ta-list\"," +
179                         "\"maxNumberofUEs\":\"10000\",\"latency\":\"7\"}")
180         currentNSSI.put("sliceProfileId", sliceProfileId)
181
182         List<String> snssais = new ArrayList<>()
183         snssais.add("s-nssai")
184         currentNSSI.put("S-NSSAIs", snssais)
185
186         DoModifyCoreNSSI spy = spy(DoModifyCoreNSSI.class)
187         when(spy.getAAIClient()).thenReturn(client)
188
189         String globalSubscriberId = "globalSubscriberId"
190         String subscriptionServiceType = "subscription-service-type"
191         String nssiId = "nssiId"
192
193         when(mockExecution.getVariable("globalSubscriberId")).thenReturn(globalSubscriberId)
194         when(mockExecution.getVariable("subscriptionServiceType")).thenReturn(subscriptionServiceType)
195
196         currentNSSI.put("nssiId", nssiId)
197
198         ServiceInstance sliceProfileInstance = new ServiceInstance()
199         sliceProfileInstance.setServiceInstanceId(UUID.randomUUID().toString())
200
201         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).
202                                                             serviceInstance(sliceProfileInstance.getServiceInstanceId()))
203
204         SliceProfile sliceProfile = new SliceProfile()
205         sliceProfile.setProfileId(sliceProfileId)
206
207         doNothing().when(client).create(uri, sliceProfileInstance)
208
209         spy.createSliceProfileInstance(mockExecution)
210
211         assertTrue("Slice Profile Instance Id doesn't exist", (currentNSSI.get("createdSliceProfileInstanceId")) != null)
212
213     }
214
215
216     @Test
217     void testAssociateSliceProfileInstanceWithNSSI() {
218         def currentNSSI = [:]
219
220         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
221
222         DoModifyCoreNSSI spy = spy(DoModifyCoreNSSI.class)
223         when(spy.getAAIClient()).thenReturn(client)
224
225         String sliceProfileId = "sliceProfileId"
226
227         when(mockExecution.getVariable("sliceProfileID")).thenReturn(sliceProfileId)
228
229         String nssiId = "5G-999"
230         currentNSSI.put("nssiId", nssiId)
231
232         String globalSubscriberId = "globalSubscriberId"
233         String subscriptionServiceType = "subscriptionServiceType"
234
235         String sliceProfileInstanceId = "slice-rpofile-instance-id"
236         currentNSSI.put("createdSliceProfileInstanceId", sliceProfileInstanceId)
237
238         AllottedResource allottedResource = new AllottedResource()
239
240         String allottedResourceId = UUID.randomUUID().toString()
241
242         allottedResource.setId(allottedResourceId)
243
244         AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(sliceProfileInstanceId).allottedResource(allottedResourceId))
245         doNothing().when(client).create(allottedResourceUri, allottedResource)
246
247         currentNSSI.put("allottedResourceUri", allottedResourceUri)
248
249         when(mockExecution.getVariable("globalSubscriberId")).thenReturn(globalSubscriberId)
250         when(mockExecution.getVariable("subscriptionServiceType")).thenReturn(subscriptionServiceType)
251
252         currentNSSI.put("sliceProfileId", sliceProfileId)
253
254         SliceProfile sliceProfile = new SliceProfile()
255         currentNSSI.put("createdSliceProfile", sliceProfile)
256
257         AAIResourceUri sliceProfileInstanceUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(sliceProfileInstanceId))
258
259         AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
260
261         ServiceInstance sliceProfileInstance = new ServiceInstance()
262         sliceProfileInstance.setServiceInstanceId(sliceProfileInstanceId)
263         Optional<ServiceInstance> sliceProfileInstanceOpt = Optional.of(sliceProfileInstance)
264
265         when(client.get(ServiceInstance.class, sliceProfileInstanceUri)).thenReturn(sliceProfileInstanceOpt)
266         doNothing().when(client).update(sliceProfileInstanceUri, sliceProfileInstance)
267
268         ServiceInstance nssi = new ServiceInstance()
269         nssi.setServiceInstanceId(nssiId)
270         nssi.setSliceProfiles(new SliceProfiles())
271         currentNSSI.put("nssi", nssi)
272
273         doNothing().when(client).connect(nssiUri, sliceProfileInstanceUri, AAIEdgeLabel.USES)
274
275         spy.associateSliceProfileInstanceWithNSSI(mockExecution)
276
277     }
278
279
280     @Test
281     void testGetNSSIAssociatedProfilesCreateSLiceProfileEmptySLiceProfiles() {
282         invokeGetNSSIAssociatedProfiles("true", true)
283     }
284
285     @Test
286     void testGetNSSIAssociatedProfilesCreateSLiceProfileNonEmptySliceProfiles() {
287         invokeGetNSSIAssociatedProfiles("true", false)
288     }
289
290     @Test
291     void testGetNSSIAssociatedProfilesDeleteSLiceProfile() {
292         invokeGetNSSIAssociatedProfiles("false", false)
293     }
294
295     void invokeGetNSSIAssociatedProfiles(String isCreateSliceProfileInstance, boolean isEmptySliceProfiles) {
296         def currentNSSI = [:]
297         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
298
299         String globalSubscriberId = "5GCustomer"
300         String subscriptionServiceType = "5G"
301         when(mockExecution.getVariable("globalSubscriberId")).thenReturn(globalSubscriberId)
302         when(mockExecution.getVariable("subscriptionServiceType")).thenReturn(subscriptionServiceType)
303
304         when(mockExecution.getVariable("isCreateSliceProfileInstance")).thenReturn(isCreateSliceProfileInstance)
305
306         String nssiId = "5G-999"
307         ServiceInstance nssi = new ServiceInstance()
308         nssi.setServiceInstanceId(nssiId)
309         currentNSSI.put("nssiId", nssiId)
310
311         String theSLiceProfileId = "slice-profile-id"
312         currentNSSI.put("sliceProfileId", theSLiceProfileId)
313
314         SliceProfiles sliceProfiles = new SliceProfiles()
315
316         List<SliceProfile> slProfiles = sliceProfiles.getSliceProfile()
317
318         if(!isEmptySliceProfiles) {
319             SliceProfile sp1 = new SliceProfile()
320             sp1.setProfileId(theSLiceProfileId)
321             slProfiles.add(sp1)
322             slProfiles.add(new SliceProfile())
323         }
324
325         //nssi.setSliceProfiles(sliceProfiles)
326         currentNSSI.put("nssi", nssi)
327
328         DoModifyCoreNSSI spy = spy(DoModifyCoreNSSI.class)
329         when(spy.getAAIClient()).thenReturn(client)
330
331         AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
332
333         AAIResultWrapper wrapperMock = mock(AAIResultWrapper.class) //new AAIResultWrapper(json)
334         Relationships rsMock = mock(Relationships.class)
335         Optional<Relationships> orsMock = Optional.of(rsMock)
336         List<AAIResourceUri> allottedUris = new ArrayList<>()
337         AAIResourceUri allottedUri = AAIUriFactory.createResourceUri(Types.ALLOTTED_RESOURCE.getFragment("allotted-id"))
338         allottedUris.add(allottedUri)
339
340         when(client.get(nssiUri)).thenReturn(wrapperMock)
341         when(wrapperMock.getRelationships()).thenReturn(orsMock)
342         when(rsMock.getRelatedUris(Types.ALLOTTED_RESOURCE)).thenReturn(allottedUris)
343
344         String sliceProfileInstanceId = "slice-profile-instance-id"
345         ServiceInstance sliceProfileInstance = new ServiceInstance()
346         sliceProfileInstance.setServiceInstanceId(sliceProfileInstanceId)
347         sliceProfileInstance.setServiceRole("slice-profile-instance")
348
349         List<AAIResourceUri> sliceProfileInstanceUris = new ArrayList<>()
350         AAIResourceUri sliceProfileInstanceUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(sliceProfileInstance.getServiceInstanceId()))
351         sliceProfileInstanceUris.add(sliceProfileInstanceUri)
352
353         Optional<ServiceInstance> sliceProfileInstanceOpt = Optional.of(sliceProfileInstance)
354
355         when(client.get(allottedUri)).thenReturn(wrapperMock)
356         when(rsMock.getRelatedUris(Types.SERVICE_INSTANCE)).thenReturn(sliceProfileInstanceUris)
357         when(client.get(ServiceInstance.class, sliceProfileInstanceUri)).thenReturn(sliceProfileInstanceOpt)
358
359
360         SliceProfiles sps = new SliceProfiles()
361         sps.getSliceProfile().addAll(slProfiles)
362         sliceProfileInstance.setSliceProfiles(sps)
363
364         Optional<SliceProfiles> sliceProfilesOpt = Optional.of(sps)
365
366         org.onap.aaiclient.client.generated.fluentbuilders.SliceProfiles sliceProfilesType =
367                 AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(sliceProfileInstance.getServiceInstanceId()).sliceProfiles()
368
369         def sliceProfilesUri = AAIUriFactory.createResourceUri(sliceProfilesType)
370         when(client.exists(sliceProfilesUri)).thenReturn(true)
371
372         when(client.get(sliceProfilesUri)).thenReturn(wrapperMock)
373
374         when(wrapperMock.asBean(SliceProfiles.class)).thenReturn(sliceProfilesOpt)
375
376         spy.getNSSIAssociatedProfiles(mockExecution)
377
378         List<SliceProfile> associatedProfiles = (List<SliceProfile>)currentNSSI.get("associatedProfiles")
379         int expectedSize = sps.getSliceProfile().size()
380
381         if(!isEmptySliceProfiles) {
382             assertTrue("sliceProfileInstanceUri not found in contect Map", currentNSSI.get("sliceProfileInstanceUri") != null)
383         }
384
385         assertTrue("Either associatedProfiles doesn't exist or size is incorrect", (associatedProfiles != null && associatedProfiles.size() == expectedSize))
386     }
387
388
389 }