aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib
diff options
context:
space:
mode:
Diffstat (limited to 'openecomp-be/lib')
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/pom.xml16
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggerFactory.java3
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggingContext.java3
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/ServiceBinder.java9
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/AuditTracker.java85
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/CombinedTracker.java55
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/ContextTracker.java66
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/HttpHeader.java73
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/LoggingFilter.java136
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/RequestProcessingResult.java49
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/ServiceNameFormatter.java36
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/Tracker.java44
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingRequestFilter.java78
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingResponseFilter.java133
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/spi/LoggerCreationService.java1
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/spi/LoggingContextService.java3
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/AuditTrackerTest.java153
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ContextTrackerTest.java165
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/HttpHeaderTest.java119
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/LoggingFilterTest.java206
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ServiceNameFormatterTest.java43
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/jaxrs/HttpHeaderTest.java97
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingRequestFilterTest.java244
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingResponseFilterTest.java302
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-spring/pom.xml32
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/pom.xml1
26 files changed, 1017 insertions, 1135 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/pom.xml b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/pom.xml
index 29efa616ab..2bd26ed045 100644
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/pom.xml
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/pom.xml
@@ -34,26 +34,14 @@
<scope>test</scope>
</dependency>
<dependency>
- <groupId>org.easymock</groupId>
- <artifactId>easymock</artifactId>
- <version>${easymock.version}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.powermock</groupId>
- <artifactId>powermock-api-easymock</artifactId>
- <version>${powermock.version}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
<groupId>org.powermock</groupId>
- <artifactId>powermock-core</artifactId>
+ <artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
- <artifactId>powermock-module-junit4</artifactId>
+ <artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggerFactory.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggerFactory.java
index 30517cd5fb..96debb56b0 100644
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggerFactory.java
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggerFactory.java
@@ -16,9 +16,8 @@
package org.openecomp.sdc.logging.api;
-import org.openecomp.sdc.logging.spi.LoggerCreationService;
-
import java.util.Objects;
+import org.openecomp.sdc.logging.spi.LoggerCreationService;
/**
* <a>Factory to hide a concrete, framework-specific implementation of logger creation.</a>
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggingContext.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggingContext.java
index 9526713093..894dd2c00c 100644
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggingContext.java
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggingContext.java
@@ -16,10 +16,9 @@
package org.openecomp.sdc.logging.api;
-import org.openecomp.sdc.logging.spi.LoggingContextService;
-
import java.util.Objects;
import java.util.concurrent.Callable;
+import org.openecomp.sdc.logging.spi.LoggingContextService;
/**
* <p>Factory to hide a concrete, framework-specific implementation of diagnostic context.</p>
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/ServiceBinder.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/ServiceBinder.java
index 5aaa301a9c..6e5b3e3501 100644
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/ServiceBinder.java
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/ServiceBinder.java
@@ -16,13 +16,12 @@
package org.openecomp.sdc.logging.api;
-import org.openecomp.sdc.logging.spi.LoggerCreationService;
-import org.openecomp.sdc.logging.spi.LoggingContextService;
-import org.openecomp.sdc.logging.spi.LoggingServiceProvider;
-
import java.util.Iterator;
import java.util.Optional;
import java.util.ServiceLoader;
+import org.openecomp.sdc.logging.spi.LoggerCreationService;
+import org.openecomp.sdc.logging.spi.LoggingContextService;
+import org.openecomp.sdc.logging.spi.LoggingServiceProvider;
/**
* <p>Binds to a concrete implementation of logging services.</p>
@@ -37,7 +36,7 @@ import java.util.ServiceLoader;
// No advanced logging can be used here because we don't know
// which underlying implementation will be used
-@SuppressWarnings({"UseOfSystemOutOrSystemErr", "squid:S106"})
+@SuppressWarnings({"UseOfSystemOutOrSystemErr", "squid:S106", "squid:S1166"})
class ServiceBinder {
private static final LoggingServiceProvider PROVIDER = lookupProvider();
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/AuditTracker.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/AuditTracker.java
new file mode 100644
index 0000000000..af9b5061fd
--- /dev/null
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/AuditTracker.java
@@ -0,0 +1,85 @@
+/*
+ * 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 java.util.Objects;
+import javax.servlet.http.HttpServletRequest;
+import org.openecomp.sdc.logging.api.AuditData;
+import org.openecomp.sdc.logging.api.Logger;
+import org.openecomp.sdc.logging.api.LoggerFactory;
+
+/**
+ * Tracks and logs audit information when a request is being processed. An instance of this class cannot be reused, and
+ * the pre- and post-request methods must be called only once.
+ *
+ * @author evitaliy
+ * @since 31 Jul 2018
+ */
+public class AuditTracker implements Tracker {
+
+ private final Logger logger;
+ private volatile long started;
+ private volatile String clientIpAddress;
+
+ /**
+ * Allows passing a class that will be used to log audit.
+ *
+ * @param resourceType audit will be logged through the logger of this class
+ */
+ public AuditTracker(Class<?> resourceType) {
+ this.logger = LoggerFactory.getLogger(resourceType);
+ }
+
+ /**
+ * Allows passing a logger that will be used to log audit.
+ *
+ * @param logger audit will be logged through this logger, cannot be null
+ */
+ public AuditTracker(Logger logger) {
+ this.logger = Objects.requireNonNull(logger);
+ }
+
+ @Override
+ public synchronized void preRequest(HttpServletRequest request) {
+
+ if (this.started > 0) {
+ throw new IllegalStateException("Pre-request has been already called");
+ }
+
+ this.started = System.currentTimeMillis();
+ this.clientIpAddress = request.getRemoteAddr();
+ }
+
+ @Override
+ public synchronized void postRequest(RequestProcessingResult result) {
+
+ if (this.started == 0) {
+ throw new IllegalStateException("Pre-request must be called first");
+ }
+
+ if (!logger.isAuditEnabled()) {
+ return;
+ }
+
+ long end = System.currentTimeMillis();
+ AuditData auditData = AuditData.builder().startTime(started).endTime(end).statusCode(result.getStatusCode())
+ .responseCode(Integer.toString(result.getStatus()))
+ .responseDescription(result.getStatusPhrase()).clientIpAddress(clientIpAddress)
+ .build();
+ logger.audit(auditData);
+ }
+}
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
new file mode 100644
index 0000000000..2dd2c12143
--- /dev/null
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/CombinedTracker.java
@@ -0,0 +1,55 @@
+/*
+ * 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 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.
+ *
+ * @author evitaliy
+ * @since 01 Aug 2018
+ */
+public class CombinedTracker implements Tracker {
+
+ private final ContextTracker context;
+ private final AuditTracker audit;
+
+ 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);
+ }
+
+ @Override
+ public void preRequest(HttpServletRequest request) {
+ this.context.preRequest(request);
+ this.audit.preRequest(request);
+ }
+
+ @Override
+ public void postRequest(RequestProcessingResult result) {
+ this.audit.postRequest(result);
+ this.context.postRequest(result);
+ }
+}
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
new file mode 100644
index 0000000000..2334f373bc
--- /dev/null
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/ContextTracker.java
@@ -0,0 +1,66 @@
+/*
+ * 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 java.util.Objects;
+import java.util.UUID;
+import javax.servlet.http.HttpServletRequest;
+import org.openecomp.sdc.logging.api.ContextData;
+import org.openecomp.sdc.logging.api.LoggingContext;
+
+/**
+ * Populates the context before a request is processed, and cleans it after the request has been processed.
+ *
+ * @author evitaliy
+ * @since 31 Jul 2018
+ */
+public class ContextTracker implements Tracker {
+
+ private final HttpHeader partnerNameHeaders;
+ private final HttpHeader requestIdHeaders;
+
+ /**
+ * 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.
+ *
+ * @param partnerNameHeaders HTTP headers to check for a partner name, cannot be null
+ * @param requestIdHeaders HTTP headers to check for a request ID, cannot be null
+ */
+ public ContextTracker(HttpHeader partnerNameHeaders, HttpHeader requestIdHeaders) {
+ this.partnerNameHeaders = Objects.requireNonNull(partnerNameHeaders);
+ this.requestIdHeaders = Objects.requireNonNull(requestIdHeaders);
+ }
+
+ @Override
+ public void preRequest(HttpServletRequest request) {
+
+ LoggingContext.clear();
+
+ String serviceName = ServiceNameFormatter.format(request);
+ String requestId = requestIdHeaders.getAny(request::getHeader).orElse(UUID.randomUUID().toString());
+ ContextData.ContextDataBuilder contextBuilder =
+ ContextData.builder().serviceName(serviceName).requestId(requestId);
+
+ partnerNameHeaders.getAny(request::getHeader).ifPresent(contextBuilder::partnerName);
+ LoggingContext.put(contextBuilder.build());
+ }
+
+ @Override
+ public void postRequest(RequestProcessingResult result) {
+ LoggingContext.clear();
+ }
+}
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 4dcc197796..db10c2e4f3 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
@@ -16,6 +16,11 @@
package org.openecomp.sdc.logging.servlet;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
import java.util.function.Function;
/**
@@ -28,28 +33,80 @@ import java.util.function.Function;
*/
public class HttpHeader {
- private final String[] keys;
+ private static final String NAMES_CANNOT_BE_NULL = "Names cannot be null";
+ private static final String AT_LEAST_ONE_NAME_REQUIRED = "At least one name required";
- public HttpHeader(String... keys) {
- this.keys = keys;
+ private final List<String> headerNames;
+
+ /**
+ * Receives a list of accepted header names as a String array.
+ *
+ * @param headerNames cannot be null or empty
+ */
+ public HttpHeader(String[] headerNames) {
+
+ if (Objects.requireNonNull(headerNames, NAMES_CANNOT_BE_NULL).length < 1) {
+ throw new IllegalArgumentException(AT_LEAST_ONE_NAME_REQUIRED);
+ }
+
+ this.headerNames = Arrays.asList(headerNames);
+ }
+
+ /**
+ * Receives a list of accepted header names as a list of String.
+ *
+ * @param headerNames cannot be null or empty
+ */
+ public HttpHeader(List<String> headerNames) {
+
+ if (Objects.requireNonNull(headerNames, NAMES_CANNOT_BE_NULL).isEmpty()) {
+ throw new IllegalArgumentException(AT_LEAST_ONE_NAME_REQUIRED);
+ }
+
+ this.headerNames = new ArrayList<>(headerNames);
}
/**
* Returns the value of any of the possible headers.
*
* @param reader function for reading an HTTP header.
- * @return value or null if not found
+ * @return value or empty if not found
*/
- public String getAny(Function<String, String> reader) {
+ public Optional<String> getAny(Function<String, String> reader) {
- for (String k : keys) {
+ for (String k : headerNames) {
String value = reader.apply(k);
if (value != null) {
- return value;
+ return Optional.of(value);
}
}
- return null;
+ return Optional.empty();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+
+ if (this == o) {
+ return true;
+ }
+
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ HttpHeader that = (HttpHeader) o;
+ return Objects.equals(headerNames, that.headerNames);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(headerNames);
+ }
+
+ @Override
+ public String toString() {
+ return "HttpHeader{headerNames=" + headerNames + '}';
}
}
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/LoggingFilter.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/LoggingFilter.java
deleted file mode 100644
index e53f28119c..0000000000
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/LoggingFilter.java
+++ /dev/null
@@ -1,136 +0,0 @@
-/*
- * 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.openecomp.sdc.logging.LoggingConstants.DEFAULT_PARTNER_NAME_HEADER;
-import static org.openecomp.sdc.logging.LoggingConstants.DEFAULT_REQUEST_ID_HEADER;
-
-import java.io.IOException;
-import java.util.UUID;
-import javax.servlet.Filter;
-import javax.servlet.FilterChain;
-import javax.servlet.FilterConfig;
-import javax.servlet.ServletException;
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
-import javax.servlet.http.HttpServletRequest;
-import org.openecomp.sdc.logging.api.ContextData;
-import org.openecomp.sdc.logging.api.Logger;
-import org.openecomp.sdc.logging.api.LoggerFactory;
-import org.openecomp.sdc.logging.api.LoggingContext;
-import org.openecomp.sdc.logging.servlet.jaxrs.LoggingRequestFilter;
-import org.openecomp.sdc.logging.servlet.jaxrs.LoggingResponseFilter;
-
-/**
- * <p>Places logging context information. Must be configured as a servlet filter in <i>web.xml</i>. The behavior can be
- * customized via init-params.</p>
- * <p>Example:</p>
- * <pre>
- *
- * &lt;filter&gt;
- * &lt;filter-name&gt;LoggingServletFilter&lt;/filter-name&gt;
- * &lt;filter-class&gt;org.openecomp.sdc.logging.servlet.LoggingFilter&lt;/filter-class&gt;
- * &lt;init-param&gt;
- * &lt;param-name&gt;requestIdHeaders&lt;/param-name&gt;
- * &lt;param-value&gt;X-ONAP-RequestID&lt;/param-value&gt;
- * &lt;/init-param&gt;
- * &lt;init-param&gt;
- * &lt;param-name&gt;partnerNameHeaders&lt;/param-name&gt;
- * &lt;param-value&gt;USER_ID&lt;/param-value&gt;
- * &lt;/init-param&gt;
- * &lt;/filter&gt;
- *
- * &lt;filter-mapping&gt;
- * &lt;filter-name&gt;LoggingServletFilter&lt;/filter-name&gt;
- * &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
- * &lt;/filter-mapping&gt;
- *
- * </pre>
- *
- * @author evitaliy
- * @since 25 Jul 2016
- * @deprecated Kept for backward compatibility. For JAX-RS application, use
- * {@link LoggingRequestFilter} and
- * {@link LoggingResponseFilter} instead.
- */
-@Deprecated
-public class LoggingFilter implements Filter {
-
- static final String MULTI_VALUE_SEPARATOR = ",";
-
- static final String REQUEST_ID_HEADERS_PARAM = "requestIdHeaders";
- static final String PARTNER_NAME_HEADERS_PARAM = "partnerNameHeaders";
-
- private static final Logger LOGGER = LoggerFactory.getLogger(LoggingFilter.class);
-
- private HttpHeader requestIdHeaders;
- private HttpHeader partnerNameHeaders;
-
- @Override
- public void init(FilterConfig config) {
- requestIdHeaders = getInitParam(config, REQUEST_ID_HEADERS_PARAM, DEFAULT_REQUEST_ID_HEADER);
- partnerNameHeaders = getInitParam(config, PARTNER_NAME_HEADERS_PARAM, DEFAULT_PARTNER_NAME_HEADER);
- }
-
- @Override
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
- throws IOException, ServletException {
-
- HttpServletRequest httpRequest = HttpServletRequest.class.cast(request);
-
- try {
-
- LoggingContext.clear();
-
- ContextData.ContextDataBuilder contextData = ContextData.builder();
-
- contextData.serviceName(httpRequest.getRequestURI());
-
- String requestId = requestIdHeaders.getAny(httpRequest::getHeader);
- contextData.requestId(requestId == null ? UUID.randomUUID().toString() : requestId);
-
- String partner = partnerNameHeaders.getAny(httpRequest::getHeader);
- if (partner != null) {
- contextData.partnerName(partner);
- }
-
- LoggingContext.put(contextData.build());
-
- chain.doFilter(request, response);
-
- } finally {
- LoggingContext.clear();
- }
- }
-
- @Override
- public void destroy() {
- // forced by the interface - not implemented
- }
-
- private HttpHeader getInitParam(FilterConfig config, String paramName, String defaultValue) {
-
- String value = config.getInitParameter(paramName);
- LOGGER.debug("Logging filter configuration param '{}' value '{}'", paramName, value);
-
- if (value == null) {
- return new HttpHeader(defaultValue);
- } else {
- return new HttpHeader(value.split(MULTI_VALUE_SEPARATOR));
- }
- }
-}
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/RequestProcessingResult.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/RequestProcessingResult.java
new file mode 100644
index 0000000000..d3625ed303
--- /dev/null
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/RequestProcessingResult.java
@@ -0,0 +1,49 @@
+/*
+ * 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 org.openecomp.sdc.logging.api.StatusCode;
+
+/**
+ * Interpretation of request processing results.
+ *
+ * @author evitaliy
+ * @since 02 Aug 2018
+ */
+public interface RequestProcessingResult {
+
+ /**
+ * Numeric status code.
+ *
+ * @return usually HTTP response status.
+ */
+ int getStatus();
+
+ /**
+ * Whether the response is considered success or failure.
+ *
+ * @return on of pre-defined status codes
+ */
+ StatusCode getStatusCode();
+
+ /**
+ * Human-friendly description of the numeric status.
+ *
+ * @return usually HTTP reason phrase
+ */
+ String getStatusPhrase();
+}
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/ServiceNameFormatter.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/ServiceNameFormatter.java
new file mode 100644
index 0000000000..f65490bf18
--- /dev/null
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/ServiceNameFormatter.java
@@ -0,0 +1,36 @@
+/*
+ * 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 javax.servlet.http.HttpServletRequest;
+
+/**
+ * Formats a service name based on HTTP method and request URI.
+ *
+ * @author evitaliy
+ * @since 01 Aug 2018
+ */
+class ServiceNameFormatter {
+
+ private ServiceNameFormatter() {
+ // utility class, prevent instantiation
+ }
+
+ public static String format(HttpServletRequest request) {
+ return request.getMethod() + ": " + request.getRequestURI();
+ }
+} \ No newline at end of file
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/Tracker.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/Tracker.java
new file mode 100644
index 0000000000..c0337cfb4d
--- /dev/null
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/Tracker.java
@@ -0,0 +1,44 @@
+/*
+ * 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 javax.servlet.http.HttpServletRequest;
+
+/**
+ * Tracks logging and tracing information through the processing of an HTTP request.
+ *
+ * @author evitaliy
+ * @since 31 Jul 2018
+ */
+public interface Tracker {
+
+ /**
+ * Will be executed before request processing has started.
+ *
+ * @param request provided by every Servlet container
+ */
+ void preRequest(HttpServletRequest request);
+
+ /**
+ * Will be executed after a request has been processed. Results may be treated differently depending on a container
+ * and application. For instance, JAX-RS applications may take into account exception mappers before generating a
+ * response; some applications may Swagger annotations to map a response status to a human-friendly message, etc.
+ *
+ * @param result application- and container-specific request results
+ */
+ void postRequest(RequestProcessingResult result);
+}
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 0e1b7d715b..f846359b82 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
@@ -19,25 +19,24 @@ package org.openecomp.sdc.logging.servlet.jaxrs;
import static org.openecomp.sdc.logging.LoggingConstants.DEFAULT_PARTNER_NAME_HEADER;
import static org.openecomp.sdc.logging.LoggingConstants.DEFAULT_REQUEST_ID_HEADER;
-import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
+import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;
-import org.openecomp.sdc.logging.api.ContextData;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
-import org.openecomp.sdc.logging.api.LoggingContext;
+import org.openecomp.sdc.logging.servlet.CombinedTracker;
import org.openecomp.sdc.logging.servlet.HttpHeader;
+import org.openecomp.sdc.logging.servlet.Tracker;
/**
* <p>Takes care of logging initialization an HTTP request hits the application. This includes populating logging
- * context and storing the request processing start time, so that it can be used for audit. The filter was built
- * <b>works in tandem</b> with {@link LoggingResponseFilter} or a similar implementation.</p>
+ * context and tracking the request for audit. The filter <b>works in tandem</b> with {@link LoggingResponseFilter} or
+ * a similar implementation.</p>
* <p>The filter requires a few HTTP header names to be configured. These HTTP headers are used for propagating logging
- * and tracing information between ONAP components.</p>
- * <p>Sample configuration for a Spring environment:</p>
+ * and tracing information between ONAP components. Sample configuration for a Spring environment:</p>
* <pre>
* &lt;jaxrs:providers&gt;
* &lt;bean class="org.openecomp.sdc.logging.ws.rs.LoggingRequestFilter"&gt;
@@ -51,40 +50,42 @@ import org.openecomp.sdc.logging.servlet.HttpHeader;
* </p>
*
* @author evitaliy, katyr
- * @since 29 Oct 17
- *
* @see ContainerRequestFilter
+ * @since 29 Oct 17
*/
@Provider
public class LoggingRequestFilter implements ContainerRequestFilter {
- static final String MULTI_VALUE_SEPARATOR = ",";
-
- static final String START_TIME_KEY = "audit.start.time";
+ static final String LOGGING_TRACKER_KEY = "onap.logging.tracker";
+ private static final String MULTI_VALUE_SEPARATOR = ",";
private static final Logger LOGGER = LoggerFactory.getLogger(LoggingRequestFilter.class);
private HttpServletRequest httpRequest;
- private HttpHeader requestIdHeader = new HttpHeader(DEFAULT_REQUEST_ID_HEADER);
- private HttpHeader partnerNameHeader = new HttpHeader(DEFAULT_PARTNER_NAME_HEADER);
- private boolean includeHttpMethod = true;
+ private HttpHeader requestIdHeader = new HttpHeader(new String[] {DEFAULT_REQUEST_ID_HEADER});
+ private HttpHeader partnerNameHeader = new HttpHeader(new String[] {DEFAULT_PARTNER_NAME_HEADER});
+
+ private ResourceInfo resource;
/**
- * Injection of HTTP request object from JAX-RS context.
+ * Injection of a resource that matches the request from JAX-RS context.
*
- * @param httpRequest automatically injected by JAX-RS container
+ * @param resource automatically injected by JAX-RS container
*/
@Context
- public void setHttpRequest(HttpServletRequest httpRequest) {
- this.httpRequest = httpRequest;
+ public void setResource(ResourceInfo resource) {
+ this.resource = resource;
}
/**
- * Configuration parameter to include the HTTP method of a request in service name.
+ * Injection of HTTP request object from JAX-RS context.
+ *
+ * @param httpRequest automatically injected by JAX-RS container
*/
- public void setHttpMethodInServiceName(boolean includeHttpMethod) {
- this.includeHttpMethod = includeHttpMethod;
+ @Context
+ public void setHttpRequest(HttpServletRequest httpRequest) {
+ this.httpRequest = httpRequest;
}
/**
@@ -104,33 +105,10 @@ public class LoggingRequestFilter implements ContainerRequestFilter {
}
@Override
- public void filter(ContainerRequestContext containerRequestContext) {
-
- LoggingContext.clear();
-
- containerRequestContext.setProperty(START_TIME_KEY, System.currentTimeMillis());
-
- ContextData.ContextDataBuilder contextData = ContextData.builder();
- contextData.serviceName(getServiceName());
-
- String partnerName = partnerNameHeader.getAny(containerRequestContext::getHeaderString);
- if (partnerName != null) {
- contextData.partnerName(partnerName);
- }
-
- String requestId = requestIdHeader.getAny(containerRequestContext::getHeaderString);
- contextData.requestId(requestId == null ? UUID.randomUUID().toString() : requestId);
-
- LoggingContext.put(contextData.build());
- }
-
- private String getServiceName() {
- return includeHttpMethod
- ? formatServiceName(this.httpRequest.getMethod(), this.httpRequest.getRequestURI())
- : this.httpRequest.getRequestURI();
- }
-
- static String formatServiceName(String httpMethod, String requestUri) {
- return httpMethod + " " + requestUri;
+ public void filter(ContainerRequestContext requestContext) {
+ Class<?> resourceClass = resource.getResourceMethod().getDeclaringClass();
+ Tracker tracker = new CombinedTracker(resourceClass, partnerNameHeader, requestIdHeader);
+ requestContext.setProperty(LOGGING_TRACKER_KEY, tracker);
+ tracker.preRequest(httpRequest);
}
}
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingResponseFilter.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingResponseFilter.java
index 4609620be1..fa9e815db8 100644
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingResponseFilter.java
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingResponseFilter.java
@@ -16,19 +16,22 @@
package org.openecomp.sdc.logging.servlet.jaxrs;
-import org.openecomp.sdc.logging.api.*;
+import static javax.ws.rs.core.Response.Status.Family.REDIRECTION;
+import static javax.ws.rs.core.Response.Status.Family.SUCCESSFUL;
+import static org.openecomp.sdc.logging.api.StatusCode.COMPLETE;
+import static org.openecomp.sdc.logging.api.StatusCode.ERROR;
+import static org.openecomp.sdc.logging.servlet.jaxrs.LoggingRequestFilter.LOGGING_TRACKER_KEY;
-import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
-import javax.ws.rs.container.ResourceInfo;
-import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
-
-import static org.openecomp.sdc.logging.api.StatusCode.COMPLETE;
-import static org.openecomp.sdc.logging.api.StatusCode.ERROR;
+import org.openecomp.sdc.logging.api.Logger;
+import org.openecomp.sdc.logging.api.LoggerFactory;
+import org.openecomp.sdc.logging.api.StatusCode;
+import org.openecomp.sdc.logging.servlet.RequestProcessingResult;
+import org.openecomp.sdc.logging.servlet.Tracker;
/**
* <p>Takes care of logging when an HTTP request leaves the application. This includes writing to audit and clearing
@@ -44,123 +47,49 @@ import static org.openecomp.sdc.logging.api.StatusCode.ERROR;
* due to unhandled application or container exceptions.</i></p>
*
* @author evitaliy
- * @since 29 Oct 17
- *
* @see ContainerResponseFilter
+ * @since 29 Oct 17
*/
@Provider
public class LoggingResponseFilter implements ContainerResponseFilter {
- private static final int UNKNOWN_START_TIME = 0;
-
private static final Logger LOGGER = LoggerFactory.getLogger(LoggingResponseFilter.class);
- /**
- * Tracks reporting configuration problems to the log. We want to report them only once, and not to write to log
- * upon every request, as the configuration will not change in runtime.
- */
- private boolean reportBadConfiguration = true;
-
- private HttpServletRequest httpRequest;
-
- private ResourceInfo resource;
-
- /**
- * Injection of HTTP request object from JAX-RS context.
- *
- * @param httpRequest automatically injected by JAX-RS container
- */
- @Context
- public void setHttpRequest(HttpServletRequest httpRequest) {
- this.httpRequest = httpRequest;
- }
-
- /**
- * Injection of a resource that matches the request from JAX-RS context.
- *
- * @param resource automatically injected by JAX-RS container
- */
- @Context
- public void setResource(ResourceInfo resource) {
- this.resource = resource;
- }
-
@Override
- public void filter(ContainerRequestContext containerRequestContext,
- ContainerResponseContext containerResponseContext) {
+ public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
- try {
+ Tracker tracker = (Tracker) requestContext.getProperty(LOGGING_TRACKER_KEY);
- if ((resource == null) || (resource.getResourceClass() == null)) {
- LOGGER.debug("No matching resource, skipping audit.");
- return;
- }
-
- writeAudit(containerRequestContext, containerResponseContext);
-
- } finally {
- LoggingContext.clear();
- }
- }
-
- private void writeAudit(ContainerRequestContext containerRequestContext,
- ContainerResponseContext containerResponseContext) {
-
- Logger resourceLogger = LoggerFactory.getLogger(resource.getResourceMethod().getDeclaringClass());
- if (!resourceLogger.isAuditEnabled()) {
+ if (tracker == null) {
+ LOGGER.debug("No logging tracker received");
return;
}
- long start = readStartTime(containerRequestContext);
- long end = System.currentTimeMillis();
-
- Response.StatusType statusInfo = containerResponseContext.getStatusInfo();
- int responseCode = statusInfo.getStatusCode();
- StatusCode statusCode = isSuccess(responseCode) ? COMPLETE : ERROR;
-
- AuditData auditData = AuditData.builder().startTime(start).endTime(end).statusCode(statusCode)
- .responseCode(Integer.toString(responseCode))
- .responseDescription(statusInfo.getReasonPhrase())
- .clientIpAddress(httpRequest.getRemoteAddr()).build();
- resourceLogger.audit(auditData);
+ tracker.postRequest(new ContainerResponseResult(responseContext.getStatusInfo()));
}
- private boolean isSuccess(int responseCode) {
- return responseCode > 199 && responseCode < 400;
- }
+ private static class ContainerResponseResult implements RequestProcessingResult {
- private long readStartTime(ContainerRequestContext containerRequestContext) {
+ private final Response.StatusType statusInfo;
- Object startTime = containerRequestContext.getProperty(LoggingRequestFilter.START_TIME_KEY);
- if (startTime == null) {
- return handleMissingStartTime();
+ private ContainerResponseResult(Response.StatusType statusInfo) {
+ this.statusInfo = statusInfo;
}
- return parseStartTime(startTime);
- }
-
- private long handleMissingStartTime() {
- reportConfigProblem("{} key was not found in JAX-RS request context. "
- + "Make sure you configured a request filter", LoggingRequestFilter.START_TIME_KEY);
- return UNKNOWN_START_TIME;
- }
-
- private long parseStartTime(Object startTime) {
-
- try {
- return Long.class.cast(startTime);
- } catch (ClassCastException e) {
- reportConfigProblem("{} key in JAX-RS request context contains an object of type '{}', but 'java.lang.Long'"
- + " is expected", LoggingRequestFilter.START_TIME_KEY, startTime.getClass().getName(), e);
- return 0;
+ @Override
+ public int getStatus() {
+ return statusInfo.getStatusCode();
}
- }
- private void reportConfigProblem(String message, Object... arguments) {
+ @Override
+ public StatusCode getStatusCode() {
+ Response.Status.Family family = statusInfo.getFamily();
+ return family.equals(SUCCESSFUL) || family.equals(REDIRECTION) ? COMPLETE : ERROR;
+ }
- if (reportBadConfiguration) {
- reportBadConfiguration = false;
- LOGGER.error(message, arguments);
+ @Override
+ public String getStatusPhrase() {
+ return statusInfo.getReasonPhrase();
}
}
}
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/spi/LoggerCreationService.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/spi/LoggerCreationService.java
index 90aebfa221..54b5be3a27 100644
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/spi/LoggerCreationService.java
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/spi/LoggerCreationService.java
@@ -20,7 +20,6 @@ import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
/**
- *
* Implements a framework-specific logging, to be used by {@link LoggerFactory}.
*
* @author evitaliy
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/spi/LoggingContextService.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/spi/LoggingContextService.java
index 7c71465846..37e212ac54 100644
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/spi/LoggingContextService.java
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/spi/LoggingContextService.java
@@ -16,9 +16,8 @@
package org.openecomp.sdc.logging.spi;
-import org.openecomp.sdc.logging.api.ContextData;
-
import java.util.concurrent.Callable;
+import org.openecomp.sdc.logging.api.ContextData;
/**
* Should be used to implement a framework-specific mechanism of managing a per-thread diagnostic context (for instance
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/AuditTrackerTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/AuditTrackerTest.java
new file mode 100644
index 0000000000..1bd13a9dfd
--- /dev/null
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/AuditTrackerTest.java
@@ -0,0 +1,153 @@
+/*
+ * 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 junit.framework.TestCase.assertEquals;
+import static junit.framework.TestCase.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import javax.servlet.http.HttpServletRequest;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.openecomp.sdc.logging.api.AuditData;
+import org.openecomp.sdc.logging.api.Logger;
+import org.openecomp.sdc.logging.api.StatusCode;
+
+/**
+ * Test initialization and of audit tracker and log invocation.
+ *
+ * @author evitaliy
+ * @since 01 Aug 2018
+ */
+public class AuditTrackerTest {
+
+ @Test(expected = NullPointerException.class)
+ public void throwExceptionWhenLoggerNull() {
+ new AuditTracker((Logger) null);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void throwExceptionWhenResourceTypeNull() {
+ new AuditTracker((Class<?>) null);
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void throwExceptionWhenPreRequestCalledMoreThanOnce() {
+ AuditTracker tracker = new AuditTracker(mock(Logger.class));
+ HttpServletRequest request = mock(HttpServletRequest.class);
+ tracker.preRequest(request);
+ tracker.preRequest(request);
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void throwExceptionWhenRreRequestNeverCalled() {
+ AuditTracker tracker = new AuditTracker(mock(Logger.class));
+ tracker.postRequest(mock(RequestProcessingResult.class));
+ }
+
+ @Test
+ public void nothingHappensWhenAuditDisabled() {
+
+ Logger logger = mock(Logger.class);
+ when(logger.isAuditEnabled()).thenReturn(false);
+
+ AuditTracker tracker = new AuditTracker(logger);
+ tracker.preRequest(mock(HttpServletRequest.class));
+ tracker.postRequest(mock(RequestProcessingResult.class));
+
+ verify(logger, never()).audit(any(AuditData.class));
+ }
+
+ @Test
+ public void reportedTimePositive() {
+
+ Logger logger = mock(Logger.class);
+ when(logger.isAuditEnabled()).thenReturn(true);
+
+ AuditTracker tracker = new AuditTracker(logger);
+ tracker.preRequest(mock(HttpServletRequest.class));
+ tracker.postRequest(mock(RequestProcessingResult.class));
+
+ ArgumentCaptor<AuditData> auditDataCaptor = ArgumentCaptor.forClass(AuditData.class);
+ verify(logger).audit(auditDataCaptor.capture());
+ assertTrue(auditDataCaptor.getValue().getEndTime() > 0);
+ }
+
+ @Test
+ public void interpretedRequestResultsPassedDownToAudit() {
+
+ Logger logger = mock(Logger.class);
+ when(logger.isAuditEnabled()).thenReturn(true);
+
+ AuditTracker tracker = new AuditTracker(logger);
+ tracker.preRequest(mock(HttpServletRequest.class));
+
+ final StubRequestProcessingResult result = new StubRequestProcessingResult();
+ tracker.postRequest(result);
+
+ ArgumentCaptor<AuditData> auditDataCaptor = ArgumentCaptor.forClass(AuditData.class);
+ verify(logger).audit(auditDataCaptor.capture());
+
+ AuditData capturedAuditData = auditDataCaptor.getValue();
+ assertEquals(Integer.toString(result.getStatus()), capturedAuditData.getResponseCode());
+ assertEquals(result.getStatusCode(), capturedAuditData.getStatusCode());
+ assertEquals(result.getStatusPhrase(), capturedAuditData.getResponseDescription());
+ }
+
+ @Test
+ public void clientIpAddressPassedDownToAudit() {
+
+ Logger logger = mock(Logger.class);
+ when(logger.isAuditEnabled()).thenReturn(true);
+
+ final String address = "196.50.30.122";
+ HttpServletRequest request = mock(HttpServletRequest.class);
+ when(request.getRemoteAddr()).thenReturn(address);
+
+ AuditTracker tracker = new AuditTracker(logger);
+ tracker.preRequest(request);
+ tracker.postRequest(mock(RequestProcessingResult.class));
+
+ ArgumentCaptor<AuditData> auditDataCaptor = ArgumentCaptor.forClass(AuditData.class);
+ verify(logger).audit(auditDataCaptor.capture());
+
+ AuditData capturedAuditData = auditDataCaptor.getValue();
+ assertEquals(address, capturedAuditData.getClientIpAddress());
+ }
+
+ private static class StubRequestProcessingResult implements RequestProcessingResult {
+
+ @Override
+ public int getStatus() {
+ return 505;
+ }
+
+ @Override
+ public StatusCode getStatusCode() {
+ return StatusCode.ERROR;
+ }
+
+ @Override
+ public String getStatusPhrase() {
+ return "Test request failed";
+ }
+ }
+} \ 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
new file mode 100644
index 0000000000..a247d119c8
--- /dev/null
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ContextTrackerTest.java
@@ -0,0 +1,165 @@
+/*
+ * 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.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 javax.servlet.http.HttpServletRequest;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+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.
+ *
+ * @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";
+ private static final HttpHeader REQUEST_ID_HEADER = new HttpHeader(new String[] {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});
+
+ @Test(expected = NullPointerException.class)
+ public void throwExceptionWhenPartnerNamesNull() {
+ new ContextTracker(null, REQUEST_ID_HEADER);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void throwExceptionWhenRequestIdsNull() {
+ 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());
+ }
+
+ @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();
+ assertNotNull(requestId);
+ assertFalse(requestId.isEmpty());
+ }
+
+ @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());
+ }
+
+ @Test
+ public void partnerNameAbsentWhenNotGiven() {
+
+ 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());
+
+ assertNull(contextDataCaptor.getValue().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);
+
+ LoggingContext.put(contextDataCaptor.capture());
+
+ assertNotNull(contextDataCaptor.getValue().getServiceName());
+ }
+
+ @Test
+ public void contextClearedWhenRequestFinished() {
+
+ mockStatic(LoggingContext.class);
+
+ ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER);
+ tracker.postRequest(mock(RequestProcessingResult.class));
+
+ verifyStatic(LoggingContext.class);
+ LoggingContext.clear();
+ }
+}
+
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
new file mode 100644
index 0000000000..5cbd0c0d76
--- /dev/null
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/HttpHeaderTest.java
@@ -0,0 +1,119 @@
+/*
+ * 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 junit.framework.TestCase.assertTrue;
+import static org.junit.Assert.assertFalse;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import org.junit.Test;
+
+/**
+ * Unit tests multiple-option headers.
+ *
+ * @author evitaliy
+ * @since 31 Jul 2018
+ */
+public class HttpHeaderTest {
+
+ private static final Supplier<? extends Throwable> VALUE_EXPECTED = () -> new AssertionError("Value expected");
+ private static final Function<String, String> NULL_WHEN_NAME_NOT_B = k -> "B".equals(k) ? "Value" : null;
+
+ @Test(expected = NullPointerException.class)
+ public void throwExceptionWhenInputArrayNull() {
+ new HttpHeader((String[]) null);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void throwExceptionWhenInputListNull() {
+ new HttpHeader((List<String>) null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void throwExceptionWhenInputArrayEmpty() {
+ new HttpHeader(new String[0]);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void throwExceptionWhenInputListEmpty() {
+ new HttpHeader(Collections.emptyList());
+ }
+
+ @Test
+ public void valueNotReturnedWhenNameInArrayNotRequested() {
+ HttpHeader header = new HttpHeader(new String[] {"A"});
+ assertFalse(header.getAny(NULL_WHEN_NAME_NOT_B).isPresent());
+ }
+
+ @Test
+ public void valueNotReturnedWhenNameInListNotRequested() {
+ HttpHeader header = new HttpHeader(Collections.singletonList("A"));
+ assertFalse(header.getAny(NULL_WHEN_NAME_NOT_B).isPresent());
+ }
+
+ @Test
+ public void valueReturnedWhenSinglePossibleHeaderInArrayMatches() {
+ HttpHeader header = new HttpHeader(new String[] {"B"});
+ assertTrue(header.getAny(NULL_WHEN_NAME_NOT_B).isPresent());
+ }
+
+ @Test
+ public void valueReturnedWhenSinglePossibleHeaderInListMatches() {
+ HttpHeader header = new HttpHeader(Collections.singletonList("B"));
+ assertTrue(header.getAny(NULL_WHEN_NAME_NOT_B).isPresent());
+ }
+
+ @Test
+ public void valueReturnedWhenLastHeaderInArrayMatches() throws Throwable {
+ HttpHeader header = new HttpHeader(new String[] {"A", "B"});
+ header.getAny(NULL_WHEN_NAME_NOT_B).orElseThrow(VALUE_EXPECTED);
+ }
+
+ @Test
+ public void valueReturnedWhenLastHeaderInListMatches() throws Throwable {
+ HttpHeader header = new HttpHeader(Arrays.asList("A", "B"));
+ header.getAny(NULL_WHEN_NAME_NOT_B).orElseThrow(VALUE_EXPECTED);
+ }
+
+ @Test
+ public void valueReturnedWhenFirstHeaderInArrayMatches() throws Throwable {
+ HttpHeader header = new HttpHeader(new String[] {"B", "A"});
+ header.getAny(NULL_WHEN_NAME_NOT_B).orElseThrow(VALUE_EXPECTED);
+ }
+
+ @Test
+ public void valueReturnedWhenFirstHeaderInListMatches() throws Throwable {
+ HttpHeader header = new HttpHeader(Arrays.asList("B", "A"));
+ header.getAny(NULL_WHEN_NAME_NOT_B).orElseThrow(VALUE_EXPECTED);
+ }
+
+ @Test
+ public void valueReturnedWhenMiddleHeaderInArrayMatches() throws Throwable {
+ HttpHeader header = new HttpHeader(new String[] {"A", "B", "C"});
+ header.getAny(NULL_WHEN_NAME_NOT_B).orElseThrow(VALUE_EXPECTED);
+ }
+
+ @Test
+ public void valueReturnedWhenMiddleHeaderInListMatches() throws Throwable {
+ HttpHeader header = new HttpHeader(Arrays.asList("A", "B", "C"));
+ header.getAny(NULL_WHEN_NAME_NOT_B).orElseThrow(VALUE_EXPECTED);
+ }
+} \ 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/LoggingFilterTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/LoggingFilterTest.java
deleted file mode 100644
index 1ba418f317..0000000000
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/LoggingFilterTest.java
+++ /dev/null
@@ -1,206 +0,0 @@
-/*
- * 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 org.easymock.EasyMock;
-import org.junit.After;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TestName;
-import org.junit.runner.RunWith;
-import org.openecomp.sdc.logging.api.ContextData;
-import org.openecomp.sdc.logging.api.LoggingContext;
-import org.powermock.api.easymock.PowerMock;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
-
-import javax.servlet.FilterChain;
-import javax.servlet.FilterConfig;
-import javax.servlet.ServletException;
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-import java.util.UUID;
-
-import static org.easymock.EasyMock.anyObject;
-import static org.openecomp.sdc.logging.LoggingConstants.DEFAULT_PARTNER_NAME_HEADER;
-import static org.openecomp.sdc.logging.LoggingConstants.DEFAULT_REQUEST_ID_HEADER;
-import static org.openecomp.sdc.logging.servlet.LoggingFilter.PARTNER_NAME_HEADERS_PARAM;
-import static org.openecomp.sdc.logging.servlet.LoggingFilter.REQUEST_ID_HEADERS_PARAM;
-
-/**
- * Unit-tests logging filter for initialization and data retrieval.
- *
- * @author evitaliy
- * @since 17 Aug 2016
- */
-@RunWith(PowerMockRunner.class)
-@PrepareForTest(LoggingContext.class)
-public class LoggingFilterTest {
-
- private static final String RANDOM_REQUEST_URI = UUID.randomUUID().toString();
- private static final String RANDOM_REQUEST_ID = UUID.randomUUID().toString();
- private static final String RANDOM_PARTNER_NAME = UUID.randomUUID().toString();
-
- @Rule
- public TestName testName = new TestName();
-
- /**
- * Verify all mocks after each test.
- */
- @After
- public void verifyMocks() {
-
- try {
- PowerMock.verifyAll();
- } catch (AssertionError e) {
- throw new AssertionError("Expectations failed in " + testName.getMethodName() + "()", e);
- }
- }
-
-
- @Test
- public void filterPopulatesValuesWhenNoInitParamsAndNoHeaders() throws IOException, ServletException {
-
- mockLoggingContext();
- LoggingFilter loggingFilter = new LoggingFilter();
- loggingFilter.init(mockFilterConfig(null, null));
- loggingFilter.doFilter(new MockRequestBuilder().build(), mockResponse(), mockChain());
- }
-
- @Test
- public void filterPopulatesValuesWhenNoInitParamsAndExistingHeaders() throws IOException, ServletException {
-
- mockLoggingContext();
-
- LoggingFilter loggingFilter = new LoggingFilter();
- loggingFilter.init(mockFilterConfig(null, null));
-
- HttpServletRequest mockRequest = new MockRequestBuilder().partnerName(RANDOM_PARTNER_NAME)
- .requestId(RANDOM_REQUEST_ID).build();
- loggingFilter.doFilter(mockRequest, mockResponse(), mockChain());
- }
-
- @Test
- public void filterPopulatesValuesWhenCustomInitParamsAndNoHeaders() throws IOException, ServletException {
-
- mockLoggingContext();
-
- final String requestIdHeader = "x-request";
- final String partnerNameHeader = "x-partner";
-
- LoggingFilter loggingFilter = new LoggingFilter();
- FilterConfig mockConfig = mockFilterConfig(requestIdHeader, partnerNameHeader);
- loggingFilter.init(mockConfig);
-
- HttpServletRequest mockRequest = new MockRequestBuilder().requestIdHeader(requestIdHeader)
- .partnerNameHeader(partnerNameHeader).build();
- loggingFilter.doFilter(mockRequest, mockResponse(), mockChain());
- }
-
- @Test
- public void filterPopulatesValuesWhenCustomInitParamsAndExistingHeaders() throws IOException, ServletException {
-
- mockLoggingContext();
-
- final String requestIdHeader = "x-request-id";
- final String partnerNameHeader = "x-partner-name";
-
- LoggingFilter loggingFilter = new LoggingFilter();
- FilterConfig mockConfig = mockFilterConfig(requestIdHeader, partnerNameHeader);
- loggingFilter.init(mockConfig);
-
- HttpServletRequest mockRequest = new MockRequestBuilder()
- .partnerNameHeader(partnerNameHeader).partnerName(RANDOM_PARTNER_NAME)
- .requestIdHeader(requestIdHeader).requestId(RANDOM_REQUEST_ID).build();
- loggingFilter.doFilter(mockRequest, mockResponse(), mockChain());
- }
-
- private FilterConfig mockFilterConfig(String requestIdHeader, String partnerNameHeader) {
- FilterConfig config = EasyMock.mock(FilterConfig.class);
- EasyMock.expect(config.getInitParameter(REQUEST_ID_HEADERS_PARAM)).andReturn(requestIdHeader);
- EasyMock.expect(config.getInitParameter(PARTNER_NAME_HEADERS_PARAM)).andReturn(partnerNameHeader);
- EasyMock.replay(config);
- return config;
- }
-
- private FilterChain mockChain() throws IOException, ServletException {
- FilterChain chain = EasyMock.mock(FilterChain.class);
- chain.doFilter(anyObject(ServletRequest.class), anyObject(ServletResponse.class));
- EasyMock.expectLastCall().once();
- EasyMock.replay(chain);
- return chain;
- }
-
- private ServletResponse mockResponse() {
- HttpServletResponse servletResponse = EasyMock.mock(HttpServletResponse.class);
- EasyMock.replay(servletResponse);
- return servletResponse;
- }
-
- private void mockLoggingContext() {
-
- PowerMock.mockStatic(LoggingContext.class);
-
- LoggingContext.clear();
- EasyMock.expectLastCall().times(2);
-
- LoggingContext.put(anyObject(ContextData.class));
- EasyMock.expectLastCall().once();
-
- PowerMock.replay(LoggingContext.class);
- }
-
- private static class MockRequestBuilder {
-
- private String requestIdHeader = DEFAULT_REQUEST_ID_HEADER;
- private String partnerNameHeader = DEFAULT_PARTNER_NAME_HEADER;
- private String requestId = null;
- private String partnerName = null;
-
- MockRequestBuilder requestIdHeader(String h) {
- this.requestIdHeader = h;
- return this;
- }
-
- MockRequestBuilder requestId(String id) {
- this.requestId = id;
- return this;
- }
-
- MockRequestBuilder partnerNameHeader(String h) {
- this.partnerNameHeader = h;
- return this;
- }
-
- MockRequestBuilder partnerName(String name) {
- this.partnerName = name;
- return this;
- }
-
- HttpServletRequest build() {
- HttpServletRequest mockRequest = EasyMock.mock(HttpServletRequest.class);
- EasyMock.expect(mockRequest.getRequestURI()).andReturn(RANDOM_REQUEST_URI);
- EasyMock.expect(mockRequest.getHeader(requestIdHeader)).andReturn(requestId);
- EasyMock.expect(mockRequest.getHeader(partnerNameHeader)).andReturn(partnerName);
- EasyMock.replay(mockRequest);
- return mockRequest;
- }
- }
-}
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ServiceNameFormatterTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ServiceNameFormatterTest.java
new file mode 100644
index 0000000000..87fb6f789c
--- /dev/null
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ServiceNameFormatterTest.java
@@ -0,0 +1,43 @@
+/*
+ * 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.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import javax.servlet.http.HttpServletRequest;
+import org.junit.Test;
+
+/**
+ * Service name formatting.
+ *
+ * @author evitaliy
+ * @since 01 Aug 2018
+ */
+public class ServiceNameFormatterTest {
+
+ @Test
+ public void includesMethodAndUri() {
+ final String method = "ABC";
+ final String uri = "/a/b/c";
+ HttpServletRequest request = mock(HttpServletRequest.class);
+ when(request.getRequestURI()).thenReturn(uri);
+ when(request.getMethod()).thenReturn(method);
+ assertEquals(method + ": " + uri, ServiceNameFormatter.format(request));
+ }
+} \ 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/jaxrs/HttpHeaderTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/jaxrs/HttpHeaderTest.java
deleted file mode 100644
index ad7be95cec..0000000000
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/jaxrs/HttpHeaderTest.java
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * 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.jaxrs;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-
-import java.util.function.Function;
-import org.junit.Test;
-import org.openecomp.sdc.logging.servlet.HttpHeader;
-
-/**
- * Unit tests multiple-option headers.
- *
- * @author evitaliy
- * @since 25 Mar 2018
- */
-public class HttpHeaderTest {
-
- private static final String KEY_FIRST = "First";
- private static final String KEY_SECOND = "Second";
-
- @Test
- public void valueReturnedWhenSinglePossibleHeader() {
-
- final String key = "Head";
- final String value = "1234";
-
- Function<String, String> reader = createReader(key, value);
- HttpHeader header = new HttpHeader(key);
- assertEquals(value, header.getAny(reader));
- }
-
- @Test
- public void nullReturnedWhenSingleNoMatchingHeader() {
-
- final String key = "Head";
-
- Function<String, String> reader = createReader(key, null);
- HttpHeader header = new HttpHeader(key);
- assertNull(header.getAny(reader));
- }
-
- @Test
- public void nullReturnedWhenNoneHeaderMatches() {
- Function<String, String> reader = createReader("None", "Value");
- HttpHeader header = new HttpHeader("A", "B", "C");
- assertNull(header.getAny(reader));
- }
-
- @Test
- public void valueReturnedWhenLastHeaderMatches() {
-
- final String lastKey = "Last";
- final String value = "1234";
-
- Function<String, String> reader = createReader(lastKey, value);
- HttpHeader header = new HttpHeader(KEY_FIRST, KEY_SECOND, lastKey);
- assertEquals(value, header.getAny(reader));
- }
-
- @Test
- public void valueReturnedWhenFirstHeaderMatches() {
-
- final String value = "1234";
- Function<String, String> reader = createReader(KEY_FIRST, value);
- HttpHeader header = new HttpHeader(KEY_FIRST, KEY_SECOND, "Third");
- assertEquals(value, header.getAny(reader));
- }
-
- @Test
- public void valueReturnedWhenMiddleHeaderMatches() {
-
- final String value = "1234";
- Function<String, String> reader = createReader(KEY_SECOND, value);
- HttpHeader header = new HttpHeader(KEY_FIRST, KEY_SECOND, "Third");
- assertEquals(value, header.getAny(reader));
- }
-
- private Function<String, String> createReader(String key, String value) {
- return h -> h.equals(key) ? value : null;
- }
-} \ 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/jaxrs/LoggingRequestFilterTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingRequestFilterTest.java
index 4098809e42..aa77dea53a 100644
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingRequestFilterTest.java
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingRequestFilterTest.java
@@ -16,25 +16,18 @@
package org.openecomp.sdc.logging.servlet.jaxrs;
-import static org.easymock.EasyMock.anyObject;
-import static org.easymock.EasyMock.anyString;
-import static org.openecomp.sdc.logging.servlet.jaxrs.LoggingRequestFilter.START_TIME_KEY;
+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.mockito.Mockito.when;
+import static org.openecomp.sdc.logging.servlet.jaxrs.LoggingRequestFilter.LOGGING_TRACKER_KEY;
-import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.container.ContainerRequestContext;
-import org.easymock.EasyMock;
-import org.junit.After;
-import org.junit.Rule;
+import javax.ws.rs.container.ResourceInfo;
import org.junit.Test;
-import org.junit.rules.TestName;
-import org.junit.runner.RunWith;
-import org.openecomp.sdc.logging.LoggingConstants;
-import org.openecomp.sdc.logging.api.ContextData;
-import org.openecomp.sdc.logging.api.LoggingContext;
-import org.powermock.api.easymock.PowerMock;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
+import org.openecomp.sdc.logging.servlet.Tracker;
/**
@@ -43,222 +36,25 @@ import org.powermock.modules.junit4.PowerMockRunner;
* @author evitaliy
* @since 19 Mar 2018
*/
-@RunWith(PowerMockRunner.class)
-@PrepareForTest({LoggingContext.class, ContextData.class})
public class LoggingRequestFilterTest {
- private static final String REQUEST_URI = "/test";
- private static final String REQUEST_METHOD = "GET";
- private static final String RANDOM_REQUEST_ID = UUID.randomUUID().toString();
- private static final String RANDOM_PARTNER_NAME = UUID.randomUUID().toString();
-
- @Rule
- public TestName testName = new TestName();
-
- /**
- * Verify all mocks after each test.
- */
- @After
- public void verifyMocks() {
-
- try {
- PowerMock.verifyAll();
- } catch (AssertionError e) {
- throw new AssertionError("Expectations failed in " + testName.getMethodName() + "()", e);
- }
- }
-
- @Test
- public void serviceNamePopulatedWhenThereIsMatchingResource() {
-
- mockContextDataBuilder(null, null, LoggingRequestFilter.formatServiceName(REQUEST_METHOD, REQUEST_URI));
- mockLoggingContext();
-
- LoggingRequestFilter filter = new LoggingRequestFilter();
- filter.setHttpRequest(mockHttpRequest(true));
-
- filter.filter(mockContainerRequestContext(
- new RequestIdHeader(null),
- new PartnerHeader(null)));
- }
-
- @Test
- public void serviceNameDoesNotIncludeHttpMethodWhenHttpMethodDisabled() {
-
- mockContextDataBuilder(null, null, REQUEST_URI);
- mockLoggingContext();
-
- LoggingRequestFilter filter = new LoggingRequestFilter();
- filter.setHttpMethodInServiceName(false);
- filter.setHttpRequest(mockHttpRequest(false));
-
- filter.filter(mockContainerRequestContext(
- new RequestIdHeader(null),
- new PartnerHeader(null)));
- }
-
- @Test
- public void partnerNamePopulatedWhenPresentInDefaultHeader() {
-
- mockContextDataBuilder(null, RANDOM_PARTNER_NAME,
- LoggingRequestFilter.formatServiceName(REQUEST_METHOD, REQUEST_URI));
- mockLoggingContext();
-
- LoggingRequestFilter filter = new LoggingRequestFilter();
- filter.setHttpRequest(mockHttpRequest(true));
-
- filter.filter(mockContainerRequestContext(
- new RequestIdHeader(null),
- new PartnerHeader(RANDOM_PARTNER_NAME)));
- }
-
- @Test
- public void partnerNamePopulatedWhenPresentInCustomHeader() {
-
- final String partnerHeader = "x-partner-header";
- mockContextDataBuilder(null, RANDOM_PARTNER_NAME,
- LoggingRequestFilter.formatServiceName(REQUEST_METHOD, REQUEST_URI));
- mockLoggingContext();
-
- LoggingRequestFilter filter = new LoggingRequestFilter();
- filter.setHttpRequest(mockHttpRequest(true));
- filter.setPartnerNameHeaders(partnerHeader);
-
- filter.filter(mockContainerRequestContext(
- new RequestIdHeader(null),
- new PartnerHeader(partnerHeader, RANDOM_PARTNER_NAME)));
- }
-
@Test
- public void requestIdPopulatedWhenPresentInDefaultHeader() {
-
- mockContextDataBuilder(RANDOM_REQUEST_ID, null,
- LoggingRequestFilter.formatServiceName(REQUEST_METHOD, REQUEST_URI));
- mockLoggingContext();
-
- LoggingRequestFilter filter = new LoggingRequestFilter();
- filter.setHttpRequest(mockHttpRequest(true));
-
- filter.filter(mockContainerRequestContext(
- new RequestIdHeader(RANDOM_REQUEST_ID),
- new PartnerHeader(null)));
- }
-
- @Test
- public void requestIdPopulatedWhenPresentInCustomHeader() {
-
- final String requestIdHeader = "x-request-id";
- mockContextDataBuilder(RANDOM_REQUEST_ID, null,
- LoggingRequestFilter.formatServiceName(REQUEST_METHOD, REQUEST_URI));
- mockLoggingContext();
+ public void trackerPushedOntoRequestContext() {
- LoggingRequestFilter filter = new LoggingRequestFilter();
- filter.setRequestIdHeaders(requestIdHeader);
- filter.setHttpRequest(mockHttpRequest(true));
+ LoggingRequestFilter requestFilter = new LoggingRequestFilter();
+ requestFilter.setHttpRequest(mock(HttpServletRequest.class));
- filter.filter(mockContainerRequestContext(
- new RequestIdHeader(requestIdHeader, RANDOM_REQUEST_ID),
- new PartnerHeader(null)));
- }
-
- private HttpServletRequest mockHttpRequest(boolean includeMethod) {
-
- HttpServletRequest servletRequest = EasyMock.mock(HttpServletRequest.class);
- EasyMock.expect(servletRequest.getRequestURI()).andReturn(REQUEST_URI);
-
- if (includeMethod) {
- EasyMock.expect(servletRequest.getMethod()).andReturn(REQUEST_METHOD);
- }
-
- EasyMock.replay(servletRequest);
- return servletRequest;
- }
-
- private ContainerRequestContext mockContainerRequestContext(Header... headers) {
-
- ContainerRequestContext requestContext = EasyMock.mock(ContainerRequestContext.class);
-
- for (Header h : headers) {
- EasyMock.expect(requestContext.getHeaderString(h.key)).andReturn(h.value);
- }
-
- requestContext.setProperty(EasyMock.eq(START_TIME_KEY), EasyMock.anyLong());
- EasyMock.expectLastCall();
-
- EasyMock.replay(requestContext);
- return requestContext;
- }
-
- private void mockContextDataBuilder(String requestId, String partnerName, String serviceName) {
-
- ContextData.ContextDataBuilder mockBuilder = EasyMock.mock(ContextData.ContextDataBuilder.class);
-
- if (requestId != null) {
- EasyMock.expect(mockBuilder.requestId(requestId)).andReturn(mockBuilder);
- } else {
- EasyMock.expect(mockBuilder.requestId(anyString())).andReturn(mockBuilder);
- }
-
- EasyMock.expect(mockBuilder.serviceName(serviceName)).andReturn(mockBuilder);
-
- if (partnerName != null) {
- EasyMock.expect(mockBuilder.partnerName(partnerName)).andReturn(mockBuilder);
- }
-
- EasyMock.expect(mockBuilder.build()).andReturn(EasyMock.mock(ContextData.class));
- EasyMock.replay(mockBuilder);
+ ResourceInfo resourceInfo = mock(ResourceInfo.class);
+ when(resourceInfo.getResourceMethod()).thenReturn(Resource.class.getDeclaredMethods()[0]);
+ requestFilter.setResource(resourceInfo);
- PowerMock.mockStatic(ContextData.class);
-
- ContextData.builder();
- PowerMock.expectLastCall().andReturn(mockBuilder);
-
- PowerMock.replay(ContextData.class);
- }
-
- private void mockLoggingContext() {
-
- PowerMock.mockStatic(LoggingContext.class);
-
- LoggingContext.clear();
- EasyMock.expectLastCall();
-
- LoggingContext.put(anyObject(ContextData.class));
- EasyMock.expectLastCall();
-
- PowerMock.replay(LoggingContext.class);
- }
-
- private abstract static class Header {
-
- private final String key;
- private final String value;
-
- private Header(String key, String value) {
- this.key = key;
- this.value = value;
- }
+ ContainerRequestContext requestContext = mock(ContainerRequestContext.class);
+ requestFilter.filter(requestContext);
+ verify(requestContext).setProperty(eq(LOGGING_TRACKER_KEY), any(Tracker.class));
}
- private static class PartnerHeader extends Header {
-
- private PartnerHeader(String value) {
- super(LoggingConstants.DEFAULT_PARTNER_NAME_HEADER, value);
- }
-
- private PartnerHeader(String key, String value) {
- super(key, value);
- }
- }
-
- private static class RequestIdHeader extends Header {
-
- private RequestIdHeader(String value) {
- super(LoggingConstants.DEFAULT_REQUEST_ID_HEADER, value);
- }
-
- private RequestIdHeader(String key, String value) {
- super(key, value);
- }
+ private interface Resource {
+ @SuppressWarnings("unused")
+ void method();
}
} \ 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/jaxrs/LoggingResponseFilterTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingResponseFilterTest.java
index 710cf12b7e..336840be58 100644
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingResponseFilterTest.java
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingResponseFilterTest.java
@@ -16,35 +16,18 @@
package org.openecomp.sdc.logging.servlet.jaxrs;
-import static org.easymock.EasyMock.anyObject;
-import static org.easymock.EasyMock.anyString;
+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 static org.mockito.Mockito.when;
+import static org.openecomp.sdc.logging.servlet.jaxrs.LoggingRequestFilter.LOGGING_TRACKER_KEY;
-import java.lang.reflect.Method;
-import java.util.Comparator;
-import java.util.Objects;
-import java.util.function.Consumer;
-import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
-import javax.ws.rs.container.ResourceInfo;
-import javax.ws.rs.core.Response;
-import org.easymock.EasyMock;
-import org.easymock.LogicalOperator;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.TestName;
-import org.junit.runner.RunWith;
-import org.openecomp.sdc.logging.api.AuditData;
-import org.openecomp.sdc.logging.api.Logger;
-import org.openecomp.sdc.logging.api.LoggerFactory;
-import org.openecomp.sdc.logging.api.LoggingContext;
-import org.openecomp.sdc.logging.api.StatusCode;
-import org.powermock.api.easymock.PowerMock;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
-
+import org.openecomp.sdc.logging.servlet.RequestProcessingResult;
+import org.openecomp.sdc.logging.servlet.Tracker;
/**
* Unit testing JAX-RS response filter.
@@ -52,277 +35,24 @@ import org.powermock.modules.junit4.PowerMockRunner;
* @author evitaliy
* @since 19 Mar 2018
*/
-@RunWith(PowerMockRunner.class)
public class LoggingResponseFilterTest {
- private static final Class RESOURCE_TYPE = Resource.class;
- private static final Method RESOURCE_METHOD = Resource.class.getDeclaredMethods()[0];
-
- @Rule
- public TestName testName = new TestName();
-
- @Before
- public void prepareLoggingContext() {
- mockLoggingContext();
- }
-
- /**
- * Verify all mocks after each test.
- */
- @After
- public void verifyMocks() {
-
- try {
- PowerMock.verifyAll();
- } catch (AssertionError e) {
- throw new AssertionError("Expectations failed in " + testName.getMethodName() + "()", e);
- }
- }
-
@Test
- @PrepareForTest({LoggingContext.class, LoggerFactory.class})
- public void noAuditWhenNoMatchingResource() {
-
- PowerMock.mockStatic(LoggerFactory.class);
- mockFilterLogger(logger -> {
- logger.debug(anyString());
- EasyMock.expectLastCall();
- });
- PowerMock.replay(LoggerFactory.class);
-
- LoggingResponseFilter responseFilter = new LoggingResponseFilter();
- responseFilter.filter(mockDisabledRequestContext(), mockDisabledResponseContext());
+ public void noExceptionsWhenTrackerNotPassed() {
+ new LoggingResponseFilter().filter(mock(ContainerRequestContext.class), mock(ContainerResponseContext.class));
}
@Test
- @PrepareForTest({LoggingContext.class, LoggerFactory.class})
- public void noAuditWhenNullResource() {
+ public void postRequestWhenTrackerPassed() {
- PowerMock.mockStatic(LoggerFactory.class);
- mockFilterLogger(logger -> {
- logger.debug(anyString());
- EasyMock.expectLastCall();
- });
- PowerMock.replay(LoggerFactory.class);
+ Tracker tracker = mock(Tracker.class);
- LoggingResponseFilter responseFilter = new LoggingResponseFilter();
- responseFilter.setResource(null);
- responseFilter.filter(mockDisabledRequestContext(), mockDisabledResponseContext());
- }
-
- @Test
- @PrepareForTest({LoggingContext.class, LoggerFactory.class})
- public void noAuditWhenAuditDisabled() {
-
- PowerMock.mockStatic(LoggerFactory.class);
- mockFilterLogger();
- mockResourceLogger(false, AuditData.builder().build());
- PowerMock.replay(LoggerFactory.class);
+ ContainerRequestContext requestContext = mock(ContainerRequestContext.class);
+ when(requestContext.getProperty(LOGGING_TRACKER_KEY)).thenReturn(tracker);
LoggingResponseFilter responseFilter = new LoggingResponseFilter();
- responseFilter.setResource(mockResource());
- responseFilter.filter(mockDisabledRequestContext(), mockDisabledResponseContext());
- }
-
- @Test
- @PrepareForTest({LoggingContext.class, LoggerFactory.class})
- public void startTimeReadWhenPresentInRequestContext() {
-
- final String clientIp = "IP1";
- final long startTime = 12345L;
- final Response.Status ok = Response.Status.OK;
-
- PowerMock.mockStatic(LoggerFactory.class);
- mockResourceLogger(true, buildAuditData(startTime, clientIp, ok, StatusCode.COMPLETE));
- mockFilterLogger();
- PowerMock.replay(LoggerFactory.class);
-
- LoggingResponseFilter filter = new LoggingResponseFilter();
- filter.setResource(mockResource());
- filter.setHttpRequest(mockHttpRequest(clientIp));
-
- filter.filter(mockRequestContext(startTime), mockResponseContext(ok));
- }
-
- @Test
- @PrepareForTest({LoggingContext.class, LoggerFactory.class})
- public void startTimeZeroWhenNotPresentInRequestContext() {
-
- final String clientIp = "IP2";
- final Response.Status ok = Response.Status.OK;
-
- AuditData expectedAuditData = buildAuditData(0, clientIp, ok, StatusCode.COMPLETE);
-
- PowerMock.mockStatic(LoggerFactory.class);
- mockResourceLogger(true, expectedAuditData);
- mockFilterLogger(logger -> {
- logger.error(anyString(), anyObject(Object[].class));
- EasyMock.expectLastCall();
- });
- PowerMock.replay(LoggerFactory.class);
-
- LoggingResponseFilter filter = new LoggingResponseFilter();
- filter.setResource(mockResource());
- filter.setHttpRequest(mockHttpRequest(clientIp));
-
- filter.filter(mockRequestContext(null), mockResponseContext(ok));
- }
-
- @Test
- @PrepareForTest({LoggingContext.class, LoggerFactory.class})
- public void startTimeZeroWhenIncorrectObjectType() {
-
- final String clientIp = "IP3";
- final Response.Status accepted = Response.Status.ACCEPTED;
-
- AuditData expectedAuditData = buildAuditData(0, clientIp, accepted, StatusCode.COMPLETE);
-
- PowerMock.mockStatic(LoggerFactory.class);
- mockFilterLogger(logger -> {
- logger.error(anyString(), anyString(), anyString(), anyObject());
- EasyMock.expectLastCall();
- });
- mockResourceLogger(true, expectedAuditData);
- PowerMock.replay(LoggerFactory.class);
-
- LoggingResponseFilter filter = new LoggingResponseFilter();
- filter.setResource(mockResource());
- filter.setHttpRequest(mockHttpRequest(clientIp));
-
- filter.filter(mockRequestContext("string object"), mockResponseContext(accepted));
- }
-
- @Test
- @PrepareForTest({LoggingContext.class, LoggerFactory.class})
- public void statusErrorWhenHttpResponseGreaterThan399() {
-
- final Response.Status error = Response.Status.BAD_REQUEST;
- final String clientIp = "IP13";
- final long startTime = 88668603L;
-
- AuditData expectedAuditData = buildAuditData(startTime, clientIp, error, StatusCode.ERROR);
-
- PowerMock.mockStatic(LoggerFactory.class);
- mockResourceLogger(true, expectedAuditData);
- mockFilterLogger();
- PowerMock.replay(LoggerFactory.class);
-
- LoggingResponseFilter filter = new LoggingResponseFilter();
- filter.setResource(mockResource());
- filter.setHttpRequest(mockHttpRequest(clientIp));
-
- filter.filter(mockRequestContext(startTime), mockResponseContext(error));
- }
-
- private AuditData buildAuditData(long startTime, String clientIp, Response.Status responseStatus,
- StatusCode status) {
- return AuditData.builder().startTime(startTime).responseCode(Integer.toString(responseStatus.getStatusCode()))
- .responseDescription(responseStatus.getReasonPhrase()).clientIpAddress(clientIp)
- .statusCode(status).build();
- }
-
- private void mockResourceLogger(boolean enabled, AuditData auditData) {
-
- Logger resourceLogger = EasyMock.mock(Logger.class);
-
- EasyMock.expect(resourceLogger.isAuditEnabled()).andReturn(enabled).atLeastOnce();
-
- if (enabled) {
- resourceLogger.audit(EasyMock.cmp(auditData, new AuditDataComparator(), LogicalOperator.EQUAL));
- EasyMock.expectLastCall();
- }
-
- EasyMock.replay(resourceLogger);
-
- LoggerFactory.getLogger(RESOURCE_TYPE);
- PowerMock.expectLastCall().andReturn(resourceLogger);
- }
-
- @SafeVarargs
- private final void mockFilterLogger(Consumer<Logger>... expectations) {
-
- Logger filterLogger = EasyMock.mock(Logger.class);
-
- for (Consumer<Logger> expect : expectations) {
- expect.accept(filterLogger);
- }
-
- EasyMock.replay(filterLogger);
-
- LoggerFactory.getLogger(LoggingResponseFilter.class);
- PowerMock.expectLastCall().andReturn(filterLogger);
- }
-
- private void mockLoggingContext() {
- PowerMock.mockStatic(LoggingContext.class);
- LoggingContext.clear();
- EasyMock.expectLastCall();
- PowerMock.replay(LoggingContext.class);
- }
-
- private ContainerRequestContext mockDisabledRequestContext() {
- ContainerRequestContext requestContext = EasyMock.mock(ContainerRequestContext.class);
- EasyMock.replay(requestContext);
- return requestContext;
- }
-
- private ContainerResponseContext mockDisabledResponseContext() {
- ContainerResponseContext responseContext = EasyMock.mock(ContainerResponseContext.class);
- EasyMock.replay(responseContext);
- return responseContext;
- }
-
- private HttpServletRequest mockHttpRequest(String clientIp) {
- HttpServletRequest servletRequest = EasyMock.mock(HttpServletRequest.class);
- EasyMock.expect(servletRequest.getRemoteAddr()).andReturn(clientIp);
- EasyMock.replay(servletRequest);
- return servletRequest;
- }
-
- private ContainerRequestContext mockRequestContext(Object startTime) {
- ContainerRequestContext requestContext = EasyMock.mock(ContainerRequestContext.class);
- EasyMock.expect(requestContext.getProperty(LoggingRequestFilter.START_TIME_KEY)).andReturn(startTime);
- EasyMock.replay(requestContext);
- return requestContext;
- }
-
- private ContainerResponseContext mockResponseContext(Response.StatusType statusInfo) {
- ContainerResponseContext responseContext = EasyMock.mock(ContainerResponseContext.class);
- EasyMock.expect(responseContext.getStatusInfo()).andReturn(statusInfo);
- EasyMock.replay(responseContext);
- return responseContext;
- }
-
- private ResourceInfo mockResource() {
- ResourceInfo resource = EasyMock.mock(ResourceInfo.class);
- //noinspection unchecked
- EasyMock.expect(resource.getResourceClass()).andReturn(RESOURCE_TYPE).anyTimes();
- EasyMock.expect(resource.getResourceMethod()).andReturn(RESOURCE_METHOD).anyTimes();
- EasyMock.replay(resource);
- return resource;
- }
-
- private static class AuditDataComparator implements Comparator<AuditData> {
-
- @Override
- public int compare(AuditData one, AuditData two) {
-
- // don't compare end time as it changes
- if (Objects.equals(one.getClientIpAddress(), two.getClientIpAddress())
- && Objects.equals(one.getResponseCode(), two.getResponseCode())
- && Objects.equals(one.getResponseDescription(), two.getResponseDescription())
- && one.getStartTime() == two.getStartTime()
- && Objects.equals(one.getStatusCode(), two.getStatusCode())) {
-
- return 0;
- }
-
- return -1;
- }
- }
+ responseFilter.filter(requestContext, mock(ContainerResponseContext.class));
- interface Resource {
- @SuppressWarnings("unused")
- void method();
+ verify(tracker, 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-spring/pom.xml b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-spring/pom.xml
new file mode 100644
index 0000000000..b556a2a9b0
--- /dev/null
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-spring/pom.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ 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.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+ <parent>
+ <artifactId>openecomp-sdc-logging-lib</artifactId>
+ <groupId>org.openecomp.sdc</groupId>
+ <version>1.3.0-SNAPSHOT</version>
+ </parent>
+
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>openecomp-sdc-logging-spring</artifactId>
+
+
+</project> \ No newline at end of file
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/pom.xml b/openecomp-be/lib/openecomp-sdc-logging-lib/pom.xml
index 42da150269..16fb85364e 100644
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/pom.xml
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/pom.xml
@@ -17,6 +17,7 @@
<modules>
<module>openecomp-sdc-logging-api</module>
<module>openecomp-sdc-logging-core</module>
+ <module>openecomp-sdc-logging-spring</module>
</modules>
</project>