ae8ed2cbdbf537f3ef0d662fdb36b8e9cb15c98c
[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 package org.oransc.policyagent.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.Rule;
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.MockitoJUnit;
40 import org.mockito.junit.MockitoRule;
41 import org.mockito.junit.jupiter.MockitoExtension;
42 import org.oransc.policyagent.utils.LoggingUtils;
43
44 @ExtendWith(MockitoExtension.class)
45 class LogAspectTest {
46     @Rule
47     MockitoRule mockitoRule = MockitoJUnit.rule();
48
49     @Mock
50     private ProceedingJoinPoint proceedingJoinPoint;
51
52     @Mock
53     private MethodSignature methodSignature;
54
55     private LogAspect sampleAspect = new LogAspect();
56
57     @Test
58     void testExecutetimeTime_shouldLogTime() throws Throwable {
59         when(proceedingJoinPoint.getSignature()).thenReturn(methodSignature);
60         when(methodSignature.getDeclaringType()).thenReturn(this.getClass());
61
62         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(LogAspect.class, TRACE);
63
64         sampleAspect.executimeTime(proceedingJoinPoint);
65         // 'proceed()' is called exactly once
66         verify(proceedingJoinPoint, times(1)).proceed();
67         // 'proceed(Object[])' is never called
68         verify(proceedingJoinPoint, never()).proceed(null);
69
70         assertThat(logAppender.list.get(0).getFormattedMessage()).startsWith("Execution time of");
71     }
72
73     @Test
74     void testEntryLog_shouldLogEntry() throws Throwable {
75         when(proceedingJoinPoint.getSignature()).thenReturn(methodSignature);
76         String signature = "signature";
77         when(methodSignature.getName()).thenReturn(signature);
78
79         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(LogAspect.class, TRACE);
80
81         sampleAspect.entryLog(proceedingJoinPoint);
82
83         assertThat(logAppender.list.get(0).getFormattedMessage()).isEqualTo("Entering method: " + signature);
84     }
85
86     @Test
87     void testExitLog_shouldLogExit() throws Throwable {
88         when(proceedingJoinPoint.getSignature()).thenReturn(methodSignature);
89         String signature = "signature";
90         when(methodSignature.getName()).thenReturn(signature);
91
92         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(LogAspect.class, TRACE);
93
94         sampleAspect.exitLog(proceedingJoinPoint);
95
96         assertThat(logAppender.list.get(0).getFormattedMessage()).isEqualTo("Exiting method: " + signature);
97     }
98 }