diff options
author | vempo <vitaliy.emporopulo@amdocs.com> | 2018-06-27 20:36:49 +0300 |
---|---|---|
committer | vempo <vitaliy.emporopulo@amdocs.com> | 2018-06-27 20:36:49 +0300 |
commit | c6a769eff2930fcf4f4dadb8106937abf86d8638 (patch) | |
tree | 2d4ed94def6a22c64f8a84c4283f0f1455346fb2 | |
parent | 0cc593ea8c181cfbcde343be45b8cf1cdd6ddbff (diff) |
Improvements in audit logger for onboarding
A few optimizations, more unit-tests,
log via the handling resource's logger instead
of filter's logger.
Change-Id: I40cef2c86a82b25ded1f8fdca1ec3b0f2fe062d8
Issue-ID: SDC-1451
Signed-off-by: vempo <vitaliy.emporopulo@amdocs.com>
29 files changed, 686 insertions, 631 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 4a573f853b..1a269ff6b5 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 @@ -28,9 +28,9 @@ </dependency> <!-- testing --> <dependency> - <groupId>org.testng</groupId> - <artifactId>testng</artifactId> - <version>${testng.version}</version> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> @@ -53,7 +53,7 @@ </dependency> <dependency> <groupId>org.powermock</groupId> - <artifactId>powermock-module-testng</artifactId> + <artifactId>powermock-module-junit4</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 1f3df8bcc0..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 @@ -36,8 +36,8 @@ public class LoggerFactory { // use the no-op service to prevent recursion in case of an attempt to log an exception as a // result of a logger initialization error - private static final LoggerCreationService SERVICE = ServiceBinder.getCreationServiceBinding().orElse( - new NoOpLoggerCreationService()); + private static final LoggerCreationService SERVICE = ServiceBinder.getCreationServiceBinding().orElseGet( + NoOpLoggerCreationService::new); private LoggerFactory() { // prevent instantiation @@ -53,10 +53,10 @@ public class LoggerFactory { private static class NoOpLoggerCreationService implements LoggerCreationService { - private static final Logger NO_OP_LOGGER = new NoOpLogger(); - private static class NoOpLogger implements Logger { + private static final Logger INSTANCE = new NoOpLogger(); + @Override public String getName() { return "No-Op Logger"; @@ -211,13 +211,13 @@ public class LoggerFactory { @Override public Logger getLogger(String className) { Objects.requireNonNull(className, "Name cannot be null"); - return NO_OP_LOGGER; + return NoOpLogger.INSTANCE; } @Override public Logger getLogger(Class<?> clazz) { Objects.requireNonNull(clazz, "Class cannot be null"); - return NO_OP_LOGGER; + return NoOpLogger.INSTANCE; } } } 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 c17149e98e..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 @@ -36,7 +36,7 @@ import org.openecomp.sdc.logging.spi.LoggingContextService; public class LoggingContext { private static final LoggingContextService SERVICE = - ServiceBinder.getContextServiceBinding().orElse(new NoOpLoggingContextService()); + ServiceBinder.getContextServiceBinding().orElseGet(NoOpLoggingContextService::new); private LoggingContext() { // prevent instantiation @@ -64,8 +64,6 @@ public class LoggingContext { private static class NoOpLoggingContextService implements LoggingContextService { - private static final ContextData EMPTY_CONTEXT = ContextData.builder().build(); - @Override public void put(ContextData contextData) { Objects.requireNonNull(contextData, "Context data cannot be null"); @@ -73,7 +71,7 @@ public class LoggingContext { @Override public ContextData get() { - return EMPTY_CONTEXT; + return EmptyContextData.INSTANCE; } @Override @@ -92,5 +90,14 @@ public class LoggingContext { Objects.requireNonNull(callable, "Callable cannot be null"); return callable; } + + private static class EmptyContextData { + + private static final ContextData INSTANCE = ContextData.builder().build(); + + private EmptyContextData() { + // prevent instantiation + } + } } } 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 498587414a..0e1b7d715b 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,12 +19,10 @@ 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.lang.reflect.Method; -import java.lang.reflect.Proxy; 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; @@ -66,19 +64,27 @@ public class LoggingRequestFilter implements ContainerRequestFilter { private static final Logger LOGGER = LoggerFactory.getLogger(LoggingRequestFilter.class); - private ResourceInfo resource; + 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; /** - * Injection of a resource that matches the request from JAX-RS context. + * Injection of HTTP request object from JAX-RS context. * - * @param resource automatically injected by JAX-RS container + * @param httpRequest automatically injected by JAX-RS container */ @Context - public void setResource(ResourceInfo resource) { - this.resource = resource; + public void setHttpRequest(HttpServletRequest httpRequest) { + this.httpRequest = httpRequest; + } + + /** + * Configuration parameter to include the HTTP method of a request in service name. + */ + public void setHttpMethodInServiceName(boolean includeHttpMethod) { + this.includeHttpMethod = includeHttpMethod; } /** @@ -100,22 +106,10 @@ public class LoggingRequestFilter implements ContainerRequestFilter { @Override public void filter(ContainerRequestContext containerRequestContext) { - if (resource == null) { - // JAX-RS could not find a mapping this response, probably due to HTTP 404 (not found), - // 405 (method not allowed), 415 (unsupported media type), etc. with a message in Web server log - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("No matching resource was found for URI '{}' and method '{}'", - containerRequestContext.getUriInfo().getPath(), containerRequestContext.getMethod()); - } - - return; - } + LoggingContext.clear(); containerRequestContext.setProperty(START_TIME_KEY, System.currentTimeMillis()); - LoggingContext.clear(); - ContextData.ContextDataBuilder contextData = ContextData.builder(); contextData.serviceName(getServiceName()); @@ -131,48 +125,12 @@ public class LoggingRequestFilter implements ContainerRequestFilter { } private String getServiceName() { - - Class<?> resourceClass = resource.getResourceClass(); - Method resourceMethod = resource.getResourceMethod(); - - if (Proxy.isProxyClass(resourceClass)) { - LOGGER.debug("Proxy class injected for JAX-RS resource"); - return getServiceNameFromJavaProxy(resourceClass, resourceMethod); - } - - return formatServiceName(resourceClass, resourceMethod); + return includeHttpMethod + ? formatServiceName(this.httpRequest.getMethod(), this.httpRequest.getRequestURI()) + : this.httpRequest.getRequestURI(); } - private String getServiceNameFromJavaProxy(Class<?> proxyType, Method resourceMethod) { - - for (Class<?> interfaceType : proxyType.getInterfaces()) { - - if (isMatchingInterface(interfaceType, resourceMethod)) { - return formatServiceName(interfaceType, resourceMethod); - } - } - - LOGGER.debug("Failed to find method '{}' in interfaces implemented by injected Java proxy", resourceMethod); - return formatServiceName(proxyType, resourceMethod); - } - - private String formatServiceName(Class<?> resourceClass, Method resourceMethod) { - return resourceClass.getName() + "#" + resourceMethod.getName(); - } - - private boolean isMatchingInterface(Class<?> candidateType, Method requestedMethod) { - - try { - - Method candidate = candidateType.getDeclaredMethod(requestedMethod.getName(), - requestedMethod.getParameterTypes()); - return candidate != null; - - } catch (NoSuchMethodException ignored) { - // ignore and move on to the next - LOGGER.debug("Failed to find method '{}' in interface '{}'", requestedMethod, candidateType); - } - - return false; + static String formatServiceName(String httpMethod, String requestUri) { + return httpMethod + " " + requestUri; } } 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 fbe28a79eb..e0353a43ca 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 @@ -23,6 +23,7 @@ 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; @@ -53,7 +54,9 @@ import org.openecomp.sdc.logging.api.StatusCode; @Provider public class LoggingResponseFilter implements ContainerResponseFilter { - private final Logger logger = LoggerFactory.getLogger(this.getClass()); + 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 @@ -63,6 +66,8 @@ public class LoggingResponseFilter implements ContainerResponseFilter { private HttpServletRequest httpRequest; + private ResourceInfo resource; + /** * Injection of HTTP request object from JAX-RS context. * @@ -73,12 +78,29 @@ public class LoggingResponseFilter implements ContainerResponseFilter { 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) { try { + + if ((resource == null) || (resource.getResourceClass() == null)) { + LOGGER.debug("No matching resource, skipping audit."); + return; + } + writeAudit(containerRequestContext, containerResponseContext); + } finally { LoggingContext.clear(); } @@ -87,7 +109,8 @@ public class LoggingResponseFilter implements ContainerResponseFilter { private void writeAudit(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) { - if (!logger.isAuditEnabled()) { + Logger resourceLogger = LoggerFactory.getLogger(resource.getResourceMethod().getDeclaringClass()); + if (!resourceLogger.isAuditEnabled()) { return; } @@ -102,7 +125,7 @@ public class LoggingResponseFilter implements ContainerResponseFilter { .responseCode(Integer.toString(responseCode)) .responseDescription(statusInfo.getReasonPhrase()) .clientIpAddress(httpRequest.getRemoteAddr()).build(); - logger.audit(auditData); + resourceLogger.audit(auditData); } private boolean isSuccess(int responseCode) { @@ -122,7 +145,7 @@ public class LoggingResponseFilter implements ContainerResponseFilter { 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 0; + return UNKNOWN_START_TIME; } private long parseStartTime(Object startTime) { @@ -131,7 +154,7 @@ public class LoggingResponseFilter implements ContainerResponseFilter { 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()); + + " is expected", LoggingRequestFilter.START_TIME_KEY, startTime.getClass().getName(), e); return 0; } } @@ -140,7 +163,7 @@ public class LoggingResponseFilter implements ContainerResponseFilter { if (reportBadConfiguration) { reportBadConfiguration = false; - logger.error(message, arguments); + LOGGER.error(message, arguments); } } } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/AuditDataTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/AuditDataTest.java index 08ce5089b7..98da27dcc5 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/AuditDataTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/AuditDataTest.java @@ -16,11 +16,14 @@ package org.openecomp.sdc.logging.api; -import static org.testng.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; -import org.testng.annotations.Test; +import org.junit.Test; /** + * Test correct population of audit data. + * * @author EVITALIY * @since 04 Mar 18 */ @@ -33,24 +36,25 @@ public class AuditDataTest { final long end = start + 100; final String responseCode = "Response-Code"; final String responseDescription = "Response-Description"; - final String ipAddress = "10.56.20.70"; + final String ipAddress = "DUMMY.IP.ADDRESS"; AuditData data = AuditData.builder().startTime(start).endTime(end).statusCode(StatusCode.COMPLETE) - .responseCode(responseCode).responseDescription(responseDescription).clientIpAddress(ipAddress).build(); - - assertEquals(data.getClientIpAddress(), ipAddress); - assertEquals(data.getEndTime(), end); - assertEquals(data.getStartTime(), start); - assertEquals(data.getResponseCode(), responseCode); - assertEquals(data.getResponseDescription(), responseDescription); - assertEquals(data.getStatusCode(), StatusCode.COMPLETE); + .responseCode(responseCode).responseDescription(responseDescription) + .clientIpAddress(ipAddress).build(); + + assertEquals(ipAddress, data.getClientIpAddress()); + assertEquals(end, data.getEndTime()); + assertEquals(start, data.getStartTime()); + assertEquals(responseCode, data.getResponseCode()); + assertEquals(responseDescription, data.getResponseDescription()); + assertEquals(StatusCode.COMPLETE, data.getStatusCode()); } @Test public void allPropertiesEmptyWhenUnpopulated() { AuditData data = AuditData.builder().build(); - assertEquals(data.getStartTime(), 0); - assertEquals(data.getEndTime(), 0); + assertEquals(0, data.getStartTime()); + assertEquals(0, data.getEndTime()); assertNull(data.getClientIpAddress()); assertNull(data.getResponseCode()); assertNull(data.getResponseDescription()); diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/ContextDataTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/ContextDataTest.java index 8a173b452a..873a489b84 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/ContextDataTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/ContextDataTest.java @@ -16,10 +16,10 @@ package org.openecomp.sdc.logging.api; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; -import org.testng.annotations.Test; +import org.junit.Test; /** * Unit-testing context data builder. @@ -37,11 +37,11 @@ public class ContextDataTest { final String requestId = "123412341234"; ContextData data = ContextData.builder() - .serviceName(serviceName).partnerName(partnerName).requestId(requestId).build(); + .serviceName(serviceName).partnerName(partnerName).requestId(requestId).build(); - assertEquals(data.getRequestId(), requestId); - assertEquals(data.getServiceName(), serviceName); - assertEquals(data.getPartnerName(), partnerName); + assertEquals(requestId, data.getRequestId()); + assertEquals(serviceName, data.getServiceName()); + assertEquals(partnerName, data.getPartnerName()); } @Test diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggerFactoryTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggerFactoryTest.java index a1fe8c2b0f..6f860af60a 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggerFactoryTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggerFactoryTest.java @@ -16,11 +16,11 @@ package org.openecomp.sdc.logging.api; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import java.lang.reflect.Field; -import org.testng.annotations.Test; +import org.junit.Test; /** * Unit-test creation of a logger via factory, assuming not default binding. @@ -31,15 +31,15 @@ import org.testng.annotations.Test; public class LoggerFactoryTest { @Test - public void shouldHoldNoOpWhenNoBinding() throws Exception { + public void shouldHoldNoOpWhenNoBinding() throws NoSuchFieldException, IllegalAccessException { // set up to access the private static field Field factory = LoggerFactory.class.getDeclaredField("SERVICE"); factory.setAccessible(true); Object impl = factory.get(null); - assertEquals(impl.getClass().getName(), - "org.openecomp.sdc.logging.api.LoggerFactory$NoOpLoggerCreationService"); + assertEquals("org.openecomp.sdc.logging.api.LoggerFactory$NoOpLoggerCreationService", + impl.getClass().getName()); } @Test @@ -54,12 +54,12 @@ public class LoggerFactoryTest { verifyLoggerWorks(logger); } - @Test(expectedExceptions = NullPointerException.class) + @Test(expected = NullPointerException.class) public void throwNpeWhenGetByNameWithNull() { LoggerFactory.getLogger((String) null); } - @Test(expectedExceptions = NullPointerException.class) + @Test(expected = NullPointerException.class) public void throwNpeWhenGetByClassWithNull() { LoggerFactory.getLogger((Class<LoggerFactoryTest>) null); } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggingContextTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggingContextTest.java index 3e7bbe14e0..ea908ae079 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggingContextTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggingContextTest.java @@ -16,12 +16,12 @@ package org.openecomp.sdc.logging.api; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; import java.lang.reflect.Field; import java.util.concurrent.Callable; -import org.testng.annotations.Test; +import org.junit.Test; /** * Unit-testing default context service implementation. @@ -32,15 +32,15 @@ import org.testng.annotations.Test; public class LoggingContextTest { @Test - public void shouldHoldNoOpWhenNoBinding() throws Exception { + public void shouldHoldNoOpWhenNoBinding() throws NoSuchFieldException, IllegalAccessException { Field factory = LoggingContext.class.getDeclaredField("SERVICE"); factory.setAccessible(true); Object impl = factory.get(null); - assertEquals(impl.getClass().getName(), - "org.openecomp.sdc.logging.api.LoggingContext$NoOpLoggingContextService"); + assertEquals("org.openecomp.sdc.logging.api.LoggingContext$NoOpLoggingContextService", + impl.getClass().getName()); } - @Test(expectedExceptions = NullPointerException.class) + @Test(expected = NullPointerException.class) public void throwNpeWhenContextIsNull() { LoggingContext.put(null); } @@ -53,10 +53,10 @@ public class LoggingContextTest { @Test public void toRunnableReturnsSameInstance() { Runnable test = () -> { /* do nothing */ }; - assertTrue(test == LoggingContext.copyToRunnable(test)); + assertSame(test, LoggingContext.copyToRunnable(test)); } - @Test(expectedExceptions = NullPointerException.class) + @Test(expected = NullPointerException.class) public void throwNpeWhenToRunnableWithNull() { LoggingContext.copyToRunnable(null); } @@ -64,10 +64,10 @@ public class LoggingContextTest { @Test public void toCallableReturnsSameInstance() { Callable<String> test = () -> ""; - assertTrue(test == LoggingContext.copyToCallable(test)); + assertSame(test, LoggingContext.copyToCallable(test)); } - @Test(expectedExceptions = NullPointerException.class) + @Test(expected = NullPointerException.class) public void throwNpeWhenToCallableWithNull() { LoggingContext.copyToCallable(null); } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/MetricsDataTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/MetricsDataTest.java index a3c8b1039a..43f913ecab 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/MetricsDataTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/MetricsDataTest.java @@ -16,10 +16,10 @@ package org.openecomp.sdc.logging.api; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; -import org.testng.annotations.Test; +import org.junit.Test; /** * Unit-testing metrics builder and structure. @@ -36,7 +36,7 @@ public class MetricsDataTest { final long end = start + 1000; final String responseCode = "Metrics-Response-Code"; final String responseDescription = "Metrics-Response-Description"; - final String ipAddress = "10.56.20.72"; + final String ipAddress = "IP72"; final String targetEntity = "Metrics-Target-Entity"; final String targetVirtualEntity = "Metrics-Target-Virtual-Entity"; @@ -45,22 +45,22 @@ public class MetricsDataTest { .clientIpAddress(ipAddress).targetEntity(targetEntity) .targetVirtualEntity(targetVirtualEntity).build(); - assertEquals(data.getClientIpAddress(), ipAddress); - assertEquals(data.getEndTime(), end); - assertEquals(data.getStartTime(), start); - assertEquals(data.getResponseCode(), responseCode); - assertEquals(data.getResponseDescription(), responseDescription); - assertEquals(data.getStatusCode(), StatusCode.COMPLETE); - assertEquals(data.getTargetEntity(), targetEntity); - assertEquals(data.getTargetVirtualEntity(), targetVirtualEntity); + assertEquals(ipAddress, data.getClientIpAddress()); + assertEquals(end, data.getEndTime()); + assertEquals(start, data.getStartTime()); + assertEquals(responseCode, data.getResponseCode()); + assertEquals(responseDescription, data.getResponseDescription()); + assertEquals(StatusCode.COMPLETE, data.getStatusCode()); + assertEquals(targetEntity, data.getTargetEntity()); + assertEquals(targetVirtualEntity, data.getTargetVirtualEntity()); } @Test public void allMetricsPropertiesEmptyWhenUnpopulated() { MetricsData data = MetricsData.builder().build(); - assertEquals(data.getStartTime(), 0); - assertEquals(data.getEndTime(), 0); + assertEquals(0, data.getStartTime()); + assertEquals(0, data.getEndTime()); assertNull(data.getClientIpAddress()); assertNull(data.getResponseCode()); assertNull(data.getResponseDescription()); diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/ServiceBinderTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/ServiceBinderTest.java index 1a5c81d90d..74dd9e93f1 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/ServiceBinderTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/ServiceBinderTest.java @@ -16,11 +16,13 @@ package org.openecomp.sdc.logging.api; -import org.testng.annotations.Test; +import static org.junit.Assert.assertFalse; -import static org.testng.Assert.*; +import org.junit.Test; /** + * Test default service binding. + * * @author EVITALIY * @since 08 Jan 18 */ 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 index 535a50ca42..9bba28c5e4 100644 --- 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 @@ -32,14 +32,16 @@ import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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.testng.PowerMockTestCase; -import org.testng.ITestResult; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.Test; +import org.powermock.modules.junit4.PowerMockRunner; /** * Unit-tests logging filter for initialization and data retrieval. @@ -47,29 +49,33 @@ import org.testng.annotations.Test; * @author evitaliy * @since 17 Aug 2016 */ +@RunWith(PowerMockRunner.class) @PrepareForTest(LoggingContext.class) -public class LoggingFilterTest extends PowerMockTestCase { +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. */ - @AfterMethod - public void verifyMocks(ITestResult result) { + @After + public void verifyMocks() { try { PowerMock.verifyAll(); } catch (AssertionError e) { - throw new AssertionError("Expectations failed in: " + result.getMethod().getMethodName(), e); + throw new AssertionError("Expectations failed in " + testName.getMethodName() + "()", e); } } @Test - public void filterPopulatesValuesWhenNoInitParamsAndNoHeaders() throws Exception { + public void filterPopulatesValuesWhenNoInitParamsAndNoHeaders() throws IOException, ServletException { mockLoggingContext(); LoggingFilter loggingFilter = new LoggingFilter(); @@ -78,7 +84,7 @@ public class LoggingFilterTest extends PowerMockTestCase { } @Test - public void filterPopulatesValuesWhenNoInitParamsAndExistingHeaders() throws Exception { + public void filterPopulatesValuesWhenNoInitParamsAndExistingHeaders() throws IOException, ServletException { mockLoggingContext(); @@ -91,7 +97,7 @@ public class LoggingFilterTest extends PowerMockTestCase { } @Test - public void filterPopulatesValuesWhenCustomInitParamsAndNoHeaders() throws Exception { + public void filterPopulatesValuesWhenCustomInitParamsAndNoHeaders() throws IOException, ServletException { mockLoggingContext(); @@ -108,7 +114,7 @@ public class LoggingFilterTest extends PowerMockTestCase { } @Test - public void filterPopulatesValuesWhenCustomInitParamsAndExistingHeaders() throws Exception { + public void filterPopulatesValuesWhenCustomInitParamsAndExistingHeaders() throws IOException, ServletException { mockLoggingContext(); 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 index 3376c922e4..ad7be95cec 100644 --- 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 @@ -16,21 +16,24 @@ package org.openecomp.sdc.logging.servlet.jaxrs; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; +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; -import org.testng.annotations.Test; /** - * Unit tests mutliple-option headers. + * 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() { @@ -39,7 +42,7 @@ public class HttpHeaderTest { Function<String, String> reader = createReader(key, value); HttpHeader header = new HttpHeader(key); - assertEquals(header.getAny(reader), value); + assertEquals(value, header.getAny(reader)); } @Test @@ -66,30 +69,26 @@ public class HttpHeaderTest { final String value = "1234"; Function<String, String> reader = createReader(lastKey, value); - HttpHeader header = new HttpHeader("First", "Second", lastKey); - assertEquals(header.getAny(reader), value); + HttpHeader header = new HttpHeader(KEY_FIRST, KEY_SECOND, lastKey); + assertEquals(value, header.getAny(reader)); } @Test public void valueReturnedWhenFirstHeaderMatches() { - final String firstKey = "First"; final String value = "1234"; - - Function<String, String> reader = createReader(firstKey, value); - HttpHeader header = new HttpHeader(firstKey, "Second", "Third"); - assertEquals(header.getAny(reader), value); + 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 middleKey = "Second"; final String value = "1234"; - - Function<String, String> reader = createReader(middleKey, value); - HttpHeader header = new HttpHeader("First", middleKey, "Third"); - assertEquals(header.getAny(reader), value); + 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) { 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 579f41c3e6..4098809e42 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 @@ -20,21 +20,22 @@ 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 java.lang.reflect.Method; -import java.lang.reflect.Proxy; import java.util.UUID; +import javax.servlet.http.HttpServletRequest; import javax.ws.rs.container.ContainerRequestContext; -import javax.ws.rs.container.ResourceInfo; 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.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.testng.PowerMockTestCase; -import org.testng.ITestResult; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.Test; +import org.powermock.modules.junit4.PowerMockRunner; + /** * Unit testing JAX-RS request filter. @@ -42,76 +43,39 @@ import org.testng.annotations.Test; * @author evitaliy * @since 19 Mar 2018 */ +@RunWith(PowerMockRunner.class) @PrepareForTest({LoggingContext.class, ContextData.class}) -public class LoggingRequestFilterTest extends PowerMockTestCase { - - private static final Class DEFAULT_RESOURCE_CLASS = MockResource.class; - private static final Method DEFAULT_RESOURCE_METHOD = MockResource.class.getDeclaredMethods()[0]; - private static final String DEFAULT_SERVICE_NAME = - formatServiceName(DEFAULT_RESOURCE_CLASS, DEFAULT_RESOURCE_METHOD); +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(); - private static String formatServiceName(Class resourceClass, Method resourceMethod) { - return resourceClass.getName() + "#" + resourceMethod.getName(); - } + @Rule + public TestName testName = new TestName(); /** * Verify all mocks after each test. */ - @AfterMethod - public void verifyMocks(ITestResult result) { + @After + public void verifyMocks() { try { PowerMock.verifyAll(); } catch (AssertionError e) { - throw new AssertionError("Expectations failed in: " + result.getMethod().getMethodName(), e); + throw new AssertionError("Expectations failed in " + testName.getMethodName() + "()", e); } } @Test - public void notHandledWhenNoMatchingResource() { - - PowerMock.mockStatic(LoggingContext.class); - PowerMock.replay(LoggingContext.class); - - new LoggingRequestFilter().filter(mockEmptyContainerRequestContext()); - } - - @Test - public void serviceNamePopulatedWhenThereIsMatchingResourceAndConcreteType() { - - mockContextDataBuilder(null, DEFAULT_SERVICE_NAME, null); - mockLoggingContext(); - - LoggingRequestFilter filter = new LoggingRequestFilter(); - filter.setResource(mockResource()); - - filter.filter(mockContainerRequestContext( - new RequestIdHeader(null), - new PartnerHeader(null))); - } - - @Test - public void serviceNamePopulatedWhenThereIsMatchingResourceAndJavaProxyType() throws NoSuchMethodException { - - Object proxyResource = Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), - new Class<?>[] {MockResource.class}, (proxy, method, arguments) -> null); - - final String serviceName = formatServiceName(MockResource.class, DEFAULT_RESOURCE_METHOD); + public void serviceNamePopulatedWhenThereIsMatchingResource() { - mockContextDataBuilder(null, serviceName, null); + mockContextDataBuilder(null, null, LoggingRequestFilter.formatServiceName(REQUEST_METHOD, REQUEST_URI)); mockLoggingContext(); LoggingRequestFilter filter = new LoggingRequestFilter(); - - Class<?> proxyClass = proxyResource.getClass(); - Method proxyMethod = - proxyClass.getMethod(DEFAULT_RESOURCE_METHOD.getName(), DEFAULT_RESOURCE_METHOD.getParameterTypes()); - - filter.setResource(mockResource(proxyClass, proxyMethod)); + filter.setHttpRequest(mockHttpRequest(true)); filter.filter(mockContainerRequestContext( new RequestIdHeader(null), @@ -119,20 +83,14 @@ public class LoggingRequestFilterTest extends PowerMockTestCase { } @Test - public void serviceNameIncludesProxyClassnameWhenJavaProxyTypeAndNoMatchingInterface() { - - Object proxyResource = Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), - new Class<?>[] {Comparable.class}, (proxy, method, arguments) -> null); + public void serviceNameDoesNotIncludeHttpMethodWhenHttpMethodDisabled() { - final String serviceName = formatServiceName(proxyResource.getClass(), DEFAULT_RESOURCE_METHOD); - - mockContextDataBuilder(null, serviceName, null); + mockContextDataBuilder(null, null, REQUEST_URI); mockLoggingContext(); LoggingRequestFilter filter = new LoggingRequestFilter(); - - Class<?> proxyClass = proxyResource.getClass(); - filter.setResource(mockResource(proxyClass, DEFAULT_RESOURCE_METHOD)); + filter.setHttpMethodInServiceName(false); + filter.setHttpRequest(mockHttpRequest(false)); filter.filter(mockContainerRequestContext( new RequestIdHeader(null), @@ -142,11 +100,12 @@ public class LoggingRequestFilterTest extends PowerMockTestCase { @Test public void partnerNamePopulatedWhenPresentInDefaultHeader() { - mockContextDataBuilder(null, DEFAULT_SERVICE_NAME, RANDOM_PARTNER_NAME); + mockContextDataBuilder(null, RANDOM_PARTNER_NAME, + LoggingRequestFilter.formatServiceName(REQUEST_METHOD, REQUEST_URI)); mockLoggingContext(); LoggingRequestFilter filter = new LoggingRequestFilter(); - filter.setResource(mockResource()); + filter.setHttpRequest(mockHttpRequest(true)); filter.filter(mockContainerRequestContext( new RequestIdHeader(null), @@ -157,11 +116,12 @@ public class LoggingRequestFilterTest extends PowerMockTestCase { public void partnerNamePopulatedWhenPresentInCustomHeader() { final String partnerHeader = "x-partner-header"; - mockContextDataBuilder(null, DEFAULT_SERVICE_NAME, RANDOM_PARTNER_NAME); + mockContextDataBuilder(null, RANDOM_PARTNER_NAME, + LoggingRequestFilter.formatServiceName(REQUEST_METHOD, REQUEST_URI)); mockLoggingContext(); LoggingRequestFilter filter = new LoggingRequestFilter(); - filter.setResource(mockResource()); + filter.setHttpRequest(mockHttpRequest(true)); filter.setPartnerNameHeaders(partnerHeader); filter.filter(mockContainerRequestContext( @@ -172,11 +132,12 @@ public class LoggingRequestFilterTest extends PowerMockTestCase { @Test public void requestIdPopulatedWhenPresentInDefaultHeader() { - mockContextDataBuilder(RANDOM_REQUEST_ID, DEFAULT_SERVICE_NAME, null); + mockContextDataBuilder(RANDOM_REQUEST_ID, null, + LoggingRequestFilter.formatServiceName(REQUEST_METHOD, REQUEST_URI)); mockLoggingContext(); LoggingRequestFilter filter = new LoggingRequestFilter(); - filter.setResource(mockResource()); + filter.setHttpRequest(mockHttpRequest(true)); filter.filter(mockContainerRequestContext( new RequestIdHeader(RANDOM_REQUEST_ID), @@ -187,35 +148,30 @@ public class LoggingRequestFilterTest extends PowerMockTestCase { public void requestIdPopulatedWhenPresentInCustomHeader() { final String requestIdHeader = "x-request-id"; - mockContextDataBuilder(RANDOM_REQUEST_ID, DEFAULT_SERVICE_NAME, null); + mockContextDataBuilder(RANDOM_REQUEST_ID, null, + LoggingRequestFilter.formatServiceName(REQUEST_METHOD, REQUEST_URI)); mockLoggingContext(); LoggingRequestFilter filter = new LoggingRequestFilter(); - filter.setResource(mockResource()); filter.setRequestIdHeaders(requestIdHeader); + filter.setHttpRequest(mockHttpRequest(true)); filter.filter(mockContainerRequestContext( new RequestIdHeader(requestIdHeader, RANDOM_REQUEST_ID), new PartnerHeader(null))); } - private ResourceInfo mockResource() { - return mockResource(DEFAULT_RESOURCE_CLASS, DEFAULT_RESOURCE_METHOD); - } + private HttpServletRequest mockHttpRequest(boolean includeMethod) { - private ResourceInfo mockResource(Class resourceType, Method resourceMethod) { - ResourceInfo resource = EasyMock.mock(ResourceInfo.class); - //noinspection unchecked - EasyMock.expect(resource.getResourceClass()).andReturn(resourceType); - EasyMock.expect(resource.getResourceMethod()).andReturn(resourceMethod); - EasyMock.replay(resource); - return resource; - } + HttpServletRequest servletRequest = EasyMock.mock(HttpServletRequest.class); + EasyMock.expect(servletRequest.getRequestURI()).andReturn(REQUEST_URI); - private ContainerRequestContext mockEmptyContainerRequestContext() { - ContainerRequestContext requestContext = EasyMock.mock(ContainerRequestContext.class); - EasyMock.replay(requestContext); - return requestContext; + if (includeMethod) { + EasyMock.expect(servletRequest.getMethod()).andReturn(REQUEST_METHOD); + } + + EasyMock.replay(servletRequest); + return servletRequest; } private ContainerRequestContext mockContainerRequestContext(Header... headers) { @@ -233,7 +189,7 @@ public class LoggingRequestFilterTest extends PowerMockTestCase { return requestContext; } - private void mockContextDataBuilder(String requestId, String serviceName, String partnerName) { + private void mockContextDataBuilder(String requestId, String partnerName, String serviceName) { ContextData.ContextDataBuilder mockBuilder = EasyMock.mock(ContextData.ContextDataBuilder.class); @@ -243,9 +199,7 @@ public class LoggingRequestFilterTest extends PowerMockTestCase { EasyMock.expect(mockBuilder.requestId(anyString())).andReturn(mockBuilder); } - if (serviceName != null) { - EasyMock.expect(mockBuilder.serviceName(serviceName)).andReturn(mockBuilder); - } + EasyMock.expect(mockBuilder.serviceName(serviceName)).andReturn(mockBuilder); if (partnerName != null) { EasyMock.expect(mockBuilder.partnerName(partnerName)).andReturn(mockBuilder); @@ -267,10 +221,10 @@ public class LoggingRequestFilterTest extends PowerMockTestCase { PowerMock.mockStatic(LoggingContext.class); LoggingContext.clear(); - EasyMock.expectLastCall().once(); + EasyMock.expectLastCall(); LoggingContext.put(anyObject(ContextData.class)); - EasyMock.expectLastCall().once(); + EasyMock.expectLastCall(); PowerMock.replay(LoggingContext.class); } @@ -307,20 +261,4 @@ public class LoggingRequestFilterTest extends PowerMockTestCase { super(key, value); } } - - private interface MockResource { - - @SuppressWarnings("EmptyMethod") - void process(); - } - - private static class MockResourceImpl implements MockResource { - - @Override - public void process() { - // no-op - } - } - - }
\ 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 37627d5d5a..710cf12b7e 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 @@ -19,15 +19,23 @@ package org.openecomp.sdc.logging.servlet.jaxrs; import static org.easymock.EasyMock.anyObject; import static org.easymock.EasyMock.anyString; +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; @@ -35,10 +43,7 @@ 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.testng.PowerMockTestCase; -import org.testng.ITestResult; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.Test; +import org.powermock.modules.junit4.PowerMockRunner; /** @@ -47,183 +52,267 @@ import org.testng.annotations.Test; * @author evitaliy * @since 19 Mar 2018 */ -@PrepareForTest({LoggingContext.class, LoggerFactory.class}) -public class LoggingResponseFilterTest extends PowerMockTestCase { +@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. */ - @AfterMethod - public void verifyMocks(ITestResult result) { + @After + public void verifyMocks() { try { PowerMock.verifyAll(); } catch (AssertionError e) { - throw new AssertionError("Expectations failed in: " + result.getMethod().getMethodName(), e); + throw new AssertionError("Expectations failed in " + testName.getMethodName() + "()", e); } } @Test - public void noAuditWhenAuditDisabled() { - mockLogger(false, AuditData.builder().build()); - mockLoggingContext(); - new LoggingResponseFilter().filter(mockDisabledRequestContext(), mockDisabledResponseContext()); - } - - private void mockLogger(boolean enabled, AuditData auditData, Consumer<Logger>... additionalMockings) { - - Logger logger = EasyMock.mock(Logger.class); + @PrepareForTest({LoggingContext.class, LoggerFactory.class}) + public void noAuditWhenNoMatchingResource() { - EasyMock.expect(logger.isAuditEnabled()).andReturn(enabled).atLeastOnce(); - - if (enabled) { - logger.audit(EasyMock.cmp(auditData, new AuditDataComparator(), LogicalOperator.EQUAL)); + PowerMock.mockStatic(LoggerFactory.class); + mockFilterLogger(logger -> { + logger.debug(anyString()); EasyMock.expectLastCall(); - } + }); + PowerMock.replay(LoggerFactory.class); - for (Consumer<Logger> mocking : additionalMockings) { - mocking.accept(logger); - } + LoggingResponseFilter responseFilter = new LoggingResponseFilter(); + responseFilter.filter(mockDisabledRequestContext(), mockDisabledResponseContext()); + } - EasyMock.replay(logger); + @Test + @PrepareForTest({LoggingContext.class, LoggerFactory.class}) + public void noAuditWhenNullResource() { PowerMock.mockStatic(LoggerFactory.class); - LoggerFactory.getLogger(LoggingResponseFilter.class); - PowerMock.expectLastCall().andReturn(logger); + mockFilterLogger(logger -> { + logger.debug(anyString()); + EasyMock.expectLastCall(); + }); PowerMock.replay(LoggerFactory.class); - } - private void mockLoggingContext() { - PowerMock.mockStatic(LoggingContext.class); - LoggingContext.clear(); - EasyMock.expectLastCall().once(); - PowerMock.replay(LoggingContext.class); + LoggingResponseFilter responseFilter = new LoggingResponseFilter(); + responseFilter.setResource(null); + responseFilter.filter(mockDisabledRequestContext(), mockDisabledResponseContext()); } - private ContainerRequestContext mockDisabledRequestContext() { - ContainerRequestContext requestContext = EasyMock.mock(ContainerRequestContext.class); - EasyMock.replay(requestContext); - return requestContext; - } + @Test + @PrepareForTest({LoggingContext.class, LoggerFactory.class}) + public void noAuditWhenAuditDisabled() { - private ContainerResponseContext mockDisabledResponseContext() { - ContainerResponseContext responseContext = EasyMock.mock(ContainerResponseContext.class); - EasyMock.replay(responseContext); - return responseContext; + PowerMock.mockStatic(LoggerFactory.class); + mockFilterLogger(); + mockResourceLogger(false, AuditData.builder().build()); + PowerMock.replay(LoggerFactory.class); + + LoggingResponseFilter responseFilter = new LoggingResponseFilter(); + responseFilter.setResource(mockResource()); + responseFilter.filter(mockDisabledRequestContext(), mockDisabledResponseContext()); } @Test + @PrepareForTest({LoggingContext.class, LoggerFactory.class}) public void startTimeReadWhenPresentInRequestContext() { - final String clientIp = "10.56.56.10"; + final String clientIp = "IP1"; final long startTime = 12345L; final Response.Status ok = Response.Status.OK; - mockLogger(true, buildAuditData(startTime, clientIp, ok, StatusCode.COMPLETE)); + PowerMock.mockStatic(LoggerFactory.class); + mockResourceLogger(true, buildAuditData(startTime, clientIp, ok, StatusCode.COMPLETE)); + mockFilterLogger(); + PowerMock.replay(LoggerFactory.class); - mockLoggingContext(); LoggingResponseFilter filter = new LoggingResponseFilter(); + filter.setResource(mockResource()); filter.setHttpRequest(mockHttpRequest(clientIp)); filter.filter(mockRequestContext(startTime), mockResponseContext(ok)); } - 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 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; - } - @Test + @PrepareForTest({LoggingContext.class, LoggerFactory.class}) public void startTimeZeroWhenNotPresentInRequestContext() { - final String clientIp = "10.56.56.12"; + final String clientIp = "IP2"; final Response.Status ok = Response.Status.OK; AuditData expectedAuditData = buildAuditData(0, clientIp, ok, StatusCode.COMPLETE); - mockLogger(true, expectedAuditData, logger -> { + PowerMock.mockStatic(LoggerFactory.class); + mockResourceLogger(true, expectedAuditData); + mockFilterLogger(logger -> { logger.error(anyString(), anyObject(Object[].class)); EasyMock.expectLastCall(); }); + PowerMock.replay(LoggerFactory.class); - mockLoggingContext(); 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 = "10.56.56.13"; + final String clientIp = "IP3"; final Response.Status accepted = Response.Status.ACCEPTED; AuditData expectedAuditData = buildAuditData(0, clientIp, accepted, StatusCode.COMPLETE); - mockLogger(true, expectedAuditData, logger -> { - logger.error(anyString(), new Object[] {anyString(), anyString()}); + PowerMock.mockStatic(LoggerFactory.class); + mockFilterLogger(logger -> { + logger.error(anyString(), anyString(), anyString(), anyObject()); EasyMock.expectLastCall(); }); + mockResourceLogger(true, expectedAuditData); + PowerMock.replay(LoggerFactory.class); - mockLoggingContext(); 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 = "10.56.56.13"; + final String clientIp = "IP13"; final long startTime = 88668603L; AuditData expectedAuditData = buildAuditData(startTime, clientIp, error, StatusCode.ERROR); - mockLogger(true, expectedAuditData); + PowerMock.mockStatic(LoggerFactory.class); + mockResourceLogger(true, expectedAuditData); + mockFilterLogger(); + PowerMock.replay(LoggerFactory.class); - mockLoggingContext(); 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(), one.getResponseDescription()) && one.getStartTime() == two - .getStartTime() && Objects.equals(one.getStatusCode(), two.getStatusCode())) { + 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; } @@ -231,4 +320,9 @@ public class LoggingResponseFilterTest extends PowerMockTestCase { return -1; } } + + 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-core/pom.xml b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/pom.xml index c231facfce..b30857620a 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/pom.xml +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/pom.xml @@ -34,9 +34,9 @@ <scope>provided</scope> </dependency> <dependency> - <groupId>org.testng</groupId> - <artifactId>testng</artifactId> - <version>${testng.version}</version> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> @@ -59,7 +59,7 @@ </dependency> <dependency> <groupId>org.powermock</groupId> - <artifactId>powermock-module-testng</artifactId> + <artifactId>powermock-module-junit4</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency> diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/RoutingTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/RoutingTest.java index adc1d8e71d..7be90264d6 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/RoutingTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/RoutingTest.java @@ -4,9 +4,9 @@ * 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. @@ -21,132 +21,130 @@ import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.sift.SiftingAppender; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.AppenderBase; -import org.openecomp.sdc.logging.logback.EventTypeDiscriminator; -import org.openecomp.sdc.logging.slf4j.Markers; -import org.slf4j.LoggerFactory; -import org.testng.Assert; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Predicate; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.openecomp.sdc.logging.logback.EventTypeDiscriminator; +import org.openecomp.sdc.logging.slf4j.Markers; +import org.slf4j.LoggerFactory; /** - * TODO: Add more negative tests + * Routing of events to the correct appender. * * @author EVITALIY * @since 17/08/2016. */ public class RoutingTest { - private static final String ERROR = "Error"; - private static final String DEBUG = "Debug"; - private static final String AUDIT = "Audit"; - private static final String METRICS = "Metrics"; - - private Logger logger; - private final Map<String, TestAppender> result = new ConcurrentHashMap<>(); - - @BeforeClass - public void setUp() { - - SiftingAppender appender = new SiftingAppender(); - - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - discriminator.start(); - - appender.setDiscriminator(discriminator); - appender.setAppenderFactory((context, discriminatingValue) -> - result.computeIfAbsent(discriminatingValue, f -> { - TestAppender tmp = new TestAppender(); - tmp.start(); - return tmp; - })); - - appender.start(); - - logger = (Logger) LoggerFactory.getLogger(RoutingTest.class.getName()); - // prevent from writing into appenders attached via parent loggers - logger.setAdditive(false); - logger.addAppender(appender); - logger.setLevel(Level.DEBUG); - } - - @Test - public void testWarning() { - String msg = "This is a test warning"; - logger.warn(msg); - TestAppender appender = result.get(ERROR); - Assert.assertTrue(appender.contains((event) -> - Level.WARN.equals(event.getLevel()) && msg.equals(event.getFormattedMessage()))); - } - - @Test - public void testError() { - String msg = "This is a test error"; - logger.error(msg); - TestAppender appender = result.get(ERROR); - Assert.assertTrue(appender.contains((event) -> - Level.ERROR.equals(event.getLevel()) && msg.equals(event.getFormattedMessage()))); - } - - @Test - public void testDebug() { - String msg = "This is a test debug"; - logger.debug(msg); - TestAppender appender = result.get(DEBUG); - Assert.assertTrue(appender.contains((event) -> - Level.DEBUG.equals(event.getLevel()) && msg.equals(event.getFormattedMessage()))); - } - - @Test - public void testInfo() { - String msg = "This is a test info"; - logger.info(msg); - TestAppender appender = result.get(ERROR); - Assert.assertTrue(appender.contains((event) -> - Level.INFO.equals(event.getLevel()) && msg.equals(event.getFormattedMessage()))); - } - - @Test - public void testAudit() { - String msg = "This is a test audit"; - logger.info(Markers.AUDIT, msg); - TestAppender appender = result.get(AUDIT); - Assert.assertTrue(appender.contains((event) -> - Level.INFO.equals(event.getLevel()) && msg.equals(event.getFormattedMessage()))); - } - - @Test - public void testMetrics() { - String msg = "This is a test metrics"; - logger.info(Markers.METRICS, msg); - TestAppender appender = result.get(METRICS); - Assert.assertTrue(appender.contains((event) -> - Level.INFO.equals(event.getLevel()) && msg.equals(event.getFormattedMessage()))); - } - - /** - * An appender that just accumulates messages in a list and enables to inspect them - * - * @author EVITALIY - * @since 17/08/2016. - */ - private static class TestAppender extends AppenderBase<ILoggingEvent> { - - private final List<ILoggingEvent> events = Collections.synchronizedList(new ArrayList<>(10)); - - @Override - protected void append(ILoggingEvent event) { - this.events.add(event); + private static final String ERROR = "Error"; + private static final String DEBUG = "Debug"; + private static final String AUDIT = "Audit"; + private static final String METRICS = "Metrics"; + private static final Map<String, TestAppender> result = new ConcurrentHashMap<>(); + private static Logger logger; + + @BeforeClass + public static void setUp() { + + SiftingAppender appender = new SiftingAppender(); + + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + discriminator.start(); + + appender.setDiscriminator(discriminator); + appender.setAppenderFactory((context, + discriminatingValue) -> result.computeIfAbsent(discriminatingValue, f -> { + TestAppender tmp = new TestAppender(); + tmp.start(); + return tmp; + })); + + appender.start(); + + logger = (Logger) LoggerFactory.getLogger(RoutingTest.class.getName()); + // prevent from writing into appenders attached via parent loggers + logger.setAdditive(false); + logger.addAppender(appender); + logger.setLevel(Level.DEBUG); } - boolean contains(Predicate<ILoggingEvent> predicate) { - return events.stream().anyMatch(predicate); + @Test + public void testWarning() { + String msg = "This is a test warning"; + logger.warn(msg); + TestAppender appender = result.get(ERROR); + Assert.assertTrue(appender.contains( + (event) -> Level.WARN.equals(event.getLevel()) && msg.equals(event.getFormattedMessage()))); + } + + @Test + public void testError() { + String msg = "This is a test error"; + logger.error(msg); + TestAppender appender = result.get(ERROR); + Assert.assertTrue(appender.contains( + (event) -> Level.ERROR.equals(event.getLevel()) && msg.equals(event.getFormattedMessage()))); + } + + @Test + public void testDebug() { + String msg = "This is a test debug"; + logger.debug(msg); + TestAppender appender = result.get(DEBUG); + Assert.assertTrue(appender.contains( + (event) -> Level.DEBUG.equals(event.getLevel()) && msg.equals(event.getFormattedMessage()))); + } + + @Test + public void testInfo() { + String msg = "This is a test info"; + logger.info(msg); + TestAppender appender = result.get(ERROR); + Assert.assertTrue(appender.contains( + (event) -> Level.INFO.equals(event.getLevel()) && msg.equals(event.getFormattedMessage()))); + } + + @Test + public void testAudit() { + String msg = "This is a test audit"; + logger.info(Markers.AUDIT, msg); + TestAppender appender = result.get(AUDIT); + Assert.assertTrue(appender.contains( + (event) -> Level.INFO.equals(event.getLevel()) && msg.equals(event.getFormattedMessage()))); + } + + @Test + public void testMetrics() { + String msg = "This is a test metrics"; + logger.info(Markers.METRICS, msg); + TestAppender appender = result.get(METRICS); + Assert.assertTrue(appender.contains( + (event) -> Level.INFO.equals(event.getLevel()) && msg.equals(event.getFormattedMessage()))); + } + + /** + * An appender that just accumulates messages in a list and enables to inspect them. + * + * @author EVITALIY + * @since 17/08/2016. + */ + private static class TestAppender extends AppenderBase<ILoggingEvent> { + + private final List<ILoggingEvent> events = Collections.synchronizedList(new ArrayList<>(10)); + + @Override + protected void append(ILoggingEvent event) { + this.events.add(event); + } + + boolean contains(Predicate<ILoggingEvent> predicate) { + return events.stream().anyMatch(predicate); + } } - } } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressCacheTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressCacheTest.java index f75b634177..512de191c0 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressCacheTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressCacheTest.java @@ -16,15 +16,16 @@ package org.openecomp.sdc.logging.context; -import static org.testng.Assert.assertTrue; +import static org.junit.Assert.assertTrue; import java.net.InetAddress; import java.net.UnknownHostException; import org.easymock.EasyMock; +import org.junit.Test; +import org.junit.runner.RunWith; import org.powermock.api.easymock.PowerMock; import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.testng.PowerMockTestCase; -import org.testng.annotations.Test; +import org.powermock.modules.junit4.PowerMockRunner; /** * Retrieval and caching of host address. @@ -33,7 +34,8 @@ import org.testng.annotations.Test; * @since 28 Mar 2018 */ @PrepareForTest(InetAddress.class) -public class HostAddressCacheTest extends PowerMockTestCase { +@RunWith(PowerMockRunner.class) +public class HostAddressCacheTest { @Test public void hostAddressIsAlwaysPopulated() { diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/InstanceIdTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/InstanceIdTest.java index 984cb3bade..7afa3a2512 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/InstanceIdTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/InstanceIdTest.java @@ -16,9 +16,9 @@ package org.openecomp.sdc.logging.context; -import static org.testng.Assert.assertNotNull; +import static org.junit.Assert.assertNotNull; -import org.testng.annotations.Test; +import org.junit.Test; /** * Retrieval of instance ID. diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/logback/EventTypeDiscriminatorTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/logback/EventTypeDiscriminatorTest.java index 050c583346..2a71f72063 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/logback/EventTypeDiscriminatorTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/logback/EventTypeDiscriminatorTest.java @@ -16,15 +16,17 @@ package org.openecomp.sdc.logging.logback; +import static org.junit.Assert.assertEquals; + import ch.qos.logback.classic.Level; import ch.qos.logback.classic.spi.LoggingEvent; +import org.junit.Test; import org.openecomp.sdc.logging.slf4j.Markers; import org.slf4j.MarkerFactory; -import org.testng.annotations.Test; - -import static org.testng.Assert.assertEquals; /** + * Test categorizing of events. + * * @author EVITALIY * @since 17/08/2016. */ @@ -37,7 +39,7 @@ public class EventTypeDiscriminatorTest { public void testGetDefaultDiscriminatingValue() { EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); LoggingEvent event = new LoggingEvent(); - assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); + assertEquals(DEBUG, discriminator.getDiscriminatingValue(event)); } @Test @@ -45,7 +47,7 @@ public class EventTypeDiscriminatorTest { EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); LoggingEvent event = new LoggingEvent(); event.setLevel(Level.ERROR); - assertEquals(discriminator.getDiscriminatingValue(event), ERROR); + assertEquals(ERROR, discriminator.getDiscriminatingValue(event)); } @Test @@ -53,7 +55,7 @@ public class EventTypeDiscriminatorTest { EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); LoggingEvent event = new LoggingEvent(); event.setLevel(Level.WARN); - assertEquals(discriminator.getDiscriminatingValue(event), ERROR); + assertEquals(ERROR, discriminator.getDiscriminatingValue(event)); } @Test @@ -61,7 +63,7 @@ public class EventTypeDiscriminatorTest { EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); LoggingEvent event = new LoggingEvent(); event.setLevel(Level.INFO); - assertEquals(discriminator.getDiscriminatingValue(event), ERROR); + assertEquals(ERROR, discriminator.getDiscriminatingValue(event)); } @Test @@ -69,7 +71,7 @@ public class EventTypeDiscriminatorTest { EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); LoggingEvent event = new LoggingEvent(); event.setLevel(Level.TRACE); - assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); + assertEquals(DEBUG, discriminator.getDiscriminatingValue(event)); } @Test @@ -78,7 +80,7 @@ public class EventTypeDiscriminatorTest { LoggingEvent event = new LoggingEvent(); event.setLevel(Level.ERROR); event.setMarker(Markers.AUDIT); - assertEquals(discriminator.getDiscriminatingValue(event), ERROR); + assertEquals(ERROR, discriminator.getDiscriminatingValue(event)); } @Test @@ -87,7 +89,7 @@ public class EventTypeDiscriminatorTest { LoggingEvent event = new LoggingEvent(); event.setLevel(Level.ERROR); event.setMarker(Markers.METRICS); - assertEquals(discriminator.getDiscriminatingValue(event), ERROR); + assertEquals(ERROR, discriminator.getDiscriminatingValue(event)); } @Test @@ -96,7 +98,7 @@ public class EventTypeDiscriminatorTest { LoggingEvent event = new LoggingEvent(); event.setLevel(Level.WARN); event.setMarker(Markers.AUDIT); - assertEquals(discriminator.getDiscriminatingValue(event), ERROR); + assertEquals(ERROR, discriminator.getDiscriminatingValue(event)); } @Test @@ -105,7 +107,7 @@ public class EventTypeDiscriminatorTest { LoggingEvent event = new LoggingEvent(); event.setLevel(Level.WARN); event.setMarker(Markers.METRICS); - assertEquals(discriminator.getDiscriminatingValue(event), ERROR); + assertEquals(ERROR, discriminator.getDiscriminatingValue(event)); } @Test @@ -114,7 +116,7 @@ public class EventTypeDiscriminatorTest { LoggingEvent event = new LoggingEvent(); event.setLevel(Level.DEBUG); event.setMarker(Markers.AUDIT); - assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); + assertEquals(DEBUG, discriminator.getDiscriminatingValue(event)); } @Test @@ -123,7 +125,7 @@ public class EventTypeDiscriminatorTest { LoggingEvent event = new LoggingEvent(); event.setLevel(Level.DEBUG); event.setMarker(Markers.METRICS); - assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); + assertEquals(DEBUG, discriminator.getDiscriminatingValue(event)); } @Test @@ -132,7 +134,7 @@ public class EventTypeDiscriminatorTest { LoggingEvent event = new LoggingEvent(); event.setLevel(Level.TRACE); event.setMarker(Markers.AUDIT); - assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); + assertEquals(DEBUG, discriminator.getDiscriminatingValue(event)); } @Test @@ -141,7 +143,7 @@ public class EventTypeDiscriminatorTest { LoggingEvent event = new LoggingEvent(); event.setLevel(Level.TRACE); event.setMarker(Markers.METRICS); - assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); + assertEquals(DEBUG, discriminator.getDiscriminatingValue(event)); } @Test @@ -150,7 +152,7 @@ public class EventTypeDiscriminatorTest { LoggingEvent event = new LoggingEvent(); event.setLevel(Level.ERROR); event.setMarker(MarkerFactory.getMarker("Dummy")); - assertEquals(discriminator.getDiscriminatingValue(event), ERROR); + assertEquals(ERROR, discriminator.getDiscriminatingValue(event)); } @Test @@ -159,7 +161,7 @@ public class EventTypeDiscriminatorTest { LoggingEvent event = new LoggingEvent(); event.setLevel(Level.WARN); event.setMarker(MarkerFactory.getMarker("Dummy")); - assertEquals(discriminator.getDiscriminatingValue(event), ERROR); + assertEquals(ERROR, discriminator.getDiscriminatingValue(event)); } @Test @@ -168,7 +170,7 @@ public class EventTypeDiscriminatorTest { LoggingEvent event = new LoggingEvent(); event.setLevel(Level.DEBUG); event.setMarker(MarkerFactory.getMarker("Dummy")); - assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); + assertEquals(DEBUG, discriminator.getDiscriminatingValue(event)); } @Test @@ -177,7 +179,7 @@ public class EventTypeDiscriminatorTest { LoggingEvent event = new LoggingEvent(); event.setLevel(Level.TRACE); event.setMarker(MarkerFactory.getMarker("Dummy")); - assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); + assertEquals(DEBUG, discriminator.getDiscriminatingValue(event)); } @Test @@ -186,7 +188,7 @@ public class EventTypeDiscriminatorTest { LoggingEvent event = new LoggingEvent(); event.setLevel(Level.INFO); event.setMarker(MarkerFactory.getMarker("Dummy")); - assertEquals(discriminator.getDiscriminatingValue(event), ERROR); + assertEquals(ERROR, discriminator.getDiscriminatingValue(event)); } @Test @@ -195,7 +197,7 @@ public class EventTypeDiscriminatorTest { LoggingEvent event = new LoggingEvent(); event.setLevel(Level.INFO); event.setMarker(Markers.AUDIT); - assertEquals(discriminator.getDiscriminatingValue(event), "Audit"); + assertEquals("Audit", discriminator.getDiscriminatingValue(event)); } @Test @@ -204,7 +206,7 @@ public class EventTypeDiscriminatorTest { LoggingEvent event = new LoggingEvent(); event.setLevel(Level.INFO); event.setMarker(Markers.METRICS); - assertEquals(discriminator.getDiscriminatingValue(event), "Metrics"); + assertEquals("Metrics", discriminator.getDiscriminatingValue(event)); } @Test diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/CallableContextPropagationTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/CallableContextPropagationTest.java index 897a87193f..e8aaa47649 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/CallableContextPropagationTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/CallableContextPropagationTest.java @@ -16,6 +16,8 @@ package org.openecomp.sdc.logging.slf4j; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_EMPTY; import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_EXCEPTION_FROM_INNER; import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_INNER_RUN; @@ -32,7 +34,6 @@ import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.IS_SU import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.assertContextEmpty; import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.assertContextFields; import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.putUniqueValues; -import static org.testng.Assert.assertTrue; import java.util.Map; import java.util.concurrent.Callable; @@ -41,8 +42,9 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import org.junit.Before; +import org.junit.Test; import org.openecomp.sdc.logging.spi.LoggingContextService; -import org.testng.annotations.Test; /** * Tests propagation of logging fields to Callable via the logging service. @@ -55,7 +57,12 @@ public class CallableContextPropagationTest { private final LoggingContextService ctxService = new SLF4JLoggingServiceProvider(); - @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + @Before + public void checkSuitableLogbackVersion() { + assumeTrue(IS_SUITABLE_LOGBACK_VERSION); + } + + @Test public void testContextPropagated() throws Exception { Map<ContextField, String> values = putUniqueValues(); @@ -63,16 +70,16 @@ public class CallableContextPropagationTest { // pass the callable to the context service first execute(ctxService.copyToCallable(() -> { - assertContextFields(values, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(EXPECT_PROPAGATED_TO_CHILD, values); complete.set(true); return null; })); - assertContextFields(values, EXPECT_RETAINED_IN_CURRENT); - assertTrue(complete.get(), EXPECT_INNER_RUN); + assertContextFields(EXPECT_RETAINED_IN_CURRENT, values); + assertTrue(EXPECT_INNER_RUN, complete.get()); } - @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + @Test public void testContextReplacement() throws Exception { Map<ContextField, String> innerValues = putUniqueValues(); @@ -80,7 +87,7 @@ public class CallableContextPropagationTest { // should run with the context of main thread Callable inner = ctxService.copyToCallable(() -> { - assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(EXPECT_PROPAGATED_TO_CHILD, innerValues); innerComplete.set(true); return null; }); @@ -90,17 +97,17 @@ public class CallableContextPropagationTest { execute(() -> { Map<ContextField, String> outerValues = putUniqueValues(); inner.call(); - assertContextFields(outerValues, EXPECT_REPLACED_WITH_STORED); + assertContextFields(EXPECT_REPLACED_WITH_STORED, outerValues); outerComplete.set(true); return null; }); - assertContextFields(innerValues, EXPECT_RETAINED_IN_CURRENT); - assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); - assertTrue(innerComplete.get(), EXPECT_INNER_RUN); + assertContextFields(EXPECT_RETAINED_IN_CURRENT, innerValues); + assertTrue(EXPECT_OUTER_RUN, outerComplete.get()); + assertTrue(EXPECT_INNER_RUN, innerComplete.get()); } - @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + @Test public void testContextRemainsEmpty() throws Exception { ctxService.clear(); @@ -114,10 +121,10 @@ public class CallableContextPropagationTest { })); assertContextEmpty(EXPECT_EMPTY); - assertTrue(complete.get(), EXPECT_INNER_RUN); + assertTrue(EXPECT_INNER_RUN, complete.get()); } - @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + @Test public void testContextCleanedUp() throws Exception { Map<ContextField, String> innerValues = putUniqueValues(); @@ -125,7 +132,7 @@ public class CallableContextPropagationTest { AtomicBoolean innerComplete = new AtomicBoolean(false); // should run with the context of main thread Callable inner = ctxService.copyToCallable((() -> { - assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(EXPECT_PROPAGATED_TO_CHILD, innerValues); innerComplete.set(true); return null; })); @@ -140,12 +147,12 @@ public class CallableContextPropagationTest { return null; }); - assertContextFields(innerValues, EXPECT_RETAINED_IN_PARENT); - assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); - assertTrue(innerComplete.get(), EXPECT_INNER_RUN); + assertContextFields(EXPECT_RETAINED_IN_PARENT, innerValues); + assertTrue(EXPECT_OUTER_RUN, outerComplete.get()); + assertTrue(EXPECT_INNER_RUN, innerComplete.get()); } - @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + @Test public void testCleanupAfterError() throws Exception { Map<ContextField, String> innerValues = putUniqueValues(); @@ -153,7 +160,7 @@ public class CallableContextPropagationTest { // should run with the context of main thread AtomicBoolean innerComplete = new AtomicBoolean(false); Callable inner = ctxService.copyToCallable(() -> { - assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(EXPECT_PROPAGATED_TO_CHILD, innerValues); innerComplete.set(true); throw new IllegalArgumentException(); }); @@ -164,24 +171,24 @@ public class CallableContextPropagationTest { execute(() -> { Map<ContextField, String> outerValues = putUniqueValues(); - assertContextFields(outerValues, EXPECT_POPULATED); + assertContextFields(EXPECT_POPULATED, outerValues); try { inner.call(); } catch (IllegalArgumentException e) { exceptionThrown.set(true); } finally { - assertContextFields(outerValues, EXPECT_REVERTED_ON_EXCEPTION); + assertContextFields(EXPECT_REVERTED_ON_EXCEPTION, outerValues); outerComplete.set(true); } return null; }); - assertContextFields(innerValues, EXPECT_RETAINED_IN_PARENT); - assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); - assertTrue(innerComplete.get(), EXPECT_INNER_RUN); - assertTrue(exceptionThrown.get(), EXPECT_EXCEPTION_FROM_INNER); + assertContextFields(EXPECT_RETAINED_IN_PARENT, innerValues); + assertTrue(EXPECT_OUTER_RUN, outerComplete.get()); + assertTrue(EXPECT_INNER_RUN, innerComplete.get()); + assertTrue(EXPECT_EXCEPTION_FROM_INNER, exceptionThrown.get()); } private void execute(Callable<Object> callable) throws Exception { diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/ContextPropagationTestHelper.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/ContextPropagationTestHelper.java index fccc6ba59a..eb74e2a05c 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/ContextPropagationTestHelper.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/ContextPropagationTestHelper.java @@ -16,8 +16,8 @@ package org.openecomp.sdc.logging.slf4j; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import java.util.EnumMap; import java.util.Map; @@ -44,6 +44,10 @@ class ContextPropagationTestHelper { static final String EXPECT_REVERTED_ON_EXCEPTION = "Expected context data to be reverted even in case of exception"; static final String EXPECT_EXCEPTION_FROM_INNER = "Expected the inner class to throw exception"; + private ContextPropagationTestHelper() { + // prevent instantiation + } + static Map<ContextField, String> putUniqueValues() { Map<ContextField, String> values = new EnumMap<>(ContextField.class); @@ -59,17 +63,17 @@ class ContextPropagationTestHelper { return values; } - static void assertContextFields(Map<ContextField, String> values, String error) { + static void assertContextFields(String error, Map<ContextField, String> values) { for (ContextField f : ContextField.values()) { - assertEquals(MDC.get(f.asKey()), values.get(f), error); + assertEquals(error, MDC.get(f.asKey()), values.get(f)); } } static void assertContextEmpty(String error) { for (ContextField key : ContextField.values()) { - assertNull(MDC.get(key.asKey()), error); + assertNull(error, MDC.get(key.asKey())); } } } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/ContextTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/ContextTest.java index bed5cec20e..192e66031a 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/ContextTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/ContextTest.java @@ -16,13 +16,13 @@ package org.openecomp.sdc.logging.slf4j; -import static org.testng.Assert.assertEquals; +import static org.junit.Assert.assertEquals; import java.util.EnumMap; import java.util.Map; +import org.junit.After; +import org.junit.Test; import org.slf4j.MDC; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.Test; /** * Unit-tests context replacement on MDC. @@ -36,7 +36,7 @@ public class ContextTest { private static final String KEY = FIELD.asKey(); private static final String VALUE = "service-name-value"; - @AfterMethod + @After public void clearMdc() { MDC.clear(); } @@ -49,7 +49,7 @@ public class ContextTest { MDC.put(KEY, "modified-" + VALUE); context.replace(); - assertEquals(MDC.get(KEY), VALUE); + assertEquals(VALUE, MDC.get(KEY)); } @Test @@ -57,8 +57,8 @@ public class ContextTest { MDC.put(KEY, VALUE); Map<ContextField, String> old = new Context().replace(); - assertEquals(old.size(), 1); - assertEquals(old.get(FIELD), VALUE); + assertEquals(1, old.size()); + assertEquals(VALUE, old.get(FIELD)); } @Test @@ -68,6 +68,6 @@ public class ContextTest { Map<ContextField, String> values = new EnumMap<>(ContextField.class); values.put(FIELD, VALUE); context.revert(values); - assertEquals(MDC.get(KEY), VALUE); + assertEquals(VALUE, MDC.get(KEY)); } } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/GlobalContextProviderTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/GlobalContextProviderTest.java index d1e4be38a6..e58c6e9b55 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/GlobalContextProviderTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/GlobalContextProviderTest.java @@ -16,10 +16,10 @@ package org.openecomp.sdc.logging.slf4j; -import static org.testng.Assert.assertNotNull; +import static org.junit.Assert.assertNotNull; import java.util.Map; -import org.testng.annotations.Test; +import org.junit.Test; /** * Tests data supplied by the global logging context. diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggerFactoryTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggerFactoryTest.java index e04af23a91..ad3c27c247 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggerFactoryTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggerFactoryTest.java @@ -16,13 +16,15 @@ package org.openecomp.sdc.logging.slf4j; +import static org.junit.Assert.assertEquals; + +import org.junit.Test; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; -import org.testng.annotations.Test; - -import static org.testng.Assert.assertEquals; /** + * Make sure the logger factory returns correct loggers. + * * @author EVITALIY * @since 08 Jan 18 */ @@ -33,23 +35,23 @@ public class LoggerFactoryTest { @Test public void returnSlf4jLoggerWhenGetByClass() { Logger logger = LoggerFactory.getLogger(LoggerFactoryTest.class); - assertEquals(logger.getClass(), SLF4JLoggerWrapper.class); - assertEquals(logger.getName(), CLASS_NAME); + assertEquals(SLF4JLoggerWrapper.class, logger.getClass()); + assertEquals(CLASS_NAME, logger.getName()); } @Test public void returnSlf4jLoggerWhenGetByName() { Logger logger = LoggerFactory.getLogger(CLASS_NAME); - assertEquals(logger.getClass(), SLF4JLoggerWrapper.class); - assertEquals(logger.getName(), CLASS_NAME); + assertEquals(SLF4JLoggerWrapper.class, logger.getClass()); + assertEquals(CLASS_NAME, logger.getName()); } - @Test(expectedExceptions = NullPointerException.class) + @Test(expected = NullPointerException.class) public void throwNpeWhenGetByNameWithNull() { LoggerFactory.getLogger((String) null); } - @Test(expectedExceptions = NullPointerException.class) + @Test(expected = NullPointerException.class) public void throwNpeWhenGetByClassWithNull() { LoggerFactory.getLogger((Class<LoggerFactoryTest>) null); } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggingContextTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggingContextTest.java index c2cef88899..1e7cfe834f 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggingContextTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggingContextTest.java @@ -16,16 +16,16 @@ package org.openecomp.sdc.logging.slf4j; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertNull; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import java.util.UUID; +import org.junit.After; +import org.junit.Test; import org.openecomp.sdc.logging.api.ContextData; import org.openecomp.sdc.logging.api.LoggingContext; import org.slf4j.MDC; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.Test; /** * Unit-testing logging context service via its facade. @@ -35,27 +35,27 @@ import org.testng.annotations.Test; */ public class LoggingContextTest { - @AfterMethod + @After public void clearMdc() { MDC.clear(); } @Test public void returnMdcWrapperWhenToRunnableCalled() { - assertEquals(LoggingContext.copyToRunnable(() -> { }).getClass(), MDCRunnableWrapper.class); + assertEquals(MDCRunnableWrapper.class, LoggingContext.copyToRunnable(() -> { }).getClass()); } - @Test(expectedExceptions = NullPointerException.class) + @Test(expected = NullPointerException.class) public void throwNpeWhenToRunnableWithNull() { LoggingContext.copyToRunnable(null); } @Test public void returnMdcWrapperWhenToCallableCalled() { - assertEquals(LoggingContext.copyToCallable(() -> "").getClass(), MDCCallableWrapper.class); + assertEquals(MDCCallableWrapper.class, LoggingContext.copyToCallable(() -> "").getClass()); } - @Test(expectedExceptions = NullPointerException.class) + @Test(expected = NullPointerException.class) public void throwNpeWhenToCallableWithNull() { LoggingContext.copyToCallable(null); } @@ -82,7 +82,7 @@ public class LoggingContextTest { MDC.put(randomKey, randomValue); LoggingContext.clear(); - assertEquals(MDC.get(randomKey), randomValue); + assertEquals(randomValue, MDC.get(randomKey)); } @Test @@ -94,7 +94,7 @@ public class LoggingContextTest { assertEquals(random, MDC.get(ContextField.SERVICE_NAME.asKey())); } - @Test(expectedExceptions = NullPointerException.class) + @Test(expected = NullPointerException.class) public void throwNpeWhenContextDataNull() { LoggingContext.put(null); } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RequestContextProviderTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RequestContextProviderTest.java index a2d36ef085..c4c1677637 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RequestContextProviderTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RequestContextProviderTest.java @@ -16,13 +16,15 @@ package org.openecomp.sdc.logging.slf4j; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; -import java.util.HashMap; +import java.util.Collections; +import java.util.EnumMap; +import java.util.Map; +import org.junit.Test; import org.openecomp.sdc.logging.api.ContextData; -import org.testng.annotations.Test; /** * Unit-test retrieving values from client-provided request data. @@ -43,7 +45,7 @@ public class RequestContextProviderTest { final String service = "supplied-service-name"; RequestContextProvider provider = RequestContextProvider.from(ContextData.builder().serviceName(service).build()); - assertEquals(provider.values().get(ContextField.SERVICE_NAME), service); + assertEquals(service, provider.values().get(ContextField.SERVICE_NAME)); } @Test @@ -51,7 +53,7 @@ public class RequestContextProviderTest { final String partner = "supplied-partner-name"; RequestContextProvider provider = RequestContextProvider.from(ContextData.builder().partnerName(partner).build()); - assertEquals(provider.values().get(ContextField.PARTNER_NAME), partner); + assertEquals(partner, provider.values().get(ContextField.PARTNER_NAME)); } @Test @@ -59,12 +61,12 @@ public class RequestContextProviderTest { final String request = "supplied-request-id"; RequestContextProvider provider = RequestContextProvider.from(ContextData.builder().requestId(request).build()); - assertEquals(provider.values().get(ContextField.REQUEST_ID), request); + assertEquals(request, provider.values().get(ContextField.REQUEST_ID)); } @Test public void dataEmptyWhenValuesEmpty() { - ContextData data = RequestContextProvider.to(new HashMap<>()); + ContextData data = RequestContextProvider.to(Collections.emptyMap()); assertNull(data.getPartnerName()); assertNull(data.getRequestId()); assertNull(data.getServiceName()); @@ -73,7 +75,7 @@ public class RequestContextProviderTest { @Test public void serviceNameInDataWhenSuppliedInValues() { final String service = "values-service-name"; - HashMap<ContextField, String> values = new HashMap<>(); + Map<ContextField, String> values = new EnumMap<>(ContextField.class); values.put(ContextField.SERVICE_NAME, service); ContextData data = RequestContextProvider.to(values); assertEquals(data.getServiceName(), service); @@ -82,7 +84,7 @@ public class RequestContextProviderTest { @Test public void partnerNameInDataWhenSuppliedInValues() { final String partner = "values-partner-name"; - HashMap<ContextField, String> values = new HashMap<>(); + Map<ContextField, String> values = new EnumMap<>(ContextField.class); values.put(ContextField.PARTNER_NAME, partner); ContextData data = RequestContextProvider.to(values); assertEquals(data.getPartnerName(), partner); @@ -91,7 +93,7 @@ public class RequestContextProviderTest { @Test public void requestIdInDataWhenSuppliedInValues() { final String request = "values-request-id"; - HashMap<ContextField, String> values = new HashMap<>(); + Map<ContextField, String> values = new EnumMap<>(ContextField.class); values.put(ContextField.REQUEST_ID, request); ContextData data = RequestContextProvider.to(values); assertEquals(data.getRequestId(), request); diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RunnableContextPropagationTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RunnableContextPropagationTest.java index 6f988c22d5..772a89e86f 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RunnableContextPropagationTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RunnableContextPropagationTest.java @@ -16,6 +16,8 @@ package org.openecomp.sdc.logging.slf4j; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_EMPTY; import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_EXCEPTION_FROM_INNER; import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_INNER_RUN; @@ -32,12 +34,12 @@ import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.IS_SU import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.assertContextEmpty; import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.assertContextFields; import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.putUniqueValues; -import static org.testng.Assert.assertTrue; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; +import org.junit.Before; +import org.junit.Test; import org.openecomp.sdc.logging.spi.LoggingContextService; -import org.testng.annotations.Test; /** * Unit-testing logging context propagation to Runnable. @@ -50,7 +52,12 @@ public class RunnableContextPropagationTest { private final LoggingContextService ctxService = new SLF4JLoggingServiceProvider(); - @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + @Before + public void checkSuitableLogbackVersion() { + assumeTrue(IS_SUITABLE_LOGBACK_VERSION); + } + + @Test public void contextNotCopiedToChildThreadByDefault() throws InterruptedException { Map<ContextField, String> values = putUniqueValues(); @@ -66,11 +73,11 @@ public class RunnableContextPropagationTest { thread.start(); thread.join(); - assertContextFields(values, EXPECT_RETAINED_IN_CURRENT); - assertTrue(complete.get(), EXPECT_INNER_RUN); + assertContextFields(EXPECT_RETAINED_IN_CURRENT, values); + assertTrue(EXPECT_INNER_RUN, complete.get()); } - @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + @Test public void contextCopiedWhenToRunnableCalled() throws InterruptedException { Map<ContextField, String> values = putUniqueValues(); @@ -78,18 +85,18 @@ public class RunnableContextPropagationTest { // pass the runnable to the context service first Thread thread = new Thread(ctxService.copyToRunnable(() -> { - assertContextFields(values, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(EXPECT_PROPAGATED_TO_CHILD, values); complete.set(true); })); thread.start(); thread.join(); - assertContextFields(values, EXPECT_RETAINED_IN_CURRENT); - assertTrue(complete.get(), EXPECT_INNER_RUN); + assertContextFields(EXPECT_RETAINED_IN_CURRENT, values); + assertTrue(EXPECT_INNER_RUN, complete.get()); } - @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + @Test public void copiedContextRetainedEvenWhenAnotherPushed() throws InterruptedException { Map<ContextField, String> innerValues = putUniqueValues(); @@ -97,7 +104,7 @@ public class RunnableContextPropagationTest { // should run with the context of main thread Runnable inner = ctxService.copyToRunnable(() -> { - assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(EXPECT_PROPAGATED_TO_CHILD, innerValues); innerComplete.set(true); }); @@ -106,19 +113,19 @@ public class RunnableContextPropagationTest { Thread outer = new Thread(() -> { Map<ContextField, String> outerValues = putUniqueValues(); inner.run(); - assertContextFields(outerValues, EXPECT_REPLACED_WITH_STORED); + assertContextFields(EXPECT_REPLACED_WITH_STORED, outerValues); outerComplete.set(true); }); outer.start(); outer.join(); - assertContextFields(innerValues, EXPECT_RETAINED_IN_CURRENT); - assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); - assertTrue(innerComplete.get(), EXPECT_INNER_RUN); + assertContextFields(EXPECT_RETAINED_IN_CURRENT, innerValues); + assertTrue(EXPECT_OUTER_RUN, outerComplete.get()); + assertTrue(EXPECT_INNER_RUN, innerComplete.get()); } - @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + @Test public void contextRemainsEmptyWhenParentWasEmpty() throws InterruptedException { ctxService.clear(); @@ -135,17 +142,17 @@ public class RunnableContextPropagationTest { thread.join(); assertContextEmpty(EXPECT_EMPTY); - assertTrue(complete.get(), EXPECT_INNER_RUN); + assertTrue(EXPECT_INNER_RUN, complete.get()); } - @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + @Test public void childThreadCleanedUpAfterRunnableRuns() throws Exception { Map<ContextField, String> innerValues = putUniqueValues(); AtomicBoolean innerComplete = new AtomicBoolean(false); // should run with the context of main thread Runnable inner = ctxService.copyToRunnable(() -> { - assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(EXPECT_PROPAGATED_TO_CHILD, innerValues); innerComplete.set(true); }); @@ -161,12 +168,12 @@ public class RunnableContextPropagationTest { outer.start(); outer.join(); - assertContextFields(innerValues, EXPECT_RETAINED_IN_PARENT); - assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); - assertTrue(innerComplete.get(), EXPECT_INNER_RUN); + assertContextFields(EXPECT_RETAINED_IN_PARENT, innerValues); + assertTrue(EXPECT_OUTER_RUN, outerComplete.get()); + assertTrue(EXPECT_INNER_RUN, innerComplete.get()); } - @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + @Test public void childThreadCleanedUpAfterException() throws Exception { Map<ContextField, String> innerValues = putUniqueValues(); @@ -174,7 +181,7 @@ public class RunnableContextPropagationTest { // should run with the context of main thread AtomicBoolean innerComplete = new AtomicBoolean(false); Runnable inner = ctxService.copyToRunnable(() -> { - assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(EXPECT_PROPAGATED_TO_CHILD, innerValues); innerComplete.set(true); throw new IllegalArgumentException(); }); @@ -185,14 +192,14 @@ public class RunnableContextPropagationTest { Thread outer = new Thread(() -> { Map<ContextField, String> outerValues = putUniqueValues(); - assertContextFields(outerValues, EXPECT_POPULATED); + assertContextFields(EXPECT_POPULATED, outerValues); try { inner.run(); } catch (IllegalArgumentException e) { exceptionThrown.set(true); } finally { - assertContextFields(outerValues, EXPECT_REVERTED_ON_EXCEPTION); + assertContextFields(EXPECT_REVERTED_ON_EXCEPTION, outerValues); outerComplete.set(true); } }); @@ -200,9 +207,9 @@ public class RunnableContextPropagationTest { outer.start(); outer.join(); - assertContextFields(innerValues, EXPECT_RETAINED_IN_PARENT); - assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); - assertTrue(innerComplete.get(), EXPECT_INNER_RUN); - assertTrue(exceptionThrown.get(), EXPECT_EXCEPTION_FROM_INNER); + assertContextFields(EXPECT_RETAINED_IN_PARENT, innerValues); + assertTrue(EXPECT_OUTER_RUN, outerComplete.get()); + assertTrue(EXPECT_INNER_RUN, innerComplete.get()); + assertTrue(EXPECT_EXCEPTION_FROM_INNER, exceptionThrown.get()); } }
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapperTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapperTest.java index f4a29efe8b..73dd25ccae 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapperTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapperTest.java @@ -16,22 +16,22 @@ package org.openecomp.sdc.logging.slf4j; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertNull; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.Arrays; import java.util.Map; +import org.junit.Test; import org.openecomp.sdc.logging.api.AuditData; import org.openecomp.sdc.logging.api.MetricsData; import org.openecomp.sdc.logging.api.StatusCode; import org.slf4j.Logger; import org.slf4j.MDC; import org.slf4j.Marker; -import org.testng.annotations.Test; /** * Unit-test of SLF4J implementation of Logger. @@ -106,14 +106,14 @@ public class SLF4JLoggerWrapperTest { public void auditStatusCodeAvailableWhenPassed() { SpyLogger spy = createSpy(); new SLF4JLoggerWrapper(spy).audit(AuditData.builder().statusCode(StatusCode.COMPLETE).build()); - assertEquals(spy.mdc().get(AuditField.STATUS_CODE.asKey()), StatusCode.COMPLETE.name()); + assertEquals(StatusCode.COMPLETE.name(), spy.mdc().get(AuditField.STATUS_CODE.asKey())); } @Test public void metricsStatusCodeAvailableWhenPassed() { SpyLogger spy = createSpy(); new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().statusCode(StatusCode.COMPLETE).build()); - assertEquals(spy.mdc().get(MetricsField.STATUS_CODE.asKey()), StatusCode.COMPLETE.name()); + assertEquals(StatusCode.COMPLETE.name(), spy.mdc().get(MetricsField.STATUS_CODE.asKey())); } @Test @@ -135,7 +135,7 @@ public class SLF4JLoggerWrapperTest { final String responseCode = "AuditSpyResponse"; SpyLogger spy = createSpy(); new SLF4JLoggerWrapper(spy).audit(AuditData.builder().responseCode(responseCode).build()); - assertEquals(spy.mdc().get(AuditField.RESPONSE_CODE.asKey()), responseCode); + assertEquals(responseCode, spy.mdc().get(AuditField.RESPONSE_CODE.asKey())); } @Test @@ -143,7 +143,7 @@ public class SLF4JLoggerWrapperTest { final String responseCode = "MetricsSpyResponse"; SpyLogger spy = createSpy(); new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().responseCode(responseCode).build()); - assertEquals(spy.mdc().get(MetricsField.RESPONSE_CODE.asKey()), responseCode); + assertEquals(responseCode, spy.mdc().get(MetricsField.RESPONSE_CODE.asKey())); } @Test @@ -165,7 +165,7 @@ public class SLF4JLoggerWrapperTest { final String responseDescription = "AuditSpyDescription"; SpyLogger spy = createSpy(); new SLF4JLoggerWrapper(spy).audit(AuditData.builder().responseDescription(responseDescription).build()); - assertEquals(spy.mdc().get(AuditField.RESPONSE_DESCRIPTION.asKey()), responseDescription); + assertEquals(responseDescription, spy.mdc().get(AuditField.RESPONSE_DESCRIPTION.asKey())); } @Test @@ -173,7 +173,7 @@ public class SLF4JLoggerWrapperTest { final String responseDescription = "MetricsSpyDescription"; SpyLogger spy = createSpy(); new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().responseDescription(responseDescription).build()); - assertEquals(spy.mdc().get(MetricsField.RESPONSE_DESCRIPTION.asKey()), responseDescription); + assertEquals(responseDescription, spy.mdc().get(MetricsField.RESPONSE_DESCRIPTION.asKey())); } @Test @@ -195,7 +195,7 @@ public class SLF4JLoggerWrapperTest { final String ipAddress = "10.56.20.20"; SpyLogger spy = createSpy(); new SLF4JLoggerWrapper(spy).audit(AuditData.builder().clientIpAddress(ipAddress).build()); - assertEquals(spy.mdc().get(AuditField.CLIENT_IP_ADDRESS.asKey()), ipAddress); + assertEquals(ipAddress, spy.mdc().get(AuditField.CLIENT_IP_ADDRESS.asKey())); } @Test @@ -203,7 +203,7 @@ public class SLF4JLoggerWrapperTest { final String ipAddress = "10.56.20.22"; SpyLogger spy = createSpy(); new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().clientIpAddress(ipAddress).build()); - assertEquals(spy.mdc().get(MetricsField.CLIENT_IP_ADDRESS.asKey()), ipAddress); + assertEquals(ipAddress, spy.mdc().get(MetricsField.CLIENT_IP_ADDRESS.asKey())); } @Test @@ -225,7 +225,7 @@ public class SLF4JLoggerWrapperTest { final String targetEntity = "MetricsTargetEntity"; SpyLogger spy = createSpy(); new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().targetEntity(targetEntity).build()); - assertEquals(spy.mdc().get(MetricsField.TARGET_ENTITY.asKey()), targetEntity); + assertEquals(targetEntity, spy.mdc().get(MetricsField.TARGET_ENTITY.asKey())); } @Test @@ -240,7 +240,7 @@ public class SLF4JLoggerWrapperTest { final String targetEntity = "MetricsTargetVirtualEntity"; SpyLogger spy = createSpy(); new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().targetVirtualEntity(targetEntity).build()); - assertEquals(spy.mdc().get(MetricsField.TARGET_VIRTUAL_ENTITY.asKey()), targetEntity); + assertEquals(targetEntity, spy.mdc().get(MetricsField.TARGET_VIRTUAL_ENTITY.asKey())); } @Test |