diff options
author | Einat Vinouze <einat.vinouze@intl.att.com> | 2019-09-12 15:29:11 +0300 |
---|---|---|
committer | Eylon Malin <eylon.malin@intl.att.com> | 2019-09-12 13:33:16 +0000 |
commit | d4838465bd008b85ab3214b6431e6daa7d059589 (patch) | |
tree | 6bba8d26ce1a845c670381647e21ba2fc574ae69 /vid-app-common | |
parent | ca6f40516028602d3551856301cd9d7cda65f812 (diff) |
logging requests and responses in SyncRestClient
Issue-ID: VID-611
Signed-off-by: Einat Vinouze <einat.vinouze@intl.att.com>
Change-Id: I0e64e9520e566f9317f3fcbf0495b67349993ce4
Diffstat (limited to 'vid-app-common')
3 files changed, 117 insertions, 14 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/client/SyncRestClient.java b/vid-app-common/src/main/java/org/onap/vid/client/SyncRestClient.java index 18f87223c..f7087568d 100644 --- a/vid-app-common/src/main/java/org/onap/vid/client/SyncRestClient.java +++ b/vid-app-common/src/main/java/org/onap/vid/client/SyncRestClient.java @@ -3,6 +3,7 @@ * VID * ================================================================================ * Copyright (C) 2018 - 2019 Nokia. All rights reserved. + * Modifications 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. @@ -54,6 +55,7 @@ import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; import org.onap.portalsdk.core.util.SystemProperties; import org.onap.vid.properties.VidProperties; import org.onap.vid.utils.Logging; +import org.springframework.http.HttpMethod; public class SyncRestClient implements SyncRestClientInterface { private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SyncRestClient.class); @@ -89,68 +91,133 @@ public class SyncRestClient implements SyncRestClientInterface { @Override public HttpResponse<JsonNode> post(String url, Map<String, String> headers, Object body) { - return callWithRetryOverHttp(url, url2 -> restClient.post(url2).headers(headers).body(body).asJson()); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.POST, url, body); + + HttpResponse<JsonNode> response = callWithRetryOverHttp(url, + url2 -> restClient.post(url2).headers(headers).body(body).asJson()); + + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.POST, url, response); + + return response; } @Override public <T> HttpResponse<T> post(String url, Map<String, String> headers, Object body, Class<T> responseClass) { - return callWithRetryOverHttp(url, - url2 -> restClient.post(url2).headers(headers).body(body).asObject(responseClass)); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.POST, url, body); + + HttpResponse<T> response = callWithRetryOverHttp(url, + url2 -> restClient.post(url2).headers(headers).body(body).asObject(responseClass)); + + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.POST, url, response); + + return response; } @Override public HttpResponse<JsonNode> get(String url, Map<String, String> headers, Map<String, String> routeParams) { - return callWithRetryOverHttp(url, url2 -> { + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.GET, url, routeParams); + + HttpResponse<JsonNode> response = callWithRetryOverHttp(url, url2 -> { GetRequest getRequest = restClient.get(url2).headers(headers); routeParams.forEach(getRequest::routeParam); return getRequest.asJson(); }); + + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, response); + + return response; } @Override public <T> HttpResponse<T> get(String url, Map<String, String> headers, Map<String, String> routeParams, Class<T> responseClass) { - return callWithRetryOverHttp(url, url2 -> { + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.GET, url, routeParams); + + HttpResponse<T> response = callWithRetryOverHttp(url, url2 -> { GetRequest getRequest = restClient.get(url2).headers(headers); routeParams.forEach(getRequest::routeParam); return getRequest.asObject(responseClass); }); + + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, response); + + return response; } @Override public HttpResponse<InputStream> getStream(String url, Map<String, String> headers, Map<String, String> routeParams) { - return callWithRetryOverHttp(url, url2 -> { + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.GET, url, routeParams); + + HttpResponse<InputStream> response = callWithRetryOverHttp(url, url2 -> { GetRequest getRequest = restClient.get(url2).headers(headers); routeParams.forEach(getRequest::routeParam); return getRequest.asBinary(); }); + + //no logging of the response since the response is too long + return response; + } @Override public HttpResponse<JsonNode> put(String url, Map<String, String> headers, Object body) { - return callWithRetryOverHttp(url, url2 -> restClient.put(url2).headers(headers).body(body).asJson()); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.PUT, url, body); + + HttpResponse<JsonNode> response = callWithRetryOverHttp(url, + url2 -> restClient.put(url2).headers(headers).body(body).asJson()); + + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.PUT, url, response); + + return response; } @Override public <T> HttpResponse<T> put(String url, Map<String, String> headers, Object body, Class<T> responseClass) { - return callWithRetryOverHttp(url, - url2 -> restClient.put(url2).headers(headers).body(body).asObject(responseClass)); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.PUT, url, body); + + HttpResponse<T> response = callWithRetryOverHttp(url, + url2 -> restClient.put(url2).headers(headers).body(body).asObject(responseClass)); + + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.PUT, url, response); + + return response; } @Override public <T> HttpResponse<T> delete(String url, Map<String, String> headers, Object body, Class<T> responseClass) { - return callWithRetryOverHttp(url, url2 -> restClient.delete(url2).headers(headers).body(body).asObject(responseClass)); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url, body); + + HttpResponse<T> response = callWithRetryOverHttp(url, + url2 -> restClient.delete(url2).headers(headers).body(body).asObject(responseClass)); + + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, response); + + return response; } @Override public <T> HttpResponse<T> delete(String url, Map<String, String> headers, Class<T> responseClass) { - return callWithRetryOverHttp(url, url2 -> restClient.delete(url2).headers(headers).asObject(responseClass)); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url); + + HttpResponse<T> response = callWithRetryOverHttp(url, + url2 -> restClient.delete(url2).headers(headers).asObject(responseClass)); + + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, response); + + return response; } @Override public HttpResponse<JsonNode> delete(String url, Map<String, String> headers) { - return callWithRetryOverHttp(url, url2 -> restClient.delete(url2).headers(headers).asJson()); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url); + + HttpResponse<JsonNode> response = callWithRetryOverHttp(url, + url2 -> restClient.delete(url2).headers(headers).asJson()); + + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, response); + + return response; } @Override @@ -171,7 +238,7 @@ public class SyncRestClient implements SyncRestClientInterface { return patched(httpRequest.apply(url)); } catch (RestClientException e) { if (causedBySslHandshakeError(e)) { - logger.warn(EELFLoggerDelegate.debugLogger, "SSL Handshake problem occured. Will try to retry over Http.", e); + logger.warn("SSL Handshake problem occured. Will try to retry over Http.", e); return patched(httpRequest.apply(url.replaceFirst(HTTPS_SCHEMA, HTTP_SCHEMA))); } throw e; @@ -218,7 +285,7 @@ public class SyncRestClient implements SyncRestClientInterface { return HttpClients.custom().setSSLSocketFactory(sslSf).build(); } catch (IOException | CertificateException | UnrecoverableKeyException | NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) { - logger.warn(EELFLoggerDelegate.debugLogger, "Cannot initialize custom http client from current configuration. Using default one.", e); + logger.warn("Cannot initialize custom http client from current configuration. Using default one.", e); return HttpClients.createDefault(); } } diff --git a/vid-app-common/src/test/java/org/onap/vid/client/SyncRestClientForHttpServerTest.java b/vid-app-common/src/test/java/org/onap/vid/client/SyncRestClientForHttpServerTest.java index b30cf5f32..5a2eb59d2 100644 --- a/vid-app-common/src/test/java/org/onap/vid/client/SyncRestClientForHttpServerTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/client/SyncRestClientForHttpServerTest.java @@ -3,6 +3,7 @@ * VID * ================================================================================ * Copyright (C) 2018 - 2019 Nokia. All rights reserved. + * Modifications 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. @@ -26,9 +27,13 @@ import static com.xebialabs.restito.semantics.Action.contentType; import static com.xebialabs.restito.semantics.Action.ok; import static com.xebialabs.restito.semantics.Action.status; import static com.xebialabs.restito.semantics.Action.stringContent; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.testng.Assert.assertEquals; +import com.att.eelf.configuration.EELFLogger; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableMap; @@ -42,6 +47,7 @@ import java.util.Map; import org.glassfish.grizzly.http.Method; import org.glassfish.grizzly.http.util.HttpStatus; import org.onap.vid.utils.Logging; +import org.springframework.http.HttpMethod; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -80,6 +86,8 @@ public class SyncRestClientForHttpServerTest { .get(url, Collections.emptyMap(), Collections.emptyMap()); // then verifyHttp(stubServer).once(Condition.method(Method.GET), Condition.url(url)); + verify(mockLoggingService).logRequest(any(EELFLogger.class), eq(HttpMethod.GET), eq(url), eq(Collections.emptyMap())); + verify(mockLoggingService).logResponse(any(EELFLogger.class), eq(HttpMethod.GET), eq(url), eq(jsonNodeHttpResponse)); assertEquals(jsonNodeHttpResponse.getStatus(), 200); assertEquals(jsonNodeHttpResponse.getBody().getObject().get("key"), 1); assertEquals(jsonNodeHttpResponse.getBody().getObject().get("value"), "test"); @@ -95,6 +103,8 @@ public class SyncRestClientForHttpServerTest { .get(url, Collections.emptyMap(), Collections.emptyMap(), SyncRestClientModel.TestModel.class); // then verifyHttp(stubServer).once(Condition.method(Method.GET), Condition.url(url)); + verify(mockLoggingService).logRequest(any(EELFLogger.class), eq(HttpMethod.GET), eq(url), eq(Collections.emptyMap())); + verify(mockLoggingService).logResponse(any(EELFLogger.class), eq(HttpMethod.GET), eq(url), eq(testModelHttpResponse)); assertEquals(testModelHttpResponse.getStatus(), 200); assertEquals(testModelHttpResponse.getBody().getKey(), 1); assertEquals(testModelHttpResponse.getBody().getValue(), "test"); @@ -108,6 +118,8 @@ public class SyncRestClientForHttpServerTest { // when HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.post(url, Collections.emptyMap(), testObject); // then + verify(mockLoggingService).logRequest(any(EELFLogger.class), eq(HttpMethod.POST), eq(url), eq(testObject)); + verify(mockLoggingService).logResponse(any(EELFLogger.class), eq(HttpMethod.POST), eq(url), eq(jsonNodeHttpResponse)); verifyHttp(stubServer).once(Condition.method(Method.POST), Condition.url(url)); assertEquals(jsonNodeHttpResponse.getStatus(), 200); assertEquals(jsonNodeHttpResponse.getBody().getObject().get("key"), 1); @@ -123,6 +135,8 @@ public class SyncRestClientForHttpServerTest { HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient .post(url, Collections.emptyMap(), NOT_EXISTING_OBJECT); // then + verify(mockLoggingService).logRequest(any(EELFLogger.class), eq(HttpMethod.POST), eq(url), eq(NOT_EXISTING_OBJECT)); + verify(mockLoggingService).logResponse(any(EELFLogger.class), eq(HttpMethod.POST), eq(url), eq(jsonNodeHttpResponse)); assertEquals(jsonNodeHttpResponse.getStatus(), 404); assertEquals(jsonNodeHttpResponse.getStatusText(), "Not Found"); } @@ -137,6 +151,8 @@ public class SyncRestClientForHttpServerTest { HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.post(url, headers, testObject); // then verifyHttp(stubServer).once(Condition.withHeader("Authorization")); + verify(mockLoggingService).logRequest(any(EELFLogger.class), eq(HttpMethod.POST), eq(url), eq(testObject)); + verify(mockLoggingService).logResponse(any(EELFLogger.class), eq(HttpMethod.POST), eq(url), eq(jsonNodeHttpResponse)); assertEquals(jsonNodeHttpResponse.getStatus(), 200); } @@ -160,6 +176,8 @@ public class SyncRestClientForHttpServerTest { .post(url, Collections.emptyMap(), testObject, SyncRestClientModel.TestModel.class); // then verifyHttp(stubServer).once(Condition.method(Method.POST), Condition.url(url)); + verify(mockLoggingService).logRequest(any(EELFLogger.class), eq(HttpMethod.POST), eq(url), eq(testObject)); + verify(mockLoggingService).logResponse(any(EELFLogger.class), eq(HttpMethod.POST), eq(url), eq(objectHttpResponse)); assertEquals(objectHttpResponse.getStatus(), 200); assertEquals(objectHttpResponse.getBody().getKey(), 1); assertEquals(objectHttpResponse.getBody().getValue(), "test"); @@ -174,6 +192,8 @@ public class SyncRestClientForHttpServerTest { HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.put(url, Collections.emptyMap(), testObject); // then verifyHttp(stubServer).once(Condition.method(Method.PUT), Condition.url(url)); + verify(mockLoggingService).logRequest(any(EELFLogger.class), eq(HttpMethod.PUT), eq(url), eq(testObject)); + verify(mockLoggingService).logResponse(any(EELFLogger.class), eq(HttpMethod.PUT), eq(url), eq(jsonNodeHttpResponse)); assertEquals(jsonNodeHttpResponse.getStatus(), 201); assertEquals(jsonNodeHttpResponse.getBody().getObject().get("key"), 1); assertEquals(jsonNodeHttpResponse.getBody().getObject().get("value"), "test"); @@ -189,6 +209,8 @@ public class SyncRestClientForHttpServerTest { .put(url, Collections.emptyMap(), testObject, SyncRestClientModel.TestModel.class); // then verifyHttp(stubServer).once(Condition.method(Method.PUT), Condition.url(url)); + verify(mockLoggingService).logRequest(any(EELFLogger.class), eq(HttpMethod.PUT), eq(url), eq(testObject)); + verify(mockLoggingService).logResponse(any(EELFLogger.class), eq(HttpMethod.PUT), eq(url), eq(modelHttpResponse)); assertEquals(modelHttpResponse.getStatus(), 201); assertEquals(modelHttpResponse.getBody().getKey(), 1); assertEquals(modelHttpResponse.getBody().getValue(), "test"); @@ -203,6 +225,8 @@ public class SyncRestClientForHttpServerTest { HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.delete(url, Collections.emptyMap()); // then verifyHttp(stubServer).once(Condition.method(Method.DELETE), Condition.url(url)); + verify(mockLoggingService).logRequest(any(EELFLogger.class), eq(HttpMethod.DELETE), eq(url)); + verify(mockLoggingService).logResponse(any(EELFLogger.class), eq(HttpMethod.DELETE), eq(url), eq(jsonNodeHttpResponse)); assertEquals(jsonNodeHttpResponse.getStatus(), 200); assertEquals(jsonNodeHttpResponse.getBody().getObject().get("key"), 1); assertEquals(jsonNodeHttpResponse.getBody().getObject().get("value"), "test"); @@ -218,6 +242,8 @@ public class SyncRestClientForHttpServerTest { .delete(url, Collections.emptyMap(), SyncRestClientModel.TestModel.class); // then verifyHttp(stubServer).once(Condition.method(Method.DELETE), Condition.url(url)); + verify(mockLoggingService).logRequest(any(EELFLogger.class), eq(HttpMethod.DELETE), eq(url)); + verify(mockLoggingService).logResponse(any(EELFLogger.class), eq(HttpMethod.DELETE), eq(url), eq(modelHttpResponse)); assertEquals(modelHttpResponse.getStatus(), 200); assertEquals(modelHttpResponse.getBody().getKey(), 1); assertEquals(modelHttpResponse.getBody().getValue(), "test"); diff --git a/vid-app-common/src/test/java/org/onap/vid/client/SyncRestClientForHttpsServerTest.java b/vid-app-common/src/test/java/org/onap/vid/client/SyncRestClientForHttpsServerTest.java index 758dd070b..f4692c564 100644 --- a/vid-app-common/src/test/java/org/onap/vid/client/SyncRestClientForHttpsServerTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/client/SyncRestClientForHttpsServerTest.java @@ -3,6 +3,7 @@ * VID * ================================================================================ * Copyright (C) 2018 - 2019 Nokia. All rights reserved. + * Modifications 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. @@ -26,9 +27,13 @@ import static com.xebialabs.restito.semantics.Action.contentType; import static com.xebialabs.restito.semantics.Action.ok; import static com.xebialabs.restito.semantics.Action.stringContent; import static org.apache.http.client.config.RequestConfig.custom; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.testng.Assert.assertEquals; +import com.att.eelf.configuration.EELFLogger; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.xebialabs.restito.semantics.Action; @@ -50,6 +55,7 @@ import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.glassfish.grizzly.http.Method; import org.onap.vid.utils.Logging; +import org.springframework.http.HttpMethod; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -89,6 +95,8 @@ public class SyncRestClientForHttpsServerTest { // then verifyHttp(stubServer) .once(Condition.method(Method.GET), Condition.url(securedUrl), Condition.not(Condition.url(notSecuredUrl))); + verify(mockLoggingService).logRequest(any(EELFLogger.class), eq(HttpMethod.GET), eq(securedUrl), eq(Collections.emptyMap())); + verify(mockLoggingService).logResponse(any(EELFLogger.class), eq(HttpMethod.GET), eq(securedUrl), eq(jsonNodeHttpResponse)); assertEquals(jsonNodeHttpResponse.getStatus(), 200); assertEquals(jsonNodeHttpResponse.getBody().getObject().get("key"), 1); assertEquals(jsonNodeHttpResponse.getBody().getObject().get("value"), "test"); @@ -107,6 +115,8 @@ public class SyncRestClientForHttpsServerTest { // then verifyHttp(stubServer) .once(Condition.method(Method.GET), Condition.url(securedUrl), Condition.not(Condition.url(notSecuredUrl))); + verify(mockLoggingService).logRequest(any(EELFLogger.class), eq(HttpMethod.GET), eq(securedUrl), eq(Collections.emptyMap())); + verify(mockLoggingService).logResponse(any(EELFLogger.class), eq(HttpMethod.GET), eq(securedUrl), eq(testModelHttpResponse)); assertEquals(testModelHttpResponse.getStatus(), 200); assertEquals(testModelHttpResponse.getBody().getKey(), 1); assertEquals(testModelHttpResponse.getBody().getValue(), "test"); |