From a969489e138c6fa44d468f05e8b27860c26c7578 Mon Sep 17 00:00:00 2001 From: mmis Date: Tue, 27 Feb 2018 14:05:23 +0000 Subject: Added junit tests in ONAP-Logging Added junit tests to org.onap.policy.common.logging.flexlogger package in ONAP-Logging Issue-ID: POLICY-582 Change-Id: I024f990960fa71bff7655da9c2cd5dd0db8ee33c Signed-off-by: mmis --- .../common/logging/eelf/PolicyLoggerTest.java | 18 +- .../common/logging/flexlogger/EelfLoggerTest.java | 364 ++++++++++++++ .../common/logging/flexlogger/FlexLoggerTest.java | 133 +++++ .../common/logging/flexlogger/Logger4JTest.java | 341 +++++++++++++ .../logging/flexlogger/PropertyUtilTest.java | 120 +++++ .../logging/flexlogger/SystemOutLoggerTest.java | 541 +++++++++++++++++++++ .../onap/policy/common/logging/util/TestUtils.java | 57 +++ 7 files changed, 1557 insertions(+), 17 deletions(-) create mode 100644 common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/EelfLoggerTest.java create mode 100644 common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/FlexLoggerTest.java create mode 100644 common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/Logger4JTest.java create mode 100644 common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/PropertyUtilTest.java create mode 100644 common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/SystemOutLoggerTest.java create mode 100644 common-logging/src/test/java/org/onap/policy/common/logging/util/TestUtils.java (limited to 'common-logging/src/test') diff --git a/common-logging/src/test/java/org/onap/policy/common/logging/eelf/PolicyLoggerTest.java b/common-logging/src/test/java/org/onap/policy/common/logging/eelf/PolicyLoggerTest.java index 910e0ba2..18361ac1 100644 --- a/common-logging/src/test/java/org/onap/policy/common/logging/eelf/PolicyLoggerTest.java +++ b/common-logging/src/test/java/org/onap/policy/common/logging/eelf/PolicyLoggerTest.java @@ -33,8 +33,7 @@ import static org.onap.policy.common.logging.eelf.Configuration.RESPONSE_CODE; import static org.onap.policy.common.logging.eelf.Configuration.RESPONSE_DESCRIPTION; import static org.onap.policy.common.logging.eelf.Configuration.SERVER_NAME; import static org.onap.policy.common.logging.eelf.Configuration.STATUS_CODE; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; +import static org.onap.policy.common.logging.util.TestUtils.*; import java.time.Instant; import java.util.Properties; import java.util.UUID; @@ -664,20 +663,5 @@ public class PolicyLoggerTest { PolicyLogger.setServerInfo("serverHost", "serverPort"); assertEquals("serverHost:serverPort", MDC.get(SERVER_NAME)); } - - private static void overrideStaticField(@SuppressWarnings("rawtypes") final Class clazz, final String fieldName, final Object newValue) { - try { - final Field field = clazz.getDeclaredField(fieldName); - field.setAccessible(true); - - final Field modifiersField = Field.class.getDeclaredField("modifiers"); - modifiersField.setAccessible(true); - modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); - - field.set(null, newValue); - } catch (final Exception e) { - fail(e.toString()); - } - } } diff --git a/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/EelfLoggerTest.java b/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/EelfLoggerTest.java new file mode 100644 index 00000000..6d6399b2 --- /dev/null +++ b/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/EelfLoggerTest.java @@ -0,0 +1,364 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-Logging + * ================================================================================ + * Copyright (C) 2018 Ericsson. 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.onap.policy.common.logging.flexlogger; + +import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID; +import static org.junit.Assert.*; +import static org.onap.policy.common.logging.util.TestUtils.overrideStaticField; +import java.util.UUID; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.policy.common.logging.eelf.MessageCodes; +import org.onap.policy.common.logging.eelf.PolicyLogger; +import org.slf4j.MDC; +import com.att.eelf.configuration.EELFLogger; + +public class EelfLoggerTest { + + EelfLogger eelfLogger = new EelfLogger("EelfLoggerTest", "transactionId"); + + @Test + public void testEelfLoggerClassOfQ() { + new EelfLogger(this.getClass()); + assertNotNull(PolicyLogger.getTransId()); + assertFalse(PolicyLogger.getTransId().isEmpty()); + } + + @Test + public void testEelfLoggerString() { + new EelfLogger("EelfLoggerTest"); + assertNotNull(PolicyLogger.getTransId()); + assertFalse(PolicyLogger.getTransId().isEmpty()); + } + + @Test + public void testEelfLoggerClassOfQBoolean() { + new EelfLogger(this.getClass(), true); + assertNotNull(PolicyLogger.getTransId()); + assertFalse(PolicyLogger.getTransId().isEmpty()); + } + + @Test + public void testEelfLoggerStringBoolean() { + new EelfLogger("EelfLoggerTest", true); + assertNotNull(PolicyLogger.getTransId()); + assertFalse(PolicyLogger.getTransId().isEmpty()); + } + + @Test + public void testEelfLoggerClassOfQString() { + new EelfLogger(this.getClass(), "transactionId"); + assertEquals("transactionId", PolicyLogger.getTransId()); + } + + @Test + public void testEelfLoggerStringString() { + new EelfLogger("EelfLoggerTest", "transactionId"); + assertEquals("transactionId", PolicyLogger.getTransId()); + } + + @Test + public void testSetAndGetTransId() { + assertEquals("transactionId", eelfLogger.getTransId()); + eelfLogger.setTransId("transactionId2"); + assertEquals("transactionId2", eelfLogger.getTransId()); + } + + @Test + public void testDebugObject() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "debugLogger", mockLogger); + eelfLogger.debug("message"); + Mockito.verify(mockLogger).debug(MessageCodes.GENERAL_INFO, "message"); + } + + @Test + public void testErrorObject() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "errorLogger", mockLogger); + eelfLogger.error("message"); + Mockito.verify(mockLogger).error(MessageCodes.GENERAL_ERROR, "message"); + } + + @Test + public void testInfoObject() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "debugLogger", mockLogger); + eelfLogger.info("message"); + Mockito.verify(mockLogger).info(MessageCodes.GENERAL_INFO, "message"); + } + + @Test + public void testWarnObject() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "debugLogger", mockLogger); + eelfLogger.warn("message"); + Mockito.verify(mockLogger).warn(MessageCodes.GENERAL_INFO, "message"); + } + + @Test + public void testTraceObject() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "debugLogger", mockLogger); + eelfLogger.trace("message"); + Mockito.verify(mockLogger).trace(MessageCodes.GENERAL_INFO, "message"); + } + + @Test + public void testIsDebugEnabled() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "debugLogger", mockLogger); + Mockito.when(mockLogger.isDebugEnabled()).thenReturn(false).thenReturn(true); + assertFalse(eelfLogger.isDebugEnabled()); + assertTrue(eelfLogger.isDebugEnabled()); + + } + + @Test + public void testIsInfoEnabled() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "debugLogger", mockLogger); + Mockito.when(mockLogger.isInfoEnabled()).thenReturn(false).thenReturn(true); + assertFalse(eelfLogger.isInfoEnabled()); + assertTrue(eelfLogger.isInfoEnabled()); + } + + @Test + public void testIsWarnEnabled() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "debugLogger", mockLogger); + Mockito.when(mockLogger.isWarnEnabled()).thenReturn(false).thenReturn(true); + assertFalse(eelfLogger.isWarnEnabled()); + assertTrue(eelfLogger.isWarnEnabled()); + } + + @Test + public void testIsErrorEnabled() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "errorLogger", mockLogger); + Mockito.when(mockLogger.isErrorEnabled()).thenReturn(false).thenReturn(true); + assertFalse(eelfLogger.isErrorEnabled()); + assertTrue(eelfLogger.isErrorEnabled()); + } + + @Test + public void testIsAuditEnabled() { + PolicyLogger.setOverrideLogbackLevel(true); + PolicyLogger.setAuditLevel("ERROR"); + assertTrue(eelfLogger.isAuditEnabled()); + } + + @Test + public void testIsMetricsEnabled() { + PolicyLogger.setOverrideLogbackLevel(true); + PolicyLogger.setMetricsLevel("ERROR"); + assertTrue(eelfLogger.isMetricsEnabled()); + } + + @Test + public void testIsTraceEnabled() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "debugLogger", mockLogger); + Mockito.when(mockLogger.isDebugEnabled()).thenReturn(false).thenReturn(true); + assertFalse(eelfLogger.isTraceEnabled()); + assertTrue(eelfLogger.isTraceEnabled()); + } + + @Test + public void testAuditObject() { + PolicyLogger.setOverrideLogbackLevel(true); + PolicyLogger.setAuditLevel("ERROR"); + assertTrue(eelfLogger.isAuditEnabled()); + } + + @Test + public void testDebugObjectThrowable() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "debugLogger", mockLogger); + eelfLogger.debug("message", new NullPointerException()); + Mockito.verify(mockLogger).debug((MessageCodes)Mockito.any(), Mockito.startsWith("message:java.lang.NullPointerException")); + } + + @Test + public void testErrorObjectThrowable() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "errorLogger", mockLogger); + eelfLogger.error("message", new NullPointerException()); + Mockito.verify(mockLogger).error((MessageCodes)Mockito.any(), Mockito.startsWith("message:java.lang.NullPointerException")); + eelfLogger.error("message", new NullPointerException()); + } + + @Test + public void testInfoObjectThrowable() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "debugLogger", mockLogger); + eelfLogger.info("message", new NullPointerException()); + Mockito.verify(mockLogger).info((MessageCodes)Mockito.any(), Mockito.startsWith("message:java.lang.NullPointerException")); + } + + @Test + public void testWarnObjectThrowable() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "debugLogger", mockLogger); + eelfLogger.warn("message", new NullPointerException()); + Mockito.verify(mockLogger).warn((MessageCodes)Mockito.any(), Mockito.startsWith("message:java.lang.NullPointerException")); + } + + @Test + public void testTraceObjectThrowable() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "debugLogger", mockLogger); + eelfLogger.trace("message", new NullPointerException()); + Mockito.verify(mockLogger).trace("message"); + } + + @Test + public void testAuditObjectThrowable() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "auditLogger", mockLogger); + eelfLogger.audit("message", new NullPointerException()); + Mockito.verify(mockLogger).info("message"); + } + + @Test + public void testRecordAuditEventStartString() { + eelfLogger.recordAuditEventStart("eventId"); + assertNotNull(PolicyLogger.getEventTracker().getEventInfo().get("eventId")); + } + + @Test + public void testRecordAuditEventStartUUID() { + UUID uuid = UUID.randomUUID(); + eelfLogger.recordAuditEventStart(uuid); + assertNotNull(PolicyLogger.getEventTracker().getEventInfo().get(uuid.toString())); + } + + @Test + public void testRecordAuditEventEndStringStringString() { + eelfLogger.recordAuditEventStart("eventId"); + assertNotNull(PolicyLogger.getEventTracker().getEventInfo().get("eventId")); + + eelfLogger.recordAuditEventEnd("eventId", "rule", "policyVersion"); + assertNull(PolicyLogger.getEventTracker().getEventInfo().get("eventId")); + } + + @Test + public void testRecordAuditEventEndUUIDStringString() { + UUID uuid = UUID.randomUUID(); + eelfLogger.recordAuditEventStart(uuid);; + assertNotNull(PolicyLogger.getEventTracker().getEventInfo().get(uuid.toString())); + + eelfLogger.recordAuditEventEnd(uuid, "rule", "policyVersion"); + assertNull(PolicyLogger.getEventTracker().getEventInfo().get(uuid.toString())); + } + + @Test + public void testRecordAuditEventEndStringString() { + eelfLogger.recordAuditEventStart("eventId"); + assertNotNull(PolicyLogger.getEventTracker().getEventInfo().get("eventId")); + + eelfLogger.recordAuditEventEnd("eventId", "rule"); + assertNull(PolicyLogger.getEventTracker().getEventInfo().get("eventId")); + } + + @Test + public void testRecordAuditEventEndUUIDString() { + UUID uuid = UUID.randomUUID(); + eelfLogger.recordAuditEventStart(uuid);; + assertNotNull(PolicyLogger.getEventTracker().getEventInfo().get(uuid.toString())); + + eelfLogger.recordAuditEventEnd(uuid, "rule"); + assertNull(PolicyLogger.getEventTracker().getEventInfo().get(uuid.toString())); + } + + @Test + public void testRecordMetricEventStringString() { + eelfLogger.recordMetricEvent("eventId", "str1"); + assertEquals("eventId", MDC.get(MDC_KEY_REQUEST_ID)); + } + + @Test + public void testRecordMetricEventUUIDString() { + UUID uuid = UUID.randomUUID(); + eelfLogger.recordMetricEvent(uuid, "str2"); + assertEquals(uuid.toString(), MDC.get(MDC_KEY_REQUEST_ID)); + } + + @Test + public void testMetrics() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "metricsLogger", mockLogger); + eelfLogger.metrics(1); + Mockito.verify(mockLogger).info(Mockito.eq(MessageCodes.RULE_METRICS_INFO), Mockito.anyString(), Mockito.eq("1")); + } + + @Test + public void testErrorMessageCodesThrowableStringArray() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "errorLogger", mockLogger); + eelfLogger.error(MessageCodes.GENERAL_ERROR, new NullPointerException(), "str1", "str2"); + Mockito.verify(mockLogger).error((MessageCodes)Mockito.any(), Mockito.startsWith("str1:str2:java.lang.NullPointerException")); + } + + @Test + public void testErrorMessageCodesStringArray() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "errorLogger", mockLogger); + eelfLogger.error(MessageCodes.GENERAL_ERROR, "str1", "str2"); + Mockito.verify(mockLogger).error(MessageCodes.GENERAL_ERROR, "str1", "str2"); + + } + + @Test + public void testPostMDCInfoForEventString() { + eelfLogger.postMDCInfoForEvent("transactionId"); + assertEquals("transactionId", MDC.get(MDC_KEY_REQUEST_ID)); + } + + @Test + public void testWarnMessageCodesStringArray() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "debugLogger", mockLogger); + eelfLogger.warn(MessageCodes.GENERAL_ERROR, "str1", "str2"); + Mockito.verify(mockLogger).warn(MessageCodes.GENERAL_ERROR, "str1", "str2"); + } + + @Test + public void testWarnMessageCodesThrowableStringArray() { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + overrideStaticField(PolicyLogger.class, "debugLogger", mockLogger); + eelfLogger.warn(MessageCodes.GENERAL_ERROR, new NullPointerException(), "str1", "str2"); + Mockito.verify(mockLogger).warn((MessageCodes)Mockito.any(), Mockito.startsWith("str1:str2:java.lang.NullPointerException")); + + } + + @Test + public void testPostMDCInfoForTriggeredRule() { + eelfLogger.postMDCInfoForTriggeredRule("transactionId"); + assertEquals("transactionId", MDC.get(MDC_KEY_REQUEST_ID)); + } + + @Test + public void testPostMDCInfoForEventObject() { + eelfLogger.postMDCInfoForEvent(1); + assertEquals("1", MDC.get(MDC_KEY_REQUEST_ID)); + } + +} diff --git a/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/FlexLoggerTest.java b/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/FlexLoggerTest.java new file mode 100644 index 00000000..af7edcab --- /dev/null +++ b/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/FlexLoggerTest.java @@ -0,0 +1,133 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-Logging + * ================================================================================ + * Copyright (C) 2018 Ericsson. 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.onap.policy.common.logging.flexlogger; + +import static org.junit.Assert.*; +import static org.onap.policy.common.logging.util.TestUtils.*; +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; +import org.junit.Test; +import org.onap.policy.common.logging.flexlogger.FlexLogger.PropertiesCallBack; + +public class FlexLoggerTest { + + @Test + public void testGetLoggerClassOfQEelf() { + overrideStaticField(FlexLogger.class, "loggerType", LoggerType.EELF); + Logger logger = FlexLogger.getLogger((Class)null); + assertSame(logger, FlexLogger.getLogger((Class)null)); + assertNotEquals(logger, FlexLogger.getLogger(String.class)); + } + + @Test + public void testGetLoggerClassOfQLog4j() { + overrideStaticField(FlexLogger.class, "loggerType", LoggerType.LOG4J); + Logger logger = FlexLogger.getLogger(this.getClass()); + assertSame(logger, FlexLogger.getLogger(this.getClass())); + } + + @Test + public void testGetLoggerClassOfQSystemOut() { + overrideStaticField(FlexLogger.class, "loggerType", LoggerType.SYSTEMOUT); + Logger logger = FlexLogger.getLogger(this.getClass()); + assertSame(logger, FlexLogger.getLogger(this.getClass())); + } + + @Test + public void testGetLoggerStringEelf() { + overrideStaticField(FlexLogger.class, "loggerType", LoggerType.EELF); + Logger logger = FlexLogger.getLogger("str1"); + assertSame(logger, FlexLogger.getLogger("str1")); + } + + @Test + public void testGetLoggerStringLog4j() { + overrideStaticField(FlexLogger.class, "loggerType", LoggerType.LOG4J); + Logger logger = FlexLogger.getLogger("str1"); + assertSame(logger, FlexLogger.getLogger("str1")); + } + + @Test + public void testGetLoggerStringSystemOut() { + overrideStaticField(FlexLogger.class, "loggerType", LoggerType.SYSTEMOUT); + Logger logger = FlexLogger.getLogger("str1"); + assertSame(logger, FlexLogger.getLogger("str1")); + } + + @Test + public void testGetLoggerClassOfQBooleanEelf() { + overrideStaticField(FlexLogger.class, "loggerType", LoggerType.EELF); + Logger logger = FlexLogger.getLogger(this.getClass(), true); + assertSame(logger, FlexLogger.getLogger(this.getClass(), true)); + } + + @Test + public void testGetLoggerClassOfQBooleanLog4j() { + overrideStaticField(FlexLogger.class, "loggerType", LoggerType.LOG4J); + Logger logger = FlexLogger.getLogger(this.getClass(), true); + assertSame(logger, FlexLogger.getLogger(this.getClass(), true)); + } + + @Test + public void testGetLoggerClassOfQBooleanSystemOut() { + overrideStaticField(FlexLogger.class, "loggerType", LoggerType.SYSTEMOUT); + Logger logger = FlexLogger.getLogger(this.getClass(), true); + assertSame(logger, FlexLogger.getLogger(this.getClass(), true)); + } + + @Test + public void testGetLoggerStringBooleanEelf() { + overrideStaticField(FlexLogger.class, "loggerType", LoggerType.EELF); + Logger logger = FlexLogger.getLogger("str1", true); + assertSame(logger, FlexLogger.getLogger("str1", true)); + } + + @Test + public void testGetLoggerStringBooleanLog4j() { + overrideStaticField(FlexLogger.class, "loggerType", LoggerType.LOG4J); + Logger logger = FlexLogger.getLogger("str1", true); + assertSame(logger, FlexLogger.getLogger("str1", true)); + } + + @Test + public void testGetLoggerStringBooleanSystemOut() { + overrideStaticField(FlexLogger.class, "loggerType", LoggerType.SYSTEMOUT); + Logger logger = FlexLogger.getLogger("str1", true); + assertSame(logger, FlexLogger.getLogger("str1", true)); + } + + @Test + public void testGetClassName() { + assertNotEquals("FlexLogger", new FlexLogger().getClassName()); + } + + @Test + public void testPropertiesCallBack() throws IOException{ + PropertiesCallBack propertiesCallBack = new PropertiesCallBack("name"); + Set changedKeys = new HashSet<>(); + changedKeys.add("debugLogger.level"); + changedKeys.add("metricsLogger.level"); + changedKeys.add("error.level"); + changedKeys.add("audit.level"); + propertiesCallBack.propertiesChanged(PropertyUtil.getProperties("config/policyLogger.properties"), changedKeys); + } + +} diff --git a/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/Logger4JTest.java b/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/Logger4JTest.java new file mode 100644 index 00000000..3650b18a --- /dev/null +++ b/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/Logger4JTest.java @@ -0,0 +1,341 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-Logging + * ================================================================================ + * Copyright (C) 2018 Ericsson. 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.onap.policy.common.logging.flexlogger; + +import static org.junit.Assert.*; +import java.util.UUID; +import org.apache.log4j.Logger; +import org.apache.log4j.Priority; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.policy.common.logging.eelf.MessageCodes; +import org.onap.policy.common.logging.util.TestUtils; + +public class Logger4JTest { + + private Logger4J logger4J = new Logger4J("str1", "Logger4JTest"); + + @Test + public void testLogger4JClassOfQ() { + new Logger4J(this.getClass()); + } + + @Test + public void testSetAndGetTransId() { + logger4J.setTransId("transactionId"); + assertEquals("transactionId", logger4J.getTransId()); + } + + @Test + public void testDebugObject() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + Mockito.when(logger.isDebugEnabled()).thenReturn(true); + logger4J.setTransId("transactionId"); + logger4J.debug("message"); + Mockito.verify(logger).debug("transactionId|message"); + } + + @Test + public void testErrorObject() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + logger4J.setTransId("transactionId"); + logger4J.error("message"); + Mockito.verify(logger).error("transactionId|Logger4JTest|message"); + } + + @Test + public void testInfoObject() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + logger4J.setTransId("transactionId"); + logger4J.info("message"); + Mockito.verify(logger).info("transactionId|Logger4JTest|message"); + } + + @Test + public void testWarnObject() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + logger4J.setTransId("transactionId"); + logger4J.warn("message"); + Mockito.verify(logger).warn("transactionId|Logger4JTest|message"); + } + + @Test + public void testTraceObject() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + logger4J.setTransId("transactionId"); + logger4J.trace("message"); + Mockito.verify(logger).trace("transactionId|Logger4JTest|message"); + } + + @Test + public void testIsDebugEnabled() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + Mockito.when(logger.isDebugEnabled()).thenReturn(true).thenReturn(false); + assertTrue(logger4J.isDebugEnabled()); + assertFalse(logger4J.isDebugEnabled()); + } + + @Test + public void testIsErrorEnabled() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + Mockito.when(logger.isEnabledFor(Priority.ERROR)).thenReturn(true).thenReturn(false); + assertTrue(logger4J.isErrorEnabled()); + assertFalse(logger4J.isErrorEnabled()); + } + + @Test + public void testIsInfoEnabled() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + Mockito.when(logger.isInfoEnabled()).thenReturn(true).thenReturn(false); + assertTrue(logger4J.isInfoEnabled()); + assertFalse(logger4J.isInfoEnabled()); + } + + @Test + public void testIsWarnEnabled() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + Mockito.when(logger.isEnabledFor(Priority.WARN)).thenReturn(true).thenReturn(false); + assertTrue(logger4J.isWarnEnabled()); + assertFalse(logger4J.isWarnEnabled()); + } + + @Test + public void testAuditObject() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + logger4J.audit("str1"); + Mockito.verify(logger).info("Logger4JTest|str1"); + } + + @Test + public void testRecordAuditEventStartString() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + logger4J.recordAuditEventEnd("eventId", "rule"); + Mockito.verify(logger).info("Logger4JTest|eventId:rule"); + } + + @Test + public void testRecordAuditEventStartUUID() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + UUID uuid = UUID.randomUUID(); + logger4J.recordAuditEventStart(uuid); + Mockito.verify(logger).info("Logger4JTest|recordAuditEventStart with eventId " + uuid.toString()); + } + + @Test + public void testRecordAuditEventEndStringStringString() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + logger4J.recordAuditEventEnd("eventId", "rule", "policyVersion"); + Mockito.verify(logger).info("Logger4JTest|eventId:rule"); + } + + @Test + public void testRecordAuditEventEndUUIDStringString() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + UUID uuid = UUID.randomUUID(); + logger4J.recordAuditEventEnd(uuid, "rule", "policyVersion"); + Mockito.verify(logger).info("Logger4JTest|" + uuid.toString() + ":rule"); + } + + @Test + public void testRecordAuditEventEndStringString() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + logger4J.recordAuditEventEnd("eventId", "rule"); + Mockito.verify(logger).info("Logger4JTest|eventId:rule"); + } + + @Test + public void testRecordAuditEventEndUUIDString() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + UUID uuid = UUID.randomUUID(); + logger4J.recordAuditEventEnd(uuid, "rule"); + Mockito.verify(logger).info("Logger4JTest|" + uuid.toString() + ":rule"); + } + + @Test + public void testRecordMetricEventStringString() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + logger4J.recordMetricEvent("eventId", "str1"); + Mockito.verify(logger).info("Logger4JTest|eventId:str1"); + } + + @Test + public void testRecordMetricEventUUIDString() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + UUID uuid = UUID.randomUUID(); + logger4J.recordMetricEvent(uuid, "str1"); + Mockito.verify(logger).info("Logger4JTest|" + uuid.toString() + ":str1"); + } + + @Test + public void testMetrics() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + logger4J.metrics("str1"); + Mockito.verify(logger).info("str1"); + } + + @Test + public void testErrorMessageCodesThrowableStringArray() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + logger4J.setTransId("transactionId"); + logger4J.error(MessageCodes.GENERAL_ERROR, new NullPointerException(), "str1", "str2"); + Mockito.verify(logger).error("transactionId|Logger4JTest|MessageCodes :" + MessageCodes.GENERAL_ERROR + "[str1, str2]"); + } + + @Test + public void testErrorMessageCodesStringArray() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + logger4J.setTransId("transactionId"); + logger4J.error(MessageCodes.GENERAL_ERROR, "str1", "str2"); + Mockito.verify(logger).error("transactionId|Logger4JTest|MessageCode:" + MessageCodes.GENERAL_ERROR + "[str1, str2]"); + } + + @Test + public void testPostMDCInfoForEventString() { + String returnedTransactionId = logger4J.postMDCInfoForEvent("transactionId"); + assertEquals("transactionId", returnedTransactionId); + } + + @Test + public void testPostMDCInfoForEventEmptyString() { + String returnedTransactionId = logger4J.postMDCInfoForEvent(""); + assertNotNull("", returnedTransactionId); + assertNotEquals("", returnedTransactionId); + } + + @Test + public void testWarnMessageCodesStringArray() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + logger4J.warn(MessageCodes.GENERAL_ERROR, "str1", "str2"); + Mockito.verify(logger).warn("Logger4JTest|MessageCodes:" + MessageCodes.GENERAL_ERROR + "[str1, str2]"); + } + + @Test + public void testWarnMessageCodesThrowableStringArray() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + logger4J.setTransId("transactionId"); + logger4J.warn(MessageCodes.GENERAL_ERROR, new NullPointerException(), "str1", "str2"); + Mockito.verify(logger).warn("Logger4JTest|MessageCodes:" + MessageCodes.GENERAL_ERROR + "[str1, str2]"); + } + + @Test + public void testDebugObjectThrowable() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + logger4J.setTransId("transactionId"); + Exception exception = new NullPointerException(); + logger4J.debug("message", exception); + Mockito.verify(logger).debug("message", exception); + } + + @Test + public void testErrorObjectThrowable() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + logger4J.setTransId("transactionId"); + Exception exception = new NullPointerException(); + logger4J.error("message", exception); + Mockito.verify(logger).error("message", exception); + } + + @Test + public void testInfoObjectThrowable() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + logger4J.setTransId("transactionId"); + Exception exception = new NullPointerException(); + logger4J.info("message", exception); + Mockito.verify(logger).info("message", exception); + } + + @Test + public void testWarnObjectThrowable() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + Exception exception = new NullPointerException(); + logger4J.warn("message", exception); + Mockito.verify(logger).warn("message", exception); + } + + @Test + public void testTraceObjectThrowable() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + Exception exception = new NullPointerException(); + logger4J.trace("message", exception); + Mockito.verify(logger).trace("message", exception); + } + + @Test + public void testAuditObjectThrowable() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + Exception exception = new NullPointerException(); + logger4J.audit("message", exception); + Mockito.verify(logger).info("message", exception); + } + + @Test + public void testIsTraceEnabled() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + logger4J.isTraceEnabled(); + Mockito.verify(logger).isTraceEnabled(); + } + + @Test + public void testPostMDCInfoForTriggeredRule() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + logger4J.postMDCInfoForTriggeredRule("transactionId"); + Mockito.verify(logger).info("transactionId"); + } + + @Test + public void testPostMDCInfoForEventObject() { + Logger logger = Mockito.mock(Logger.class); + TestUtils.overrideField(Logger4J.class, logger4J, "log", logger); + logger4J.postMDCInfoForEvent(1); + Mockito.verify(logger).info(1); + } + +} diff --git a/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/PropertyUtilTest.java b/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/PropertyUtilTest.java new file mode 100644 index 00000000..b4fd5362 --- /dev/null +++ b/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/PropertyUtilTest.java @@ -0,0 +1,120 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-Logging + * ================================================================================ + * Copyright (C) 2018 Ericsson. 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.onap.policy.common.logging.flexlogger; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Properties; +import java.util.Set; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.logging.flexlogger.PropertyUtil.Listener; + +public class PropertyUtilTest { + + private static final File FILE = new File("target/test.properties"); + private TestListener testListener = new TestListener(); + + @Before + public void setUp() throws IOException{ + FileOutputStream fileOutputStream = new FileOutputStream(FILE); + Properties properties = new Properties(); + properties.put("testProperty", "testValue"); + properties.store(fileOutputStream, ""); + fileOutputStream.close(); + } + + @After + public void tearDown() throws IOException{ + PropertyUtil.stopListening(FILE, testListener); + FILE.delete(); + } + + @Test + public void testGetProperties() throws IOException{ + FileOutputStream fileOutputStream = new FileOutputStream(FILE); + Properties properties = new Properties(); + properties.put("testProperty", "testValue"); + properties.store(fileOutputStream, ""); + fileOutputStream.close(); + + Properties readProperties = PropertyUtil.getProperties(FILE, testListener); + assertEquals("testValue", readProperties.getProperty("testProperty")); + } + + @Test + public void testPropertiesChanged() throws IOException, InterruptedException{ + PropertyUtil.getProperties(FILE, testListener); + + FileOutputStream fileOutputStream = new FileOutputStream(FILE); + Properties newProperties = new Properties(); + newProperties.put("testProperty", "testValueNew"); + newProperties.store(fileOutputStream, ""); + + assertTrue(testListener.isPropertiesChangedInvoked()); + + } + + @Test + public void testStopListening() throws IOException{ + FileOutputStream fileOutputStream = new FileOutputStream(FILE); + Properties properties = new Properties(); + properties.put("testProperty", "testValue"); + properties.store(fileOutputStream, ""); + + Properties readProperties = PropertyUtil.getProperties(FILE, testListener); + assertEquals("testValue", readProperties.getProperty("testProperty")); + + PropertyUtil.stopListening(FILE, testListener); + + properties.put("testProperty", "testValueNew"); + properties.store(fileOutputStream, ""); + fileOutputStream.close(); + readProperties = PropertyUtil.getProperties(FILE, testListener); + // If stopListening did not remove the listener, the properties file will not be re-read until poll expires and + // hence "testValue" will be returned here instead of "testNewValue" + assertEquals("testValueNew", readProperties.getProperty("testProperty")); + } + + private class TestListener implements Listener { + + boolean propertiesChangedInvoked = false; + + @Override + public void propertiesChanged(Properties properties, Set changedKeys) { + propertiesChangedInvoked = true; + } + + public boolean isPropertiesChangedInvoked() throws InterruptedException{ + for (int i =0; i<20; i++){ + if (propertiesChangedInvoked){ + return true; + } + Thread.sleep(1000); + } + return false; + } + } + +} diff --git a/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/SystemOutLoggerTest.java b/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/SystemOutLoggerTest.java new file mode 100644 index 00000000..ceec3cce --- /dev/null +++ b/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/SystemOutLoggerTest.java @@ -0,0 +1,541 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-Logging + * ================================================================================ + * Copyright (C) 2018 Ericsson. 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.onap.policy.common.logging.flexlogger; + +import static org.junit.Assert.*; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.Test; +import org.onap.policy.common.logging.eelf.MessageCodes; +import org.onap.policy.common.logging.eelf.PolicyLogger; +import com.att.eelf.configuration.EELFLogger.Level; + +public class SystemOutLoggerTest { + + SystemOutLogger systemOutLogger = new SystemOutLogger("SystemOutLoggerTest"); + + @Test + public void testSystemOutLoggerClassOfQ() { + new SystemOutLogger(SystemOutLoggerTest.class); + } + + @Test + public void testSetAndGetTransId() { + systemOutLogger.setTransId("transactionId"); + assertEquals("transactionId", systemOutLogger.getTransId()); + } + + @Test + public void testDebugObject() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + systemOutLogger.setTransId("transactionId"); + System.setOut(ps); + systemOutLogger.debug("message"); + assertTrue(baos.toString().contains("transactionId|SystemOutLoggerTest : message")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testErrorObject() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + systemOutLogger.setTransId("transactionId"); + System.setOut(ps); + systemOutLogger.error("message"); + assertTrue(baos.toString().contains("transactionId|SystemOutLoggerTest : message")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testInfoObject() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + systemOutLogger.setTransId("transactionId"); + System.setOut(ps); + systemOutLogger.info("message"); + assertTrue(baos.toString().contains("transactionId|SystemOutLoggerTest : message")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testWarnObject() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + systemOutLogger.setTransId("transactionId"); + System.setOut(ps); + systemOutLogger.warn("message"); + assertTrue(baos.toString().contains("transactionId|SystemOutLoggerTest : message")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testTraceObject() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + systemOutLogger.setTransId("transactionId"); + System.setOut(ps); + systemOutLogger.trace("message"); + assertTrue(baos.toString().contains("transactionId|SystemOutLoggerTest : message")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testIsDebugEnabled() { + PolicyLogger.setDebugLevel(Level.DEBUG); + systemOutLogger = new SystemOutLogger("SystemOutLoggerTest"); + assertTrue(systemOutLogger.isDebugEnabled()); + PolicyLogger.setDebugLevel(Level.INFO); + systemOutLogger = new SystemOutLogger("SystemOutLoggerTest"); + assertFalse(systemOutLogger.isDebugEnabled()); + } + + @Test + public void testIsWarnEnabled() { + PolicyLogger.setDebugLevel(Level.WARN); + systemOutLogger = new SystemOutLogger("SystemOutLoggerTest"); + assertTrue(systemOutLogger.isWarnEnabled()); + PolicyLogger.setDebugLevel(Level.OFF); + systemOutLogger = new SystemOutLogger("SystemOutLoggerTest"); + assertFalse(systemOutLogger.isWarnEnabled()); + } + + @Test + public void testIsInfoEnabled() { + PolicyLogger.setDebugLevel(Level.INFO); + systemOutLogger = new SystemOutLogger("SystemOutLoggerTest"); + assertTrue(systemOutLogger.isInfoEnabled()); + PolicyLogger.setDebugLevel(Level.OFF); + systemOutLogger = new SystemOutLogger("SystemOutLoggerTest"); + assertFalse(systemOutLogger.isInfoEnabled()); + } + + @Test + public void testIsErrorEnabled() { + PolicyLogger.setErrorLevel(Level.ERROR); + systemOutLogger = new SystemOutLogger("SystemOutLoggerTest"); + assertTrue(systemOutLogger.isErrorEnabled()); + PolicyLogger.setErrorLevel(Level.OFF); + systemOutLogger = new SystemOutLogger("SystemOutLoggerTest"); + assertFalse(systemOutLogger.isErrorEnabled()); + } + + @Test + public void testIsAuditEnabled() { + PolicyLogger.setAuditLevel(Level.INFO); + systemOutLogger = new SystemOutLogger("SystemOutLoggerTest"); + assertTrue(systemOutLogger.isAuditEnabled()); + PolicyLogger.setAuditLevel(Level.OFF); + systemOutLogger = new SystemOutLogger("SystemOutLoggerTest"); + assertFalse(systemOutLogger.isAuditEnabled()); + } + + @Test + public void testIsMetricsEnabled() { + PolicyLogger.setMetricsLevel(Level.INFO); + systemOutLogger = new SystemOutLogger("SystemOutLoggerTest"); + assertTrue(systemOutLogger.isMetricsEnabled()); + PolicyLogger.setMetricsLevel(Level.OFF); + systemOutLogger = new SystemOutLogger("SystemOutLoggerTest"); + assertFalse(systemOutLogger.isMetricsEnabled()); + } + + @Test + public void testAuditObject() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + systemOutLogger.setTransId("transactionId"); + System.setOut(ps); + systemOutLogger.audit("message"); + assertTrue(baos.toString().contains("transactionId|SystemOutLoggerTest : message")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testRecordAuditEventStartString() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + systemOutLogger.setTransId("transactionId"); + System.setOut(ps); + systemOutLogger.recordAuditEventStart("eventId"); + assertTrue(baos.toString().contains("transactionId|SystemOutLoggerTest : eventId")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testRecordAuditEventStartUUID() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + UUID uuid = UUID.randomUUID(); + System.setOut(ps); + systemOutLogger.recordAuditEventStart(uuid); + assertTrue(baos.toString().contains(uuid.toString())); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testRecordAuditEventEndStringStringString() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + System.setOut(ps); + systemOutLogger.recordAuditEventEnd("eventId", "rule", "policyVersion"); + assertTrue(baos.toString().contains("SystemOutLoggerTest : eventId:rule:policyVersion")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testRecordAuditEventEndUUIDStringString() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + UUID uuid = UUID.randomUUID(); + System.setOut(ps); + systemOutLogger.recordAuditEventEnd(uuid, "rule", "policyVersion"); + assertTrue(baos.toString().contains("SystemOutLoggerTest : " + uuid + ":rule:policyVersion")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testRecordAuditEventEndStringString() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + System.setOut(ps); + systemOutLogger.recordAuditEventEnd("eventId", "rule"); + assertTrue(baos.toString().contains("SystemOutLoggerTest : eventId:rule")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testRecordAuditEventEndUUIDString() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + UUID uuid = UUID.randomUUID(); + System.setOut(ps); + systemOutLogger.recordAuditEventEnd(uuid, "rule"); + assertTrue(baos.toString().contains("SystemOutLoggerTest : " + uuid + ":rule")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testRecordMetricEventStringString() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + System.setOut(ps); + systemOutLogger.recordMetricEvent("eventId", "rule"); + assertTrue(baos.toString(), baos.toString().contains("SystemOutLoggerTest : eventId:eventIdmessage:rule")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testRecordMetricEventUUIDString() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + UUID uuid = UUID.randomUUID(); + System.setOut(ps); + systemOutLogger.recordMetricEvent(uuid, "str1"); + assertTrue(baos.toString().contains("SystemOutLoggerTest : " + uuid + ":str1")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testMetrics() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + System.setOut(ps); + systemOutLogger.metrics("message"); + assertTrue(baos.toString().contains("SystemOutLoggerTest : message")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testErrorMessageCodesThrowableStringArray() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + System.setOut(ps); + systemOutLogger.error(MessageCodes.ERROR_DATA_ISSUE, new NullPointerException(), "str1", "str2"); + assertTrue(baos.toString().contains("SystemOutLoggerTest : MessageCodes :" + MessageCodes.ERROR_DATA_ISSUE + "[str1, str2]")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testErrorMessageCodesStringArray() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + System.setOut(ps); + systemOutLogger.setTransId("transactionId"); + systemOutLogger.error(MessageCodes.ERROR_DATA_ISSUE, "str1", "str2"); + assertTrue(baos.toString().contains("transactionId|SystemOutLoggerTest : MessageCode:" + MessageCodes.ERROR_DATA_ISSUE + "[str1, str2]")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testPostMDCInfoForEventString() { + assertEquals("transactionId", systemOutLogger.postMDCInfoForEvent("transactionId")); + } + + @Test + public void testWarnMessageCodesStringArray() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + System.setOut(ps); + systemOutLogger.setTransId("transactionId"); + systemOutLogger.warn(MessageCodes.ERROR_DATA_ISSUE, "str1", "str2"); + assertTrue(baos.toString().contains("transactionId|SystemOutLoggerTest : MessageCodes:" + MessageCodes.ERROR_DATA_ISSUE + "[str1, str2]")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testWarnMessageCodesThrowableStringArray() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + System.setOut(ps); + systemOutLogger.setTransId("transactionId"); + systemOutLogger.warn(MessageCodes.ERROR_DATA_ISSUE, new NullPointerException(), "str1", "str2"); + assertTrue(baos.toString().contains("transactionId|SystemOutLoggerTest : MessageCodes:" + MessageCodes.ERROR_DATA_ISSUE + "[str1, str2]")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testDebugObjectThrowable() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + System.setOut(ps); + systemOutLogger.setTransId("transactionId"); + systemOutLogger.debug(1, new NullPointerException()); + assertTrue(baos.toString(), baos.toString().contains("transactionId|SystemOutLoggerTest : 1:java.lang.NullPointerException")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testErrorObjectThrowable() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + System.setOut(ps); + systemOutLogger.setTransId("transactionId"); + systemOutLogger.error(1, new NullPointerException()); + assertTrue(baos.toString(), baos.toString().contains("transactionId|SystemOutLoggerTest : 1:java.lang.NullPointerException")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testInfoObjectThrowable() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + System.setOut(ps); + systemOutLogger.setTransId("transactionId"); + systemOutLogger.info(1, new NullPointerException()); + assertTrue(baos.toString(), baos.toString().contains("transactionId|SystemOutLoggerTest : 1:java.lang.NullPointerException")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testWarnObjectThrowable() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + System.setOut(ps); + systemOutLogger.setTransId("transactionId"); + systemOutLogger.warn(1, new NullPointerException()); + assertTrue(baos.toString(), baos.toString().contains("transactionId|SystemOutLoggerTest : 1:java.lang.NullPointerException")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testTraceObjectThrowable() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + System.setOut(ps); + systemOutLogger.setTransId("transactionId"); + systemOutLogger.trace(1, new NullPointerException()); + assertTrue(baos.toString(), baos.toString().contains("transactionId|SystemOutLoggerTest : 1:java.lang.NullPointerException")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testAuditObjectThrowable() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + System.setOut(ps); + systemOutLogger.setTransId("transactionId"); + systemOutLogger.audit(1, new NullPointerException()); + assertTrue(baos.toString(), baos.toString().contains("transactionId|SystemOutLoggerTest : 1:java.lang.NullPointerException")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testIsTraceEnabled() { + assertFalse(systemOutLogger.isTraceEnabled()); + } + + @Test + public void testPostMDCInfoForTriggeredRule() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + System.setOut(ps); + systemOutLogger.postMDCInfoForTriggeredRule("transactionId"); + assertTrue(baos.toString(), baos.toString().contains("transactionId")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + + @Test + public void testPostMDCInfoForEventObject() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + try{ + System.setOut(ps); + systemOutLogger.postMDCInfoForEvent(1); + assertTrue(baos.toString(), baos.toString().contains("1")); + } finally { + System.out.flush(); + System.setOut(old); + } + } + +} diff --git a/common-logging/src/test/java/org/onap/policy/common/logging/util/TestUtils.java b/common-logging/src/test/java/org/onap/policy/common/logging/util/TestUtils.java new file mode 100644 index 00000000..17eee02a --- /dev/null +++ b/common-logging/src/test/java/org/onap/policy/common/logging/util/TestUtils.java @@ -0,0 +1,57 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-Logging + * ================================================================================ + * Copyright (C) 2018 Ericsson. 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.onap.policy.common.logging.util; + +import static org.junit.Assert.fail; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + +public class TestUtils { + + public static void overrideField(@SuppressWarnings("rawtypes") final Class clazz, final Object object, final String fieldName, final Object newValue) { + try { + final Field field = clazz.getDeclaredField(fieldName); + field.setAccessible(true); + + final Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + + field.set(object, newValue); + } catch (final Exception e) { + fail(e.toString()); + } + } + + public static void overrideStaticField(@SuppressWarnings("rawtypes") final Class clazz, final String fieldName, final Object newValue) { + try { + final Field field = clazz.getDeclaredField(fieldName); + field.setAccessible(true); + + final Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); + + field.set(null, newValue); + } catch (final Exception e) { + fail(e.toString()); + } + } + +} -- cgit 1.2.3-korg