From 456cf4f6af9c22449dd4a39b1d13900fc38481c1 Mon Sep 17 00:00:00 2001 From: "k.kedron" Date: Tue, 30 Apr 2019 16:37:54 +0200 Subject: New unit tests and sonar fixes Assertion arguments should be passed in the correct order in: - CldsReferencePropertiesItCase - LoggingUtils Remove of duplicate code in LoggingUtils. Typo in README file. Add LoggingUtilsTest unit test. Change-Id: Iddc61877659e9d4308479451a7eb4901b3fc5ea8 Issue-ID: CLAMP-355 Signed-off-by: Krystian Kedron --- .../org/onap/clamp/clds/util/LoggingUtils.java | 66 +++++++++------------- 1 file changed, 26 insertions(+), 40 deletions(-) (limited to 'src/main') diff --git a/src/main/java/org/onap/clamp/clds/util/LoggingUtils.java b/src/main/java/org/onap/clamp/clds/util/LoggingUtils.java index cbe7eba96..163ab6913 100644 --- a/src/main/java/org/onap/clamp/clds/util/LoggingUtils.java +++ b/src/main/java/org/onap/clamp/clds/util/LoggingUtils.java @@ -28,6 +28,7 @@ import com.att.eelf.configuration.EELFManager; import java.net.HttpURLConnection; import java.net.InetAddress; +import java.net.URLConnection; import java.net.UnknownHostException; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -114,7 +115,7 @@ public class LoggingUtils { */ public static void setResponseContext(String code, String description, String className) { MDC.put("ResponseCode", code); - MDC.put("StatusCode", code.equals("0") ? "COMPLETE" : "ERROR"); + MDC.put("StatusCode", "0".equals(code) ? "COMPLETE" : "ERROR"); MDC.put("ResponseDescription", description != null ? description : ""); MDC.put("ClassName", className != null ? className : ""); } @@ -167,8 +168,6 @@ public class LoggingUtils { return dateFormat; } - - /********************************************************************************************* * Method for ONAP Application Logging Specification v1.2 ********************************************************************************************/ @@ -256,7 +255,7 @@ public class LoggingUtils { public String getProperties(String name) { return MDC.get(name); } - + /** * Report pending invocation with INVOKE marker, * setting standard ONAP logging headers automatically. @@ -267,24 +266,7 @@ public class LoggingUtils { * @return The HTTP url connection */ public HttpURLConnection invoke(final HttpURLConnection con, String targetEntity, String targetServiceName) { - final String invocationId = UUID.randomUUID().toString(); - - // Set standard HTTP headers on (southbound request) builder. - con.setRequestProperty(ONAPLogConstants.Headers.REQUEST_ID, - defaultToEmpty(MDC.get(ONAPLogConstants.MDCs.REQUEST_ID))); - con.setRequestProperty(ONAPLogConstants.Headers.INVOCATION_ID, - invocationId); - con.setRequestProperty(ONAPLogConstants.Headers.PARTNER_NAME, - defaultToEmpty(MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME))); - - invokeContext(targetEntity, targetServiceName, invocationId); - - // Log INVOKE*, with the invocationID as the message body. - // (We didn't really want this kind of behavior in the standard, - // but is it worse than new, single-message MDC?) - this.mlogger.info(ONAPLogConstants.Markers.INVOKE); - this.mlogger.info(ONAPLogConstants.Markers.INVOKE_SYNC + "{" + invocationId + "}"); - return con; + return this.invokeGeneric(con, targetEntity, targetServiceName); } /** @@ -316,24 +298,7 @@ public class LoggingUtils { * @return The HTTPS url connection */ public HttpsURLConnection invokeHttps(final HttpsURLConnection con, String targetEntity, String targetServiceName) { - final String invocationId = UUID.randomUUID().toString(); - - // Set standard HTTP headers on (southbound request) builder. - con.setRequestProperty(ONAPLogConstants.Headers.REQUEST_ID, - defaultToEmpty(MDC.get(ONAPLogConstants.MDCs.REQUEST_ID))); - con.setRequestProperty(ONAPLogConstants.Headers.INVOCATION_ID, - invocationId); - con.setRequestProperty(ONAPLogConstants.Headers.PARTNER_NAME, - defaultToEmpty(MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME))); - - invokeContext(targetEntity, targetServiceName, invocationId); - - // Log INVOKE*, with the invocationID as the message body. - // (We didn't really want this kind of behavior in the standard, - // but is it worse than new, single-message MDC?) - this.mlogger.info(ONAPLogConstants.Markers.INVOKE); - this.mlogger.info(ONAPLogConstants.Markers.INVOKE_SYNC + "{" + invocationId + "}"); - return con; + return this.invokeGeneric(con, targetEntity, targetServiceName); } /** @@ -410,4 +375,25 @@ public class LoggingUtils { MDC.remove(ONAPLogConstants.MDCs.INVOCATIONID_OUT); MDC.remove(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP); } + + private T invokeGeneric(final T con, String targetEntity, String targetServiceName) { + final String invocationId = UUID.randomUUID().toString(); + + // Set standard HTTP headers on (southbound request) builder. + con.setRequestProperty(ONAPLogConstants.Headers.REQUEST_ID, + defaultToEmpty(MDC.get(ONAPLogConstants.MDCs.REQUEST_ID))); + con.setRequestProperty(ONAPLogConstants.Headers.INVOCATION_ID, + invocationId); + con.setRequestProperty(ONAPLogConstants.Headers.PARTNER_NAME, + defaultToEmpty(MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME))); + + invokeContext(targetEntity, targetServiceName, invocationId); + + // Log INVOKE*, with the invocationID as the message body. + // (We didn't really want this kind of behavior in the standard, + // but is it worse than new, single-message MDC?) + this.mlogger.info(ONAPLogConstants.Markers.INVOKE); + this.mlogger.info(ONAPLogConstants.Markers.INVOKE_SYNC + "{" + invocationId + "}"); + return con; + } } -- cgit 1.2.3-korg