diff options
author | AviZi <avi.ziv@amdocs.com> | 2017-06-09 02:39:56 +0300 |
---|---|---|
committer | AviZi <avi.ziv@amdocs.com> | 2017-06-09 02:39:56 +0300 |
commit | 280f8015d06af1f41a3ef12e8300801c7a5e0d54 (patch) | |
tree | 9c1d3978c04cd28068f02073038c936bb49ca9e0 /common/openecomp-logging-lib/openecomp-logging-core/src/test/java/org/openecomp/core/logging/context | |
parent | fd3821dad11780d33c5373d74c957c442489945e (diff) |
[SDC-29] Amdocs OnBoard 1707 initial commit.
Change-Id: Ie4d12a3f574008b792899b368a0902a8b46b5370
Signed-off-by: AviZi <avi.ziv@amdocs.com>
Diffstat (limited to 'common/openecomp-logging-lib/openecomp-logging-core/src/test/java/org/openecomp/core/logging/context')
2 files changed, 242 insertions, 0 deletions
diff --git a/common/openecomp-logging-lib/openecomp-logging-core/src/test/java/org/openecomp/core/logging/context/MDCPropagationFactoryTest.java b/common/openecomp-logging-lib/openecomp-logging-core/src/test/java/org/openecomp/core/logging/context/MDCPropagationFactoryTest.java new file mode 100644 index 0000000000..f2a6549a2a --- /dev/null +++ b/common/openecomp-logging-lib/openecomp-logging-core/src/test/java/org/openecomp/core/logging/context/MDCPropagationFactoryTest.java @@ -0,0 +1,216 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.core.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.*; + +/** + * @author evitaliy + * @since 12/09/2016. + */ +public class MDCPropagationFactoryTest { + + @Test + 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")); + complete.set(true); + }; + + Thread thread = new Thread(runnable); + thread.start(); + thread.join(); + + assertEquals(MDC.get("data"), uuid); + assertTrue(complete.get()); + } + + @Test + 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); + complete.set(true); + }); + + Thread thread = new Thread(runnable); + thread.start(); + thread.join(); + + assertEquals(MDC.get("data"), uuid); + assertTrue(complete.get()); + } + + @Test + 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); + 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); + outerComplete.set(true); + }; + + + Thread thread = new Thread(outer); + thread.start(); + thread.join(); + + assertEquals(MDC.get("data"), innerUuid); + assertTrue(outerComplete.get()); + assertTrue(innerComplete.get()); + } + + @Test + public void testEmpty() throws InterruptedException { + + final AtomicBoolean complete = new AtomicBoolean(false); + + MDC.remove("data"); + assertNull(MDC.get("data")); + + MdcPropagationService factory = new MdcPropagationService(); + Runnable runnable = factory.create(() -> { + assertNull(MDC.get("data")); + complete.set(true); + }); + + Thread thread = new Thread(runnable); + thread.start(); + thread.join(); + + assertNull(MDC.get("data")); + assertTrue(complete.get()); + } + + @Test + 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); + innerComplete.set(true); + }); + + // pushes its own context, but runs the inner runnable + Runnable outer = () -> { + assertNull(MDC.get("data")); + inner.run(); + assertNull(MDC.get("data")); + outerComplete.set(true); + }; + + Thread thread = new Thread(outer); + thread.start(); + thread.join(); + + assertEquals(MDC.get("data"), innerUuid); + assertTrue(outerComplete.get()); + assertTrue(innerComplete.get()); + } + + @Test + 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); + 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); + + try { + inner.run(); + } catch (RuntimeException e) { + exceptionThrown.set(true); + } finally { + assertEquals(MDC.get("data"), outerUuid); + outerComplete.set(true); + } + }; + + Thread thread = new Thread(outer); + thread.start(); + thread.join(); + + assertEquals(MDC.get("data"), innerUuid); + assertTrue(outerComplete.get()); + assertTrue(innerComplete.get()); + assertTrue(exceptionThrown.get()); + } + +} diff --git a/common/openecomp-logging-lib/openecomp-logging-core/src/test/java/org/openecomp/core/logging/context/TaskFactoryTest.java b/common/openecomp-logging-lib/openecomp-logging-core/src/test/java/org/openecomp/core/logging/context/TaskFactoryTest.java new file mode 100644 index 0000000000..1649302056 --- /dev/null +++ b/common/openecomp-logging-lib/openecomp-logging-core/src/test/java/org/openecomp/core/logging/context/TaskFactoryTest.java @@ -0,0 +1,26 @@ +package org.openecomp.core.logging.context; + +import org.openecomp.core.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 |