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.beans.nsmf.JobStatusResponse
 
  30 import org.onap.so.beans.nsmf.NssiResponse
 
  31 import org.onap.so.beans.nsmf.ResponseDescriptor
 
  32 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
 
  33 import org.onap.so.bpmn.core.domain.ServiceArtifact
 
  34 import org.onap.so.bpmn.core.domain.ServiceDecomposition
 
  35 import org.onap.so.bpmn.core.domain.ServiceInfo
 
  36 import org.onap.so.client.HttpClient
 
  37 import org.onap.so.client.HttpClientFactory
 
  38 import org.onap.so.client.aai.AAIObjectType
 
  39 import org.onap.so.client.aai.entities.uri.AAIResourceUri
 
  40 import org.onap.so.client.aai.entities.uri.AAIUriFactory
 
  42 import javax.ws.rs.core.Response
 
  44 import static org.junit.Assert.assertNotNull
 
  45 import static org.junit.Assert.assertTrue
 
  46 import static org.mockito.ArgumentMatchers.anyString
 
  47 import static org.mockito.ArgumentMatchers.eq
 
  48 import static org.mockito.Mockito.doNothing
 
  49 import static org.mockito.Mockito.mock
 
  50 import static org.mockito.Mockito.spy
 
  51 import static org.mockito.Mockito.times
 
  52 import static org.mockito.Mockito.when
 
  54 class DoDeallocateNSSITest extends MsoGroovyTest {
 
  56     private HttpClientFactory httpClientFactoryMock
 
  57     private HttpClient httpClientMock
 
  60     void init() throws IOException {
 
  61         super.init("DoDeallocateNSSITest")
 
  65     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
 
  69     void testPreProcessRequest(){
 
  71         currentNSSI.put("nssiServiceInstanceId","5G-999")
 
  72         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
 
  74         DoDeallocateNSSI ddnssi = new DoDeallocateNSSI()
 
  75         ddnssi.preProcessRequest(mockExecution)
 
  76         Mockito.verify(mockExecution,times(1)).getVariable("currentNSSI")
 
  80     void testPrepareDecomposeService(){
 
  82         currentNSSI.put("modelInvariantId", "21d57d4b-52ad-4d3c-a798-248b5bb9124b")
 
  83         currentNSSI.put("modelVersionId", "bfba363e-e39c-4bd9-a9d5-1371c28f4d22")
 
  84         currentNSSI.put("nssiServiceInstanceId","5G-999")
 
  85         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
 
  87         DoDeallocateNSSI ddnssi = new DoDeallocateNSSI()
 
  88         ddnssi.prepareDecomposeService(mockExecution)
 
  89         Mockito.verify(mockExecution,times(1)).setVariable(eq("serviceModelInfo"), captor.capture())
 
  90         String serviceModelInfo = captor.getValue()
 
  91         assertNotNull(serviceModelInfo)
 
  95     void testProcessDecomposition(){
 
  97         ServiceArtifact artifact = new ServiceArtifact()
 
  98         artifact.setContent(getArtifactContent())
 
  99         ServiceInfo serviceInfo = new ServiceInfo()
 
 100         List<ServiceArtifact> artifactList = new ArrayList<>()
 
 101         artifactList.add(artifact)
 
 102         serviceInfo.setServiceArtifact(artifactList)
 
 103         ServiceDecomposition decomposition = new ServiceDecomposition()
 
 104         decomposition.setServiceInfo(serviceInfo)
 
 105         when(mockExecution.getVariable("serviceDecomposition")).thenReturn(decomposition)
 
 106         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
 
 108         DoDeallocateNSSI ddnssi = new DoDeallocateNSSI()
 
 109         ddnssi.processDecomposition(mockExecution)
 
 110         String vendor = currentNSSI.get("vendor")
 
 111         assertNotNull(vendor)
 
 115     void testHandleJobStatus(){
 
 116         def currentNSSI = [:]
 
 117         currentNSSI.put("jobProgress", 10)
 
 118         currentNSSI.put("proportion", 90)
 
 119         currentNSSI.put("statusDescription","")
 
 120         currentNSSI.put("e2eServiceInstanceId","21d57d4b-52ad-4d3c-a798-248b5bb9124b")
 
 121         currentNSSI.put("operationId","4c614769-f58a-4556-8ad9-dcd903077c82")
 
 122         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
 
 124         DoDeallocateNSSI ddnssi = new DoDeallocateNSSI()
 
 125         ddnssi.handleJobStatus(mockExecution)
 
 126         Mockito.verify(mockExecution,times(1)).setVariable(eq("updateOperationStatus"), captor.capture())
 
 127         String updateOperationStatus= captor.getValue()
 
 128         assertNotNull(updateOperationStatus)
 
 132     void testDelSliceProfileFromAAI(){
 
 133         def currentNSSI = [:]
 
 134         currentNSSI.put("nssiServiceInstanceId", "5G-999")
 
 135         currentNSSI.put("profileId", "ddf57704-fe8d-417b-882d-2f2a12ddb225")
 
 136         currentNSSI.put("globalSubscriberId","5GCustomer")
 
 137         currentNSSI.put("serviceType","5G")
 
 138         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
 
 140         AAIResourceUri profileUri = AAIUriFactory.createResourceUri(AAIObjectType.SLICE_PROFILE, "5GCustomer", "5G", "5G-999", "ddf57704-fe8d-417b-882d-2f2a12ddb225")
 
 141         DoDeallocateNSSI obj = spy(DoDeallocateNSSI.class)
 
 142         when(obj.getAAIClient()).thenReturn(client)
 
 143         when(client.exists(profileUri)).thenReturn(true)
 
 144         doNothing().when(client).delete(profileUri)
 
 146         obj.delSliceProfileFromAAI(mockExecution)
 
 147         Mockito.verify(client,times(1)).delete(profileUri)
 
 151     void testSendRequestToNSSMF(){
 
 152         httpClientFactoryMock = mock(HttpClientFactory.class)
 
 153         httpClientMock = mock(HttpClient.class)
 
 155         def currentNSSI = [:]
 
 156         currentNSSI.put("snssai", "01-010101")
 
 157         currentNSSI.put("profileId", "ddf57704-fe8d-417b-882d-2f2a12ddb225")
 
 158         currentNSSI.put("nssiServiceInstanceId","5G-999")
 
 159         currentNSSI.put("nsiServiceInstanceId","5G-888")
 
 161         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
 
 162         when(mockExecution.getVariable("mso.adapters.nssmf.endpoint")).thenReturn("http://so-nssmf-adapter.onap:8088")
 
 163         String nssmfRequest = "http://so-nssmf-adapter.onap:8088/api/rest/provMns/v1/NSS/SliceProfiles/ddf57704-fe8d-417b-882d-2f2a12ddb225"
 
 165         when(httpClientFactoryMock.newJsonClient(new URL(nssmfRequest), ONAPComponents.EXTERNAL)).thenReturn(httpClientMock)
 
 166         DoDeallocateNSSI obj = spy(DoDeallocateNSSI.class)
 
 167         when(obj.getHttpClientFactory()).thenReturn(httpClientFactoryMock)
 
 168         Response responseMock = mock(Response.class)
 
 169         NssiResponse response = new NssiResponse()
 
 170         response.setNssiId("NSSI-C-004-HDBHZ-NSSMF-01-A-HW")
 
 171         response.setJobId("a5c5913d-448a-bcb1-9b800a944d84")
 
 172         when(httpClientMock.post(anyString())).thenReturn(responseMock)
 
 173         when(responseMock.getStatus()).thenReturn(202)
 
 174         when(responseMock.readEntity(NssiResponse.class)) thenReturn(response)
 
 175         when(responseMock.hasEntity()).thenReturn(true)
 
 177         obj.sendRequestToNSSMF(mockExecution)
 
 178         String jobId = currentNSSI['jobId']
 
 183     void testGetJobStatus(){
 
 184         httpClientFactoryMock = mock(HttpClientFactory.class)
 
 185         httpClientMock = mock(HttpClient.class)
 
 187         def currentNSSI = [:]
 
 188         currentNSSI.put("jobId", "a5c5913d-448a-bcb1-9b800a944d84")
 
 189         currentNSSI.put("nssiServiceInstanceId","5G-999")
 
 190         currentNSSI.put("nsiServiceInstanceId","5G-888")
 
 191         currentNSSI.put("jobProgress",60)
 
 193         when(mockExecution.getVariable("isNSSIDeAllocated")).thenReturn(false)
 
 194         when(mockExecution.getVariable("isNSSIDeAllocated")).thenReturn(false)
 
 195         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
 
 196         when(mockExecution.getVariable("mso.adapters.nssmf.endpoint")).thenReturn("http://so-nssmf-adapter.onap:8088")
 
 197         String nssmfRequest = "http://so-nssmf-adapter.onap:8088/api/rest/provMns/v1/NSS/jobs/a5c5913d-448a-bcb1-9b800a944d84"
 
 199         when(httpClientFactoryMock.newJsonClient(new URL(nssmfRequest), ONAPComponents.EXTERNAL)).thenReturn(httpClientMock)
 
 200         DoDeallocateNSSI obj = spy(DoDeallocateNSSI.class)
 
 201         when(obj.getHttpClientFactory()).thenReturn(httpClientFactoryMock)
 
 202         Response responseMock = mock(Response.class)
 
 203         ResponseDescriptor descriptor = new ResponseDescriptor()
 
 204         descriptor.setProgress(100)
 
 205         descriptor.setStatusDescription("finished deallocate nssi")
 
 206         JobStatusResponse jobStatusResponse = new JobStatusResponse()
 
 207         jobStatusResponse.setResponseDescriptor(descriptor)
 
 208         when(httpClientMock.post(anyString())).thenReturn(responseMock)
 
 209         when(responseMock.getStatus()).thenReturn(202)
 
 210         when(responseMock.readEntity(JobStatusResponse.class)) thenReturn(jobStatusResponse)
 
 211         when(responseMock.hasEntity()).thenReturn(true)
 
 213         obj.getJobStatus(mockExecution)
 
 214         Mockito.verify(mockExecution,times(1)).setVariable(eq("isNSSIDeAllocated"), captor.capture())
 
 215         boolean value = captor.getValue()
 
 220     private String getArtifactContent(){
 
 225                             "id":"NSST-C-001-HDBNJ-NSSMF-01-A-HW",
 
 229                             "description":"eMBB for demo",
 
 236                                 "constrainstsl":"less_or_equal",
 
 241                                 "constrainstsl":"less_or_equal",
 
 246                                 "constrainstsl":"less_or_equal",
 
 251                                 "constrainstsl":"less_or_equal",