Add MsoControllerTests 47/87947/5
authorkurczews <krzysztof.kurczewski@nokia.com>
Fri, 17 May 2019 07:10:27 +0000 (09:10 +0200)
committerWojciech Sliwka <wojciech.sliwka@nokia.com>
Thu, 30 May 2019 12:27:49 +0000 (12:27 +0000)
Issue-ID: VID-470
Change-Id: I9416247d8d9548d1c01af070d8106b1512dee200
Signed-off-by: kurczews <krzysztof.kurczewski@nokia.com>
vid-app-common/pom.xml
vid-app-common/src/test/java/org/onap/vid/controller/MsoControllerTest.java

index d3c6fbe..333181d 100755 (executable)
             <groupId>org.assertj</groupId>
             <artifactId>assertj-core</artifactId>
             <version>3.10.0</version>
-            <scope>compile</scope>
+            <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>com.google.guava</groupId>
index 9b0c320..b0378b0 100644 (file)
 
 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<String> responseEntityNew = msoController.createSvcInstanceNew(null, requestDetails);
-        ResponseEntity<String> 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<String> responseEntityNew = msoController.createSvcInstance(null, requestDetails);
-        //TODO: make ths test to really test something
-//        ResponseEntity<String> 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<RequestDetails> 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<String> responseEntity = msoController.createSvcInstance(null, requestDetails);
+    @Test
+    public void shouldReturnOrchestrationRequestsForDashboard() throws Exception {
+        // given
+        List<Request> 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<Request> orchestrationRequestsForDashboard = msoController.getOrchestrationRequestsForDashboard();
+    @Test
+    public void shouldReturnManualTasksById() throws Exception {
+        // given
+        List<Task> 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<Task> 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<String> 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 <T> String asJson(T value) {
+        try {
+            return objectMapper.writeValueAsString(value);
+        } catch (JsonProcessingException e) {
+            throw new RuntimeException(e);
+        }
     }
-
-
-}
+}
\ No newline at end of file