65dc8b5a5d5f62b00ea540bd25a0da4af559febc
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.aspect;
22
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;
29
30 import ch.qos.logback.classic.spi.ILoggingEvent;
31 import ch.qos.logback.core.read.ListAppender;
32
33 import org.aspectj.lang.ProceedingJoinPoint;
34 import org.aspectj.lang.reflect.MethodSignature;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.extension.ExtendWith;
37 import org.mockito.Mock;
38 import org.mockito.junit.jupiter.MockitoExtension;
39 import org.onap.ccsdk.oran.a1policymanagementservice.utils.LoggingUtils;
40
41 @ExtendWith(MockitoExtension.class)
42 class LogAspectTest {
43     @Mock
44     private ProceedingJoinPoint proceedingJoinPoint;
45
46     @Mock
47     private MethodSignature methodSignature;
48
49     private LogAspect sampleAspect = new LogAspect();
50
51     @Test
52     void testExecutetimeTime_shouldLogTime() throws Throwable {
53         when(proceedingJoinPoint.getSignature()).thenReturn(methodSignature);
54         when(methodSignature.getDeclaringType()).thenReturn(this.getClass());
55
56         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(LogAspect.class, TRACE);
57
58         sampleAspect.executimeTime(proceedingJoinPoint);
59         // 'proceed()' is called exactly once
60         verify(proceedingJoinPoint, times(1)).proceed();
61         // 'proceed(Object[])' is never called
62         verify(proceedingJoinPoint, never()).proceed(null);
63
64         assertThat(logAppender.list.get(0).getFormattedMessage()).startsWith("Execution time of");
65     }
66
67     @Test
68     void testEntryLog_shouldLogEntry() throws Throwable {
69         when(proceedingJoinPoint.getSignature()).thenReturn(methodSignature);
70         String signature = "signature";
71         when(methodSignature.getName()).thenReturn(signature);
72
73         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(LogAspect.class, TRACE);
74
75         sampleAspect.entryLog(proceedingJoinPoint);
76
77         assertThat(logAppender.list.get(0).getFormattedMessage()).isEqualTo("Entering method: " + signature);
78     }
79
80     @Test
81     void testExitLog_shouldLogExit() throws Throwable {
82         when(proceedingJoinPoint.getSignature()).thenReturn(methodSignature);
83         String signature = "signature";
84         when(methodSignature.getName()).thenReturn(signature);
85
86         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(LogAspect.class, TRACE);
87
88         sampleAspect.exitLog(proceedingJoinPoint);
89
90         assertThat(logAppender.list.get(0).getFormattedMessage()).isEqualTo("Exiting method: " + signature);
91     }
92 }