Implant vid-app-common org.onap.vid.job (main and test)
[vid.git] / vid-app-common / src / test / java / org / onap / vid / controller / MsoControllerTest.java
index c69bce3..a1b4559 100644 (file)
+/*-
+ * ============LICENSE_START=======================================================
+ * VID
+ * ================================================================================
+ * Copyright (C) 2017 - 2019 AT&T Intellectual Property. 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.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.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;
+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.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+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.onap.vid.mso.MsoBusinessLogic;
+import org.onap.vid.mso.MsoResponseWrapper;
+import org.onap.vid.mso.rest.MsoRestClientNew;
 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 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;
+    private MsoRestClientNew msoRestClient;
+
+    @Before
+    public void setUp() {
+        msoBusinessLogic = mock(MsoBusinessLogic.class);
+        cloudService = mock(CloudOwnerService.class);
+        msoRestClient = mock(MsoRestClientNew.class);
+        MsoController msoController = new MsoController(msoBusinessLogic, msoRestClient, cloudService);
+
+        mockMvc = MockMvcBuilders.standaloneSetup(msoController).build();
+    }
 
-import java.util.List;
+    @Test
+    public void shouldDelegateNewServiceInstantiation() throws Exception {
+        // given
+        RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
 
+        MsoResponseWrapper expectedResponse = new MsoResponseWrapper(200, "test");
+        given(msoBusinessLogic
+            .createSvcInstance(objectEqualTo(requestDetails)))
+            .willReturn(expectedResponse);
 
-@WebAppConfiguration
-@ContextConfiguration(classes = {SystemProperties.class, MsoConfig.class})
-public class MsoControllerTest extends AbstractTestNGSpringContextTests {
+        // when & then
+        mockMvc.perform(post("/mso/mso_create_svc_instance")
+            .content(asJson(requestDetails))
+            .contentType(APPLICATION_JSON))
+            .andExpect(status().isOk())
+            .andExpect(content().json(asJson(expectedResponse)));
 
-    @Autowired
-    MsoRequestFactory msoRequestFactory;
+        then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
+    }
+
+    @Test
+    public void shouldReturnOrchestrationRequestsForDashboard() throws Exception {
+        // given
+        List<Request> orchestrationRequests = modelGenerator
+            .objects(Request.class, 2)
+            .collect(Collectors.toList());
 
-    @Test(enabled = false)
-    public void testInstanceCreationNew() throws Exception {
+        given(msoBusinessLogic.getOrchestrationRequestsForDashboard()).willReturn(orchestrationRequests);
 
-        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);
+        // 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 testInstanceCreationLocalWithRest() throws Exception {
+    @Test
+    public void shouldReturnManualTasksById() throws Exception {
+        // given
+        List<Task> manualTasks = modelGenerator
+            .objects(Task.class, 2)
+            .collect(Collectors.toList());
 
-        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());
+        String originalRequestId = "za1234d1-5a33-55df-13ab-12abad84e335";
+        given(msoBusinessLogic.getManualTasksByRequestId(originalRequestId)).willReturn(manualTasks);
 
+        // when & then
+        mockMvc.perform(get("/mso/mso_get_man_task/{id}", originalRequestId))
+            .andExpect(status().isOk())
+            .andExpect(content().json(objectMapper.writeValueAsString(manualTasks)));
+
+        then(cloudService).shouldHaveZeroInteractions();
     }
 
-    @Test(enabled = false)
-    public void testInstanceCreation() throws Exception {
+    @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();
+    }
 
-        RequestDetails requestDetails = msoRequestFactory.createMsoRequest("msoRequest.json");
-        MsoController msoController = new MsoController(null, null);
-        ResponseEntity<String> responseEntity = msoController.createSvcInstance(null, requestDetails);
+    @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);
 
-        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(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(enabled = false)
-    public void testGetOrchestrationRequestsForDashboard() throws Exception {
-        MsoController msoController = new MsoController(null, null);
-        List<Request> orchestrationRequestsForDashboard = msoController.getOrchestrationRequestsForDashboard();
+    @Test
+    public void shouldDelegateVnfInstantiation() throws Exception {
+        // given
+        RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
+        String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
 
-        Assert.assertEquals(orchestrationRequestsForDashboard.size(), 2);
+        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(enabled = false)
-    public void testGetManualTasksByRequestId() throws Exception {
-        MsoController msoController = new MsoController(null, null);
-        List<Task> orchestrationRequestsForDashboard = msoController.getManualTasksByRequestId("za1234d1-5a33-55df-13ab-12abad84e335");
+    @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"));
 
-        Assert. assertEquals(orchestrationRequestsForDashboard.get(0).getTaskId(), "daf4dd84-b77a-42da-a051-3239b7a9392c");
+        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";
 
-    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));
+        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);
+        } catch (JsonProcessingException e) {
+            throw new RuntimeException(e);
+        }
+    }
 
-}
+    private <T> T objectEqualTo(T expected) {
+        return argThat(given -> asJson(given).equals(asJson(expected)));
+    }
+}
\ No newline at end of file