2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
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.oransc.policyagent.tasks;
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;
36 import ch.qos.logback.classic.spi.ILoggingEvent;
37 import ch.qos.logback.core.read.ListAppender;
39 import java.time.Duration;
40 import java.util.Arrays;
41 import java.util.Collections;
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;
64 import reactor.core.publisher.Flux;
65 import reactor.core.publisher.Mono;
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) //
75 private static final String RIC_1_NAME = "ric1";
76 private static final Ric RIC_1 = new Ric(ImmutableRicConfig.builder() //
78 .baseUrl("baseUrl1") //
79 .managedElementIds(Collections.emptyList()) //
80 .controllerName("controllerName") //
83 private static final Policy POLICY_1 = ImmutablePolicy.builder() //
86 .ownerServiceName("service") //
88 .type(POLICY_TYPE_1) //
89 .lastModified("now") //
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);
97 private A1Client a1ClientMock;
100 private A1ClientFactory a1ClientFactoryMock;
102 private PolicyTypes policyTypes;
103 private Policies policies;
104 private Services services;
108 policyTypes = new PolicyTypes();
109 policies = new Policies();
110 services = new Services();
111 RIC_1.setState(RicState.UNDEFINED);
112 RIC_1.clearSupportedPolicyTypes();
116 public void ricAlreadySynchronizing_thenNoSynchronization() {
117 RIC_1.setState(RicState.SYNCHRONIZING);
118 RIC_1.addSupportedPolicyType(POLICY_TYPE_1);
120 policyTypes.put(POLICY_TYPE_1);
121 policies.put(POLICY_1);
123 RicSynchronizationTask synchronizerUnderTest =
124 new RicSynchronizationTask(a1ClientFactoryMock, policyTypes, policies, services);
126 synchronizerUnderTest.run(RIC_1);
128 verifyNoInteractions(a1ClientMock);
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);
137 public void ricIdlePolicyTypeInRepo_thenSynchronizationWithReuseOfTypeFromRepoAndCorrectServiceNotified() {
138 RIC_1.setState(RicState.IDLE);
140 policyTypes.put(POLICY_TYPE_1);
142 services.put(SERVICE_1);
143 Service serviceWithoutCallbackUrlShouldNotBeNotified = new Service("service2", Duration.ofSeconds(1), "");
144 services.put(serviceWithoutCallbackUrlShouldNotBeNotified);
146 setUpCreationOfA1Client();
147 simulateRicWithOnePolicyType();
149 RicSynchronizationTask synchronizerUnderTest =
150 spy(new RicSynchronizationTask(a1ClientFactoryMock, policyTypes, policies, services));
152 AsyncRestClient restClientMock = setUpCreationOfAsyncRestClient(synchronizerUnderTest);
153 when(restClientMock.put(anyString(), anyString())).thenReturn(Mono.just("Ok"));
155 synchronizerUnderTest.run(RIC_1);
157 verify(a1ClientMock, times(1)).getPolicyTypeIdentities();
158 verifyNoMoreInteractions(a1ClientMock);
160 verify(synchronizerUnderTest).run(RIC_1);
161 verify(synchronizerUnderTest).createNotificationClient(SERVICE_1_CALLBACK_URL);
162 verifyNoMoreInteractions(synchronizerUnderTest);
164 verify(restClientMock).put("", "Synchronization completed for:" + RIC_1_NAME);
165 verifyNoMoreInteractions(restClientMock);
167 assertThat(policyTypes.size()).isEqualTo(1);
168 assertThat(policies.size()).isEqualTo(0);
169 assertThat(RIC_1.getState()).isEqualTo(RicState.IDLE);
173 public void ricIdlePolicyTypeNotInRepo_thenSynchronizationWithTypeFromRic() throws Exception {
174 RIC_1.setState(RicState.IDLE);
176 setUpCreationOfA1Client();
177 simulateRicWithOnePolicyType();
178 String typeSchema = "schema";
179 when(a1ClientMock.getPolicyTypeSchema(POLICY_TYPE_1_NAME)).thenReturn(Mono.just(typeSchema));
181 RicSynchronizationTask synchronizerUnderTest =
182 new RicSynchronizationTask(a1ClientFactoryMock, policyTypes, policies, services);
184 synchronizerUnderTest.run(RIC_1);
186 verify(a1ClientMock).getPolicyTypeIdentities();
187 verifyNoMoreInteractions(a1ClientMock);
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);
196 public void ricIdleAndHavePolicies_thenSynchronizationWithRecreationOfPolicies() {
197 RIC_1.setState(RicState.IDLE);
199 policies.put(POLICY_1);
201 setUpCreationOfA1Client();
202 simulateRicWithNoPolicyTypes();
204 when(a1ClientMock.deleteAllPolicies()).thenReturn(Flux.just("OK"));
205 when(a1ClientMock.putPolicy(any(Policy.class))).thenReturn(Mono.just("OK"));
207 RicSynchronizationTask synchronizerUnderTest =
208 new RicSynchronizationTask(a1ClientFactoryMock, policyTypes, policies, services);
210 synchronizerUnderTest.run(RIC_1);
212 verify(a1ClientMock).deleteAllPolicies();
213 verify(a1ClientMock).putPolicy(POLICY_1);
214 verifyNoMoreInteractions(a1ClientMock);
216 assertThat(policyTypes.size()).isEqualTo(0);
217 assertThat(policies.size()).isEqualTo(1);
218 assertThat(RIC_1.getState()).isEqualTo(RicState.IDLE);
222 public void ricIdleAndErrorDeletingPoliciesFirstTime_thenSynchronizationWithDeletionOfPolicies() {
223 RIC_1.setState(RicState.IDLE);
225 policies.put(POLICY_1);
227 setUpCreationOfA1Client();
228 simulateRicWithNoPolicyTypes();
230 when(a1ClientMock.deleteAllPolicies()) //
231 .thenReturn(Flux.error(new Exception("Exception"))) //
232 .thenReturn(Flux.just("OK"));
234 RicSynchronizationTask synchronizerUnderTest =
235 new RicSynchronizationTask(a1ClientFactoryMock, policyTypes, policies, services);
237 synchronizerUnderTest.run(RIC_1);
239 verify(a1ClientMock, times(2)).deleteAllPolicies();
240 verifyNoMoreInteractions(a1ClientMock);
242 assertThat(policyTypes.size()).isEqualTo(0);
243 assertThat(policies.size()).isEqualTo(0);
244 assertThat(RIC_1.getState()).isEqualTo(RicState.IDLE);
248 public void ricIdleAndErrorDeletingPoliciesAllTheTime_thenSynchronizationWithFailedRecovery() {
249 RIC_1.setState(RicState.IDLE);
251 policies.put(POLICY_1);
253 setUpCreationOfA1Client();
254 simulateRicWithNoPolicyTypes();
256 String originalErrorMessage = "Exception";
257 when(a1ClientMock.deleteAllPolicies()).thenReturn(Flux.error(new Exception(originalErrorMessage)));
259 RicSynchronizationTask synchronizerUnderTest =
260 new RicSynchronizationTask(a1ClientFactoryMock, policyTypes, policies, services);
262 final ListAppender<ILoggingEvent> logAppender =
263 LoggingUtils.getLogListAppender(RicSynchronizationTask.class, WARN);
265 synchronizerUnderTest.run(RIC_1);
267 verifyCorrectLogMessage(0, logAppender,
268 "Recreation of policies failed for ric: " + RIC_1_NAME + ", reason: " + originalErrorMessage);
270 verify(a1ClientMock, times(2)).deleteAllPolicies();
271 verifyNoMoreInteractions(a1ClientMock);
273 assertThat(policyTypes.size()).isEqualTo(0);
274 assertThat(policies.size()).isEqualTo(0);
275 assertThat(RIC_1.getState()).isEqualTo(RicState.UNDEFINED);
279 public void ricIdlePolicyTypeInRepo_thenSynchronizationWithErrorOnServiceNotificationErrorLogged() {
280 RIC_1.setState(RicState.IDLE);
282 policyTypes.put(POLICY_TYPE_1);
284 services.put(SERVICE_1);
286 setUpCreationOfA1Client();
287 simulateRicWithOnePolicyType();
289 final ListAppender<ILoggingEvent> logAppender =
290 LoggingUtils.getLogListAppender(RicSynchronizationTask.class, WARN);
292 RicSynchronizationTask synchronizerUnderTest =
293 spy(new RicSynchronizationTask(a1ClientFactoryMock, policyTypes, policies, services));
295 AsyncRestClient restClientMock = setUpCreationOfAsyncRestClient(synchronizerUnderTest);
296 String originalErrorMessage = "Exception";
297 when(restClientMock.put(anyString(), anyString())).thenReturn(Mono.error(new Exception(originalErrorMessage)));
299 synchronizerUnderTest.run(RIC_1);
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);
306 private void setUpCreationOfA1Client() {
307 when(a1ClientFactoryMock.createA1Client(any(Ric.class))).thenReturn(Mono.just(a1ClientMock));
308 doReturn(Flux.empty()).when(a1ClientMock).deleteAllPolicies();
311 private AsyncRestClient setUpCreationOfAsyncRestClient(RicSynchronizationTask synchronizerUnderTest) {
312 AsyncRestClient restClientMock = mock(AsyncRestClient.class);
313 doReturn(restClientMock).when(synchronizerUnderTest).createNotificationClient(anyString());
314 return restClientMock;
317 private void simulateRicWithOnePolicyType() {
318 when(a1ClientMock.getPolicyTypeIdentities()).thenReturn(Mono.just(Arrays.asList(POLICY_TYPE_1_NAME)));
321 private void simulateRicWithNoPolicyTypes() {
322 when(a1ClientMock.getPolicyTypeIdentities()).thenReturn(Mono.just(Collections.emptyList()));
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();