summaryrefslogtreecommitdiffstats
path: root/openecomp-be
diff options
context:
space:
mode:
Diffstat (limited to 'openecomp-be')
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/ContextTracker.java35
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ContextTrackerTest.java117
2 files changed, 78 insertions, 74 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();
}
}
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ContextTrackerTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ContextTrackerTest.java
index f1066c447b..272dfdd164 100644
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ContextTrackerTest.java
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ContextTrackerTest.java
@@ -16,23 +16,19 @@
package org.openecomp.sdc.logging.servlet;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.powermock.api.mockito.PowerMockito.mock;
-import static org.powermock.api.mockito.PowerMockito.mockStatic;
-import static org.powermock.api.mockito.PowerMockito.verifyStatic;
-import static org.powermock.api.mockito.PowerMockito.when;
-
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.function.Consumer;
+import java.util.function.Supplier;
import javax.servlet.http.HttpServletRequest;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.ArgumentCaptor;
+import org.junit.jupiter.api.Test;
+
import org.openecomp.sdc.logging.api.ContextData;
-import org.openecomp.sdc.logging.api.LoggingContext;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
/**
* Populating context from request data.
@@ -40,8 +36,6 @@ import org.powermock.modules.junit4.PowerMockRunner;
* @author evitaliy
* @since 01 Aug 2018
*/
-@RunWith(PowerMockRunner.class)
-@PrepareForTest(LoggingContext.class)
public class ContextTrackerTest {
private static final String X_REQUEST_ID = "X-REQUEST-ID";
@@ -50,50 +44,52 @@ public class ContextTrackerTest {
private static final String X_PARTNER_NAME = "X-PARTNER-NAME";
private static final HttpHeader PARTNER_NAME_HEADER = new HttpHeader(X_PARTNER_NAME);
- @Test(expected = NullPointerException.class)
+ private static final String PROVIDER_PATH = "org.openecomp.sdc.logging.servlet.TestLogginContextService";
+
+ private ContextData lastContext = ContextData.builder().build();
+
+ private final Supplier<Void> loggingContextClear = () -> {
+ lastContext = ContextData.builder().build();
+ return null;
+ };
+
+ private final Consumer<ContextData> loggingContextPut = (contextData) -> lastContext = contextData;
+
+ private final ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER,
+ REQUEST_ID_HEADER,
+ loggingContextClear,
+ loggingContextPut);
+
+ @Test
public void throwExceptionWhenPartnerNamesNull() {
- new ContextTracker(null, REQUEST_ID_HEADER);
+ assertThrows(NullPointerException.class,
+ () -> new ContextTracker(null, REQUEST_ID_HEADER));
}
- @Test(expected = NullPointerException.class)
+ @Test
public void throwExceptionWhenRequestIdsNull() {
- new ContextTracker(PARTNER_NAME_HEADER, null);
+ assertThrows(NullPointerException.class,
+ () -> new ContextTracker(PARTNER_NAME_HEADER, null));
}
@Test
public void requestIdCopiedWhenGiven() {
- mockStatic(LoggingContext.class);
-
final String requestId = "request-id-for-unit-testing";
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getHeader(X_REQUEST_ID)).thenReturn(requestId);
- ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER);
tracker.preRequest(request);
- ArgumentCaptor<ContextData> contextDataCaptor = ArgumentCaptor.forClass(ContextData.class);
- verifyStatic(LoggingContext.class);
-
- LoggingContext.put(contextDataCaptor.capture());
-
- assertEquals(requestId, contextDataCaptor.getValue().getRequestId());
+ assertEquals(requestId, lastContext.getRequestId());
}
@Test
public void requestIdGeneratedWhenNotGiven() {
- mockStatic(LoggingContext.class);
-
- ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER);
tracker.preRequest(mock(HttpServletRequest.class));
- ArgumentCaptor<ContextData> contextDataCaptor = ArgumentCaptor.forClass(ContextData.class);
- verifyStatic(LoggingContext.class);
-
- LoggingContext.put(contextDataCaptor.capture());
-
- String requestId = contextDataCaptor.getValue().getRequestId();
+ String requestId = lastContext.getRequestId();
assertNotNull(requestId);
assertFalse(requestId.isEmpty());
}
@@ -101,65 +97,44 @@ public class ContextTrackerTest {
@Test
public void partnerNameCopiedWhenGiven() {
- mockStatic(LoggingContext.class);
-
final String partner = "partner-name-for-unit-testing";
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getHeader(X_PARTNER_NAME)).thenReturn(partner);
- ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER);
tracker.preRequest(request);
- ArgumentCaptor<ContextData> contextDataCaptor = ArgumentCaptor.forClass(ContextData.class);
- verifyStatic(LoggingContext.class);
-
- LoggingContext.put(contextDataCaptor.capture());
-
- assertEquals(partner, contextDataCaptor.getValue().getPartnerName());
+ assertEquals(partner, lastContext.getPartnerName());
}
@Test
public void partnerNameIsUnknownWhenNotGiven() {
- mockStatic(LoggingContext.class);
-
- ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER);
tracker.preRequest(mock(HttpServletRequest.class));
- ArgumentCaptor<ContextData> contextDataCaptor = ArgumentCaptor.forClass(ContextData.class);
- verifyStatic(LoggingContext.class);
-
- LoggingContext.put(contextDataCaptor.capture());
-
- assertEquals(contextDataCaptor.getValue().getPartnerName(), "UNKNOWN");
+ assertEquals("UNKNOWN", lastContext.getPartnerName());
}
@Test
public void serviceNameGenerated() {
- mockStatic(LoggingContext.class);
-
- ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER);
- tracker.preRequest(mock(HttpServletRequest.class));
-
- ArgumentCaptor<ContextData> contextDataCaptor = ArgumentCaptor.forClass(ContextData.class);
- verifyStatic(LoggingContext.class);
+ HttpServletRequest request = mock(HttpServletRequest.class);
- LoggingContext.put(contextDataCaptor.capture());
+ when(request.getMethod()).thenReturn("GET");
+ when(request.getRequestURI()).thenReturn("/testUri");
+ tracker.preRequest(request);
- assertNotNull(contextDataCaptor.getValue().getServiceName());
+ assertEquals("GET: /testUri", lastContext .getServiceName());
}
@Test
public void contextClearedWhenRequestFinished() {
- mockStatic(LoggingContext.class);
+ tracker.preRequest(mock(HttpServletRequest.class));
+
+ assertNotNull(lastContext.getRequestId());
- ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER);
tracker.postRequest(mock(RequestProcessingResult.class));
- verifyStatic(LoggingContext.class);
- LoggingContext.clear();
+ assertEquals(ContextData.builder().build(), lastContext);
}
}
-