Remove call stack from original exceptions in log
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / tasks / RicSynchronizationTaskTest.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.tasks;
22
23 import static ch.qos.logback.classic.Level.WARN;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.anyString;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.spy;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.verifyNoInteractions;
33 import static org.mockito.Mockito.verifyNoMoreInteractions;
34 import static org.mockito.Mockito.when;
35
36 import ch.qos.logback.classic.spi.ILoggingEvent;
37 import ch.qos.logback.core.read.ListAppender;
38
39 import java.time.Duration;
40 import java.util.Arrays;
41 import java.util.Collections;
42
43 import org.junit.jupiter.api.BeforeEach;
44 import org.junit.jupiter.api.Test;
45 import org.junit.jupiter.api.extension.ExtendWith;
46 import org.mockito.Mock;
47 import org.mockito.junit.jupiter.MockitoExtension;
48 import org.oransc.policyagent.clients.A1Client;
49 import org.oransc.policyagent.clients.A1ClientFactory;
50 import org.oransc.policyagent.clients.AsyncRestClient;
51 import org.oransc.policyagent.configuration.ImmutableRicConfig;
52 import org.oransc.policyagent.repository.ImmutablePolicy;
53 import org.oransc.policyagent.repository.ImmutablePolicyType;
54 import org.oransc.policyagent.repository.Policies;
55 import org.oransc.policyagent.repository.Policy;
56 import org.oransc.policyagent.repository.PolicyType;
57 import org.oransc.policyagent.repository.PolicyTypes;
58 import org.oransc.policyagent.repository.Ric;
59 import org.oransc.policyagent.repository.Ric.RicState;
60 import org.oransc.policyagent.repository.Service;
61 import org.oransc.policyagent.repository.Services;
62 import org.oransc.policyagent.utils.LoggingUtils;
63
64 import reactor.core.publisher.Flux;
65 import reactor.core.publisher.Mono;
66
67 @ExtendWith(MockitoExtension.class)
68 public class RicSynchronizationTaskTest {
69     private static final String POLICY_TYPE_1_NAME = "type1";
70     private static final PolicyType POLICY_TYPE_1 = ImmutablePolicyType.builder() //
71         .name(POLICY_TYPE_1_NAME) //
72         .schema("") //
73         .build();
74
75     private static final String RIC_1_NAME = "ric1";
76     private static final Ric RIC_1 = new Ric(ImmutableRicConfig.builder() //
77         .name(RIC_1_NAME) //
78         .baseUrl("baseUrl1") //
79         .managedElementIds(Collections.emptyList()) //
80         .controllerName("controllerName") //
81         .build());
82
83     private static final Policy POLICY_1 = ImmutablePolicy.builder() //
84         .id("policyId1") //
85         .json("") //
86         .ownerServiceName("service") //
87         .ric(RIC_1) //
88         .type(POLICY_TYPE_1) //
89         .lastModified("now") //
90         .build();
91
92     private static final String SERVICE_1_NAME = "service1";
93     private static final String SERVICE_1_CALLBACK_URL = "callbackUrl";
94     private static final Service SERVICE_1 = new Service(SERVICE_1_NAME, Duration.ofSeconds(1), SERVICE_1_CALLBACK_URL);
95
96     @Mock
97     private A1Client a1ClientMock;
98
99     @Mock
100     private A1ClientFactory a1ClientFactoryMock;
101
102     private PolicyTypes policyTypes;
103     private Policies policies;
104     private Services services;
105
106     @BeforeEach
107     public void init() {
108         policyTypes = new PolicyTypes();
109         policies = new Policies();
110         services = new Services();
111         RIC_1.setState(RicState.UNDEFINED);
112         RIC_1.clearSupportedPolicyTypes();
113     }
114
115     @Test
116     public void ricAlreadySynchronizing_thenNoSynchronization() {
117         RIC_1.setState(RicState.SYNCHRONIZING);
118         RIC_1.addSupportedPolicyType(POLICY_TYPE_1);
119
120         policyTypes.put(POLICY_TYPE_1);
121         policies.put(POLICY_1);
122
123         RicSynchronizationTask synchronizerUnderTest =
124             new RicSynchronizationTask(a1ClientFactoryMock, policyTypes, policies, services);
125
126         synchronizerUnderTest.run(RIC_1);
127
128         verifyNoInteractions(a1ClientMock);
129
130         assertThat(policyTypes.size()).isEqualTo(1);
131         assertThat(policies.size()).isEqualTo(1);
132         assertThat(RIC_1.getState()).isEqualTo(RicState.SYNCHRONIZING);
133         assertThat(RIC_1.getSupportedPolicyTypeNames().size()).isEqualTo(1);
134     }
135
136     @Test
137     public void ricIdlePolicyTypeInRepo_thenSynchronizationWithReuseOfTypeFromRepoAndCorrectServiceNotified() {
138         RIC_1.setState(RicState.IDLE);
139
140         policyTypes.put(POLICY_TYPE_1);
141
142         services.put(SERVICE_1);
143         Service serviceWithoutCallbackUrlShouldNotBeNotified = new Service("service2", Duration.ofSeconds(1), "");
144         services.put(serviceWithoutCallbackUrlShouldNotBeNotified);
145
146         setUpCreationOfA1Client();
147         simulateRicWithOnePolicyType();
148
149         RicSynchronizationTask synchronizerUnderTest =
150             spy(new RicSynchronizationTask(a1ClientFactoryMock, policyTypes, policies, services));
151
152         AsyncRestClient restClientMock = setUpCreationOfAsyncRestClient(synchronizerUnderTest);
153         when(restClientMock.put(anyString(), anyString())).thenReturn(Mono.just("Ok"));
154
155         synchronizerUnderTest.run(RIC_1);
156
157         verify(a1ClientMock, times(1)).getPolicyTypeIdentities();
158         verifyNoMoreInteractions(a1ClientMock);
159
160         verify(synchronizerUnderTest).run(RIC_1);
161         verify(synchronizerUnderTest).createNotificationClient(SERVICE_1_CALLBACK_URL);
162         verifyNoMoreInteractions(synchronizerUnderTest);
163
164         verify(restClientMock).put("", "Synchronization completed for:" + RIC_1_NAME);
165         verifyNoMoreInteractions(restClientMock);
166
167         assertThat(policyTypes.size()).isEqualTo(1);
168         assertThat(policies.size()).isEqualTo(0);
169         assertThat(RIC_1.getState()).isEqualTo(RicState.IDLE);
170     }
171
172     @Test
173     public void ricIdlePolicyTypeNotInRepo_thenSynchronizationWithTypeFromRic() throws Exception {
174         RIC_1.setState(RicState.IDLE);
175
176         setUpCreationOfA1Client();
177         simulateRicWithOnePolicyType();
178         String typeSchema = "schema";
179         when(a1ClientMock.getPolicyTypeSchema(POLICY_TYPE_1_NAME)).thenReturn(Mono.just(typeSchema));
180
181         RicSynchronizationTask synchronizerUnderTest =
182             new RicSynchronizationTask(a1ClientFactoryMock, policyTypes, policies, services);
183
184         synchronizerUnderTest.run(RIC_1);
185
186         verify(a1ClientMock).getPolicyTypeIdentities();
187         verifyNoMoreInteractions(a1ClientMock);
188
189         assertThat(policyTypes.size()).isEqualTo(1);
190         assertThat(policyTypes.getType(POLICY_TYPE_1_NAME).schema()).isEqualTo(typeSchema);
191         assertThat(policies.size()).isEqualTo(0);
192         assertThat(RIC_1.getState()).isEqualTo(RicState.IDLE);
193     }
194
195     @Test
196     public void ricIdleAndHavePolicies_thenSynchronizationWithRecreationOfPolicies() {
197         RIC_1.setState(RicState.IDLE);
198
199         policies.put(POLICY_1);
200
201         setUpCreationOfA1Client();
202         simulateRicWithNoPolicyTypes();
203
204         when(a1ClientMock.deleteAllPolicies()).thenReturn(Flux.just("OK"));
205         when(a1ClientMock.putPolicy(any(Policy.class))).thenReturn(Mono.just("OK"));
206
207         RicSynchronizationTask synchronizerUnderTest =
208             new RicSynchronizationTask(a1ClientFactoryMock, policyTypes, policies, services);
209
210         synchronizerUnderTest.run(RIC_1);
211
212         verify(a1ClientMock).deleteAllPolicies();
213         verify(a1ClientMock).putPolicy(POLICY_1);
214         verifyNoMoreInteractions(a1ClientMock);
215
216         assertThat(policyTypes.size()).isEqualTo(0);
217         assertThat(policies.size()).isEqualTo(1);
218         assertThat(RIC_1.getState()).isEqualTo(RicState.IDLE);
219     }
220
221     @Test
222     public void ricIdleAndErrorDeletingPoliciesFirstTime_thenSynchronizationWithDeletionOfPolicies() {
223         RIC_1.setState(RicState.IDLE);
224
225         policies.put(POLICY_1);
226
227         setUpCreationOfA1Client();
228         simulateRicWithNoPolicyTypes();
229
230         when(a1ClientMock.deleteAllPolicies()) //
231             .thenReturn(Flux.error(new Exception("Exception"))) //
232             .thenReturn(Flux.just("OK"));
233
234         RicSynchronizationTask synchronizerUnderTest =
235             new RicSynchronizationTask(a1ClientFactoryMock, policyTypes, policies, services);
236
237         synchronizerUnderTest.run(RIC_1);
238
239         verify(a1ClientMock, times(2)).deleteAllPolicies();
240         verifyNoMoreInteractions(a1ClientMock);
241
242         assertThat(policyTypes.size()).isEqualTo(0);
243         assertThat(policies.size()).isEqualTo(0);
244         assertThat(RIC_1.getState()).isEqualTo(RicState.IDLE);
245     }
246
247     @Test
248     public void ricIdleAndErrorDeletingPoliciesAllTheTime_thenSynchronizationWithFailedRecovery() {
249         RIC_1.setState(RicState.IDLE);
250
251         policies.put(POLICY_1);
252
253         setUpCreationOfA1Client();
254         simulateRicWithNoPolicyTypes();
255
256         String originalErrorMessage = "Exception";
257         when(a1ClientMock.deleteAllPolicies()).thenReturn(Flux.error(new Exception(originalErrorMessage)));
258
259         RicSynchronizationTask synchronizerUnderTest =
260             new RicSynchronizationTask(a1ClientFactoryMock, policyTypes, policies, services);
261
262         final ListAppender<ILoggingEvent> logAppender =
263             LoggingUtils.getLogListAppender(RicSynchronizationTask.class, WARN);
264
265         synchronizerUnderTest.run(RIC_1);
266
267         verifyCorrectLogMessage(0, logAppender,
268             "Recreation of policies failed for ric: " + RIC_1_NAME + ", reason: " + originalErrorMessage);
269
270         verify(a1ClientMock, times(2)).deleteAllPolicies();
271         verifyNoMoreInteractions(a1ClientMock);
272
273         assertThat(policyTypes.size()).isEqualTo(0);
274         assertThat(policies.size()).isEqualTo(0);
275         assertThat(RIC_1.getState()).isEqualTo(RicState.UNDEFINED);
276     }
277
278     @Test
279     public void ricIdlePolicyTypeInRepo_thenSynchronizationWithErrorOnServiceNotificationErrorLogged() {
280         RIC_1.setState(RicState.IDLE);
281
282         policyTypes.put(POLICY_TYPE_1);
283
284         services.put(SERVICE_1);
285
286         setUpCreationOfA1Client();
287         simulateRicWithOnePolicyType();
288
289         final ListAppender<ILoggingEvent> logAppender =
290             LoggingUtils.getLogListAppender(RicSynchronizationTask.class, WARN);
291
292         RicSynchronizationTask synchronizerUnderTest =
293             spy(new RicSynchronizationTask(a1ClientFactoryMock, policyTypes, policies, services));
294
295         AsyncRestClient restClientMock = setUpCreationOfAsyncRestClient(synchronizerUnderTest);
296         String originalErrorMessage = "Exception";
297         when(restClientMock.put(anyString(), anyString())).thenReturn(Mono.error(new Exception(originalErrorMessage)));
298
299         synchronizerUnderTest.run(RIC_1);
300
301         ILoggingEvent loggingEvent = logAppender.list.get(0);
302         assertThat(loggingEvent.getLevel()).isEqualTo(WARN);
303         verifyCorrectLogMessage(0, logAppender, "Service notification failed for service: " + SERVICE_1_NAME);
304     }
305
306     private void setUpCreationOfA1Client() {
307         when(a1ClientFactoryMock.createA1Client(any(Ric.class))).thenReturn(Mono.just(a1ClientMock));
308         doReturn(Flux.empty()).when(a1ClientMock).deleteAllPolicies();
309     }
310
311     private AsyncRestClient setUpCreationOfAsyncRestClient(RicSynchronizationTask synchronizerUnderTest) {
312         AsyncRestClient restClientMock = mock(AsyncRestClient.class);
313         doReturn(restClientMock).when(synchronizerUnderTest).createNotificationClient(anyString());
314         return restClientMock;
315     }
316
317     private void simulateRicWithOnePolicyType() {
318         when(a1ClientMock.getPolicyTypeIdentities()).thenReturn(Mono.just(Arrays.asList(POLICY_TYPE_1_NAME)));
319     }
320
321     private void simulateRicWithNoPolicyTypes() {
322         when(a1ClientMock.getPolicyTypeIdentities()).thenReturn(Mono.just(Collections.emptyList()));
323     }
324
325     private void verifyCorrectLogMessage(int messageIndex, ListAppender<ILoggingEvent> logAppender,
326         String expectedMessage) {
327         ILoggingEvent loggingEvent = logAppender.list.get(messageIndex);
328         assertThat(loggingEvent.toString().contains(expectedMessage)).isTrue();
329     }
330 }