Merge "Add license text"
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / aspect / LogAspectTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
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
22 package org.oransc.policyagent.aspect;
23
24 import static ch.qos.logback.classic.Level.TRACE;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.mockito.Mockito.never;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import ch.qos.logback.classic.spi.ILoggingEvent;
32 import ch.qos.logback.core.read.ListAppender;
33
34 import org.aspectj.lang.ProceedingJoinPoint;
35 import org.aspectj.lang.reflect.MethodSignature;
36 import org.junit.Rule;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.mockito.Mock;
40 import org.mockito.junit.MockitoJUnit;
41 import org.mockito.junit.MockitoRule;
42 import org.mockito.junit.jupiter.MockitoExtension;
43 import org.oransc.policyagent.utils.LoggingUtils;
44
45 @ExtendWith(MockitoExtension.class)
46 class LogAspectTest {
47     @Rule
48     MockitoRule mockitoRule = MockitoJUnit.rule();
49
50     @Mock
51     private ProceedingJoinPoint proceedingJoinPoint;
52
53     @Mock
54     private MethodSignature methodSignature;
55
56     private LogAspect sampleAspect = new LogAspect();
57
58     @Test
59     void testExecutetimeTime_shouldLogTime() throws Throwable {
60         when(proceedingJoinPoint.getSignature()).thenReturn(methodSignature);
61         when(methodSignature.getDeclaringType()).thenReturn(this.getClass());
62
63         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(LogAspect.class, TRACE);
64
65         sampleAspect.executimeTime(proceedingJoinPoint);
66         // 'proceed()' is called exactly once
67         verify(proceedingJoinPoint, times(1)).proceed();
68         // 'proceed(Object[])' is never called
69         verify(proceedingJoinPoint, never()).proceed(null);
70
71         assertThat(logAppender.list.get(0).getFormattedMessage()).startsWith("Execution time of");
72     }
73
74     @Test
75     void testEntryLog_shouldLogEntry() throws Throwable {
76         when(proceedingJoinPoint.getSignature()).thenReturn(methodSignature);
77         String signature = "signature";
78         when(methodSignature.getName()).thenReturn(signature);
79
80         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(LogAspect.class, TRACE);
81
82         sampleAspect.entryLog(proceedingJoinPoint);
83
84         assertThat(logAppender.list.get(0).getFormattedMessage()).isEqualTo("Entering method: " + signature);
85     }
86
87     @Test
88     void testExitLog_shouldLogExit() throws Throwable {
89         when(proceedingJoinPoint.getSignature()).thenReturn(methodSignature);
90         String signature = "signature";
91         when(methodSignature.getName()).thenReturn(signature);
92
93         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(LogAspect.class, TRACE);
94
95         sampleAspect.exitLog(proceedingJoinPoint);
96
97         assertThat(logAppender.list.get(0).getFormattedMessage()).isEqualTo("Exiting method: " + signature);
98     }
99 }