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.AuditData;
25 import org.openecomp.sdc.logging.api.Logger;
26 import org.openecomp.sdc.logging.api.LoggerFactory;
27 import org.powermock.api.easymock.PowerMock;
28 import org.powermock.core.classloader.annotations.PrepareForTest;
29 import org.powermock.modules.testng.PowerMockTestCase;
30 import org.testng.Assert;
31 import org.testng.annotations.Test;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.UUID;
37 import java.util.concurrent.atomic.AtomicInteger;
38 import java.util.function.Predicate;
44 @PrepareForTest(LoggerFactory.class)
45 public class MetricsAspectTest extends PowerMockTestCase {
47 private static final Object OBJ_TO_RETURN = new Object();
48 private static final String EXPECTED_MESSAGE = "'{}' took {} milliseconds";
51 public void testLogExecutionTime() throws Throwable {
53 String className = UUID.randomUUID().toString();
54 String methodName = UUID.randomUUID().toString();
56 TestLogger logger = initLogging(className, true);
58 MetricsAspect aspect = new MetricsAspect();
59 MockProceedingJoinPoint pjp = new MockProceedingJoinPoint(className, methodName);
60 Object returned = aspect.logExecutionTime(pjp);
62 Assert.assertEquals(OBJ_TO_RETURN, returned);
63 assertExecution(methodName, pjp, logger);
67 public void testMetricsDisabled() throws Throwable {
69 String className = UUID.randomUUID().toString();
70 String methodName = UUID.randomUUID().toString();
72 TestLogger logger = initLogging(className, false);
74 MetricsAspect aspect = new MetricsAspect();
75 MockProceedingJoinPoint pjp = new MockProceedingJoinPoint(className, methodName);
76 Object returned = aspect.logExecutionTime(pjp);
78 Assert.assertEquals(OBJ_TO_RETURN, returned);
79 Assert.assertEquals(1, pjp.getCount());
80 // return any event - must be empty
81 Assert.assertFalse(logger.contains((event) -> true));
84 @Test(expectedExceptions = IllegalArgumentException.class)
85 public void testThrowingError() throws Throwable {
87 String className = UUID.randomUUID().toString();
88 String methodName = UUID.randomUUID().toString();
90 final TestLogger logger = initLogging(className, true);
92 MetricsAspect aspect = new MetricsAspect();
93 MockProceedingJoinPoint pjp = new MockProceedingJoinPointWithException(className, methodName);
96 aspect.logExecutionTime(pjp);
98 assertExecution(methodName, pjp, logger);
102 private TestLogger initLogging(String className, boolean enabled) {
103 TestLogger logger = new TestLogger(enabled);
104 PowerMock.mockStatic(LoggerFactory.class);
105 EasyMock.expect(LoggerFactory.getLogger(className)).andReturn(logger);
106 PowerMock.replay(LoggerFactory.class);
110 private void assertExecution(String methodName, MockProceedingJoinPoint pjp, TestLogger logger) {
112 Assert.assertEquals(1, pjp.getCount());
113 Assert.assertTrue(logger.contains((event) ->
114 (event != null) && (event.length == 3) && EXPECTED_MESSAGE.equals(event[0])
115 && methodName.equals(event[1]) && (event[2] instanceof Long)));
118 private static class MockSignature implements Signature {
120 private final String className;
121 private final String methodName;
123 private MockSignature(String className, String methodName) {
124 this.className = className;
125 this.methodName = methodName;
129 public String toShortString() {
134 public String toLongString() {
139 public String getName() {
144 public int getModifiers() {
149 public Class getDeclaringType() {
154 public String getDeclaringTypeName() {
159 private static class MockProceedingJoinPoint implements ProceedingJoinPoint {
161 private AtomicInteger count = new AtomicInteger(0);
162 private Signature signature;
164 MockProceedingJoinPoint(String className, String methodName) {
165 this.signature = new MockSignature(className, methodName);
173 public Object proceed() throws Throwable {
174 count.incrementAndGet();
175 return OBJ_TO_RETURN;
179 public void set$AroundClosure(AroundClosure aroundClosure) {
184 public Object proceed(Object[] objects) throws Throwable {
189 public String toShortString() {
194 public String toLongString() {
199 public Object getThis() {
204 public Object getTarget() {
209 public Object[] getArgs() {
210 return new Object[0];
214 public Signature getSignature() {
215 return this.signature;
219 public SourceLocation getSourceLocation() {
224 public String getKind() {
229 public StaticPart getStaticPart() {
234 private static class MockProceedingJoinPointWithException extends MockProceedingJoinPoint {
236 MockProceedingJoinPointWithException(String className, String methodName) {
237 super(className, methodName);
241 public Object proceed() throws Throwable {
243 throw new IllegalArgumentException();
247 private class TestLogger implements Logger {
249 private final boolean enabled;
250 private List<Object[]> events = Collections.synchronizedList(new ArrayList<>(10));
252 TestLogger(boolean enabled) {
253 this.enabled = enabled;
257 public String getName() {
258 throw new RuntimeException("Not implemented");
262 public boolean isMetricsEnabled() {
267 public void metrics(String var1) {
268 throw new RuntimeException("Not implemented");
272 public void metrics(String var1, Object var2) {
273 throw new RuntimeException("Not implemented");
277 public void metrics(String var1, Object var2, Object var3) {
280 events.add(new Object[]{var1, var2, var3});
285 public void metrics(String var1, Object... var2) {
286 throw new RuntimeException("Not implemented");
290 public void metrics(String var1, Throwable throwable) {
291 throw new RuntimeException("Not implemented");
295 public boolean isAuditEnabled() {
296 throw new RuntimeException("Not implemented");
300 public void audit(AuditData var1) {
301 throw new RuntimeException("Not implemented");
305 public boolean isDebugEnabled() {
306 throw new RuntimeException("Not implemented");
310 public void debug(String var1) {
311 throw new RuntimeException("Not implemented");
315 public void debug(String var1, Object var2) {
316 throw new RuntimeException("Not implemented");
320 public void debug(String var1, Object var2, Object var3) {
321 throw new RuntimeException("Not implemented");
325 public void debug(String var1, Object... var2) {
326 throw new RuntimeException("Not implemented");
330 public void debug(String var1, Throwable throwable) {
331 throw new RuntimeException("Not implemented");
335 public boolean isInfoEnabled() {
336 throw new RuntimeException("Not implemented");
340 public void info(String var1) {
341 throw new RuntimeException("Not implemented");
345 public void info(String var1, Object var2) {
346 throw new RuntimeException("Not implemented");
350 public void info(String var1, Object var2, Object var3) {
351 throw new RuntimeException("Not implemented");
355 public void info(String var1, Object... var2) {
356 throw new RuntimeException("Not implemented");
360 public void info(String var1, Throwable throwable) {
361 throw new RuntimeException("Not implemented");
365 public boolean isWarnEnabled() {
366 throw new RuntimeException("Not implemented");
370 public void warn(String var1) {
371 throw new RuntimeException("Not implemented");
375 public void warn(String var1, Object var2) {
376 throw new RuntimeException("Not implemented");
380 public void warn(String var1, Object... var2) {
381 throw new RuntimeException("Not implemented");
385 public void warn(String var1, Object var2, Object var3) {
386 throw new RuntimeException("Not implemented");
390 public void warn(String var1, Throwable throwable) {
391 throw new RuntimeException("Not implemented");
395 public boolean isErrorEnabled() {
396 throw new RuntimeException("Not implemented");
400 public void error(String var1) {
401 throw new RuntimeException("Not implemented");
405 public void error(String var1, Object var2) {
406 throw new RuntimeException("Not implemented");
410 public void error(String var1, Object var2, Object var3) {
411 throw new RuntimeException("Not implemented");
415 public void error(String var1, Object... var2) {
416 throw new RuntimeException("Not implemented");
420 public void error(String var1, Throwable throwable) {
421 throw new RuntimeException("Not implemented");
424 public boolean contains(Predicate<Object[]> predicate) {
425 return events.stream().anyMatch(predicate);