From: raviteja.karumuri Date: Wed, 15 Feb 2023 00:31:29 +0000 (+0000) Subject: [SO] Pending Unit test cases for create changes in SO X-Git-Tag: 1.12.1~10 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=so.git;a=commitdiff_plain;h=d0493f954a48021662b5b6d3ba45b262750690c9 [SO] Pending Unit test cases for create changes in SO Issue-ID: SO-4070 Signed-off-by: raviteja.karumuri Change-Id: I75f0b41bc37f9c437fd1451a7efb85be803a001d --- diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/MonitorCnfmCreateJobTask.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/MonitorCnfmCreateJobTask.java index 06b44e4512..eb1cd1c1b6 100644 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/MonitorCnfmCreateJobTask.java +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/MonitorCnfmCreateJobTask.java @@ -22,6 +22,7 @@ import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.CREAT import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.OPERATION_STATUS_PARAM_NAME; import java.net.URI; import java.util.Optional; +import org.onap.logging.filter.base.ONAPComponents; import org.onap.so.bpmn.common.BuildingBlockExecution; import org.onap.so.client.exception.ExceptionBuilder; import org.slf4j.Logger; @@ -61,10 +62,10 @@ public class MonitorCnfmCreateJobTask { public void getCurrentOperationStatus(final BuildingBlockExecution execution) { try { LOGGER.debug("Executing getCurrentOperationStatus ..."); - final URI operation_status_url = execution.getVariable(CNFM_REQUEST_STATUS_CHECK_URL); - LOGGER.debug("Executing getCurrentOperationStatus for CNF... :{}", operation_status_url.toString()); + final URI operationStatusURL = execution.getVariable(CNFM_REQUEST_STATUS_CHECK_URL); + LOGGER.debug("Executing getCurrentOperationStatus for CNF... :{}", operationStatusURL); final Optional instantiateOperationJobStatus = - cnfmHttpServiceProvider.getInstantiateOperationJobStatus(operation_status_url.toString()); + cnfmHttpServiceProvider.getInstantiateOperationJobStatus(operationStatusURL.toString()); if (instantiateOperationJobStatus.isPresent()) { final AsLcmOpOcc asLcmOpOccResponse = instantiateOperationJobStatus.get(); if (asLcmOpOccResponse.getOperationState() != null) { @@ -77,12 +78,12 @@ public class MonitorCnfmCreateJobTask { LOGGER.debug("Operation {} without operationStatus and operation retrieval status :{}", asLcmOpOccResponse.getId(), asLcmOpOccResponse.getOperationState()); } - execution.setVariable(CREATE_CNF_STATUS_RESPONSE_PARAM_NAME, instantiateOperationJobStatus.get()); + execution.setVariable(CREATE_CNF_STATUS_RESPONSE_PARAM_NAME, instantiateOperationJobStatus.orElseThrow()); LOGGER.debug("Finished executing getCurrentOperationStatus for CNF..."); } catch (final Exception exception) { final String message = "Unable to invoke get current Operation status"; LOGGER.error(message); - exceptionUtil.buildAndThrowWorkflowException(execution, 1209, message); + exceptionUtil.buildAndThrowWorkflowException(execution, 1209, message, ONAPComponents.SO); } } @@ -95,7 +96,7 @@ public class MonitorCnfmCreateJobTask { public void timeOutLogFailue(final BuildingBlockExecution execution) { final String message = "CNF Instantiation operation time out"; LOGGER.error(message); - exceptionUtil.buildAndThrowWorkflowException(execution, 1205, message); + exceptionUtil.buildAndThrowWorkflowException(execution, 1205, message, ONAPComponents.SO); } /** @@ -112,15 +113,14 @@ public class MonitorCnfmCreateJobTask { + (cnfInstantiateStautusResponse != null ? cnfInstantiateStautusResponse.getId() : "null") + "Unable to retrieve OperationStatus"; LOGGER.error(message); - exceptionUtil.buildAndThrowWorkflowException(execution, 1206, message); + exceptionUtil.buildAndThrowWorkflowException(execution, 1206, message, ONAPComponents.SO); } else { - final OperationStateEnum operationStatus = operationStatusOption; - if (operationStatus != OperationStateEnum.COMPLETED) { + if (operationStatusOption != OperationStateEnum.COMPLETED) { final String message = "Unable to instantiate jobId: " + (cnfInstantiateStautusResponse != null ? cnfInstantiateStautusResponse.getId() : "null") - + " OperationStatus: " + operationStatus; + + " OperationStatus: " + operationStatusOption; LOGGER.error(message); - exceptionUtil.buildAndThrowWorkflowException(execution, 1207, message); + exceptionUtil.buildAndThrowWorkflowException(execution, 1207, message, ONAPComponents.SO); } LOGGER.debug("Successfully completed CNF instatiation of job status {}", cnfInstantiateStautusResponse); } diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/CnfmHttpServiceConfigurationTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/CnfmHttpServiceConfigurationTest.java new file mode 100644 index 0000000000..48325972ec --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/CnfmHttpServiceConfigurationTest.java @@ -0,0 +1,56 @@ +/*- + * ============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.assertNotNull; +import org.apache.http.impl.client.CloseableHttpClient; +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.configuration.rest.HttpComponentsClientConfiguration; +import org.onap.so.rest.service.HttpRestServiceProvider; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; + +/** + * @author Raviteja Karumuri (raviteja.karumuri@est.tech) + */ +@RunWith(MockitoJUnitRunner.class) +public class CnfmHttpServiceConfigurationTest { + + @Mock + private HttpComponentsClientConfiguration httpComponentsClientConfiguration; + @Mock + private CloseableHttpClient httpclient; + + @Test + public void cnfmHttpRestServiceProvider_NotNull() { + + final CnfmHttpServiceConfiguration objForTest = new CnfmHttpServiceConfiguration(); + HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpclient); + Mockito.when(httpComponentsClientConfiguration.httpComponentsClientHttpRequestFactory()) + .thenReturn(requestFactory); + final HttpRestServiceProvider returnedValue = objForTest + .cnfmHttpRestServiceProvider(objForTest.cnfmRestTemplateBean(httpComponentsClientConfiguration)); + assertNotNull(returnedValue); + } +} 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 new file mode 100644 index 0000000000..aac685fe37 --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/CnfmHttpServiceProviderImplTest.java @@ -0,0 +1,139 @@ +/*- + * ============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.mockito.ArgumentMatchers.eq; +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.cnfm.lcm.model.AsInstance; +import org.onap.so.cnfm.lcm.model.CreateAsRequest; +import org.onap.so.cnfm.lcm.model.InstantiateAsRequest; +import org.onap.so.rest.service.HttpRestServiceProvider; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +/** + * @author Raviteja Karumuri (raviteja.karumuri@est.tech) + */ +@RunWith(MockitoJUnitRunner.class) +public class CnfmHttpServiceProviderImplTest { + + @Mock + private CnfmUrlProvider cnfmUrlProvider; + @Mock + private HttpRestServiceProvider httpRestServiceProvider; + @Mock + private ResponseEntity responseEntity; + + 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"; + private CnfmHttpServiceProviderImpl cnfmHttpServiceProviderImpl; + private final URI uri = URI.create("sample"); + private final CreateAsRequest createAsRequest = new CreateAsRequest(); + private final InstantiateAsRequest instantiateAsRequest = new InstantiateAsRequest(); + + @Before + public void setup() { + cnfmHttpServiceProviderImpl = new CnfmHttpServiceProviderImpl(cnfmUrlProvider, httpRestServiceProvider); + when(httpRestServiceProvider.postHttpRequest(Mockito.any(), Mockito.anyString(), eq(AsInstance.class))) + .thenReturn(responseEntity); + } + + @Test + public void invokeCreateAsRequest_withStatuscodeSuccess_hasBody() { + + when(cnfmUrlProvider.getCreateAsRequestUrl()).thenReturn(createURL); + when(responseEntity.getStatusCode()).thenReturn(HttpStatus.ACCEPTED); + when(responseEntity.getBody()).thenReturn(getAsInstance()); + when(responseEntity.hasBody()).thenReturn(true); + final Optional returnedContent = cnfmHttpServiceProviderImpl.invokeCreateAsRequest(createAsRequest); + assertEquals(returnedContent.orElseThrow().getAsInstanceid(), getAsInstance().getAsInstanceid()); + } + + @Test + public void invokeCreateAsRequest_withStatuscodeSuccess_hasNoBody() { + + when(cnfmUrlProvider.getCreateAsRequestUrl()).thenReturn(createURL); + when(responseEntity.getStatusCode()).thenReturn(HttpStatus.ACCEPTED); + final Optional returnedContent = cnfmHttpServiceProviderImpl.invokeCreateAsRequest(createAsRequest); + assertFalse(returnedContent.isPresent()); + } + + + @Test + public void invokeCreateAsRequest_withStatuscodeSuccess_hasBody_emptyInstanceID() { + + AsInstance emptyInstanceID = getAsInstance(); + emptyInstanceID.setAsInstanceid(null); + when(cnfmUrlProvider.getCreateAsRequestUrl()).thenReturn(createURL); + when(responseEntity.getStatusCode()).thenReturn(HttpStatus.ACCEPTED); + when(responseEntity.getBody()).thenReturn(emptyInstanceID); + when(responseEntity.hasBody()).thenReturn(true); + final Optional returnedContent = cnfmHttpServiceProviderImpl.invokeCreateAsRequest(createAsRequest); + assertFalse(returnedContent.isPresent()); + } + + @Test + public void invokeCreateAsRequest_withStatuscodeNotSuccess_hasBody_emptyInstanceID() { + + AsInstance emptyInstanceID = getAsInstance(); + emptyInstanceID.setAsInstanceid(null); + when(cnfmUrlProvider.getCreateAsRequestUrl()).thenReturn(createURL); + when(responseEntity.getStatusCode()).thenReturn(HttpStatus.BAD_REQUEST); + final Optional returnedContent = cnfmHttpServiceProviderImpl.invokeCreateAsRequest(createAsRequest); + assertFalse(returnedContent.isPresent()); + } + + + @Test + public void InstantiateAsRequest_withStatuscodeSuccess() { + final String asinstanceId = getAsInstance().getAsInstanceid(); + when(cnfmUrlProvider.getInstantiateAsRequestUrl(asinstanceId)).thenReturn(instantiateURL); + when(responseEntity.getStatusCode()).thenReturn(HttpStatus.ACCEPTED); + when(responseEntity.getHeaders()).thenReturn(getHttpHeaders()); + final Optional returnedContent = + cnfmHttpServiceProviderImpl.invokeInstantiateAsRequest(instantiateAsRequest, asinstanceId); + assertEquals(uri.toString(), returnedContent.orElseThrow().toString()); + } + + private AsInstance getAsInstance() { + AsInstance asInstance = new AsInstance(); + asInstance.setAsInstanceid("12345"); + return asInstance; + } + + private HttpHeaders getHttpHeaders() { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setLocation(uri); + return httpHeaders; + } +} 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 new file mode 100644 index 0000000000..c32f6dd905 --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/MonitorCnfmCreateJobTaskTest.java @@ -0,0 +1,152 @@ +/*- + * ============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); + } + +}