handle errors in change management and tenant isolation 28/92728/4
authorEylon Malin <eylon.malin@intl.att.com>
Mon, 5 Aug 2019 20:38:18 +0000 (23:38 +0300)
committerEylon Malin <eylon.malin@intl.att.com>
Tue, 6 Aug 2019 09:32:00 +0000 (12:32 +0300)
use more tolerance way for parsing responses from MSO so VID can handle
error responses with html body
Issue-ID: VID-378
Signed-off-by: Eylon Malin <eylon.malin@intl.att.com>
Change-Id: I72741ec1bf636a1200e8ecbd684ab1f3b2ab332a
Signed-off-by: Ittay Stern <ittay.stern@att.com>
Signed-off-by: Eylon Malin <eylon.malin@intl.att.com>
vid-app-common/src/main/java/org/onap/vid/controller/OperationalEnvironmentController.java
vid-app-common/src/main/java/org/onap/vid/mso/MsoResponseWrapper2.java
vid-app-common/src/main/java/org/onap/vid/mso/MsoUtil.java
vid-app-common/src/main/java/org/onap/vid/mso/rest/MsoRestClientNew.java
vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTest.java

index a6778ad..44bdc81 100644 (file)
@@ -33,6 +33,7 @@ import org.onap.vid.model.RequestReferencesContainer;
 import org.onap.vid.mso.MsoBusinessLogic;
 import org.onap.vid.mso.MsoInterface;
 import org.onap.vid.mso.MsoResponseWrapper2;
+import org.onap.vid.mso.MsoUtil;
 import org.onap.vid.mso.model.OperationalEnvironmentActivateInfo;
 import org.onap.vid.mso.model.OperationalEnvironmentDeactivateInfo;
 import org.onap.vid.mso.rest.OperationalEnvironment.OperationEnvironmentRequestDetails;
@@ -80,9 +81,7 @@ public class OperationalEnvironmentController extends VidRestrictedBaseControlle
         RequestDetailsWrapper<OperationEnvironmentRequestDetails> requestDetailsWrapper = msoBusinessLogic.convertParametersToRequestDetails(operationalEnvironment, userId);
         String path = msoBusinessLogic.getOperationalEnvironmentCreationPath();
 
-        HttpResponse<RequestReferencesContainer> msoResponse = restMso.post(path, requestDetailsWrapper, RequestReferencesContainer.class);
-        debugEnd(msoResponse);
-        return new MsoResponseWrapper2<>(msoResponse);
+        return MsoUtil.wrapResponse2(restMso.post(path, requestDetailsWrapper, String.class), RequestReferencesContainer.class);
     }
 
     @RequestMapping(value = "/activate", method = RequestMethod.POST)
@@ -105,10 +104,7 @@ public class OperationalEnvironmentController extends VidRestrictedBaseControlle
         String path = msoBusinessLogic.getOperationalEnvironmentActivationPath(activateInfo);
         RequestDetailsWrapper<RequestDetails> requestDetailsWrapper = msoBusinessLogic.createOperationalEnvironmentActivationRequestDetails(activateInfo);
 
-        HttpResponse<RequestReferencesContainer> msoResponse = restMso.post(path, requestDetailsWrapper, RequestReferencesContainer.class);
-
-        debugEnd(msoResponse);
-        return new MsoResponseWrapper2<>(msoResponse);
+        return MsoUtil.wrapResponse2(restMso.post(path, requestDetailsWrapper, String.class), RequestReferencesContainer.class);
     }
 
     @RequestMapping(value = "/deactivate", method = RequestMethod.POST)
@@ -126,10 +122,7 @@ public class OperationalEnvironmentController extends VidRestrictedBaseControlle
         String path = msoBusinessLogic.getOperationalEnvironmentDeactivationPath(deactivateInfo);
         RequestDetailsWrapper<RequestDetails> requestDetailsWrapper =  msoBusinessLogic.createOperationalEnvironmentDeactivationRequestDetails(deactivateInfo);
 
-        HttpResponse<RequestReferencesContainer> msoResponse = restMso.post(path, requestDetailsWrapper, RequestReferencesContainer.class);
-
-        debugEnd(msoResponse);
-        return new MsoResponseWrapper2<>(msoResponse);
+        return MsoUtil.wrapResponse2(restMso.post(path, requestDetailsWrapper, String.class), RequestReferencesContainer.class);
     }
 
     @RequestMapping(value = "/requestStatus", method = RequestMethod.GET)
@@ -140,10 +133,7 @@ public class OperationalEnvironmentController extends VidRestrictedBaseControlle
         verifyIsNotEmpty(requestId, "requestId");
         String path = msoBusinessLogic.getCloudResourcesRequestsStatusPath(requestId);
 
-        HttpResponse<HashMap> msoResponse = restMso.get(path, HashMap.class);
-
-        LOGGER.debug(EELFLoggerDelegate.debugLogger, "end {}() => {}", getMethodName(), msoResponse);
-        return new MsoResponseWrapper2<>(msoResponse);
+        return MsoUtil.wrapResponse2(restMso.get(path, String.class), HashMap.class);
     }
 
     @ExceptionHandler({
index 9b61718..318aa6e 100644 (file)
@@ -65,6 +65,12 @@ public class MsoResponseWrapper2<T> implements MsoResponseWrapperInterface {
         this.raw = null;
     }
 
+    public MsoResponseWrapper2(int status, T entity, String raw) {
+        this.status = status;
+        this.entity = entity;
+        this.raw = raw;
+    }
+
     public int getStatus() {
         return status;
     }
index fcc20fa..5171b7a 100644 (file)
@@ -25,7 +25,6 @@ import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
 
 import com.fasterxml.jackson.core.JsonParseException;
 import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
 import io.joshworks.restclient.http.HttpResponse;
 import java.io.IOException;
 import org.apache.commons.lang3.StringUtils;
@@ -34,7 +33,6 @@ import org.onap.vid.exceptions.GenericUncheckedException;
 
 public class MsoUtil {
 
-    static final ObjectMapper objectMapper = new ObjectMapper();
 
     private MsoUtil() {
     }
@@ -51,7 +49,7 @@ public class MsoUtil {
         if (httpResponse.getRawBody() != null) {
             try {
                 T body = httpResponse.getBody();
-                String entityStr = body instanceof String ? (String) body : objectMapper.writeValueAsString(httpResponse.getBody());
+                String entityStr = body instanceof String ? (String) body : JACKSON_OBJECT_MAPPER.writeValueAsString(httpResponse.getBody());
                 msoResponseWrapper.setEntity(entityStr);
             } catch(JsonProcessingException e) {
                 ExceptionUtils.rethrow(e);
@@ -60,6 +58,22 @@ public class MsoUtil {
         return msoResponseWrapper;
     }
 
+    public static <T> MsoResponseWrapper2<T> wrapResponse2(HttpResponse<String> httpResponse, Class<T> clazz) {
+        String raw = httpResponse.getBody(); // As body's T is String, use getBody as a simple way to get raw
+        if (raw != null) {
+            try {
+                T entity = JACKSON_OBJECT_MAPPER.readValue(raw, clazz);
+                return new MsoResponseWrapper2<>(httpResponse.getStatus(), entity, raw);
+            } catch (JsonProcessingException exception) {
+                return new MsoResponseWrapper2<>(httpResponse.getStatus(), null, raw);
+            } catch (IOException exception) {
+                ExceptionUtils.rethrow(exception);
+            }
+        }
+
+        return new MsoResponseWrapper2<>(httpResponse.getStatus(), null, null);
+    }
+
     public static String formatExceptionAdditionalInfo(int statusCode, String msoResponse) {
         String errorMsg = "Http Code:" + statusCode;
         if (!StringUtils.isEmpty(msoResponse)) {
index 64182e0..df8034b 100644 (file)
@@ -330,8 +330,8 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf
     @Override
     public MsoResponseWrapperInterface changeManagementUpdate(RequestDetailsWrapper requestDetails, String endpoint) {
         String path = baseUrl + endpoint;
-        HttpResponse<RequestReferencesContainer> response = client.post(path, commonHeaders, requestDetails, RequestReferencesContainer.class);
-        return MsoUtil.wrapResponse(response);
+        HttpResponse<String> response = client.post(path, commonHeaders, requestDetails, String.class);
+        return MsoUtil.wrapResponse2(response, RequestReferencesContainer.class);
     }
 
     public MsoResponseWrapper replaceInstance(org.onap.vid.changeManagement.RequestDetails request, String path) {
index 8ea4836..e4e699d 100644 (file)
@@ -29,6 +29,7 @@ import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.when;
 import static org.mockito.MockitoAnnotations.initMocks;
 
+import com.fasterxml.jackson.core.JsonProcessingException;
 import io.joshworks.restclient.http.HttpResponse;
 import io.joshworks.restclient.http.JsonMapper;
 import org.apache.http.ProtocolVersion;
@@ -60,6 +61,7 @@ import java.util.UUID;
 
 import static org.mockito.ArgumentMatchers.refEq;
 import static org.mockito.hamcrest.MockitoHamcrest.argThat;
+import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
 
 
 @ContextConfiguration(classes = {LocalWebConfig.class, SystemProperties.class})
@@ -593,25 +595,23 @@ public class MsoRestClientTest {
     }
 
     @Test
-    public void shouldProperlyChangeManagementUpdate() {
+    public void shouldProperlyChangeManagementUpdate() throws JsonProcessingException {
         //  given
         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
         RequestDetailsWrapper<RequestDetails> requestDetailsWrapper = new RequestDetailsWrapper<>(requestDetails);
 
         String endpoint = "testEndpoint";
-        HttpResponse<RequestReferencesContainer> httpResponse = HttpResponse.fallback(
-                new RequestReferencesContainer(
-                        new RequestReferences()));
+        RequestReferencesContainer entity = new RequestReferencesContainer(new RequestReferences());
+        HttpResponse<String> httpResponse = HttpResponse.fallback(JACKSON_OBJECT_MAPPER.writeValueAsString(entity));
 
-        MsoResponseWrapperInterface expectedResponse = MsoUtil.wrapResponse(httpResponse);
-
-        when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetailsWrapper), eq(RequestReferencesContainer.class))).thenReturn(httpResponse);
+        when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetailsWrapper), eq(String.class))).thenReturn(httpResponse);
 
         //  when
         MsoResponseWrapperInterface response = restClient.changeManagementUpdate(requestDetailsWrapper, endpoint);
 
         //  then
-        assertThat(expectedResponse).isEqualToComparingFieldByField(response);
+        assertThat(response.getEntity()).isEqualToComparingFieldByField(entity);
+        assertThat(response.getStatus()).isEqualTo(0);
     }
 
     @Test