diff options
Diffstat (limited to 'vid-app-common/src/test/java/org/onap/vid/mso')
4 files changed, 364 insertions, 224 deletions
diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/MsoBusinessLogicImplTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/MsoBusinessLogicImplTest.java index 36f4bdd43..461673224 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/MsoBusinessLogicImplTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/MsoBusinessLogicImplTest.java @@ -1,153 +1,306 @@ +/* + * ============LICENSE_START========================================== + * =================================================================== + * Modifications Copyright (C) 2018 Nokia. All rights reserved. + * =================================================================== + * 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. + * ============LICENSE_END============================================ + * + * + */ package org.onap.vid.mso; -import org.mockito.InjectMocks; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.junit.Assert.assertEquals; +import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.argThat; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.onap.vid.mso.MsoBusinessLogicImpl.validateEndpointPath; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.stream.Collectors; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentMatcher; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import org.onap.portalsdk.core.util.SystemProperties; +import org.onap.vid.exceptions.GenericUncheckedException; +import org.onap.vid.mso.rest.Request; import org.onap.vid.mso.rest.RequestDetails; +import org.onap.vid.mso.rest.RequestDetailsWrapper; import org.onap.vid.properties.Features; -import org.onap.portalsdk.core.util.SystemProperties; +import org.springframework.http.HttpStatus; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; -import org.springframework.test.context.web.WebAppConfiguration; -import org.testng.Assert; -import org.testng.annotations.BeforeTest; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.togglz.core.manager.FeatureManager; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.onap.vid.controllers.MsoController.SVC_INSTANCE_ID; -import static org.onap.vid.controllers.MsoController.VNF_INSTANCE_ID; -import static org.onap.vid.mso.MsoBusinessLogicImpl.validateEndpointPath; - @ContextConfiguration(classes = {SystemProperties.class}) -@WebAppConfiguration -public class MsoBusinessLogicImplTest extends AbstractTestNGSpringContextTests { +@RunWith(SpringJUnit4ClassRunner.class) +public class MsoBusinessLogicImplTest { - @InjectMocks - private MsoBusinessLogicImpl msoBusinessLogic; + private static final ObjectMapper objectMapper = new ObjectMapper(); @Mock - private FeatureManager featureManagerMock; + private FeatureManager featureManager; @Mock - private MsoInterface msoInterfaceMock; + private MsoInterface msoInterface; + private MsoBusinessLogicImpl msoBusinessLogic; - @BeforeTest - public void initMocks(){ + @Before + public void setUp() { MockitoAnnotations.initMocks(this); + msoBusinessLogic = new MsoBusinessLogicImpl(msoInterface, featureManager); } @Test - public void validateEndpointPath_endPointIsNotEmptyAndVaild_returnProperty(){ - System.setProperty("TestEnv","123"); - String foundEndPoint = validateEndpointPath("TestEnv"); - Assert.assertEquals("123",foundEndPoint); - } + public void createConfigurationInstance_shouldCallMsoInterface_withCorrectServiceInstanceId() throws Exception { + // given + String serviceInstanceId = "3f93c7cb-2fd0-4557-9514-e189b7b04f9d"; + String endpointTemplate = String.format("/serviceInstances/v6/%s/configurations", serviceInstanceId); + RequestDetailsWrapper requestDetailsWrapper = createRequestDetails("mso_request_create_configuration.json"); + MsoResponseWrapper expectedResponse = createOkResponse(); + given(msoInterface.createConfigurationInstance(requestDetailsWrapper, endpointTemplate)) + .willReturn(expectedResponse); - @Test(expectedExceptions = RuntimeException.class) - public void validateEndpointPath_endPointIsNull_throwRuntimeException(){ - validateEndpointPath("NotExists"); - } + // when + MsoResponseWrapper msoResponseWrapper = msoBusinessLogic + .createConfigurationInstance(requestDetailsWrapper, serviceInstanceId); - @Test(expectedExceptions = RuntimeException.class) - public void validateEndpointPath_endPointIsNotEmptyButDoesntExists_throwRuntimeException(){ - System.setProperty("EmptyEndPoint",""); - validateEndpointPath("EmptyEndPoint"); + // then + assertThat(msoResponseWrapper).isEqualToComparingFieldByField(expectedResponse); } + private RequestDetailsWrapper createRequestDetails(String bodyFileName) throws Exception { + final URL resource = this.getClass().getResource("/payload_jsons/" + bodyFileName); + RequestDetails requestDetails = objectMapper.readValue(resource, RequestDetails.class); + return new RequestDetailsWrapper(requestDetails); + } - //@Test(dataProvider = "unAssignOrDeleteParams") - public void deleteSvcInstance_verifyEndPointPathConstructing_unAssignFeatureOffOrUnAssignFlagIsFalse(boolean isAssignFlag,String status) { - Mockito.reset(msoInterfaceMock); - String endpoint = validateEndpointPath(isAssignFlag ? MsoProperties.MSO_DELETE_OR_UNASSIGN_REST_API_SVC_INSTANCE : MsoProperties.MSO_REST_API_SVC_INSTANCE); - RequestDetails requestDetails = new RequestDetails(); - - when(featureManagerMock.isActive(Features.FLAG_UNASSIGN_SERVICE)).thenReturn(isAssignFlag); - - msoBusinessLogic.deleteSvcInstance(requestDetails, "tempId", status); + @Test + public void validateEndpointPath_endPointIsNotEmptyAndVaild_returnProperty() { + System.setProperty("TestEnv", "123"); + String foundEndPoint = validateEndpointPath("TestEnv"); + assertEquals("123", foundEndPoint); + } - verify(msoInterfaceMock).deleteSvcInstance(requestDetails, endpoint + "/tempId"); + @Test + public void validateEndpointPath_endPointIsNull_throwRuntimeException() { + assertThatExceptionOfType(RuntimeException.class) + .isThrownBy(() -> validateEndpointPath("NotExists")); } - @DataProvider - public Object[][] unAssignOrDeleteParams() { - return new Object[][]{ - {Boolean.FALSE, "active"}, - {Boolean.FALSE, "created"}, - {Boolean.TRUE, "Active"}, - {Boolean.TRUE, "unexpected-status"}, - }; + @Test + public void validateEndpointPath_endPointIsNotEmptyButDoesntExists_throwRuntimeException() { + String endPoint = "EmptyEndPoint"; + System.setProperty(endPoint, ""); + assertThatExceptionOfType(GenericUncheckedException.class) + .isThrownBy(() -> validateEndpointPath(endPoint)) + .withMessage(endPoint + " env variable is not defined"); } - //@Test(dataProvider = "unAssignStatus") - public void deleteSvcInstance_verifyEndPointPathConstructing_unAssignFeatureOnAndUnAssignFlagIsTrue(String status) { - Mockito.reset(msoInterfaceMock); - // in the test Features.FLAG_UNASSIGN_SERVICE is active so the endpoint should be MsoProperties.MSO_DELETE_OR_UNASSIGN_REST_API_SVC_INSTANCE - String endpoint = validateEndpointPath(MsoProperties.MSO_DELETE_OR_UNASSIGN_REST_API_SVC_INSTANCE); + @Test + public void deleteSvcInstance_verifyEndPointPathConstructing_unAssignFeatureOffOrUnAssignFlagIsFalse() { + // given + String endpointTemplate = "/serviceInstances/v5/%s"; + String serviceInstanceId = "3f93c7cb-2fd0-4557-9514-e189b7b04f9d"; + String svcEndpoint = String.format(endpointTemplate, serviceInstanceId); RequestDetails requestDetails = new RequestDetails(); + MsoResponseWrapper expectedResponse = createOkResponse(); + given(msoInterface.deleteSvcInstance(requestDetails, svcEndpoint)).willReturn(expectedResponse); + given(featureManager.isActive(Features.FLAG_UNASSIGN_SERVICE)).willReturn(false); - when(featureManagerMock.isActive(Features.FLAG_UNASSIGN_SERVICE)).thenReturn(true); + // when + MsoResponseWrapper msoResponseWrapper = msoBusinessLogic + .deleteSvcInstance(requestDetails, serviceInstanceId, "unAssignOrDeleteParams"); - msoBusinessLogic.deleteSvcInstance(requestDetails, "tempId", status); - - verify(msoInterfaceMock).unassignSvcInstance(requestDetails, endpoint + "/tempId/unassign"); + // then + assertThat(msoResponseWrapper).isEqualToComparingFieldByField(expectedResponse); } - @DataProvider - public Object[][] unAssignStatus() { - return new Object[][]{ - {"Created"}, - {"Pendingdelete"}, - {"pending-Delete"}, - {"Assigned"} - }; + @Test + public void deleteSvcInstance_verifyEndPointPathConstructing_unAssignFeatureOnAndUnAssignFlagIsTrue() { + // given + String endpointTemplate = "/serviceInstantiation/v5/serviceInstances/%s/unassign"; + String serviceInstanceId = "3f93c7cb-2fd0-4557-9514-e189b7b04f9d"; + String svcEndpoint = String.format(endpointTemplate, serviceInstanceId); + RequestDetails requestDetails = new RequestDetails(); + MsoResponseWrapper expectedResponse = createOkResponse(); + given(msoInterface.unassignSvcInstance(requestDetails, svcEndpoint)).willReturn(expectedResponse); + given(featureManager.isActive(Features.FLAG_UNASSIGN_SERVICE)).willReturn(true); + + // when + MsoResponseWrapper msoResponseWrapper = msoBusinessLogic + .deleteSvcInstance(requestDetails, serviceInstanceId, "assigned"); + + // then + assertThat(msoResponseWrapper).isEqualToComparingFieldByField(expectedResponse); } @Test public void deleteVnf_verifyEndPointPathConstructing() { - String endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_VNF_INSTANCE); + // when + String endpointTemplate = "/serviceInstances/v5/%s/vnfs/%s"; + String serviceInstanceId = "3f93c7cb-2fd0-4557-9514-e189b7b04f9d"; + String vnfInstanceId = "testVnfInstanceTempId"; + String vnfEndpoint = String.format(endpointTemplate, serviceInstanceId, vnfInstanceId); RequestDetails requestDetails = new RequestDetails(); + MsoResponseWrapper expectedResponse = createOkResponse(); + given(msoInterface.deleteVnf(requestDetails, vnfEndpoint)).willReturn(expectedResponse); - String vnf_endpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, "serviceInstanceTempId"); + // when + MsoResponseWrapper msoResponseWrapper = msoBusinessLogic + .deleteVnf(requestDetails, serviceInstanceId, vnfInstanceId); - msoBusinessLogic.deleteVnf(requestDetails, "serviceInstanceTempId","vnfInstanceTempId"); - verify(msoInterfaceMock).deleteVnf(requestDetails, vnf_endpoint + "/vnfInstanceTempId"); + // then + assertThat(msoResponseWrapper).isEqualToComparingFieldByField(expectedResponse); } @Test public void deleteVfModule_verifyEndPointPathConstructing() { - String endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE); + // when + String endpointTemplate = "/serviceInstances/v7/%s/vnfs/%s/vfModules/%s"; + String serviceInstanceId = "3f93c7cb-2fd0-4557-9514-e189b7b04f9d"; + String vnfInstanceId = "testVnfInstanceTempId"; + String vfModuleId = "testVfModuleId"; + String vnfEndpoint = String.format(endpointTemplate, serviceInstanceId, vnfInstanceId, vfModuleId); RequestDetails requestDetails = new RequestDetails(); + MsoResponseWrapper expectedResponse = createOkResponse(); + given(msoInterface.deleteVfModule(requestDetails, vnfEndpoint)).willReturn(expectedResponse); + + // when + MsoResponseWrapper msoResponseWrapper = msoBusinessLogic + .deleteVfModule(requestDetails, serviceInstanceId, vnfInstanceId, vfModuleId); + // then + assertThat(msoResponseWrapper).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldSendProperScaleOutRequest() throws IOException { + // given + String serviceInstanceId = "3f93c7cb-2fd0-4557-9514-e189b7b04f9d"; + String vnfInstanceId = "testVnfInstanceTempId"; + String endpointTemplate = "/serviceInstantiation/v7/serviceInstances/%s/vnfs/%s/vfModules/scaleOut"; + String vnfEndpoint = String.format(endpointTemplate, serviceInstanceId, vnfInstanceId); + org.onap.vid.changeManagement.RequestDetails requestDetails = readRequest( + "scaleOutVfModulePayload.json"); + org.onap.vid.changeManagement.RequestDetailsWrapper expectedRequest = readExpectedRequest( + "scaleOutVfModulePayloadToMso.json"); + MsoResponseWrapper expectedMsoResponseWrapper = createOkResponse(); + given( + msoInterface + .scaleOutVFModuleInstance(argThat(new MsoRequestWrapperMatcher(expectedRequest)), + eq(vnfEndpoint))) + .willReturn(expectedMsoResponseWrapper); + + // when + MsoResponseWrapper msoResponseWrapper = msoBusinessLogic + .scaleOutVfModuleInstance(requestDetails, serviceInstanceId, vnfInstanceId); + + // then + assertThat(msoResponseWrapper).isEqualToComparingFieldByField(expectedMsoResponseWrapper); + } - String vf__modules_endpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, "serviceInstanceTempId").replaceFirst(VNF_INSTANCE_ID, "vnfInstanceTempId"); + private org.onap.vid.changeManagement.RequestDetails readRequest(String requestJsonFilename) throws IOException { + Path path = Paths.get("payload_jsons", requestJsonFilename); + URL url = this.getClass().getClassLoader().getResource(path.toString()); + return objectMapper.readValue(url, org.onap.vid.changeManagement.RequestDetails.class); + } - msoBusinessLogic.deleteVfModule(requestDetails, "serviceInstanceTempId","vnfInstanceTempId", "vfModuleTempId"); - verify(msoInterfaceMock).deleteVfModule(requestDetails, vf__modules_endpoint + "/vfModuleTempId" ); + private org.onap.vid.changeManagement.RequestDetailsWrapper readExpectedRequest(String requestJsonFilename) + throws IOException { + Path path = Paths.get("payload_jsons", requestJsonFilename); + URL url = this.getClass().getClassLoader().getResource(path.toString()); + return objectMapper.readValue(url, + new TypeReference<org.onap.vid.changeManagement.RequestDetailsWrapper<org.onap.vid.changeManagement.RequestDetails>>() { + }); + } + + private MsoResponseWrapper createOkResponse() { + HttpStatus expectedStatus = HttpStatus.ACCEPTED; + String expectedBody = " \"body\": {\n" + + " \"requestReferences\": {\n" + + " \"instanceId\": \" 123456 \",\n" + + " \"requestId\": \"b6dc9806-b094-42f7-9386-a48de8218ce8\"\n" + + " }"; + MsoResponseWrapper responseWrapper = new MsoResponseWrapper(); + responseWrapper.setEntity(expectedBody); + responseWrapper.setStatus(expectedStatus.value()); + return responseWrapper; } @Test - public void insertServiceInstantiationToDB_StartJob() { - -// broker = new JobsBrokerServiceInDatabaseImpl(dataAccessServiceMock, sessionFactory); -// ((JobsBrokerServiceInDatabaseImpl)broker).deleteAll(); -// -//// msoBusinessLogic.setDataAccessService(dataAccessServiceMock); -//// msoBusinessLogic.setJobsBrokerService(broker); -//// msoBusinessLogic.setJobAdapter(jobAdapter); -// -// ServiceInstantiation serviceInstantiation = new ServiceInstantiation(); -// serviceInstantiation.setCount(2); -// serviceInstantiation.setInstanceName("TestName"); -// -// msoBusinessLogic.pushBulkJob(serviceInstantiation, "testUserId"); -// -// List<ServiceInfo> serviceInfoList = dataAccessServiceMock.getList(ServiceInfo.class, null); -// int k = 9; -// Assert.assertEquals(serviceInstantiation, containsInAnyOrder(serviceInfoList.toArray())); + public void shouldFilterOutOrchestrationRequestsNotAllowedInDashboard() throws IOException { + //given + String vnfModelTypeOrchestrationRequests = getFileContentAsString("mso_model_info_sample_response.json"); + String scaleOutActionOrchestrationRequests = getFileContentAsString("mso_action_scaleout_sample_response.json"); + + MsoResponseWrapper msoResponseWrapperMock = mock(MsoResponseWrapper.class); + given(msoInterface + .getOrchestrationRequestsForDashboard(any(String.class), any(String.class), any(String.class), + any(RestObject.class))) + .willReturn(msoResponseWrapperMock); + given(msoResponseWrapperMock.getEntity()) + .willReturn(vnfModelTypeOrchestrationRequests, scaleOutActionOrchestrationRequests); + + //when + List<Request> filteredOrchestrationReqs = msoBusinessLogic.getOrchestrationRequestsForDashboard(); + + //then + assertThat(filteredOrchestrationReqs).hasSize(3); + assertThat(MsoBusinessLogicImpl.DASHBOARD_ALLOWED_TYPES) + .containsAll(filteredOrchestrationReqs + .stream() + .map(el -> el.getRequestType().toUpperCase()) + .collect(Collectors.toList())); + assertThat(filteredOrchestrationReqs) + .extracting(org.onap.vid.domain.mso.Request::getRequestScope) + .containsOnly("vnf", "vfModule"); + } + + private String getFileContentAsString(String resourceName) throws IOException { + URL url = this.getClass().getClassLoader().getResource("."); + Path path = Paths.get(url.getPath(), "payload_jsons", resourceName); + return new String(Files.readAllBytes(path)); + } + + private static class MsoRequestWrapperMatcher extends + ArgumentMatcher<org.onap.vid.changeManagement.RequestDetailsWrapper> { + + private final org.onap.vid.changeManagement.RequestDetailsWrapper expectedRequest; + + public MsoRequestWrapperMatcher(org.onap.vid.changeManagement.RequestDetailsWrapper expectedRequest) { + this.expectedRequest = expectedRequest; + } + + @Override + public boolean matches(Object argument) { + org.onap.vid.changeManagement.RequestDetailsWrapper requestDetailsWrapper = (org.onap.vid.changeManagement.RequestDetailsWrapper) argument; + return expectedRequest.requestDetails.equals(requestDetailsWrapper.requestDetails); + } } } diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/MsoBusinessLogicTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/MsoBusinessLogicTest.java deleted file mode 100644 index af7f74b3d..000000000 --- a/vid-app-common/src/test/java/org/onap/vid/mso/MsoBusinessLogicTest.java +++ /dev/null @@ -1,61 +0,0 @@ -package org.onap.vid.mso; - -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.runners.MockitoJUnitRunner; -import org.onap.vid.mso.MsoBusinessLogicImpl; -import org.onap.vid.mso.MsoInterface; -import org.onap.vid.mso.MsoResponseWrapper; -import org.onap.vid.mso.rest.RequestDetails; -import org.onap.vid.mso.rest.RequestDetailsWrapper; -import org.testng.annotations.Test; - -import java.net.URL; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; - -@RunWith(MockitoJUnitRunner.class) -public class MsoBusinessLogicTest { - - @InjectMocks - private MsoBusinessLogicImpl msoBusinessLogic; - - @Mock - private MsoInterface msoClient; - - @Test - public void testCreateInstance() throws Exception { - String instanceId = "3f93c7cb-2fd0-4557-9514-e189b7b04f9d"; - final RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper(); - requestDetailsWrapper.requestDetails = setRequestDetails("mso_request_create_configuration.json"); - Mockito.doReturn(getOkResponse(instanceId)).when(msoClient).createConfigurationInstance(requestDetailsWrapper, "/serviceInstances/v6/3f93c7cb-2fd0-4557-9514-e189b7b04f9d/configurations"); - final MsoResponseWrapper msoResponseWrapper = msoBusinessLogic.createConfigurationInstance(requestDetailsWrapper, instanceId); - - assertNotNull(msoResponseWrapper); - assertEquals(202, msoResponseWrapper.getStatus()); - } - - private MsoResponseWrapper getOkResponse(String instanceId){ - MsoResponseWrapper responseWrapper = new MsoResponseWrapper(); - String entity = " \"body\": {\n" + - " \"requestReferences\": {\n" + - " \"instanceId\": \""+instanceId+"\",\n" + - " \"requestId\": \"b6dc9806-b094-42f7-9386-a48de8218ce8\"\n" + - " }"; - responseWrapper.setEntity(entity); - responseWrapper.setStatus(202); - return responseWrapper; - } - - private RequestDetails setRequestDetails(String bodyFileName)throws Exception { - final URL resource = this.getClass().getResource("/payload_jsons/" + bodyFileName); - ObjectMapper mapper = new ObjectMapper(); - RequestDetails requestDetails = mapper.readValue(resource, RequestDetails.class); - return requestDetails; - - } -} diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientNewTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientNewTest.java index 402386a50..bfc82ce2a 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientNewTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientNewTest.java @@ -21,17 +21,12 @@ package org.onap.vid.mso.rest; import com.xebialabs.restito.server.StubServer; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Properties; -import java.util.UUID; import org.glassfish.grizzly.http.util.HttpStatus; import org.junit.AfterClass; import org.junit.BeforeClass; +import org.junit.Ignore; import org.junit.Test; +import org.onap.portalsdk.core.util.SystemProperties; import org.onap.vid.client.SyncRestClient; import org.onap.vid.controllers.MsoController; import org.onap.vid.mso.MsoInterface; @@ -39,16 +34,30 @@ import org.onap.vid.mso.MsoProperties; import org.onap.vid.mso.MsoResponseWrapper; import org.onap.vid.mso.MsoResponseWrapperInterface; import org.onap.vid.mso.RestObject; +import org.springframework.test.context.ContextConfiguration; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Properties; +import java.util.UUID; + +import static org.onap.vid.controllers.MsoController.SVC_INSTANCE_ID; +import static org.onap.vid.controllers.MsoController.VNF_INSTANCE_ID; + +@ContextConfiguration(classes = {SystemProperties.class}) public class MsoRestClientNewTest { private static StubServer server; private static StubServer securedServer; private static Properties props = new Properties(); private static String msoCreateServiceInstanceJson; + private static String msoScaleOutVfModule; private final static String CREATE_INSTANCE_RESPONSE_STR = - "{\"requestReferences\":{\"instanceId\":\"baa13544-0e95-4644-9565-9a198a29a294\"," - + "\"requestId\":\"a42a1a35-3d63-4629-bbe0-4989fa7414cb\"}}"; + "{\"requestReferences\":{\"instanceId\":\"baa13544-0e95-4644-9565-9a198a29a294\"," + + "\"requestId\":\"a42a1a35-3d63-4629-bbe0-4989fa7414cb\"}}"; private final static String SERVICE_INSTANCE_ID = "12345"; private static final String SAMPLE_VNF_INSTANCE_ID = "111"; private static final String SAMPLE_VNF_MODULE_ID = "987"; @@ -63,15 +72,19 @@ public class MsoRestClientNewTest { securedServer = new StubServer().secured().run(); Path resourceDirectory = - Paths.get("src", "test", "resources", "WEB-INF", "conf", "system.properties"); - try(InputStream is = Files.newInputStream(resourceDirectory)) { + Paths.get("src", "test", "resources", "WEB-INF", "conf", "system.properties"); + try (InputStream is = Files.newInputStream(resourceDirectory)) { props.load(is); } Path msoServiceInstantiationJsonFilePath = - Paths.get("src", "test", "resources", "payload_jsons", "mso_service_instantiation.json"); + Paths.get("src", "test", "resources", "payload_jsons", "mso_service_instantiation.json"); + + Path scaleOutJsonFilePath = Paths.get("src", "test", "resources", "payload_jsons", "scaleOutVfModulePayloadToMso.json"); msoCreateServiceInstanceJson = - String.join("\n", Files.readAllLines(msoServiceInstantiationJsonFilePath)); + String.join("\n", Files.readAllLines(msoServiceInstantiationJsonFilePath)); + msoScaleOutVfModule = String.join("\n", Files.readAllLines(scaleOutJsonFilePath)); + } @AfterClass @@ -89,11 +102,11 @@ public class MsoRestClientNewTest { public void testCreateSvcInstance() throws Exception { String endpoint = props.getProperty(MsoProperties.MSO_REST_API_CONFIGURATIONS); endpoint = endpoint.replace(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID); - try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil( - server, - endpoint, - HttpStatus.ACCEPTED_202, - CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) { + try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil( + server, + endpoint, + HttpStatus.ACCEPTED_202, + CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) { closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createSvcInstance); } } @@ -102,12 +115,12 @@ public class MsoRestClientNewTest { public void testCreateVnf() throws Exception { String endpoint = props.getProperty(MsoProperties.MSO_REST_API_VNF_INSTANCE); endpoint = endpoint.replace(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID); - try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil( - server, - endpoint, - HttpStatus.ACCEPTED_202, - CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) { - + try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil( + server, + endpoint, + HttpStatus.ACCEPTED_202, + CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) { + closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createVnf); } } @@ -116,11 +129,11 @@ public class MsoRestClientNewTest { public void testCreateNwInstance() throws Exception { String endpoint = props.getProperty(MsoProperties.MSO_REST_API_NETWORK_INSTANCE); String nw_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID); - try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil( - server, - nw_endpoint, - HttpStatus.ACCEPTED_202, - CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) { + try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil( + server, + nw_endpoint, + HttpStatus.ACCEPTED_202, + CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) { closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createNwInstance); } } @@ -130,11 +143,11 @@ public class MsoRestClientNewTest { String endpoint = props.getProperty(MsoProperties.MSO_REST_API_VOLUME_GROUP_INSTANCE); String vnf_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID); vnf_endpoint = vnf_endpoint.replaceFirst(MsoController.VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID); - try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil( - server, - vnf_endpoint, - HttpStatus.ACCEPTED_202, - CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) { + try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil( + server, + vnf_endpoint, + HttpStatus.ACCEPTED_202, + CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) { closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createVolumeGroupInstance); } } @@ -144,14 +157,14 @@ public class MsoRestClientNewTest { String endpoint = props.getProperty(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE); String partial_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID); String vf_module_endpoint = - partial_endpoint.replaceFirst(MsoController.VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID); + partial_endpoint.replaceFirst(MsoController.VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID); - try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil( - server, - vf_module_endpoint, - HttpStatus.ACCEPTED_202, - CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) { + try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil( + server, + vf_module_endpoint, + HttpStatus.ACCEPTED_202, + CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) { closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createVfModuleInstance); } } @@ -170,36 +183,38 @@ public class MsoRestClientNewTest { } catch (Exception e) { } } - + @Ignore @Test public void testDeleteSvcInstance() throws Exception { String endpoint = props.getProperty(MsoProperties.MSO_REST_API_SVC_INSTANCE); endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID); - try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil( - server, - endpoint, - HttpStatus.NO_CONTENT_204, - CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) { + try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil( + server, + endpoint, + HttpStatus.NO_CONTENT_204, + CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) { closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteSvcInstance); } } + @Ignore @Test public void testDeleteVnf() throws Exception { String endpoint = props.getProperty(MsoProperties.MSO_REST_API_VNF_INSTANCE); endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID); - try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil( - server, - endpoint, - HttpStatus.NO_CONTENT_204, - CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) { + try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil( + server, + endpoint, + HttpStatus.NO_CONTENT_204, + CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) { closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteVnf); } } + @Ignore @Test public void testDeleteVfModule() throws Exception { String endpoint = props.getProperty(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE); @@ -207,15 +222,16 @@ public class MsoRestClientNewTest { String vf_modules_endpoint = part_endpoint.replaceFirst(MsoController.VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID); String delete_vf_endpoint = vf_modules_endpoint + '/' + SAMPLE_VNF_MODULE_ID; - try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil( - server, - delete_vf_endpoint, - HttpStatus.NO_CONTENT_204, - CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) { + try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil( + server, + delete_vf_endpoint, + HttpStatus.NO_CONTENT_204, + CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) { closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteVfModule); } } + @Ignore @Test public void testDeleteVolumeGroupInstance() throws Exception { String endpoint = props.getProperty(MsoProperties.MSO_REST_API_VOLUME_GROUP_INSTANCE); @@ -223,15 +239,16 @@ public class MsoRestClientNewTest { String vnf_endpoint = svc_endpoint.replaceFirst(MsoController.VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID); String delete_volume_group_endpoint = vnf_endpoint + "/" + SAMPLE_VNF_MODULE_ID; - try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil( - server, - delete_volume_group_endpoint, - HttpStatus.NO_CONTENT_204, - CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) { + try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil( + server, + delete_volume_group_endpoint, + HttpStatus.NO_CONTENT_204, + CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) { closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteVolumeGroupInstance); } } + @Ignore @Test public void testDeleteNwInstance() throws Exception { String endpoint = props.getProperty(MsoProperties.MSO_REST_API_NETWORK_INSTANCE); @@ -410,11 +427,11 @@ public class MsoRestClientNewTest { String serviceEndpoint = props.getProperty(MsoProperties.MSO_REST_API_SVC_INSTANCE); String removeRelationshipsPath = serviceEndpoint + "/" + SERVICE_INSTANCE_ID + "/removeRelationships"; - try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil( - server, - removeRelationshipsPath, - HttpStatus.ACCEPTED_202, - CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) { + try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil( + server, + removeRelationshipsPath, + HttpStatus.ACCEPTED_202, + CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) { closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::removeRelationshipFromServiceInstance); } } @@ -433,6 +450,20 @@ public class MsoRestClientNewTest { } catch (Exception e) { } } + @Test + public void testScaleOutVfModule() throws IOException { + String serviceEndpoint = props.getProperty(MsoProperties.MSO_REST_API_VF_MODULE_SCALE_OUT); + String partial_endpoint = serviceEndpoint.replaceFirst(SVC_INSTANCE_ID, SERVICE_INSTANCE_ID); + String vf_module_endpoint = partial_endpoint.replaceFirst(VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID); + try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil( + server, + vf_module_endpoint, + HttpStatus.ACCEPTED_202, + CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) { + closure.executePostCall(msoScaleOutVfModule, msoRestClient()::scaleOutVFModuleInstance); + } + + } private MsoRestClientNew msoRestClient() { return new MsoRestClientNew(new SyncRestClient(MsoInterface.objectMapper()), baseUrl()); diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTestUtil.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTestUtil.java index e8f556999..c81fa16fd 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTestUtil.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTestUtil.java @@ -20,11 +20,13 @@ import java.util.function.BiFunction; import java.util.function.Function; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; + import org.glassfish.grizzly.http.Method; import org.glassfish.grizzly.http.util.HttpStatus; import org.json.JSONObject; import org.junit.Assert; import org.onap.portalsdk.core.util.SystemProperties; +import org.onap.vid.changeManagement.RequestDetailsWrapper; import org.onap.vid.mso.MsoResponseWrapper; class MsoRestClientTestUtil implements AutoCloseable { @@ -60,6 +62,21 @@ class MsoRestClientTestUtil implements AutoCloseable { verifyServer(server, endpoint, Method.POST); } + void executePostCall(String jsonPayload, BiFunction<RequestDetailsWrapper, String, MsoResponseWrapper> func) throws IOException { + whenHttp(server) + .match(post(endpoint)) + .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON)); + + RequestDetailsWrapper sampleRequestDetails = + new ObjectMapper().readValue(jsonPayload, RequestDetailsWrapper.class); + + MsoResponseWrapper response = func.apply(sampleRequestDetails, endpoint); + JSONObject actualJson = new JSONObject(response.getEntity()); + + Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus()); + Assert.assertEquals(expectedResponseStr, actualJson.toString()); + verifyServer(server, endpoint, Method.POST); + } void executeDelete(String jsonPayload, BiFunction<RequestDetails, String, MsoResponseWrapper> func) throws IOException { |