remove payload logging from metric/audit side 53/79053/1
authorBenjamin, Max (mb388a) <mb388a@us.att.com>
Fri, 22 Feb 2019 22:51:42 +0000 (17:51 -0500)
committerBenjamin, Max (mb388a) <mb388a@us.att.com>
Fri, 22 Feb 2019 22:51:56 +0000 (17:51 -0500)
fixed unit test after changing how entities are read
remove payload logging from metric/audit side

Change-Id: Ibfe7cf96c76920926e9ae9ce5041389324d09b46
Issue-ID: SO-1564
Signed-off-by: Benjamin, Max (mb388a) <mb388a@us.att.com>
bpmn/MSOCommonBPMN/src/test/java/org/onap/so/client/ResponseExceptionMapperImplTest.java
common/src/main/java/org/onap/so/client/ResponseExceptionMapper.java
common/src/main/java/org/onap/so/logging/jaxrs/filter/JaxRsClientLogging.java
common/src/test/java/org/onap/so/client/aai/AAIResourcesClientTest.java
common/src/test/resources/__files/aai/error-message.json [new file with mode: 0644]

index 2df4b0b..14a3039 100644 (file)
@@ -95,7 +95,7 @@ public class ResponseExceptionMapperImplTest{
         // given
        Response response = createMockResponse(Status.BAD_REQUEST);
         when(response.hasEntity()).thenReturn(true);
-        when(response.getEntity()).thenReturn(new ByteArrayInputStream("test message".getBytes(StandardCharsets.UTF_8)));
+        when(response.readEntity(String.class)).thenReturn("test message");
         
         expectedExceptionTest.expect(BadRequestException.class);
         expectedExceptionTest.expectMessage("test message");
index 31cdd50..bcc60b6 100644 (file)
@@ -20,9 +20,6 @@
 
 package org.onap.so.client;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.StringWriter;
 import java.util.Optional;
 
 import javax.ws.rs.BadRequestException;
@@ -33,29 +30,34 @@ import javax.ws.rs.NotAllowedException;
 import javax.ws.rs.NotAuthorizedException;
 import javax.ws.rs.NotFoundException;
 import javax.ws.rs.NotSupportedException;
+import javax.ws.rs.ProcessingException;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Response;
 
-import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public abstract class ResponseExceptionMapper {
-
+       private static final Logger logger = LoggerFactory.getLogger(ResponseExceptionMapper.class);
        public void map(Response response) {
-               
-               response.bufferEntity();
                if (response.getStatus() >= 300) {
+                       String body = "";
                        String message = "empty message";
-                       if (response.hasEntity()) {
-                               StringWriter writer = new StringWriter();
-                               try {
-                                       IOUtils.copy((InputStream)response.getEntity(), writer, "UTF-8");
-                               } catch (IOException e) {
-                                       writer.append("failed to read entity stream");
-                               }
-                               Optional<String> result = this.extractMessage(writer.toString());
-                               if (result.isPresent()) {
-                                       message = result.get();
+                       try {
+                               response.bufferEntity();
+                               if (response.hasEntity()) {
+                                       body = response.readEntity(String.class);
                                }
+                       } catch (IllegalStateException e) {
+                               body = "failed to read entity stream";
+                               logger.error(body, e);
+                       } catch (ProcessingException e) {
+                               body = "could not buffer stream";
+                               logger.error(body, e);
+                       }
+                       Optional<String> result = this.extractMessage(body);
+                       if (result.isPresent()) {
+                               message = result.get();
                        }
                        Response.Status status = Response.Status.fromStatusCode(response.getStatus());
                        WebApplicationException webAppException;
index 6c2a96c..436faef 100644 (file)
@@ -121,7 +121,6 @@ public class JaxRsClientLogging implements ClientRequestFilter,ClientResponseFil
                 statusCode=ONAPLogConstants.ResponseStatus.ERROR.toString();                           
             }
             MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, String.valueOf(responseContext.getStatus()));
-            MDC.put(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION,getStringFromInputStream(responseContext));
             MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode);
             logger.info(ONAPLogConstants.Markers.INVOKE_RETURN, "InvokeReturn");
             clearClientMDCs();
@@ -142,21 +141,4 @@ public class JaxRsClientLogging implements ClientRequestFilter,ClientResponseFil
         MDC.remove(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP);
     }
 
-    private static String getStringFromInputStream(ClientResponseContext clientResponseContext) {
-
-        InputStream is = clientResponseContext.getEntityStream();
-        ByteArrayOutputStream boas = new ByteArrayOutputStream();
-
-        try {
-            IOUtils.copy(is,boas);
-            InputStream copiedStream = new ByteArrayInputStream(boas.toByteArray());
-            clientResponseContext.setEntityStream(copiedStream);
-            return boas.toString();
-
-        } catch (IOException e) {
-            logger.warn("Failed to read response body", e);
-        }
-        return "Unable to read input stream";
-    }
-
 }
index a55fbc9..73fbff6 100644 (file)
@@ -35,6 +35,7 @@ import static org.junit.Assert.assertThat;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.spy;
 
+import javax.ws.rs.BadRequestException;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -206,6 +207,23 @@ public class AAIResourcesClientTest {
                AAIResultWrapper result = client.get(path, NotFoundException.class);
        }
        
+       @Test
+       public void verifyFailedCallException() {
+               AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
+               wireMockRule.stubFor(get(
+                               urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
+                               .willReturn(
+                                       aResponse()
+                                       .withHeader("Content-Type", "text/plain")
+                                       .withBodyFile("aai/error-message.json")
+                                       .withStatus(400)));
+               AAIResourcesClient client= aaiClient;
+               
+               thrown.expect(BadRequestException.class);
+               thrown.expectMessage(containsString("Invalid input performing PUT on url (msg=Precondition Required:resource-version not passed for update of url"));
+               AAIResultWrapper result = client.get(path);
+       }
+       
        @Test
        public void buildRelationshipTest() {
                AAIResourcesClient client = aaiClient;
diff --git a/common/src/test/resources/__files/aai/error-message.json b/common/src/test/resources/__files/aai/error-message.json
new file mode 100644 (file)
index 0000000..1552322
--- /dev/null
@@ -0,0 +1 @@
+{"requestError":{"serviceException":{"messageId":"SVC3000","text":"Invalid input performing %1 on %2 (msg=%3) (ec=%4)","variables":["PUT","url","Precondition Required:resource-version not passed for update of url","ERR.5.4.6130"]}}}
\ No newline at end of file