From b1d739306129ef001a8f9f9f75dc34c568a3d5d0 Mon Sep 17 00:00:00 2001 From: kurczews Date: Fri, 17 May 2019 09:10:27 +0200 Subject: Add MsoControllerTests Issue-ID: VID-470 Change-Id: I9416247d8d9548d1c01af070d8106b1512dee200 Signed-off-by: kurczews --- vid-app-common/pom.xml | 2 +- .../org/onap/vid/controller/MsoControllerTest.java | 189 +++++++++++---------- 2 files changed, 104 insertions(+), 87 deletions(-) (limited to 'vid-app-common') diff --git a/vid-app-common/pom.xml b/vid-app-common/pom.xml index d3c6fbeeb..333181d9d 100755 --- a/vid-app-common/pom.xml +++ b/vid-app-common/pom.xml @@ -822,7 +822,7 @@ org.assertj assertj-core 3.10.0 - compile + test com.google.guava 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 9b0c32028..b0378b054 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 @@ -20,107 +20,124 @@ package org.onap.vid.controller; -import org.apache.commons.lang.StringEscapeUtils; -import org.onap.portalsdk.core.util.SystemProperties; -import org.onap.vid.factories.MsoRequestFactory; -import org.onap.vid.mso.model.RequestInfo; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.only; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.List; +import java.util.stream.Collectors; +import org.jeasy.random.EasyRandom; +import org.jeasy.random.EasyRandomParameters; +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; import org.onap.vid.mso.rest.RequestDetails; import org.onap.vid.mso.rest.Task; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; -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.Test; - -import java.util.List; - - -@WebAppConfiguration -@ContextConfiguration(classes = {SystemProperties.class, MsoConfig.class}) -public class MsoControllerTest extends AbstractTestNGSpringContextTests { - - @Autowired - MsoRequestFactory msoRequestFactory; - - @Test(enabled = false) - public void testInstanceCreationNew() throws Exception { - - RequestDetails requestDetails = msoRequestFactory.createMsoRequest("msoRequest.json"); - MsoController msoController = new MsoController(null, null); - //TODO: make ths test to really test something - //ResponseEntity responseEntityNew = msoController.createSvcInstanceNew(null, requestDetails); - ResponseEntity responseEntity = msoController.createSvcInstance(null, requestDetails); - //Assert.assertEquals(responseEntityNew, responseEntity); - +import org.onap.vid.services.CloudOwnerService; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +public class MsoControllerTest { + + private static final long STATIC_SEED = 5336L; + private EasyRandomParameters parameters = new EasyRandomParameters() + .randomize(Boolean.class, new BooleanRandomizer(STATIC_SEED)) + .randomize(String.class, new StringRandomizer(4, 4, STATIC_SEED)) + .collectionSizeRange(2, 3); + private EasyRandom modelGenerator = new EasyRandom(parameters); + private ObjectMapper objectMapper = new ObjectMapper(); + + private MockMvc mockMvc; + private MsoBusinessLogic msoBusinessLogic; + private CloudOwnerService cloudService; + + @Before + public void setUp() { + msoBusinessLogic = mock(MsoBusinessLogic.class); + cloudService = mock(CloudOwnerService.class); + MsoController msoController = new MsoController(msoBusinessLogic, cloudService); + + mockMvc = MockMvcBuilders.standaloneSetup(msoController).build(); } - @Test(enabled = false) - public void testInstanceCreationLocalWithRest() throws Exception { - - RequestDetails requestDetails = msoRequestFactory.createMsoRequest("msoRequest.json"); - MsoController msoController = new MsoController(null, null); - ResponseEntity responseEntityNew = msoController.createSvcInstance(null, requestDetails); - //TODO: make ths test to really test something -// ResponseEntity responseEntityRest = msoController.createSvcInstanceNewRest(null, requestDetails); -// -// Assert.assertEquals(responseEntityNew.getBody(), responseEntityRest.getBody()); - + @Test + public void shouldDelegateNewInstanceCreation() throws Exception { + // given + RequestDetails given = modelGenerator.nextObject(RequestDetails.class); + String payload = objectMapper.writeValueAsString(given); + + MsoResponseWrapper expectedResponse = new MsoResponseWrapper(200, "test"); + given(msoBusinessLogic + .createSvcInstance(argThat(request -> asJson(request).equals(payload)))) + .willReturn(expectedResponse); + + // when & then + mockMvc.perform(post("/mso/mso_create_svc_instance") + .content(payload) + .contentType(APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().json(asJson(expectedResponse))); + + ArgumentCaptor captor = ArgumentCaptor.forClass(RequestDetails.class); + then(cloudService).should(only()).enrichRequestWithCloudOwner(captor.capture()); + assertThat(captor.getValue()).matches(request -> asJson(request).equals(payload)); } - @Test(enabled = false) - public void testInstanceCreation() throws Exception { - - RequestDetails requestDetails = msoRequestFactory.createMsoRequest("msoRequest.json"); - MsoController msoController = new MsoController(null, null); - ResponseEntity responseEntity = msoController.createSvcInstance(null, requestDetails); + @Test + public void shouldReturnOrchestrationRequestsForDashboard() throws Exception { + // given + List orchestrationRequests = modelGenerator + .objects(Request.class, 2) + .collect(Collectors.toList()); + given(msoBusinessLogic.getOrchestrationRequestsForDashboard()).willReturn(orchestrationRequests); - Assert.assertEquals(responseEntity.getBody(), "{ \"status\": 200, \"entity\": {\n" + - " \"requestReferences\": {\n" + - " \"instanceId\": \"ba00de9b-3c3e-4b0a-a1ad-0c5489e711fb\",\n" + - " \"requestId\": \"311cc766-b673-4a50-b9c5-471f68914586\"\n" + - " }\n" + - "}}"); + // when & then + mockMvc.perform(get("/mso/mso_get_orch_reqs/dashboard")) + .andExpect(status().isOk()) + .andExpect(content().json(objectMapper.writeValueAsString(orchestrationRequests))); + then(cloudService).shouldHaveZeroInteractions(); } - @Test(enabled = false) - public void testGetOrchestrationRequestsForDashboard() throws Exception { - MsoController msoController = new MsoController(null, null); - List orchestrationRequestsForDashboard = msoController.getOrchestrationRequestsForDashboard(); + @Test + public void shouldReturnManualTasksById() throws Exception { + // given + List manualTasks = modelGenerator + .objects(Task.class, 2) + .collect(Collectors.toList()); - Assert.assertEquals(orchestrationRequestsForDashboard.size(), 2); - } + String originalRequestId = "za1234d1-5a33-55df-13ab-12abad84e335"; + given(msoBusinessLogic.getManualTasksByRequestId(originalRequestId)).willReturn(manualTasks); - @Test(enabled = false) - public void testGetManualTasksByRequestId() throws Exception { - MsoController msoController = new MsoController(null, null); - List orchestrationRequestsForDashboard = msoController.getManualTasksByRequestId("za1234d1-5a33-55df-13ab-12abad84e335"); + // when & then + mockMvc.perform(get("/mso/mso_get_man_task/{id}", originalRequestId)) + .andExpect(status().isOk()) + .andExpect(content().json(objectMapper.writeValueAsString(manualTasks))); - Assert. assertEquals(orchestrationRequestsForDashboard.get(0).getTaskId(), "daf4dd84-b77a-42da-a051-3239b7a9392c"); + then(cloudService).shouldHaveZeroInteractions(); } - - public void testCompleteManualTask() throws Exception { // TODO not done yet - RequestInfo requestInfo = new RequestInfo(); - requestInfo.setResponseValue("rollback"); - requestInfo.setRequestorId("abc"); - requestInfo.setSource("VID"); - RequestDetails requestDetails = new RequestDetails(); - requestDetails.setRequestInfo(requestInfo); - MsoController msoController = new MsoController(null, null); - ResponseEntity responseEntity = msoController.manualTaskComplete("daf4dd84-b77a-42da-a051-3239b7a9392c", requestDetails); - String assertString = "{ \\\"status\\\": 200, \\\"entity\\\": {\\n\" +\n" + - " \" \\\"taskRequestReference\\\": {\\n\" +\n" + - " \" \\\"taskId\\\": \\\"daf4dd84-b77a-42da-a051-3239b7a9392c\\\"\\n\" +\n" + - " \" }\\n\" +\n" + - " \"}\\n\" +\n" + - " \"}"; - Assert.assertEquals(responseEntity.getBody(), StringEscapeUtils.unescapeJava(assertString)); + private String asJson(T value) { + try { + return objectMapper.writeValueAsString(value); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } } - - -} +} \ No newline at end of file -- cgit 1.2.3-korg