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.logging.filter.base.ONAPComponents
 
  29 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
 
  30 import org.onap.so.bpmn.core.WorkflowException
 
  31 import org.onap.so.client.HttpClient
 
  32 import org.onap.so.client.HttpClientFactory
 
  33 import org.onap.aaiclient.client.aai.AAIObjectType
 
  34 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
 
  35 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
 
  36 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
 
  38 import javax.ws.rs.NotFoundException
 
  39 import javax.ws.rs.core.Response
 
  41 import static org.junit.Assert.assertEquals
 
  42 import static org.junit.Assert.assertNotNull
 
  43 import static org.mockito.ArgumentMatchers.eq
 
  44 import static org.mockito.Mockito.*
 
  46 class DeleteCommunicationServiceTest extends MsoGroovyTest {
 
  48     private HttpClientFactory httpClientFactoryMock
 
  49     private HttpClient httpClientMock
 
  52     void init() throws IOException {
 
  53         super.init("DeleteCommunicationServiceTest")
 
  57     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
 
  60     void testPreProcessRequest(){
 
  61         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
 
  62         when(mockExecution.getVariable("bpmnRequest")).thenReturn("""
 
  64             "globalSubscriberId ":"5GCustomer",
 
  66         }""".replaceAll("\\\\s+", ""))
 
  67         when(mockExecution.getVariable("mso-request-id")).thenReturn("4c614769-f58a-4556-8ad9-dcd903077c82")
 
  69         DeleteCommunicationService delCS = new DeleteCommunicationService()
 
  70         delCS.preProcessRequest(mockExecution)
 
  71         Mockito.verify(mockExecution,times(3)).setVariable(captor.capture() as String, captor.capture())
 
  72         List<ExecutionEntity> values = captor.getAllValues()
 
  77     void testPreInitUpdateOperationStatus(){
 
  78         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
 
  79         when(mockExecution.getVariable("operationId")).thenReturn("998c2081-5a71-4a39-9ae6-d6b7c5bb50c0")
 
  80         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
 
  81         when(mockExecution.getVariable("serviceInstanceName")).thenReturn("5G-test")
 
  82         when(mockExecution.getVariable("result")).thenReturn("processing")
 
  83         when(mockExecution.getVariable("progress")).thenReturn("0")
 
  84         when(mockExecution.getVariable("operationContent")).thenReturn("delete communication service operation start")
 
  86         DeleteCommunicationService delCS = new DeleteCommunicationService()
 
  87         delCS.preInitUpdateOperationStatus(mockExecution)
 
  88         Mockito.verify(mockExecution,times(1)).setVariable(eq("updateOperationStatus"), captor.capture())
 
  89         String updateOperationStatus= captor.getValue()
 
  90         assertNotNull(updateOperationStatus)
 
  94     void testQueryCommunicationSeriveFromAAI(){
 
  95         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
 
  96         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
 
  97         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
 
  99         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "5GCustomer", "5G", "5ad89cf9-0569-4a93-9306-d8324321e2be")
 
 100         DeleteCommunicationService obj = spy(DeleteCommunicationService.class)
 
 102         AAIResultWrapper wrapper = new AAIResultWrapper(mockQueryCommunicationServiceReturn())
 
 103         when(obj.getAAIClient()).thenReturn(client)
 
 104         when(client.exists(resourceUri)).thenReturn(true)
 
 105         when(client.get(resourceUri, NotFoundException.class)).thenReturn(wrapper)
 
 106         obj.queryCommunicationSeriveFromAAI(mockExecution)
 
 107         Mockito.verify(mockExecution,times(1)).setVariable(eq("e2eSliceServiceInstanceId"), captor.capture())
 
 108         String e2eSliceServiceInstanceId = captor.getValue()
 
 109         assertNotNull(e2eSliceServiceInstanceId)
 
 113     void testPrepareCallCheckProcessStatus(){
 
 114         DeleteCommunicationService dcs = new DeleteCommunicationService()
 
 115         dcs.prepareCallCheckProcessStatus(mockExecution)
 
 116         Mockito.verify(mockExecution,times(1)).setVariable(eq("endProgress"), captor.capture())
 
 117         int endProgress = captor.getValue()
 
 118         assertEquals(90,endProgress)
 
 122     void testDelCSProfileFromAAI()
 
 124         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
 
 125         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
 
 126         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
 
 128         AAIResultWrapper wrapper = new AAIResultWrapper(mockQueryCommunicationServiceProfile())
 
 129         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.COMMUNICATION_PROFILE_ALL, "5GCustomer", "5G", "5ad89cf9-0569-4a93-9306-d8324321e2be")
 
 130         AAIResourceUri profileUri = AAIUriFactory.createResourceUri(AAIObjectType.COMMUNICATION_SERVICE_PROFILE, "5GCustomer", "5G", "5ad89cf9-0569-4a93-9306-d8324321e2be", "5G-111111")
 
 132         DeleteCommunicationService obj = spy(DeleteCommunicationService.class)
 
 133         when(obj.getAAIClient()).thenReturn(client)
 
 134         when(client.exists(resourceUri)).thenReturn(true)
 
 135         when(client.exists(profileUri)).thenReturn(true)
 
 136         when(client.get(resourceUri, NotFoundException.class)).thenReturn(wrapper)
 
 137         doNothing().when(client).delete(profileUri)
 
 138         obj.delCSProfileFromAAI(mockExecution)
 
 139         Mockito.verify(client,times(1)).delete(profileUri)
 
 143     void testPrepareFailureStatus(){
 
 144         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
 
 145         when(mockExecution.getVariable("operationId")).thenReturn("998c2081-5a71-4a39-9ae6-d6b7c5bb50c0")
 
 146         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
 
 147         when(mockExecution.getVariable("serviceInstanceName")).thenReturn("5G-test")
 
 148         when(mockExecution.getVariable("result")).thenReturn("finished")
 
 149         when(mockExecution.getVariable("progress")).thenReturn("100")
 
 150         when(mockExecution.getVariable("operationContent")).thenReturn("terminate service failure.")
 
 152         DeleteCommunicationService dcs = new DeleteCommunicationService()
 
 153         dcs.prepareFailureStatus(mockExecution)
 
 154         Mockito.verify(mockExecution,times(1)).setVariable(eq("updateOperationStatus"), captor.capture())
 
 155         String updateOperationStatus= captor.getValue()
 
 156         assertNotNull(updateOperationStatus)
 
 160     void testDelCSFromAAI(){
 
 161         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
 
 162         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
 
 163         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
 
 165         AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "5GCustomer", "5G", "5ad89cf9-0569-4a93-9306-d8324321e2be")
 
 166         DeleteCommunicationService obj = spy(DeleteCommunicationService.class)
 
 167         when(obj.getAAIClient()).thenReturn(client)
 
 168         doNothing().when(client).delete(serviceInstanceUri)
 
 170         obj.delCSFromAAI(mockExecution)
 
 171         Mockito.verify(mockExecution,times(1)).setVariable(eq("updateOperationStatus"), captor.capture())
 
 172         String updateOperationStatus= captor.getValue()
 
 173         assertNotNull(updateOperationStatus)
 
 177     void testPreFailedOperationStatus(){
 
 178         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
 
 179         when(mockExecution.getVariable("operationId")).thenReturn("998c2081-5a71-4a39-9ae6-d6b7c5bb50c0")
 
 180         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
 
 181         when(mockExecution.getVariable("serviceInstanceName")).thenReturn("5G-test")
 
 182         when(mockExecution.getVariable("result")).thenReturn("error")
 
 183         when(mockExecution.getVariable("progress")).thenReturn("100")
 
 184         when(mockExecution.getVariable("operationContent")).thenReturn("terminate service failure")
 
 186         DeleteCommunicationService deleteCommunicationService = new DeleteCommunicationService()
 
 187         WorkflowException exception = new WorkflowException("11113",7000,"terminate service failure")
 
 188         when(mockExecution.getVariable("WorkflowException")).thenReturn(exception)
 
 189         deleteCommunicationService.preFailedOperationStatus(mockExecution)
 
 191         Mockito.verify(mockExecution,times(1)).setVariable(eq("updateOperationStatus"), captor.capture())
 
 192         String updateOperationStatus= captor.getValue()
 
 193         assertNotNull(updateOperationStatus)
 
 197     void testSendRequest2NSMFWF(){
 
 198         httpClientMock = mock(HttpClient.class)
 
 199         httpClientFactoryMock = mock(HttpClientFactory.class)
 
 200         String url ="http://so.onap:8080/onap/so/infra/e2eServiceInstances/v3/5G-777"
 
 201         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
 
 202         when(mockExecution.getVariable("operationId")).thenReturn("998c2081-5a71-4a39-9ae6-d6b7c5bb50c0")
 
 203         when(mockExecution.getVariable("progress")).thenReturn("20")
 
 204         when(mockExecution.getVariable("operationContent")).thenReturn("waiting nsmf service delete finished")
 
 205         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
 
 206         when(mockExecution.getVariable("mso.infra.endpoint.url")).thenReturn("http://so.onap:8080/onap/so/infra")
 
 207         when(mockExecution.getVariable("e2eSliceServiceInstanceId")).thenReturn("5G-777")
 
 208         when(mockExecution.getVariable("e2eOperationId")).thenReturn("e151059a-d924-4629-845f-264db19e50b3")
 
 209         when(httpClientFactoryMock.newJsonClient(new URL(url), ONAPComponents.SO)).thenReturn(httpClientMock)
 
 210         DeleteCommunicationService obj = spy(DeleteCommunicationService.class)
 
 212         Response responseMock = mock(Response.class)
 
 213         when(responseMock.getStatus()).thenReturn(200)
 
 214         when(responseMock.hasEntity()).thenReturn(true)
 
 215         when(responseMock.getEntity()).thenReturn(getNSSMFResponse())
 
 216         when(obj.getHttpClientFactory()).thenReturn(httpClientFactoryMock)
 
 217         when(httpClientMock.delete(anyString())).thenReturn(responseMock)
 
 219         obj.sendRequest2NSMFWF(mockExecution)
 
 220         Mockito.verify(mockExecution,times(1)).setVariable(eq("updateOperationStatus"), captor.capture())
 
 221         String updateOperationStatus= captor.getValue()
 
 222         assertNotNull(updateOperationStatus)
 
 225     private String getNSSMFResponse(){
 
 226         String response = """{
 
 228                 "serviceId":"e151059a-d924-4629-845f-264db19e50b4",        
 
 229                 "operationId":"e151059a-d924-4629-845f-264db19e50b3"        
 
 238     private String mockQueryCommunicationServiceReturn()
 
 242                         "service-instance-id": "5G-666",
 
 243                         "service-instance-name": "eMBB_Slice_Communication_Service_5GCustomer",
 
 244                         "service-type": "eMBB",
 
 245                         "service-role": "communication-service",
 
 246                         "environment-context": "01-010101",
 
 247                         "workload-context": "12",
 
 248                         "created-at": "2019-12-11 19:56:00",
 
 250                         "model-invariant-id": "e75698d9-925a-4cdd-a6c0-edacbe6a0b51",
 
 251                         "model-version-id": "8ee5926d-720b-4bb2-86f9-d20e921c143b",
 
 252                         "service-instance-location-id": "300-01|300-02",
 
 253                         "resource-version": "1582623470778",
 
 254                         "orchestration-status": "created",
 
 255                         "relationship-list": {
 
 258                                     "related-to": "service-instance",
 
 259                                     "relationship-label": "org.onap.relationships.inventory.ComposedOf",
 
 260                                     "related-link": "/aai/v16/business/customers/customer/5GCustomer/service-subscriptions/service-subscription/5G/service-instances/service-instance/5G-777",
 
 261                                     "relationship-data": [
 
 263                                             "relationship-key": "customer.global-customer-id",
 
 264                                             "relationship-value": "5GCustomer"
 
 267                                             "relationship-key": "service-subscription.service-type",
 
 268                                             "relationship-value": "5G"
 
 271                                             "relationship-key": "service-instance.service-instance-id",
 
 272                                             "relationship-value": "5G-777"
 
 275                                     "related-to-property": [
 
 277                                             "property-key": "service-instance.service-instance-name",
 
 278                                             "property-value": "eMBB_e2e_Slice_Service_5GCustomer"
 
 288     private String mockQueryCommunicationServiceProfile()
 
 292             "communication-service-profile": [
 
 294                     "profile-id": "5G-111111",
 
 295                     "max-number-of-UEs": 50,
 
 296                     "coverage-area-list": "longgang,futian",
 
 298                     "exp-data-rate-UL": 300,
 
 299                     "exp-data-rate-DL": 500,
 
 300                     "ue-mobility-level": "stationary",
 
 301                     "resource-sharing-level": "Non-Shared",
 
 302                     "resource-version": "1577454950460"