2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T 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.policy.drools.activestandby;
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.junit.Assert.assertEquals;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.doThrow;
28 import static org.mockito.Mockito.never;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
33 import java.util.Properties;
34 import java.util.Timer;
35 import java.util.TimerTask;
36 import org.junit.AfterClass;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.ArgumentCaptor;
42 import org.mockito.Mock;
43 import org.mockito.junit.MockitoJUnitRunner;
44 import org.onap.policy.common.im.StateManagement;
45 import org.onap.policy.drools.system.PolicyEngine;
47 @RunWith(MockitoJUnitRunner.class)
48 public class PmStandbyStateChangeNotifierTest {
49 private static final String UNSUPPORTED_STATUS = "unsupported status";
50 private static final String PDP_ID = "my-pdp";
51 private static final long UPDATE_INTERVAL = 100;
52 private static final long WAIT_INTERVAL = 2 * UPDATE_INTERVAL + 2000;
54 private static Factory saveFactory;
57 private Factory factory;
60 private PolicyEngine engmgr;
66 private StateManagement mgmt;
68 private PmStandbyStateChangeNotifier notifier;
71 * Initializes the properties.
74 public static void setUpBeforeClass() {
75 Properties props = new Properties();
76 props.setProperty(ActiveStandbyProperties.NODE_NAME, PDP_ID);
77 props.setProperty(ActiveStandbyProperties.PDP_UPDATE_INTERVAL, String.valueOf(UPDATE_INTERVAL));
79 ActiveStandbyProperties.initProperties(props);
81 saveFactory = Factory.getInstance();
85 public static void tearDownAfterClass() {
86 Factory.setInstance(saveFactory);
90 * Initializes objects, including the notifier.
94 Factory.setInstance(factory);
95 when(factory.makeTimer()).thenReturn(timer);
97 notifier = new MyNotifier();
101 public void testHandleStateChange_Null() {
102 notifier.update(mgmt, null);
103 verify(engmgr).deactivate();
104 assertEquals(StateManagement.NULL_VALUE, notifier.getPreviousStandbyStatus());
106 // repeat - nothing else should be done
107 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.NULL_VALUE);
108 notifier.update(mgmt, null);
109 verify(engmgr, times(1)).deactivate();
110 assertEquals(StateManagement.NULL_VALUE, notifier.getPreviousStandbyStatus());
114 public void testHandleStateChange_Null_Ex() {
115 doThrow(new MyException()).when(engmgr).deactivate();
117 // should not throw an exception
118 notifier.update(mgmt, null);
119 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
123 public void testHandleStateChange_HotOrCold() {
124 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.HOT_STANDBY);
125 notifier.update(mgmt, null);
126 verify(engmgr).deactivate();
127 assertEquals(PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY, notifier.getPreviousStandbyStatus());
129 // repeat - nothing else should be done
130 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.COLD_STANDBY);
131 notifier.update(mgmt, null);
132 verify(engmgr, times(1)).deactivate();
133 assertEquals(PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY, notifier.getPreviousStandbyStatus());
137 public void testHandleStateChange_HotOrCold_Ex() {
138 doThrow(new MyException()).when(engmgr).deactivate();
140 // should not throw an exception
141 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.HOT_STANDBY);
142 notifier.update(mgmt, null);
143 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
147 public void testHandleStateChange_ProvidingService() {
148 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
149 notifier.update(mgmt, null);
150 verify(engmgr, never()).activate();
151 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
153 ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
154 verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));
156 // execute the timer task
157 captor.getValue().run();
159 verify(engmgr).activate();
160 assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus());
162 // repeat - nothing else should be done
163 notifier.update(mgmt, null);
164 verify(engmgr, never()).deactivate();
165 verify(engmgr, times(1)).activate();
166 verify(timer, times(1)).schedule(captor.capture(), eq(WAIT_INTERVAL));
167 assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus());
171 public void testHandleStateChange_ProvidingService_BeforeActivation() {
172 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
173 notifier.update(mgmt, null);
175 // repeat - nothing else should be done
176 notifier.update(mgmt, null);
177 verify(engmgr, never()).deactivate();
178 verify(engmgr, never()).activate();
180 verify(timer, times(1)).schedule(any(), eq(WAIT_INTERVAL));
181 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
185 public void testHandleStateChange_ProvidingService_Ex() {
186 when(factory.makeTimer()).thenThrow(new MyException());
188 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
189 notifier.update(mgmt, null);
191 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
195 public void testHandleStateChange_Unsupported() {
196 when(mgmt.getStandbyStatus()).thenReturn(UNSUPPORTED_STATUS);
197 notifier.update(mgmt, null);
199 verify(engmgr).deactivate();
200 assertEquals(PmStandbyStateChangeNotifier.UNSUPPORTED, notifier.getPreviousStandbyStatus());
202 // repeat - nothing else should be done
203 notifier.update(mgmt, null);
204 verify(engmgr, times(1)).deactivate();
205 assertEquals(PmStandbyStateChangeNotifier.UNSUPPORTED, notifier.getPreviousStandbyStatus());
209 public void testHandleStateChange_Unsupported_Ex() {
210 doThrow(new MyException()).when(engmgr).deactivate();
212 // should not throw an exception
213 when(mgmt.getStandbyStatus()).thenReturn(UNSUPPORTED_STATUS);
214 notifier.update(mgmt, null);
215 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
219 public void testCancelTimer() {
220 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
221 notifier.update(mgmt, null);
223 when(mgmt.getStandbyStatus()).thenReturn(null);
224 notifier.update(mgmt, null);
226 verify(timer).cancel();
230 public void testDelayActivateClass() {
231 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
232 notifier.update(mgmt, null);
233 verify(engmgr, never()).activate();
234 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
236 ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
237 verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));
239 // execute the timer task
240 captor.getValue().run();
242 verify(engmgr).activate();
243 assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus());
247 public void testDelayActivateClass_Ex() {
248 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
249 notifier.update(mgmt, null);
250 verify(engmgr, never()).activate();
251 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
253 ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
254 verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));
256 doThrow(new MyException()).when(engmgr).activate();
258 // execute the timer task
259 captor.getValue().run();
261 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
265 public void testGetPolicyEngineManager() {
266 // use real object with real method - no exception expected
267 assertThatCode(() -> new PmStandbyStateChangeNotifier().getPolicyEngineManager()).doesNotThrowAnyException();
270 private class MyNotifier extends PmStandbyStateChangeNotifier {
272 protected PolicyEngine getPolicyEngineManager() {
277 private static class MyException extends RuntimeException {
278 private static final long serialVersionUID = 1L;
280 public MyException() {
281 super("expected exception");