diff options
Diffstat (limited to 'common-logging')
5 files changed, 97 insertions, 87 deletions
diff --git a/common-logging/pom.xml b/common-logging/pom.xml index f2b6706f..413991c1 100644 --- a/common-logging/pom.xml +++ b/common-logging/pom.xml @@ -3,6 +3,7 @@ ONAP Policy Engine - Common Modules ================================================================================ Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved. + Modifications Copyright (C) 2023 Nordix Foundation. ================================================================================ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -71,8 +72,13 @@ <scope>test</scope> </dependency> <dependency> - <groupId>org.powermock</groupId> - <artifactId>powermock-api-mockito2</artifactId> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-test</artifactId> <scope>test</scope> </dependency> <dependency> 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 1c412ddc..62fcfaf4 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 @@ -4,6 +4,7 @@ * ================================================================================ * Copyright (C) 2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -51,8 +52,8 @@ import java.util.UUID; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; -import org.powermock.reflect.Whitebox; import org.slf4j.MDC; +import org.springframework.test.util.ReflectionTestUtils; public class PolicyLoggerTest { @@ -189,7 +190,7 @@ public class PolicyLoggerTest { @Test public void testInfoMessageCodesStringStringArray() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.info(MessageCodes.ERROR_DATA_ISSUE, "str1", "str2"); Mockito.verify(mockLogger).info(MessageCodes.ERROR_DATA_ISSUE, "str2"); } @@ -197,7 +198,7 @@ public class PolicyLoggerTest { @Test public void testInfoStringString() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.info("str1", "str2"); Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); Mockito.when(mockLogger.isInfoEnabled()).thenReturn(true); @@ -208,7 +209,7 @@ public class PolicyLoggerTest { @Test public void testInfoObject() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.info("str1"); Mockito.verify(mockLogger).info(MessageCodes.GENERAL_INFO, "str1"); } @@ -216,7 +217,7 @@ public class PolicyLoggerTest { @Test public void testInfoMessageCodesThrowableStringArray() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.info(MessageCodes.ERROR_DATA_ISSUE, new NullPointerException(), "str1", "str2"); Mockito.verify(mockLogger).info((MessageCodes) Mockito.any(), Mockito.startsWith("str1:str2:java.lang.NullPointerException")); @@ -225,7 +226,7 @@ public class PolicyLoggerTest { @Test public void testInfoMessageCodesStringThrowableStringArray() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.info(MessageCodes.ERROR_DATA_ISSUE, "PolicyLoggerTest", new NullPointerException(), "str1", "str2"); Mockito.verify(mockLogger).info((MessageCodes) Mockito.any(), @@ -235,7 +236,7 @@ public class PolicyLoggerTest { @Test public void testWarnMessageCodesStringStringArray() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.warn(MessageCodes.ERROR_DATA_ISSUE, "str1"); Mockito.verify(mockLogger).warn(MessageCodes.ERROR_DATA_ISSUE); } @@ -243,7 +244,7 @@ public class PolicyLoggerTest { @Test public void testWarnStringString() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.warn("str1", "str2"); Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); Mockito.when(mockLogger.isWarnEnabled()).thenReturn(true); @@ -254,7 +255,7 @@ public class PolicyLoggerTest { @Test public void testWarnObject() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.warn(1); Mockito.verify(mockLogger).warn(MessageCodes.GENERAL_WARNING, "1"); } @@ -262,7 +263,7 @@ public class PolicyLoggerTest { @Test public void testWarnMessageCodesThrowableStringArray() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.warn(MessageCodes.ERROR_DATA_ISSUE, new NullPointerException(), "str1", "str2"); Mockito.verify(mockLogger).warn((MessageCodes) Mockito.any(), Mockito.startsWith("str1:str2:java.lang.NullPointerException")); @@ -271,7 +272,7 @@ public class PolicyLoggerTest { @Test public void testWarnMessageCodesStringThrowableStringArray() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.warn(MessageCodes.ERROR_DATA_ISSUE, "PolicyLoggerTest", new NullPointerException(), "str1", "str2"); Mockito.verify(mockLogger).warn((MessageCodes) Mockito.any(), @@ -281,7 +282,7 @@ public class PolicyLoggerTest { @Test public void testWarnString() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.warn("str1"); Mockito.verify(mockLogger).warn(MessageCodes.GENERAL_WARNING, "str1"); } @@ -289,7 +290,7 @@ public class PolicyLoggerTest { @Test public void testErrorStringString() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "errorLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "errorLogger", mockLogger); PolicyLogger.error("str1", "str2"); Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); Mockito.when(mockLogger.isErrorEnabled()).thenReturn(true); @@ -303,7 +304,7 @@ public class PolicyLoggerTest { @Test public void testErrorString() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "errorLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "errorLogger", mockLogger); PolicyLogger.error("str1"); Mockito.verify(mockLogger).error(MessageCodes.GENERAL_ERROR, "str1"); assertEquals("ERROR", MDC.get("ErrorCategory")); @@ -315,7 +316,7 @@ public class PolicyLoggerTest { @Test public void testErrorObject() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "errorLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "errorLogger", mockLogger); PolicyLogger.error(1); Mockito.verify(mockLogger).error(MessageCodes.GENERAL_ERROR, "1"); assertEquals("ERROR", MDC.get("ErrorCategory")); @@ -327,7 +328,7 @@ public class PolicyLoggerTest { @Test public void testErrorMessageCodesThrowableStringArray() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "errorLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "errorLogger", mockLogger); PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, new NullPointerException(), "str1", "str2"); Mockito.verify(mockLogger).error((MessageCodes) Mockito.any(), Mockito.startsWith("str1:str2:java.lang.NullPointerException")); @@ -336,7 +337,7 @@ public class PolicyLoggerTest { @Test public void testErrorMessageCodesStringThrowableStringArray() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "errorLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "errorLogger", mockLogger); PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "PolicyLoggerTest", new NullPointerException(), "str1", "str2"); Mockito.verify(mockLogger).error((MessageCodes) Mockito.any(), @@ -346,7 +347,7 @@ public class PolicyLoggerTest { @Test public void testErrorMessageCodesStringArray() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "errorLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "errorLogger", mockLogger); PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "str1", "str2"); Mockito.verify(mockLogger).error(MessageCodes.ERROR_DATA_ISSUE, "str1", "str2"); } @@ -354,7 +355,7 @@ public class PolicyLoggerTest { @Test public void testDebugMessageCodesStringArray() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.debug(MessageCodes.ERROR_DATA_ISSUE, "str1", "str2"); Mockito.verify(mockLogger).debug(MessageCodes.ERROR_DATA_ISSUE, "str1", "str2"); } @@ -362,7 +363,7 @@ public class PolicyLoggerTest { @Test public void testDebugStringString() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.debug("str1", "str2"); Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); Mockito.when(mockLogger.isDebugEnabled()).thenReturn(true); @@ -373,7 +374,7 @@ public class PolicyLoggerTest { @Test public void testDebugString() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.debug("str1"); Mockito.verify(mockLogger).debug("str1"); } @@ -381,7 +382,7 @@ public class PolicyLoggerTest { @Test public void testDebugObject() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.debug(1); Mockito.verify(mockLogger).debug("{}", 1); } @@ -389,7 +390,7 @@ public class PolicyLoggerTest { @Test public void testAuditStringObject() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "auditLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "auditLogger", mockLogger); PolicyLogger.audit("PolicyLoggerTest", 1); Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); Mockito.when(mockLogger.isInfoEnabled()).thenReturn(true); @@ -402,7 +403,7 @@ public class PolicyLoggerTest { @Test public void testAuditObject() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "auditLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "auditLogger", mockLogger); PolicyLogger.audit(1); assertEquals("", MDC.get("ClassName")); assertEquals("COMPLETE", MDC.get("StatusCode")); @@ -412,7 +413,7 @@ public class PolicyLoggerTest { @Test public void testDebugMessageCodesThrowableStringArray() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.debug(MessageCodes.ERROR_DATA_ISSUE, new NullPointerException(), "str1", "str2"); Mockito.verify(mockLogger).debug((MessageCodes) Mockito.any(), Mockito.startsWith("str1:str2:java.lang.NullPointerException")); @@ -421,7 +422,7 @@ public class PolicyLoggerTest { @Test public void testDebugMessageCodesStringThrowableStringArray() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.debug(MessageCodes.ERROR_DATA_ISSUE, "PolicyLoggerTest", new NullPointerException(), "str1", "str2"); Mockito.verify(mockLogger).debug((MessageCodes) Mockito.any(), @@ -431,7 +432,7 @@ public class PolicyLoggerTest { @Test public void testIsDebugEnabled() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); Mockito.when(mockLogger.isDebugEnabled()).thenReturn(false).thenReturn(true); assertFalse(PolicyLogger.isDebugEnabled()); assertTrue(PolicyLogger.isDebugEnabled()); @@ -440,7 +441,7 @@ public class PolicyLoggerTest { @Test public void testIsErrorEnabled() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "errorLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "errorLogger", mockLogger); Mockito.when(mockLogger.isErrorEnabled()).thenReturn(false).thenReturn(true); assertFalse(PolicyLogger.isErrorEnabled()); assertTrue(PolicyLogger.isErrorEnabled()); @@ -449,7 +450,7 @@ public class PolicyLoggerTest { @Test public void testIsWarnEnabled() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); Mockito.when(mockLogger.isWarnEnabled()).thenReturn(false).thenReturn(true); assertFalse(PolicyLogger.isWarnEnabled()); assertTrue(PolicyLogger.isWarnEnabled()); @@ -458,7 +459,7 @@ public class PolicyLoggerTest { @Test public void testIsInfoEnabled1() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); Mockito.when(mockLogger.isInfoEnabled()).thenReturn(false).thenReturn(true); assertFalse(PolicyLogger.isInfoEnabled1()); assertTrue(PolicyLogger.isInfoEnabled1()); @@ -467,7 +468,7 @@ public class PolicyLoggerTest { @Test public void testIsAuditEnabled() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); Mockito.when(mockLogger.isInfoEnabled()).thenReturn(false).thenReturn(true); assertFalse(PolicyLogger.isAuditEnabled()); assertTrue(PolicyLogger.isAuditEnabled()); @@ -476,7 +477,7 @@ public class PolicyLoggerTest { @Test public void testIsInfoEnabled() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); Mockito.when(mockLogger.isInfoEnabled()).thenReturn(false).thenReturn(true); assertFalse(PolicyLogger.isInfoEnabled()); assertTrue(PolicyLogger.isInfoEnabled()); @@ -485,7 +486,7 @@ public class PolicyLoggerTest { @Test public void testTraceStringString() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.trace("str1", "str2"); Mockito.verify(mockLogger).trace(MessageCodes.GENERAL_INFO, "str2"); } @@ -493,7 +494,7 @@ public class PolicyLoggerTest { @Test public void testTraceObject() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.trace(1); Mockito.verify(mockLogger).trace("{}", 1); } @@ -585,7 +586,7 @@ public class PolicyLoggerTest { @Test public void testRecordMetricEventString() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "metricsLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "metricsLogger", mockLogger); PolicyLogger.recordMetricEvent("eventId"); Mockito.verify(mockLogger).info(Mockito.eq(MessageCodes.RULE_METRICS_INFO), Mockito.anyString(), Mockito.eq("eventId")); @@ -594,7 +595,7 @@ public class PolicyLoggerTest { @Test public void testMetricsString() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "metricsLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "metricsLogger", mockLogger); PolicyLogger.metrics("str1"); Mockito.verify(mockLogger).info(Mockito.eq(MessageCodes.RULE_METRICS_INFO), Mockito.anyString(), Mockito.eq("str1")); @@ -603,7 +604,7 @@ public class PolicyLoggerTest { @Test public void testMetricsStringObject() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "metricsLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "metricsLogger", mockLogger); PolicyLogger.metrics("PolicyLoggerTest", 1); Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); Mockito.when(mockLogger.isInfoEnabled()).thenReturn(true); @@ -615,7 +616,7 @@ public class PolicyLoggerTest { @Test public void testMetricsObject() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "metricsLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "metricsLogger", mockLogger); PolicyLogger.metrics(1); Mockito.verify(mockLogger).info(Mockito.eq(MessageCodes.RULE_METRICS_INFO), Mockito.anyString(), Mockito.eq("1")); @@ -624,7 +625,7 @@ public class PolicyLoggerTest { @Test public void testMetricsPrintln() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "metricsLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "metricsLogger", mockLogger); PolicyLogger.metricsPrintln("str1"); Mockito.verify(mockLogger).info("str1"); } 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 index 8085aa3a..1d77451b 100644 --- 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 @@ -4,6 +4,7 @@ * ================================================================================ * Copyright (C) 2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,8 +36,8 @@ 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.powermock.reflect.Whitebox; import org.slf4j.MDC; +import org.springframework.test.util.ReflectionTestUtils; public class EelfLoggerTest { @@ -92,7 +93,7 @@ public class EelfLoggerTest { @Test public void testDebugObject() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); eelfLogger.debug("message"); Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); Mockito.when(mockLogger.isDebugEnabled()).thenReturn(true); @@ -103,7 +104,7 @@ public class EelfLoggerTest { @Test public void testErrorObject() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "errorLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "errorLogger", mockLogger); eelfLogger.error("message"); Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); Mockito.when(mockLogger.isErrorEnabled()).thenReturn(true); @@ -114,7 +115,7 @@ public class EelfLoggerTest { @Test public void testInfoObject() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); eelfLogger.info("message"); Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); Mockito.when(mockLogger.isInfoEnabled()).thenReturn(true); @@ -125,7 +126,7 @@ public class EelfLoggerTest { @Test public void testWarnObject() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); eelfLogger.warn("message"); Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); Mockito.when(mockLogger.isWarnEnabled()).thenReturn(true); @@ -136,7 +137,7 @@ public class EelfLoggerTest { @Test public void testTraceObject() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); eelfLogger.trace("message"); Mockito.verify(mockLogger).trace(MessageCodes.GENERAL_INFO, "message"); } @@ -144,7 +145,7 @@ public class EelfLoggerTest { @Test public void testIsDebugEnabled() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); Mockito.when(mockLogger.isDebugEnabled()).thenReturn(false).thenReturn(true); assertFalse(eelfLogger.isDebugEnabled()); assertTrue(eelfLogger.isDebugEnabled()); @@ -154,7 +155,7 @@ public class EelfLoggerTest { @Test public void testIsInfoEnabled() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); Mockito.when(mockLogger.isInfoEnabled()).thenReturn(false).thenReturn(true); assertFalse(eelfLogger.isInfoEnabled()); assertTrue(eelfLogger.isInfoEnabled()); @@ -163,7 +164,7 @@ public class EelfLoggerTest { @Test public void testIsWarnEnabled() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); Mockito.when(mockLogger.isWarnEnabled()).thenReturn(false).thenReturn(true); assertFalse(eelfLogger.isWarnEnabled()); assertTrue(eelfLogger.isWarnEnabled()); @@ -172,7 +173,7 @@ public class EelfLoggerTest { @Test public void testIsErrorEnabled() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "errorLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "errorLogger", mockLogger); Mockito.when(mockLogger.isErrorEnabled()).thenReturn(false).thenReturn(true); assertFalse(eelfLogger.isErrorEnabled()); assertTrue(eelfLogger.isErrorEnabled()); @@ -195,7 +196,7 @@ public class EelfLoggerTest { @Test public void testIsTraceEnabled() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); Mockito.when(mockLogger.isDebugEnabled()).thenReturn(false).thenReturn(true); assertFalse(eelfLogger.isTraceEnabled()); assertTrue(eelfLogger.isTraceEnabled()); @@ -211,7 +212,7 @@ public class EelfLoggerTest { @Test public void testDebugObjectThrowable() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); eelfLogger.debug("message", new NullPointerException()); Mockito.verify(mockLogger).debug((MessageCodes) Mockito.any(), Mockito.startsWith("message:java.lang.NullPointerException")); @@ -220,7 +221,7 @@ public class EelfLoggerTest { @Test public void testErrorObjectThrowable() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "errorLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "errorLogger", mockLogger); eelfLogger.error("message", new NullPointerException()); Mockito.verify(mockLogger).error((MessageCodes) Mockito.any(), Mockito.startsWith("message:java.lang.NullPointerException")); @@ -230,7 +231,7 @@ public class EelfLoggerTest { @Test public void testInfoObjectThrowable() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); eelfLogger.info("message", new NullPointerException()); Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); Mockito.when(mockLogger.isInfoEnabled()).thenReturn(true); @@ -241,7 +242,7 @@ public class EelfLoggerTest { @Test public void testWarnObjectThrowable() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); eelfLogger.warn("message", new NullPointerException()); Mockito.verify(mockLogger).warn((MessageCodes) Mockito.any(), Mockito.startsWith("message:java.lang.NullPointerException")); @@ -250,7 +251,7 @@ public class EelfLoggerTest { @Test public void testTraceObjectThrowable() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); eelfLogger.trace("message", new NullPointerException()); Mockito.verify(mockLogger).trace("{}", "message"); } @@ -258,7 +259,7 @@ public class EelfLoggerTest { @Test public void testAuditObjectThrowable() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "auditLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "auditLogger", mockLogger); eelfLogger.audit("message", new NullPointerException()); Mockito.verify(mockLogger).info("{}", "message"); } @@ -330,7 +331,7 @@ public class EelfLoggerTest { @Test public void testMetrics() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "metricsLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "metricsLogger", mockLogger); eelfLogger.metrics(1); Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); Mockito.when(mockLogger.isInfoEnabled()).thenReturn(true); @@ -342,7 +343,7 @@ public class EelfLoggerTest { @Test public void testErrorMessageCodesThrowableStringArray() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "errorLogger", mockLogger); + ReflectionTestUtils.setField(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")); @@ -351,7 +352,7 @@ public class EelfLoggerTest { @Test public void testErrorMessageCodesStringArray() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "errorLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "errorLogger", mockLogger); eelfLogger.error(MessageCodes.GENERAL_ERROR, "str1", "str2"); Mockito.verify(mockLogger).error(MessageCodes.GENERAL_ERROR, "str1", "str2"); @@ -366,7 +367,7 @@ public class EelfLoggerTest { @Test public void testWarnMessageCodesStringArray() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(PolicyLogger.class, "debugLogger", mockLogger); eelfLogger.warn(MessageCodes.GENERAL_ERROR, "str1", "str2"); Mockito.verify(mockLogger).warn(MessageCodes.GENERAL_ERROR, "str1", "str2"); } @@ -374,7 +375,7 @@ public class EelfLoggerTest { @Test public void testWarnMessageCodesThrowableStringArray() { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); - Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); + ReflectionTestUtils.setField(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")); 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 index f5fcefe6..c0194f2b 100644 --- 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 @@ -4,6 +4,7 @@ * ================================================================================ * Copyright (C) 2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,13 +31,13 @@ import java.util.HashSet; import java.util.Set; import org.junit.Test; import org.onap.policy.common.logging.flexlogger.FlexLogger.PropertiesCallBack; -import org.powermock.reflect.Whitebox; +import org.springframework.test.util.ReflectionTestUtils; public class FlexLoggerTest { @Test public void testGetLoggerClassOfQEelf() { - Whitebox.setInternalState(FlexLogger.class, "loggerType", LoggerType.EELF); + ReflectionTestUtils.setField(FlexLogger.class, "loggerType", LoggerType.EELF); Logger logger = FlexLogger.getLogger((Class<?>) null); assertSame(logger, FlexLogger.getLogger((Class<?>) null)); assertNotEquals(logger, FlexLogger.getLogger(String.class)); @@ -44,49 +45,49 @@ public class FlexLoggerTest { @Test public void testGetLoggerClassOfQSystemOut() { - Whitebox.setInternalState(FlexLogger.class, "loggerType", LoggerType.SYSTEMOUT); + ReflectionTestUtils.setField(FlexLogger.class, "loggerType", LoggerType.SYSTEMOUT); Logger logger = FlexLogger.getLogger(this.getClass()); assertSame(logger, FlexLogger.getLogger(this.getClass())); } @Test public void testGetLoggerStringEelf() { - Whitebox.setInternalState(FlexLogger.class, "loggerType", LoggerType.EELF); + ReflectionTestUtils.setField(FlexLogger.class, "loggerType", LoggerType.EELF); Logger logger = FlexLogger.getLogger(); assertSame(logger, FlexLogger.getLogger()); } @Test public void testGetLoggerStringSystemOut() { - Whitebox.setInternalState(FlexLogger.class, "loggerType", LoggerType.SYSTEMOUT); + ReflectionTestUtils.setField(FlexLogger.class, "loggerType", LoggerType.SYSTEMOUT); Logger logger = FlexLogger.getLogger(); assertSame(logger, FlexLogger.getLogger()); } @Test public void testGetLoggerClassOfQBooleanEelf() { - Whitebox.setInternalState(FlexLogger.class, "loggerType", LoggerType.EELF); + ReflectionTestUtils.setField(FlexLogger.class, "loggerType", LoggerType.EELF); Logger logger = FlexLogger.getLogger(this.getClass(), true); assertSame(logger, FlexLogger.getLogger(this.getClass(), true)); } @Test public void testGetLoggerClassOfQBooleanSystemOut() { - Whitebox.setInternalState(FlexLogger.class, "loggerType", LoggerType.SYSTEMOUT); + ReflectionTestUtils.setField(FlexLogger.class, "loggerType", LoggerType.SYSTEMOUT); Logger logger = FlexLogger.getLogger(this.getClass(), true); assertSame(logger, FlexLogger.getLogger(this.getClass(), true)); } @Test public void testGetLoggerStringBooleanEelf() { - Whitebox.setInternalState(FlexLogger.class, "loggerType", LoggerType.EELF); + ReflectionTestUtils.setField(FlexLogger.class, "loggerType", LoggerType.EELF); Logger logger = FlexLogger.getLogger(true); assertSame(logger, FlexLogger.getLogger(true)); } @Test public void testGetLoggerStringBooleanSystemOut() { - Whitebox.setInternalState(FlexLogger.class, "loggerType", LoggerType.SYSTEMOUT); + ReflectionTestUtils.setField(FlexLogger.class, "loggerType", LoggerType.SYSTEMOUT); Logger logger = FlexLogger.getLogger(true); assertSame(logger, FlexLogger.getLogger(true)); } 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 index bbf77179..13f525c9 100644 --- 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 @@ -3,13 +3,14 @@ * ONAP-Logging * ================================================================================ * Copyright (C) 2018-2020 Ericsson, AT&T. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * 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. @@ -43,28 +44,28 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.onap.policy.common.logging.flexlogger.PropertyUtil.Listener; -import org.powermock.reflect.Whitebox; +import org.springframework.test.util.ReflectionTestUtils; public class PropertyUtilTest { private static final String TIMER_FIELD = "timer"; private static final File FILE = new File("target/test.properties"); private static Timer saveTimer; - + private TimerTask task; private Timer timer; private TestListener testListener; - + @BeforeClass public static void setUpBeforeClass() { - saveTimer = Whitebox.getInternalState(PropertyUtil.LazyHolder.class, TIMER_FIELD); - + saveTimer = (Timer) ReflectionTestUtils.getField(PropertyUtil.LazyHolder.class, TIMER_FIELD); + } - + @AfterClass public static void tearDownAfterClass() { - Whitebox.setInternalState(PropertyUtil.LazyHolder.class, TIMER_FIELD, saveTimer); - + ReflectionTestUtils.setField(PropertyUtil.LazyHolder.class, TIMER_FIELD, saveTimer); + } /** @@ -74,15 +75,15 @@ public class PropertyUtilTest { public void setUp() throws IOException { task = null; timer = mock(Timer.class); - Whitebox.setInternalState(PropertyUtil.LazyHolder.class, TIMER_FIELD, timer); - + ReflectionTestUtils.setField(PropertyUtil.LazyHolder.class, TIMER_FIELD, timer); + doAnswer(args -> { task = args.getArgument(0, TimerTask.class); return null; }).when(timer).schedule(any(TimerTask.class), anyLong(), anyLong()); - + testListener = new TestListener(); - + FileOutputStream fileOutputStream = new FileOutputStream(FILE); Properties properties = new Properties(); properties.put("testProperty", "testValue"); @@ -95,7 +96,7 @@ public class PropertyUtilTest { PropertyUtil.stopListening(FILE, testListener); FILE.delete(); } - + @Test public void testTimer() { assertNotNull(saveTimer); |