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.so.bpmn.common.scripts.MsoGroovyTest
 
  29 import org.onap.so.bpmn.core.WorkflowException
 
  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.mockito.ArgumentMatchers.eq
 
  40 import static org.mockito.Mockito.*
 
  42 public class DeleteCommunicationServiceTest extends MsoGroovyTest {
 
  45     void init() throws IOException {
 
  46         super.init("DeleteCommunicationServiceTest")
 
  50     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
 
  53     void testPreProcessRequest(){
 
  54         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
 
  55         when(mockExecution.getVariable("bpmnRequest")).thenReturn("""
 
  57             "globalSubscriberId ":"5GCustomer",
 
  59         }""".replaceAll("\\\\s+", ""))
 
  60         when(mockExecution.getVariable("mso-request-id")).thenReturn("4c614769-f58a-4556-8ad9-dcd903077c82")
 
  62         DeleteCommunicationService delCS = new DeleteCommunicationService()
 
  63         delCS.preProcessRequest(mockExecution)
 
  64         Mockito.verify(mockExecution,times(3)).setVariable(captor.capture() as String, captor.capture())
 
  65         List<ExecutionEntity> values = captor.getAllValues()
 
  70     void testPreInitUpdateOperationStatus(){
 
  71         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
 
  72         when(mockExecution.getVariable("operationId")).thenReturn("998c2081-5a71-4a39-9ae6-d6b7c5bb50c0")
 
  73         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
 
  74         when(mockExecution.getVariable("serviceInstanceName")).thenReturn("5G-test")
 
  75         when(mockExecution.getVariable("result")).thenReturn("processing")
 
  76         when(mockExecution.getVariable("progress")).thenReturn("0")
 
  77         when(mockExecution.getVariable("operationContent")).thenReturn("delete communication service operation start")
 
  79         DeleteCommunicationService delCS = new DeleteCommunicationService()
 
  80         delCS.preInitUpdateOperationStatus(mockExecution)
 
  81         Mockito.verify(mockExecution,times(1)).setVariable(eq("updateOperationStatus"), captor.capture())
 
  82         String updateOperationStatus= captor.getAllValues()
 
  83         assertNotNull(updateOperationStatus)
 
  87     void testQueryCommunicationSeriveFromAAI(){
 
  88         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
 
  89         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
 
  90         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
 
  92         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "5GCustomer", "5G", "5ad89cf9-0569-4a93-9306-d8324321e2be")
 
  93         DeleteCommunicationService obj = spy(DeleteCommunicationService.class)
 
  95         AAIResultWrapper wrapper = new AAIResultWrapper(mockQueryCommunicationServiceReturn())
 
  96         when(obj.getAAIClient()).thenReturn(client)
 
  97         when(client.exists(resourceUri)).thenReturn(true)
 
  98         when(client.get(resourceUri, NotFoundException.class)).thenReturn(wrapper)
 
  99         obj.queryCommunicationSeriveFromAAI(mockExecution)
 
 100         Mockito.verify(mockExecution,times(1)).setVariable(eq("e2eSliceServiceInstanceId"), captor.capture())
 
 101         String e2eSliceServiceInstanceId = captor.getValue()
 
 102         assertNotNull(e2eSliceServiceInstanceId)
 
 106     void testPrepareCallCheckProcessStatus(){
 
 107         DeleteCommunicationService dcs = new DeleteCommunicationService()
 
 108         dcs.prepareCallCheckProcessStatus(mockExecution)
 
 109         Mockito.verify(mockExecution,times(1)).setVariable(eq("endProgress"), captor.capture())
 
 110         int endProgress = captor.getValue()
 
 111         assertEquals(90,endProgress)
 
 115     void testDelCSProfileFromAAI()
 
 117         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
 
 118         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
 
 119         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
 
 121         AAIResultWrapper wrapper = new AAIResultWrapper(mockQueryCommunicationServiceProfile())
 
 122         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.COMMUNICATION_PROFILE_ALL, "5GCustomer", "5G", "5ad89cf9-0569-4a93-9306-d8324321e2be")
 
 123         AAIResourceUri profileUri = AAIUriFactory.createResourceUri(AAIObjectType.COMMUNICATION_SERVICE_PROFILE, "5GCustomer", "5G", "5ad89cf9-0569-4a93-9306-d8324321e2be", "5G-111111")
 
 125         DeleteCommunicationService obj = spy(DeleteCommunicationService.class)
 
 126         when(obj.getAAIClient()).thenReturn(client)
 
 127         when(client.exists(resourceUri)).thenReturn(true)
 
 128         when(client.exists(profileUri)).thenReturn(true)
 
 129         when(client.get(resourceUri, NotFoundException.class)).thenReturn(wrapper)
 
 130         doNothing().when(client).delete(profileUri)
 
 131         obj.delCSProfileFromAAI(mockExecution)
 
 132         Mockito.verify(client,times(1)).delete(profileUri)
 
 136     void testPrepareFailureStatus(){
 
 137         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
 
 138         when(mockExecution.getVariable("operationId")).thenReturn("998c2081-5a71-4a39-9ae6-d6b7c5bb50c0")
 
 139         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
 
 140         when(mockExecution.getVariable("serviceInstanceName")).thenReturn("5G-test")
 
 141         when(mockExecution.getVariable("result")).thenReturn("finished")
 
 142         when(mockExecution.getVariable("progress")).thenReturn("100")
 
 143         when(mockExecution.getVariable("operationContent")).thenReturn("terminate service failure.")
 
 145         DeleteCommunicationService dcs = new DeleteCommunicationService()
 
 146         dcs.prepareFailureStatus(mockExecution)
 
 147         Mockito.verify(mockExecution,times(1)).setVariable(eq("updateOperationStatus"), captor.capture())
 
 148         String updateOperationStatus= captor.getAllValues()
 
 149         assertNotNull(updateOperationStatus)
 
 153     void testDelCSFromAAI(){
 
 154         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
 
 155         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
 
 156         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
 
 158         AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "5GCustomer", "5G", "5ad89cf9-0569-4a93-9306-d8324321e2be")
 
 159         DeleteCommunicationService obj = spy(DeleteCommunicationService.class)
 
 160         when(obj.getAAIClient()).thenReturn(client)
 
 161         doNothing().when(client).delete(serviceInstanceUri)
 
 163         obj.delCSFromAAI(mockExecution)
 
 164         Mockito.verify(mockExecution,times(1)).setVariable(eq("updateOperationStatus"), captor.capture())
 
 165         String updateOperationStatus= captor.getAllValues()
 
 166         assertNotNull(updateOperationStatus)
 
 170     void testPreFailedOperationStatus(){
 
 171         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
 
 172         when(mockExecution.getVariable("operationId")).thenReturn("998c2081-5a71-4a39-9ae6-d6b7c5bb50c0")
 
 173         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
 
 174         when(mockExecution.getVariable("serviceInstanceName")).thenReturn("5G-test")
 
 175         when(mockExecution.getVariable("result")).thenReturn("error")
 
 176         when(mockExecution.getVariable("progress")).thenReturn("100")
 
 177         when(mockExecution.getVariable("operationContent")).thenReturn("terminate service failure")
 
 179         DeleteCommunicationService deleteCommunicationService = new DeleteCommunicationService()
 
 180         WorkflowException exception = new WorkflowException("11113",7000,"terminate service failure")
 
 181         when(mockExecution.getVariable("WorkflowException")).thenReturn(exception)
 
 182         deleteCommunicationService.preFailedOperationStatus(mockExecution)
 
 184         Mockito.verify(mockExecution,times(1)).setVariable(eq("updateOperationStatus"), captor.capture())
 
 185         String updateOperationStatus= captor.getAllValues()
 
 186         assertNotNull(updateOperationStatus)
 
 190     private String mockQueryCommunicationServiceReturn()
 
 194                         "service-instance-id": "5G-666",
 
 195                         "service-instance-name": "eMBB_Slice_Communication_Service_5GCustomer",
 
 196                         "service-type": "eMBB",
 
 197                         "service-role": "communication-service",
 
 198                         "environment-context": "01-010101",
 
 199                         "workload-context": "12",
 
 200                         "created-at": "2019-12-11 19:56:00",
 
 202                         "model-invariant-id": "e75698d9-925a-4cdd-a6c0-edacbe6a0b51",
 
 203                         "model-version-id": "8ee5926d-720b-4bb2-86f9-d20e921c143b",
 
 204                         "service-instance-location-id": "300-01|300-02",
 
 205                         "resource-version": "1582623470778",
 
 206                         "orchestration-status": "created",
 
 207                         "relationship-list": {
 
 210                                     "related-to": "service-instance",
 
 211                                     "relationship-label": "org.onap.relationships.inventory.ComposedOf",
 
 212                                     "related-link": "/aai/v16/business/customers/customer/5GCustomer/service-subscriptions/service-subscription/5G/service-instances/service-instance/5G-777",
 
 213                                     "relationship-data": [
 
 215                                             "relationship-key": "customer.global-customer-id",
 
 216                                             "relationship-value": "5GCustomer"
 
 219                                             "relationship-key": "service-subscription.service-type",
 
 220                                             "relationship-value": "5G"
 
 223                                             "relationship-key": "service-instance.service-instance-id",
 
 224                                             "relationship-value": "5G-777"
 
 227                                     "related-to-property": [
 
 229                                             "property-key": "service-instance.service-instance-name",
 
 230                                             "property-value": "eMBB_e2e_Slice_Service_5GCustomer"
 
 240     private String mockQueryCommunicationServiceProfile()
 
 244             "communication-service-profile": [
 
 246                     "profile-id": "5G-111111",
 
 247                     "max-number-of-UEs": 50,
 
 248                     "coverage-area-list": "longgang,futian",
 
 250                     "exp-data-rate-UL": 300,
 
 251                     "exp-data-rate-DL": 500,
 
 252                     "ue-mobility-level": "stationary",
 
 253                     "resource-sharing-level": "Non-Shared",
 
 254                     "resource-version": "1577454950460"