summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src
diff options
context:
space:
mode:
authorvempo <vitaliy.emporopulo@amdocs.com>2018-08-06 16:21:01 +0300
committerAvi Gaffa <avi.gaffa@amdocs.com>2018-08-06 16:09:04 +0000
commit44651d903d83f9256c324ec2a3c8886e91e1e433 (patch)
tree0e1961ce6c69dcb8eb18cf8b4e8af1e5eed6e3b4 /openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src
parent1ecdbf74427467327de0b1e11cd0a839d6dc05bd (diff)
Minor improvements to logging for servlets
Fixed import order, more resilient statuses for Spring, cleaner code and tests, more convenient APIs. Change-Id: I6493c81d9c3c1c543c354562e074876268794438 Issue-ID: SDC-1580 Signed-off-by: vempo <vitaliy.emporopulo@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src')
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/CombinedTracker.java33
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/HttpHeader.java2
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingRequestFilter.java10
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/CombinedTrackerTest.java59
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ContextTrackerTest.java4
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/HttpHeaderTest.java12
6 files changed, 91 insertions, 29 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/CombinedTracker.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/CombinedTracker.java
index 2dd2c12143..5d940aa821 100644
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/CombinedTracker.java
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/CombinedTracker.java
@@ -17,39 +17,38 @@
package org.openecomp.sdc.logging.servlet;
import javax.servlet.http.HttpServletRequest;
-import org.openecomp.sdc.logging.api.Logger;
/**
- * Tracker for all the elements of ONAP logging and tracing at an entry point to an application - context and audit.
- * The order of invocations is important, assuming the context must be kept as long as audit hasn't been finished.
+ * Tracker for all the elements of ONAP logging and tracing at an entry point to an application.
+ * The order of invocations is important, and on {@link #preRequest(HttpServletRequest)} it respects the order of
+ * trackers passed to the constructor. On {@link #postRequest(RequestProcessingResult)}, the invocation will be in the
+ * <b>reverse</b> order.
*
* @author evitaliy
* @since 01 Aug 2018
*/
public class CombinedTracker implements Tracker {
- private final ContextTracker context;
- private final AuditTracker audit;
+ private final Tracker[] trackers;
- public CombinedTracker(Logger logger, HttpHeader partnerNameHeader, HttpHeader requestIdHeader) {
- this.context = new ContextTracker(partnerNameHeader, requestIdHeader);
- this.audit = new AuditTracker(logger);
- }
-
- public CombinedTracker(Class<?> resourceType, HttpHeader partnerNameHeader, HttpHeader requestIdHeader) {
- this.context = new ContextTracker(partnerNameHeader, requestIdHeader);
- this.audit = new AuditTracker(resourceType);
+ public CombinedTracker(Tracker... trackers) {
+ this.trackers = new Tracker[trackers.length];
+ System.arraycopy(trackers, 0, this.trackers, 0, trackers.length);
}
@Override
public void preRequest(HttpServletRequest request) {
- this.context.preRequest(request);
- this.audit.preRequest(request);
+
+ for (Tracker t : trackers) {
+ t.preRequest(request);
+ }
}
@Override
public void postRequest(RequestProcessingResult result) {
- this.audit.postRequest(result);
- this.context.postRequest(result);
+
+ for (int i = trackers.length - 1; i > -1; i--) {
+ trackers[i].postRequest(result);
+ }
}
}
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/HttpHeader.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/HttpHeader.java
index db10c2e4f3..dd5af859d4 100644
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/HttpHeader.java
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/HttpHeader.java
@@ -43,7 +43,7 @@ public class HttpHeader {
*
* @param headerNames cannot be null or empty
*/
- public HttpHeader(String[] headerNames) {
+ public HttpHeader(String... headerNames) {
if (Objects.requireNonNull(headerNames, NAMES_CANNOT_BE_NULL).length < 1) {
throw new IllegalArgumentException(AT_LEAST_ONE_NAME_REQUIRED);
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingRequestFilter.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingRequestFilter.java
index f846359b82..fb651495ac 100644
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingRequestFilter.java
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingRequestFilter.java
@@ -27,7 +27,9 @@ import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
+import org.openecomp.sdc.logging.servlet.AuditTracker;
import org.openecomp.sdc.logging.servlet.CombinedTracker;
+import org.openecomp.sdc.logging.servlet.ContextTracker;
import org.openecomp.sdc.logging.servlet.HttpHeader;
import org.openecomp.sdc.logging.servlet.Tracker;
@@ -63,8 +65,8 @@ public class LoggingRequestFilter implements ContainerRequestFilter {
private HttpServletRequest httpRequest;
- private HttpHeader requestIdHeader = new HttpHeader(new String[] {DEFAULT_REQUEST_ID_HEADER});
- private HttpHeader partnerNameHeader = new HttpHeader(new String[] {DEFAULT_PARTNER_NAME_HEADER});
+ private HttpHeader requestIdHeader = new HttpHeader(DEFAULT_REQUEST_ID_HEADER);
+ private HttpHeader partnerNameHeader = new HttpHeader(DEFAULT_PARTNER_NAME_HEADER);
private ResourceInfo resource;
@@ -107,7 +109,9 @@ public class LoggingRequestFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) {
Class<?> resourceClass = resource.getResourceMethod().getDeclaringClass();
- Tracker tracker = new CombinedTracker(resourceClass, partnerNameHeader, requestIdHeader);
+ Tracker tracker = new CombinedTracker(
+ new ContextTracker(partnerNameHeader, requestIdHeader),
+ new AuditTracker(resourceClass));
requestContext.setProperty(LOGGING_TRACKER_KEY, tracker);
tracker.preRequest(httpRequest);
}
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/CombinedTrackerTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/CombinedTrackerTest.java
new file mode 100644
index 0000000000..4454493c01
--- /dev/null
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/CombinedTrackerTest.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright © 2016-2018 European Support Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openecomp.sdc.logging.servlet;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import javax.servlet.http.HttpServletRequest;
+import org.junit.Test;
+
+/**
+ * Test the construction and invocation of combined tracker.
+ *
+ * @author evitaliy
+ * @since 01 Aug 2018
+ */
+public class CombinedTrackerTest {
+
+ @Test(expected = NullPointerException.class)
+ public void throwExceptionWhenTrackersNull() {
+ new CombinedTracker((Tracker[]) null);
+ }
+
+ @Test
+ public void trackersCalledWhenPreRequest() {
+ Tracker firstTracker = mock(Tracker.class);
+ Tracker secondTracker = mock(Tracker.class);
+ CombinedTracker tracker = new CombinedTracker(firstTracker, secondTracker);
+ tracker.preRequest(mock(HttpServletRequest.class));
+ verify(firstTracker, times(1)).preRequest(any(HttpServletRequest.class));
+ verify(secondTracker, times(1)).preRequest(any(HttpServletRequest.class));
+ }
+
+ @Test
+ public void trackersCalledWhenPostRequest() {
+ Tracker firstTracker = mock(Tracker.class);
+ Tracker secondTracker = mock(Tracker.class);
+ CombinedTracker tracker = new CombinedTracker(firstTracker, secondTracker);
+ tracker.postRequest(mock(RequestProcessingResult.class));
+ verify(firstTracker, times(1)).postRequest(any(RequestProcessingResult.class));
+ verify(secondTracker, times(1)).postRequest(any(RequestProcessingResult.class));
+ }
+} \ No newline at end of file
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 a247d119c8..59c690d763 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
@@ -45,10 +45,10 @@ import org.powermock.modules.junit4.PowerMockRunner;
public class ContextTrackerTest {
private static final String X_REQUEST_ID = "X-REQUEST-ID";
- private static final HttpHeader REQUEST_ID_HEADER = new HttpHeader(new String[] {X_REQUEST_ID});
+ private static final HttpHeader REQUEST_ID_HEADER = new HttpHeader(X_REQUEST_ID);
private static final String X_PARTNER_NAME = "X-PARTNER-NAME";
- private static final HttpHeader PARTNER_NAME_HEADER = new HttpHeader(new String[] {X_PARTNER_NAME});
+ private static final HttpHeader PARTNER_NAME_HEADER = new HttpHeader(X_PARTNER_NAME);
@Test(expected = NullPointerException.class)
public void throwExceptionWhenPartnerNamesNull() {
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/HttpHeaderTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/HttpHeaderTest.java
index 5cbd0c0d76..2effa9fab1 100644
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/HttpHeaderTest.java
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/HttpHeaderTest.java
@@ -49,7 +49,7 @@ public class HttpHeaderTest {
@Test(expected = IllegalArgumentException.class)
public void throwExceptionWhenInputArrayEmpty() {
- new HttpHeader(new String[0]);
+ new HttpHeader();
}
@Test(expected = IllegalArgumentException.class)
@@ -59,7 +59,7 @@ public class HttpHeaderTest {
@Test
public void valueNotReturnedWhenNameInArrayNotRequested() {
- HttpHeader header = new HttpHeader(new String[] {"A"});
+ HttpHeader header = new HttpHeader("A");
assertFalse(header.getAny(NULL_WHEN_NAME_NOT_B).isPresent());
}
@@ -71,7 +71,7 @@ public class HttpHeaderTest {
@Test
public void valueReturnedWhenSinglePossibleHeaderInArrayMatches() {
- HttpHeader header = new HttpHeader(new String[] {"B"});
+ HttpHeader header = new HttpHeader("B");
assertTrue(header.getAny(NULL_WHEN_NAME_NOT_B).isPresent());
}
@@ -83,7 +83,7 @@ public class HttpHeaderTest {
@Test
public void valueReturnedWhenLastHeaderInArrayMatches() throws Throwable {
- HttpHeader header = new HttpHeader(new String[] {"A", "B"});
+ HttpHeader header = new HttpHeader("A", "B");
header.getAny(NULL_WHEN_NAME_NOT_B).orElseThrow(VALUE_EXPECTED);
}
@@ -95,7 +95,7 @@ public class HttpHeaderTest {
@Test
public void valueReturnedWhenFirstHeaderInArrayMatches() throws Throwable {
- HttpHeader header = new HttpHeader(new String[] {"B", "A"});
+ HttpHeader header = new HttpHeader("B", "A");
header.getAny(NULL_WHEN_NAME_NOT_B).orElseThrow(VALUE_EXPECTED);
}
@@ -107,7 +107,7 @@ public class HttpHeaderTest {
@Test
public void valueReturnedWhenMiddleHeaderInArrayMatches() throws Throwable {
- HttpHeader header = new HttpHeader(new String[] {"A", "B", "C"});
+ HttpHeader header = new HttpHeader("A", "B", "C");
header.getAny(NULL_WHEN_NAME_NOT_B).orElseThrow(VALUE_EXPECTED);
}