From fb516859f920197eba4cbc1f5b82279f3e3753c6 Mon Sep 17 00:00:00 2001 From: "raviteja.karumuri" Date: Wed, 13 Jul 2022 13:17:49 +0100 Subject: [SO] SO changes to support Delete AS Issue-ID: SO-4084 Signed-off-by: raviteja.karumuri Change-Id: Ib75076bdd98291b48149a9ad859fd383a551cd45 --- .../adapter/cnfm/tasks/CnfDeleteTaskTest.java | 126 +++++++++++++++++ .../adapter/cnfm/tasks/CnfInstantiateTaskTest.java | 14 +- .../tasks/CnfmHttpServiceProviderImplTest.java | 140 ++++++++++++++++++- .../cnfm/tasks/MonitorCnfmCreateJobTaskTest.java | 152 --------------------- .../adapter/cnfm/tasks/MonitorCnfmJobTaskTest.java | 143 +++++++++++++++++++ 5 files changed, 412 insertions(+), 163 deletions(-) create mode 100644 bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/CnfDeleteTaskTest.java delete mode 100644 bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/MonitorCnfmCreateJobTaskTest.java create mode 100644 bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/MonitorCnfmJobTaskTest.java (limited to 'bpmn/so-bpmn-tasks/src/test/java/org/onap') diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/CnfDeleteTaskTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/CnfDeleteTaskTest.java new file mode 100644 index 0000000000..4764aa00f1 --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/CnfDeleteTaskTest.java @@ -0,0 +1,126 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.so.bpmn.infrastructure.adapter.cnfm.tasks; + + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import java.net.URI; +import java.util.Optional; +import javax.swing.text.html.Option; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.so.bpmn.common.BuildingBlockExecution; +import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf; +import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB; +import org.onap.so.client.exception.BBObjectNotFoundException; +import org.onap.so.client.exception.ExceptionBuilder; +import org.onap.so.cnfm.lcm.model.TerminateAsRequest; +import org.reactivestreams.Publisher; + +/** + * @author raviteja.kaumuri@est.tech + */ +@RunWith(MockitoJUnitRunner.class) +public class CnfDeleteTaskTest { + + @Mock + private CnfmHttpServiceProvider cnfmHttpServiceProvider; + @Mock + private ExceptionBuilder exceptionUtil; + @Mock + private ExtractPojosForBB extractPojosForBB; + private CnfDeleteTask cnfDeleteTask; + private static final String TERMINATE_AS_REQUEST_OBJECT = "TerminateAsRequest"; + private static final String CNFM_REQUEST_STATUS_CHECK_URL = "CnfmStatusCheckUrl"; + private final BuildingBlockExecution stubbedExecution = new StubbedBuildingBlockExecution(); + + @Before + public void setup() { + cnfDeleteTask = new CnfDeleteTask(cnfmHttpServiceProvider, exceptionUtil, extractPojosForBB); + } + + @Test + public void test_createTerminateAsRequest_success() { + cnfDeleteTask.createTerminateAsRequest(stubbedExecution); + assertNotNull(stubbedExecution.getVariable(TERMINATE_AS_REQUEST_OBJECT)); + } + + @Test + public void test_invokeCnfmToTerminateAsInstance_success() throws BBObjectNotFoundException { + stubbedExecution.setVariable(TERMINATE_AS_REQUEST_OBJECT, getTerminateAsRequest()); + when(extractPojosForBB.extractByKey(Mockito.any(), Mockito.any())).thenReturn(getGenericVnf()); + when(cnfmHttpServiceProvider.invokeTerminateAsRequest(Mockito.anyString(), + Mockito.any(TerminateAsRequest.class))).thenReturn(getURI()); + cnfDeleteTask.invokeCnfmToTerminateAsInstance(stubbedExecution); + URI returnedContent = stubbedExecution.getVariable(CNFM_REQUEST_STATUS_CHECK_URL); + assertEquals(getURI().orElseThrow().getPath(), returnedContent.getPath()); + } + + @Test + public void test_invokeCnfmToTerminateAsInstance_Exception() throws BBObjectNotFoundException { + stubbedExecution.setVariable(TERMINATE_AS_REQUEST_OBJECT, getTerminateAsRequest()); + when(extractPojosForBB.extractByKey(Mockito.any(), Mockito.any())).thenThrow(new RuntimeException()); + cnfDeleteTask.invokeCnfmToTerminateAsInstance(stubbedExecution); + verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(2002), + any(Exception.class)); + } + + @Test + public void test_invokeCnfmToDeleteAsInstance_success() throws BBObjectNotFoundException { + when(extractPojosForBB.extractByKey(Mockito.any(), Mockito.any())).thenReturn(getGenericVnf()); + when(cnfmHttpServiceProvider.invokeDeleteAsRequest(Mockito.anyString())).thenReturn(Optional.of(Boolean.TRUE)); + CnfDeleteTask mockCnfDeleteTask = Mockito.spy(cnfDeleteTask); + mockCnfDeleteTask.invokeCnfmToDeleteAsInstance(stubbedExecution); + verify(mockCnfDeleteTask, times(1)).invokeCnfmToDeleteAsInstance(stubbedExecution); + } + + @Test + public void test_invokeCnfmToDeleteAsInstance_Exception() throws BBObjectNotFoundException { + when(extractPojosForBB.extractByKey(Mockito.any(), Mockito.any())).thenThrow(new RuntimeException()); + cnfDeleteTask.invokeCnfmToDeleteAsInstance(stubbedExecution); + verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(2003), + any(Exception.class)); + } + + private GenericVnf getGenericVnf() { + GenericVnf genericVnf = new GenericVnf(); + genericVnf.setVnfId("12345"); + return genericVnf; + } + + private TerminateAsRequest getTerminateAsRequest() { + return new TerminateAsRequest(); + } + + private Optional getURI() { + return Optional.of(URI.create("test_sample")); + } +} diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/CnfInstantiateTaskTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/CnfInstantiateTaskTest.java index f4d8bb8439..79b47776f2 100644 --- a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/CnfInstantiateTaskTest.java +++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/CnfInstantiateTaskTest.java @@ -33,19 +33,20 @@ import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.INPUT import static org.onap.so.cnfm.lcm.model.utils.AdditionalParamsConstants.CLOUD_OWNER_PARAM_KEY; import static org.onap.so.cnfm.lcm.model.utils.AdditionalParamsConstants.CLOUD_REGION_PARAM_KEY; import static org.onap.so.cnfm.lcm.model.utils.AdditionalParamsConstants.TENANT_ID_PARAM_KEY; -import java.net.URI; -import java.util.Optional; import java.util.Collections; +import java.util.Optional; import java.util.UUID; import org.camunda.bpm.engine.delegate.BpmnError; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.Mock; -import org.onap.so.bpmn.BaseTaskTest; +import org.mockito.junit.MockitoJUnitRunner; import org.onap.so.bpmn.common.BuildingBlockExecution; import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.InputParameter; import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf; import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock; import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoGenericVnf; +import org.onap.so.client.exception.ExceptionBuilder; import org.onap.so.cnfm.lcm.model.AsInstance; import org.onap.so.cnfm.lcm.model.CreateAsRequest; import org.onap.so.cnfm.lcm.model.InstantiateAsRequest; @@ -54,12 +55,13 @@ import org.onap.so.cnfm.lcm.model.InstantiateAsRequest; /** * @author raviteja.kaumuri@est.tech */ -public class CnfInstantiateTaskTest extends BaseTaskTest { +@RunWith(MockitoJUnitRunner.class) +public class CnfInstantiateTaskTest { + @Mock + protected ExceptionBuilder exceptionUtil; private static final String CREATE_AS_REQUEST_OBJECT = "CreateAsRequestObject"; private static final String INSTANTIATE_AS_REQUEST_OBJECT = "InstantiateAsRequest"; - private static final String CNFM_REQUEST_STATUS_CHECK_URL = "CnfmStatusCheckUrl"; - private static final String MONITOR_JOB_NAME = "MonitorJobName"; private static final String MODEL_INSTANCE_NAME = "instanceTest"; private static final String AS_INSTANCE_ID = "asInstanceid"; private static final String CLOUD_OWNER = "CloudOwner"; diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/CnfmHttpServiceProviderImplTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/CnfmHttpServiceProviderImplTest.java index aac685fe37..aab44cee3d 100644 --- a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/CnfmHttpServiceProviderImplTest.java +++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/CnfmHttpServiceProviderImplTest.java @@ -22,6 +22,7 @@ package org.onap.so.bpmn.infrastructure.adapter.cnfm.tasks; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; import java.net.URI; @@ -33,9 +34,12 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import org.onap.so.cnfm.lcm.model.AsInstance; +import org.onap.so.cnfm.lcm.model.AsLcmOpOcc; import org.onap.so.cnfm.lcm.model.CreateAsRequest; import org.onap.so.cnfm.lcm.model.InstantiateAsRequest; +import org.onap.so.cnfm.lcm.model.TerminateAsRequest; import org.onap.so.rest.service.HttpRestServiceProvider; +import org.onap.so.rest.service.HttpRestServiceProviderImpl; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -49,10 +53,13 @@ public class CnfmHttpServiceProviderImplTest { @Mock private CnfmUrlProvider cnfmUrlProvider; @Mock - private HttpRestServiceProvider httpRestServiceProvider; + private HttpRestServiceProviderImpl httpRestServiceProvider; @Mock private ResponseEntity responseEntity; + @Mock + private ResponseEntity responseEntityVoid; + private ResponseEntity responseEntityAsLCM; private final String createURL = "http://so-cnfm-lcm.onap:9888/so/so-cnfm/v1/api/aslcm/v1/as_instances"; private final String instantiateURL = "http://so-cnfm-lcm.onap:9888/so/so-cnfm/v1/api/aslcm/v1/as_instances" + getAsInstance().getAsInstanceid() + "/instantiate"; @@ -60,6 +67,7 @@ public class CnfmHttpServiceProviderImplTest { private final URI uri = URI.create("sample"); private final CreateAsRequest createAsRequest = new CreateAsRequest(); private final InstantiateAsRequest instantiateAsRequest = new InstantiateAsRequest(); + private final TerminateAsRequest terminateAsRequest = new TerminateAsRequest(); @Before public void setup() { @@ -69,7 +77,7 @@ public class CnfmHttpServiceProviderImplTest { } @Test - public void invokeCreateAsRequest_withStatuscodeSuccess_hasBody() { + public void invokeCreateAsRequest_withStatuscode_Success_hasBody() { when(cnfmUrlProvider.getCreateAsRequestUrl()).thenReturn(createURL); when(responseEntity.getStatusCode()).thenReturn(HttpStatus.ACCEPTED); @@ -90,7 +98,7 @@ public class CnfmHttpServiceProviderImplTest { @Test - public void invokeCreateAsRequest_withStatuscodeSuccess_hasBody_emptyInstanceID() { + public void invokeCreateAsRequest_withStatuscode_Success_hasBody_emptyInstanceID() { AsInstance emptyInstanceID = getAsInstance(); emptyInstanceID.setAsInstanceid(null); @@ -103,7 +111,7 @@ public class CnfmHttpServiceProviderImplTest { } @Test - public void invokeCreateAsRequest_withStatuscodeNotSuccess_hasBody_emptyInstanceID() { + public void invokeCreateAsRequest_withStatuscode_NotSuccess_hasBody_emptyInstanceID() { AsInstance emptyInstanceID = getAsInstance(); emptyInstanceID.setAsInstanceid(null); @@ -115,7 +123,8 @@ public class CnfmHttpServiceProviderImplTest { @Test - public void InstantiateAsRequest_withStatuscodeSuccess() { + public void invokeInstantiateAsRequest_withStatuscode_Success() { + final String asinstanceId = getAsInstance().getAsInstanceid(); when(cnfmUrlProvider.getInstantiateAsRequestUrl(asinstanceId)).thenReturn(instantiateURL); when(responseEntity.getStatusCode()).thenReturn(HttpStatus.ACCEPTED); @@ -125,6 +134,115 @@ public class CnfmHttpServiceProviderImplTest { assertEquals(uri.toString(), returnedContent.orElseThrow().toString()); } + @Test + public void invokeInstantiateAsRequest_withStatuscode_Success_NoStatusURI() { + + final String asinstanceId = getAsInstance().getAsInstanceid(); + final HttpHeaders httpHeaders = getHttpHeaders(); + httpHeaders.setLocation(null); + when(cnfmUrlProvider.getInstantiateAsRequestUrl(asinstanceId)).thenReturn(instantiateURL); + when(responseEntity.getStatusCode()).thenReturn(HttpStatus.ACCEPTED); + when(responseEntity.getHeaders()).thenReturn(httpHeaders); + final Optional returnedContent = + cnfmHttpServiceProviderImpl.invokeInstantiateAsRequest(instantiateAsRequest, asinstanceId); + assertFalse(returnedContent.isPresent()); + } + + @Test + public void invokeInstantiateAsRequest_withStatuscode_NotSuccess_NoStatusURI() { + + final String asinstanceId = getAsInstance().getAsInstanceid(); + when(cnfmUrlProvider.getInstantiateAsRequestUrl(asinstanceId)).thenReturn(instantiateURL); + when(responseEntity.getStatusCode()).thenReturn(HttpStatus.BAD_REQUEST); + final Optional returnedContent = + cnfmHttpServiceProviderImpl.invokeInstantiateAsRequest(instantiateAsRequest, asinstanceId); + assertFalse(returnedContent.isPresent()); + } + + @Test + public void test_getOperationJobStatus_statuscode_Accepted() { + + responseEntityAsLCM = getResponseEntityAsLCM(HttpStatus.ACCEPTED); + when(httpRestServiceProvider.getHttpResponse(Mockito.anyString(), eq(AsLcmOpOcc.class))) + .thenReturn(responseEntityAsLCM); + final Optional returnedContent = cnfmHttpServiceProviderImpl.getOperationJobStatus("sample URL"); + assertEquals(returnedContent.orElseThrow().getAsInstanceId(), getAsLcmOpOcc().getAsInstanceId()); + } + + @Test + public void test_getOperationJobStatus_statuscode_NotFound() { + + responseEntityAsLCM = getResponseEntityAsLCM(HttpStatus.NOT_FOUND); + when(httpRestServiceProvider.getHttpResponse(Mockito.anyString(), eq(AsLcmOpOcc.class))) + .thenReturn(responseEntityAsLCM); + final Optional returnedContent = cnfmHttpServiceProviderImpl.getOperationJobStatus("sample URL"); + assertFalse(returnedContent.isPresent()); + } + + @Test + public void invokeDeleteAsRequest_withStatuscode_Success() { + + when(cnfmUrlProvider.getDeleteAsRequestUrl(Mockito.anyString())).thenReturn("deleteURL"); + when(httpRestServiceProvider.deleteHttpRequest(Mockito.anyString(), eq(Void.class))) + .thenReturn(responseEntityVoid); + when(responseEntityVoid.getStatusCode()).thenReturn(HttpStatus.ACCEPTED); + final Optional returnedContent = cnfmHttpServiceProviderImpl.invokeDeleteAsRequest("2345"); + assertTrue(returnedContent.orElseThrow()); + } + + @Test + public void invokeDeleteAsRequest_withStatuscode_BadRequest() { + + when(cnfmUrlProvider.getDeleteAsRequestUrl(Mockito.anyString())).thenReturn("deleteURL"); + when(httpRestServiceProvider.deleteHttpRequest(Mockito.anyString(), eq(Void.class))) + .thenReturn(responseEntityVoid); + when(responseEntityVoid.getStatusCode()).thenReturn(HttpStatus.BAD_REQUEST); + final Optional returnedContent = cnfmHttpServiceProviderImpl.invokeDeleteAsRequest("2345"); + assertTrue(returnedContent.isEmpty()); + } + + @Test + public void invokeTerminateAsRequest_withStatuscode_Success() { + + when(cnfmUrlProvider.getTerminateAsRequestUrl(Mockito.anyString())).thenReturn("terminateURL"); + when(httpRestServiceProvider.postHttpRequest(Mockito.any(TerminateAsRequest.class), Mockito.anyString(), + eq(Void.class))).thenReturn(responseEntityVoid); + when(responseEntityVoid.getStatusCode()).thenReturn(HttpStatus.ACCEPTED); + when(responseEntityVoid.getHeaders()).thenReturn(getHttpHeaders()); + final Optional returnedContent = + cnfmHttpServiceProviderImpl.invokeTerminateAsRequest("12356", terminateAsRequest); + assertTrue(returnedContent.isPresent()); + assertEquals(uri.getPath(), returnedContent.orElseThrow().getPath()); + } + + @Test + public void invokeTerminateAsRequest_withStatuscode_Success_NullStatusURI() { + + final HttpHeaders httpHeaders = getHttpHeaders(); + httpHeaders.setLocation(null); + when(cnfmUrlProvider.getTerminateAsRequestUrl(Mockito.anyString())).thenReturn("terminateURL"); + when(httpRestServiceProvider.postHttpRequest(Mockito.any(TerminateAsRequest.class), Mockito.anyString(), + eq(Void.class))).thenReturn(responseEntityVoid); + when(responseEntityVoid.getStatusCode()).thenReturn(HttpStatus.ACCEPTED); + when(responseEntityVoid.getHeaders()).thenReturn(httpHeaders); + final Optional returnedContent = + cnfmHttpServiceProviderImpl.invokeTerminateAsRequest("12356", terminateAsRequest); + assertTrue(returnedContent.isEmpty()); + } + + @Test + public void invokeTerminateAsRequest_withStatuscode_BadRequest() { + + when(cnfmUrlProvider.getTerminateAsRequestUrl(Mockito.anyString())).thenReturn("terminateURL"); + when(httpRestServiceProvider.postHttpRequest(Mockito.any(TerminateAsRequest.class), Mockito.anyString(), + eq(Void.class))).thenReturn(responseEntityVoid); + when(responseEntityVoid.getStatusCode()).thenReturn(HttpStatus.BAD_REQUEST); + // when(responseEntityVoid.getHeaders()).thenReturn(getHttpHeaders()); + final Optional returnedContent = + cnfmHttpServiceProviderImpl.invokeTerminateAsRequest("12356", terminateAsRequest); + assertTrue(returnedContent.isEmpty()); + } + private AsInstance getAsInstance() { AsInstance asInstance = new AsInstance(); asInstance.setAsInstanceid("12345"); @@ -136,4 +254,16 @@ public class CnfmHttpServiceProviderImplTest { httpHeaders.setLocation(uri); return httpHeaders; } + + private ResponseEntity getResponseEntityAsLCM(HttpStatus httpStatus) { + ResponseEntity asLcmOpOccResponseEntity = + new ResponseEntity(getAsLcmOpOcc(), httpStatus); + return asLcmOpOccResponseEntity; + } + + private AsLcmOpOcc getAsLcmOpOcc() { + AsLcmOpOcc asLcmOpOcc = new AsLcmOpOcc(); + asLcmOpOcc.setAsInstanceId("12345"); + return asLcmOpOcc; + } } diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/MonitorCnfmCreateJobTaskTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/MonitorCnfmCreateJobTaskTest.java deleted file mode 100644 index c32f6dd905..0000000000 --- a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/MonitorCnfmCreateJobTaskTest.java +++ /dev/null @@ -1,152 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2023 Nordix Foundation. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.so.bpmn.infrastructure.adapter.cnfm.tasks; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import java.net.URI; -import java.util.Optional; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; -import org.onap.so.bpmn.common.BuildingBlockExecution; -import org.onap.so.client.exception.ExceptionBuilder; -import org.onap.so.cnfm.lcm.model.AsLcmOpOcc; - -/** - * @author Raviteja Karumuri (raviteja.karumuri@est.tech) - */ -@RunWith(MockitoJUnitRunner.class) -public class MonitorCnfmCreateJobTaskTest { - - private final BuildingBlockExecution stubbedExecution = new StubbedBuildingBlockExecution(); - public static final String CREATE_CNF_STATUS_RESPONSE_PARAM_NAME = "createCnfStatusResponse"; - private final String CNFM_REQUEST_STATUS_CHECK_URL = "CnfmStatusCheckUrl"; - public static final String OPERATION_STATUS_PARAM_NAME = "operationStatus"; - private MonitorCnfmCreateJobTask monitorCnfmCreateJobTask; - @Mock - private CnfmHttpServiceProvider mockedCnfmHttpServiceProvider; - @Mock - private ExceptionBuilder exceptionUtil; - - @Before - public void setup() { - monitorCnfmCreateJobTask = new MonitorCnfmCreateJobTask(mockedCnfmHttpServiceProvider, exceptionUtil); - } - - @Test - public void getCurrentOperationStatus_completed() { - stubbedExecution.setVariable(CNFM_REQUEST_STATUS_CHECK_URL, URI.create("sampleURL")); - when(mockedCnfmHttpServiceProvider.getInstantiateOperationJobStatus(Mockito.anyString())) - .thenReturn(getAsLcmOpOcc()); - monitorCnfmCreateJobTask.getCurrentOperationStatus(stubbedExecution); - assertEquals(AsLcmOpOcc.OperationStateEnum.COMPLETED, - stubbedExecution.getVariable(OPERATION_STATUS_PARAM_NAME)); - } - - @Test - public void test_getCurrentOperationStatus_Exception() { - stubbedExecution.setVariable(CNFM_REQUEST_STATUS_CHECK_URL, URI.create("sampleURL")); - when(mockedCnfmHttpServiceProvider.getInstantiateOperationJobStatus(Mockito.anyString())) - .thenThrow(new RuntimeException()); - monitorCnfmCreateJobTask.getCurrentOperationStatus(stubbedExecution); - verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1209), anyString(), - any()); - } - - @Test - public void test_checkIfOperationWasSuccessful_status_completed() { - final MonitorCnfmCreateJobTask mockedMonitorCnfmCreateJobTask = Mockito.spy(monitorCnfmCreateJobTask); - mockedMonitorCnfmCreateJobTask.checkIfOperationWasSuccessful(stubbedExecution); - verify(mockedMonitorCnfmCreateJobTask, times(1)).checkIfOperationWasSuccessful(stubbedExecution); - } - - @Test - public void test_checkIfOperationWasSuccessful_status_Failed() { - Optional mockedAsLcmOpOcc = getAsLcmOpOcc(); - mockedAsLcmOpOcc.orElseThrow().setOperationState(AsLcmOpOcc.OperationStateEnum.FAILED); - stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, AsLcmOpOcc.OperationStateEnum.FAILED); - stubbedExecution.setVariable(CREATE_CNF_STATUS_RESPONSE_PARAM_NAME, mockedAsLcmOpOcc.orElseThrow()); - monitorCnfmCreateJobTask.checkIfOperationWasSuccessful(stubbedExecution); - verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1207), anyString(), - any()); - } - - @Test - public void test_checkIfOperationWasSuccessful_status_Null() { - stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, null); - monitorCnfmCreateJobTask.checkIfOperationWasSuccessful(stubbedExecution); - verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1206), anyString(), - any()); - } - - @Test - public void test_hasOperationFinished_status_completed() { - stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, AsLcmOpOcc.OperationStateEnum.COMPLETED); - boolean returnedValue = monitorCnfmCreateJobTask.hasOperationFinished(stubbedExecution); - assertTrue(returnedValue); - } - - @Test - public void test_hasOperationFinished_status_failed() { - stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, AsLcmOpOcc.OperationStateEnum.FAILED); - boolean returnedValue = monitorCnfmCreateJobTask.hasOperationFinished(stubbedExecution); - assertTrue(returnedValue); - } - - @Test - public void test_hasOperationFinished_status_rollback() { - stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, AsLcmOpOcc.OperationStateEnum.ROLLED_BACK); - boolean returnedValue = monitorCnfmCreateJobTask.hasOperationFinished(stubbedExecution); - assertTrue(returnedValue); - } - - @Test - public void test_hasOperationFinished_status_null() { - stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, null); - boolean returnedValue = monitorCnfmCreateJobTask.hasOperationFinished(stubbedExecution); - assertFalse(returnedValue); - } - - @Test - public void test_timeOutLogFailure() { - monitorCnfmCreateJobTask.timeOutLogFailue(stubbedExecution); - verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1205), anyString(), - any()); - } - - private Optional getAsLcmOpOcc() { - final AsLcmOpOcc asLcmOpOcc = new AsLcmOpOcc(); - asLcmOpOcc.setOperationState(AsLcmOpOcc.OperationStateEnum.COMPLETED); - return Optional.of(asLcmOpOcc); - } - -} diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/MonitorCnfmJobTaskTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/MonitorCnfmJobTaskTest.java new file mode 100644 index 0000000000..1ef3bc8753 --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/MonitorCnfmJobTaskTest.java @@ -0,0 +1,143 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.so.bpmn.infrastructure.adapter.cnfm.tasks; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import java.net.URI; +import java.util.Optional; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.so.bpmn.common.BuildingBlockExecution; +import org.onap.so.client.exception.ExceptionBuilder; +import org.onap.so.cnfm.lcm.model.AsLcmOpOcc; + +/** + * @author Raviteja Karumuri (raviteja.karumuri@est.tech) + */ +@RunWith(MockitoJUnitRunner.class) +public class MonitorCnfmJobTaskTest { + + private final BuildingBlockExecution stubbedExecution = new StubbedBuildingBlockExecution(); + public static final String CREATE_CNF_STATUS_RESPONSE_PARAM_NAME = "createCnfStatusResponse"; + private final String CNFM_REQUEST_STATUS_CHECK_URL = "CnfmStatusCheckUrl"; + public static final String OPERATION_STATUS_PARAM_NAME = "operationStatus"; + private MonitorCnfmJobTask monitorCnfmCreateJobTask; + @Mock + private CnfmHttpServiceProvider mockedCnfmHttpServiceProvider; + @Mock + private ExceptionBuilder exceptionUtil; + + @Before + public void setup() { + monitorCnfmCreateJobTask = new MonitorCnfmJobTask(mockedCnfmHttpServiceProvider, exceptionUtil); + } + + @Test + public void test_getCurrentOperationStatus_completed() { + stubbedExecution.setVariable(CNFM_REQUEST_STATUS_CHECK_URL, URI.create("sampleURL")); + when(mockedCnfmHttpServiceProvider.getOperationJobStatus(Mockito.anyString())).thenReturn(getAsLcmOpOcc()); + monitorCnfmCreateJobTask.getCurrentOperationStatus(stubbedExecution); + assertEquals(AsLcmOpOcc.OperationStateEnum.COMPLETED, + stubbedExecution.getVariable(OPERATION_STATUS_PARAM_NAME)); + } + + @Test + public void test_getCurrentOperationStatus_Exception() { + stubbedExecution.setVariable(CNFM_REQUEST_STATUS_CHECK_URL, URI.create("sampleURL")); + when(mockedCnfmHttpServiceProvider.getOperationJobStatus(Mockito.anyString())) + .thenThrow(new RuntimeException()); + monitorCnfmCreateJobTask.getCurrentOperationStatus(stubbedExecution); + verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1209), + any(Exception.class)); + } + + @Test + public void test_checkIfOperationWasSuccessful_status_completed() { + stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, AsLcmOpOcc.OperationStateEnum.COMPLETED); + final MonitorCnfmJobTask mockedMonitorCnfmCreateJobTask = Mockito.spy(monitorCnfmCreateJobTask); + mockedMonitorCnfmCreateJobTask.checkIfOperationWasSuccessful(stubbedExecution); + verify(mockedMonitorCnfmCreateJobTask, times(1)).checkIfOperationWasSuccessful(stubbedExecution); + } + + @Test + public void test_checkIfOperationWasSuccessful_status_Failed() { + Optional mockedAsLcmOpOcc = getAsLcmOpOcc(); + mockedAsLcmOpOcc.orElseThrow().setOperationState(AsLcmOpOcc.OperationStateEnum.FAILED); + stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, AsLcmOpOcc.OperationStateEnum.FAILED); + stubbedExecution.setVariable(CREATE_CNF_STATUS_RESPONSE_PARAM_NAME, mockedAsLcmOpOcc.orElseThrow()); + monitorCnfmCreateJobTask.checkIfOperationWasSuccessful(stubbedExecution); + verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1206), + any(Exception.class)); + } + + @Test + public void test_hasOperationFinished_status_completed() { + stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, AsLcmOpOcc.OperationStateEnum.COMPLETED); + boolean returnedValue = monitorCnfmCreateJobTask.hasOperationFinished(stubbedExecution); + assertTrue(returnedValue); + } + + @Test + public void test_hasOperationFinished_status_failed() { + stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, AsLcmOpOcc.OperationStateEnum.FAILED); + boolean returnedValue = monitorCnfmCreateJobTask.hasOperationFinished(stubbedExecution); + assertTrue(returnedValue); + } + + @Test + public void test_hasOperationFinished_status_rollback() { + stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, AsLcmOpOcc.OperationStateEnum.ROLLED_BACK); + boolean returnedValue = monitorCnfmCreateJobTask.hasOperationFinished(stubbedExecution); + assertTrue(returnedValue); + } + + @Test + public void test_hasOperationFinished_status_null() { + stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, null); + boolean returnedValue = monitorCnfmCreateJobTask.hasOperationFinished(stubbedExecution); + assertFalse(returnedValue); + } + + @Test + public void test_timeOutLogFailure() { + monitorCnfmCreateJobTask.timeOutLogFailure(stubbedExecution); + verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1205), + any(Exception.class)); + } + + private Optional getAsLcmOpOcc() { + final AsLcmOpOcc asLcmOpOcc = new AsLcmOpOcc(); + asLcmOpOcc.setOperationState(AsLcmOpOcc.OperationStateEnum.COMPLETED); + return Optional.of(asLcmOpOcc); + } + +} -- cgit 1.2.3-korg