diff options
author | vempo <vitaliy.emporopulo@amdocs.com> | 2018-03-23 23:10:30 +0300 |
---|---|---|
committer | vempo <vitaliy.emporopulo@amdocs.com> | 2018-03-23 23:10:30 +0300 |
commit | 6eceb46537df31756446760484ad1925a997205a (patch) | |
tree | e6a9675ca1c80d55cf5a884a5868491a7d648814 /openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java | |
parent | 79550d5dc1c5bcb79123128aa37b381a368e38d9 (diff) |
Logging refactoring and global context
Implemented global context (instance ID, host address).
Refactored the logging context API for clarity and
separation of concerns. Added unit tests. Fixed Javadoc.
Change-Id: I6e29b7b3613aebf23112dc017490ad2828e0fb91
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/java')
8 files changed, 346 insertions, 235 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 deleted file mode 100644 index 52a794f937..0000000000 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/BaseContextPropagationTest.java +++ /dev/null @@ -1,97 +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.slf4j; - -import org.openecomp.sdc.logging.api.LoggingContext; -import org.openecomp.sdc.logging.spi.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 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 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 putRequestId(String requestId) { - LoggingContext.putRequestId(requestId); - } - - @Override - public void putServiceName(String serviceName) { - LoggingContext.putServiceName(serviceName); - } - - @Override - public void putPartnerName(String partnerName) { - LoggingContext.putPartnerName(partnerName); - } - - @Override - public void clear() { - LoggingContext.clear(); - } - - @Override - public Runnable copyToRunnable(Runnable runnable) { - return LoggingContext.copyToRunnable(runnable); - } - - @Override - public <V> Callable<V> copyToCallable(Callable<V> callable) { - return LoggingContext.copyToCallable(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 index 58d52bfa0f..897a87193f 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/CallableContextPropagationTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/CallableContextPropagationTest.java @@ -16,6 +16,19 @@ package org.openecomp.sdc.logging.slf4j; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_EMPTY; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_EXCEPTION_FROM_INNER; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_INNER_RUN; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_NOT_COPIED; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_OUTER_RUN; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_POPULATED; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_PROPAGATED_TO_CHILD; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_REMAIN_EMPTY; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_REPLACED_WITH_STORED; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_RETAINED_IN_CURRENT; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_RETAINED_IN_PARENT; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_REVERTED_ON_EXCEPTION; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.IS_SUITABLE_LOGBACK_VERSION; 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; @@ -28,24 +41,28 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; -import org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField; import org.openecomp.sdc.logging.spi.LoggingContextService; import org.testng.annotations.Test; /** + * Tests propagation of logging fields to Callable via the logging service. + * * @author evitaliy * @since 08 Jan 18 */ -public class CallableContextPropagationTest extends BaseContextPropagationTest { +@SuppressWarnings("DefaultAnnotationParam") // see the comment to ENABLED +public class CallableContextPropagationTest { - @Test(enabled = ENABLED, dataProvider = PROVIDER) - public void testContextPropagated(LoggingContextService ctx) throws Exception { + private final LoggingContextService ctxService = new SLF4JLoggingServiceProvider(); - Map<ContextField, String> values = putUniqueValues(ctx); + @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + public void testContextPropagated() throws Exception { + + Map<ContextField, String> values = putUniqueValues(); AtomicBoolean complete = new AtomicBoolean(false); // pass the callable to the context service first - execute(ctx.copyToCallable(() -> { + execute(ctxService.copyToCallable(() -> { assertContextFields(values, EXPECT_PROPAGATED_TO_CHILD); complete.set(true); return null; @@ -55,14 +72,14 @@ public class CallableContextPropagationTest extends BaseContextPropagationTest { assertTrue(complete.get(), EXPECT_INNER_RUN); } - @Test(enabled = ENABLED, dataProvider = PROVIDER) - public void testContextReplacement(LoggingContextService ctx) throws Exception { + @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + public void testContextReplacement() throws Exception { - Map<ContextField, String> innerValues = putUniqueValues(ctx); + Map<ContextField, String> innerValues = putUniqueValues(); AtomicBoolean innerComplete = new AtomicBoolean(false); // should run with the context of main thread - Callable inner = ctx.copyToCallable(() -> { + Callable inner = ctxService.copyToCallable(() -> { assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); innerComplete.set(true); return null; @@ -71,7 +88,7 @@ public class CallableContextPropagationTest extends BaseContextPropagationTest { // pushes its own context, but the inner must run with its own context AtomicBoolean outerComplete = new AtomicBoolean(false); execute(() -> { - Map<ContextField, String> outerValues = putUniqueValues(ctx); + Map<ContextField, String> outerValues = putUniqueValues(); inner.call(); assertContextFields(outerValues, EXPECT_REPLACED_WITH_STORED); outerComplete.set(true); @@ -83,14 +100,14 @@ public class CallableContextPropagationTest extends BaseContextPropagationTest { assertTrue(innerComplete.get(), EXPECT_INNER_RUN); } - @Test(enabled = ENABLED, dataProvider = PROVIDER) - public void testContextRemainsEmpty(LoggingContextService ctx) throws Exception { + @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + public void testContextRemainsEmpty() throws Exception { - ctx.clear(); + ctxService.clear(); assertContextEmpty(EXPECT_EMPTY); final AtomicBoolean complete = new AtomicBoolean(false); - execute(ctx.copyToCallable(() -> { + execute(ctxService.copyToCallable(() -> { assertContextEmpty(EXPECT_EMPTY); complete.set(true); return null; @@ -100,14 +117,14 @@ public class CallableContextPropagationTest extends BaseContextPropagationTest { assertTrue(complete.get(), EXPECT_INNER_RUN); } - @Test(enabled = ENABLED, dataProvider = PROVIDER) - public void testContextCleanedUp(LoggingContextService ctx) throws Exception { + @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + public void testContextCleanedUp() throws Exception { - Map<ContextField, String> innerValues = putUniqueValues(ctx); + Map<ContextField, String> innerValues = putUniqueValues(); AtomicBoolean innerComplete = new AtomicBoolean(false); // should run with the context of main thread - Callable inner = ctx.copyToCallable((() -> { + Callable inner = ctxService.copyToCallable((() -> { assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); innerComplete.set(true); return null; @@ -128,14 +145,14 @@ public class CallableContextPropagationTest extends BaseContextPropagationTest { assertTrue(innerComplete.get(), EXPECT_INNER_RUN); } - @Test(enabled = ENABLED, dataProvider = PROVIDER) - public void testCleanupAfterError(LoggingContextService ctx) throws Exception { + @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + public void testCleanupAfterError() throws Exception { - Map<ContextField, String> innerValues = putUniqueValues(ctx); + Map<ContextField, String> innerValues = putUniqueValues(); // should run with the context of main thread AtomicBoolean innerComplete = new AtomicBoolean(false); - Callable inner = ctx.copyToCallable(() -> { + Callable inner = ctxService.copyToCallable(() -> { assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); innerComplete.set(true); throw new IllegalArgumentException(); @@ -146,7 +163,7 @@ public class CallableContextPropagationTest extends BaseContextPropagationTest { AtomicBoolean exceptionThrown = new AtomicBoolean(false); execute(() -> { - Map<ContextField, String> outerValues = putUniqueValues(ctx); + Map<ContextField, String> outerValues = putUniqueValues(); assertContextFields(outerValues, EXPECT_POPULATED); try { diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/ContextPropagationTestHelper.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/ContextPropagationTestHelper.java index 5112d37ac5..fccc6ba59a 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/ContextPropagationTestHelper.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/ContextPropagationTestHelper.java @@ -17,39 +17,44 @@ package org.openecomp.sdc.logging.slf4j; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; -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)); + // Set to "false" if an old version of logback implementation is being used. + // Explicit context propagation should be used when the context 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 IS_SUITABLE_LOGBACK_VERSION = true; - static Map<ContextField, String> putUniqueValues(LoggingContextService ctx) { + 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"; - Map<ContextField, String> values = new EnumMap<>(ContextField.class); + static Map<ContextField, String> putUniqueValues() { - String service = UUID.randomUUID().toString(); - ctx.putServiceName(service); - values.put(ContextField.SERVICE_NAME, service); + Map<ContextField, String> values = new EnumMap<>(ContextField.class); - String partner = UUID.randomUUID().toString(); - ctx.putPartnerName(partner); - values.put(ContextField.PARTNER_NAME, partner); + String random = UUID.randomUUID().toString(); - String request = UUID.randomUUID().toString(); - ctx.putRequestId(request); - values.put(ContextField.REQUEST_ID, request); + for (ContextField key : ContextField.values()) { + String value = random + "-" + key.name(); + values.put(key, value); + MDC.put(key.asKey(), value); + } return values; } @@ -62,6 +67,9 @@ class ContextPropagationTestHelper { } static void assertContextEmpty(String error) { - assertContextFields(EMPTY_CONTEXT, error); + + for (ContextField key : ContextField.values()) { + assertNull(MDC.get(key.asKey()), error); + } } } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/ContextTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/ContextTest.java new file mode 100644 index 0000000000..bed5cec20e --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/ContextTest.java @@ -0,0 +1,73 @@ +/* + * 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.slf4j; + +import static org.testng.Assert.assertEquals; + +import java.util.EnumMap; +import java.util.Map; +import org.slf4j.MDC; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.Test; + +/** + * Unit-tests context replacement on MDC. + * + * @author evitaliy + * @since 23 Mar 2018 + */ +public class ContextTest { + + private static final ContextField FIELD = ContextField.SERVICE_NAME; + private static final String KEY = FIELD.asKey(); + private static final String VALUE = "service-name-value"; + + @AfterMethod + public void clearMdc() { + MDC.clear(); + } + + @Test + public void mdcUpdatedWhenContextReplaced() { + + MDC.put(KEY, VALUE); + Context context = new Context(); + MDC.put(KEY, "modified-" + VALUE); + + context.replace(); + assertEquals(MDC.get(KEY), VALUE); + } + + @Test + public void oldValueReturnedWhenContextReplaced() { + + MDC.put(KEY, VALUE); + Map<ContextField, String> old = new Context().replace(); + assertEquals(old.size(), 1); + assertEquals(old.get(FIELD), VALUE); + } + + @Test + public void mdcUpdatedWhenContextReverted() { + + Context context = new Context(); + Map<ContextField, String> values = new EnumMap<>(ContextField.class); + values.put(FIELD, VALUE); + context.revert(values); + assertEquals(MDC.get(KEY), VALUE); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/GlobalContextProviderTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/GlobalContextProviderTest.java new file mode 100644 index 0000000000..d1e4be38a6 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/GlobalContextProviderTest.java @@ -0,0 +1,41 @@ +/* + * 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.slf4j; + +import static org.testng.Assert.assertNotNull; + +import java.util.Map; +import org.testng.annotations.Test; + +/** + * Tests data supplied by the global logging context. + * + * @author evitaliy + * @since 23 Mar 2018 + */ + +public class GlobalContextProviderTest { + + @Test + public void providedValuesPopulated() { + GlobalContextProvider provider = new GlobalContextProvider(); + Map<ContextField, String> values = provider.values(); + assertNotNull(values.get(ContextField.INSTANCE_ID)); + assertNotNull(values.get(ContextField.SERVER)); + assertNotNull(values.get(ContextField.SERVER_IP_ADDRESS)); + } +}
\ 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/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 430d4d4c54..47386d4cf3 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggingContextTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggingContextTest.java @@ -16,22 +16,30 @@ package org.openecomp.sdc.logging.slf4j; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNull; + +import java.util.UUID; +import org.openecomp.sdc.logging.api.ContextData; import org.openecomp.sdc.logging.api.LoggingContext; -import org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField; import org.slf4j.MDC; +import org.testng.annotations.AfterMethod; import org.testng.annotations.Test; -import java.util.UUID; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; - /** + * Unit-testing logging context service via its facade. + * * @author evitaliy * @since 12 Sep 2016 */ public class LoggingContextTest { + @AfterMethod + public void clearMdc() { + MDC.clear(); + } + @Test public void returnMdcWrapperWhenToRunnableCalled() { assertEquals(LoggingContext.copyToRunnable(() -> {}).getClass(), MDCRunnableWrapper.class); @@ -57,18 +65,12 @@ public class LoggingContextTest { String value = UUID.randomUUID().toString(); - try { - LoggingContext.putPartnerName(value); - LoggingContext.putServiceName(value); - LoggingContext.putRequestId(value); - LoggingContext.clear(); - - for (ContextField field : ContextField.values()) { - assertNull(MDC.get(field.asKey())); - } + ContextData context = ContextData.builder().partnerName(value).requestId(value).serviceName(value).build(); + LoggingContext.put(context); + LoggingContext.clear(); - } finally { - MDC.clear(); + for (ContextField field : ContextField.values()) { + assertNull(MDC.get(field.asKey())); } } @@ -78,68 +80,64 @@ public class LoggingContextTest { String randomValue = UUID.randomUUID().toString(); String randomKey = "Key-" + randomValue; - try { - - MDC.put(randomKey, randomValue); - LoggingContext.clear(); - assertEquals(MDC.get(randomKey), randomValue); - - } finally { - MDC.clear(); - } + MDC.put(randomKey, randomValue); + LoggingContext.clear(); + assertEquals(MDC.get(randomKey), randomValue); } @Test public void contextHasServiceNameWhenPut() { String random = UUID.randomUUID().toString(); - - try { - LoggingContext.putServiceName(random); - assertEquals(random, MDC.get(ContextField.SERVICE_NAME.asKey())); - } finally { - MDC.clear(); - } + ContextData context = ContextData.builder().serviceName(random).build(); + LoggingContext.put(context); + assertEquals(random, MDC.get(ContextField.SERVICE_NAME.asKey())); } @Test(expectedExceptions = NullPointerException.class) - public void throwNpeWhenServiceNameNull() { - LoggingContext.putServiceName(null); + public void throwNpeWhenContextDataNull() { + LoggingContext.put(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 throwNpeWhenRequestIdNull() { - LoggingContext.putRequestId(null); + ContextData context = ContextData.builder().requestId(random).build(); + LoggingContext.put(context); + assertEquals(random, MDC.get(ContextField.REQUEST_ID.asKey())); } @Test public void contextHasPartnerNameWhenPut() { String random = UUID.randomUUID().toString(); + ContextData context = ContextData.builder().partnerName(random).build(); + LoggingContext.put(context); + assertEquals(random, MDC.get(ContextField.PARTNER_NAME.asKey())); + } - try { - LoggingContext.putPartnerName(random); - assertEquals(random, MDC.get(ContextField.PARTNER_NAME.asKey())); - } finally { - MDC.clear(); - } + @Test + public void contextHasServerHostWhenPopulated() { + + ContextData context = ContextData.builder().build(); + LoggingContext.put(context); + assertNotNull(MDC.get(ContextField.SERVER.asKey())); } - @Test(expectedExceptions = NullPointerException.class) - public void throwNpeWhenPartnerNameNull() { - LoggingContext.putPartnerName(null); + @Test + public void contextHasServerAddressWhenPopulated() { + + ContextData context = ContextData.builder().build(); + LoggingContext.put(context); + assertNotNull(MDC.get(ContextField.SERVER_IP_ADDRESS.asKey())); + } + + @Test + public void contextHasInstanceIdWhenPopulated() { + + ContextData context = ContextData.builder().build(); + LoggingContext.put(context); + assertNotNull(MDC.get(ContextField.INSTANCE_ID.asKey())); } } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RequestContextProviderTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RequestContextProviderTest.java new file mode 100644 index 0000000000..fa7926acef --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RequestContextProviderTest.java @@ -0,0 +1,62 @@ +/* + * 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.slf4j; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +import org.openecomp.sdc.logging.api.ContextData; +import org.testng.annotations.Test; + +/** + * Unit-test retrieving values from client-provided request data. + * + * @author evitaliy + * @since 23 Mar 2018 + */ +public class RequestContextProviderTest { + + @Test + public void valuesEmptyWhenInputEmpty() { + RequestContextProvider provider = new RequestContextProvider(ContextData.builder().build()); + assertTrue(provider.values().isEmpty()); + } + + @Test + public void serviceNameReturnedWhenSupplied() { + final String service = "supplied-service-name"; + RequestContextProvider provider = + new RequestContextProvider(ContextData.builder().serviceName(service).build()); + assertEquals(provider.values().get(ContextField.SERVICE_NAME), service); + } + + @Test + public void partnerNameReturnedWhenSupplied() { + final String partner = "supplied-partner-name"; + RequestContextProvider provider = + new RequestContextProvider(ContextData.builder().partnerName(partner).build()); + assertEquals(provider.values().get(ContextField.PARTNER_NAME), partner); + } + + @Test + public void requestIdReturnedWhenSupplied() { + final String request = "supplied-request-id"; + RequestContextProvider provider = + new RequestContextProvider(ContextData.builder().requestId(request).build()); + assertEquals(provider.values().get(ContextField.REQUEST_ID), request); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RunnableContextPropagationTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RunnableContextPropagationTest.java index fcd1a56252..6f988c22d5 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RunnableContextPropagationTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RunnableContextPropagationTest.java @@ -16,6 +16,19 @@ package org.openecomp.sdc.logging.slf4j; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_EMPTY; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_EXCEPTION_FROM_INNER; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_INNER_RUN; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_NOT_COPIED; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_OUTER_RUN; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_POPULATED; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_PROPAGATED_TO_CHILD; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_REMAIN_EMPTY; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_REPLACED_WITH_STORED; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_RETAINED_IN_CURRENT; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_RETAINED_IN_PARENT; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_REVERTED_ON_EXCEPTION; +import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.IS_SUITABLE_LOGBACK_VERSION; 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; @@ -23,27 +36,30 @@ import static org.testng.Assert.assertTrue; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; -import org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField; import org.openecomp.sdc.logging.spi.LoggingContextService; import org.testng.annotations.Test; /** + * Unit-testing logging context propagation to Runnable. + * * @author evitaliy * @since 08 Jan 18 */ -public class RunnableContextPropagationTest extends BaseContextPropagationTest { +@SuppressWarnings("DefaultAnnotationParam") +public class RunnableContextPropagationTest { + + private final LoggingContextService ctxService = new SLF4JLoggingServiceProvider(); - @Test(enabled = ENABLED, dataProvider = PROVIDER) - public void contextNotCopiedToChildThreadByDefault(LoggingContextService ctx) - throws InterruptedException { + @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + public void contextNotCopiedToChildThreadByDefault() throws InterruptedException { - Map<ContextField, String> values = putUniqueValues(ctx); + Map<ContextField, String> values = putUniqueValues(); AtomicBoolean complete = new AtomicBoolean(false); // create thread right away without copying context Thread thread = new Thread(() -> { - assertContextEmpty("Data unexpectedly copied to a child thread. " + - "Are you using an old version of SLF4J diagnostic context implementation (e.g. logback)?"); + 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); }); @@ -54,15 +70,14 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { assertTrue(complete.get(), EXPECT_INNER_RUN); } - @Test(enabled = ENABLED, dataProvider = PROVIDER) - public void contextCopiedWhenToRunnableCalled(LoggingContextService ctx) - throws InterruptedException { + @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + public void contextCopiedWhenToRunnableCalled() throws InterruptedException { - Map<ContextField, String> values = putUniqueValues(ctx); + Map<ContextField, String> values = putUniqueValues(); AtomicBoolean complete = new AtomicBoolean(false); // pass the runnable to the context service first - Thread thread = new Thread(ctx.copyToRunnable(() -> { + Thread thread = new Thread(ctxService.copyToRunnable(() -> { assertContextFields(values, EXPECT_PROPAGATED_TO_CHILD); complete.set(true); })); @@ -74,15 +89,14 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { assertTrue(complete.get(), EXPECT_INNER_RUN); } - @Test(enabled = ENABLED, dataProvider = PROVIDER) - public void copiedContextRetainedEvenWhenAnotherPushed(LoggingContextService ctx) - throws InterruptedException { + @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + public void copiedContextRetainedEvenWhenAnotherPushed() throws InterruptedException { - Map<ContextField, String> innerValues = putUniqueValues(ctx); + Map<ContextField, String> innerValues = putUniqueValues(); AtomicBoolean innerComplete = new AtomicBoolean(false); // should run with the context of main thread - Runnable inner = ctx.copyToRunnable(() -> { + Runnable inner = ctxService.copyToRunnable(() -> { assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); innerComplete.set(true); }); @@ -90,7 +104,7 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { // pushes its context, but the inner must run with its own context AtomicBoolean outerComplete = new AtomicBoolean(false); Thread outer = new Thread(() -> { - Map<ContextField, String> outerValues = putUniqueValues(ctx); + Map<ContextField, String> outerValues = putUniqueValues(); inner.run(); assertContextFields(outerValues, EXPECT_REPLACED_WITH_STORED); outerComplete.set(true); @@ -104,15 +118,14 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { assertTrue(innerComplete.get(), EXPECT_INNER_RUN); } - @Test(enabled = ENABLED, dataProvider = PROVIDER) - public void contextRemainsEmptyWhenParentWasEmpty(LoggingContextService ctx) - throws InterruptedException { + @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + public void contextRemainsEmptyWhenParentWasEmpty() throws InterruptedException { - ctx.clear(); + ctxService.clear(); assertContextEmpty(EXPECT_EMPTY); final AtomicBoolean complete = new AtomicBoolean(false); - Runnable runnable = ctx.copyToRunnable(() -> { + Runnable runnable = ctxService.copyToRunnable(() -> { assertContextEmpty(EXPECT_EMPTY); complete.set(true); }); @@ -125,14 +138,13 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { assertTrue(complete.get(), EXPECT_INNER_RUN); } - @Test(enabled = ENABLED, dataProvider = PROVIDER) - public void childThreadCleanedUpAfterRunnableRuns(LoggingContextService ctx) - throws Exception { + @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + public void childThreadCleanedUpAfterRunnableRuns() throws Exception { - Map<ContextField, String> innerValues = putUniqueValues(ctx); + Map<ContextField, String> innerValues = putUniqueValues(); AtomicBoolean innerComplete = new AtomicBoolean(false); // should run with the context of main thread - Runnable inner = ctx.copyToRunnable(() -> { + Runnable inner = ctxService.copyToRunnable(() -> { assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); innerComplete.set(true); }); @@ -154,15 +166,14 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { assertTrue(innerComplete.get(), EXPECT_INNER_RUN); } - @Test(enabled = ENABLED, dataProvider = PROVIDER) - public void childThreadCleanedUpAfterException(LoggingContextService ctx) - throws Exception { + @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + public void childThreadCleanedUpAfterException() throws Exception { - Map<ContextField, String> innerValues = putUniqueValues(ctx); + Map<ContextField, String> innerValues = putUniqueValues(); // should run with the context of main thread AtomicBoolean innerComplete = new AtomicBoolean(false); - Runnable inner = ctx.copyToRunnable(() -> { + Runnable inner = ctxService.copyToRunnable(() -> { assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); innerComplete.set(true); throw new IllegalArgumentException(); @@ -173,7 +184,7 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { AtomicBoolean exceptionThrown = new AtomicBoolean(false); Thread outer = new Thread(() -> { - Map<ContextField, String> outerValues = putUniqueValues(ctx); + Map<ContextField, String> outerValues = putUniqueValues(); assertContextFields(outerValues, EXPECT_POPULATED); try { @@ -194,6 +205,4 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { assertTrue(innerComplete.get(), EXPECT_INNER_RUN); assertTrue(exceptionThrown.get(), EXPECT_EXCEPTION_FROM_INNER); } - - }
\ No newline at end of file |