1 package org.openecomp.sdc.common.log.wrappers;
3 import org.junit.Before;
5 import org.junit.runner.RunWith;
6 import org.mockito.ArgumentCaptor;
7 import org.mockito.Captor;
8 import org.mockito.InjectMocks;
9 import org.mockito.Mock;
10 import org.mockito.junit.MockitoJUnitRunner;
11 import org.openecomp.sdc.common.log.api.ILogConfiguration;
12 import org.openecomp.sdc.common.log.elements.LoggerError;
13 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
14 import org.openecomp.sdc.common.log.enums.LogLevel;
15 import org.openecomp.sdc.common.log.enums.Severity;
16 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
18 import org.slf4j.Marker;
20 import javax.ws.rs.container.ContainerRequestContext;
21 import javax.ws.rs.core.Response;
22 import javax.ws.rs.core.UriInfo;
24 import java.net.URISyntaxException;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNull;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.ArgumentMatchers.anyString;
30 import static org.mockito.ArgumentMatchers.contains;
31 import static org.mockito.ArgumentMatchers.eq;
32 import static org.mockito.Mockito.*;
33 import static org.openecomp.sdc.common.log.api.ILogConfiguration.*;
35 @RunWith(MockitoJUnitRunner.class)
36 public class LoggerTest {
38 private final static String targetEntity = "DCEA";
39 private final static String serviceName = "testService";
40 private final static String message = "Logger message";
41 private final static String exceptionMsg= "Exception testing";
42 private final static String missingFieldsMessageFragment = "mandatory parameters for ECOMP logging";
45 private org.slf4j.Logger logger;
47 private ContainerRequestContext requestContext;
49 private UriInfo uriInfo;
51 private Response.StatusType statusType;
54 private Logger commonLogger;
57 private ArgumentCaptor<String> captor;
65 public void validateErrorLogWhenErrorSettingsProvided() {
66 when(logger.isErrorEnabled()).thenReturn(true);
67 commonLogger.error(EcompLoggerErrorCode.PERMISSION_ERROR, serviceName, targetEntity, message);
69 verify(logger).error(any(Marker.class), captor.capture(), any(Object[].class));
70 assertEquals(message, captor.getValue());
71 assertEquals(String.valueOf(EcompLoggerErrorCode.PERMISSION_ERROR.getErrorCode()), MDC.get(ILogConfiguration.MDC_ERROR_CODE));
72 assertEquals(LogLevel.ERROR.name(), MDC.get(ILogConfiguration.MDC_ERROR_CATEGORY));
73 assertEquals(targetEntity, MDC.get(ILogConfiguration.MDC_TARGET_ENTITY));
74 assertEquals(serviceName, MDC.get(ILogConfiguration.MDC_SERVICE_NAME));
78 public void validateWarnMessageIsLoggedWhenAllErrorSettingsProvided() {
79 when(logger.isErrorEnabled()).thenReturn(true);
80 commonLogger.error(EcompLoggerErrorCode.AVAILABILITY_TIMEOUTS_ERROR, serviceName, targetEntity, message);
82 verify(logger).error(any(Marker.class), captor.capture(), any(Object[].class));
83 assertEquals(message, captor.getValue());
84 assertEquals(String.valueOf(EcompLoggerErrorCode.AVAILABILITY_TIMEOUTS_ERROR.getErrorCode()), MDC.get(ILogConfiguration.MDC_ERROR_CODE));
85 assertEquals(LogLevel.ERROR.name(), MDC.get(ILogConfiguration.MDC_ERROR_CATEGORY));
86 assertEquals(targetEntity, MDC.get(ILogConfiguration.MDC_TARGET_ENTITY));
87 assertEquals(serviceName, MDC.get(ILogConfiguration.MDC_SERVICE_NAME));
91 public void validateFatalMessageIsLoggedWhenAllErrorSettingsProvided() {
92 when(logger.isErrorEnabled()).thenReturn(true);
93 commonLogger.fatal(EcompLoggerErrorCode.PERMISSION_ERROR, serviceName, targetEntity, message);
95 verify(logger).error(any(Marker.class), captor.capture(), any(Object[].class));
96 assertEquals(message, captor.getValue());
97 assertEquals(String.valueOf(EcompLoggerErrorCode.PERMISSION_ERROR.getErrorCode()), MDC.get(ILogConfiguration.MDC_ERROR_CODE));
98 assertEquals(LogLevel.FATAL.name(), MDC.get(ILogConfiguration.MDC_ERROR_CATEGORY));
99 assertEquals(targetEntity, MDC.get(ILogConfiguration.MDC_TARGET_ENTITY));
100 assertEquals(serviceName, MDC.get(ILogConfiguration.MDC_SERVICE_NAME));
104 public void validateErrorMessageIsNotLoggedWhenErrorLevelIsDisabledEvenIfErrorSettingsProvided() {
105 commonLogger.error(EcompLoggerErrorCode.PERMISSION_ERROR, serviceName, targetEntity, message);
106 verify(logger, never()).error(any(Marker.class), any(String.class));
110 public void validateErrorLogWhenErrorSettingsProvidedPartially() {
111 when(logger.isErrorEnabled()).thenReturn(true);
112 commonLogger.error(message);
114 verify(logger).error(any(Marker.class), eq(message), any(Object[].class));
115 assertEquals(String.valueOf(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR.getErrorCode()), MDC.get(ILogConfiguration.MDC_ERROR_CODE));
116 assertEquals(LogLevel.ERROR.name(), MDC.get(ILogConfiguration.MDC_ERROR_CATEGORY));
117 assertNull(MDC.get(ILogConfiguration.MDC_TARGET_ENTITY));
118 assertEquals(LoggerError.defaultServiceName, MDC.get(ILogConfiguration.MDC_SERVICE_NAME));
122 public void errorMessageIsNotLoggedWhenErrorLevelIsDisabled() {
123 commonLogger.error(message);
124 verify(logger, times(0)).error(any(Marker.class), anyString());
128 public void traceMessageWithExceptionIsNotLoggedWhenTraceLevelIsDisabled() {
129 commonLogger.trace(message, new UnsupportedOperationException());
130 verify(logger, times(0)).trace(any(Marker.class), anyString());
134 public void verifyInfoMessage() {
135 when(logger.isInfoEnabled()).thenReturn(true);
136 commonLogger.info("Text");
137 assertEquals(LogLevel.INFO.name(), MDC.get(MDC_ERROR_CATEGORY));
138 assertEquals(String.valueOf(EcompLoggerErrorCode.SUCCESS.getErrorCode()), MDC.get(MDC_ERROR_CODE));
139 assertEquals(LoggerError.defaultServiceName, MDC.get(ILogConfiguration.MDC_SERVICE_NAME));
143 public void verifyWarnMessage() {
144 when(logger.isWarnEnabled()).thenReturn(true);
145 commonLogger.warn("Text");
146 assertEquals(LogLevel.WARN.name(), MDC.get(MDC_ERROR_CATEGORY));
147 assertEquals(String.valueOf(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR.getErrorCode()), MDC.get(MDC_ERROR_CODE));
148 assertEquals(LoggerError.defaultServiceName, MDC.get(ILogConfiguration.MDC_SERVICE_NAME));
152 public void validateErrorLogWithExceptionWhenErrorSettingsProvidedPartially() {
153 ThreadLocalsHolder.setUuid("uuid");
154 final String logFieldsNotProvidedMsg = "mandatory parameters for ECOMP logging, missing fields: ServiceName PartnerName";
155 when(logger.isWarnEnabled()).thenReturn(true);
156 commonLogger.warn(message, new NullPointerException(exceptionMsg));
158 //the expected warn message
159 verify(logger).warn(any(Marker.class), contains(message), any(Object[].class));
160 assertEquals(String.valueOf(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR.getErrorCode()), MDC.get(ILogConfiguration.MDC_ERROR_CODE));
161 assertEquals(LogLevel.WARN.name(), MDC.get(ILogConfiguration.MDC_ERROR_CATEGORY));
162 assertEquals("uuid", MDC.get(ILogConfiguration.MDC_KEY_REQUEST_ID));
163 assertNull(MDC.get(ILogConfiguration.MDC_TARGET_ENTITY));
164 assertEquals(LoggerError.defaultServiceName, MDC.get(ILogConfiguration.MDC_SERVICE_NAME));
169 public void validateDebugLogWithException() {
170 final String msg = "Debug message";
171 ThreadLocalsHolder.setUuid("uuid");
172 when(logger.isDebugEnabled()).thenReturn(true);
173 commonLogger.debug(msg, new RuntimeException());
175 verify(logger).debug(any(Marker.class), eq(msg), any(RuntimeException.class));
179 public void validateTraceLogWithExceptionAndPartialParamsAndDebugLevelDisabled() {
180 final String msg = "Debug message";
181 when(logger.isTraceEnabled()).thenReturn(true);
182 commonLogger.trace(msg, new RuntimeException());
184 verify(logger).trace(any(Marker.class), eq(msg), any(RuntimeException.class));
188 public void warnMessageWithParameterIsNotLoggedIfWarnLevelIsDisabled() {
189 commonLogger.warn("msg", "param");
190 verify(logger, times(0)).warn(any(Marker.class),
191 anyString(), any(Object.class));
195 public void verifyMdcValuesAreStoredWhenAuditAndErrorLoggersAreInvokedSequentially() throws URISyntaxException {
196 final String uuid = "12345";
197 final String message = "message";
198 when(requestContext.getHeaderString(anyString())).thenReturn("ab2222");
199 when(requestContext.getUriInfo()).thenReturn(uriInfo);
200 when(logger.isErrorEnabled()).thenReturn(true);
202 URI uri = new URI("http:/abc.com/getId");
203 when(uriInfo.getRequestUri()).thenReturn(uri);
204 when(uriInfo.getBaseUri()).thenReturn(uri);
205 when(statusType.getStatusCode()).thenReturn(200);
206 when(statusType.getReasonPhrase()).thenReturn("OK");
207 LoggerSdcAudit audit = new LoggerSdcAudit(this.getClass());
208 ThreadLocalsHolder.setUuid(uuid);
209 audit.startLog(requestContext);
210 audit.log("abc.log.com", requestContext, statusType, LogLevel.INFO, Severity.OK, message);
212 commonLogger.error(message);
213 verify(logger).error(any(Marker.class), eq(message), any(Object[].class));
214 assertEquals(uuid, MDC.get(MDC_KEY_REQUEST_ID));
215 assertEquals("/", MDC.get(MDC_SERVICE_NAME));
216 assertEquals(LogLevel.ERROR.name(), MDC.get(MDC_ERROR_CATEGORY));
217 assertEquals(String.valueOf(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR.getErrorCode()), MDC.get(MDC_ERROR_CODE));
221 public void verifyLoggerDoesNothingWhenTheLevelIsNotSet() {
222 if (commonLogger.isDebugEnabled()) {
223 commonLogger.debug("text");
225 verify(logger, times(0)).debug(any(Marker.class), anyString(), eq((Object[])null));
229 public void verifyLoggerTraceMethodIsCalledWhenTheLevelIsSet() {
230 ThreadLocalsHolder.setUuid("1234");
231 when(logger.isTraceEnabled()).thenReturn(true);
232 if (commonLogger.isTraceEnabled()) {
233 commonLogger.trace("text");
235 verify(logger, times(1)).trace(any(Marker.class), anyString(), eq((Object[])null));
240 public void verifyMdcValuesAreStoredWhenTraceLoggerIsInvokedAfterAuditStart() throws URISyntaxException {
241 final String uuid = "12345";
242 final String message = "message";
243 when(requestContext.getHeaderString(anyString())).thenReturn("ab2222");
244 when(requestContext.getUriInfo()).thenReturn(uriInfo);
245 when(logger.isTraceEnabled()).thenReturn(true);
247 URI uri = new URI("http:/abc.com/getId");
248 when(uriInfo.getRequestUri()).thenReturn(uri);
249 when(uriInfo.getBaseUri()).thenReturn(uri);
250 LoggerSdcAudit audit = new LoggerSdcAudit(this.getClass());
251 ThreadLocalsHolder.setUuid(uuid);
252 audit.startLog(requestContext);
254 commonLogger.trace(message);
255 verify(logger).trace(any(Marker.class), captor.capture(), eq((Object[])null));
256 assertEquals(message, captor.getValue());
257 assertEquals(uuid, MDC.get(MDC_KEY_REQUEST_ID));