aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test
diff options
context:
space:
mode:
authorvempo <vitaliy.emporopulo@amdocs.com>2018-03-26 20:37:06 +0300
committerOren Kleks <orenkle@amdocs.com>2018-04-01 07:57:35 +0000
commit8eff104effad587c04a00cdd008935af38ee7dc9 (patch)
tree25d6307d7e90cb8d49d0a6cd1f94097747ce7eea /openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test
parent8d74513329f8b6c6783024de866784f32c4b0f2c (diff)
Introduced metrics to logging API
Metrics data can now be passed to logger. Also general cleanup, refactoring, simpler implementation, javadocs. Change-Id: I037101aa9626b3e011737ec2e3497ab348319e4c Issue-ID: SDC-772 Signed-off-by: vempo <vitaliy.emporopulo@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test')
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/LogFileCreationTest.java66
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/aspects/MetricsAspectTest.java429
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressTest.java68
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/InstanceIdTest.java36
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapperTest.java199
5 files changed, 259 insertions, 539 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/LogFileCreationTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/LogFileCreationTest.java
deleted file mode 100644
index 14dab13c9e..0000000000
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/LogFileCreationTest.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * Copyright © 2016-2018 European Support Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.openecomp.sdc.logging;
-
-import org.openecomp.sdc.logging.api.AuditData;
-import org.openecomp.sdc.logging.api.Logger;
-import org.openecomp.sdc.logging.api.LoggerFactory;
-import org.testng.annotations.Test;
-
-/**
- * This is only for manual testing to make sure that a log file is created as expected.
- * To run change {@link #ENABLED} to 'true'
- *
- * @author evitaliy
- * @since 13/09/2016.
- */
-public class LogFileCreationTest {
-
- private static final boolean ENABLED = false; // for manual testing change to 'true'
-
- private static final Logger LOGGER = LoggerFactory.getLogger(LogFileCreationTest.class);
-
- @Test(enabled = ENABLED)
- public void testMetrics() {
- LOGGER.metrics("This is metrics");
- }
-
- @Test(enabled = ENABLED)
- public void testAudit() {
- LOGGER.audit(AuditData.builder().build());
- }
-
- @Test(enabled = ENABLED)
- public void testDebug() {
- LOGGER.debug("This is debug");
- }
-
- @Test(enabled = ENABLED)
- public void testInfo() {
- LOGGER.info("This is info");
- }
-
- @Test(enabled = ENABLED)
- public void testWarn() {
- LOGGER.warn("This is warning");
- }
-
- @Test(enabled = ENABLED)
- public void testError() {
- LOGGER.error("This is error");
- }
-}
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
deleted file mode 100644
index e4cd37995a..0000000000
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/aspects/MetricsAspectTest.java
+++ /dev/null
@@ -1,429 +0,0 @@
-/*
- * Copyright © 2016-2018 European Support Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.openecomp.sdc.logging.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;
-import org.aspectj.runtime.internal.AroundClosure;
-import org.easymock.EasyMock;
-import org.openecomp.sdc.logging.api.AuditData;
-import org.openecomp.sdc.logging.api.Logger;
-import org.openecomp.sdc.logging.api.LoggerFactory;
-import org.powermock.api.easymock.PowerMock;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.testng.PowerMockTestCase;
-import org.testng.Assert;
-import org.testng.annotations.Test;
-
-/**
- * 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);
- }
-
- 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)));
- }
-
- @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 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 final AtomicInteger count = new AtomicInteger(0);
- private final Signature signature;
-
- MockProceedingJoinPoint(String className, String methodName) {
- this.signature = new MockSignature(className, methodName);
- }
-
- int getCount() {
- return count.get();
- }
-
- @Override
- public void set$AroundClosure(AroundClosure aroundClosure) {
-
- }
-
- @Override
- public Object proceed() throws Throwable {
- count.incrementAndGet();
- return OBJ_TO_RETURN;
- }
-
- @Override
- public Object proceed(Object[] objects) {
- return null;
- }
-
- @Override
- public String toShortString() {
- return null;
- }
-
- @Override
- public String toLongString() {
- return null;
- }
-
- @Override
- public Object getThis() {
- return null;
- }
-
- @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<Object[]> 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<Object[]> 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/HostAddressTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressTest.java
new file mode 100644
index 0000000000..319bf19dcc
--- /dev/null
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressTest.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright © 2016-2018 European Support Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openecomp.sdc.logging.context;
+
+import static org.testng.Assert.assertNotNull;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import org.easymock.EasyMock;
+import org.powermock.api.easymock.PowerMock;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.testng.PowerMockTestCase;
+import org.testng.annotations.Test;
+
+/**
+ * Retrieval and caching of host address.
+ *
+ * @author evitaliy
+ * @since 28 Mar 2018
+ */
+@PrepareForTest(InetAddress.class)
+public class HostAddressTest extends PowerMockTestCase {
+
+ @Test
+ public void hostAddressIsAlwaysPopulated() {
+ assertNotNull(new HostAddress().get());
+ }
+
+ @Test
+ public void cachedAddressRemainsTheSameWhenGotWithingRefreshInterval() throws UnknownHostException {
+ mockInetAddress(1);
+ HostAddress addressCache = new HostAddress(1000);
+ addressCache.get();
+ addressCache.get();
+ }
+
+ @Test
+ public void cachedAddressReplacedWhenGotAfterRefreshInterval() throws UnknownHostException {
+ mockInetAddress(2);
+ HostAddress addressCache = new HostAddress(-1);
+ addressCache.get();
+ addressCache.get();
+ }
+
+ private void mockInetAddress(int times) throws UnknownHostException {
+ InetAddress inetAddress = EasyMock.mock(InetAddress.class);
+ EasyMock.replay(inetAddress);
+ PowerMock.mockStatic(InetAddress.class);
+ //noinspection ResultOfMethodCallIgnored
+ InetAddress.getLocalHost();
+ PowerMock.expectLastCall().andReturn(inetAddress).times(times);
+ PowerMock.replay(InetAddress.class);
+ }
+} \ 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/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
new file mode 100644
index 0000000000..984cb3bade
--- /dev/null
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/InstanceIdTest.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright © 2016-2018 European Support Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openecomp.sdc.logging.context;
+
+import static org.testng.Assert.assertNotNull;
+
+import org.testng.annotations.Test;
+
+/**
+ * Retrieval of instance ID.
+ *
+ * @author evitaliy
+ * @since 28 Mar 2018
+ */
+public class InstanceIdTest {
+
+ @Test
+ public void makeSureInstanceIdNotNull() {
+ assertNotNull(InstanceId.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 2c5233f9c9..f4a29efe8b 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,13 +16,6 @@
package org.openecomp.sdc.logging.slf4j;
-import static org.openecomp.sdc.logging.slf4j.SLF4JLoggerWrapper.AuditField.BEGIN_TIMESTAMP;
-import static org.openecomp.sdc.logging.slf4j.SLF4JLoggerWrapper.AuditField.CLIENT_IP_ADDRESS;
-import static org.openecomp.sdc.logging.slf4j.SLF4JLoggerWrapper.AuditField.ELAPSED_TIME;
-import static org.openecomp.sdc.logging.slf4j.SLF4JLoggerWrapper.AuditField.END_TIMESTAMP;
-import static org.openecomp.sdc.logging.slf4j.SLF4JLoggerWrapper.AuditField.RESPONSE_CODE;
-import static org.openecomp.sdc.logging.slf4j.SLF4JLoggerWrapper.AuditField.RESPONSE_DESCRIPTION;
-import static org.openecomp.sdc.logging.slf4j.SLF4JLoggerWrapper.AuditField.STATUS_CODE;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
@@ -33,9 +26,11 @@ import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.Map;
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;
/**
@@ -53,101 +48,209 @@ public class SLF4JLoggerWrapperTest {
}
@Test
- public void beginTimeAvailableWhenPassed() {
+ public void metricsDoesNotFailWhenInputNull() {
+ new SLF4JLoggerWrapper(this.getClass()).metrics((MetricsData) null);
+ }
+
+ @Test
+ public void auditBeginTimeAvailableWhenPassed() {
SpyLogger spy = createSpy();
long start = System.currentTimeMillis();
new SLF4JLoggerWrapper(spy).audit(AuditData.builder().startTime(start).build());
- assertNotNull(spy.mdc().get(BEGIN_TIMESTAMP.asKey()));
+ assertNotNull(spy.mdc().get(AuditField.BEGIN_TIMESTAMP.asKey()));
}
@Test
- public void entTimeAvailableWhenPassed() {
+ public void metricsBeginTimeAvailableWhenPassed() {
+ SpyLogger spy = createSpy();
+ long start = System.currentTimeMillis();
+ new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().startTime(start).build());
+ assertNotNull(spy.mdc().get(MetricsField.BEGIN_TIMESTAMP.asKey()));
+ }
+
+ @Test
+ public void auditEndTimeAvailableWhenPassed() {
SpyLogger spy = createSpy();
long end = System.currentTimeMillis();
new SLF4JLoggerWrapper(spy).audit(AuditData.builder().endTime(end).build());
- assertNotNull(spy.mdc().get(END_TIMESTAMP.asKey()));
+ assertNotNull(spy.mdc().get(AuditField.END_TIMESTAMP.asKey()));
}
@Test
- public void elapsedTimeAvailableWhenPassed() {
+ public void metricsEndTimeAvailableWhenPassed() {
+ SpyLogger spy = createSpy();
+ long end = System.currentTimeMillis();
+ new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().endTime(end).build());
+ assertNotNull(spy.mdc().get(MetricsField.END_TIMESTAMP.asKey()));
+ }
+
+ @Test
+ public void auditElapsedTimeAvailableWhenPassed() {
SpyLogger spy = createSpy();
long start = System.currentTimeMillis();
new SLF4JLoggerWrapper(spy).audit(AuditData.builder()
- .startTime(start).endTime(start).build());
- assertNotNull(spy.mdc().get(ELAPSED_TIME.asKey()));
+ .startTime(start).endTime(start + 777).build());
+ assertEquals("777", spy.mdc().get(AuditField.ELAPSED_TIME.asKey()));
+ }
+
+ @Test
+ public void metricsElapsedTimeAvailableWhenPassed() {
+ SpyLogger spy = createSpy();
+ long start = System.currentTimeMillis();
+ new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder()
+ .startTime(start).endTime(start + 1024).build());
+ assertEquals("1024", spy.mdc().get(MetricsField.ELAPSED_TIME.asKey()));
}
@Test
- public void statusCodeAvailableWhenPassed() {
+ public void auditStatusCodeAvailableWhenPassed() {
SpyLogger spy = createSpy();
new SLF4JLoggerWrapper(spy).audit(AuditData.builder().statusCode(StatusCode.COMPLETE).build());
- assertEquals(spy.mdc().get(STATUS_CODE.asKey()), StatusCode.COMPLETE.name());
+ assertEquals(spy.mdc().get(AuditField.STATUS_CODE.asKey()), StatusCode.COMPLETE.name());
+ }
+
+ @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());
}
@Test
- public void statusCodeEmptyWhenNotPassed() {
+ public void auditStatusCodeEmptyWhenNotPassed() {
SpyLogger spy = createSpy();
new SLF4JLoggerWrapper(spy).audit(AuditData.builder().build());
- assertNull(spy.mdc().get(STATUS_CODE.asKey()));
+ assertNull(spy.mdc().get(AuditField.STATUS_CODE.asKey()));
+ }
+
+ @Test
+ public void metricsStatusCodeEmptyWhenNotPassed() {
+ SpyLogger spy = createSpy();
+ new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().build());
+ assertNull(spy.mdc().get(MetricsField.STATUS_CODE.asKey()));
}
@Test
- public void responseCodeAvailableWhenPassed() {
- final String responseCode = "SpyResponse";
+ public void auditResponseCodeAvailableWhenPassed() {
+ final String responseCode = "AuditSpyResponse";
SpyLogger spy = createSpy();
new SLF4JLoggerWrapper(spy).audit(AuditData.builder().responseCode(responseCode).build());
- assertEquals(spy.mdc().get(RESPONSE_CODE.asKey()), responseCode);
+ assertEquals(spy.mdc().get(AuditField.RESPONSE_CODE.asKey()), responseCode);
}
@Test
- public void responseCodeEmptyWhenNotPassed() {
+ public void metricsResponseCodeAvailableWhenPassed() {
+ 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);
+ }
+
+ @Test
+ public void auditResponseCodeEmptyWhenNotPassed() {
SpyLogger spy = createSpy();
new SLF4JLoggerWrapper(spy).audit(AuditData.builder().build());
- assertNull(spy.mdc().get(RESPONSE_CODE.asKey()));
+ assertNull(spy.mdc().get(AuditField.RESPONSE_CODE.asKey()));
+ }
+
+ @Test
+ public void metricsResponseCodeEmptyWhenNotPassed() {
+ SpyLogger spy = createSpy();
+ new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().build());
+ assertNull(spy.mdc().get(MetricsField.RESPONSE_CODE.asKey()));
}
@Test
- public void responseDescriptionAvailableWhenPassed() {
- final String responseDescription = "SpyDescription";
+ public void auditResponseDescriptionAvailableWhenPassed() {
+ final String responseDescription = "AuditSpyDescription";
SpyLogger spy = createSpy();
new SLF4JLoggerWrapper(spy).audit(AuditData.builder().responseDescription(responseDescription).build());
- assertEquals(spy.mdc().get(RESPONSE_DESCRIPTION.asKey()), responseDescription);
+ assertEquals(spy.mdc().get(AuditField.RESPONSE_DESCRIPTION.asKey()), responseDescription);
}
@Test
- public void responseDescriptionEmptyWhenNotPassed() {
+ public void metricsResponseDescriptionAvailableWhenPassed() {
+ 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);
+ }
+
+ @Test
+ public void auditResponseDescriptionEmptyWhenNotPassed() {
SpyLogger spy = createSpy();
new SLF4JLoggerWrapper(spy).audit(AuditData.builder().build());
- assertNull(spy.mdc().get(RESPONSE_DESCRIPTION.asKey()));
+ assertNull(spy.mdc().get(AuditField.RESPONSE_DESCRIPTION.asKey()));
}
@Test
- public void clientIpAddressAvailableWhenPassed() {
+ public void metricsResponseDescriptionEmptyWhenNotPassed() {
+ SpyLogger spy = createSpy();
+ new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().build());
+ assertNull(spy.mdc().get(MetricsField.RESPONSE_DESCRIPTION.asKey()));
+ }
+
+ @Test
+ public void auditClientIpAddressAvailableWhenPassed() {
final String ipAddress = "10.56.20.20";
SpyLogger spy = createSpy();
new SLF4JLoggerWrapper(spy).audit(AuditData.builder().clientIpAddress(ipAddress).build());
- assertEquals(spy.mdc().get(CLIENT_IP_ADDRESS.asKey()), ipAddress);
+ assertEquals(spy.mdc().get(AuditField.CLIENT_IP_ADDRESS.asKey()), ipAddress);
}
@Test
- public void clientIpAddressEmptyWhenNotPassed() {
+ public void metricsClientIpAddressAvailableWhenPassed() {
+ 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);
+ }
+
+ @Test
+ public void auditClientIpAddressEmptyWhenNotPassed() {
SpyLogger spy = createSpy();
new SLF4JLoggerWrapper(spy).audit(AuditData.builder().build());
- assertNull(spy.mdc().get(CLIENT_IP_ADDRESS.asKey()));
+ assertNull(spy.mdc().get(AuditField.CLIENT_IP_ADDRESS.asKey()));
}
@Test
- public void elapsedTimeEqualsDifferenceBetweenStartAndEnd() {
+ public void metricsClientIpAddressEmptyWhenNotPassed() {
SpyLogger spy = createSpy();
- final long diff = 1024;
- long start = System.currentTimeMillis();
- long end = start + diff;
- new SLF4JLoggerWrapper(spy).audit(AuditData.builder().startTime(start).endTime(end).build());
- assertEquals(spy.mdc().get(ELAPSED_TIME.asKey()), Long.toString(diff));
+ new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().build());
+ assertNull(spy.mdc().get(MetricsField.CLIENT_IP_ADDRESS.asKey()));
}
- interface SpyLogger extends Logger {
+ @Test
+ public void metricsTargetEntityAvailableWhenPassed() {
+ 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);
+ }
+ @Test
+ public void metricsTargetEntityEmptyWhenNotPassed() {
+ SpyLogger spy = createSpy();
+ new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().build());
+ assertNull(spy.mdc().get(MetricsField.TARGET_ENTITY.asKey()));
+ }
+
+ @Test
+ public void metricsTargetVirtualEntityAvailableWhenPassed() {
+ 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);
+ }
+
+ @Test
+ public void metricsTargetVirtualEntityEmptyWhenNotPassed() {
+ SpyLogger spy = createSpy();
+ new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().build());
+ assertNull(spy.mdc().get(MetricsField.TARGET_VIRTUAL_ENTITY.asKey()));
+ }
+
+ interface SpyLogger extends Logger {
Map<String, String> mdc();
}
@@ -168,16 +271,16 @@ public class SLF4JLoggerWrapperTest {
private static class SpyingInvocationHandler implements InvocationHandler {
- private Map<String, String> mdc;
+ private Map<String, String> lastMdc;
@Override
public Object invoke(Object proxy, Method method, Object[] args) {
if (isReturnMdcMethod(method)) {
- return mdc;
+ return lastMdc;
}
- if (!isAuditMethod(method, args)) {
+ if (!isAuditMethod(method, args) && !isMetricsMethod(method, args)) {
throw new UnsupportedOperationException("Method " + method.getName() + " with arguments "
+ Arrays.toString(args) + " wasn't supposed to be called");
}
@@ -186,12 +289,20 @@ public class SLF4JLoggerWrapperTest {
return null;
}
+ private boolean isMetricsMethod(Method method, Object[] args) {
+ return isSpecialLogMethod(method, args, Markers.METRICS);
+ }
+
private boolean isAuditMethod(Method method, Object[] args) {
- return (method.getName().equals("info") && args.length > 0 && args[0].equals(Markers.AUDIT));
+ return isSpecialLogMethod(method, args, Markers.AUDIT);
+ }
+
+ private boolean isSpecialLogMethod(Method method, Object[] args, Marker marker) {
+ return method.getName().equals("info") && args.length > 0 && args[0].equals(marker);
}
private void storeEffectiveMdc() {
- mdc = MDC.getCopyOfContextMap();
+ lastMdc = MDC.getCopyOfContextMap();
}
private boolean isReturnMdcMethod(Method method) {