9dbb6a5a569d9014af7cc0de31fa9a6cf323f5f6
[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 org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
23 import org.junit.Before
24 import org.junit.Test
25 import org.mockito.ArgumentCaptor
26 import org.mockito.Captor
27 import org.mockito.Mockito
28 import org.onap.aai.domain.yang.ServiceInstance
29 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
30 import org.onap.aaiclient.client.aai.AAIObjectType
31 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
32 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
33 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
34
35 import javax.ws.rs.NotFoundException
36
37 import static org.junit.Assert.assertEquals
38 import static org.junit.Assert.assertNotNull
39 import static org.junit.Assert.assertTrue
40 import static org.mockito.ArgumentMatchers.eq
41 import static org.mockito.Mockito.doNothing
42 import static org.mockito.Mockito.spy
43 import static org.mockito.Mockito.times
44 import static org.mockito.Mockito.verify
45 import static org.mockito.Mockito.when
46
47 class DoDeleteSliceServiceTest extends MsoGroovyTest {
48     @Before
49     void init() throws IOException {
50         super.init("DoDeleteSliceServiceTest")
51     }
52
53     @Captor
54     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
55
56     @Test
57     void testPreProcessRequest(){
58         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
59         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
60         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("e2a747a0-2ca6-476d-ac28-de999cf3fbfe")
61
62         DoDeleteSliceService doDeleteSliceService = new DoDeleteSliceService()
63         doDeleteSliceService.preProcessRequest(mockExecution)
64
65         Mockito.verify(mockExecution,times(1)).setVariable(captor.capture() as String, captor.capture())
66         List<ExecutionEntity> values = captor.getAllValues()
67         assertNotNull(values)
68     }
69
70     @Test
71     void testQueryE2ESliceSeriveFromAAI(){
72         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
73         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
74         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
75
76         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "5GCustomer", "5G", "5ad89cf9-0569-4a93-9306-d8324321e2be")
77         DoDeleteSliceService obj = spy(DoDeleteSliceService.class)
78
79         AAIResultWrapper wrapper = new AAIResultWrapper(mockQuerySliceServiceReturn())
80         when(obj.getAAIClient()).thenReturn(client)
81         when(client.exists(resourceUri)).thenReturn(true)
82         when(client.get(resourceUri, NotFoundException.class)).thenReturn(wrapper)
83         obj.queryE2ESliceSeriveFromAAI(mockExecution)
84         Mockito.verify(mockExecution,times(1)).setVariable(eq("snssai"), captor.capture())
85         String snssai = captor.getValue()
86         assertNotNull(snssai)
87     }
88
89     @Test
90     void testGetAllottedResFromAAI(){
91         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
92         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
93         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
94
95         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.ALLOTTED_RESOURCE_ALL, "5GCustomer", "5G", "5ad89cf9-0569-4a93-9306-d8324321e2be")
96         DoDeleteSliceService obj = spy(DoDeleteSliceService.class)
97
98         AAIResultWrapper wrapper = new AAIResultWrapper(mockQueryAllottedResource())
99         when(obj.getAAIClient()).thenReturn(client)
100         when(client.exists(resourceUri)).thenReturn(true)
101         when(client.get(resourceUri, NotFoundException.class)).thenReturn(wrapper)
102         obj.getAllottedResFromAAI(mockExecution)
103         Mockito.verify(mockExecution,times(1)).setVariable(eq("nsiId"), captor.capture())
104         String nsiId = captor.getValue()
105         assertNotNull(nsiId)
106     }
107
108     @Test
109     void testGetNSIFromAAI(){
110         when(mockExecution.getVariable("nsiId")).thenReturn("5G-888")
111         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
112         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
113
114         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "5GCustomer", "5G", "5G-888")
115         DoDeleteSliceService obj = spy(DoDeleteSliceService.class)
116
117         AAIResultWrapper wrapper = new AAIResultWrapper(mockNSIReturn())
118         when(obj.getAAIClient()).thenReturn(client)
119         when(client.exists(resourceUri)).thenReturn(true)
120         when(client.get(resourceUri, NotFoundException.class)).thenReturn(wrapper)
121         obj.getNSIFromAAI(mockExecution)
122         Mockito.verify(mockExecution,times(1)).setVariable(eq("nssiIdList"), captor.capture())
123         List<String> nssiIdList = captor.getValue()
124         assertNotNull(nssiIdList)
125     }
126
127     @Test
128     void testGetNSSIListFromAAI(){
129         List<String> nssiIdList = []
130         nssiIdList.add("5G-999")
131
132         when(mockExecution.getVariable("nssiIdList")).thenReturn(nssiIdList)
133         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
134         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
135
136         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "5GCustomer", "5G", "5G-999")
137         DoDeleteSliceService obj = spy(DoDeleteSliceService.class)
138
139         AAIResultWrapper wrapper = new AAIResultWrapper(mockNSSIReturn())
140         when(obj.getAAIClient()).thenReturn(client)
141         when(client.exists(resourceUri)).thenReturn(true)
142         when(client.get(resourceUri, NotFoundException.class)).thenReturn(wrapper)
143         obj.getNSSIListFromAAI(mockExecution)
144         Mockito.verify(mockExecution,times(1)).setVariable(eq("nssiInstanceList"), captor.capture())
145         List<ServiceInstance> nssiInstanceList = captor.getValue()
146         assertNotNull(nssiInstanceList)
147     }
148
149     @Test
150     void testGetCurrentNSSI(){
151         ServiceInstance nssi = new ServiceInstance()
152         nssi.setServiceInstanceId("5G-999")
153         nssi.setModelInvariantId("21d57d4b-52ad-4d3c-a798-248b5bb9124a")
154         nssi.setModelVersionId("bfba363e-e39c-4bd9-a9d5-1371c28f4d22")
155         List<ServiceInstance> nssiInstanceList = []
156         nssiInstanceList.add(nssi)
157         when(mockExecution.getVariable("currentNSSIIndex")).thenReturn(0)
158         when(mockExecution.getVariable("nssiInstanceList")).thenReturn(nssiInstanceList)
159         when(mockExecution.getVariable("snssai")).thenReturn("01-010101")
160         when(mockExecution.getVariable("nsiId")).thenReturn("5G-888")
161         when(mockExecution.getVariable("operationId")).thenReturn("998c2081-5a71-4a39-9ae6-d6b7c5bb50c0")
162         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5G-777")
163         when(mockExecution.getVariable("msoRequestId")).thenReturn("4c614769-f58a-4556-8ad9-dcd903077c82")
164         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
165         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
166         when(mockExecution.getVariable("proportion")).thenReturn("90")
167
168         DoDeleteSliceService ddss = new DoDeleteSliceService()
169         ddss.getCurrentNSSI(mockExecution)
170         verify(mockExecution,times(1)).setVariable(eq("currentNSSI"), captor.capture())
171         Map currentNSSI = captor.getValue()
172         assertTrue(currentNSSI.size()>0)
173     }
174
175     @Test
176     void testQuerySliceProfileFromAAI(){
177         def currentNSSI = [:]
178         currentNSSI.put("nssiServiceInstanceId","5G-999")
179         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
180         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
181         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
182
183         AAIResultWrapper wrapper = new AAIResultWrapper(mockSliceProfile())
184         AAIResourceUri profileUri = AAIUriFactory.createResourceUri(AAIObjectType.SLICE_PROFILE_ALL, "5GCustomer", "5G", "5G-999")
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 }