re base code
[sdc.git] / common-app-api / src / test / java / org / openecomp / sdc / common / log / wrappers / LoggerTest.java
1 package org.openecomp.sdc.common.log.wrappers;
2
3 import org.junit.Before;
4 import org.junit.Test;
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;
17 import org.slf4j.MDC;
18 import org.slf4j.Marker;
19
20 import javax.ws.rs.container.ContainerRequestContext;
21 import javax.ws.rs.core.Response;
22 import javax.ws.rs.core.UriInfo;
23 import java.net.URI;
24 import java.net.URISyntaxException;
25
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.*;
34
35 @RunWith(MockitoJUnitRunner.class)
36 public class LoggerTest {
37
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";
43
44     @Mock
45     private org.slf4j.Logger logger;
46     @Mock
47     private ContainerRequestContext requestContext;
48     @Mock
49     private UriInfo uriInfo;
50     @Mock
51     private Response.StatusType statusType;
52
53     @InjectMocks
54     private Logger commonLogger;
55
56     @Captor
57     private ArgumentCaptor<String> captor;
58
59     @Before
60     public void setUp() {
61         MDC.clear();
62     }
63
64     @Test
65     public void validateErrorLogWhenErrorSettingsProvided() {
66         when(logger.isErrorEnabled()).thenReturn(true);
67         commonLogger.error(EcompLoggerErrorCode.PERMISSION_ERROR, serviceName, targetEntity, message);
68
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));
75     }
76
77     @Test
78     public void validateWarnMessageIsLoggedWhenAllErrorSettingsProvided() {
79         when(logger.isErrorEnabled()).thenReturn(true);
80         commonLogger.error(EcompLoggerErrorCode.AVAILABILITY_TIMEOUTS_ERROR, serviceName, targetEntity, message);
81
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));
88     }
89
90     @Test
91     public void validateFatalMessageIsLoggedWhenAllErrorSettingsProvided() {
92         when(logger.isErrorEnabled()).thenReturn(true);
93         commonLogger.fatal(EcompLoggerErrorCode.PERMISSION_ERROR, serviceName, targetEntity, message);
94
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));
101     }
102
103     @Test
104     public void validateErrorMessageIsNotLoggedWhenErrorLevelIsDisabledEvenIfErrorSettingsProvided() {
105         commonLogger.error(EcompLoggerErrorCode.PERMISSION_ERROR, serviceName, targetEntity, message);
106         verify(logger, never()).error(any(Marker.class), any(String.class));
107     }
108
109     @Test
110     public void validateErrorLogWhenErrorSettingsProvidedPartially() {
111         when(logger.isErrorEnabled()).thenReturn(true);
112         commonLogger.error(message);
113
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));
119     }
120
121     @Test
122     public void errorMessageIsNotLoggedWhenErrorLevelIsDisabled() {
123         commonLogger.error(message);
124         verify(logger, times(0)).error(any(Marker.class), anyString());
125     }
126
127     @Test
128     public void traceMessageWithExceptionIsNotLoggedWhenTraceLevelIsDisabled() {
129         commonLogger.trace(message, new UnsupportedOperationException());
130         verify(logger, times(0)).trace(any(Marker.class), anyString());
131     }
132
133     @Test
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));
140     }
141
142     @Test
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));
149     }
150
151     @Test
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));
157
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));
165     }
166
167
168     @Test
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());
174
175         verify(logger).debug(any(Marker.class), eq(msg), any(RuntimeException.class));
176     }
177
178     @Test
179     public void validateTraceLogWithExceptionAndPartialParamsAndDebugLevelDisabled() {
180         final String msg = "Debug message";
181         when(logger.isTraceEnabled()).thenReturn(true);
182         commonLogger.trace(msg, new RuntimeException());
183
184         verify(logger).trace(any(Marker.class), eq(msg), any(RuntimeException.class));
185     }
186
187     @Test
188     public void warnMessageWithParameterIsNotLoggedIfWarnLevelIsDisabled() {
189         commonLogger.warn("msg", "param");
190         verify(logger, times(0)).warn(any(Marker.class),
191                                     anyString(), any(Object.class));
192     }
193
194     @Test
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);
201
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);
211
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));
218     }
219
220     @Test
221     public void verifyLoggerDoesNothingWhenTheLevelIsNotSet() {
222         if (commonLogger.isDebugEnabled()) {
223             commonLogger.debug("text");
224         }
225         verify(logger, times(0)).debug(any(Marker.class), anyString(), eq((Object[])null));
226     }
227
228     @Test
229     public void verifyLoggerTraceMethodIsCalledWhenTheLevelIsSet() {
230         ThreadLocalsHolder.setUuid("1234");
231         when(logger.isTraceEnabled()).thenReturn(true);
232         if (commonLogger.isTraceEnabled()) {
233             commonLogger.trace("text");
234         }
235         verify(logger, times(1)).trace(any(Marker.class), anyString(), eq((Object[])null));
236     }
237
238
239     @Test
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);
246
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);
253
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));
258     }
259
260
261 }