diff options
12 files changed, 1576 insertions, 129 deletions
diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/EelfLogger.java b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/EelfLogger.java index ff097edf..07ef5626 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/EelfLogger.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/EelfLogger.java @@ -107,6 +107,7 @@ public class EelfLogger implements Logger, Serializable { className = clazz.getName(); } PolicyLogger.postMDCInfoForEvent(transId); + this.transId = transId; } /** @@ -119,6 +120,7 @@ public class EelfLogger implements Logger, Serializable { className = s; } PolicyLogger.postMDCInfoForEvent(transId); + this.transId = transId; } /** diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/FlexLoggerTester.java b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/FlexLoggerTester.java deleted file mode 100644 index 0fa65c0c..00000000 --- a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/FlexLoggerTester.java +++ /dev/null @@ -1,80 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP-Logging - * ================================================================================ - * 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.onap.policy.common.logging.flexlogger; - -import java.util.UUID; - -public class FlexLoggerTester { - - - public void testLogging(){ - - // get an instance of logger - Logger logger = FlexLogger.getLogger(FlexLoggerTester.class); - - logger.info("logger.isAuditEnabled():" + logger.isAuditEnabled()); - logger.info("logger.isDebugEnabled():" + logger.isDebugEnabled()); - logger.info("logger.isErrorEnabled():" + logger.isErrorEnabled()); - logger.info("logger.isInfoEnabled():" + logger.isInfoEnabled()); - logger.info("logger.isMetricsEnabled():" + logger.isMetricsEnabled()); - logger.info("logger.isWarnEnabled():" + logger.isWarnEnabled()); - - if(logger.isDebugEnabled()) - logger.debug("this is from logger.debug call"); - else - logger.info("this is from logger.info call"); - - if(logger.isMetricsEnabled()) - logger.metrics("this is from logger.metrics call"); - - logger.error("this is from logger.error call"); - if(logger.isAuditEnabled()) - logger.audit("this is from logger.audit call"); - else{ - logger.audit("shouldn't see this line in audit log"); - logger.info("shouldn't see this line in audit log"); - } - - if(logger.isMetricsEnabled()) - logger.metrics("this is from logger.metrics call"); - else{ - logger.metrics("shouldn't see this line in metrics log"); - logger.info("shouldn't see this line in metrics log"); - } - - if(logger.isErrorEnabled()) { - logger.error("this is from logger.error call"); - }else{ - logger.error("shouldn't see this logger.error call in error.log"); - logger.info("error is not enabled"); - } - - logger.info("logger.isDebugEnabled() returned value:" + logger.isDebugEnabled()); - logger.recordAuditEventEnd("123345456464998", "from recordAuditEventEnd call", "12345"); - logger.recordAuditEventEnd(UUID.randomUUID(), "from recordAuditEventEnd call", "abcdf"); - logger.recordAuditEventStart("from recordAuditEventStart call"); - logger.recordAuditEventStart(UUID.randomUUID().toString()); - logger.recordMetricEvent("123345456464998", "from recordMetricEvent call"); - logger.recordMetricEvent(UUID.randomUUID(), "from recordMetricEvent call"); - logger.trace("from trace call"); - - } -} diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/Logger4J.java b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/Logger4J.java index 30349d5c..555620f2 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/Logger4J.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/Logger4J.java @@ -24,6 +24,7 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; +import java.util.Arrays; import java.util.UUID; import org.apache.log4j.Logger; @@ -316,7 +317,7 @@ public class Logger4J implements org.onap.policy.common.logging.flexlogger.Logge */ @Override public void error(MessageCodes msg, Throwable arg0, String... arguments){ - log.error(transId + "|" + className +"|" + "MessageCodes :" + msg + arguments); + log.error(transId + "|" + className +"|" + "MessageCodes :" + msg + Arrays.asList(arguments)); } @@ -327,7 +328,7 @@ public class Logger4J implements org.onap.policy.common.logging.flexlogger.Logge */ @Override public void error(MessageCodes msg, String... arguments){ - log.error(transId + "|" + className +"|" + "MessageCode:" + msg + arguments); + log.error(transId + "|" + className +"|" + "MessageCode:" + msg + Arrays.asList(arguments)); } /** @@ -351,7 +352,7 @@ public class Logger4J implements org.onap.policy.common.logging.flexlogger.Logge */ @Override public void warn(MessageCodes msg, String... arguments){ - log.warn(className +"|" +"MessageCodes:" + msg + arguments); + log.warn(className +"|" +"MessageCodes:" + msg + Arrays.asList(arguments)); } /** @@ -362,7 +363,7 @@ public class Logger4J implements org.onap.policy.common.logging.flexlogger.Logge */ @Override public void warn(MessageCodes msg, Throwable arg0, String... arguments){ - log.warn(className +"|" +"MessageCodes:" + msg + arguments); + log.warn(className +"|" +"MessageCodes:" + msg + Arrays.asList(arguments)); } /** diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/PropertyUtil.java b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/PropertyUtil.java index 1bc57f01..86d119ea 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/PropertyUtil.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/PropertyUtil.java @@ -362,12 +362,13 @@ public class PropertyUtil * @param file the properties file * @param notify if not null, this is a callback interface that was used for * notifications of changes + * @throws IOException */ - public static void stopListening(File file, Listener listener) + public static void stopListening(File file, Listener listener) throws IOException { if (listener != null) { - ListenerRegistration reg = registrations.get(file); + ListenerRegistration reg = registrations.get(file.getCanonicalFile()); if (reg != null) { reg.removeListener(listener); @@ -380,29 +381,11 @@ public class PropertyUtil * @param fileName the properties file * @param notify if not null, this is a callback interface that was used for * notifications of changes + * @throws IOException */ - public static void stopListening(String fileName, Listener listener) + public static void stopListening(String fileName, Listener listener) throws IOException { stopListening(new File(fileName), listener); } - - /* ============================================================ */ - - // TEMPORARY - used to test callback interface - public static class Test implements Listener - { - String name; - - public Test(String name) - { - this.name = name; - } - - @Override - public void propertiesChanged(Properties properties, Set<String> changedKeys) - { - System.out.println("Test(" + name + ")\nproperties = " + properties - + "\nchangedKeys = " + changedKeys); - } - } + } diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/SystemOutLogger.java b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/SystemOutLogger.java index cb005c13..825799b9 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/SystemOutLogger.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/SystemOutLogger.java @@ -21,6 +21,7 @@ package org.onap.policy.common.logging.flexlogger; import java.io.Serializable; +import java.util.Arrays; import java.util.UUID; import org.onap.policy.common.logging.eelf.MessageCodes; @@ -324,7 +325,7 @@ public class SystemOutLogger implements Logger, Serializable { @Override public void recordMetricEvent(String eventId, String arg1) { - System.out.println(className+" : "+"eventId:" + ":" + eventId + "message:" + arg1); + System.out.println(className+" : "+"eventId:" + eventId + "message:" + arg1); } @@ -358,7 +359,7 @@ public class SystemOutLogger implements Logger, Serializable { @Override public void error(MessageCodes msg, Throwable arg0, String... arguments){ - System.out.println(className+" : "+"MessageCodes :" + msg + arguments); + System.out.println(className+" : "+"MessageCodes :" + msg + Arrays.asList(arguments)); } @@ -370,7 +371,7 @@ public class SystemOutLogger implements Logger, Serializable { @Override public void error(MessageCodes msg, String... arguments){ - System.out.println(transId + "|" + className+" : "+"MessageCode:" + msg + arguments); + System.out.println(transId + "|" + className+" : "+"MessageCode:" + msg + Arrays.asList(arguments)); } /** @@ -396,7 +397,7 @@ public class SystemOutLogger implements Logger, Serializable { @Override public void warn(MessageCodes msg, String... arguments){ - System.out.println(transId + "|" + className+" : "+"MessageCodes:" + msg + arguments); + System.out.println(transId + "|" + className+" : "+"MessageCodes:" + msg + Arrays.asList(arguments)); } /** @@ -408,7 +409,7 @@ public class SystemOutLogger implements Logger, Serializable { @Override public void warn(MessageCodes msg, Throwable arg0, String... arguments){ - System.out.println(transId + "|" + className+" : "+"MessageCodes:" + msg + arguments); + System.out.println(transId + "|" + className+" : "+"MessageCodes:" + msg + Arrays.asList(arguments)); } 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<String> 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<String> 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()); + } + } + +} |