diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test')
10 files changed, 852 insertions, 460 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/LoggerFactoryTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/LoggerFactoryTest.java deleted file mode 100644 index fe81a4626a..0000000000 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/LoggerFactoryTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.openecomp.sdc.logging; - -import org.openecomp.sdc.logging.api.LoggerFactory; -import org.testng.annotations.Test; - -import java.lang.reflect.Field; - -import static org.testng.Assert.assertEquals; - -/** - * @author evitaliy - * @since 12/09/2016. - */ -public class LoggerFactoryTest { - - @Test - public void testCreate() throws Exception { - // test that the service loader loads the right implementation - LoggerFactory.getLogger(LoggerFactoryTest.class); - Field factory = LoggerFactory.class.getDeclaredField("SERVICE"); - factory.setAccessible(true); - Object implementation = factory.get(null); - assertEquals(SLF4JLoggerCreationService.class, implementation.getClass()); - } -}
\ 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/RoutingTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/RoutingTest.java index fd3f5909d4..adc1d8e71d 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 @@ -22,6 +22,7 @@ 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; @@ -48,7 +49,7 @@ public class RoutingTest { private static final String METRICS = "Metrics"; private Logger logger; - private Map<String, TestAppender> result = new ConcurrentHashMap<>(); + private final Map<String, TestAppender> result = new ConcurrentHashMap<>(); @BeforeClass public void setUp() { @@ -137,14 +138,14 @@ public class RoutingTest { */ private static class TestAppender extends AppenderBase<ILoggingEvent> { - private List<ILoggingEvent> events = Collections.synchronizedList(new ArrayList<>(10)); + private final List<ILoggingEvent> events = Collections.synchronizedList(new ArrayList<>(10)); @Override protected void append(ILoggingEvent event) { this.events.add(event); } - public boolean contains(Predicate<ILoggingEvent> predicate) { + 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/MDCPropagationFactoryTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/MDCPropagationFactoryTest.java deleted file mode 100644 index 1fda9e85b3..0000000000 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/MDCPropagationFactoryTest.java +++ /dev/null @@ -1,221 +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.context; - -import org.slf4j.MDC; -import org.testng.annotations.Test; - -import java.util.UUID; -import java.util.concurrent.atomic.AtomicBoolean; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertTrue; - -/** - * @author evitaliy - * @since 12/09/2016. - */ -public class MDCPropagationFactoryTest { - - // Disable if an old version of MDC implementation is being used. - // MDCPropagationFactory should be used when MDC is not propagated to child threads. - // See https://jira.qos.ch/browse/LOGBACK-422 and https://jira.qos.ch/browse/LOGBACK-624 - private static final boolean ENABLED = false; - - @Test(enabled = ENABLED) - public void testNoPropagation() throws InterruptedException { - - String uuid = UUID.randomUUID().toString(); - AtomicBoolean complete = new AtomicBoolean(false); - MDC.put("data", uuid); - - Runnable runnable = () -> { - assertNull(MDC.get("data"), "Data unexpectedly copied to a child thread. " + - "Are you using an old version of MDC implementation (e.g. logback)?"); - complete.set(true); - }; - - Thread thread = new Thread(runnable); - thread.start(); - thread.join(); - - assertEquals(MDC.get("data"), uuid, "Expected data to be retained in this thread"); - assertTrue(complete.get(), "Expected the inner thread to run"); - } - - @Test(enabled = ENABLED) - public void testPropagation() throws InterruptedException { - - String uuid = UUID.randomUUID().toString(); - AtomicBoolean complete = new AtomicBoolean(false); - MDC.put("data", uuid); - - MDCPropagationService factory = new MDCPropagationService(); - Runnable runnable = factory.create(() -> { - assertEquals(MDC.get("data"), uuid, "Expected data to be propagated to the child thread's MDC"); - complete.set(true); - }); - - Thread thread = new Thread(runnable); - thread.start(); - - thread.join(); - - assertEquals(MDC.get("data"), uuid, "Expected data to be retained in this thread"); - assertTrue(complete.get(), "Expected the inner thread to run"); - } - - @Test(enabled = ENABLED) - public void testReplacement() throws InterruptedException { - - String innerUuid = UUID.randomUUID().toString(); - AtomicBoolean innerComplete = new AtomicBoolean(false); - AtomicBoolean outerComplete = new AtomicBoolean(false); - - MDC.put("data", innerUuid); - - MDCPropagationService factory = new MDCPropagationService(); - - // should run with the context of main thread - Runnable inner = factory.create(() -> { - assertEquals(MDC.get("data"), innerUuid, "Expected data to be propagated to the child thread's MDC"); - innerComplete.set(true); - }); - - // pushes its own context, but runs the inner runnable - Runnable outer = () -> { - String outerUuid = UUID.randomUUID().toString(); - MDC.put("data", outerUuid); - inner.run(); - assertEquals(MDC.get("data"), outerUuid, "Expected MDC data to be replaced with stored data"); - outerComplete.set(true); - }; - - - Thread thread = new Thread(outer); - thread.start(); - thread.join(); - - assertEquals(MDC.get("data"), innerUuid, "Expected data to be retained in this thread"); - assertTrue(outerComplete.get(), "Expected the outer thread to run"); - assertTrue(innerComplete.get(), "Expected the inner thread to run"); - } - - @Test(enabled = ENABLED) - public void testEmpty() throws InterruptedException { - - final AtomicBoolean complete = new AtomicBoolean(false); - - MDC.remove("data"); - assertNull(MDC.get("data"), "Expected MDC data to be empty"); - - MDCPropagationService factory = new MDCPropagationService(); - Runnable runnable = factory.create(() -> { - assertNull(MDC.get("data"), "Expected MDC data to be empty"); - complete.set(true); - }); - - Thread thread = new Thread(runnable); - thread.start(); - thread.join(); - - assertNull(MDC.get("data"), "Expected MDC data to be empty"); - assertTrue(complete.get(), "Expected the inner thread to run"); - } - - @Test(enabled = ENABLED) - public void testCleanup() throws Exception { - - String innerUuid = UUID.randomUUID().toString(); - AtomicBoolean innerComplete = new AtomicBoolean(false); - AtomicBoolean outerComplete = new AtomicBoolean(false); - - MDC.put("data", innerUuid); - - MDCPropagationService factory = new MDCPropagationService(); - - // should run with the context of main thread - Runnable inner = factory.create(() -> { - assertEquals(MDC.get("data"), innerUuid, "Expected data to be propagated to the child thread's MDC"); - innerComplete.set(true); - }); - - // pushes its own context, but runs the inner runnable - Runnable outer = () -> { - assertNull(MDC.get("data"), "Expected MDC data not to be copied to this thread"); - inner.run(); - assertNull(MDC.get("data"), "Expected MDC data to remain empty in this thread"); - outerComplete.set(true); - }; - - Thread thread = new Thread(outer); - thread.start(); - thread.join(); - - assertEquals(MDC.get("data"), innerUuid, "Expected MDC data to be retained in parent thread"); - assertTrue(outerComplete.get(), "Expected the outer thread to run"); - assertTrue(innerComplete.get(), "Expected the inner thread to run"); - } - - @Test(enabled = ENABLED) - public void testCleanupAfterError() throws Exception { - - String innerUuid = UUID.randomUUID().toString(); - AtomicBoolean innerComplete = new AtomicBoolean(false); - AtomicBoolean outerComplete = new AtomicBoolean(false); - AtomicBoolean exceptionThrown = new AtomicBoolean(false); - - MDC.put("data", innerUuid); - - MDCPropagationService factory = new MDCPropagationService(); - - // should run with the context of main thread - Runnable inner = factory.create(() -> { - assertEquals(MDC.get("data"), innerUuid, "Expected data to be propagated to the child thread's MDC"); - innerComplete.set(true); - throw new RuntimeException(); - }); - - // pushes its own context, but runs the inner runnable - Runnable outer = () -> { - - String outerUuid = UUID.randomUUID().toString(); - MDC.put("data", outerUuid); - assertEquals(MDC.get("data"), outerUuid, "Expected MDC data to be populated in this thread"); - - try { - inner.run(); - } catch (RuntimeException e) { - exceptionThrown.set(true); - } finally { - assertEquals(MDC.get("data"), outerUuid, "Expected MDC data to be reverted even in case of exception"); - outerComplete.set(true); - } - }; - - Thread thread = new Thread(outer); - thread.start(); - thread.join(); - - assertEquals(MDC.get("data"), innerUuid, "Expected MDC data to be retained in parent thread"); - assertTrue(outerComplete.get(), "Expected the outer thread to run"); - assertTrue(innerComplete.get(), "Expected the inner thread to run"); - assertTrue(exceptionThrown.get(), "Expected the inner class to throw exception"); - } - -} diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/TaskFactoryTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/TaskFactoryTest.java deleted file mode 100644 index dad60d44d5..0000000000 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/TaskFactoryTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.openecomp.sdc.logging.context; - -import org.openecomp.sdc.logging.api.context.TaskFactory; -import org.testng.annotations.Test; - -import java.lang.reflect.Field; - -import static org.testng.Assert.assertEquals; - -/** - * @author evitaliy - * @since 12/09/2016. - */ -public class TaskFactoryTest { - - @Test - public void testCreate() throws Exception { - // test that the service loader loads the right implementation - TaskFactory.create(() -> { - }); - Field factory = TaskFactory.class.getDeclaredField("SERVICE"); - factory.setAccessible(true); - Object implementation = factory.get(null); - assertEquals(MDCPropagationService.class, implementation.getClass()); - } -}
\ 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/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 9885fc32b8..050c583346 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 @@ -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. @@ -18,7 +18,7 @@ package org.openecomp.sdc.logging.logback; import ch.qos.logback.classic.Level; import ch.qos.logback.classic.spi.LoggingEvent; -import org.openecomp.sdc.logging.Markers; +import org.openecomp.sdc.logging.slf4j.Markers; import org.slf4j.MarkerFactory; import org.testng.annotations.Test; @@ -30,187 +30,187 @@ import static org.testng.Assert.assertEquals; */ public class EventTypeDiscriminatorTest { - private static final String DEBUG = "Debug"; - private static final String ERROR = "Error"; - - @Test - public void testGetDefaultDiscriminatingValue() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - LoggingEvent event = new LoggingEvent(); - assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); - } - - @Test - public void testGetErrorDiscriminatingValue() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - LoggingEvent event = new LoggingEvent(); - event.setLevel(Level.ERROR); - assertEquals(discriminator.getDiscriminatingValue(event), ERROR); - } - - @Test - public void testGetWarnDiscriminatingValue() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - LoggingEvent event = new LoggingEvent(); - event.setLevel(Level.WARN); - assertEquals(discriminator.getDiscriminatingValue(event), ERROR); - } - - @Test - public void testGetInfoDiscriminatingValue() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - LoggingEvent event = new LoggingEvent(); - event.setLevel(Level.INFO); - assertEquals(discriminator.getDiscriminatingValue(event), ERROR); - } - - @Test - public void testGetTraceDiscriminatingValue() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - LoggingEvent event = new LoggingEvent(); - event.setLevel(Level.TRACE); - assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); - } - - @Test - public void testGetErrorWithAuditDiscriminatingValue() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - LoggingEvent event = new LoggingEvent(); - event.setLevel(Level.ERROR); - event.setMarker(Markers.AUDIT); - assertEquals(discriminator.getDiscriminatingValue(event), ERROR); - } - - @Test - public void testGetErrorWithMetricsDiscriminatingValue() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - LoggingEvent event = new LoggingEvent(); - event.setLevel(Level.ERROR); - event.setMarker(Markers.METRICS); - assertEquals(discriminator.getDiscriminatingValue(event), ERROR); - } - - @Test - public void testGetWarnWithAuditDiscriminatingValue() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - LoggingEvent event = new LoggingEvent(); - event.setLevel(Level.WARN); - event.setMarker(Markers.AUDIT); - assertEquals(discriminator.getDiscriminatingValue(event), ERROR); - } - - @Test - public void testGetWarnWithMetricsDiscriminatingValue() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - LoggingEvent event = new LoggingEvent(); - event.setLevel(Level.WARN); - event.setMarker(Markers.METRICS); - assertEquals(discriminator.getDiscriminatingValue(event), ERROR); - } - - @Test - public void testGetDebugWithAuditDiscriminatingValue() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - LoggingEvent event = new LoggingEvent(); - event.setLevel(Level.DEBUG); - event.setMarker(Markers.AUDIT); - assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); - } - - @Test - public void testGetDebugWithMetricsDiscriminatingValue() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - LoggingEvent event = new LoggingEvent(); - event.setLevel(Level.DEBUG); - event.setMarker(Markers.METRICS); - assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); - } - - @Test - public void testGetTraceWithAuditDiscriminatingValue() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - LoggingEvent event = new LoggingEvent(); - event.setLevel(Level.TRACE); - event.setMarker(Markers.AUDIT); - assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); - } - - @Test - public void testGetTraceWithMetricsDiscriminatingValue() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - LoggingEvent event = new LoggingEvent(); - event.setLevel(Level.TRACE); - event.setMarker(Markers.METRICS); - assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); - } - - @Test - public void testGetErrorWithMarkerDiscriminatingValue() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - LoggingEvent event = new LoggingEvent(); - event.setLevel(Level.ERROR); - event.setMarker(MarkerFactory.getMarker("Dummy")); - assertEquals(discriminator.getDiscriminatingValue(event), ERROR); - } - - @Test - public void testGetWarnWithMarkerDiscriminatingValue() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - LoggingEvent event = new LoggingEvent(); - event.setLevel(Level.WARN); - event.setMarker(MarkerFactory.getMarker("Dummy")); - assertEquals(discriminator.getDiscriminatingValue(event), ERROR); - } - - @Test - public void testGetDebugWithMarkerDiscriminatingValue() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - LoggingEvent event = new LoggingEvent(); - event.setLevel(Level.DEBUG); - event.setMarker(MarkerFactory.getMarker("Dummy")); - assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); - } - - @Test - public void testGetTraceWithMarkerDiscriminatingValue() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - LoggingEvent event = new LoggingEvent(); - event.setLevel(Level.TRACE); - event.setMarker(MarkerFactory.getMarker("Dummy")); - assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); - } - - @Test - public void testGetInfoWithMarkerDiscriminatingValue() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - LoggingEvent event = new LoggingEvent(); - event.setLevel(Level.INFO); - event.setMarker(MarkerFactory.getMarker("Dummy")); - assertEquals(discriminator.getDiscriminatingValue(event), ERROR); - } - - @Test - public void testGetAuditDiscriminatingValue() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - LoggingEvent event = new LoggingEvent(); - event.setLevel(Level.INFO); - event.setMarker(Markers.AUDIT); - assertEquals(discriminator.getDiscriminatingValue(event), "Audit"); - } - - @Test - public void testGetMetricsMarkerDiscriminatingValue() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - LoggingEvent event = new LoggingEvent(); - event.setLevel(Level.INFO); - event.setMarker(Markers.METRICS); - assertEquals(discriminator.getDiscriminatingValue(event), "Metrics"); - } - - @Test - public void testGetKey() throws Exception { - EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); - assertEquals("eventType", discriminator.getKey()); - } + private static final String DEBUG = "Debug"; + private static final String ERROR = "Error"; + + @Test + public void testGetDefaultDiscriminatingValue() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + LoggingEvent event = new LoggingEvent(); + assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); + } + + @Test + public void testGetErrorDiscriminatingValue() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + LoggingEvent event = new LoggingEvent(); + event.setLevel(Level.ERROR); + assertEquals(discriminator.getDiscriminatingValue(event), ERROR); + } + + @Test + public void testGetWarnDiscriminatingValue() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + LoggingEvent event = new LoggingEvent(); + event.setLevel(Level.WARN); + assertEquals(discriminator.getDiscriminatingValue(event), ERROR); + } + + @Test + public void testGetInfoDiscriminatingValue() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + LoggingEvent event = new LoggingEvent(); + event.setLevel(Level.INFO); + assertEquals(discriminator.getDiscriminatingValue(event), ERROR); + } + + @Test + public void testGetTraceDiscriminatingValue() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + LoggingEvent event = new LoggingEvent(); + event.setLevel(Level.TRACE); + assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); + } + + @Test + public void testGetErrorWithAuditDiscriminatingValue() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + LoggingEvent event = new LoggingEvent(); + event.setLevel(Level.ERROR); + event.setMarker(Markers.AUDIT); + assertEquals(discriminator.getDiscriminatingValue(event), ERROR); + } + + @Test + public void testGetErrorWithMetricsDiscriminatingValue() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + LoggingEvent event = new LoggingEvent(); + event.setLevel(Level.ERROR); + event.setMarker(Markers.METRICS); + assertEquals(discriminator.getDiscriminatingValue(event), ERROR); + } + + @Test + public void testGetWarnWithAuditDiscriminatingValue() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + LoggingEvent event = new LoggingEvent(); + event.setLevel(Level.WARN); + event.setMarker(Markers.AUDIT); + assertEquals(discriminator.getDiscriminatingValue(event), ERROR); + } + + @Test + public void testGetWarnWithMetricsDiscriminatingValue() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + LoggingEvent event = new LoggingEvent(); + event.setLevel(Level.WARN); + event.setMarker(Markers.METRICS); + assertEquals(discriminator.getDiscriminatingValue(event), ERROR); + } + + @Test + public void testGetDebugWithAuditDiscriminatingValue() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + LoggingEvent event = new LoggingEvent(); + event.setLevel(Level.DEBUG); + event.setMarker(Markers.AUDIT); + assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); + } + + @Test + public void testGetDebugWithMetricsDiscriminatingValue() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + LoggingEvent event = new LoggingEvent(); + event.setLevel(Level.DEBUG); + event.setMarker(Markers.METRICS); + assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); + } + + @Test + public void testGetTraceWithAuditDiscriminatingValue() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + LoggingEvent event = new LoggingEvent(); + event.setLevel(Level.TRACE); + event.setMarker(Markers.AUDIT); + assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); + } + + @Test + public void testGetTraceWithMetricsDiscriminatingValue() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + LoggingEvent event = new LoggingEvent(); + event.setLevel(Level.TRACE); + event.setMarker(Markers.METRICS); + assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); + } + + @Test + public void testGetErrorWithMarkerDiscriminatingValue() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + LoggingEvent event = new LoggingEvent(); + event.setLevel(Level.ERROR); + event.setMarker(MarkerFactory.getMarker("Dummy")); + assertEquals(discriminator.getDiscriminatingValue(event), ERROR); + } + + @Test + public void testGetWarnWithMarkerDiscriminatingValue() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + LoggingEvent event = new LoggingEvent(); + event.setLevel(Level.WARN); + event.setMarker(MarkerFactory.getMarker("Dummy")); + assertEquals(discriminator.getDiscriminatingValue(event), ERROR); + } + + @Test + public void testGetDebugWithMarkerDiscriminatingValue() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + LoggingEvent event = new LoggingEvent(); + event.setLevel(Level.DEBUG); + event.setMarker(MarkerFactory.getMarker("Dummy")); + assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); + } + + @Test + public void testGetTraceWithMarkerDiscriminatingValue() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + LoggingEvent event = new LoggingEvent(); + event.setLevel(Level.TRACE); + event.setMarker(MarkerFactory.getMarker("Dummy")); + assertEquals(discriminator.getDiscriminatingValue(event), DEBUG); + } + + @Test + public void testGetInfoWithMarkerDiscriminatingValue() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + LoggingEvent event = new LoggingEvent(); + event.setLevel(Level.INFO); + event.setMarker(MarkerFactory.getMarker("Dummy")); + assertEquals(discriminator.getDiscriminatingValue(event), ERROR); + } + + @Test + public void testGetAuditDiscriminatingValue() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + LoggingEvent event = new LoggingEvent(); + event.setLevel(Level.INFO); + event.setMarker(Markers.AUDIT); + assertEquals(discriminator.getDiscriminatingValue(event), "Audit"); + } + + @Test + public void testGetMetricsMarkerDiscriminatingValue() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + LoggingEvent event = new LoggingEvent(); + event.setLevel(Level.INFO); + event.setMarker(Markers.METRICS); + assertEquals(discriminator.getDiscriminatingValue(event), "Metrics"); + } + + @Test + public void testGetKey() { + EventTypeDiscriminator discriminator = new EventTypeDiscriminator(); + assertEquals("eventType", discriminator.getKey()); + } } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/BaseContextPropagationTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/BaseContextPropagationTest.java new file mode 100644 index 0000000000..a430de7d22 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/BaseContextPropagationTest.java @@ -0,0 +1,98 @@ +/* + * 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.slf4j; + +import org.openecomp.sdc.logging.api.LoggingContext; +import org.openecomp.sdc.logging.provider.LoggingContextService; +import org.testng.annotations.DataProvider; + +import java.util.concurrent.Callable; + +/** + * @author EVITALIY + * @since 08 Jan 18 + */ +public abstract class BaseContextPropagationTest { + + // Disable if an old version of ctx implementation is being used. + // ctxPropagationFactory should be used when ctx is not propagated to child threads. + // See https://jira.qos.ch/browse/LOGBACK-422 and https://jira.qos.ch/browse/LOGBACK-624 + static final boolean ENABLED = false; + + static final String PROVIDER = "context"; + static final String KEY = "test-data"; + + static final String EXPECT_PROPAGATED_TO_CHILD = "Expected the data to be propagated to the child thread's context"; + static final String EXPECT_RETAINED_IN_CURRENT = "Expected the data to be retained in this thread"; + static final String EXPECT_REPLACED_WITH_STORED = "Expected context data to be replaced with stored data"; + static final String EXPECT_INNER_RUN = "Expected the inner thread to run"; + static final String EXPECT_OUTER_RUN = "Expected the outer thread to run"; + static final String EXPECT_NOT_COPIED = "Expected context data not to be copied to this thread"; + static final String EXPECT_RETAINED_IN_PARENT = "Expected context data to be retained in parent thread"; + static final String EXPECT_POPULATED = "Expected context data to be populated in this thread"; + static final String EXPECT_EMPTY = "Expected context data to be empty"; + static final String EXPECT_REMAIN_EMPTY = "Expected context data to remain empty in this thread"; + 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"; + + @DataProvider(name = PROVIDER) + public static Object[][] contextServices() { + // try both directly call the implementation and get it via the binding + return new Object[][] { + { new SLF4JLoggingServiceProvider() }, + { new LoggingContextAdaptor() } + }; + } + + private static class LoggingContextAdaptor implements LoggingContextService { + + @Override + public void put(String key, String value) { + LoggingContext.put(key, value); + } + + @Override + public String get(String key) { + return LoggingContext.get(key); + } + + @Override + public void remove(String key) { + LoggingContext.remove(key); + } + + @Override + public void clear() { + LoggingContext.clear(); + } + + @Override + public Runnable toRunnable(Runnable runnable) { + return LoggingContext.toRunnable(runnable); + } + + @Override + public <V> Callable<V> toCallable(Callable<V> callable) { + return LoggingContext.toCallable(callable); + } + + @Override + public String toString() { + return this.getClass().getName(); + } + } +} 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 new file mode 100644 index 0000000000..5fad3f70c5 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/CallableContextPropagationTest.java @@ -0,0 +1,188 @@ +/* + * 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.slf4j; + +import org.openecomp.sdc.logging.provider.LoggingContextService; +import org.testng.annotations.Test; + +import java.util.UUID; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + +/** + * @author EVITALIY + * @since 08 Jan 18 + */ +public class CallableContextPropagationTest extends BaseContextPropagationTest { + + @Test(enabled = ENABLED, dataProvider = PROVIDER) + public void testContextPropagated(LoggingContextService ctx) throws Exception { + + String uuid = UUID.randomUUID().toString(); + ctx.put(KEY, uuid); + + AtomicBoolean complete = new AtomicBoolean(false); + + // pass the callable to the context service first + execute(ctx.toCallable(() -> { + assertEquals(ctx.get(KEY), uuid, EXPECT_PROPAGATED_TO_CHILD); + complete.set(true); + return null; + })); + + assertEquals(ctx.get(KEY), uuid, EXPECT_RETAINED_IN_CURRENT); + assertTrue(complete.get(), EXPECT_INNER_RUN); + } + + @Test(enabled = ENABLED, dataProvider = PROVIDER) + public void testContextReplacement(LoggingContextService ctx) throws Exception { + + String innerRandom = UUID.randomUUID().toString(); + ctx.put(KEY, innerRandom); + + AtomicBoolean innerComplete = new AtomicBoolean(false); + + // should run with the context of main thread + Callable inner = ctx.toCallable(() -> { + assertEquals(ctx.get(KEY), innerRandom, EXPECT_PROPAGATED_TO_CHILD); + innerComplete.set(true); + return null; + }); + + // pushes its own context, but the inner must run with its own context + AtomicBoolean outerComplete = new AtomicBoolean(false); + execute(() -> { + String outerUuid = UUID.randomUUID().toString(); + ctx.put(KEY, outerUuid); + inner.call(); + assertEquals(ctx.get(KEY), outerUuid, EXPECT_REPLACED_WITH_STORED); + outerComplete.set(true); + return null; + }); + + assertEquals(ctx.get(KEY), innerRandom, EXPECT_RETAINED_IN_CURRENT); + assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); + assertTrue(innerComplete.get(), EXPECT_INNER_RUN); + } + + @Test(enabled = ENABLED, dataProvider = PROVIDER) + public void testContextRemainsEmpty(LoggingContextService ctx) throws Exception { + + ctx.remove(KEY); + assertNull(ctx.get(KEY), EXPECT_EMPTY); + + final AtomicBoolean complete = new AtomicBoolean(false); + execute(ctx.toCallable(() -> { + assertNull(ctx.get(KEY), EXPECT_EMPTY); + complete.set(true); + return null; + })); + + assertNull(ctx.get(KEY), EXPECT_EMPTY); + assertTrue(complete.get(), EXPECT_INNER_RUN); + } + + @Test(enabled = ENABLED, dataProvider = PROVIDER) + public void testContextCleanedUp(LoggingContextService ctx) throws Exception { + + String innerRandom = UUID.randomUUID().toString(); + ctx.put(KEY, innerRandom); + + AtomicBoolean innerComplete = new AtomicBoolean(false); + // should run with the context of main thread + Callable inner = ctx.toCallable((() -> { + assertEquals(ctx.get(KEY), innerRandom, EXPECT_PROPAGATED_TO_CHILD); + innerComplete.set(true); + return null; + })); + + // pushes its own context, but runs the inner + AtomicBoolean outerComplete = new AtomicBoolean(false); + execute(() -> { + assertNull(ctx.get(KEY), EXPECT_NOT_COPIED); + inner.call(); + assertNull(ctx.get(KEY), EXPECT_REMAIN_EMPTY); + outerComplete.set(true); + return null; + }); + + assertEquals(ctx.get(KEY), innerRandom, EXPECT_RETAINED_IN_PARENT); + assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); + assertTrue(innerComplete.get(), EXPECT_INNER_RUN); + } + + @Test(enabled = ENABLED, dataProvider = PROVIDER) + public void testCleanupAfterError(LoggingContextService ctx) throws Exception { + + String innerRandom = UUID.randomUUID().toString(); + ctx.put(KEY, innerRandom); + + // should run with the context of main thread + AtomicBoolean innerComplete = new AtomicBoolean(false); + Callable inner = ctx.toCallable(() -> { + assertEquals(ctx.get(KEY), innerRandom, EXPECT_PROPAGATED_TO_CHILD); + innerComplete.set(true); + throw new IllegalArgumentException(); + }); + + // pushes its own context, but runs the inner callable + AtomicBoolean outerComplete = new AtomicBoolean(false); + AtomicBoolean exceptionThrown = new AtomicBoolean(false); + execute(() -> { + + String outerUuid = UUID.randomUUID().toString(); + ctx.put(KEY, outerUuid); + assertEquals(ctx.get(KEY), outerUuid, EXPECT_POPULATED); + + try { + inner.call(); + } catch (IllegalArgumentException e) { + exceptionThrown.set(true); + } finally { + assertEquals(ctx.get(KEY), outerUuid, EXPECT_REVERTED_ON_EXCEPTION); + outerComplete.set(true); + } + + return null; + }); + + assertEquals(ctx.get(KEY), innerRandom, EXPECT_RETAINED_IN_PARENT); + assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); + assertTrue(innerComplete.get(), EXPECT_INNER_RUN); + assertTrue(exceptionThrown.get(), EXPECT_EXCEPTION_FROM_INNER); + } + + private void execute(Callable<Object> callable) throws Exception { + + ExecutorService executor = Executors.newSingleThreadExecutor(); + + try { + Future<Object> future = executor.submit(callable); + future.get(10, TimeUnit.SECONDS); + } finally { + executor.shutdown(); + } + } +}
\ 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/LoggerFactoryTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggerFactoryTest.java new file mode 100644 index 0000000000..e04af23a91 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggerFactoryTest.java @@ -0,0 +1,56 @@ +/* + * 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.slf4j; + +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; + +/** + * @author EVITALIY + * @since 08 Jan 18 + */ +public class LoggerFactoryTest { + + private static final String CLASS_NAME = LoggerFactoryTest.class.getName(); + + @Test + public void returnSlf4jLoggerWhenGetByClass() { + Logger logger = LoggerFactory.getLogger(LoggerFactoryTest.class); + assertEquals(logger.getClass(), SLF4JLoggerWrapper.class); + assertEquals(logger.getName(), CLASS_NAME); + } + + @Test + public void returnSlf4jLoggerWhenGetByName() { + Logger logger = LoggerFactory.getLogger(CLASS_NAME); + assertEquals(logger.getClass(), SLF4JLoggerWrapper.class); + assertEquals(logger.getName(), CLASS_NAME); + } + + @Test(expectedExceptions = NullPointerException.class) + public void throwNpeWhenGetByNameWithNull() { + LoggerFactory.getLogger((String) null); + } + + @Test(expectedExceptions = 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 new file mode 100644 index 0000000000..516a27f2cd --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggingContextTest.java @@ -0,0 +1,112 @@ +/* + * 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.slf4j; + +import org.openecomp.sdc.logging.api.LoggingContext; +import org.slf4j.MDC; +import org.testng.annotations.Test; + +import java.util.UUID; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; + +/** + * @author evitaliy + * @since 12/09/2016. + */ +public class LoggingContextTest { + + @Test + public void returnMdcWrapperWhenToRunnableCalled() { + assertEquals(LoggingContext.toRunnable(() -> {}).getClass(), MDCRunnableWrapper.class); + } + + @Test(expectedExceptions = NullPointerException.class) + public void throwNpeWhenToRunnableWithNull() { + LoggingContext.toRunnable(null); + } + + @Test + public void returnMdcWrapperWhenToCallableCalled() { + assertEquals(LoggingContext.toCallable(() -> "").getClass(), MDCCallableWrapper.class); + } + + @Test(expectedExceptions = NullPointerException.class) + public void throwNpeWhenToCallableWithNull() { + LoggingContext.toCallable(null); + } + + @Test + public void clearContextWhenClearCalled() { + + String random = UUID.randomUUID().toString(); + + try { + LoggingContext.put(random, random); + LoggingContext.clear(); + assertNull(MDC.get(random)); + assertNull(LoggingContext.get(random)); + } finally { + MDC.remove(random); + } + } + + @Test + public void returnContextWhenGetCalled() { + + String random = UUID.randomUUID().toString(); + + try { + LoggingContext.put(random, random); + assertEquals(random, MDC.get(random)); + assertEquals(random, LoggingContext.get(random)); + } finally { + MDC.remove(random); + } + } + + @Test + public void removeContextWhenRemoveCalled() { + + String random = UUID.randomUUID().toString(); + + try { + LoggingContext.put(random, random); + LoggingContext.remove(random); + assertNull(MDC.get(random)); + assertNull(LoggingContext.get(random)); + } finally { + MDC.remove(random); + } + } + + @Test(expectedExceptions = NullPointerException.class) + public void throwNpeWhenPutWithKeyNull() { + LoggingContext.put(null, "---"); + } + + @Test(expectedExceptions = NullPointerException.class) + public void throwNpeWhenGetWithKeyNull() { + LoggingContext.get(null); + } + + @Test(expectedExceptions = NullPointerException.class) + public void throwNpeWhenRemoveWithKeyNull() { + LoggingContext.remove(null); + } +} 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 new file mode 100644 index 0000000000..a617abd69c --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RunnableContextPropagationTest.java @@ -0,0 +1,209 @@ +/* + * 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.slf4j; + +import org.openecomp.sdc.logging.provider.LoggingContextService; +import org.testng.annotations.Test; + +import java.util.UUID; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + +/** + * @author EVITALIY + * @since 08 Jan 18 + */ +public class RunnableContextPropagationTest extends BaseContextPropagationTest { + + @Test(enabled = ENABLED, dataProvider = PROVIDER) + public void contextNotCopiedToChildThreadByDefault(LoggingContextService ctx) + throws InterruptedException { + + String random = UUID.randomUUID().toString(); + ctx.put(KEY, random); + + AtomicBoolean complete = new AtomicBoolean(false); + + // create thread right away without copying context + Thread thread = new Thread(() -> { + assertNull(ctx.get(KEY), "Data unexpectedly copied to a child thread. " + + "Are you using an old version of SLF4J diagnostic context implementation (e.g. logback)?"); + complete.set(true); + }); + + thread.start(); + thread.join(); + + assertEquals(ctx.get(KEY), random, EXPECT_RETAINED_IN_CURRENT); + assertTrue(complete.get(), EXPECT_INNER_RUN); + } + + @Test(enabled = ENABLED, dataProvider = PROVIDER) + public void contextCopiedWhenToRunnableCalled(LoggingContextService ctx) + throws InterruptedException { + + String uuid = UUID.randomUUID().toString(); + ctx.put(KEY, uuid); + + AtomicBoolean complete = new AtomicBoolean(false); + + // pass the runnable to the context service first + Thread thread = new Thread(ctx.toRunnable(() -> { + assertEquals(ctx.get(KEY), uuid, EXPECT_PROPAGATED_TO_CHILD); + complete.set(true); + })); + + thread.start(); + thread.join(); + + assertEquals(ctx.get(KEY), uuid, EXPECT_RETAINED_IN_CURRENT); + assertTrue(complete.get(), EXPECT_INNER_RUN); + } + + @Test(enabled = ENABLED, dataProvider = PROVIDER) + public void copiedContextRetainedEvenWhenAnotherPushed(LoggingContextService ctx) + throws InterruptedException { + + String innerRandom = UUID.randomUUID().toString(); + ctx.put(KEY, innerRandom); + + AtomicBoolean innerComplete = new AtomicBoolean(false); + + // should run with the context of main thread + Runnable inner = ctx.toRunnable(() -> { + assertEquals(ctx.get(KEY), innerRandom, EXPECT_PROPAGATED_TO_CHILD); + innerComplete.set(true); + }); + + // pushes its context, but the inner must run with its own context + AtomicBoolean outerComplete = new AtomicBoolean(false); + Thread outer = new Thread(() -> { + String outerUuid = UUID.randomUUID().toString(); + ctx.put(KEY, outerUuid); + inner.run(); + assertEquals(ctx.get(KEY), outerUuid, EXPECT_REPLACED_WITH_STORED); + outerComplete.set(true); + }); + + outer.start(); + outer.join(); + + assertEquals(ctx.get(KEY), innerRandom, EXPECT_RETAINED_IN_CURRENT); + assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); + assertTrue(innerComplete.get(), EXPECT_INNER_RUN); + } + + @Test(enabled = ENABLED, dataProvider = PROVIDER) + public void contextRemainsEmptyWhenParentWasEmpty(LoggingContextService ctx) + throws InterruptedException { + + ctx.remove(KEY); + assertNull(ctx.get(KEY), EXPECT_EMPTY); + + final AtomicBoolean complete = new AtomicBoolean(false); + Runnable runnable = ctx.toRunnable(() -> { + assertNull(ctx.get(KEY), EXPECT_EMPTY); + complete.set(true); + }); + + Thread thread = new Thread(runnable); + thread.start(); + thread.join(); + + assertNull(ctx.get(KEY), EXPECT_EMPTY); + assertTrue(complete.get(), EXPECT_INNER_RUN); + } + + @Test(enabled = ENABLED, dataProvider = PROVIDER) + public void childThreadCleanedUpAfterRunnableRuns(LoggingContextService ctx) + throws Exception { + + String innerRandom = UUID.randomUUID().toString(); + ctx.put(KEY, innerRandom); + + AtomicBoolean innerComplete = new AtomicBoolean(false); + // should run with the context of main thread + Runnable inner = ctx.toRunnable(() -> { + assertEquals(ctx.get(KEY), innerRandom, EXPECT_PROPAGATED_TO_CHILD); + innerComplete.set(true); + }); + + // pushes its own context, but runs the inner + AtomicBoolean outerComplete = new AtomicBoolean(false); + Thread outer = new Thread(() -> { + assertNull(ctx.get(KEY), EXPECT_NOT_COPIED); + inner.run(); + assertNull(ctx.get(KEY), EXPECT_REMAIN_EMPTY); + outerComplete.set(true); + }); + + outer.start(); + outer.join(); + + assertEquals(ctx.get(KEY), innerRandom, EXPECT_RETAINED_IN_PARENT); + assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); + assertTrue(innerComplete.get(), EXPECT_INNER_RUN); + } + + @Test(enabled = ENABLED, dataProvider = PROVIDER) + public void childThreadCleanedUpAfterException(LoggingContextService ctx) + throws Exception { + + String innerRandom = UUID.randomUUID().toString(); + ctx.put(KEY, innerRandom); + + // should run with the context of main thread + AtomicBoolean innerComplete = new AtomicBoolean(false); + Runnable inner = ctx.toRunnable(() -> { + assertEquals(ctx.get(KEY), innerRandom, EXPECT_PROPAGATED_TO_CHILD); + innerComplete.set(true); + throw new IllegalArgumentException(); + }); + + // pushes its own context, but runs the inner runnable + AtomicBoolean outerComplete = new AtomicBoolean(false); + AtomicBoolean exceptionThrown = new AtomicBoolean(false); + Thread outer = new Thread(() -> { + + String outerUuid = UUID.randomUUID().toString(); + ctx.put(KEY, outerUuid); + assertEquals(ctx.get(KEY), outerUuid, EXPECT_POPULATED); + + try { + inner.run(); + } catch (IllegalArgumentException e) { + exceptionThrown.set(true); + } finally { + assertEquals(ctx.get(KEY), outerUuid, EXPECT_REVERTED_ON_EXCEPTION); + outerComplete.set(true); + } + }); + + outer.start(); + outer.join(); + + assertEquals(ctx.get(KEY), innerRandom, EXPECT_RETAINED_IN_PARENT); + assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); + assertTrue(innerComplete.get(), EXPECT_INNER_RUN); + assertTrue(exceptionThrown.get(), EXPECT_EXCEPTION_FROM_INNER); + } + + +}
\ No newline at end of file |