2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  # Copyright (c) 2019, CMCC Technologies Co., Ltd.
 
   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
 
  11  #       http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  20 package org.onap.so.bpmn.infrastructure.scripts
 
  22 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
 
  23 import org.junit.Before
 
  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.so.client.aai.AAIObjectType
 
  31 import org.onap.so.client.aai.entities.AAIResultWrapper
 
  32 import org.onap.so.client.aai.entities.uri.AAIResourceUri
 
  33 import org.onap.so.client.aai.entities.uri.AAIUriFactory
 
  35 import javax.ws.rs.NotFoundException
 
  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
 
  47 class DoDeleteSliceServiceTest extends MsoGroovyTest {
 
  49     void init() throws IOException {
 
  50         super.init("DoDeleteSliceServiceTest")
 
  54     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
 
  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")
 
  62         DoDeleteSliceService doDeleteSliceService = new DoDeleteSliceService()
 
  63         doDeleteSliceService.preProcessRequest(mockExecution)
 
  65         Mockito.verify(mockExecution,times(1)).setVariable(captor.capture() as String, captor.capture())
 
  66         List<ExecutionEntity> values = captor.getAllValues()
 
  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")
 
  76         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "5GCustomer", "5G", "5ad89cf9-0569-4a93-9306-d8324321e2be")
 
  77         DoDeleteSliceService obj = spy(DoDeleteSliceService.class)
 
  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()
 
  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")
 
  95         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.ALLOTTED_RESOURCE_ALL, "5GCustomer", "5G", "5ad89cf9-0569-4a93-9306-d8324321e2be")
 
  96         DoDeleteSliceService obj = spy(DoDeleteSliceService.class)
 
  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()
 
 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")
 
 114         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "5GCustomer", "5G", "5G-888")
 
 115         DoDeleteSliceService obj = spy(DoDeleteSliceService.class)
 
 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)
 
 128     void testGetNSSIListFromAAI(){
 
 129         List<String> nssiIdList = []
 
 130         nssiIdList.add("5G-999")
 
 132         when(mockExecution.getVariable("nssiIdList")).thenReturn(nssiIdList)
 
 133         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
 
 134         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
 
 136         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "5GCustomer", "5G", "5G-999")
 
 137         DoDeleteSliceService obj = spy(DoDeleteSliceService.class)
 
 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)
 
 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")
 
 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)
 
 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")
 
 183         AAIResultWrapper wrapper = new AAIResultWrapper(mockSliceProfile())
 
 184         AAIResourceUri profileUri = AAIUriFactory.createResourceUri(AAIObjectType.SLICE_PROFILE_ALL, "5GCustomer", "5G", "5G-999")
 
 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'))
 
 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)
 
 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)
 
 214     private String mockSliceProfile(){
 
 219                                 "profile-id": "ddf57704-fe8d-417b-882d-2f2a12ddb225",
 
 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,
 
 233                                 "traffic-density": 0,
 
 236                                 "resource-version": "1580800791373"
 
 244     private String mockNSSIReturn(){
 
 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": {
 
 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": [
 
 266                                             "relationship-key": "customer.global-customer-id",
 
 267                                             "relationship-value": "5GCustomer"
 
 270                                             "relationship-key": "service-subscription.service-type",
 
 271                                             "relationship-value": "5G"
 
 274                                             "relationship-key": "service-instance.service-instance-id",
 
 275                                             "relationship-value": "5G-888"
 
 278                                     "related-to-property": [
 
 280                                             "property-key": "service-instance.service-instance-name",
 
 281                                             "property-value": "eMBB_e2e_Slice_Service_5GCustomer"
 
 292     private String mockNSIReturn(){
 
 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": {
 
 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": [
 
 313                                             "relationship-key": "customer.global-customer-id",
 
 314                                             "relationship-value": "5GCustomer"
 
 317                                             "relationship-key": "service-subscription.service-type",
 
 318                                             "relationship-value": "5G"
 
 321                                             "relationship-key": "service-instance.service-instance-id",
 
 322                                             "relationship-value": "5G-999"
 
 325                                     "related-to-property": [
 
 327                                             "property-key": "service-instance.service-instance-name",
 
 328                                             "property-value": "eMBB_Slice_NSSI_5GCustomer"
 
 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": [
 
 338                                             "relationship-key": "customer.global-customer-id",
 
 339                                             "relationship-value": "5GCustomer"
 
 342                                             "relationship-key": "service-subscription.service-type",
 
 343                                             "relationship-value": "5G"
 
 346                                             "relationship-key": "service-instance.service-instance-id",
 
 347                                             "relationship-value": "5G-777"
 
 350                                             "relationship-key": "allotted-resource.id",
 
 351                                             "relationship-value": "5G-1234"
 
 354                                     "related-to-property": [
 
 356                                             "property-key": "allotted-resource.description"
 
 359                                             "property-key": "allotted-resource.allotted-resource-name"
 
 370     private String mockQueryAllottedResource(){
 
 373                         "allotted-resource": [
 
 376                                 "resource-version": "1577454983471",
 
 377                                 "relationship-list": {
 
 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": [
 
 385                                                     "relationship-key": "customer.global-customer-id",
 
 386                                                     "relationship-value": "5GCustomer"
 
 389                                                     "relationship-key": "service-subscription.service-type",
 
 390                                                     "relationship-value": "5G"
 
 393                                                     "relationship-key": "service-instance.service-instance-id",
 
 394                                                     "relationship-value": "5G-888"
 
 397                                             "related-to-property": [
 
 399                                                     "property-key": "service-instance.service-instance-name",
 
 400                                                     "property-value": "eMBB_e2e_Slice_Service_5GCustomer"
 
 413     String mockQuerySliceServiceReturn(){
 
 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": {
 
 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": [
 
 434                                             "relationship-key": "customer.global-customer-id",
 
 435                                             "relationship-value": "5GCustomer"
 
 438                                             "relationship-key": "service-subscription.service-type",
 
 439                                             "relationship-value": "5G"
 
 442                                             "relationship-key": "service-instance.service-instance-id",
 
 443                                             "relationship-value": "5G-666"
 
 446                                     "related-to-property": [
 
 448                                             "property-key": "service-instance.service-instance-name",
 
 449                                             "property-value": "eMBB_Slice_Communication_Service_5GCustomer"