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.core.logging.aspects;
23 import org.openecomp.core.logging.api.Logger;
24 import org.openecomp.core.logging.api.LoggerFactory;
25 import org.aspectj.lang.ProceedingJoinPoint;
26 import org.aspectj.lang.Signature;
27 import org.aspectj.lang.reflect.SourceLocation;
28 import org.aspectj.runtime.internal.AroundClosure;
29 import org.easymock.EasyMock;
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;
35 import sun.reflect.generics.reflectiveObjects.NotImplementedException;
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.List;
40 import java.util.UUID;
41 import java.util.concurrent.atomic.AtomicInteger;
42 import java.util.function.Predicate;
48 @PrepareForTest(LoggerFactory.class)
49 public class MetricsAspectTest extends PowerMockTestCase {
51 private static final Object OBJ_TO_RETURN = new Object();
52 private static final String EXPECTED_MESSAGE = "'{}' took {} milliseconds";
55 public void testLogExecutionTime() throws Throwable {
57 String className = UUID.randomUUID().toString();
58 String methodName = UUID.randomUUID().toString();
60 TestLogger logger = initLogging(className, true);
62 MetricsAspect aspect = new MetricsAspect();
63 MockProceedingJoinPoint pjp = new MockProceedingJoinPoint(className, methodName);
64 Object returned = aspect.logExecutionTime(pjp);
66 Assert.assertEquals(OBJ_TO_RETURN, returned);
67 assertExecution(methodName, pjp, logger);
71 public void testMetricsDisabled() throws Throwable {
73 String className = UUID.randomUUID().toString();
74 String methodName = UUID.randomUUID().toString();
76 TestLogger logger = initLogging(className, false);
78 MetricsAspect aspect = new MetricsAspect();
79 MockProceedingJoinPoint pjp = new MockProceedingJoinPoint(className, methodName);
80 Object returned = aspect.logExecutionTime(pjp);
82 Assert.assertEquals(OBJ_TO_RETURN, returned);
83 Assert.assertEquals(1, pjp.getCount());
84 // return any event - must be empty
85 Assert.assertFalse(logger.contains((event) -> true));
88 @Test(expectedExceptions = IllegalArgumentException.class)
89 public void testThrowingError() throws Throwable {
91 String className = UUID.randomUUID().toString();
92 String methodName = UUID.randomUUID().toString();
94 final TestLogger logger = initLogging(className, true);
96 MetricsAspect aspect = new MetricsAspect();
97 MockProceedingJoinPoint pjp = new MockProceedingJoinPointWithException(className, methodName);
100 aspect.logExecutionTime(pjp);
102 assertExecution(methodName, pjp, logger);
106 private TestLogger initLogging(String className, boolean enabled) {
107 TestLogger logger = new TestLogger(enabled);
108 PowerMock.mockStatic(LoggerFactory.class);
109 EasyMock.expect(LoggerFactory.getLogger(className)).andReturn(logger);
110 PowerMock.replay(LoggerFactory.class);
114 private void assertExecution(String methodName, MockProceedingJoinPoint pjp, TestLogger logger) {
116 Assert.assertEquals(1, pjp.getCount());
117 Assert.assertTrue(logger.contains((event) ->
118 (event != null) && (event.length == 3) && EXPECTED_MESSAGE.equals(event[0])
119 && methodName.equals(event[1]) && (event[2] instanceof Long)));
122 private static class MockSignature implements Signature {
124 private final String className;
125 private final String methodName;
127 private MockSignature(String className, String methodName) {
128 this.className = className;
129 this.methodName = methodName;
133 public String toShortString() {
138 public String toLongString() {
143 public String getName() {
148 public int getModifiers() {
153 public Class getDeclaringType() {
158 public String getDeclaringTypeName() {
163 private static class MockProceedingJoinPoint implements ProceedingJoinPoint {
165 private AtomicInteger count = new AtomicInteger(0);
166 private Signature signature;
168 MockProceedingJoinPoint(String className, String methodName) {
169 this.signature = new MockSignature(className, methodName);
177 public Object proceed() throws Throwable {
178 count.incrementAndGet();
179 return OBJ_TO_RETURN;
183 public void set$AroundClosure(AroundClosure aroundClosure) {
188 public Object proceed(Object[] objects) throws Throwable {
193 public String toShortString() {
198 public String toLongString() {
203 public Object getThis() {
208 public Object getTarget() {
213 public Object[] getArgs() {
214 return new Object[0];
218 public Signature getSignature() {
219 return this.signature;
223 public SourceLocation getSourceLocation() {
228 public String getKind() {
233 public StaticPart getStaticPart() {
238 private static class MockProceedingJoinPointWithException extends MockProceedingJoinPoint {
240 MockProceedingJoinPointWithException(String className, String methodName) {
241 super(className, methodName);
245 public Object proceed() throws Throwable {
247 throw new IllegalArgumentException();
251 private class TestLogger implements Logger {
253 private final boolean enabled;
254 private List<Object[]> events = Collections.synchronizedList(new ArrayList<>(10));
256 public TestLogger(boolean enabled) {
257 this.enabled = enabled;
261 public String getName() {
262 throw new NotImplementedException();
266 public boolean isMetricsEnabled() {
271 public void metrics(String var1) {
272 throw new NotImplementedException();
276 public void metrics(String var1, Object var2) {
277 throw new NotImplementedException();
281 public void metrics(String var1, Object var2, Object var3) {
284 events.add(new Object[]{var1, var2, var3});
289 public void metrics(String var1, Object... var2) {
290 throw new NotImplementedException();
294 public void metrics(String var1, Throwable throwable) {
295 throw new NotImplementedException();
299 public boolean isAuditEnabled() {
300 throw new NotImplementedException();
304 public void audit(String var1) {
305 throw new NotImplementedException();
309 public void audit(String var1, Object var2) {
310 throw new NotImplementedException();
314 public void audit(String var1, Object var2, Object var3) {
315 throw new NotImplementedException();
319 public void audit(String var1, Object... var2) {
320 throw new NotImplementedException();
324 public void audit(String var1, Throwable throwable) {
325 throw new NotImplementedException();
329 public boolean isDebugEnabled() {
330 throw new NotImplementedException();
334 public void debug(String var1) {
335 throw new NotImplementedException();
339 public void debug(String var1, Object var2) {
340 throw new NotImplementedException();
344 public void debug(String var1, Object var2, Object var3) {
345 throw new NotImplementedException();
349 public void debug(String var1, Object... var2) {
350 throw new NotImplementedException();
354 public void debug(String var1, Throwable throwable) {
355 throw new NotImplementedException();
359 public boolean isInfoEnabled() {
360 throw new NotImplementedException();
364 public void info(String var1) {
365 throw new NotImplementedException();
369 public void info(String var1, Object var2) {
370 throw new NotImplementedException();
374 public void info(String var1, Object var2, Object var3) {
375 throw new NotImplementedException();
379 public void info(String var1, Object... var2) {
380 throw new NotImplementedException();
384 public void info(String var1, Throwable throwable) {
385 throw new NotImplementedException();
389 public boolean isWarnEnabled() {
390 throw new NotImplementedException();
394 public void warn(String var1) {
395 throw new NotImplementedException();
399 public void warn(String var1, Object var2) {
400 throw new NotImplementedException();
404 public void warn(String var1, Object... var2) {
405 throw new NotImplementedException();
409 public void warn(String var1, Object var2, Object var3) {
410 throw new NotImplementedException();
414 public void warn(String var1, Throwable throwable) {
415 throw new NotImplementedException();
419 public boolean isErrorEnabled() {
420 throw new NotImplementedException();
424 public void error(String var1) {
425 throw new NotImplementedException();
429 public void error(String var1, Object var2) {
430 throw new NotImplementedException();
434 public void error(String var1, Object var2, Object var3) {
435 throw new NotImplementedException();
439 public void error(String var1, Object... var2) {
440 throw new NotImplementedException();
444 public void error(String var1, Throwable throwable) {
445 throw new NotImplementedException();
448 public boolean contains(Predicate<Object[]> predicate) {
449 return events.stream().anyMatch(predicate);