diff options
author | Rodrigo Lima <rodrigo.lima@yoppworks.com> | 2020-05-14 10:14:34 -0400 |
---|---|---|
committer | Ofir Sonsino <ofir.sonsino@intl.att.com> | 2020-05-17 14:55:50 +0000 |
commit | 768742ee00920426d9d81aef56dcd0298bf9f1d0 (patch) | |
tree | 810fecbb50ad32e803ea10ebc68e79b1b19e29b8 /openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java | |
parent | 3a28b9af8d08fc2cf8d9af9b38578fd034999dc2 (diff) |
Remove powermock from ContextTracker
- Remove powermock from ContextTrackerTest
- Add package level constructor to ContextTracker to pass clear and put functions
Issue-ID: SDC-3058
Signed-off-by: Rodrigo Lima <rodrigo.lima@yoppworks.com>
Change-Id: I82519538b763b00f2768801f06929773bfb58013
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java')
-rw-r--r-- | openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/ContextTracker.java | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/ContextTracker.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/ContextTracker.java index 2aa5a83818..02745f6287 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/ContextTracker.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/ContextTracker.java @@ -18,6 +18,8 @@ package org.openecomp.sdc.logging.servlet; import java.util.Objects; import java.util.UUID; +import java.util.function.Consumer; +import java.util.function.Supplier; import javax.servlet.http.HttpServletRequest; import org.openecomp.sdc.logging.api.ContextData; import org.openecomp.sdc.logging.api.LoggingContext; @@ -33,6 +35,9 @@ public class ContextTracker implements Tracker { private final HttpHeader partnerNameHeaders; private final HttpHeader requestIdHeaders; + private final Supplier<Void> loggingContextClear; + private final Consumer<ContextData> loggingContextPut; + /** * Constructs tracker to handle required logging context in Servlet-based applications. Refer to ONAP logging * guidelines for fields required to be put on logging context. @@ -43,12 +48,36 @@ public class ContextTracker implements Tracker { public ContextTracker(HttpHeader partnerNameHeaders, HttpHeader requestIdHeaders) { this.partnerNameHeaders = Objects.requireNonNull(partnerNameHeaders); this.requestIdHeaders = Objects.requireNonNull(requestIdHeaders); + this.loggingContextPut = LoggingContext::put; + this.loggingContextClear = () -> { + LoggingContext.clear(); + return null; + }; + } + + /** + * Package level constructor used for tests. Clean and Put are passed as functions + * in order to avoid static mock and service loader config - LoggingServiceProvider in LoggingContext + * + * @param partnerNameHeaders + * @param requestIdHeaders + * @param loggingContextClear + * @param loggingContextPut + */ + ContextTracker(HttpHeader partnerNameHeaders, + HttpHeader requestIdHeaders, + Supplier<Void> loggingContextClear, + Consumer<ContextData> loggingContextPut) { + this.partnerNameHeaders = Objects.requireNonNull(partnerNameHeaders); + this.requestIdHeaders = Objects.requireNonNull(requestIdHeaders); + this.loggingContextPut = loggingContextPut; + this.loggingContextClear =loggingContextClear; } @Override public void preRequest(HttpServletRequest request) { - LoggingContext.clear(); + loggingContextClear.get(); String serviceName = ServiceNameFormatter.format(request); String requestId = requestIdHeaders.getAny(request::getHeader).orElse(UUID.randomUUID().toString()); @@ -57,11 +86,11 @@ public class ContextTracker implements Tracker { String partnerName = partnerNameHeaders.getAny(request::getHeader).orElse("UNKNOWN"); contextBuilder.partnerName(partnerName); - LoggingContext.put(contextBuilder.build()); + loggingContextPut.accept(contextBuilder.build()); } @Override public void postRequest(RequestProcessingResult result) { - LoggingContext.clear(); + loggingContextClear.get(); } } |