From f2cee7829ae7d8fae58239dd0018b2aa790c0251 Mon Sep 17 00:00:00 2001 From: olegb Date: Mon, 19 Feb 2018 16:24:54 +0200 Subject: Added JAX-RS filters for logging Change-Id: I57ab333278d4ea26530e916ef89670df8b51b555 Issue-ID: SDC-772 Signed-off-by: olegb Signed-off-by: vempo --- .../openecomp-sdc-logging-core/pom.xml | 2 - .../sdc/logging/servlet/LoggingFilter.java | 145 ----- .../sdc/logging/aspects/MetricsAspectTest.java | 695 +++++++++++---------- .../sdc/logging/servlet/LoggingFilterTest.java | 316 ---------- .../sdc/logging/slf4j/SLF4JLoggerWrapperTest.java | 27 +- 5 files changed, 367 insertions(+), 818 deletions(-) delete mode 100644 openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/servlet/LoggingFilter.java delete mode 100644 openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/servlet/LoggingFilterTest.java (limited to 'openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core') 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 14f54b0835..dbaef2ada6 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 @@ -45,8 +45,6 @@ ${servlet.version} provided - - org.testng testng diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/servlet/LoggingFilter.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/servlet/LoggingFilter.java deleted file mode 100644 index 0ca550a20a..0000000000 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/servlet/LoggingFilter.java +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Copyright © 2016-2017 European Support Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.openecomp.sdc.logging.servlet; - -import org.openecomp.sdc.logging.api.Logger; -import org.openecomp.sdc.logging.api.LoggerFactory; -import org.slf4j.MDC; - -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import java.io.IOException; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.UUID; -import java.util.concurrent.atomic.AtomicLong; - - -/** - * - *

Pushes information required by EELF onto MDC (Mapped Diagnostic Context).

- * - *

This is servlet filter that should be configured in web.xml to be used. Example:

- * - *
- *
- *  <filter>
- *      <filter-name>LoggingServletFilter</filter-name>
- *      <filter-class>org.openecomp.sdc.logging.servlet.LoggingFilter</filter-class>
- *  </filter>
- *
- *  <filter-mapping>
- *      <filter-name>LoggingServletFilter</filter-name>
- *      <url-pattern>/*</url-pattern>
- *  </filter-mapping>
- *
- * 
- * - * @author evitaliy - * @since 25/07/2016. - */ -public class LoggingFilter implements Filter { - - // should be cashed to avoid low-level call, but with a timeout to account for IP or FQDN changes - private static final HostAddressCache HOST_ADDRESS = new HostAddressCache(); - private static final String UNKNOWN = "UNKNOWN"; - - private static final Logger LOGGER = LoggerFactory.getLogger(LoggingFilter.class); - - public void destroy() { - } - - public void doFilter(ServletRequest request, ServletResponse response, - FilterChain chain) throws IOException, ServletException { - - try { - - MDC.clear(); - - MDC.put("RequestId", UUID.randomUUID().toString()); - MDC.put("ServiceInstanceId", "N/A"); // not applicable - MDC.put("ServiceName", "ASDC"); - MDC.put("InstanceUUID", "N/A"); - - // For some reason chooses IPv4 or IPv6 in a random way - MDC.put("RemoteHost", request.getRemoteHost()); - - InetAddress host = HOST_ADDRESS.get(); - - String ipAddress; - String hostName; - if (host == null) { - ipAddress = UNKNOWN; - hostName = UNKNOWN; - } else { - ipAddress = host.getHostAddress(); - hostName = host.getHostName(); - } - - MDC.put("ServerIPAddress", ipAddress); - MDC.put("ServerFQDN", hostName); - - if(request instanceof HttpServletRequest) { - String userName = ((HttpServletRequest) request).getHeader("USER_ID"); - MDC.put("PartnerName", userName); - } - // TODO: Clarify what these stand for - // MDC.put("AlertSeverity", ); - // MDC.put("Timer", ); - - chain.doFilter(request, response); - - } finally { - MDC.clear(); - } - } - - public void init(FilterConfig config) throws ServletException { } - - private static class HostAddressCache { - - private static final long REFRESH_TIME = 1000L; - - private final AtomicLong lastUpdated = new AtomicLong(0L); - private InetAddress hostAddress; - - public InetAddress get() { - - long current = System.currentTimeMillis(); - if (current - lastUpdated.get() > REFRESH_TIME) { - - synchronized (this) { - - try { - lastUpdated.set(current); // set now to register the attempt even if failed - hostAddress = InetAddress.getLocalHost(); - } catch (UnknownHostException e) { - LOGGER.error("Failed to retrieve local hostname for logging", e); - hostAddress = null; - } - } - } - - return hostAddress; - } - } -} diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/aspects/MetricsAspectTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/aspects/MetricsAspectTest.java index 4d3d6be34b..e4cd37995a 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/aspects/MetricsAspectTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/aspects/MetricsAspectTest.java @@ -1,12 +1,12 @@ /* - * Copyright © 2016-2017 European Support Limited + * Copyright © 2016-2018 European Support Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -16,6 +16,12 @@ package org.openecomp.sdc.logging.aspects; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Predicate; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.reflect.SourceLocation; @@ -30,399 +36,394 @@ import org.powermock.modules.testng.PowerMockTestCase; import org.testng.Assert; import org.testng.annotations.Test; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.UUID; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.function.Predicate; - /** - * @author EVITALIY - * @since 17/08/2016. + * Unit-tests metrics aspect (AOP) behavior. + * + * @author evitaliy + * @since 17 Aug 2016 */ @PrepareForTest(LoggerFactory.class) public class MetricsAspectTest extends PowerMockTestCase { - private static final Object OBJ_TO_RETURN = new Object(); - private static final String EXPECTED_MESSAGE = "'{}' took {} milliseconds"; - - @Test - public void testLogExecutionTime() throws Throwable { - - String className = UUID.randomUUID().toString(); - String methodName = UUID.randomUUID().toString(); - - TestLogger logger = initLogging(className, true); - - MetricsAspect aspect = new MetricsAspect(); - MockProceedingJoinPoint pjp = new MockProceedingJoinPoint(className, methodName); - Object returned = aspect.logExecutionTime(pjp); - - Assert.assertEquals(OBJ_TO_RETURN, returned); - assertExecution(methodName, pjp, logger); - } - - @Test - public void testMetricsDisabled() throws Throwable { - - String className = UUID.randomUUID().toString(); - String methodName = UUID.randomUUID().toString(); - - TestLogger logger = initLogging(className, false); - - MetricsAspect aspect = new MetricsAspect(); - MockProceedingJoinPoint pjp = new MockProceedingJoinPoint(className, methodName); - Object returned = aspect.logExecutionTime(pjp); - - Assert.assertEquals(OBJ_TO_RETURN, returned); - Assert.assertEquals(1, pjp.getCount()); - // return any event - must be empty - Assert.assertFalse(logger.contains((event) -> true)); - } - - @Test(expectedExceptions = IllegalArgumentException.class) - public void testThrowingError() throws Throwable { - - String className = UUID.randomUUID().toString(); - String methodName = UUID.randomUUID().toString(); - - final TestLogger logger = initLogging(className, true); - - MetricsAspect aspect = new MetricsAspect(); - MockProceedingJoinPoint pjp = new MockProceedingJoinPointWithException(className, methodName); - - try { - aspect.logExecutionTime(pjp); - } finally { - assertExecution(methodName, pjp, logger); - } - } - - private TestLogger initLogging(String className, boolean enabled) { - TestLogger logger = new TestLogger(enabled); - PowerMock.mockStatic(LoggerFactory.class); - EasyMock.expect(LoggerFactory.getLogger(className)).andReturn(logger); - PowerMock.replay(LoggerFactory.class); - return logger; - } - - private void assertExecution(String methodName, MockProceedingJoinPoint pjp, TestLogger logger) { - - Assert.assertEquals(1, pjp.getCount()); - Assert.assertTrue(logger.contains((event) -> - (event != null) && (event.length == 3) && EXPECTED_MESSAGE.equals(event[0]) - && methodName.equals(event[1]) && (event[2] instanceof Long))); - } - - private static class MockSignature implements Signature { - - private final String className; - private final String methodName; - - private MockSignature(String className, String methodName) { - this.className = className; - this.methodName = methodName; - } - - @Override - public String toShortString() { - return null; - } - - @Override - public String toLongString() { - return null; - } - - @Override - public String getName() { - return methodName; - } - - @Override - public int getModifiers() { - return 0; - } - - @Override - public Class getDeclaringType() { - return null; - } - - @Override - public String getDeclaringTypeName() { - return className; - } - } - - private static class MockProceedingJoinPoint implements ProceedingJoinPoint { - - private AtomicInteger count = new AtomicInteger(0); - private Signature signature; - - MockProceedingJoinPoint(String className, String methodName) { - this.signature = new MockSignature(className, methodName); - } - - int getCount() { - return count.get(); - } - - @Override - public Object proceed() throws Throwable { - count.incrementAndGet(); - return OBJ_TO_RETURN; - } + private static final Object OBJ_TO_RETURN = new Object(); + private static final String EXPECTED_MESSAGE = "'{}' took {} milliseconds"; - @Override - public void set$AroundClosure(AroundClosure aroundClosure) { + @Test + public void testLogExecutionTime() throws Throwable { - } + String className = UUID.randomUUID().toString(); + String methodName = UUID.randomUUID().toString(); - @Override - public Object proceed(Object[] objects) throws Throwable { - return null; - } + TestLogger logger = initLogging(className, true); - @Override - public String toShortString() { - return null; - } + MetricsAspect aspect = new MetricsAspect(); + MockProceedingJoinPoint pjp = new MockProceedingJoinPoint(className, methodName); + Object returned = aspect.logExecutionTime(pjp); - @Override - public String toLongString() { - return null; + Assert.assertEquals(OBJ_TO_RETURN, returned); + assertExecution(methodName, pjp, logger); } - @Override - public Object getThis() { - return null; + private TestLogger initLogging(String className, boolean enabled) { + TestLogger logger = new TestLogger(enabled); + PowerMock.mockStatic(LoggerFactory.class); + EasyMock.expect(LoggerFactory.getLogger(className)).andReturn(logger); + PowerMock.replay(LoggerFactory.class); + return logger; } - @Override - public Object getTarget() { - return null; - } + private void assertExecution(String methodName, MockProceedingJoinPoint pjp, TestLogger logger) { - @Override - public Object[] getArgs() { - return new Object[0]; + Assert.assertEquals(1, pjp.getCount()); + Assert.assertTrue(logger.contains( + (event) -> (event != null) && (event.length == 3) && EXPECTED_MESSAGE.equals(event[0]) && methodName + .equals(event[1]) && (event[2] instanceof Long))); } - @Override - public Signature getSignature() { - return this.signature; - } + @Test + public void testMetricsDisabled() throws Throwable { - @Override - public SourceLocation getSourceLocation() { - return null; - } + String className = UUID.randomUUID().toString(); + String methodName = UUID.randomUUID().toString(); - @Override - public String getKind() { - return null; - } - - @Override - public StaticPart getStaticPart() { - return null; - } - } + TestLogger logger = initLogging(className, false); - private static class MockProceedingJoinPointWithException extends MockProceedingJoinPoint { + MetricsAspect aspect = new MetricsAspect(); + MockProceedingJoinPoint pjp = new MockProceedingJoinPoint(className, methodName); + Object returned = aspect.logExecutionTime(pjp); - MockProceedingJoinPointWithException(String className, String methodName) { - super(className, methodName); + Assert.assertEquals(OBJ_TO_RETURN, returned); + Assert.assertEquals(1, pjp.getCount()); + // return any event - must be empty + Assert.assertFalse(logger.contains((event) -> true)); } - @Override - public Object proceed() throws Throwable { - super.proceed(); - throw new IllegalArgumentException(); - } - } + @Test(expectedExceptions = IllegalArgumentException.class) + public void testThrowingError() throws Throwable { - private class TestLogger implements Logger { + String className = UUID.randomUUID().toString(); + String methodName = UUID.randomUUID().toString(); - private final boolean enabled; - private List events = Collections.synchronizedList(new ArrayList<>(10)); + final TestLogger logger = initLogging(className, true); - TestLogger(boolean enabled) { - this.enabled = enabled; - } + MetricsAspect aspect = new MetricsAspect(); + MockProceedingJoinPoint pjp = new MockProceedingJoinPointWithException(className, methodName); - @Override - public String getName() { - throw new RuntimeException("Not implemented"); + try { + aspect.logExecutionTime(pjp); + } finally { + assertExecution(methodName, pjp, logger); + } } - @Override - public boolean isMetricsEnabled() { - return this.enabled; - } + private static class MockSignature implements Signature { - @Override - public void metrics(String var1) { - throw new RuntimeException("Not implemented"); - } + private final String className; + private final String methodName; - @Override - public void metrics(String var1, Object var2) { - throw new RuntimeException("Not implemented"); - } + private MockSignature(String className, String methodName) { + this.className = className; + this.methodName = methodName; + } - @Override - public void metrics(String var1, Object var2, Object var3) { + @Override + public String toShortString() { + return null; + } - if (this.enabled) { - events.add(new Object[]{var1, var2, var3}); - } - } + @Override + public String toLongString() { + return null; + } - @Override - public void metrics(String var1, Object... var2) { - throw new RuntimeException("Not implemented"); - } + @Override + public String getName() { + return methodName; + } - @Override - public void metrics(String var1, Throwable throwable) { - throw new RuntimeException("Not implemented"); - } + @Override + public int getModifiers() { + return 0; + } - @Override - public boolean isAuditEnabled() { - throw new RuntimeException("Not implemented"); - } + @Override + public Class getDeclaringType() { + return null; + } - @Override - public void audit(AuditData var1) { - throw new RuntimeException("Not implemented"); + @Override + public String getDeclaringTypeName() { + return className; + } } - @Override - public boolean isDebugEnabled() { - throw new RuntimeException("Not implemented"); - } + private static class MockProceedingJoinPoint implements ProceedingJoinPoint { - @Override - public void debug(String var1) { - throw new RuntimeException("Not implemented"); - } + private final AtomicInteger count = new AtomicInteger(0); + private final Signature signature; - @Override - public void debug(String var1, Object var2) { - throw new RuntimeException("Not implemented"); - } + MockProceedingJoinPoint(String className, String methodName) { + this.signature = new MockSignature(className, methodName); + } - @Override - public void debug(String var1, Object var2, Object var3) { - throw new RuntimeException("Not implemented"); - } + int getCount() { + return count.get(); + } - @Override - public void debug(String var1, Object... var2) { - throw new RuntimeException("Not implemented"); - } + @Override + public void set$AroundClosure(AroundClosure aroundClosure) { - @Override - public void debug(String var1, Throwable throwable) { - throw new RuntimeException("Not implemented"); - } + } - @Override - public boolean isInfoEnabled() { - throw new RuntimeException("Not implemented"); - } + @Override + public Object proceed() throws Throwable { + count.incrementAndGet(); + return OBJ_TO_RETURN; + } - @Override - public void info(String var1) { - throw new RuntimeException("Not implemented"); - } + @Override + public Object proceed(Object[] objects) { + return null; + } - @Override - public void info(String var1, Object var2) { - throw new RuntimeException("Not implemented"); - } + @Override + public String toShortString() { + return null; + } - @Override - public void info(String var1, Object var2, Object var3) { - throw new RuntimeException("Not implemented"); - } - - @Override - public void info(String var1, Object... var2) { - throw new RuntimeException("Not implemented"); - } - - @Override - public void info(String var1, Throwable throwable) { - throw new RuntimeException("Not implemented"); - } - - @Override - public boolean isWarnEnabled() { - throw new RuntimeException("Not implemented"); - } - - @Override - public void warn(String var1) { - throw new RuntimeException("Not implemented"); - } - - @Override - public void warn(String var1, Object var2) { - throw new RuntimeException("Not implemented"); - } - - @Override - public void warn(String var1, Object... var2) { - throw new RuntimeException("Not implemented"); - } - - @Override - public void warn(String var1, Object var2, Object var3) { - throw new RuntimeException("Not implemented"); - } - - @Override - public void warn(String var1, Throwable throwable) { - throw new RuntimeException("Not implemented"); - } - - @Override - public boolean isErrorEnabled() { - throw new RuntimeException("Not implemented"); - } - - @Override - public void error(String var1) { - throw new RuntimeException("Not implemented"); - } + @Override + public String toLongString() { + return null; + } - @Override - public void error(String var1, Object var2) { - throw new RuntimeException("Not implemented"); - } - - @Override - public void error(String var1, Object var2, Object var3) { - throw new RuntimeException("Not implemented"); - } - - @Override - public void error(String var1, Object... var2) { - throw new RuntimeException("Not implemented"); - } - - @Override - public void error(String var1, Throwable throwable) { - throw new RuntimeException("Not implemented"); - } + @Override + public Object getThis() { + return null; + } - public boolean contains(Predicate predicate) { - return events.stream().anyMatch(predicate); + @Override + public Object getTarget() { + return null; + } + + @Override + public Object[] getArgs() { + return new Object[0]; + } + + @Override + public Signature getSignature() { + return this.signature; + } + + @Override + public SourceLocation getSourceLocation() { + return null; + } + + @Override + public String getKind() { + return null; + } + + @Override + public StaticPart getStaticPart() { + return null; + } + } + + private static class MockProceedingJoinPointWithException extends MockProceedingJoinPoint { + + MockProceedingJoinPointWithException(String className, String methodName) { + super(className, methodName); + } + + @Override + public Object proceed() throws Throwable { + super.proceed(); + throw new IllegalArgumentException(); + } + } + + private class TestLogger implements Logger { + + private final boolean enabled; + private final List events = Collections.synchronizedList(new ArrayList<>(10)); + + TestLogger(boolean enabled) { + this.enabled = enabled; + } + + @Override + public String getName() { + throw new RuntimeException("Not implemented"); + } + + @Override + public boolean isMetricsEnabled() { + return this.enabled; + } + + @Override + public void metrics(String var1) { + throw new RuntimeException("Not implemented"); + } + + @Override + public void metrics(String var1, Object var2) { + throw new RuntimeException("Not implemented"); + } + + @Override + public void metrics(String var1, Object var2, Object var3) { + + if (this.enabled) { + events.add(new Object[] {var1, var2, var3}); + } + } + + @Override + public void metrics(String var1, Object... var2) { + throw new RuntimeException("Not implemented"); + } + + @Override + public void metrics(String var1, Throwable throwable) { + throw new RuntimeException("Not implemented"); + } + + @Override + public boolean isAuditEnabled() { + throw new RuntimeException("Not implemented"); + } + + @Override + public void audit(AuditData var1) { + throw new RuntimeException("Not implemented"); + } + + @Override + public boolean isDebugEnabled() { + throw new RuntimeException("Not implemented"); + } + + @Override + public void debug(String var1) { + throw new RuntimeException("Not implemented"); + } + + @Override + public void debug(String var1, Object var2) { + throw new RuntimeException("Not implemented"); + } + + @Override + public void debug(String var1, Object var2, Object var3) { + throw new RuntimeException("Not implemented"); + } + + @Override + public void debug(String var1, Object... var2) { + throw new RuntimeException("Not implemented"); + } + + @Override + public void debug(String var1, Throwable throwable) { + throw new RuntimeException("Not implemented"); + } + + @Override + public boolean isInfoEnabled() { + throw new RuntimeException("Not implemented"); + } + + @Override + public void info(String var1) { + throw new RuntimeException("Not implemented"); + } + + @Override + public void info(String var1, Object var2) { + throw new RuntimeException("Not implemented"); + } + + @Override + public void info(String var1, Object var2, Object var3) { + throw new RuntimeException("Not implemented"); + } + + @Override + public void info(String var1, Object... var2) { + throw new RuntimeException("Not implemented"); + } + + @Override + public void info(String var1, Throwable throwable) { + throw new RuntimeException("Not implemented"); + } + + @Override + public boolean isWarnEnabled() { + throw new RuntimeException("Not implemented"); + } + + @Override + public void warn(String var1) { + throw new RuntimeException("Not implemented"); + } + + @Override + public void warn(String var1, Object var2) { + throw new RuntimeException("Not implemented"); + } + + @Override + public void warn(String var1, Object... var2) { + throw new RuntimeException("Not implemented"); + } + + @Override + public void warn(String var1, Object var2, Object var3) { + throw new RuntimeException("Not implemented"); + } + + @Override + public void warn(String var1, Throwable throwable) { + throw new RuntimeException("Not implemented"); + } + + @Override + public boolean isErrorEnabled() { + throw new RuntimeException("Not implemented"); + } + + @Override + public void error(String var1) { + throw new RuntimeException("Not implemented"); + } + + @Override + public void error(String var1, Object var2) { + throw new RuntimeException("Not implemented"); + } + + @Override + public void error(String var1, Object var2, Object var3) { + throw new RuntimeException("Not implemented"); + } + + @Override + public void error(String var1, Object... var2) { + throw new RuntimeException("Not implemented"); + } + + @Override + public void error(String var1, Throwable throwable) { + throw new RuntimeException("Not implemented"); + } + + boolean contains(Predicate 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/servlet/LoggingFilterTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/servlet/LoggingFilterTest.java deleted file mode 100644 index 95a5421be0..0000000000 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/servlet/LoggingFilterTest.java +++ /dev/null @@ -1,316 +0,0 @@ -/* - * Copyright © 2016-2017 European Support Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.openecomp.sdc.logging.servlet; - -import org.slf4j.MDC; -import org.testng.Assert; -import org.testng.annotations.Test; - -import javax.servlet.FilterChain; -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletException; -import javax.servlet.ServletInputStream; -import javax.servlet.ServletOutputStream; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; -import java.net.InetAddress; -import java.util.Enumeration; -import java.util.Locale; -import java.util.Map; -import java.util.UUID; -import java.util.concurrent.atomic.AtomicInteger; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; - -/** - * TODO: Add more tests - * - * @author EVITALIY - * @since 17/08/2016. - */ -public class LoggingFilterTest { - - private static final String REMOTE_HOST = UUID.randomUUID().toString(); - - @Test - public void testDoFilter() throws Exception { - LoggingFilter loggingFilter = new LoggingFilter(); - ServletRequest mockRequest = new TestServletRequest(); - ServletResponse mockResponse = new TestServletResponse(); - TestFilterChain mockChain = new TestFilterChain(); - loggingFilter.doFilter(mockRequest, mockResponse, mockChain); - assertEquals(1, mockChain.getCount()); - assertNull(MDC.getCopyOfContextMap()); - } - - private static class TestServletRequest implements ServletRequest { - - @Override - public Object getAttribute(String s) { - return null; - } - - @Override - public Enumeration getAttributeNames() { - return null; - } - - @Override - public String getCharacterEncoding() { - return null; - } - - @Override - public void setCharacterEncoding(String s) throws UnsupportedEncodingException { - - } - - @Override - public int getContentLength() { - return 0; - } - - @Override - public String getContentType() { - return null; - } - - @Override - public ServletInputStream getInputStream() throws IOException { - return null; - } - - @Override - public String getParameter(String s) { - return null; - } - - @Override - public Enumeration getParameterNames() { - return null; - } - - @Override - public String[] getParameterValues(String s) { - return new String[0]; - } - - @Override - public Map getParameterMap() { - return null; - } - - @Override - public String getProtocol() { - return null; - } - - @Override - public String getScheme() { - return null; - } - - @Override - public String getServerName() { - return null; - } - - @Override - public int getServerPort() { - return 0; - } - - @Override - public BufferedReader getReader() throws IOException { - return null; - } - - @Override - public String getRemoteAddr() { - return null; - } - - @Override - public String getRemoteHost() { - return REMOTE_HOST; - } - - @Override - public void setAttribute(String s, Object o) { - - } - - @Override - public void removeAttribute(String s) { - - } - - @Override - public Locale getLocale() { - return null; - } - - @Override - public Enumeration getLocales() { - return null; - } - - @Override - public boolean isSecure() { - return false; - } - - @Override - public RequestDispatcher getRequestDispatcher(String s) { - return null; - } - - @Override - public String getRealPath(String s) { - return null; - } - - @Override - public int getRemotePort() { - return 0; - } - - @Override - public String getLocalName() { - return null; - } - - @Override - public String getLocalAddr() { - return null; - } - - @Override - public int getLocalPort() { - return 0; - } - } - - private static class TestFilterChain implements FilterChain { - - private AtomicInteger count = new AtomicInteger(0); - - @Override - public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) - throws IOException, ServletException { - - Assert.assertNotNull(MDC.get("RequestId")); - Assert.assertEquals(MDC.get("ServiceInstanceId"), "N/A"); - Assert.assertEquals(MDC.get("ServiceName"), "ASDC"); - Assert.assertEquals(MDC.get("InstanceUUID"), "N/A"); - Assert.assertEquals(MDC.get("RemoteHost"), REMOTE_HOST); - - InetAddress host = InetAddress.getLocalHost(); - Assert.assertEquals(MDC.get("ServerIPAddress"), host.getHostAddress()); - Assert.assertEquals(MDC.get("ServerFQDN"), host.getHostName()); - - count.incrementAndGet(); - } - - public int getCount() { - return count.get(); - } - } - - private static class TestServletResponse implements ServletResponse { - - @Override - public String getCharacterEncoding() { - return null; - } - - @Override - public void setCharacterEncoding(String s) { - - } - - @Override - public String getContentType() { - return null; - } - - @Override - public void setContentType(String s) { - - } - - @Override - public ServletOutputStream getOutputStream() throws IOException { - return null; - } - - @Override - public PrintWriter getWriter() throws IOException { - return null; - } - - @Override - public void setContentLength(int i) { - - } - - @Override - public int getBufferSize() { - return 0; - } - - @Override - public void setBufferSize(int i) { - - } - - @Override - public void flushBuffer() throws IOException { - - } - - @Override - public void resetBuffer() { - - } - - @Override - public boolean isCommitted() { - return false; - } - - @Override - public void reset() { - - } - - @Override - public Locale getLocale() { - return null; - } - - @Override - public void setLocale(Locale locale) { - - } - } -} 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 9093f7d066..2c5233f9c9 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 @@ -39,9 +39,12 @@ import org.slf4j.MDC; import org.testng.annotations.Test; /** + * Unit-test of SLF4J implementation of Logger. + * * @author evitaliy * @since 05 Mar 18 */ +@SuppressWarnings("CheckStyle") public class SLF4JLoggerWrapperTest { @Test @@ -170,21 +173,29 @@ public class SLF4JLoggerWrapperTest { @Override public Object invoke(Object proxy, Method method, Object[] args) { - // return the remembered MDC for spying - if (method.getName().equals("mdc")) { + if (isReturnMdcMethod(method)) { return mdc; } - // filter out everything that's not related to audit - if (!method.getName().equals("info") || args.length == 0 || !args[0].equals(Markers.AUDIT)) { - throw new UnsupportedOperationException("Method " + method.getName() + " with arguments " + - Arrays.toString(args) + " wasn't supposed to be called"); + if (!isAuditMethod(method, args)) { + throw new UnsupportedOperationException("Method " + method.getName() + " with arguments " + + Arrays.toString(args) + " wasn't supposed to be called"); } - // remember the MDC that was active during the invocation + storeEffectiveMdc(); + return null; + } + + private boolean isAuditMethod(Method method, Object[] args) { + return (method.getName().equals("info") && args.length > 0 && args[0].equals(Markers.AUDIT)); + } + + private void storeEffectiveMdc() { mdc = MDC.getCopyOfContextMap(); + } - return null; + private boolean isReturnMdcMethod(Method method) { + return method.equals(SpyLogger.class.getDeclaredMethods()[0]); } } } \ No newline at end of file -- cgit 1.2.3-korg