2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.logging.aspects;
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;
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;
43 @PrepareForTest(LoggerFactory.class)
44 public class MetricsAspectTest extends PowerMockTestCase {
46 private static final Object OBJ_TO_RETURN = new Object();
47 private static final String EXPECTED_MESSAGE = "'{}' took {} milliseconds";
50 public void testLogExecutionTime() throws Throwable {
52 String className = UUID.randomUUID().toString();
53 String methodName = UUID.randomUUID().toString();
55 TestLogger logger = initLogging(className, true);
57 MetricsAspect aspect = new MetricsAspect();
58 MockProceedingJoinPoint pjp = new MockProceedingJoinPoint(className, methodName);
59 Object returned = aspect.logExecutionTime(pjp);
61 Assert.assertEquals(OBJ_TO_RETURN, returned);
62 assertExecution(methodName, pjp, logger);
66 public void testMetricsDisabled() throws Throwable {
68 String className = UUID.randomUUID().toString();
69 String methodName = UUID.randomUUID().toString();
71 TestLogger logger = initLogging(className, false);
73 MetricsAspect aspect = new MetricsAspect();
74 MockProceedingJoinPoint pjp = new MockProceedingJoinPoint(className, methodName);
75 Object returned = aspect.logExecutionTime(pjp);
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));
83 @Test(expectedExceptions = IllegalArgumentException.class)
84 public void testThrowingError() throws Throwable {
86 String className = UUID.randomUUID().toString();
87 String methodName = UUID.randomUUID().toString();
89 final TestLogger logger = initLogging(className, true);
91 MetricsAspect aspect = new MetricsAspect();
92 MockProceedingJoinPoint pjp = new MockProceedingJoinPointWithException(className, methodName);
95 aspect.logExecutionTime(pjp);
97 assertExecution(methodName, pjp, logger);
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);
109 private void assertExecution(String methodName, MockProceedingJoinPoint pjp, TestLogger logger) {
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)));
117 private static class MockSignature implements Signature {
119 private final String className;
120 private final String methodName;
122 private MockSignature(String className, String methodName) {
123 this.className = className;
124 this.methodName = methodName;
128 public String toShortString() {
133 public String toLongString() {
138 public String getName() {
143 public int getModifiers() {
148 public Class getDeclaringType() {
153 public String getDeclaringTypeName() {
158 private static class MockProceedingJoinPoint implements ProceedingJoinPoint {
160 private AtomicInteger count = new AtomicInteger(0);
161 private Signature signature;
163 MockProceedingJoinPoint(String className, String methodName) {
164 this.signature = new MockSignature(className, methodName);
172 public Object proceed() throws Throwable {
173 count.incrementAndGet();
174 return OBJ_TO_RETURN;
178 public void set$AroundClosure(AroundClosure aroundClosure) {
183 public Object proceed(Object[] objects) throws Throwable {
188 public String toShortString() {
193 public String toLongString() {
198 public Object getThis() {
203 public Object getTarget() {
208 public Object[] getArgs() {
209 return new Object[0];
213 public Signature getSignature() {
214 return this.signature;
218 public SourceLocation getSourceLocation() {
223 public String getKind() {
228 public StaticPart getStaticPart() {
233 private static class MockProceedingJoinPointWithException extends MockProceedingJoinPoint {
235 MockProceedingJoinPointWithException(String className, String methodName) {
236 super(className, methodName);
240 public Object proceed() throws Throwable {
242 throw new IllegalArgumentException();
246 private class TestLogger implements Logger {
248 private final boolean enabled;
249 private List<Object[]> events = Collections.synchronizedList(new ArrayList<>(10));
251 TestLogger(boolean enabled) {
252 this.enabled = enabled;
256 public String getName() {
257 throw new RuntimeException("Not implemented");
261 public boolean isMetricsEnabled() {
266 public void metrics(String var1) {
267 throw new RuntimeException("Not implemented");
271 public void metrics(String var1, Object var2) {
272 throw new RuntimeException("Not implemented");
276 public void metrics(String var1, Object var2, Object var3) {
279 events.add(new Object[]{var1, var2, var3});
284 public void metrics(String var1, Object... var2) {
285 throw new RuntimeException("Not implemented");
289 public void metrics(String var1, Throwable throwable) {
290 throw new RuntimeException("Not implemented");
294 public boolean isAuditEnabled() {
295 throw new RuntimeException("Not implemented");
299 public void audit(String var1) {
300 throw new RuntimeException("Not implemented");
304 public void audit(String var1, Object var2) {
305 throw new RuntimeException("Not implemented");
309 public void audit(String var1, Object var2, Object var3) {
310 throw new RuntimeException("Not implemented");
314 public void audit(String var1, Object... var2) {
315 throw new RuntimeException("Not implemented");
319 public void audit(String var1, Throwable throwable) {
320 throw new RuntimeException("Not implemented");
324 public boolean isDebugEnabled() {
325 throw new RuntimeException("Not implemented");
329 public void debug(String var1) {
330 throw new RuntimeException("Not implemented");
334 public void debug(String var1, Object var2) {
335 throw new RuntimeException("Not implemented");
339 public void debug(String var1, Object var2, Object var3) {
340 throw new RuntimeException("Not implemented");
344 public void debug(String var1, Object... var2) {
345 throw new RuntimeException("Not implemented");
349 public void debug(String var1, Throwable throwable) {
350 throw new RuntimeException("Not implemented");
354 public boolean isInfoEnabled() {
355 throw new RuntimeException("Not implemented");
359 public void info(String var1) {
360 throw new RuntimeException("Not implemented");
364 public void info(String var1, Object var2) {
365 throw new RuntimeException("Not implemented");
369 public void info(String var1, Object var2, Object var3) {
370 throw new RuntimeException("Not implemented");
374 public void info(String var1, Object... var2) {
375 throw new RuntimeException("Not implemented");
379 public void info(String var1, Throwable throwable) {
380 throw new RuntimeException("Not implemented");
384 public boolean isWarnEnabled() {
385 throw new RuntimeException("Not implemented");
389 public void warn(String var1) {
390 throw new RuntimeException("Not implemented");
394 public void warn(String var1, Object var2) {
395 throw new RuntimeException("Not implemented");
399 public void warn(String var1, Object... var2) {
400 throw new RuntimeException("Not implemented");
404 public void warn(String var1, Object var2, Object var3) {
405 throw new RuntimeException("Not implemented");
409 public void warn(String var1, Throwable throwable) {
410 throw new RuntimeException("Not implemented");
414 public boolean isErrorEnabled() {
415 throw new RuntimeException("Not implemented");
419 public void error(String var1) {
420 throw new RuntimeException("Not implemented");
424 public void error(String var1, Object var2) {
425 throw new RuntimeException("Not implemented");
429 public void error(String var1, Object var2, Object var3) {
430 throw new RuntimeException("Not implemented");
434 public void error(String var1, Object... var2) {
435 throw new RuntimeException("Not implemented");
439 public void error(String var1, Throwable throwable) {
440 throw new RuntimeException("Not implemented");
443 public boolean contains(Predicate<Object[]> predicate) {
444 return events.stream().anyMatch(predicate);