7ea08aea444036eb871b6f89824ca04127f65fc9
[dcaegen2/services/prh.git] /
1 /*
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
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.onap.dcaegen2.services.prh.configuration;
22
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;
33
34 import java.time.Duration;
35 import java.util.Collections;
36
37 import static org.mockito.Mockito.*;
38
39
40 @ExtendWith(MockitoExtension.class)
41 class CbsConfigRefreshSchedulerTest {
42
43     private static final Duration SOME_UPDATES_INTERVAL = Duration.ofMinutes(5);
44     private static final String CBS_UPDATES_INTERVAL_PROPERTY = "cbs.updates-interval";
45
46     @Mock
47     private ContextRefresher contextRefresher;
48     @Mock
49     private Environment environment;
50
51     private VirtualTimeScheduler virtualTimeScheduler;
52
53     private CbsConfigRefreshScheduler cbsConfigRefreshScheduler;
54
55
56     @BeforeEach
57     void setUp() {
58         virtualTimeScheduler = VirtualTimeScheduler.getOrSet();
59         when(environment.getProperty(CBS_UPDATES_INTERVAL_PROPERTY, Duration.class, Duration.ZERO))
60                 .thenReturn(SOME_UPDATES_INTERVAL);
61
62         cbsConfigRefreshScheduler = new CbsConfigRefreshScheduler(contextRefresher, environment);
63     }
64
65     @AfterEach
66     void tearDown() {
67         virtualTimeScheduler.dispose();
68     }
69
70     @Test
71     void configRefreshUpdatesShouldBeFiredAccordingToConfiguredInterval() {
72         cbsConfigRefreshScheduler.startPollingForCbsUpdates();
73
74         verify(contextRefresher, times(0)).refresh();
75
76         virtualTimeScheduler.advanceTimeBy(SOME_UPDATES_INTERVAL);
77         verify(contextRefresher, times(1)).refresh();
78
79         virtualTimeScheduler.advanceTimeBy(SOME_UPDATES_INTERVAL);
80         verify(contextRefresher, times(2)).refresh();
81     }
82
83     @Test
84     void whenConfigUpdateIntervalIsSetToZero_UpdatesShouldNotBeExecuted() {
85         when(environment.getProperty(CBS_UPDATES_INTERVAL_PROPERTY, Duration.class, Duration.ZERO))
86                 .thenReturn(Duration.ZERO);
87
88         cbsConfigRefreshScheduler.startPollingForCbsUpdates();
89
90         virtualTimeScheduler.advanceTimeBy(Duration.ofHours(10));
91
92         verifyZeroInteractions(contextRefresher);
93     }
94
95     @Test
96     void whenUpdateFails_shouldContinueWithUpdateRequestsAccordingToConfiguredSchedule() {
97         when(contextRefresher.refresh())
98                 .thenThrow(new RuntimeException("kaboom!"))
99                 .thenReturn(Collections.emptySet());
100
101         cbsConfigRefreshScheduler.startPollingForCbsUpdates();
102
103         virtualTimeScheduler.advanceTimeBy(SOME_UPDATES_INTERVAL.plus(SOME_UPDATES_INTERVAL));
104         verify(contextRefresher, times(2)).refresh();
105     }
106
107
108     @Test
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));
113
114         cbsConfigRefreshScheduler.startPollingForCbsUpdates();
115
116         cbsConfigRefreshScheduler.onEnvironmentChanged(
117                 new EnvironmentChangeEvent(Collections.singleton(CBS_UPDATES_INTERVAL_PROPERTY)));
118
119         virtualTimeScheduler.advanceTimeBy(Duration.ofMinutes(1));
120
121         verify(contextRefresher, times(6)).refresh();
122     }
123
124
125     @Test
126     void whenEnvironmentChangeDoesNotAffectUpdatesInterval_UpdatesScheduleShouldNotBeImpacted() {
127         cbsConfigRefreshScheduler.startPollingForCbsUpdates();
128
129         Duration envChangeDelay = Duration.ofMinutes(1);
130         virtualTimeScheduler.advanceTimeBy(envChangeDelay);
131
132         cbsConfigRefreshScheduler.onEnvironmentChanged(new EnvironmentChangeEvent(Collections.emptySet()));
133
134         virtualTimeScheduler.advanceTimeBy(SOME_UPDATES_INTERVAL.minus(envChangeDelay));
135
136         verify(contextRefresher).refresh();
137     }
138 }