2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2020 Nordix Foundation. 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.onap.ccsdk.oran.a1policymanagementservice.aspect;
23 import static ch.qos.logback.classic.Level.TRACE;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.mockito.Mockito.never;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
30 import ch.qos.logback.classic.spi.ILoggingEvent;
31 import ch.qos.logback.core.read.ListAppender;
33 import org.aspectj.lang.ProceedingJoinPoint;
34 import org.aspectj.lang.reflect.MethodSignature;
35 import org.junit.jupiter.api.DisplayName;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.mockito.Mock;
39 import org.mockito.junit.jupiter.MockitoExtension;
40 import org.onap.ccsdk.oran.a1policymanagementservice.utils.LoggingUtils;
42 @ExtendWith(MockitoExtension.class)
45 private ProceedingJoinPoint proceedingJoinPoint;
48 private MethodSignature methodSignature;
50 private LogAspect sampleAspect = new LogAspect();
55 @DisplayName("test Executetime Time should Log Time")
56 void testExecutetimeTime_shouldLogTime() throws Throwable {
57 when(proceedingJoinPoint.getSignature()).thenReturn(methodSignature);
58 when(methodSignature.getDeclaringType()).thenReturn(this.getClass());
60 final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(LogAspect.class, TRACE);
62 sampleAspect.executimeTime(proceedingJoinPoint);
63 // 'proceed()' is called exactly once
64 verify(proceedingJoinPoint, times(1)).proceed();
65 // 'proceed(Object[])' is never called
66 verify(proceedingJoinPoint, never()).proceed(null);
68 assertThat(logAppender.list.get(0).getFormattedMessage()).startsWith("Execution time of");
72 @DisplayName("test Entry Log should Log Entry")
73 void testEntryLog_shouldLogEntry() throws Throwable {
74 when(proceedingJoinPoint.getSignature()).thenReturn(methodSignature);
75 String signature = "signature";
76 when(methodSignature.getName()).thenReturn(signature);
78 final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(LogAspect.class, TRACE);
80 sampleAspect.entryLog(proceedingJoinPoint);
82 assertThat(logAppender.list.get(0).getFormattedMessage()).isEqualTo("Entering method: " + signature);
86 @DisplayName("test Exit Log should Log Exit")
87 void testExitLog_shouldLogExit() throws Throwable {
88 when(proceedingJoinPoint.getSignature()).thenReturn(methodSignature);
89 String signature = "signature";
90 when(methodSignature.getName()).thenReturn(signature);
92 final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(LogAspect.class, TRACE);
94 sampleAspect.exitLog(proceedingJoinPoint);
96 assertThat(logAppender.list.get(0).getFormattedMessage()).isEqualTo("Exiting method: " + signature);