update MSO create service instance URL
[vid.git] / vid-app-common / src / test / java / org / onap / vid / mso / rest / MsoRestClientNewTest.java
index f58462d..0217204 100644 (file)
  */
 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;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.UUID;
+import org.apache.commons.configuration.PropertiesConfiguration;
 import org.glassfish.grizzly.http.util.HttpStatus;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
@@ -35,25 +53,15 @@ 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.onap.vid.utils.SystemPropertiesWrapper;
 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.controller.MsoController.SVC_INSTANCE_ID;
-import static org.onap.vid.controller.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 PropertiesConfiguration props = new PropertiesConfiguration();
     private static String msoCreateServiceInstanceJson;
     private static String msoScaleOutVfModule;
     private final static String CREATE_INSTANCE_RESPONSE_STR =
@@ -68,7 +76,7 @@ public class MsoRestClientNewTest {
 
 
     @BeforeClass
-    public static void start() throws IOException {
+    public static void start() throws Exception {
         server = new StubServer().run();
         securedServer = new StubServer().secured().run();
 
@@ -101,7 +109,7 @@ public class MsoRestClientNewTest {
 
     @Test
     public void testCreateSvcInstance() throws Exception {
-        String endpoint = props.getProperty(MsoProperties.MSO_REST_API_CONFIGURATIONS);
+        String endpoint = props.getString(MsoProperties.MSO_REST_API_CONFIGURATIONS);
         endpoint = endpoint.replace(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
                 server,
@@ -114,7 +122,7 @@ public class MsoRestClientNewTest {
 
     @Test
     public void testCreateVnf() throws Exception {
-        String endpoint = props.getProperty(MsoProperties.MSO_REST_API_VNF_INSTANCE);
+        String endpoint = props.getString(MsoProperties.MSO_REST_API_VNF_INSTANCE);
         endpoint = endpoint.replace(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
                 server,
@@ -128,7 +136,7 @@ public class MsoRestClientNewTest {
 
     @Test
     public void testCreateNwInstance() throws Exception {
-        String endpoint = props.getProperty(MsoProperties.MSO_REST_API_NETWORK_INSTANCE);
+        String endpoint = props.getString(MsoProperties.MSO_REST_API_NETWORK_INSTANCE);
         String nw_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
                 server,
@@ -141,7 +149,7 @@ public class MsoRestClientNewTest {
 
     @Test
     public void testCreateVolumeGroupInstance() throws Exception {
-        String endpoint = props.getProperty(MsoProperties.MSO_REST_API_VOLUME_GROUP_INSTANCE);
+        String endpoint = props.getString(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(
@@ -155,7 +163,7 @@ public class MsoRestClientNewTest {
 
     @Test
     public void testCreateVfModuleInstance() throws Exception {
-        String endpoint = props.getProperty(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE);
+        String endpoint = props.getString(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);
@@ -187,7 +195,7 @@ public class MsoRestClientNewTest {
     @Ignore
     @Test
     public void testDeleteSvcInstance() throws Exception {
-        String endpoint = props.getProperty(MsoProperties.MSO_REST_API_SVC_INSTANCE);
+        String endpoint = props.getString(MsoProperties.MSO_RESTAPI_SERVICE_INSTANCE);
         endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
 
 
@@ -203,7 +211,7 @@ public class MsoRestClientNewTest {
     @Ignore
     @Test
     public void testDeleteVnf() throws Exception {
-        String endpoint = props.getProperty(MsoProperties.MSO_REST_API_VNF_INSTANCE);
+        String endpoint = props.getString(MsoProperties.MSO_REST_API_VNF_INSTANCE);
         endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
 
         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
@@ -218,7 +226,7 @@ public class MsoRestClientNewTest {
     @Ignore
     @Test
     public void testDeleteVfModule() throws Exception {
-        String endpoint = props.getProperty(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE);
+        String endpoint = props.getString(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE);
         String part_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
         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;
@@ -235,7 +243,7 @@ public class MsoRestClientNewTest {
     @Ignore
     @Test
     public void testDeleteVolumeGroupInstance() throws Exception {
-        String endpoint = props.getProperty(MsoProperties.MSO_REST_API_VOLUME_GROUP_INSTANCE);
+        String endpoint = props.getString(MsoProperties.MSO_REST_API_VOLUME_GROUP_INSTANCE);
         String svc_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
         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;
@@ -252,7 +260,7 @@ public class MsoRestClientNewTest {
     @Ignore
     @Test
     public void testDeleteNwInstance() throws Exception {
-        String endpoint = props.getProperty(MsoProperties.MSO_REST_API_NETWORK_INSTANCE);
+        String endpoint = props.getString(MsoProperties.MSO_REST_API_NETWORK_INSTANCE);
         String svc_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
         String delete_nw_endpoint = svc_endpoint + "/" + SAMPLE_NETWORK_INSTANCE_ID;
 
@@ -267,7 +275,7 @@ public class MsoRestClientNewTest {
 
     @Test
     public void testGetOrchestrationRequest() {
-        String p = props.getProperty(MsoProperties.MSO_REST_API_GET_ORC_REQ);
+        String p = props.getString(MsoProperties.MSO_REST_API_GET_ORC_REQ);
         String path = p + "/" + SAMPLE_REQUEST_ID;
 
         try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
@@ -280,50 +288,39 @@ public class MsoRestClientNewTest {
     }
 
     @Test
-    public void testGetManualTasks() {
-        String p = props.getProperty(MsoProperties.MSO_REST_API_GET_ORC_REQ);
+    public void testGetManualTasksByRequestId() {
+        String p = props.getString(MsoProperties.MSO_REST_API_GET_ORC_REQ);
         String path = p + "/" + UUID.randomUUID();
+        String validResponse = "" 
+            + "{ "
+            + "  \"taskList\": [ "
+            + "    { "
+            + "      \"taskId\": \"daf4dd84-b77a-42da-a051-3239b7a9392c\", "
+            + "      \"type\": \"fallout\", "
+            + "      \"nfRole\": \"vEsmeralda\", "
+            + "      \"subscriptionServiceType\": \"Emanuel\", "
+            + "      \"originalRequestId\": \"d352c70d-5ef8-4977-9ea8-4c8cbe860422\", "
+            + "      \"originalRequestorId\": \"ss835w\", "
+            + "      \"errorSource\": \"A&AI\", "
+            + "      \"errorCode\": \"404\", "
+            + "      \"errorMessage\": \"Failed in A&AI 404\", "
+            + "      \"validResponses\": [ "
+            + "        \"rollback\", "
+            + "        \"abort\", "
+            + "        \"skip\", "
+            + "        \"resume\", "
+            + "        \"retry\" "
+            + "      ] "
+            + "    } "
+            + "  ] "
+            + "}";
 
         try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
             server,
             path,
             HttpStatus.OK_200,
-            CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) {
-            closure.executeGet(msoRestClient()::getManualTasks);
-        }
-    }
-
-    @Test
-    public void testGetOrchestrationRequestsForDashboard() throws Exception {
-        MsoRestClientNew testSubject;
-        String t = "";
-        String sourceId = "";
-        String endpoint = "";
-        RestObject restObject = null;
-        MsoResponseWrapper result;
-
-        // default test
-        try {
-            testSubject = createTestSubject();
-            result = testSubject.getOrchestrationRequest(t, sourceId, endpoint, restObject, true);
-        } catch (Exception e) {
-        }
-    }
-
-    @Test
-    public void testGetManualTasksByRequestId() throws Exception {
-        MsoRestClientNew testSubject;
-        String t = "";
-        String sourceId = "";
-        String endpoint = "";
-        RestObject restObject = null;
-        MsoResponseWrapper result;
-
-        // default test
-        try {
-            testSubject = createTestSubject();
-            result = testSubject.getManualTasksByRequestId(t, sourceId, endpoint, restObject);
-        } catch (Exception e) {
+            validResponse,validResponse)) {
+            closure.executeGet(endpoint -> msoRestClient().getManualTasksByRequestId(null, null, endpoint, null));
         }
     }
 
@@ -362,7 +359,7 @@ public class MsoRestClientNewTest {
 
     @Test
     public void testSetConfigurationActiveStatus() throws Exception {
-        String endpoint = "/serviceInstances/v5/<service_instance_id>/configurations/<configuration_id>";
+        String endpoint = "/serviceInstances/v7/<service_instance_id>/configurations/<configuration_id>";
         endpoint = endpoint.replace(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
         endpoint = endpoint.replace(MsoController.CONFIGURATION_ID, SAMPLE_CONFIGURATION_ID);
         endpoint = endpoint + "/activate";
@@ -407,25 +404,34 @@ 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
     public void testRemoveRelationshipFromServiceInstance() throws Exception {
-        String serviceEndpoint = props.getProperty(MsoProperties.MSO_REST_API_SVC_INSTANCE);
+        String serviceEndpoint = props.getString(MsoProperties.MSO_RESTAPI_SERVICE_INSTANCE);
         String removeRelationshipsPath = serviceEndpoint + "/" + SERVICE_INSTANCE_ID + "/removeRelationships";
 
         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
@@ -453,7 +459,7 @@ public class MsoRestClientNewTest {
     }
     @Test
     public void testScaleOutVfModule() throws IOException {
-        String serviceEndpoint = props.getProperty(MsoProperties.MSO_REST_API_VF_MODULE_SCALE_OUT);
+        String serviceEndpoint = props.getString(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(
@@ -468,10 +474,10 @@ public class MsoRestClientNewTest {
 
     private MsoRestClientNew msoRestClient() {
         final WebConfig webConfig = new WebConfig();
-        return new MsoRestClientNew(new SyncRestClient(webConfig.unirestFasterxmlObjectMapper(new ObjectMapper())), baseUrl(),null);
+        return new MsoRestClientNew(new SyncRestClient(webConfig.unirestFasterxmlObjectMapper(new ObjectMapper())), baseUrl(), null, new SystemPropertiesWrapper());
     }
 
     private MsoRestClientNew createTestSubject() {
-        return new MsoRestClientNew(null, "",null);
+        return new MsoRestClientNew(null, "", null, new SystemPropertiesWrapper());
     }
 }