2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.logging.aspects;
23 import org.aspectj.lang.ProceedingJoinPoint;
24 import org.aspectj.lang.Signature;
25 import org.aspectj.lang.reflect.SourceLocation;
26 import org.aspectj.runtime.internal.AroundClosure;
27 import org.easymock.EasyMock;
28 import org.openecomp.sdc.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
30 import org.powermock.api.easymock.PowerMock;
31 import org.powermock.core.classloader.annotations.PrepareForTest;
32 import org.powermock.modules.testng.PowerMockTestCase;
33 import org.testng.Assert;
34 import org.testng.annotations.Test;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.List;
39 import java.util.UUID;
40 import java.util.concurrent.atomic.AtomicInteger;
41 import java.util.function.Predicate;
47 @PrepareForTest(LoggerFactory.class)
48 public class MetricsAspectTest extends PowerMockTestCase {
50 private static final Object OBJ_TO_RETURN = new Object();
51 private static final String EXPECTED_MESSAGE = "'{}' took {} milliseconds";
54 public void testLogExecutionTime() throws Throwable {
56 String className = UUID.randomUUID().toString();
57 String methodName = UUID.randomUUID().toString();
59 TestLogger logger = initLogging(className, true);
61 MetricsAspect aspect = new MetricsAspect();
62 MockProceedingJoinPoint pjp = new MockProceedingJoinPoint(className, methodName);
63 Object returned = aspect.logExecutionTime(pjp);
65 Assert.assertEquals(OBJ_TO_RETURN, returned);
66 assertExecution(methodName, pjp, logger);
70 public void testMetricsDisabled() throws Throwable {
72 String className = UUID.randomUUID().toString();
73 String methodName = UUID.randomUUID().toString();
75 TestLogger logger = initLogging(className, false);
77 MetricsAspect aspect = new MetricsAspect();
78 MockProceedingJoinPoint pjp = new MockProceedingJoinPoint(className, methodName);
79 Object returned = aspect.logExecutionTime(pjp);
81 Assert.assertEquals(OBJ_TO_RETURN, returned);
82 Assert.assertEquals(1, pjp.getCount());
83 // return any event - must be empty
84 Assert.assertFalse(logger.contains((event) -> true));
87 @Test(expectedExceptions = IllegalArgumentException.class)
88 public void testThrowingError() throws Throwable {
90 String className = UUID.randomUUID().toString();
91 String methodName = UUID.randomUUID().toString();
93 final TestLogger logger = initLogging(className, true);
95 MetricsAspect aspect = new MetricsAspect();
96 MockProceedingJoinPoint pjp = new MockProceedingJoinPointWithException(className, methodName);
99 aspect.logExecutionTime(pjp);
101 assertExecution(methodName, pjp, logger);
105 private TestLogger initLogging(String className, boolean enabled) {
106 TestLogger logger = new TestLogger(enabled);
107 PowerMock.mockStatic(LoggerFactory.class);
108 EasyMock.expect(LoggerFactory.getLogger(className)).andReturn(logger);
109 PowerMock.replay(LoggerFactory.class);
113 private void assertExecution(String methodName, MockProceedingJoinPoint pjp, TestLogger logger) {
115 Assert.assertEquals(1, pjp.getCount());
116 Assert.assertTrue(logger.contains((event) ->
117 (event != null) && (event.length == 3) && EXPECTED_MESSAGE.equals(event[0])
118 && methodName.equals(event[1]) && (event[2] instanceof Long)));
121 private static class MockSignature implements Signature {
123 private final String className;
124 private final String methodName;
126 private MockSignature(String className, String methodName) {
127 this.className = className;
128 this.methodName = methodName;
132 public String toShortString() {
137 public String toLongString() {
142 public String getName() {
147 public int getModifiers() {
152 public Class getDeclaringType() {
157 public String getDeclaringTypeName() {
162 private static class MockProceedingJoinPoint implements ProceedingJoinPoint {
164 private AtomicInteger count = new AtomicInteger(0);
165 private Signature signature;
167 MockProceedingJoinPoint(String className, String methodName) {
168 this.signature = new MockSignature(className, methodName);
176 public Object proceed() throws Throwable {
177 count.incrementAndGet();
178 return OBJ_TO_RETURN;
182 public void set$AroundClosure(AroundClosure aroundClosure) {
187 public Object proceed(Object[] objects) throws Throwable {
192 public String toShortString() {
197 public String toLongString() {
202 public Object getThis() {
207 public Object getTarget() {
212 public Object[] getArgs() {
213 return new Object[0];
217 public Signature getSignature() {
218 return this.signature;
222 public SourceLocation getSourceLocation() {
227 public String getKind() {
232 public StaticPart getStaticPart() {
237 private static class MockProceedingJoinPointWithException extends MockProceedingJoinPoint {
239 MockProceedingJoinPointWithException(String className, String methodName) {
240 super(className, methodName);
244 public Object proceed() throws Throwable {
246 throw new IllegalArgumentException();
250 private class TestLogger implements Logger {
252 private final boolean enabled;
253 private List<Object[]> events = Collections.synchronizedList(new ArrayList<>(10));
255 TestLogger(boolean enabled) {
256 this.enabled = enabled;
260 public String getName() {
261 throw new RuntimeException("Not implemented");
265 public boolean isMetricsEnabled() {
270 public void metrics(String var1) {
271 throw new RuntimeException("Not implemented");
275 public void metrics(String var1, Object var2) {
276 throw new RuntimeException("Not implemented");
280 public void metrics(String var1, Object var2, Object var3) {
283 events.add(new Object[]{var1, var2, var3});
288 public void metrics(String var1, Object... var2) {
289 throw new RuntimeException("Not implemented");
293 public void metrics(String var1, Throwable throwable) {
294 throw new RuntimeException("Not implemented");
298 public boolean isAuditEnabled() {
299 throw new RuntimeException("Not implemented");
303 public void audit(String var1) {
304 throw new RuntimeException("Not implemented");
308 public void audit(String var1, Object var2) {
309 throw new RuntimeException("Not implemented");
313 public void audit(String var1, Object var2, Object var3) {
314 throw new RuntimeException("Not implemented");
318 public void audit(String var1, Object... var2) {
319 throw new RuntimeException("Not implemented");
323 public void audit(String var1, Throwable throwable) {
324 throw new RuntimeException("Not implemented");
328 public boolean isDebugEnabled() {
329 throw new RuntimeException("Not implemented");
333 public void debug(String var1) {
334 throw new RuntimeException("Not implemented");
338 public void debug(String var1, Object var2) {
339 throw new RuntimeException("Not implemented");
343 public void debug(String var1, Object var2, Object var3) {
344 throw new RuntimeException("Not implemented");
348 public void debug(String var1, Object... var2) {
349 throw new RuntimeException("Not implemented");
353 public void debug(String var1, Throwable throwable) {
354 throw new RuntimeException("Not implemented");
358 public boolean isInfoEnabled() {
359 throw new RuntimeException("Not implemented");
363 public void info(String var1) {
364 throw new RuntimeException("Not implemented");
368 public void info(String var1, Object var2) {
369 throw new RuntimeException("Not implemented");
373 public void info(String var1, Object var2, Object var3) {
374 throw new RuntimeException("Not implemented");
378 public void info(String var1, Object... var2) {
379 throw new RuntimeException("Not implemented");
383 public void info(String var1, Throwable throwable) {
384 throw new RuntimeException("Not implemented");
388 public boolean isWarnEnabled() {
389 throw new RuntimeException("Not implemented");
393 public void warn(String var1) {
394 throw new RuntimeException("Not implemented");
398 public void warn(String var1, Object var2) {
399 throw new RuntimeException("Not implemented");
403 public void warn(String var1, Object... var2) {
404 throw new RuntimeException("Not implemented");
408 public void warn(String var1, Object var2, Object var3) {
409 throw new RuntimeException("Not implemented");
413 public void warn(String var1, Throwable throwable) {
414 throw new RuntimeException("Not implemented");
418 public boolean isErrorEnabled() {
419 throw new RuntimeException("Not implemented");
423 public void error(String var1) {
424 throw new RuntimeException("Not implemented");
428 public void error(String var1, Object var2) {
429 throw new RuntimeException("Not implemented");
433 public void error(String var1, Object var2, Object var3) {
434 throw new RuntimeException("Not implemented");
438 public void error(String var1, Object... var2) {
439 throw new RuntimeException("Not implemented");
443 public void error(String var1, Throwable throwable) {
444 throw new RuntimeException("Not implemented");
447 public boolean contains(Predicate<Object[]> predicate) {
448 return events.stream().anyMatch(predicate);