7b65d169a21bf3e983ebadfd6c8435ef73052c11
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  # Copyright (c) 2019, CMCC Technologies Co., Ltd.
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 package org.onap.so.bpmn.infrastructure.scripts
21
22 import static org.junit.Assert.assertNotNull
23 import static org.junit.Assert.assertTrue
24 import static org.mockito.ArgumentMatchers.eq
25 import static org.mockito.Mockito.spy
26 import static org.mockito.Mockito.times
27 import static org.mockito.Mockito.verify
28 import static org.mockito.Mockito.when
29 import javax.ws.rs.NotFoundException
30 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
31 import org.junit.Before
32 import org.junit.Test
33 import org.mockito.ArgumentCaptor
34 import org.mockito.Captor
35 import org.mockito.Mockito
36 import org.onap.aai.domain.yang.ServiceInstance
37 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
38 import org.onap.aaiclient.client.aai.entities.uri.AAIPluralResourceUri
39 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
40 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
41 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
42 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
43 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
44
45 class DoDeleteSliceServiceTest extends MsoGroovyTest {
46     @Before
47     void init() throws IOException {
48         super.init("DoDeleteSliceServiceTest")
49     }
50
51     @Captor
52     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
53
54     @Test
55     void testPreProcessRequest(){
56         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
57         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
58         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("e2a747a0-2ca6-476d-ac28-de999cf3fbfe")
59
60         DoDeleteSliceService doDeleteSliceService = new DoDeleteSliceService()
61         doDeleteSliceService.preProcessRequest(mockExecution)
62
63         Mockito.verify(mockExecution,times(1)).setVariable(captor.capture() as String, captor.capture())
64         List<ExecutionEntity> values = captor.getAllValues()
65         assertNotNull(values)
66     }
67
68     @Test
69     void testQueryE2ESliceSeriveFromAAI(){
70         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
71         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
72         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
73
74         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer("5GCustomer").serviceSubscription("5G").serviceInstance("5ad89cf9-0569-4a93-9306-d8324321e2be"))
75         DoDeleteSliceService obj = spy(DoDeleteSliceService.class)
76
77         AAIResultWrapper wrapper = new AAIResultWrapper(mockQuerySliceServiceReturn())
78         when(obj.getAAIClient()).thenReturn(client)
79         when(client.exists(resourceUri)).thenReturn(true)
80         when(client.get(resourceUri, NotFoundException.class)).thenReturn(wrapper)
81         obj.queryE2ESliceSeriveFromAAI(mockExecution)
82         Mockito.verify(mockExecution,times(1)).setVariable(eq("snssai"), captor.capture())
83         String snssai = captor.getValue()
84         assertNotNull(snssai)
85     }
86
87     @Test
88     void testGetAllottedResFromAAI(){
89         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
90         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
91         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
92
93         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(
94             AAIFluentTypeBuilder.business().customer("5GCustomer").serviceSubscription("5G").serviceInstance("5ad89cf9-0569-4a93-9306-d8324321e2be").allottedResources())
95             DoDeleteSliceService obj = spy(DoDeleteSliceService.class)
96
97         AAIResultWrapper wrapper = new AAIResultWrapper(mockQueryAllottedResource())
98         when(obj.getAAIClient()).thenReturn(client)
99         when(client.exists(resourceUri)).thenReturn(true)
100         when(client.get(resourceUri, NotFoundException.class)).thenReturn(wrapper)
101         obj.getAllottedResFromAAI(mockExecution)
102         Mockito.verify(mockExecution,times(1)).setVariable(eq("nsiId"), captor.capture())
103         String nsiId = captor.getValue()
104         assertNotNull(nsiId)
105     }
106
107     @Test
108     void testGetNSIFromAAI(){
109         when(mockExecution.getVariable("nsiId")).thenReturn("5G-888")
110         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
111         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
112
113         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer("5GCustomer").serviceSubscription("5G").serviceInstance("5G-888"))
114         DoDeleteSliceService obj = spy(DoDeleteSliceService.class)
115
116         AAIResultWrapper wrapper = new AAIResultWrapper(mockNSIReturn())
117         when(obj.getAAIClient()).thenReturn(client)
118         when(client.exists(resourceUri)).thenReturn(true)
119         when(client.get(resourceUri, NotFoundException.class)).thenReturn(wrapper)
120         obj.getNSIFromAAI(mockExecution)
121         Mockito.verify(mockExecution,times(1)).setVariable(eq("nssiIdList"), captor.capture())
122         List<String> nssiIdList = captor.getValue()
123         assertNotNull(nssiIdList)
124     }
125
126     @Test
127     void testGetNSSIListFromAAI(){
128         List<String> nssiIdList = []
129         nssiIdList.add("5G-999")
130
131         when(mockExecution.getVariable("nssiIdList")).thenReturn(nssiIdList)
132         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
133         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
134
135         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer("5GCustomer").serviceSubscription("5G").serviceInstance("5G-999"))
136         DoDeleteSliceService obj = spy(DoDeleteSliceService.class)
137
138         AAIResultWrapper wrapper = new AAIResultWrapper(mockNSSIReturn())
139         when(obj.getAAIClient()).thenReturn(client)
140         when(client.exists(resourceUri)).thenReturn(true)
141         when(client.get(resourceUri, NotFoundException.class)).thenReturn(wrapper)
142         obj.getNSSIListFromAAI(mockExecution)
143         Mockito.verify(mockExecution,times(1)).setVariable(eq("nssiInstanceList"), captor.capture())
144         List<ServiceInstance> nssiInstanceList = captor.getValue()
145         assertNotNull(nssiInstanceList)
146     }
147
148     @Test
149     void testGetCurrentNSSI(){
150         ServiceInstance nssi = new ServiceInstance()
151         nssi.setServiceInstanceId("5G-999")
152         nssi.setModelInvariantId("21d57d4b-52ad-4d3c-a798-248b5bb9124a")
153         nssi.setModelVersionId("bfba363e-e39c-4bd9-a9d5-1371c28f4d22")
154         List<ServiceInstance> nssiInstanceList = []
155         nssiInstanceList.add(nssi)
156         when(mockExecution.getVariable("currentNSSIIndex")).thenReturn(0)
157         when(mockExecution.getVariable("nssiInstanceList")).thenReturn(nssiInstanceList)
158         when(mockExecution.getVariable("snssai")).thenReturn("01-010101")
159         when(mockExecution.getVariable("nsiId")).thenReturn("5G-888")
160         when(mockExecution.getVariable("operationId")).thenReturn("998c2081-5a71-4a39-9ae6-d6b7c5bb50c0")
161         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5G-777")
162         when(mockExecution.getVariable("msoRequestId")).thenReturn("4c614769-f58a-4556-8ad9-dcd903077c82")
163         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
164         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
165         when(mockExecution.getVariable("proportion")).thenReturn("90")
166
167         DoDeleteSliceService ddss = new DoDeleteSliceService()
168         ddss.getCurrentNSSI(mockExecution)
169         verify(mockExecution,times(1)).setVariable(eq("currentNSSI"), captor.capture())
170         Map currentNSSI = captor.getValue()
171         assertTrue(currentNSSI.size()>0)
172     }
173
174     @Test
175     void testQuerySliceProfileFromAAI(){
176         def currentNSSI = [:]
177         currentNSSI.put("nssiServiceInstanceId","5G-999")
178         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
179         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
180         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
181
182         AAIResultWrapper wrapper = new AAIResultWrapper(mockSliceProfile())
183         AAIPluralResourceUri profileUri = AAIUriFactory.createResourceUri(
184             AAIFluentTypeBuilder.business().customer("5GCustomer").serviceSubscription("5G").serviceInstance("5G-999").sliceProfiles())
185
186         DoDeleteSliceService obj = spy(DoDeleteSliceService.class)
187         when(obj.getAAIClient()).thenReturn(client)
188         when(client.exists(profileUri)).thenReturn(true)
189         when(client.get(profileUri, NotFoundException.class)).thenReturn(wrapper)
190         obj.querySliceProfileFromAAI(mockExecution)
191         verify(mockExecution,times(1)).setVariable(eq("currentNSSI"), captor.capture())
192         Map value = captor.getValue()
193         assertNotNull(currentNSSI.get('profileId'))
194     }
195
196     @Test
197     void parseNextNSSI(){
198         ServiceInstance nssi = new ServiceInstance()
199         nssi.setServiceInstanceId("5G-999")
200         nssi.setModelInvariantId("21d57d4b-52ad-4d3c-a798-248b5bb9124b")
201         nssi.setModelVersionId("bfba363e-e39c-4bd9-a9d5-1371c28f4d22")
202         List<ServiceInstance> nssiInstanceList = []
203         nssiInstanceList.add(nssi)
204         when(mockExecution.getVariable("currentNSSIIndex")).thenReturn(0)
205         when(mockExecution.getVariable("nssiInstanceList")).thenReturn(nssiInstanceList)
206
207         DoDeleteSliceService ddss = new DoDeleteSliceService()
208         ddss.parseNextNSSI(mockExecution)
209         verify(mockExecution,times(1)).setVariable(eq("isAllNSSIFinished"), captor.capture())
210         boolean isAllNSSIFinished = captor.getValue()
211         assertTrue(isAllNSSIFinished)
212     }
213
214     private String mockSliceProfile(){
215         String expect =
216                 """{
217                         "slice-profile": [
218                             {
219                                 "profile-id": "ddf57704-fe8d-417b-882d-2f2a12ddb225",
220                                 "latency": 20,
221                                 "max-number-of-UEs": 0,
222                                 "coverage-area-TA-list": "[{\\"province\\":\\"??\\",\\"city\\":\\"???\\",\\"county\\":\\"???\\",\\"street\\":\\"?????\\"}]",
223                                 "ue-mobility-level": "stationary",
224                                 "resource-sharing-level": "0",
225                                 "exp-data-rate-UL": 100,
226                                 "exp-data-rate-DL": 100,
227                                 "activity-factor": 0,
228                                 "e2e-latency": 0,
229                                 "jitter": 0,
230                                 "survival-time": 0,
231                                 "exp-data-rate": 0,
232                                 "payload-size": 0,
233                                 "traffic-density": 0,
234                                 "conn-density": 0,
235                                 "s-nssai": "01003",
236                                 "resource-version": "1580800791373"
237                             }
238                         ]
239                     }
240                 """
241         return expect
242     }
243
244     private String mockNSSIReturn(){
245         String expect =
246                 """
247                     {
248                         "service-instance-id": "5G-999",
249                         "service-instance-name": "eMBB_Slice_NSSI_5GCustomer",
250                         "service-type": "eMBB",
251                         "service-role": "nssi",
252                         "environment-context": "cn",
253                         "model-invariant-id": "21d57d4b-52ad-4d3c-a798-248b5bb9124a",
254                         "model-version-id": "bfba363e-e39c-4bd9-a9d5-1371c28f4d22",
255                         "service-instance-location-id": "300-01|300-02",
256                         "resource-version": "1578449638032",
257                         "orchestration-status": "activated",
258                         "relationship-list": {
259                             "relationship": [
260                                 {
261                                     "related-to": "service-instance",
262                                     "relationship-label": "org.onap.relationships.inventory.ComposedOf",
263                                     "related-link": "/aai/v16/business/customers/customer/5GCustomer/service-subscriptions/service-subscription/5G/service-instances/service-instance/5G-888",
264                                     "relationship-data": [
265                                         {
266                                             "relationship-key": "customer.global-customer-id",
267                                             "relationship-value": "5GCustomer"
268                                         },
269                                         {
270                                             "relationship-key": "service-subscription.service-type",
271                                             "relationship-value": "5G"
272                                         },
273                                         {
274                                             "relationship-key": "service-instance.service-instance-id",
275                                             "relationship-value": "5G-888"
276                                         }
277                                     ],
278                                     "related-to-property": [
279                                         {
280                                             "property-key": "service-instance.service-instance-name",
281                                             "property-value": "eMBB_e2e_Slice_Service_5GCustomer"
282                                         }
283                                     ]
284                                 }
285                             ]
286                         }
287                     }
288                 """
289         return expect
290     }
291
292     private String mockNSIReturn(){
293         String expect =
294                 """
295                     {
296                         "service-instance-id": "5G-888",
297                         "service-instance-name": "eMBB_e2e_Slice_Service_5GCustomer",
298                         "service-type": "embb",
299                         "service-role": "nsi",
300                         "model-invariant-id": "0e9bcb9a-c832-433b-a0c1-74866768f608",
301                         "model-version-id": "2c5fd79d-0f84-4057-9222-952cb6f27036",
302                         "service-instance-location-id": "300-01|300-02",
303                         "resource-version": "1579691104911",
304                         "orchestration-status": "activated",
305                         "relationship-list": {
306                             "relationship": [
307                                 {
308                                     "related-to": "service-instance",
309                                     "relationship-label": "org.onap.relationships.inventory.ComposedOf",
310                                     "related-link": "/aai/v16/business/customers/customer/5GCustomer/service-subscriptions/service-subscription/5G/service-instances/service-instance/5G-999",
311                                     "relationship-data": [
312                                         {
313                                             "relationship-key": "customer.global-customer-id",
314                                             "relationship-value": "5GCustomer"
315                                         },
316                                         {
317                                             "relationship-key": "service-subscription.service-type",
318                                             "relationship-value": "5G"
319                                         },
320                                         {
321                                             "relationship-key": "service-instance.service-instance-id",
322                                             "relationship-value": "5G-999"
323                                         }
324                                     ],
325                                     "related-to-property": [
326                                         {
327                                             "property-key": "service-instance.service-instance-name",
328                                             "property-value": "eMBB_Slice_NSSI_5GCustomer"
329                                         }
330                                     ]
331                                 },
332                                 {
333                                     "related-to": "allotted-resource",
334                                     "relationship-label": "org.onap.relationships.inventory.Uses",
335                                     "related-link": "/aai/v16/business/customers/customer/5GCustomer/service-subscriptions/service-subscription/5G/service-instances/service-instance/5G-777/allotted-resources/allotted-resource/5G-1234",
336                                     "relationship-data": [
337                                         {
338                                             "relationship-key": "customer.global-customer-id",
339                                             "relationship-value": "5GCustomer"
340                                         },
341                                         {
342                                             "relationship-key": "service-subscription.service-type",
343                                             "relationship-value": "5G"
344                                         },
345                                         {
346                                             "relationship-key": "service-instance.service-instance-id",
347                                             "relationship-value": "5G-777"
348                                         },
349                                         {
350                                             "relationship-key": "allotted-resource.id",
351                                             "relationship-value": "5G-1234"
352                                         }
353                                     ],
354                                     "related-to-property": [
355                                         {
356                                             "property-key": "allotted-resource.description"
357                                         },
358                                         {
359                                             "property-key": "allotted-resource.allotted-resource-name"
360                                         }
361                                     ]
362                                 }
363                             ]
364                         }
365                     }
366                 """
367         return expect
368     }
369
370     private String mockQueryAllottedResource(){
371         String expect =
372                 """{
373                         "allotted-resource": [
374                             {
375                                 "id": "5G-1234",
376                                 "resource-version": "1577454983471",
377                                 "relationship-list": {
378                                     "relationship": [
379                                         {
380                                             "related-to": "service-instance",
381                                             "relationship-label": "org.onap.relationships.inventory.Uses",
382                                             "related-link": "/aai/v16/business/customers/customer/5GCustomer/service-subscriptions/service-subscription/5G/service-instances/service-instance/5G-888",
383                                             "relationship-data": [
384                                                 {
385                                                     "relationship-key": "customer.global-customer-id",
386                                                     "relationship-value": "5GCustomer"
387                                                 },
388                                                 {
389                                                     "relationship-key": "service-subscription.service-type",
390                                                     "relationship-value": "5G"
391                                                 },
392                                                 {
393                                                     "relationship-key": "service-instance.service-instance-id",
394                                                     "relationship-value": "5G-888"
395                                                 }
396                                             ],
397                                             "related-to-property": [
398                                                 {
399                                                     "property-key": "service-instance.service-instance-name",
400                                                     "property-value": "eMBB_e2e_Slice_Service_5GCustomer"
401                                                 }
402                                             ]
403                                         }
404                                     ]
405                                 }
406                             }
407                         ]
408                     }
409                 """
410         return expect
411     }
412
413     String mockQuerySliceServiceReturn(){
414         String expect =
415                 """{
416                         "service-instance-id": "5G-777",
417                         "service-instance-name": "eMBB_e2e_Slice_Service_5GCustomer",
418                         "service-type": "embb",
419                         "service-role": "e2eslice-service",
420                         "environment-context": "01-010101",
421                         "model-invariant-id": "e65d737a-41e0-4ad1-958f-56defdf2e907",
422                         "model-version-id": "f2f5967e-72d3-4c5c-b880-e214e71dba4e",
423                         "service-instance-location-id": "300-01|300-02",
424                         "resource-version": "1578449638436",
425                         "orchestration-status": "activated",
426                         "relationship-list": {
427                             "relationship": [
428                                 {
429                                     "related-to": "service-instance",
430                                     "relationship-label": "org.onap.relationships.inventory.ComposedOf",
431                                     "related-link": "/aai/v16/business/customers/customer/5GCustomer/service-subscriptions/service-subscription/5G/service-instances/service-instance/5G-666",
432                                     "relationship-data": [
433                                         {
434                                             "relationship-key": "customer.global-customer-id",
435                                             "relationship-value": "5GCustomer"
436                                         },
437                                         {
438                                             "relationship-key": "service-subscription.service-type",
439                                             "relationship-value": "5G"
440                                         },
441                                         {
442                                             "relationship-key": "service-instance.service-instance-id",
443                                             "relationship-value": "5G-666"
444                                         }
445                                     ],
446                                     "related-to-property": [
447                                         {
448                                             "property-key": "service-instance.service-instance-name",
449                                             "property-value": "eMBB_Slice_Communication_Service_5GCustomer"
450                                         }
451                                     ]
452                                 }
453                             ]
454                         }
455                     }
456                 """
457         return expect
458     }
459
460 }