diff options
Diffstat (limited to 'common-logging')
21 files changed, 276 insertions, 495 deletions
diff --git a/common-logging/pom.xml b/common-logging/pom.xml index e979b69f..f6e7f97c 100644 --- a/common-logging/pom.xml +++ b/common-logging/pom.xml @@ -2,7 +2,8 @@ ============LICENSE_START======================================================= ONAP Policy Engine - Common Modules ================================================================================ - Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved. + Modifications Copyright (C) 2023-2024 Nordix Foundation. ================================================================================ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -24,7 +25,7 @@ <parent> <groupId>org.onap.policy.common</groupId> <artifactId>common-modules</artifactId> - <version>1.7.1-SNAPSHOT</version> + <version>3.0.0-SNAPSHOT</version> </parent> <artifactId>ONAP-Logging</artifactId> @@ -33,20 +34,6 @@ <dependencies> <dependency> - <groupId>org.projectlombok</groupId> - <artifactId>lombok</artifactId> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>javax.servlet</groupId> - <artifactId>javax.servlet-api</artifactId> - <scope>compile</scope> - </dependency> - <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-api</artifactId> - </dependency> - <dependency> <groupId>com.att.eelf</groupId> <artifactId>eelf-core</artifactId> <version>2.0.0-oss</version> @@ -57,23 +44,41 @@ </exclusion> </exclusions> </dependency> - <dependency> + <dependency> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + <scope>provided</scope> + </dependency> + <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <dependency> + <groupId>com.google.re2j</groupId> + <artifactId>re2j</artifactId> + </dependency> + <dependency> + <groupId>jakarta.servlet</groupId> + <artifactId>jakarta.servlet-api</artifactId> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <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>junit</groupId> - <artifactId>junit</artifactId> + <groupId>org.springframework</groupId> + <artifactId>spring-test</artifactId> <scope>test</scope> </dependency> </dependencies> @@ -81,42 +86,6 @@ <build> <plugins> <plugin> - <artifactId>maven-checkstyle-plugin</artifactId> - <executions> - <execution> - <id>onap-java-style</id> - <goals> - <goal>check</goal> - </goals> - <phase>process-sources</phase> - <configuration> - <!-- Use Google Java Style Guide: - https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml - with minor changes --> - <configLocation>onap-checkstyle/onap-java-style.xml</configLocation> - <!-- <sourceDirectory> is needed so that checkstyle ignores the generated sources directory --> - <sourceDirectories>${project.build.sourceDirectory}</sourceDirectories> - <includeResources>true</includeResources> - <includeTestSourceDirectory>true</includeTestSourceDirectory> - <includeTestResources>true</includeTestResources> - <excludes> - </excludes> - <consoleOutput>true</consoleOutput> - <failsOnViolation>true</failsOnViolation> - <violationSeverity>warning</violationSeverity> - </configuration> - </execution> - </executions> - <dependencies> - <dependency> - <groupId>org.onap.oparent</groupId> - <artifactId>checkstyle</artifactId> - <version>${oparent.version}</version> - <scope>compile</scope> - </dependency> - </dependencies> - </plugin> - <plugin> <groupId>com.att.eelf</groupId> <artifactId>eelf-maven-plugin</artifactId> <version>2.0.0-oss</version> diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/OnapLoggingContext.java b/common-logging/src/main/java/org/onap/policy/common/logging/OnapLoggingContext.java index 47c05041..e457f5f3 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/OnapLoggingContext.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/OnapLoggingContext.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 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. @@ -143,7 +143,7 @@ public class OnapLoggingContext { * with key "TransactionElapsedTime". */ public void transactionEnded() { - Instant transactionEndTime = Instant.now(); + var transactionEndTime = Instant.now(); setTransactionEndTimestamp(transactionEndTime); setTransactionElapsedTime(transactionEndTime); } @@ -163,7 +163,7 @@ public class OnapLoggingContext { * "MetricElapsedTime". */ public void metricEnded() { - Instant metricEndTime = Instant.now(); + var metricEndTime = Instant.now(); setMetricEndTimestamp(metricEndTime); setMetricElapsedTime(metricEndTime); } @@ -449,7 +449,7 @@ public class OnapLoggingContext { * @param transactionStartTime transaction start time */ public void setTransactionBeginTimestamp(Instant transactionStartTime) { - SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT); + var sdf = new SimpleDateFormat(TIME_FORMAT); context.put(TRANSACTION_BEGIN_TIME_STAMP, sdf.format(Date.from(transactionStartTime))); } @@ -468,7 +468,7 @@ public class OnapLoggingContext { * @param transactionEndTime transaction end time */ public void setTransactionEndTimestamp(Instant transactionEndTime) { - SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT); + var sdf = new SimpleDateFormat(TIME_FORMAT); context.put(TRANSACTION_END_TIME_STAMP, sdf.format(Date.from(transactionEndTime))); } @@ -510,7 +510,7 @@ public class OnapLoggingContext { * @param metricStartTime metric start time */ public void setMetricBeginTimestamp(Instant metricStartTime) { - SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT); + var sdf = new SimpleDateFormat(TIME_FORMAT); context.put(METRIC_BEGIN_TIME_STAMP, sdf.format(Date.from(metricStartTime))); } @@ -529,7 +529,7 @@ public class OnapLoggingContext { * @param metricEndTime metric end time */ public void setMetricEndTimestamp(Instant metricEndTime) { - SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT); + var sdf = new SimpleDateFormat(TIME_FORMAT); context.put(METRIC_END_TIME_STAMP, sdf.format(Date.from(metricEndTime))); } diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/OnapLoggingUtils.java b/common-logging/src/main/java/org/onap/policy/common/logging/OnapLoggingUtils.java index 369e77a4..91133d1a 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/OnapLoggingUtils.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/OnapLoggingUtils.java @@ -2,7 +2,8 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * 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. @@ -20,13 +21,16 @@ package org.onap.policy.common.logging; -import javax.servlet.http.HttpServletRequest; +import com.google.re2j.Pattern; +import jakarta.servlet.http.HttpServletRequest; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; -public class OnapLoggingUtils { +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class OnapLoggingUtils { - private OnapLoggingUtils() { - // Private constructor to prevent subclassing - } + private static final Pattern COMMA_PAT = Pattern.compile(","); + private static final Pattern CURLS_PAT = Pattern.compile("[{][}]"); /** * Get the ONAPLoggingContext for a request. @@ -36,8 +40,8 @@ public class OnapLoggingUtils { * @return the ONAPLoggingContext */ public static OnapLoggingContext getLoggingContextForRequest(HttpServletRequest request, - OnapLoggingContext baseContext) { - OnapLoggingContext requestContext = new OnapLoggingContext(baseContext); + OnapLoggingContext baseContext) { + var requestContext = new OnapLoggingContext(baseContext); if (request.getLocalAddr() != null) { // may be null in junit tests requestContext.setServerIpAddress(request.getLocalAddr()); } @@ -45,7 +49,7 @@ public class OnapLoggingUtils { // otherwise from remote address in the request String forwarded = request.getHeader("X-Forwarded-For"); if (forwarded != null && forwarded.trim().length() > 0) { - forwarded = forwarded.trim().split(",")[0]; + forwarded = COMMA_PAT.split(forwarded.trim())[0]; requestContext.setClientIpAddress(forwarded); } else if (request.getRemoteAddr() != null) { // may be null in junit tests requestContext.setClientIpAddress(request.getRemoteAddr()); @@ -65,15 +69,15 @@ public class OnapLoggingUtils { * if last argument is throwable/exception, pass it as argument to logger. * @param format message format can contains text and {} * @param arguments output arguments - * @return + * @return the formatted message as a String */ public static String formatMessage(String format, Object...arguments) { if (arguments.length <= 0 || arguments[0] == null) { return format; } int index; - StringBuilder builder = new StringBuilder(); - String[] token = format.split("[{][}]"); + var builder = new StringBuilder(); + String[] token = CURLS_PAT.split(format); for (index = 0; index < arguments.length; index++) { if (index < token.length) { builder.append(token[index]); diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/ErrorCodeMap.java b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/ErrorCodeMap.java index 32bb6fec..0eaa06b0 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/ErrorCodeMap.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/ErrorCodeMap.java @@ -2,7 +2,8 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * Copyright (C) 2020 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018, 2020-2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +22,9 @@ package org.onap.policy.common.logging.eelf; import java.util.EnumMap; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; /** * ErrorCodeMap contains a HashMap of ErrorCodeInfo (error code and error description). @@ -32,7 +36,8 @@ import java.util.EnumMap; * 500 – business process errors * 900 – unknown errors */ -public class ErrorCodeMap { +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class ErrorCodeMap { private static final EnumMap<MessageCodes, ErrorCodeInfo> hm = new EnumMap<>(MessageCodes.class); @@ -83,7 +88,6 @@ public class ErrorCodeMap { new ErrorCodeInfo(MISS_PROPERTY_ERROR, MISS_PROPERTY_ERROR_DESCRIPTION)); hm.put(MessageCodes.UPDATE_ERROR, new ErrorCodeInfo(UPDATE_ERROR, UPDATE_ERROR_DESCRIPTION)); hm.put(MessageCodes.ERROR_SYSTEM_ERROR, new ErrorCodeInfo(ERROR_SYSTEM_ERROR, ERROR_SYSTEM_ERROR_DESCRIPTION)); - hm.put(MessageCodes.ERROR_DATA_ISSUE, new ErrorCodeInfo(ERROR_DATA_ISSUE, ERROR_DATA_ISSUE_DESCRIPTION)); hm.put(MessageCodes.ERROR_PERMISSIONS, new ErrorCodeInfo(ERROR_PERMISSIONS, ERROR_PERMISSIONS_DESCRIPTION)); hm.put(MessageCodes.ERROR_DATA_ISSUE, new ErrorCodeInfo(ERROR_DATA_ISSUE, ERROR_DATA_ISSUE_DESCRIPTION)); hm.put(MessageCodes.ERROR_PROCESS_FLOW, new ErrorCodeInfo(ERROR_PROCESS_FLOW, ERROR_PROCESS_FLOW_DESCRIPTION)); @@ -93,32 +97,21 @@ public class ErrorCodeMap { hm.put(MessageCodes.ERROR_AUDIT, new ErrorCodeInfo(ERROR_AUDIT, ERROR_AUDIT_DESCRIPTION)); } - private ErrorCodeMap() { - // Private constructor to prevent subclassing - } - public static ErrorCodeInfo getErrorCodeInfo(MessageCodes messageCode) { return hm.get(messageCode); } + @Getter static class ErrorCodeInfo { - private String errorCode; - private String errorDesc; + private final String errorCode; + private final String errorDesc; public ErrorCodeInfo(String errorCode, String errorDesc) { this.errorCode = errorCode; this.errorDesc = errorDesc; } - public String getErrorCode() { - return errorCode; - } - - public String getErrorDesc() { - return errorDesc; - } - } } diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/EventData.java b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/EventData.java index 23be38ba..87a96a19 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/EventData.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/EventData.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019, 2021 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. @@ -21,7 +21,9 @@ package org.onap.policy.common.logging.eelf; import java.time.Instant; +import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; /** @@ -29,32 +31,14 @@ import lombok.Setter; */ @Getter @Setter +@NoArgsConstructor +@AllArgsConstructor public class EventData { private String requestId = null; private Instant startTime = null; private Instant endTime = null; - // Default constructor takes no arguments. - // Is empty because instance variables are assigned - // their default values upon declaration. - public EventData() { - // See above comments for the reason this constructor is empty - } - - /** - * Create an instance. - * - * @param requestId the request ID - * @param startTime the start time - * @param endTime the end time - */ - public EventData(String requestId, Instant startTime, Instant endTime) { - this.requestId = requestId; - this.startTime = startTime; - this.endTime = endTime; - } - @Override public String toString() { return requestId + " Starting Time : " + this.startTime + " Ending Time : " + this.endTime; @@ -62,8 +46,8 @@ public class EventData { @Override public int hashCode() { - final int prime = 31; - int result = 1; + final var prime = 31; + var result = 1; result = prime * result + ((requestId == null) ? 0 : requestId.hashCode()); return result; } diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/EventTrackInfo.java b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/EventTrackInfo.java index 71476c6a..f6f38d0d 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/EventTrackInfo.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/EventTrackInfo.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019, 2021 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. @@ -22,12 +22,14 @@ package org.onap.policy.common.logging.eelf; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import lombok.Getter; /** * EventTrackInfo contains a ConcurrentHashMap of EventData. */ public class EventTrackInfo { + @Getter private final ConcurrentMap<String, EventData> eventInfo; /** @@ -66,9 +68,7 @@ public class EventTrackInfo { return; } // in case override the start time, check the original event was already stored or not - if (!eventInfo.containsKey(id)) { - eventInfo.put(id, event); - } + eventInfo.putIfAbsent(id, event); } } @@ -82,11 +82,4 @@ public class EventTrackInfo { eventInfo.remove(eventId); } } - - /** - * Returns a ConcurrentHashMap of EventData. - */ - public ConcurrentMap<String, EventData> getEventInfo() { - return eventInfo; - } } diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/EventTrackInfoHandler.java b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/EventTrackInfoHandler.java index 1a87de53..f5203683 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/EventTrackInfoHandler.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/EventTrackInfoHandler.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 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. @@ -49,7 +49,7 @@ public class EventTrackInfoHandler extends TimerTask { */ private void cleanUp() { - EventTrackInfo eventTrackInfo = PolicyLogger.getEventTracker(); + var eventTrackInfo = PolicyLogger.getEventTracker(); if (eventTrackInfo == null) { return; } diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/MessageCodes.java b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/MessageCodes.java index 629ead82..0535b9d7 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/MessageCodes.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/MessageCodes.java @@ -3,13 +3,14 @@ * ONAP-Logging * ================================================================================ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2024 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. @@ -73,12 +74,6 @@ public enum MessageCodes implements EELFResolvableResourceEnum { RULE_METRICS_INFO, - UEB_AUDIT_EXEC_INFO, - - UEB_AUDIT_BEGIN_INFO, - - UEB_AUDIT_END_INFO, - UPDATE_ERROR, GENERAL_ERROR, diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/OnapConfigProperties.java b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/OnapConfigProperties.java index 47c027c2..cfb14643 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/OnapConfigProperties.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/OnapConfigProperties.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * Copyright (C) 2017-2018, 2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018, 2020-2021 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. @@ -20,8 +20,11 @@ package org.onap.policy.common.logging.eelf; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; -public class OnapConfigProperties { +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class OnapConfigProperties { /** * The Date-time of the start of a transaction. @@ -58,7 +61,7 @@ public class OnapConfigProperties { public static final String RESPONSE_CODE = "ResponseCode"; /** - * Human readable description of the application specific response code. + * Human-readable description of the application specific response code. */ public static final String RESPONSE_DESCRIPTION = "ResponseDescription"; @@ -81,8 +84,4 @@ public class OnapConfigProperties { public static final String SERVER_NAME = "ServerName"; public static final String INVOCATION_ID = "InvocationID"; - - private OnapConfigProperties() { - // do nothing - } } diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/PolicyLogger.java b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/PolicyLogger.java index 3f94483b..662ca764 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/PolicyLogger.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/PolicyLogger.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 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. @@ -61,6 +61,10 @@ import java.util.Timer; import java.util.UUID; import java.util.concurrent.ConcurrentMap; import java.util.function.Consumer; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; import org.apache.commons.lang3.StringUtils; import org.onap.policy.common.logging.OnapLoggingUtils; import org.onap.policy.common.logging.flexlogger.LoggerType; @@ -69,6 +73,7 @@ import org.slf4j.MDC; /** * PolicyLogger contains all the static methods for EELF logging. */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class PolicyLogger { private static EELFLogger errorLogger = EELFManager.getErrorLogger(); @@ -110,7 +115,9 @@ public class PolicyLogger { // size drops to this point, stop the Timer private static int stopCheckPoint = 2500; - private static boolean isOverrideLogbackLevel = false; + @Getter + @Setter + private static boolean overrideLogbackLevel = false; private static Level debugLevel = Level.INFO; private static Level auditLevel = Level.INFO; @@ -133,10 +140,6 @@ public class PolicyLogger { } } - private PolicyLogger() { - - } - public static synchronized Level getDebugLevel() { return debugLevel; } @@ -150,7 +153,7 @@ public class PolicyLogger { */ public static synchronized void setDebugLevel(String newDebugLevel) { - if (isOverrideLogbackLevel) { + if (overrideLogbackLevel) { PolicyLogger.debugLevel = Level.valueOf(newDebugLevel); debugLogger.setLevel(debugLevel); } @@ -170,7 +173,7 @@ public class PolicyLogger { */ public static synchronized void setAuditLevel(String newAuditLevel) { - if (isOverrideLogbackLevel) { + if (overrideLogbackLevel) { if ("OFF".equalsIgnoreCase(newAuditLevel)) { PolicyLogger.auditLevel = Level.OFF; auditLogger.setLevel(auditLevel); @@ -195,7 +198,7 @@ public class PolicyLogger { */ public static synchronized void setMetricsLevel(String newMetricsLevel) { - if (isOverrideLogbackLevel) { + if (overrideLogbackLevel) { if ("OFF".equalsIgnoreCase(newMetricsLevel)) { PolicyLogger.metricsLevel = Level.OFF; metricsLogger.setLevel(metricsLevel); @@ -221,7 +224,7 @@ public class PolicyLogger { */ public static synchronized void setErrorLevel(String newErrorLevel) { - if (isOverrideLogbackLevel) { + if (overrideLogbackLevel) { if ("OFF".equalsIgnoreCase(newErrorLevel)) { PolicyLogger.errorLevel = Level.OFF; errorLogger.setLevel(errorLevel); @@ -301,8 +304,8 @@ public class PolicyLogger { setMdcHostInfo(); - Instant startTime = Instant.now(); - Instant endTime = Instant.now(); + var startTime = Instant.now(); + var endTime = Instant.now(); seTimeStamps(startTime, endTime); @@ -334,8 +337,8 @@ public class PolicyLogger { MDC.put(MDC_INSTANCE_UUID, ""); MDC.put(MDC_ALERT_SEVERITY, ""); - Instant startTime = Instant.now(); - Instant endTime = Instant.now(); + var startTime = Instant.now(); + var endTime = Instant.now(); seTimeStamps(startTime, endTime); @@ -348,7 +351,7 @@ public class PolicyLogger { } private static void seTimeStamps(Instant startTime, Instant endTime) { - SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); + var sdf = new SimpleDateFormat(DATE_FORMAT); String formatedTime = sdf.format(Date.from(startTime)); MDC.put(BEGIN_TIME_STAMP, formatedTime); @@ -874,7 +877,7 @@ public class PolicyLogger { if (eventTracker == null) { eventTracker = new EventTrackInfo(); } - EventData event = new EventData(); + var event = new EventData(); event.setRequestId(eventId); event.setStartTime(Instant.now()); eventTracker.storeEventData(event); @@ -1007,7 +1010,7 @@ public class PolicyLogger { return; } - EventData event = eventTracker.getEventDataByRequestId(eventId); + var event = eventTracker.getEventDataByRequestId(eventId); if (event != null) { Instant endTime = event.getEndTime(); @@ -1035,7 +1038,7 @@ public class PolicyLogger { return; } - EventData event = eventTracker.getEventDataByRequestId(eventId.toString()); + var event = eventTracker.getEventDataByRequestId(eventId.toString()); if (event != null) { Instant endTime = event.getEndTime(); @@ -1226,13 +1229,13 @@ public class PolicyLogger { * @param arguments the messages */ private static String getNormalizedStackTrace(Throwable throwable, String... arguments) { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); + var sw = new StringWriter(); + var pw = new PrintWriter(sw); throwable.printStackTrace(pw); String newStValue = sw.toString().replace('|', '!').replace("\n", " - "); int curSize = arguments == null ? 0 : arguments.length; - StringBuilder newArgument = new StringBuilder(); - for (int i = 0; i < curSize; i++) { + var newArgument = new StringBuilder(); + for (var i = 0; i < curSize; i++) { newArgument.append(arguments[i]); newArgument.append(":"); } @@ -1246,7 +1249,7 @@ public class PolicyLogger { private static void startCleanUp() { if (!isEventTrackerRunning) { - EventTrackInfoHandler ttrcker = new EventTrackInfoHandler(); + var ttrcker = new EventTrackInfoHandler(); timer = new Timer(true); timer.scheduleAtFixedRate(ttrcker, timerDelayTime, checkInterval); debugLogger.info("EventTrackInfoHandler begins! : {}", new Date()); @@ -1279,7 +1282,7 @@ public class PolicyLogger { */ public static LoggerType init(Properties properties) { - Properties loggerProperties = getLoggerProperties(properties); + var loggerProperties = getLoggerProperties(properties); // fetch and verify definitions of some properties try { @@ -1321,7 +1324,7 @@ public class PolicyLogger { } private static int getIntProp(Properties properties, String propName, int defaultValue) { - final int propValue = Integer.parseInt(properties.getProperty(propName, String.valueOf(defaultValue))); + final var propValue = Integer.parseInt(properties.getProperty(propName, String.valueOf(defaultValue))); debugLogger.info("{} value: {}", propName, propValue); @@ -1373,10 +1376,10 @@ public class PolicyLogger { } private static void setOverrideLogbackLevels(Properties loggerProperties) { - final String overrideLogbackLevel = loggerProperties.getProperty("override.logback.level.setup"); + final var overrideLogbackLevelText = loggerProperties.getProperty("override.logback.level.setup"); - if (!StringUtils.isBlank(overrideLogbackLevel)) { - isOverrideLogbackLevel = "TRUE".equalsIgnoreCase(overrideLogbackLevel); + if (!StringUtils.isBlank(overrideLogbackLevelText)) { + overrideLogbackLevel = "TRUE".equalsIgnoreCase(overrideLogbackLevelText); } } @@ -1395,24 +1398,6 @@ public class PolicyLogger { } } - - /** - * Returns true for overriding logback levels; returns false for not. - */ - public static boolean isOverrideLogbackLevel() { - - return isOverrideLogbackLevel; - } - - /** - * Sets true for overriding logback levels; sets false for not. - */ - public static void setOverrideLogbackLevel(boolean odl) { - - isOverrideLogbackLevel = odl; - - } - /** * Sets server information to MDC. */ diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/DisplayUtils.java b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/DisplayUtils.java index 6cc5dc13..425c62a4 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/DisplayUtils.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/DisplayUtils.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 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. @@ -20,16 +20,17 @@ package org.onap.policy.common.logging.flexlogger; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + /** * Utilities to display messages. These are generally used while logging is being * configured, or when logging being directed to System.out. As a result, it directly * writes to System.out rather than to a logger. */ -public class DisplayUtils { - private DisplayUtils() { - // do nothing - } +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class DisplayUtils { /* * As the comment above says, these purposely write to System.out rather than a 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 837a584f..0e0ef2bf 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 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. @@ -23,6 +23,7 @@ package org.onap.policy.common.logging.flexlogger; import com.att.eelf.configuration.EELFLogger.Level; import java.io.Serializable; import java.util.UUID; +import lombok.Getter; import org.onap.policy.common.logging.eelf.MessageCodes; import org.onap.policy.common.logging.eelf.PolicyLogger; @@ -34,6 +35,7 @@ public class EelfLogger implements Logger, Serializable { private static final long serialVersionUID = 5385586713941277192L; private String className = ""; + @Getter private String transId = UUID.randomUUID().toString(); /** @@ -135,14 +137,6 @@ public class EelfLogger implements Logger, Serializable { } /** - * Returns transaction Id for logging. - */ - @Override - public String getTransId() { - return transId; - } - - /** * Records a message. * * @param message the message diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/FlexLogger.java b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/FlexLogger.java index eb6daee9..6c301712 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/FlexLogger.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/FlexLogger.java @@ -2,7 +2,8 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * 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. @@ -44,7 +45,7 @@ public class FlexLogger extends SecurityManager { // --- init logger first static { - loggerType = initlogger(); + loggerType = initLogger(); } /** @@ -53,66 +54,32 @@ public class FlexLogger extends SecurityManager { * @param clazz the class */ public static Logger getLogger(Class<?> clazz) { - Logger logger = null; - displayMessage(GET_LOGGER_PREFIX + loggerType); - switch (loggerType) { - - case EELF: - logger = getEelfLogger(clazz, false); - break; - case SYSTEMOUT: - default: - logger = getSystemOutLogger(); - break; - } - - return logger; - + return getLogger(clazz, false); } /** * Returns an instance of Logger. */ public static Logger getLogger() { - Logger logger = null; - displayMessage(GET_LOGGER_PREFIX + loggerType); - switch (loggerType) { - - case EELF: - logger = getEelfLogger(null, false); - break; - case SYSTEMOUT: - default: - logger = getSystemOutLogger(); - break; - } - - return logger; - + return getLogger(null); } /** * Returns an instance of Logger. * - * @param clazz the class + * @param clazz the class * @param isNewTransaction is a new transaction */ public static Logger getLogger(Class<?> clazz, boolean isNewTransaction) { - Logger logger = null; + Logger logger; displayMessage(GET_LOGGER_PREFIX + loggerType); - switch (loggerType) { - - case EELF: - logger = getEelfLogger(clazz, isNewTransaction); - break; - case SYSTEMOUT: - default: - logger = getSystemOutLogger(); - break; + if (loggerType == LoggerType.EELF) { + logger = getEelfLogger(clazz, isNewTransaction); + } else { + logger = getSystemOutLogger(); } return logger; - } /** @@ -121,20 +88,7 @@ public class FlexLogger extends SecurityManager { * @param isNewTransaction is a new transaction */ public static Logger getLogger(boolean isNewTransaction) { - Logger logger = null; - displayMessage(GET_LOGGER_PREFIX + loggerType); - switch (loggerType) { - - case EELF: - logger = getEelfLogger(null, isNewTransaction); - break; - case SYSTEMOUT: - default: - logger = getSystemOutLogger(); - break; - } - - return logger; + return getLogger(null, isNewTransaction); } /** @@ -148,7 +102,7 @@ public class FlexLogger extends SecurityManager { /** * Returns an instance of EelfLogger. * - * @param clazz the class + * @param clazz the class * @param isNewTransaction is a new transaction */ private static EelfLogger getEelfLogger(Class<?> clazz, boolean isNewTransaction) { @@ -179,37 +133,30 @@ public class FlexLogger extends SecurityManager { String className = new FlexLogger().getClassName(); - if (!systemOutMap.containsKey(className)) { - SystemOutLogger logger = new SystemOutLogger(className); - systemOutMap.put(className, logger); - } - - return systemOutMap.get(className); + return systemOutMap.computeIfAbsent(className, SystemOutLogger::new); } /** * loads the logger properties. */ - private static LoggerType initlogger() { - LoggerType loggerType = LoggerType.EELF; + private static LoggerType initLogger() { + var loggerType = LoggerType.EELF; Properties properties = null; try { properties = PropertyUtil.getProperties("config/policyLogger.properties"); displayMessage("FlexLogger:properties => " + properties); - if (properties != null) { - String overrideLogbackLevel = properties.getProperty("override.logback.level.setup"); - displayMessage("FlexLogger:overrideLogbackLevel => " + overrideLogbackLevel); - String loggerTypeString = properties.getProperty("logger.type"); - if ("EELF".equalsIgnoreCase(loggerTypeString) && "TRUE".equalsIgnoreCase(overrideLogbackLevel)) { - displayMessage("FlexLogger: start listener."); - properties = PropertyUtil.getProperties("config/policyLogger.properties", - new PropertiesCallBack("FlexLogger-CallBack")); - } + String overrideLogbackLevel = properties.getProperty("override.logback.level.setup"); + displayMessage("FlexLogger:overrideLogbackLevel => " + overrideLogbackLevel); + var loggerTypeString = properties.getProperty("logger.type"); + if ("EELF".equalsIgnoreCase(loggerTypeString) && "TRUE".equalsIgnoreCase(overrideLogbackLevel)) { + displayMessage("FlexLogger: start listener."); + properties = PropertyUtil.getProperties("config/policyLogger.properties", + new PropertiesCallBack("FlexLogger-CallBack")); } } catch (IOException e1) { - displayMessage("initlogger" + e1); + displayMessage("initLogger" + e1); } finally { // OK to pass no properties (null) loggerType = PolicyLogger.init(properties); @@ -239,10 +186,10 @@ public class FlexLogger extends SecurityManager { String auditLevel = properties.getProperty("audit.level"); String errorLevel = properties.getProperty("error.level"); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+00:00"); - Instant startTime = Instant.now(); - String formatedTime = sdf.format(Date.from(startTime)); - displayMessage("FlexLogger.propertiesChanged : called at time : " + formatedTime); + var sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+00:00"); + var startTime = Instant.now(); + String formattedTime = sdf.format(Date.from(startTime)); + displayMessage("FlexLogger.propertiesChanged : called at time : " + formattedTime); displayMessage("FlexLogger.propertiesChanged : debugLevel : " + debugLevel); if (changedKeys != null) { 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 b8241432..ef6c2e95 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 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. @@ -32,23 +32,23 @@ import java.util.Properties; import java.util.Set; import java.util.Timer; import java.util.TimerTask; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; /** * This class provides utilities to read properties from a properties file, and optionally get * notifications of future changes. */ -public class PropertyUtil { +public final class PropertyUtil { + + @NoArgsConstructor(access = AccessLevel.PRIVATE) protected static class LazyHolder { /** * Timer thread. Will not be allocated by the JVM until it is first referenced. * This may be overridden by junit tests. */ private static Timer timer = new Timer("PropertyUtil-Timer", true); - - private LazyHolder() { - super(); - } } // this table maps canonical file into a 'ListenerRegistration' instance @@ -64,9 +64,9 @@ public class PropertyUtil { */ public static Properties getProperties(File file) throws IOException { // create an InputStream (may throw a FileNotFoundException) - try (FileInputStream fis = new FileInputStream(file)) { + try (var fis = new FileInputStream(file)) { // create the properties instance - Properties rval = new Properties(); + var rval = new Properties(); // load properties (may throw an IOException) rval.load(fis); @@ -202,7 +202,7 @@ public class PropertyUtil { lastModified = timestamp; // Save old set, and initial set of changed properties. - Properties oldProperties = properties; + var oldProperties = properties; HashSet<String> changedProperties = new HashSet<>(oldProperties.stringPropertyNames()); // Fetch the list of listeners that we will potentially notify, @@ -238,7 +238,7 @@ public class PropertyUtil { for (final Listener notify : listeners) { // Copy 'properties' and 'changedProperties', so it doesn't // cause problems if the recipient makes changes. - final Properties tmpProperties = (Properties) (properties.clone()); + final var tmpProperties = (Properties) (properties.clone()); final HashSet<String> tmpChangedProperties = new HashSet<>(changedProperties); // Do the notification in a separate thread, so blocking @@ -276,7 +276,7 @@ public class PropertyUtil { // Convert the file to a canonical form in order to avoid the situation // where different names refer to the same file. - File tempFile = file.getCanonicalFile(); + var tempFile = file.getCanonicalFile(); // See if there is an existing registration. The 'synchronized' block // is needed to handle the case where a new listener is added at about 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 a059bd77..d16a1d7a 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 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. @@ -26,6 +26,7 @@ import com.att.eelf.configuration.EELFLogger.Level; import java.io.Serializable; import java.util.Arrays; import java.util.UUID; +import lombok.Getter; import org.onap.policy.common.logging.OnapLoggingUtils; import org.onap.policy.common.logging.eelf.MessageCodes; import org.onap.policy.common.logging.eelf.PolicyLogger; @@ -33,16 +34,17 @@ import org.onap.policy.common.logging.eelf.PolicyLogger; /** * SystemOutLogger implements all the methods of interface Logger by calling System.out.println */ +@Getter public class SystemOutLogger implements Logger, Serializable { private static final long serialVersionUID = 4956408061058933929L; private String className = ""; - private boolean isDebugEnabled = true; - private boolean isInfoEnabled = true; - private boolean isWarnEnabled = true; - private boolean isErrorEnabled = true; - private boolean isAuditEnabled = true; - private boolean isMetricsEnabled = true; + private boolean debugEnabled = true; + private boolean infoEnabled = true; + private boolean warnEnabled = true; + private boolean errorEnabled = true; + private boolean auditEnabled = true; + private boolean metricsEnabled = true; private String transId = UUID.randomUUID().toString(); /** @@ -77,35 +79,35 @@ public class SystemOutLogger implements Logger, Serializable { private void initLevel() { if (PolicyLogger.getDebugLevel() == Level.DEBUG) { - isDebugEnabled = true; - isInfoEnabled = true; - isWarnEnabled = true; + debugEnabled = true; + infoEnabled = true; + warnEnabled = true; } else { - isDebugEnabled = false; + debugEnabled = false; } if (PolicyLogger.getDebugLevel() == Level.INFO) { - isInfoEnabled = true; - isWarnEnabled = true; - isDebugEnabled = false; + infoEnabled = true; + warnEnabled = true; + debugEnabled = false; } if (PolicyLogger.getDebugLevel() == Level.OFF) { - isInfoEnabled = false; - isWarnEnabled = false; - isDebugEnabled = false; + infoEnabled = false; + warnEnabled = false; + debugEnabled = false; } if (PolicyLogger.getErrorLevel() == Level.OFF) { - isErrorEnabled = false; + errorEnabled = false; } if (PolicyLogger.getAuditLevel() == Level.OFF) { - isAuditEnabled = false; + auditEnabled = false; } if (PolicyLogger.getMetricsLevel() == Level.OFF) { - isMetricsEnabled = false; + metricsEnabled = false; } } @@ -120,15 +122,6 @@ public class SystemOutLogger implements Logger, Serializable { } /** - * Returns transaction Id. - */ - @Override - public String getTransId() { - - return transId; - } - - /** * Records a message. * * @param message the message @@ -345,68 +338,6 @@ public class SystemOutLogger implements Logger, Serializable { } /** - * Returns true for debug enabled, or false for not. - * - * @return boolean - */ - @Override - public boolean isDebugEnabled() { - return isDebugEnabled; - } - - /** - * Returns true for warn enabled, or false for not. - * - * @return boolean - */ - @Override - public boolean isWarnEnabled() { - return isWarnEnabled; - } - - /** - * Returns true for info enabled, or false for not. - * - * @return boolean - */ - @Override - public boolean isInfoEnabled() { - return isInfoEnabled; - } - - /** - * Returns true for error enabled, or false for not. - * - * @return boolean - */ - @Override - public boolean isErrorEnabled() { - return isErrorEnabled; - } - - /** - * Returns true for audit enabled, or false for not. - * - * @return boolean - */ - @Override - public boolean isAuditEnabled() { - - return isAuditEnabled; - } - - /** - * Returns true for metrics enabled, or false for not. - * - * @return boolean - */ - @Override - public boolean isMetricsEnabled() { - - return isMetricsEnabled; - } - - /** * Records an audit message. * * @param message the message diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/nsa/LoggingContextFactory.java b/common-logging/src/main/java/org/onap/policy/common/logging/nsa/LoggingContextFactory.java index ee64306f..eb2b318e 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/nsa/LoggingContextFactory.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/nsa/LoggingContextFactory.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019, 2021 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. @@ -20,6 +20,8 @@ package org.onap.policy.common.logging.nsa; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import org.onap.policy.common.logging.nsa.impl.SharedContext; import org.onap.policy.common.logging.nsa.impl.Slf4jLoggingContext; @@ -27,6 +29,7 @@ import org.onap.policy.common.logging.nsa.impl.Slf4jLoggingContext; * A factory for setting up a LoggingContext. * */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class LoggingContextFactory { public static class Builder { @@ -47,8 +50,4 @@ public class LoggingContextFactory { return forShared ? new SharedContext(baseContext) : new Slf4jLoggingContext(baseContext); } } - - private LoggingContextFactory() { - // do nothing - } } diff --git a/common-logging/src/main/resources/org/onap/policy/common/logging/eelf/Resources.properties b/common-logging/src/main/resources/org/onap/policy/common/logging/eelf/Resources.properties index 4f2c3f6d..0f9d0f8c 100644 --- a/common-logging/src/main/resources/org/onap/policy/common/logging/eelf/Resources.properties +++ b/common-logging/src/main/resources/org/onap/policy/common/logging/eelf/Resources.properties @@ -3,6 +3,7 @@ # ONAP-Logging # ================================================================================ # Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. +# Modifications Copyright (C) 2024 Nordix Foundation. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -111,24 +112,6 @@ RULE_METRICS_INFO=\ No resolution needed|\ Generate information for Metric events -UEB_AUDIT_EXEC_INFO=\ - POLICY-511I|\ - Service Name: {0}:Executing UEB: {1}|\ - No resolution needed|\ - Executing method - -UEB_AUDIT_BEGIN_INFO=\ - POLICY-512I|\ - Service Name: {0}:Entering UEB: {1}|\ - No resolution needed|\ - Entering method - -UEB_AUDIT_END_INFO=\ - POLICY-513I|\ - Service Name: {0}:Exiting UEB: {1}|\ - No resolution needed|\ - Exiting method - RULE_AUDIT_START_END_INFO=\ 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); |