2 * Copyright © 2016-2018 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 java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.UUID;
23 import java.util.concurrent.atomic.AtomicInteger;
24 import java.util.function.Predicate;
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.openecomp.sdc.logging.api.AuditData;
31 import org.openecomp.sdc.logging.api.Logger;
32 import org.openecomp.sdc.logging.api.LoggerFactory;
33 import org.powermock.api.easymock.PowerMock;
34 import org.powermock.core.classloader.annotations.PrepareForTest;
35 import org.powermock.modules.testng.PowerMockTestCase;
36 import org.testng.Assert;
37 import org.testng.annotations.Test;
40 * Unit-tests metrics aspect (AOP) behavior.
45 @PrepareForTest(LoggerFactory.class)
46 public class MetricsAspectTest extends PowerMockTestCase {
48 private static final Object OBJ_TO_RETURN = new Object();
49 private static final String EXPECTED_MESSAGE = "'{}' took {} milliseconds";
52 public void testLogExecutionTime() throws Throwable {
54 String className = UUID.randomUUID().toString();
55 String methodName = UUID.randomUUID().toString();
57 TestLogger logger = initLogging(className, true);
59 MetricsAspect aspect = new MetricsAspect();
60 MockProceedingJoinPoint pjp = new MockProceedingJoinPoint(className, methodName);
61 Object returned = aspect.logExecutionTime(pjp);
63 Assert.assertEquals(OBJ_TO_RETURN, returned);
64 assertExecution(methodName, pjp, logger);
67 private TestLogger initLogging(String className, boolean enabled) {
68 TestLogger logger = new TestLogger(enabled);
69 PowerMock.mockStatic(LoggerFactory.class);
70 EasyMock.expect(LoggerFactory.getLogger(className)).andReturn(logger);
71 PowerMock.replay(LoggerFactory.class);
75 private void assertExecution(String methodName, MockProceedingJoinPoint pjp, TestLogger logger) {
77 Assert.assertEquals(1, pjp.getCount());
78 Assert.assertTrue(logger.contains(
79 (event) -> (event != null) && (event.length == 3) && EXPECTED_MESSAGE.equals(event[0]) && methodName
80 .equals(event[1]) && (event[2] instanceof Long)));
84 public void testMetricsDisabled() throws Throwable {
86 String className = UUID.randomUUID().toString();
87 String methodName = UUID.randomUUID().toString();
89 TestLogger logger = initLogging(className, false);
91 MetricsAspect aspect = new MetricsAspect();
92 MockProceedingJoinPoint pjp = new MockProceedingJoinPoint(className, methodName);
93 Object returned = aspect.logExecutionTime(pjp);
95 Assert.assertEquals(OBJ_TO_RETURN, returned);
96 Assert.assertEquals(1, pjp.getCount());
97 // return any event - must be empty
98 Assert.assertFalse(logger.contains((event) -> true));
101 @Test(expectedExceptions = IllegalArgumentException.class)
102 public void testThrowingError() throws Throwable {
104 String className = UUID.randomUUID().toString();
105 String methodName = UUID.randomUUID().toString();
107 final TestLogger logger = initLogging(className, true);
109 MetricsAspect aspect = new MetricsAspect();
110 MockProceedingJoinPoint pjp = new MockProceedingJoinPointWithException(className, methodName);
113 aspect.logExecutionTime(pjp);
115 assertExecution(methodName, pjp, logger);
119 private static class MockSignature implements Signature {
121 private final String className;
122 private final String methodName;
124 private MockSignature(String className, String methodName) {
125 this.className = className;
126 this.methodName = methodName;
130 public String toShortString() {
135 public String toLongString() {
140 public String getName() {
145 public int getModifiers() {
150 public Class getDeclaringType() {
155 public String getDeclaringTypeName() {
160 private static class MockProceedingJoinPoint implements ProceedingJoinPoint {
162 private final AtomicInteger count = new AtomicInteger(0);
163 private final Signature signature;
165 MockProceedingJoinPoint(String className, String methodName) {
166 this.signature = new MockSignature(className, methodName);
174 public void set$AroundClosure(AroundClosure aroundClosure) {
179 public Object proceed() throws Throwable {
180 count.incrementAndGet();
181 return OBJ_TO_RETURN;
185 public Object proceed(Object[] objects) {
190 public String toShortString() {
195 public String toLongString() {
200 public Object getThis() {
205 public Object getTarget() {
210 public Object[] getArgs() {
211 return new Object[0];
215 public Signature getSignature() {
216 return this.signature;
220 public SourceLocation getSourceLocation() {
225 public String getKind() {
230 public StaticPart getStaticPart() {
235 private static class MockProceedingJoinPointWithException extends MockProceedingJoinPoint {
237 MockProceedingJoinPointWithException(String className, String methodName) {
238 super(className, methodName);
242 public Object proceed() throws Throwable {
244 throw new IllegalArgumentException();
248 private class TestLogger implements Logger {
250 private final boolean enabled;
251 private final List<Object[]> events = Collections.synchronizedList(new ArrayList<>(10));
253 TestLogger(boolean enabled) {
254 this.enabled = enabled;
258 public String getName() {
259 throw new RuntimeException("Not implemented");
263 public boolean isMetricsEnabled() {
268 public void metrics(String var1) {
269 throw new RuntimeException("Not implemented");
273 public void metrics(String var1, Object var2) {
274 throw new RuntimeException("Not implemented");
278 public void metrics(String var1, Object var2, Object var3) {
281 events.add(new Object[] {var1, var2, var3});
286 public void metrics(String var1, Object... var2) {
287 throw new RuntimeException("Not implemented");
291 public void metrics(String var1, Throwable throwable) {
292 throw new RuntimeException("Not implemented");
296 public boolean isAuditEnabled() {
297 throw new RuntimeException("Not implemented");
301 public void audit(AuditData var1) {
302 throw new RuntimeException("Not implemented");
306 public boolean isDebugEnabled() {
307 throw new RuntimeException("Not implemented");
311 public void debug(String var1) {
312 throw new RuntimeException("Not implemented");
316 public void debug(String var1, Object var2) {
317 throw new RuntimeException("Not implemented");
321 public void debug(String var1, Object var2, Object var3) {
322 throw new RuntimeException("Not implemented");
326 public void debug(String var1, Object... var2) {
327 throw new RuntimeException("Not implemented");
331 public void debug(String var1, Throwable throwable) {
332 throw new RuntimeException("Not implemented");
336 public boolean isInfoEnabled() {
337 throw new RuntimeException("Not implemented");
341 public void info(String var1) {
342 throw new RuntimeException("Not implemented");
346 public void info(String var1, Object var2) {
347 throw new RuntimeException("Not implemented");
351 public void info(String var1, Object var2, Object var3) {
352 throw new RuntimeException("Not implemented");
356 public void info(String var1, Object... var2) {
357 throw new RuntimeException("Not implemented");
361 public void info(String var1, Throwable throwable) {
362 throw new RuntimeException("Not implemented");
366 public boolean isWarnEnabled() {
367 throw new RuntimeException("Not implemented");
371 public void warn(String var1) {
372 throw new RuntimeException("Not implemented");
376 public void warn(String var1, Object var2) {
377 throw new RuntimeException("Not implemented");
381 public void warn(String var1, Object... var2) {
382 throw new RuntimeException("Not implemented");
386 public void warn(String var1, Object var2, Object var3) {
387 throw new RuntimeException("Not implemented");
391 public void warn(String var1, Throwable throwable) {
392 throw new RuntimeException("Not implemented");
396 public boolean isErrorEnabled() {
397 throw new RuntimeException("Not implemented");
401 public void error(String var1) {
402 throw new RuntimeException("Not implemented");
406 public void error(String var1, Object var2) {
407 throw new RuntimeException("Not implemented");
411 public void error(String var1, Object var2, Object var3) {
412 throw new RuntimeException("Not implemented");
416 public void error(String var1, Object... var2) {
417 throw new RuntimeException("Not implemented");
421 public void error(String var1, Throwable throwable) {
422 throw new RuntimeException("Not implemented");
425 boolean contains(Predicate<Object[]> predicate) {
426 return events.stream().anyMatch(predicate);