Added oparent to sdc main
[sdc.git] / openecomp-be / lib / openecomp-sdc-logging-lib / openecomp-sdc-logging-core / src / test / java / org / openecomp / sdc / logging / slf4j / SLF4JLoggerWrapperTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.logging.slf4j;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22
23 import java.lang.reflect.InvocationHandler;
24 import java.lang.reflect.Method;
25 import java.lang.reflect.Proxy;
26 import java.util.Arrays;
27 import java.util.Map;
28 import org.junit.Test;
29 import org.openecomp.sdc.logging.api.AuditData;
30 import org.openecomp.sdc.logging.api.MetricsData;
31 import org.openecomp.sdc.logging.api.StatusCode;
32 import org.slf4j.Logger;
33 import org.slf4j.MDC;
34 import org.slf4j.Marker;
35
36 /**
37  * Unit-test of SLF4J implementation of Logger.
38  *
39  * @author evitaliy
40  * @since 05 Mar 18
41  */
42 @SuppressWarnings("CheckStyle")
43 public class SLF4JLoggerWrapperTest {
44
45     @Test
46     public void auditDoesNotFailWhenInputNull() {
47         new SLF4JLoggerWrapper(this.getClass()).audit(null);
48     }
49
50     @Test
51     public void metricsDoesNotFailWhenInputNull() {
52         new SLF4JLoggerWrapper(this.getClass()).metrics((MetricsData) null);
53     }
54
55     @Test
56     public void auditBeginTimeAvailableWhenPassed() {
57         SpyLogger spy = createSpy();
58         long start = System.currentTimeMillis();
59         new SLF4JLoggerWrapper(spy).audit(AuditData.builder().startTime(start).build());
60         assertNotNull(spy.mdc().get(AuditField.BEGIN_TIMESTAMP.asKey()));
61     }
62
63     @Test
64     public void metricsBeginTimeAvailableWhenPassed() {
65         SpyLogger spy = createSpy();
66         long start = System.currentTimeMillis();
67         new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().startTime(start).build());
68         assertNotNull(spy.mdc().get(MetricsField.BEGIN_TIMESTAMP.asKey()));
69     }
70
71     @Test
72     public void auditEndTimeAvailableWhenPassed() {
73         SpyLogger spy = createSpy();
74         long end = System.currentTimeMillis();
75         new SLF4JLoggerWrapper(spy).audit(AuditData.builder().endTime(end).build());
76         assertNotNull(spy.mdc().get(AuditField.END_TIMESTAMP.asKey()));
77     }
78
79     @Test
80     public void metricsEndTimeAvailableWhenPassed() {
81         SpyLogger spy = createSpy();
82         long end = System.currentTimeMillis();
83         new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().endTime(end).build());
84         assertNotNull(spy.mdc().get(MetricsField.END_TIMESTAMP.asKey()));
85     }
86
87     @Test
88     public void auditElapsedTimeAvailableWhenPassed() {
89         SpyLogger spy = createSpy();
90         long start = System.currentTimeMillis();
91         new SLF4JLoggerWrapper(spy).audit(AuditData.builder()
92                                                    .startTime(start).endTime(start + 777).build());
93         assertEquals("777", spy.mdc().get(AuditField.ELAPSED_TIME.asKey()));
94     }
95
96     @Test
97     public void metricsElapsedTimeAvailableWhenPassed() {
98         SpyLogger spy = createSpy();
99         long start = System.currentTimeMillis();
100         new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder()
101                                                        .startTime(start).endTime(start + 1024).build());
102         assertEquals("1024", spy.mdc().get(MetricsField.ELAPSED_TIME.asKey()));
103     }
104
105     @Test
106     public void auditStatusCodeAvailableWhenPassed() {
107         SpyLogger spy = createSpy();
108         new SLF4JLoggerWrapper(spy).audit(AuditData.builder().statusCode(StatusCode.COMPLETE).build());
109         assertEquals(StatusCode.COMPLETE.name(), spy.mdc().get(AuditField.STATUS_CODE.asKey()));
110     }
111
112     @Test
113     public void metricsStatusCodeAvailableWhenPassed() {
114         SpyLogger spy = createSpy();
115         new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().statusCode(StatusCode.COMPLETE).build());
116         assertEquals(StatusCode.COMPLETE.name(), spy.mdc().get(MetricsField.STATUS_CODE.asKey()));
117     }
118
119     @Test
120     public void auditStatusCodeEmptyWhenNotPassed() {
121         SpyLogger spy = createSpy();
122         new SLF4JLoggerWrapper(spy).audit(AuditData.builder().build());
123         assertNull(spy.mdc().get(AuditField.STATUS_CODE.asKey()));
124     }
125
126     @Test
127     public void metricsStatusCodeEmptyWhenNotPassed() {
128         SpyLogger spy = createSpy();
129         new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().build());
130         assertNull(spy.mdc().get(MetricsField.STATUS_CODE.asKey()));
131     }
132
133     @Test
134     public void auditResponseCodeAvailableWhenPassed() {
135         final String responseCode = "AuditSpyResponse";
136         SpyLogger spy = createSpy();
137         new SLF4JLoggerWrapper(spy).audit(AuditData.builder().responseCode(responseCode).build());
138         assertEquals(responseCode, spy.mdc().get(AuditField.RESPONSE_CODE.asKey()));
139     }
140
141     @Test
142     public void metricsResponseCodeAvailableWhenPassed() {
143         final String responseCode = "MetricsSpyResponse";
144         SpyLogger spy = createSpy();
145         new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().responseCode(responseCode).build());
146         assertEquals(responseCode, spy.mdc().get(MetricsField.RESPONSE_CODE.asKey()));
147     }
148
149     @Test
150     public void auditResponseCodeEmptyWhenNotPassed() {
151         SpyLogger spy = createSpy();
152         new SLF4JLoggerWrapper(spy).audit(AuditData.builder().build());
153         assertNull(spy.mdc().get(AuditField.RESPONSE_CODE.asKey()));
154     }
155
156     @Test
157     public void metricsResponseCodeEmptyWhenNotPassed() {
158         SpyLogger spy = createSpy();
159         new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().build());
160         assertNull(spy.mdc().get(MetricsField.RESPONSE_CODE.asKey()));
161     }
162
163     @Test
164     public void auditResponseDescriptionAvailableWhenPassed() {
165         final String responseDescription = "AuditSpyDescription";
166         SpyLogger spy = createSpy();
167         new SLF4JLoggerWrapper(spy).audit(AuditData.builder().responseDescription(responseDescription).build());
168         assertEquals(responseDescription, spy.mdc().get(AuditField.RESPONSE_DESCRIPTION.asKey()));
169     }
170
171     @Test
172     public void metricsResponseDescriptionAvailableWhenPassed() {
173         final String responseDescription = "MetricsSpyDescription";
174         SpyLogger spy = createSpy();
175         new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().responseDescription(responseDescription).build());
176         assertEquals(responseDescription, spy.mdc().get(MetricsField.RESPONSE_DESCRIPTION.asKey()));
177     }
178
179     @Test
180     public void auditResponseDescriptionEmptyWhenNotPassed() {
181         SpyLogger spy = createSpy();
182         new SLF4JLoggerWrapper(spy).audit(AuditData.builder().build());
183         assertNull(spy.mdc().get(AuditField.RESPONSE_DESCRIPTION.asKey()));
184     }
185
186     @Test
187     public void metricsResponseDescriptionEmptyWhenNotPassed() {
188         SpyLogger spy = createSpy();
189         new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().build());
190         assertNull(spy.mdc().get(MetricsField.RESPONSE_DESCRIPTION.asKey()));
191     }
192
193     @Test
194     public void auditClientIpAddressAvailableWhenPassed() {
195         final String ipAddress = "10.56.20.20";
196         SpyLogger spy = createSpy();
197         new SLF4JLoggerWrapper(spy).audit(AuditData.builder().clientIpAddress(ipAddress).build());
198         assertEquals(ipAddress, spy.mdc().get(AuditField.CLIENT_IP_ADDRESS.asKey()));
199     }
200
201     @Test
202     public void metricsClientIpAddressAvailableWhenPassed() {
203         final String ipAddress = "10.56.20.22";
204         SpyLogger spy = createSpy();
205         new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().clientIpAddress(ipAddress).build());
206         assertEquals(ipAddress, spy.mdc().get(MetricsField.CLIENT_IP_ADDRESS.asKey()));
207     }
208
209     @Test
210     public void auditClientIpAddressEmptyWhenNotPassed() {
211         SpyLogger spy = createSpy();
212         new SLF4JLoggerWrapper(spy).audit(AuditData.builder().build());
213         assertNull(spy.mdc().get(AuditField.CLIENT_IP_ADDRESS.asKey()));
214     }
215
216     @Test
217     public void metricsClientIpAddressEmptyWhenNotPassed() {
218         SpyLogger spy = createSpy();
219         new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().build());
220         assertNull(spy.mdc().get(MetricsField.CLIENT_IP_ADDRESS.asKey()));
221     }
222
223     @Test
224     public void metricsTargetEntityAvailableWhenPassed() {
225         final String targetEntity = "MetricsTargetEntity";
226         SpyLogger spy = createSpy();
227         new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().targetEntity(targetEntity).build());
228         assertEquals(targetEntity, spy.mdc().get(MetricsField.TARGET_ENTITY.asKey()));
229     }
230
231     @Test
232     public void metricsTargetEntityEmptyWhenNotPassed() {
233         SpyLogger spy = createSpy();
234         new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().build());
235         assertNull(spy.mdc().get(MetricsField.TARGET_ENTITY.asKey()));
236     }
237
238     @Test
239     public void metricsTargetVirtualEntityAvailableWhenPassed() {
240         final String targetEntity = "MetricsTargetVirtualEntity";
241         SpyLogger spy = createSpy();
242         new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().targetVirtualEntity(targetEntity).build());
243         assertEquals(targetEntity, spy.mdc().get(MetricsField.TARGET_VIRTUAL_ENTITY.asKey()));
244     }
245
246     @Test
247     public void metricsTargetVirtualEntityEmptyWhenNotPassed() {
248         SpyLogger spy = createSpy();
249         new SLF4JLoggerWrapper(spy).metrics(MetricsData.builder().build());
250         assertNull(spy.mdc().get(MetricsField.TARGET_VIRTUAL_ENTITY.asKey()));
251     }
252
253     interface SpyLogger extends Logger {
254         Map<String, String> mdc();
255     }
256
257     /**
258      * Creates a in instance of Logger that can be used to track MDC changes as part of an invocation of
259      * Logger.audit().
260      *
261      * @return object that implements {@link SpyLogger}
262      */
263     private static SpyLogger createSpy() {
264
265         // build a dynamic proxy to avoid implementing the long list of Logger methods
266         // when we actually need just Logger.info() with the audit marker
267         ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
268         return (SpyLogger) Proxy.newProxyInstance(classLoader, new Class<?>[] {SpyLogger.class},
269                 new SpyingInvocationHandler());
270     }
271
272     private static class SpyingInvocationHandler implements InvocationHandler {
273
274         private Map<String, String> lastMdc;
275
276         @Override
277         public Object invoke(Object proxy, Method method, Object[] args) {
278
279             if (isReturnMdcMethod(method)) {
280                 return lastMdc;
281             }
282
283             if (!isAuditMethod(method, args) && !isMetricsMethod(method, args)) {
284                 throw new UnsupportedOperationException("Method " + method.getName() + " with arguments "
285                         + Arrays.toString(args) + " wasn't supposed to be called");
286             }
287
288             storeEffectiveMdc();
289             return null;
290         }
291
292         private boolean isMetricsMethod(Method method, Object[] args) {
293             return isSpecialLogMethod(method, args, Markers.METRICS);
294         }
295
296         private boolean isAuditMethod(Method method, Object[] args) {
297             return isSpecialLogMethod(method, args, Markers.AUDIT);
298         }
299
300         private boolean isSpecialLogMethod(Method method, Object[] args, Marker marker) {
301             return method.getName().equals("info") && args.length > 0 && args[0].equals(marker);
302         }
303
304         private void storeEffectiveMdc() {
305             lastMdc = MDC.getCopyOfContextMap();
306         }
307
308         private boolean isReturnMdcMethod(Method method) {
309             return method.equals(SpyLogger.class.getDeclaredMethods()[0]);
310         }
311     }
312 }