diff options
Diffstat (limited to 'vid-app-common/src/test/java/org')
4 files changed, 150 insertions, 61 deletions
diff --git a/vid-app-common/src/test/java/org/onap/vid/controller/MsoControllerTest.java b/vid-app-common/src/test/java/org/onap/vid/controller/MsoControllerTest.java index b0378b054..02ab287be 100644 --- a/vid-app-common/src/test/java/org/onap/vid/controller/MsoControllerTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/controller/MsoControllerTest.java @@ -7,9 +7,9 @@ * 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. @@ -20,8 +20,8 @@ package org.onap.vid.controller; -import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.mock; @@ -34,7 +34,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import org.jeasy.random.EasyRandom; import org.jeasy.random.EasyRandomParameters; @@ -42,7 +44,6 @@ import org.jeasy.random.randomizers.misc.BooleanRandomizer; import org.jeasy.random.randomizers.text.StringRandomizer; import org.junit.Before; import org.junit.Test; -import org.mockito.ArgumentCaptor; import org.onap.vid.mso.MsoBusinessLogic; import org.onap.vid.mso.MsoResponseWrapper; import org.onap.vid.mso.rest.Request; @@ -76,26 +77,23 @@ public class MsoControllerTest { } @Test - public void shouldDelegateNewInstanceCreation() throws Exception { + public void shouldDelegateNewServiceInstantiation() throws Exception { // given - RequestDetails given = modelGenerator.nextObject(RequestDetails.class); - String payload = objectMapper.writeValueAsString(given); + RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class); MsoResponseWrapper expectedResponse = new MsoResponseWrapper(200, "test"); given(msoBusinessLogic - .createSvcInstance(argThat(request -> asJson(request).equals(payload)))) + .createSvcInstance(objectEqualTo(requestDetails))) .willReturn(expectedResponse); // when & then mockMvc.perform(post("/mso/mso_create_svc_instance") - .content(payload) + .content(asJson(requestDetails)) .contentType(APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().json(asJson(expectedResponse))); - ArgumentCaptor<RequestDetails> captor = ArgumentCaptor.forClass(RequestDetails.class); - then(cloudService).should(only()).enrichRequestWithCloudOwner(captor.capture()); - assertThat(captor.getValue()).matches(request -> asJson(request).equals(payload)); + then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails)); } @Test @@ -133,6 +131,106 @@ public class MsoControllerTest { then(cloudService).shouldHaveZeroInteractions(); } + @Test + public void shouldDelegateE2EServiceInstantiation() throws Exception { + // given + String requestDetails = "some request details"; + Map<String, Object> payload = new LinkedHashMap<>(); + payload.put("requestDetails", requestDetails); + + MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class); + given(wrapper.getResponse()).willReturn("some response"); + given(msoBusinessLogic.createE2eSvcInstance(requestDetails)).willReturn(wrapper); + + // when & then + mockMvc.perform(post("/mso/mso_create_e2e_svc_instance") + .content(asJson(payload)) + .contentType(APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string("some response")); + + then(cloudService).shouldHaveZeroInteractions(); + } + + @Test + public void shouldDelegateServiceInstantiation() throws Exception { + // given + RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class); + + MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class); + given(wrapper.getResponse()).willReturn("some response"); + given(msoBusinessLogic.createSvcInstance(objectEqualTo(requestDetails))).willReturn(wrapper); + + // when & then + mockMvc.perform(post("/mso/mso_create_svc_instance") + .content(asJson(requestDetails)) + .contentType(APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string("some response")); + + then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails)); + } + + @Test + public void shouldDelegateVnfInstantiation() throws Exception { + // given + RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class); + String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014"; + + MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class); + given(wrapper.getResponse()).willReturn("some response"); + given(msoBusinessLogic.createVnf(objectEqualTo(requestDetails), eq(serviceInstanceId))).willReturn(wrapper); + + // when & then + mockMvc.perform(post("/mso/mso_create_vnf_instance/" + serviceInstanceId) + .content(asJson(requestDetails)) + .contentType(APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string("some response")); + + then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails)); + } + + @Test + public void shouldDelegateNewInstanceCreation() throws Exception { + // given + RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class); + String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014"; + + MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class); + given(wrapper.getResponse()).willReturn("some response"); + given(msoBusinessLogic.createNwInstance(objectEqualTo(requestDetails), eq(serviceInstanceId))).willReturn(wrapper); + + // when & then + mockMvc.perform(post("/mso/mso_create_nw_instance/" + serviceInstanceId) + .content(asJson(requestDetails)) + .contentType(APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string("some response")); + + then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails)); + } + + @Test + public void shouldCompleteManualTask() throws Exception { + // given + RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class); + String taskId = "bc305d54-75b4-431b-adb2-eb6b9e546014"; + + MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class); + given(wrapper.getResponse()).willReturn("some response"); + given(msoBusinessLogic.completeManualTask(objectEqualTo(requestDetails), eq(taskId))).willReturn(wrapper); + + // when & then + mockMvc.perform(post("/mso/mso_post_man_task/" + taskId) + .content(asJson(requestDetails)) + .contentType(APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string("some response")); + + then(cloudService).shouldHaveZeroInteractions(); + } + private <T> String asJson(T value) { try { return objectMapper.writeValueAsString(value); @@ -140,4 +238,8 @@ public class MsoControllerTest { throw new RuntimeException(e); } } + + private <T> T objectEqualTo(T expected) { + return argThat(given -> asJson(given).equals(asJson(expected))); + } }
\ No newline at end of file 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 a7fa8cd13..0efdb6b69 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 @@ -92,7 +92,6 @@ import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import static org.onap.vid.controller.MsoController.CONFIGURATION_ID; import static org.onap.vid.controller.MsoController.REQUEST_TYPE; import static org.onap.vid.controller.MsoController.SVC_INSTANCE_ID; @@ -628,26 +627,6 @@ public class MsoBusinessLogicImplTest extends AbstractTestNGSpringContextTests { } @Test(expectedExceptions = MsoTestException.class) - public void shouldThrowExceptionWhenActivateServiceInstanceWithWrongParameters() { - //given - RequestDetails requestDetails = new RequestDetails(); - String taskId = "testTaskId"; - - RestObject<String> restObjStr = new RestObject<>(); - restObjStr.set(""); - MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(restObjStr); - - doThrow(new MsoTestException("testException")). - when(msoInterface).setServiceInstanceStatus(eq(requestDetails), any(String.class), any(String.class), any(String.class), any(RestObject.class)); - - //when - MsoResponseWrapper response = msoBusinessLogic.activateServiceInstance(requestDetails, taskId); - - //then - assertThat(response).isEqualToComparingFieldByField(expectedResponse); - } - - @Test(expectedExceptions = MsoTestException.class) public void shouldThrowExceptionWhenManualTaskWithWrongParameters() { //given RequestDetails requestDetails = new RequestDetails(); @@ -1118,38 +1097,34 @@ public class MsoBusinessLogicImplTest extends AbstractTestNGSpringContextTests { public void shouldProperlySetServiceInstanceStatusActiveWithProperParameters() { // given String serviceInstanceId = "testServiceId"; + MsoResponseWrapper okResponse = createOkResponse(); org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails(); - RestObject<String> restObjStr = new RestObject<>(); - restObjStr.set(""); - MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(restObjStr); + given(msoInterface.setServiceInstanceStatus(eq(requestDetails), endsWith(serviceInstanceId + "/activate"))).willReturn(okResponse); // when MsoResponseWrapper response = msoBusinessLogic.setServiceInstanceStatus(requestDetails, serviceInstanceId, true); // then - assertThat(response).isEqualToComparingFieldByField(expectedResponse); - + assertThat(response).isEqualToComparingFieldByField(okResponse); } @Test public void shouldProperlySetServiceInstanceStatusDeactivateWithProperParameters() { // given String serviceInstanceId = "testServiceId"; + MsoResponseWrapper okResponse = createOkResponse(); org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails(); - RestObject<String> restObjStr = new RestObject<>(); - restObjStr.set(""); - MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(restObjStr); + given(msoInterface.setServiceInstanceStatus(eq(requestDetails), endsWith(serviceInstanceId + "/deactivate"))).willReturn(okResponse); // when MsoResponseWrapper response = msoBusinessLogic.setServiceInstanceStatus(requestDetails, serviceInstanceId, false); // then - assertThat(response).isEqualToComparingFieldByField(expectedResponse); - + assertThat(response).isEqualToComparingFieldByField(okResponse); } @Test(expectedExceptions = MsoTestException.class) @@ -1158,7 +1133,7 @@ public class MsoBusinessLogicImplTest extends AbstractTestNGSpringContextTests { String serviceInstanceId = "testServiceId"; doThrow(new MsoTestException("testException")). - when(msoInterface).setServiceInstanceStatus(eq(null), any(String.class), any(String.class), any(String.class), any(RestObject.class)); + when(msoInterface).setServiceInstanceStatus(eq(null), any(String.class)); // when msoBusinessLogic.setServiceInstanceStatus(null, serviceInstanceId, true); 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 832aa098a..d4abfae38 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 @@ -20,11 +20,19 @@ */ package org.onap.vid.mso.rest; +import static org.apache.commons.io.IOUtils.toInputStream; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.ArgumentMatchers.anyMap; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import static org.onap.vid.controller.MsoController.SVC_INSTANCE_ID; import static org.onap.vid.controller.MsoController.VNF_INSTANCE_ID; import com.fasterxml.jackson.databind.ObjectMapper; import com.xebialabs.restito.server.StubServer; +import io.joshworks.restclient.http.HttpResponse; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; @@ -413,20 +421,29 @@ public class MsoRestClientNewTest { } @Test - public void testSetServiceInstanceStatus() throws Exception { - MsoRestClientNew testSubject; - RequestDetails requestDetails = null; + public void testSetServiceInstanceStatus_givenValidResponse_responseIsPopulatedAccordingly() { + RequestDetails requestDetails = new RequestDetails(); String t = ""; String sourceId = ""; String endpoint = ""; - RestObject<String> restObject = null; + final SyncRestClient client = mock(SyncRestClient.class); + MsoRestClientNew testSubject = new MsoRestClientNew(client, "", null, new SystemPropertiesWrapper()); - // default test - try { - testSubject = createTestSubject(); - testSubject.setServiceInstanceStatus(requestDetails, t, sourceId, endpoint, restObject); - } catch (Exception e) { - } + // setup + final HttpResponse<String> response = mock(HttpResponse.class); + final int expectedStatus = 202; + final String expectedResponse = "expected response"; + + when(client.post(eq(endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(response); + when(response.getStatus()).thenReturn(expectedStatus); + when(response.getBody()).thenReturn(expectedResponse); + when(response.getRawBody()).thenReturn(toInputStream(expectedResponse)); + + // test + MsoResponseWrapper responseWrapper = testSubject.setServiceInstanceStatus(requestDetails, endpoint); + + assertThat(responseWrapper.getStatus(), is(expectedStatus)); + assertThat(responseWrapper.getEntity(), is(expectedResponse)); } @Test diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTest.java index 050fa0dce..8ea4836a2 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTest.java @@ -35,11 +35,9 @@ import org.apache.http.ProtocolVersion; import org.apache.http.StatusLine; import org.apache.http.message.BasicHttpResponse; import org.apache.http.message.BasicStatusLine; -import org.jetbrains.annotations.NotNull; import org.mockito.Mock; import org.onap.portalsdk.core.util.SystemProperties; import org.onap.vid.changeManagement.RequestDetailsWrapper; -import org.onap.vid.changeManagement.RequestParameters; import org.onap.vid.changeManagement.WorkflowRequestDetail; import org.onap.vid.client.SyncRestClient; import org.onap.vid.controller.LocalWebConfig; @@ -49,7 +47,6 @@ import org.onap.vid.mso.MsoResponseWrapper; import org.onap.vid.mso.MsoResponseWrapperInterface; import org.onap.vid.mso.MsoUtil; import org.onap.vid.mso.RestObject; -import org.onap.vid.mso.model.CloudConfiguration; import org.onap.vid.mso.model.RequestReferences; import org.onap.vid.utils.SystemPropertiesWrapper; import org.springframework.test.context.ContextConfiguration; @@ -57,9 +54,7 @@ import org.springframework.test.context.web.WebAppConfiguration; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.UUID; @@ -664,7 +659,7 @@ public class MsoRestClientTest { when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse); // when - restClient.setServiceInstanceStatus(requestDetails,"", "", endpoint, restObject); + restClient.setServiceInstanceStatus(requestDetails, endpoint); } @Test( expectedExceptions = MsoTestException.class) @@ -675,7 +670,7 @@ public class MsoRestClientTest { when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception")); // when - restClient.setServiceInstanceStatus(null,"", "", endpoint, null); + restClient.setServiceInstanceStatus(null, endpoint); } @Test |