42cbdcef143eac56b169c3cf80de0e4eebd1b1d8
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 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.aspects;
18
19 import org.aspectj.lang.ProceedingJoinPoint;
20 import org.aspectj.lang.Signature;
21 import org.aspectj.lang.reflect.SourceLocation;
22 import org.aspectj.runtime.internal.AroundClosure;
23 import org.easymock.EasyMock;
24 import org.openecomp.sdc.logging.api.Logger;
25 import org.openecomp.sdc.logging.api.LoggerFactory;
26 import org.powermock.api.easymock.PowerMock;
27 import org.powermock.core.classloader.annotations.PrepareForTest;
28 import org.powermock.modules.testng.PowerMockTestCase;
29 import org.testng.Assert;
30 import org.testng.annotations.Test;
31
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.UUID;
36 import java.util.concurrent.atomic.AtomicInteger;
37 import java.util.function.Predicate;
38
39 /**
40  * @author EVITALIY
41  * @since 17/08/2016.
42  */
43 @PrepareForTest(LoggerFactory.class)
44 public class MetricsAspectTest extends PowerMockTestCase {
45
46   private static final Object OBJ_TO_RETURN = new Object();
47   private static final String EXPECTED_MESSAGE = "'{}' took {} milliseconds";
48
49   @Test
50   public void testLogExecutionTime() throws Throwable {
51
52     String className = UUID.randomUUID().toString();
53     String methodName = UUID.randomUUID().toString();
54
55     TestLogger logger = initLogging(className, true);
56
57     MetricsAspect aspect = new MetricsAspect();
58     MockProceedingJoinPoint pjp = new MockProceedingJoinPoint(className, methodName);
59     Object returned = aspect.logExecutionTime(pjp);
60
61     Assert.assertEquals(OBJ_TO_RETURN, returned);
62     assertExecution(methodName, pjp, logger);
63   }
64
65   @Test
66   public void testMetricsDisabled() throws Throwable {
67
68     String className = UUID.randomUUID().toString();
69     String methodName = UUID.randomUUID().toString();
70
71     TestLogger logger = initLogging(className, false);
72
73     MetricsAspect aspect = new MetricsAspect();
74     MockProceedingJoinPoint pjp = new MockProceedingJoinPoint(className, methodName);
75     Object returned = aspect.logExecutionTime(pjp);
76
77     Assert.assertEquals(OBJ_TO_RETURN, returned);
78     Assert.assertEquals(1, pjp.getCount());
79     // return any event - must be empty
80     Assert.assertFalse(logger.contains((event) -> true));
81   }
82
83   @Test(expectedExceptions = IllegalArgumentException.class)
84   public void testThrowingError() throws Throwable {
85
86     String className = UUID.randomUUID().toString();
87     String methodName = UUID.randomUUID().toString();
88
89     final TestLogger logger = initLogging(className, true);
90
91     MetricsAspect aspect = new MetricsAspect();
92     MockProceedingJoinPoint pjp = new MockProceedingJoinPointWithException(className, methodName);
93
94     try {
95       aspect.logExecutionTime(pjp);
96     } finally {
97       assertExecution(methodName, pjp, logger);
98     }
99   }
100
101   private TestLogger initLogging(String className, boolean enabled) {
102     TestLogger logger = new TestLogger(enabled);
103     PowerMock.mockStatic(LoggerFactory.class);
104     EasyMock.expect(LoggerFactory.getLogger(className)).andReturn(logger);
105     PowerMock.replay(LoggerFactory.class);
106     return logger;
107   }
108
109   private void assertExecution(String methodName, MockProceedingJoinPoint pjp, TestLogger logger) {
110
111     Assert.assertEquals(1, pjp.getCount());
112     Assert.assertTrue(logger.contains((event) ->
113         (event != null) && (event.length == 3) && EXPECTED_MESSAGE.equals(event[0])
114             && methodName.equals(event[1]) && (event[2] instanceof Long)));
115   }
116
117   private static class MockSignature implements Signature {
118
119     private final String className;
120     private final String methodName;
121
122     private MockSignature(String className, String methodName) {
123       this.className = className;
124       this.methodName = methodName;
125     }
126
127     @Override
128     public String toShortString() {
129       return null;
130     }
131
132     @Override
133     public String toLongString() {
134       return null;
135     }
136
137     @Override
138     public String getName() {
139       return methodName;
140     }
141
142     @Override
143     public int getModifiers() {
144       return 0;
145     }
146
147     @Override
148     public Class getDeclaringType() {
149       return null;
150     }
151
152     @Override
153     public String getDeclaringTypeName() {
154       return className;
155     }
156   }
157
158   private static class MockProceedingJoinPoint implements ProceedingJoinPoint {
159
160     private AtomicInteger count = new AtomicInteger(0);
161     private Signature signature;
162
163     MockProceedingJoinPoint(String className, String methodName) {
164       this.signature = new MockSignature(className, methodName);
165     }
166
167     int getCount() {
168       return count.get();
169     }
170
171     @Override
172     public Object proceed() throws Throwable {
173       count.incrementAndGet();
174       return OBJ_TO_RETURN;
175     }
176
177     @Override
178     public void set$AroundClosure(AroundClosure aroundClosure) {
179
180     }
181
182     @Override
183     public Object proceed(Object[] objects) throws Throwable {
184       return null;
185     }
186
187     @Override
188     public String toShortString() {
189       return null;
190     }
191
192     @Override
193     public String toLongString() {
194       return null;
195     }
196
197     @Override
198     public Object getThis() {
199       return null;
200     }
201
202     @Override
203     public Object getTarget() {
204       return null;
205     }
206
207     @Override
208     public Object[] getArgs() {
209       return new Object[0];
210     }
211
212     @Override
213     public Signature getSignature() {
214       return this.signature;
215     }
216
217     @Override
218     public SourceLocation getSourceLocation() {
219       return null;
220     }
221
222     @Override
223     public String getKind() {
224       return null;
225     }
226
227     @Override
228     public StaticPart getStaticPart() {
229       return null;
230     }
231   }
232
233   private static class MockProceedingJoinPointWithException extends MockProceedingJoinPoint {
234
235     MockProceedingJoinPointWithException(String className, String methodName) {
236       super(className, methodName);
237     }
238
239     @Override
240     public Object proceed() throws Throwable {
241       super.proceed();
242       throw new IllegalArgumentException();
243     }
244   }
245
246   private class TestLogger implements Logger {
247
248     private final boolean enabled;
249     private List<Object[]> events = Collections.synchronizedList(new ArrayList<>(10));
250
251     TestLogger(boolean enabled) {
252       this.enabled = enabled;
253     }
254
255     @Override
256     public String getName() {
257       throw new RuntimeException("Not implemented");
258     }
259
260     @Override
261     public boolean isMetricsEnabled() {
262       return this.enabled;
263     }
264
265     @Override
266     public void metrics(String var1) {
267       throw new RuntimeException("Not implemented");
268     }
269
270     @Override
271     public void metrics(String var1, Object var2) {
272       throw new RuntimeException("Not implemented");
273     }
274
275     @Override
276     public void metrics(String var1, Object var2, Object var3) {
277
278       if (this.enabled) {
279         events.add(new Object[]{var1, var2, var3});
280       }
281     }
282
283     @Override
284     public void metrics(String var1, Object... var2) {
285       throw new RuntimeException("Not implemented");
286     }
287
288     @Override
289     public void metrics(String var1, Throwable throwable) {
290       throw new RuntimeException("Not implemented");
291     }
292
293     @Override
294     public boolean isAuditEnabled() {
295       throw new RuntimeException("Not implemented");
296     }
297
298     @Override
299     public void audit(String var1) {
300       throw new RuntimeException("Not implemented");
301     }
302
303     @Override
304     public void audit(String var1, Object var2) {
305       throw new RuntimeException("Not implemented");
306     }
307
308     @Override
309     public void audit(String var1, Object var2, Object var3) {
310       throw new RuntimeException("Not implemented");
311     }
312
313     @Override
314     public void audit(String var1, Object... var2) {
315       throw new RuntimeException("Not implemented");
316     }
317
318     @Override
319     public void audit(String var1, Throwable throwable) {
320       throw new RuntimeException("Not implemented");
321     }
322
323     @Override
324     public boolean isDebugEnabled() {
325       throw new RuntimeException("Not implemented");
326     }
327
328     @Override
329     public void debug(String var1) {
330       throw new RuntimeException("Not implemented");
331     }
332
333     @Override
334     public void debug(String var1, Object var2) {
335       throw new RuntimeException("Not implemented");
336     }
337
338     @Override
339     public void debug(String var1, Object var2, Object var3) {
340       throw new RuntimeException("Not implemented");
341     }
342
343     @Override
344     public void debug(String var1, Object... var2) {
345       throw new RuntimeException("Not implemented");
346     }
347
348     @Override
349     public void debug(String var1, Throwable throwable) {
350       throw new RuntimeException("Not implemented");
351     }
352
353     @Override
354     public boolean isInfoEnabled() {
355       throw new RuntimeException("Not implemented");
356     }
357
358     @Override
359     public void info(String var1) {
360       throw new RuntimeException("Not implemented");
361     }
362
363     @Override
364     public void info(String var1, Object var2) {
365       throw new RuntimeException("Not implemented");
366     }
367
368     @Override
369     public void info(String var1, Object var2, Object var3) {
370       throw new RuntimeException("Not implemented");
371     }
372
373     @Override
374     public void info(String var1, Object... var2) {
375       throw new RuntimeException("Not implemented");
376     }
377
378     @Override
379     public void info(String var1, Throwable throwable) {
380       throw new RuntimeException("Not implemented");
381     }
382
383     @Override
384     public boolean isWarnEnabled() {
385       throw new RuntimeException("Not implemented");
386     }
387
388     @Override
389     public void warn(String var1) {
390       throw new RuntimeException("Not implemented");
391     }
392
393     @Override
394     public void warn(String var1, Object var2) {
395       throw new RuntimeException("Not implemented");
396     }
397
398     @Override
399     public void warn(String var1, Object... var2) {
400       throw new RuntimeException("Not implemented");
401     }
402
403     @Override
404     public void warn(String var1, Object var2, Object var3) {
405       throw new RuntimeException("Not implemented");
406     }
407
408     @Override
409     public void warn(String var1, Throwable throwable) {
410       throw new RuntimeException("Not implemented");
411     }
412
413     @Override
414     public boolean isErrorEnabled() {
415       throw new RuntimeException("Not implemented");
416     }
417
418     @Override
419     public void error(String var1) {
420       throw new RuntimeException("Not implemented");
421     }
422
423     @Override
424     public void error(String var1, Object var2) {
425       throw new RuntimeException("Not implemented");
426     }
427
428     @Override
429     public void error(String var1, Object var2, Object var3) {
430       throw new RuntimeException("Not implemented");
431     }
432
433     @Override
434     public void error(String var1, Object... var2) {
435       throw new RuntimeException("Not implemented");
436     }
437
438     @Override
439     public void error(String var1, Throwable throwable) {
440       throw new RuntimeException("Not implemented");
441     }
442
443     public boolean contains(Predicate<Object[]> predicate) {
444       return events.stream().anyMatch(predicate);
445     }
446   }
447 }