From 1c1edf2e7b1ddef504d8b075b2763e0b0d83a2aa Mon Sep 17 00:00:00 2001 From: vempo Date: Mon, 8 Jan 2018 20:59:19 +0200 Subject: Added logging context service, refactoring Change-Id: Ib040d4579107b60c8da2c7a6da829f49c1cd8dd4 Issue-ID: SDC-772 Signed-off-by: vempo --- .../sdc/logging/api/LoggerFactoryTest.java | 74 ++++++++------- .../sdc/logging/api/LoggingContextTest.java | 101 +++++++++++++++++++++ .../sdc/logging/api/ServiceBinderTest.java | 38 ++++++++ .../sdc/logging/api/context/TaskFactoryTest.java | 32 ------- 4 files changed, 179 insertions(+), 66 deletions(-) create mode 100644 openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggingContextTest.java create mode 100644 openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/ServiceBinderTest.java delete mode 100644 openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/context/TaskFactoryTest.java (limited to 'openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test') diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggerFactoryTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggerFactoryTest.java index 4b3a1ba7c7..9fde4e5e6c 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggerFactoryTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggerFactoryTest.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. @@ -19,10 +19,8 @@ package org.openecomp.sdc.logging.api; import org.testng.annotations.Test; import java.lang.reflect.Field; -import java.util.ServiceLoader; import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; /** @@ -31,40 +29,48 @@ import static org.testng.Assert.assertNotNull; */ public class LoggerFactoryTest { - @Test - public void testNoOpLoggerService() throws Exception { + @Test + public void shouldHoldNoOpWhenNoBinding() throws Exception { + + // set up to access the private static field + Field factory = LoggerFactory.class.getDeclaredField("SERVICE"); + factory.setAccessible(true); + Object impl = factory.get(null); - assertFalse(ServiceLoader.load(LoggerCreationService.class).iterator().hasNext()); + assertEquals(impl.getClass().getName(), + "org.openecomp.sdc.logging.api.LoggerFactory$NoOpLoggerCreationService"); + } - LoggerFactory.getLogger(LoggerFactoryTest.class); - Field factory = LoggerFactory.class.getDeclaredField("SERVICE"); - factory.setAccessible(true); - Object impl = factory.get(null); - assertEquals("org.openecomp.sdc.logging.api.LoggerFactory$NoOpLoggerCreationService", - impl.getClass().getName()); - } + @Test + public void verifyNoOpLoggerWorksWhenGotByClass() { + Logger logger = LoggerFactory.getLogger(LoggerFactoryTest.class); + verifyLoggerWorks(logger); + } - @Test - public void testNoOpLoggerByClass() throws Exception { - Logger logger = LoggerFactory.getLogger(LoggerFactoryTest.class); - verifyLogger(logger); - } + @Test + public void verifyNoOpLoggerWorksWhenGotByName() { + Logger logger = LoggerFactory.getLogger(LoggerFactoryTest.class.getName()); + verifyLoggerWorks(logger); + } - @Test - public void testNoOpLoggerByName() throws Exception { - Logger logger = LoggerFactory.getLogger(LoggerFactoryTest.class.getName()); - verifyLogger(logger); - } + @Test(expectedExceptions = NullPointerException.class) + public void throwNpeWhenGetByNameWithNull() { + LoggerFactory.getLogger((String) null); + } - private void verifyLogger(Logger logger) { - assertNotNull(logger); + @Test(expectedExceptions = NullPointerException.class) + public void throwNpeWhenGetByClassWithNull() { + LoggerFactory.getLogger((Class) null); + } - // make sure no exceptions are thrown - logger.error(""); - logger.warn(""); - logger.info(""); - logger.debug(""); - logger.audit(""); - logger.metrics(""); - } + private void verifyLoggerWorks(Logger logger) { + assertNotNull(logger); + // make sure no exceptions are thrown + logger.error(""); + logger.warn(""); + logger.info(""); + logger.debug(""); + logger.audit(""); + logger.metrics(""); + } } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggingContextTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggingContextTest.java new file mode 100644 index 0000000000..7899eebd51 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggingContextTest.java @@ -0,0 +1,101 @@ +/* + * 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.api; + +import org.testng.annotations.Test; + +import java.lang.reflect.Field; +import java.util.concurrent.Callable; + +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 LoggingContextTest { + + @Test + public void shouldHoldNoOpWhenNoBinding() throws Exception { + Field factory = LoggingContext.class.getDeclaredField("SERVICE"); + factory.setAccessible(true); + Object impl = factory.get(null); + assertEquals(impl.getClass().getName(), + "org.openecomp.sdc.logging.api.LoggingContext$NoOpLoggingContextService"); + } + + @Test + public void putDoesNotHaveEffectWhenNoBinding() { + final String key = "Key"; + LoggingContext.put(key, "Dummy"); + assertNull(LoggingContext.get(key)); + } + + @Test(expectedExceptions = NullPointerException.class) + public void throwNpeWhenPutWithKeyNull() { + LoggingContext.put(null, "value"); + } + + @Test + public void getAlwaysReturnsNull() { + assertNull(LoggingContext.get("GetKey")); + } + + @Test(expectedExceptions = NullPointerException.class) + public void throwNpeWhenGetWithKeyNull() { + LoggingContext.get(null); + } + + @Test + public void removeDoesNotFail() { + LoggingContext.remove("RemoveKey"); + } + + @Test(expectedExceptions = NullPointerException.class) + public void throwNpWhenRemoveWithKeyNull() { + LoggingContext.remove(null); + } + + @Test + public void clearDoesNotFail() { + LoggingContext.clear(); + } + + @Test + public void toRunnableReturnsSameInstance() { + Runnable test = () -> { /* do nothing */ }; + assertTrue(test == LoggingContext.toRunnable(test)); + } + + @Test(expectedExceptions = NullPointerException.class) + public void throwNpeWhenToRunnableWithNull() { + LoggingContext.toRunnable(null); + } + + @Test + public void toCallableReturnsSameInstance() { + Callable test = () -> ""; + assertTrue(test == LoggingContext.toCallable(test)); + } + + @Test(expectedExceptions = NullPointerException.class) + public void throwNpeWhenToCallableWithNull() { + LoggingContext.toCallable(null); + } +} \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/ServiceBinderTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/ServiceBinderTest.java new file mode 100644 index 0000000000..1a5c81d90d --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/ServiceBinderTest.java @@ -0,0 +1,38 @@ +/* + * 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.api; + +import org.testng.annotations.Test; + +import static org.testng.Assert.*; + +/** + * @author EVITALIY + * @since 08 Jan 18 + */ +public class ServiceBinderTest { + + @Test + public void makeSureNoContextServiceBinding() { + assertFalse(ServiceBinder.getContextServiceBinding().isPresent()); + } + + @Test + public void makeSureNoCreationServiceBinding() { + assertFalse(ServiceBinder.getCreationServiceBinding().isPresent()); + } +} \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/context/TaskFactoryTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/context/TaskFactoryTest.java deleted file mode 100644 index f5c2187024..0000000000 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/context/TaskFactoryTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.openecomp.sdc.logging.api.context; - -import org.testng.annotations.Test; - -import java.util.ServiceLoader; - -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; - -/** - * @author evitaliy - * @since 14/09/2016. - */ -public class TaskFactoryTest { - - @Test(expectedExceptions = RuntimeException.class) - public void testNoImplementation() throws Exception { - - assertFalse(ServiceLoader.load(ContextPropagationService.class).iterator().hasNext()); - - try { - TaskFactory.create(() -> { - }); - } catch (RuntimeException e) { - Throwable cause = e.getCause(); - assertNotNull(cause); - assertTrue(cause.getMessage().contains(ContextPropagationService.class.getName())); - throw e; - } - } -} \ No newline at end of file -- cgit 1.2.3-korg