2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.junit.Assert.assertEquals;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Matchers.eq;
26 import static org.mockito.Mockito.doThrow;
27 import static org.mockito.Mockito.never;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
32 import java.util.Properties;
33 import java.util.Timer;
34 import java.util.TimerTask;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.mockito.ArgumentCaptor;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.onap.policy.common.im.StateManagement;
43 import org.onap.policy.drools.system.PolicyEngine;
45 public class PmStandbyStateChangeNotifierTest {
46 private static final String UNSUPPORTED_STATUS = "unsupported status";
47 private static final String PDP_ID = "my-pdp";
48 private static final long UPDATE_INTERVAL = 100;
49 private static final long WAIT_INTERVAL = 2 * UPDATE_INTERVAL + 2000;
51 private static Factory saveFactory;
54 private Factory factory;
57 private PolicyEngine engmgr;
63 private StateManagement mgmt;
65 private PmStandbyStateChangeNotifier notifier;
68 * Initializes the properties.
71 public static void setUpBeforeClass() {
72 Properties props = new Properties();
73 props.setProperty(ActiveStandbyProperties.NODE_NAME, PDP_ID);
74 props.setProperty(ActiveStandbyProperties.PDP_UPDATE_INTERVAL, String.valueOf(UPDATE_INTERVAL));
76 ActiveStandbyProperties.initProperties(props);
78 saveFactory = Factory.getInstance();
82 public static void tearDownAfterClass() {
83 Factory.setInstance(saveFactory);
87 * Initializes objects, including the notifier.
91 MockitoAnnotations.initMocks(this);
93 Factory.setInstance(factory);
94 when(factory.makeTimer()).thenReturn(timer);
96 notifier = new MyNotifier();
100 public void testHandleStateChange_Null() {
101 notifier.update(mgmt, null);
102 verify(engmgr).deactivate();
103 assertEquals(StateManagement.NULL_VALUE, notifier.getPreviousStandbyStatus());
105 // repeat - nothing else should be done
106 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.NULL_VALUE);
107 notifier.update(mgmt, null);
108 verify(engmgr, times(1)).deactivate();
109 assertEquals(StateManagement.NULL_VALUE, notifier.getPreviousStandbyStatus());
113 public void testHandleStateChange_Null_Ex() {
114 doThrow(new MyException()).when(engmgr).deactivate();
116 // should not throw an exception
117 notifier.update(mgmt, null);
118 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
122 public void testHandleStateChange_HotOrCold() {
123 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.HOT_STANDBY);
124 notifier.update(mgmt, null);
125 verify(engmgr).deactivate();
126 assertEquals(PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY, notifier.getPreviousStandbyStatus());
128 // repeat - nothing else should be done
129 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.COLD_STANDBY);
130 notifier.update(mgmt, null);
131 verify(engmgr, times(1)).deactivate();
132 assertEquals(PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY, notifier.getPreviousStandbyStatus());
136 public void testHandleStateChange_HotOrCold_Ex() {
137 doThrow(new MyException()).when(engmgr).deactivate();
139 // should not throw an exception
140 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.HOT_STANDBY);
141 notifier.update(mgmt, null);
142 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
146 public void testHandleStateChange_ProvidingService() {
147 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
148 notifier.update(mgmt, null);
149 verify(engmgr, never()).activate();
150 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
152 ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
153 verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));
155 // execute the timer task
156 captor.getValue().run();
158 verify(engmgr).activate();
159 assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus());
161 // repeat - nothing else should be done
162 notifier.update(mgmt, null);
163 verify(engmgr, never()).deactivate();
164 verify(engmgr, times(1)).activate();
165 verify(timer, times(1)).schedule(captor.capture(), eq(WAIT_INTERVAL));
166 assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus());
170 public void testHandleStateChange_ProvidingService_BeforeActivation() {
171 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
172 notifier.update(mgmt, null);
174 // repeat - nothing else should be done
175 notifier.update(mgmt, null);
176 verify(engmgr, never()).deactivate();
177 verify(engmgr, never()).activate();
179 verify(timer, times(1)).schedule(any(), eq(WAIT_INTERVAL));
180 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
184 public void testHandleStateChange_ProvidingService_Ex() {
185 when(factory.makeTimer()).thenThrow(new MyException());
187 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
188 notifier.update(mgmt, null);
190 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
194 public void testHandleStateChange_Unsupported() {
195 when(mgmt.getStandbyStatus()).thenReturn(UNSUPPORTED_STATUS);
196 notifier.update(mgmt, null);
198 verify(engmgr).deactivate();
199 assertEquals(PmStandbyStateChangeNotifier.UNSUPPORTED, notifier.getPreviousStandbyStatus());
201 // repeat - nothing else should be done
202 notifier.update(mgmt, null);
203 verify(engmgr, times(1)).deactivate();
204 assertEquals(PmStandbyStateChangeNotifier.UNSUPPORTED, notifier.getPreviousStandbyStatus());
208 public void testHandleStateChange_Unsupported_Ex() {
209 doThrow(new MyException()).when(engmgr).deactivate();
211 // should not throw an exception
212 when(mgmt.getStandbyStatus()).thenReturn(UNSUPPORTED_STATUS);
213 notifier.update(mgmt, null);
214 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
218 public void testCancelTimer() {
219 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
220 notifier.update(mgmt, null);
222 when(mgmt.getStandbyStatus()).thenReturn(null);
223 notifier.update(mgmt, null);
225 verify(timer).cancel();
229 public void testDelayActivateClass() {
230 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
231 notifier.update(mgmt, null);
232 verify(engmgr, never()).activate();
233 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
235 ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
236 verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));
238 // execute the timer task
239 captor.getValue().run();
241 verify(engmgr).activate();
242 assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus());
246 public void testDelayActivateClass_Ex() {
247 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
248 notifier.update(mgmt, null);
249 verify(engmgr, never()).activate();
250 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
252 ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
253 verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));
255 doThrow(new MyException()).when(engmgr).activate();
257 // execute the timer task
258 captor.getValue().run();
260 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
264 public void testGetPolicyEngineManager() {
265 // use real object with real method - no exception expected
266 new PmStandbyStateChangeNotifier().getPolicyEngineManager();
269 private class MyNotifier extends PmStandbyStateChangeNotifier {
271 protected PolicyEngine getPolicyEngineManager() {
276 private static class MyException extends RuntimeException {
277 private static final long serialVersionUID = 1L;
279 public MyException() {
280 super("expected exception");