diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp')
6 files changed, 225 insertions, 143 deletions
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 index 636ff94a39..52a794f937 100644 --- 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 @@ -1,5 +1,5 @@ /* - * 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. @@ -23,18 +23,17 @@ import org.testng.annotations.DataProvider; import java.util.concurrent.Callable; /** - * @author EVITALIY + * @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. + // Disable if an old version of logback implementation is being used. + // Context propagation 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"; @@ -52,27 +51,27 @@ public abstract class BaseContextPropagationTest { @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() } + 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); + public void putRequestId(String requestId) { + LoggingContext.putRequestId(requestId); } @Override - public String get(String key) { - return LoggingContext.get(key); + public void putServiceName(String serviceName) { + LoggingContext.putServiceName(serviceName); } @Override - public void remove(String key) { - LoggingContext.remove(key); + public void putPartnerName(String partnerName) { + LoggingContext.putPartnerName(partnerName); } @Override diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/CallableContextPropagationTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/CallableContextPropagationTest.java index 0c8de97e1a..58d52bfa0f 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/CallableContextPropagationTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/CallableContextPropagationTest.java @@ -16,23 +16,24 @@ package org.openecomp.sdc.logging.slf4j; -import org.openecomp.sdc.logging.spi.LoggingContextService; -import org.testng.annotations.Test; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.assertContextEmpty; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.assertContextFields; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.putUniqueValues; +import static org.testng.Assert.assertTrue; -import java.util.UUID; +import java.util.Map; 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; +import org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField; +import org.openecomp.sdc.logging.spi.LoggingContextService; +import org.testng.annotations.Test; /** - * @author EVITALIY + * @author evitaliy * @since 08 Jan 18 */ public class CallableContextPropagationTest extends BaseContextPropagationTest { @@ -40,33 +41,29 @@ 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); - + Map<ContextField, String> values = putUniqueValues(ctx); AtomicBoolean complete = new AtomicBoolean(false); // pass the callable to the context service first execute(ctx.copyToCallable(() -> { - assertEquals(ctx.get(KEY), uuid, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(values, EXPECT_PROPAGATED_TO_CHILD); complete.set(true); return null; })); - assertEquals(ctx.get(KEY), uuid, EXPECT_RETAINED_IN_CURRENT); + assertContextFields(values, 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); - + Map<ContextField, String> innerValues = putUniqueValues(ctx); AtomicBoolean innerComplete = new AtomicBoolean(false); // should run with the context of main thread Callable inner = ctx.copyToCallable(() -> { - assertEquals(ctx.get(KEY), innerRandom, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); innerComplete.set(true); return null; }); @@ -74,15 +71,14 @@ public class CallableContextPropagationTest extends BaseContextPropagationTest { // 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); + Map<ContextField, String> outerValues = putUniqueValues(ctx); inner.call(); - assertEquals(ctx.get(KEY), outerUuid, EXPECT_REPLACED_WITH_STORED); + assertContextFields(outerValues, EXPECT_REPLACED_WITH_STORED); outerComplete.set(true); return null; }); - assertEquals(ctx.get(KEY), innerRandom, EXPECT_RETAINED_IN_CURRENT); + assertContextFields(innerValues, EXPECT_RETAINED_IN_CURRENT); assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); assertTrue(innerComplete.get(), EXPECT_INNER_RUN); } @@ -90,30 +86,29 @@ public class CallableContextPropagationTest extends BaseContextPropagationTest { @Test(enabled = ENABLED, dataProvider = PROVIDER) public void testContextRemainsEmpty(LoggingContextService ctx) throws Exception { - ctx.remove(KEY); - assertNull(ctx.get(KEY), EXPECT_EMPTY); + ctx.clear(); + assertContextEmpty(EXPECT_EMPTY); final AtomicBoolean complete = new AtomicBoolean(false); execute(ctx.copyToCallable(() -> { - assertNull(ctx.get(KEY), EXPECT_EMPTY); + assertContextEmpty(EXPECT_EMPTY); complete.set(true); return null; })); - assertNull(ctx.get(KEY), EXPECT_EMPTY); + assertContextEmpty(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); + Map<ContextField, String> innerValues = putUniqueValues(ctx); AtomicBoolean innerComplete = new AtomicBoolean(false); // should run with the context of main thread Callable inner = ctx.copyToCallable((() -> { - assertEquals(ctx.get(KEY), innerRandom, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); innerComplete.set(true); return null; })); @@ -121,14 +116,14 @@ public class CallableContextPropagationTest extends BaseContextPropagationTest { // pushes its own context, but runs the inner AtomicBoolean outerComplete = new AtomicBoolean(false); execute(() -> { - assertNull(ctx.get(KEY), EXPECT_NOT_COPIED); + assertContextEmpty(EXPECT_NOT_COPIED); inner.call(); - assertNull(ctx.get(KEY), EXPECT_REMAIN_EMPTY); + assertContextEmpty(EXPECT_REMAIN_EMPTY); outerComplete.set(true); return null; }); - assertEquals(ctx.get(KEY), innerRandom, EXPECT_RETAINED_IN_PARENT); + assertContextFields(innerValues, EXPECT_RETAINED_IN_PARENT); assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); assertTrue(innerComplete.get(), EXPECT_INNER_RUN); } @@ -136,13 +131,12 @@ public class CallableContextPropagationTest extends BaseContextPropagationTest { @Test(enabled = ENABLED, dataProvider = PROVIDER) public void testCleanupAfterError(LoggingContextService ctx) throws Exception { - String innerRandom = UUID.randomUUID().toString(); - ctx.put(KEY, innerRandom); + Map<ContextField, String> innerValues = putUniqueValues(ctx); // should run with the context of main thread AtomicBoolean innerComplete = new AtomicBoolean(false); Callable inner = ctx.copyToCallable(() -> { - assertEquals(ctx.get(KEY), innerRandom, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); innerComplete.set(true); throw new IllegalArgumentException(); }); @@ -152,23 +146,22 @@ public class CallableContextPropagationTest extends BaseContextPropagationTest { AtomicBoolean exceptionThrown = new AtomicBoolean(false); execute(() -> { - String outerUuid = UUID.randomUUID().toString(); - ctx.put(KEY, outerUuid); - assertEquals(ctx.get(KEY), outerUuid, EXPECT_POPULATED); + Map<ContextField, String> outerValues = putUniqueValues(ctx); + assertContextFields(outerValues, EXPECT_POPULATED); try { inner.call(); } catch (IllegalArgumentException e) { exceptionThrown.set(true); } finally { - assertEquals(ctx.get(KEY), outerUuid, EXPECT_REVERTED_ON_EXCEPTION); + assertContextFields(outerValues, EXPECT_REVERTED_ON_EXCEPTION); outerComplete.set(true); } return null; }); - assertEquals(ctx.get(KEY), innerRandom, EXPECT_RETAINED_IN_PARENT); + assertContextFields(innerValues, EXPECT_RETAINED_IN_PARENT); assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); assertTrue(innerComplete.get(), EXPECT_INNER_RUN); assertTrue(exceptionThrown.get(), EXPECT_EXCEPTION_FROM_INNER); diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/ContextPropagationTestHelper.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/ContextPropagationTestHelper.java new file mode 100644 index 0000000000..5112d37ac5 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/ContextPropagationTestHelper.java @@ -0,0 +1,67 @@ +/* + * 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 static org.testng.Assert.assertEquals; + +import java.util.Collections; +import java.util.EnumMap; +import java.util.Map; +import java.util.UUID; +import org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField; +import org.openecomp.sdc.logging.spi.LoggingContextService; +import org.slf4j.MDC; + +/** + * @author evitaliy + * @since 08 Mar 18 + */ +class ContextPropagationTestHelper { + + private static final Map<ContextField, String> EMPTY_CONTEXT = + Collections.unmodifiableMap(new EnumMap<>(ContextField.class)); + + static Map<ContextField, String> putUniqueValues(LoggingContextService ctx) { + + Map<ContextField, String> values = new EnumMap<>(ContextField.class); + + String service = UUID.randomUUID().toString(); + ctx.putServiceName(service); + values.put(ContextField.SERVICE_NAME, service); + + String partner = UUID.randomUUID().toString(); + ctx.putPartnerName(partner); + values.put(ContextField.PARTNER_NAME, partner); + + String request = UUID.randomUUID().toString(); + ctx.putRequestId(request); + values.put(ContextField.REQUEST_ID, request); + + return values; + } + + static void assertContextFields(Map<ContextField, String> values, String error) { + + for (ContextField f : ContextField.values()) { + assertEquals(MDC.get(f.asKey()), values.get(f), error); + } + } + + static void assertContextEmpty(String error) { + assertContextFields(EMPTY_CONTEXT, error); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggingContextTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggingContextTest.java index e93c114193..430d4d4c54 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggingContextTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggingContextTest.java @@ -17,6 +17,7 @@ package org.openecomp.sdc.logging.slf4j; import org.openecomp.sdc.logging.api.LoggingContext; +import org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField; import org.slf4j.MDC; import org.testng.annotations.Test; @@ -27,7 +28,7 @@ import static org.testng.Assert.assertNull; /** * @author evitaliy - * @since 12/09/2016. + * @since 12 Sep 2016 */ public class LoggingContextTest { @@ -52,61 +53,93 @@ public class LoggingContextTest { } @Test - public void clearContextWhenClearCalled() { + public void keysClearedWhenContextCleared() { - String random = UUID.randomUUID().toString(); + String value = UUID.randomUUID().toString(); try { - LoggingContext.put(random, random); + LoggingContext.putPartnerName(value); + LoggingContext.putServiceName(value); + LoggingContext.putRequestId(value); LoggingContext.clear(); - assertNull(MDC.get(random)); - assertNull(LoggingContext.get(random)); + + for (ContextField field : ContextField.values()) { + assertNull(MDC.get(field.asKey())); + } + } finally { - MDC.remove(random); + MDC.clear(); } } @Test - public void returnContextWhenGetCalled() { + public void unrelatedKeysRemainWhenContextCleared() { - String random = UUID.randomUUID().toString(); + String randomValue = UUID.randomUUID().toString(); + String randomKey = "Key-" + randomValue; try { - LoggingContext.put(random, random); - assertEquals(random, MDC.get(random)); - assertEquals(random, LoggingContext.get(random)); + + MDC.put(randomKey, randomValue); + LoggingContext.clear(); + assertEquals(MDC.get(randomKey), randomValue); + } finally { - MDC.remove(random); + MDC.clear(); } } @Test - public void removeContextWhenRemoveCalled() { + public void contextHasServiceNameWhenPut() { String random = UUID.randomUUID().toString(); try { - LoggingContext.put(random, random); - LoggingContext.remove(random); - assertNull(MDC.get(random)); - assertNull(LoggingContext.get(random)); + LoggingContext.putServiceName(random); + assertEquals(random, MDC.get(ContextField.SERVICE_NAME.asKey())); } finally { - MDC.remove(random); + MDC.clear(); } } @Test(expectedExceptions = NullPointerException.class) - public void throwNpeWhenPutWithKeyNull() { - LoggingContext.put(null, "---"); + public void throwNpeWhenServiceNameNull() { + LoggingContext.putServiceName(null); + } + + @Test + public void contextHasRequestIdWhenPut() { + + String random = UUID.randomUUID().toString(); + + try { + LoggingContext.putRequestId(random); + assertEquals(random, MDC.get(ContextField.REQUEST_ID.asKey())); + } finally { + MDC.clear(); + } } @Test(expectedExceptions = NullPointerException.class) - public void throwNpeWhenGetWithKeyNull() { - LoggingContext.get(null); + public void throwNpeWhenRequestIdNull() { + LoggingContext.putRequestId(null); + } + + @Test + public void contextHasPartnerNameWhenPut() { + + String random = UUID.randomUUID().toString(); + + try { + LoggingContext.putPartnerName(random); + assertEquals(random, MDC.get(ContextField.PARTNER_NAME.asKey())); + } finally { + MDC.clear(); + } } @Test(expectedExceptions = NullPointerException.class) - public void throwNpeWhenRemoveWithKeyNull() { - LoggingContext.remove(null); + public void throwNpeWhenPartnerNameNull() { + LoggingContext.putPartnerName(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 index 90d6cda52a..fcd1a56252 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RunnableContextPropagationTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RunnableContextPropagationTest.java @@ -16,18 +16,19 @@ package org.openecomp.sdc.logging.slf4j; -import org.openecomp.sdc.logging.spi.LoggingContextService; -import org.testng.annotations.Test; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.assertContextEmpty; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.assertContextFields; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.putUniqueValues; +import static org.testng.Assert.assertTrue; -import java.util.UUID; +import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertTrue; +import org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField; +import org.openecomp.sdc.logging.spi.LoggingContextService; +import org.testng.annotations.Test; /** - * @author EVITALIY + * @author evitaliy * @since 08 Jan 18 */ public class RunnableContextPropagationTest extends BaseContextPropagationTest { @@ -36,14 +37,12 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { public void contextNotCopiedToChildThreadByDefault(LoggingContextService ctx) throws InterruptedException { - String random = UUID.randomUUID().toString(); - ctx.put(KEY, random); - + Map<ContextField, String> values = putUniqueValues(ctx); 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. " + + assertContextEmpty("Data unexpectedly copied to a child thread. " + "Are you using an old version of SLF4J diagnostic context implementation (e.g. logback)?"); complete.set(true); }); @@ -51,7 +50,7 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { thread.start(); thread.join(); - assertEquals(ctx.get(KEY), random, EXPECT_RETAINED_IN_CURRENT); + assertContextFields(values, EXPECT_RETAINED_IN_CURRENT); assertTrue(complete.get(), EXPECT_INNER_RUN); } @@ -59,21 +58,19 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { public void contextCopiedWhenToRunnableCalled(LoggingContextService ctx) throws InterruptedException { - String uuid = UUID.randomUUID().toString(); - ctx.put(KEY, uuid); - + Map<ContextField, String> values = putUniqueValues(ctx); AtomicBoolean complete = new AtomicBoolean(false); // pass the runnable to the context service first Thread thread = new Thread(ctx.copyToRunnable(() -> { - assertEquals(ctx.get(KEY), uuid, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(values, EXPECT_PROPAGATED_TO_CHILD); complete.set(true); })); thread.start(); thread.join(); - assertEquals(ctx.get(KEY), uuid, EXPECT_RETAINED_IN_CURRENT); + assertContextFields(values, EXPECT_RETAINED_IN_CURRENT); assertTrue(complete.get(), EXPECT_INNER_RUN); } @@ -81,31 +78,28 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { public void copiedContextRetainedEvenWhenAnotherPushed(LoggingContextService ctx) throws InterruptedException { - String innerRandom = UUID.randomUUID().toString(); - ctx.put(KEY, innerRandom); - + Map<ContextField, String> innerValues = putUniqueValues(ctx); AtomicBoolean innerComplete = new AtomicBoolean(false); // should run with the context of main thread Runnable inner = ctx.copyToRunnable(() -> { - assertEquals(ctx.get(KEY), innerRandom, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(innerValues, 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); + Map<ContextField, String> outerValues = putUniqueValues(ctx); inner.run(); - assertEquals(ctx.get(KEY), outerUuid, EXPECT_REPLACED_WITH_STORED); + assertContextFields(outerValues, EXPECT_REPLACED_WITH_STORED); outerComplete.set(true); }); outer.start(); outer.join(); - assertEquals(ctx.get(KEY), innerRandom, EXPECT_RETAINED_IN_CURRENT); + assertContextFields(innerValues, EXPECT_RETAINED_IN_CURRENT); assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); assertTrue(innerComplete.get(), EXPECT_INNER_RUN); } @@ -114,12 +108,12 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { public void contextRemainsEmptyWhenParentWasEmpty(LoggingContextService ctx) throws InterruptedException { - ctx.remove(KEY); - assertNull(ctx.get(KEY), EXPECT_EMPTY); + ctx.clear(); + assertContextEmpty(EXPECT_EMPTY); final AtomicBoolean complete = new AtomicBoolean(false); Runnable runnable = ctx.copyToRunnable(() -> { - assertNull(ctx.get(KEY), EXPECT_EMPTY); + assertContextEmpty(EXPECT_EMPTY); complete.set(true); }); @@ -127,7 +121,7 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { thread.start(); thread.join(); - assertNull(ctx.get(KEY), EXPECT_EMPTY); + assertContextEmpty(EXPECT_EMPTY); assertTrue(complete.get(), EXPECT_INNER_RUN); } @@ -135,29 +129,27 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { public void childThreadCleanedUpAfterRunnableRuns(LoggingContextService ctx) throws Exception { - String innerRandom = UUID.randomUUID().toString(); - ctx.put(KEY, innerRandom); - + Map<ContextField, String> innerValues = putUniqueValues(ctx); AtomicBoolean innerComplete = new AtomicBoolean(false); // should run with the context of main thread Runnable inner = ctx.copyToRunnable(() -> { - assertEquals(ctx.get(KEY), innerRandom, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(innerValues, 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); + assertContextEmpty(EXPECT_NOT_COPIED); inner.run(); - assertNull(ctx.get(KEY), EXPECT_REMAIN_EMPTY); + assertContextEmpty(EXPECT_REMAIN_EMPTY); outerComplete.set(true); }); outer.start(); outer.join(); - assertEquals(ctx.get(KEY), innerRandom, EXPECT_RETAINED_IN_PARENT); + assertContextFields(innerValues, EXPECT_RETAINED_IN_PARENT); assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); assertTrue(innerComplete.get(), EXPECT_INNER_RUN); } @@ -166,13 +158,12 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { public void childThreadCleanedUpAfterException(LoggingContextService ctx) throws Exception { - String innerRandom = UUID.randomUUID().toString(); - ctx.put(KEY, innerRandom); + Map<ContextField, String> innerValues = putUniqueValues(ctx); // should run with the context of main thread AtomicBoolean innerComplete = new AtomicBoolean(false); Runnable inner = ctx.copyToRunnable(() -> { - assertEquals(ctx.get(KEY), innerRandom, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); innerComplete.set(true); throw new IllegalArgumentException(); }); @@ -182,16 +173,15 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { 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); + Map<ContextField, String> outerValues = putUniqueValues(ctx); + assertContextFields(outerValues, EXPECT_POPULATED); try { inner.run(); } catch (IllegalArgumentException e) { exceptionThrown.set(true); } finally { - assertEquals(ctx.get(KEY), outerUuid, EXPECT_REVERTED_ON_EXCEPTION); + assertContextFields(outerValues, EXPECT_REVERTED_ON_EXCEPTION); outerComplete.set(true); } }); @@ -199,7 +189,7 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { outer.start(); outer.join(); - assertEquals(ctx.get(KEY), innerRandom, EXPECT_RETAINED_IN_PARENT); + assertContextFields(innerValues, EXPECT_RETAINED_IN_PARENT); assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); assertTrue(innerComplete.get(), EXPECT_INNER_RUN); assertTrue(exceptionThrown.get(), EXPECT_EXCEPTION_FROM_INNER); 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 a4a5e5c72c..9093f7d066 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,13 @@ package org.openecomp.sdc.logging.slf4j; -import static org.openecomp.sdc.logging.slf4j.SLF4JLoggerWrapper.BEGIN_TIMESTAMP; -import static org.openecomp.sdc.logging.slf4j.SLF4JLoggerWrapper.CLIENT_IP_ADDRESS; -import static org.openecomp.sdc.logging.slf4j.SLF4JLoggerWrapper.ELAPSED_TIME; -import static org.openecomp.sdc.logging.slf4j.SLF4JLoggerWrapper.END_TIMESTAMP; -import static org.openecomp.sdc.logging.slf4j.SLF4JLoggerWrapper.RESPONSE_CODE; -import static org.openecomp.sdc.logging.slf4j.SLF4JLoggerWrapper.RESPONSE_DESCRIPTION; -import static org.openecomp.sdc.logging.slf4j.SLF4JLoggerWrapper.STATUS_CODE; +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; @@ -39,7 +39,7 @@ import org.slf4j.MDC; import org.testng.annotations.Test; /** - * @author EVITALIY + * @author evitaliy * @since 05 Mar 18 */ public class SLF4JLoggerWrapperTest { @@ -54,7 +54,7 @@ public class SLF4JLoggerWrapperTest { SpyLogger spy = createSpy(); long start = System.currentTimeMillis(); new SLF4JLoggerWrapper(spy).audit(AuditData.builder().startTime(start).build()); - assertNotNull(spy.mdc().get(BEGIN_TIMESTAMP)); + assertNotNull(spy.mdc().get(BEGIN_TIMESTAMP.asKey())); } @Test @@ -62,7 +62,7 @@ public class SLF4JLoggerWrapperTest { SpyLogger spy = createSpy(); long end = System.currentTimeMillis(); new SLF4JLoggerWrapper(spy).audit(AuditData.builder().endTime(end).build()); - assertNotNull(spy.mdc().get(END_TIMESTAMP)); + assertNotNull(spy.mdc().get(END_TIMESTAMP.asKey())); } @Test @@ -71,21 +71,21 @@ public class SLF4JLoggerWrapperTest { long start = System.currentTimeMillis(); new SLF4JLoggerWrapper(spy).audit(AuditData.builder() .startTime(start).endTime(start).build()); - assertNotNull(spy.mdc().get(ELAPSED_TIME)); + assertNotNull(spy.mdc().get(ELAPSED_TIME.asKey())); } @Test public void statusCodeAvailableWhenPassed() { SpyLogger spy = createSpy(); new SLF4JLoggerWrapper(spy).audit(AuditData.builder().statusCode(StatusCode.COMPLETE).build()); - assertEquals(spy.mdc().get(STATUS_CODE), StatusCode.COMPLETE.name()); + assertEquals(spy.mdc().get(STATUS_CODE.asKey()), StatusCode.COMPLETE.name()); } @Test public void statusCodeEmptyWhenNotPassed() { SpyLogger spy = createSpy(); new SLF4JLoggerWrapper(spy).audit(AuditData.builder().build()); - assertNull(spy.mdc().get(STATUS_CODE)); + assertNull(spy.mdc().get(STATUS_CODE.asKey())); } @Test @@ -93,14 +93,14 @@ public class SLF4JLoggerWrapperTest { final String responseCode = "SpyResponse"; SpyLogger spy = createSpy(); new SLF4JLoggerWrapper(spy).audit(AuditData.builder().responseCode(responseCode).build()); - assertEquals(spy.mdc().get(RESPONSE_CODE), responseCode); + assertEquals(spy.mdc().get(RESPONSE_CODE.asKey()), responseCode); } @Test public void responseCodeEmptyWhenNotPassed() { SpyLogger spy = createSpy(); new SLF4JLoggerWrapper(spy).audit(AuditData.builder().build()); - assertNull(spy.mdc().get(RESPONSE_CODE)); + assertNull(spy.mdc().get(RESPONSE_CODE.asKey())); } @Test @@ -108,14 +108,14 @@ public class SLF4JLoggerWrapperTest { final String responseDescription = "SpyDescription"; SpyLogger spy = createSpy(); new SLF4JLoggerWrapper(spy).audit(AuditData.builder().responseDescription(responseDescription).build()); - assertEquals(spy.mdc().get(RESPONSE_DESCRIPTION), responseDescription); + assertEquals(spy.mdc().get(RESPONSE_DESCRIPTION.asKey()), responseDescription); } @Test public void responseDescriptionEmptyWhenNotPassed() { SpyLogger spy = createSpy(); new SLF4JLoggerWrapper(spy).audit(AuditData.builder().build()); - assertNull(spy.mdc().get(RESPONSE_DESCRIPTION)); + assertNull(spy.mdc().get(RESPONSE_DESCRIPTION.asKey())); } @Test @@ -123,14 +123,14 @@ public class SLF4JLoggerWrapperTest { final String ipAddress = "10.56.20.20"; SpyLogger spy = createSpy(); new SLF4JLoggerWrapper(spy).audit(AuditData.builder().clientIpAddress(ipAddress).build()); - assertEquals(spy.mdc().get(CLIENT_IP_ADDRESS), ipAddress); + assertEquals(spy.mdc().get(CLIENT_IP_ADDRESS.asKey()), ipAddress); } @Test public void clientIpAddressEmptyWhenNotPassed() { SpyLogger spy = createSpy(); new SLF4JLoggerWrapper(spy).audit(AuditData.builder().build()); - assertNull(spy.mdc().get(CLIENT_IP_ADDRESS)); + assertNull(spy.mdc().get(CLIENT_IP_ADDRESS.asKey())); } @Test @@ -140,7 +140,7 @@ public class SLF4JLoggerWrapperTest { 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), Long.toString(diff)); + assertEquals(spy.mdc().get(ELAPSED_TIME.asKey()), Long.toString(diff)); } interface SpyLogger extends Logger { |