2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
4 * ================================================================================
5 * Copyright (C) 2019 NOKIA Intellectual Property. 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
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.onap.dcaegen2.services.prh.configuration;
23 import org.junit.jupiter.api.AfterEach;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.ExtendWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.jupiter.MockitoExtension;
29 import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
30 import org.springframework.cloud.context.refresh.ContextRefresher;
31 import org.springframework.core.env.Environment;
32 import reactor.test.scheduler.VirtualTimeScheduler;
34 import java.time.Duration;
35 import java.util.Collections;
37 import static org.mockito.Mockito.*;
40 @ExtendWith(MockitoExtension.class)
41 class CbsConfigRefreshSchedulerTest {
43 private static final Duration SOME_UPDATES_INTERVAL = Duration.ofMinutes(5);
44 private static final String CBS_UPDATES_INTERVAL_PROPERTY = "cbs.updates-interval";
47 private ContextRefresher contextRefresher;
49 private Environment environment;
51 private VirtualTimeScheduler virtualTimeScheduler;
53 private CbsConfigRefreshScheduler cbsConfigRefreshScheduler;
58 virtualTimeScheduler = VirtualTimeScheduler.getOrSet();
59 when(environment.getProperty(CBS_UPDATES_INTERVAL_PROPERTY, Duration.class, Duration.ZERO))
60 .thenReturn(SOME_UPDATES_INTERVAL);
62 cbsConfigRefreshScheduler = new CbsConfigRefreshScheduler(contextRefresher, environment);
67 virtualTimeScheduler.dispose();
71 void configRefreshUpdatesShouldBeFiredAccordingToConfiguredInterval() {
72 cbsConfigRefreshScheduler.startPollingForCbsUpdates();
74 verify(contextRefresher, times(0)).refresh();
76 virtualTimeScheduler.advanceTimeBy(SOME_UPDATES_INTERVAL);
77 verify(contextRefresher, times(1)).refresh();
79 virtualTimeScheduler.advanceTimeBy(SOME_UPDATES_INTERVAL);
80 verify(contextRefresher, times(2)).refresh();
84 void whenConfigUpdateIntervalIsSetToZero_UpdatesShouldNotBeExecuted() {
85 when(environment.getProperty(CBS_UPDATES_INTERVAL_PROPERTY, Duration.class, Duration.ZERO))
86 .thenReturn(Duration.ZERO);
88 cbsConfigRefreshScheduler.startPollingForCbsUpdates();
90 virtualTimeScheduler.advanceTimeBy(Duration.ofHours(10));
92 verifyZeroInteractions(contextRefresher);
96 void whenUpdateFails_shouldContinueWithUpdateRequestsAccordingToConfiguredSchedule() {
97 when(contextRefresher.refresh())
98 .thenThrow(new RuntimeException("kaboom!"))
99 .thenReturn(Collections.emptySet());
101 cbsConfigRefreshScheduler.startPollingForCbsUpdates();
103 virtualTimeScheduler.advanceTimeBy(SOME_UPDATES_INTERVAL.plus(SOME_UPDATES_INTERVAL));
104 verify(contextRefresher, times(2)).refresh();
109 void whenUpdatesIntervalIsChangedInEnvironment_UpdatesShouldBeRescheduled() {
110 when(environment.getProperty(CBS_UPDATES_INTERVAL_PROPERTY, Duration.class, Duration.ZERO))
111 .thenReturn(Duration.ofMinutes(30))
112 .thenReturn(Duration.ofSeconds(10));
114 cbsConfigRefreshScheduler.startPollingForCbsUpdates();
116 cbsConfigRefreshScheduler.onEnvironmentChanged(
117 new EnvironmentChangeEvent(Collections.singleton(CBS_UPDATES_INTERVAL_PROPERTY)));
119 virtualTimeScheduler.advanceTimeBy(Duration.ofMinutes(1));
121 verify(contextRefresher, times(6)).refresh();
126 void whenEnvironmentChangeDoesNotAffectUpdatesInterval_UpdatesScheduleShouldNotBeImpacted() {
127 cbsConfigRefreshScheduler.startPollingForCbsUpdates();
129 Duration envChangeDelay = Duration.ofMinutes(1);
130 virtualTimeScheduler.advanceTimeBy(envChangeDelay);
132 cbsConfigRefreshScheduler.onEnvironmentChanged(new EnvironmentChangeEvent(Collections.emptySet()));
134 virtualTimeScheduler.advanceTimeBy(SOME_UPDATES_INTERVAL.minus(envChangeDelay));
136 verify(contextRefresher).refresh();