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.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.mockito.ArgumentCaptor;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.onap.policy.common.im.StateManagement;
42 import org.onap.policy.drools.system.PolicyEngine;
44 public class PmStandbyStateChangeNotifierTest {
45 private static final String UNSUPPORTED_STATUS = "unsupported status";
46 private static final String PDP_ID = "my-pdp";
47 private static final long UPDATE_INTERVAL = 100;
48 private static final long WAIT_INTERVAL = 2 * UPDATE_INTERVAL + 2000;
51 private PolicyEngine engmgr;
57 private StateManagement mgmt;
59 private PmStandbyStateChangeNotifier notifier;
62 * Initializes the properties.
65 public static void setUpBeforeClass() {
66 Properties props = new Properties();
67 props.setProperty(ActiveStandbyProperties.NODE_NAME, PDP_ID);
68 props.setProperty(ActiveStandbyProperties.PDP_UPDATE_INTERVAL, String.valueOf(UPDATE_INTERVAL));
70 ActiveStandbyProperties.initProperties(props);
74 * Initializes objects, including the notifier.
79 MockitoAnnotations.initMocks(this);
81 notifier = new MyNotifier();
85 public void testHandleStateChange_Null() {
86 notifier.update(mgmt, null);
87 verify(engmgr).deactivate();
88 assertEquals(StateManagement.NULL_VALUE, notifier.getPreviousStandbyStatus());
90 // repeat - nothing else should be done
91 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.NULL_VALUE);
92 notifier.update(mgmt, null);
93 verify(engmgr, times(1)).deactivate();
94 assertEquals(StateManagement.NULL_VALUE, notifier.getPreviousStandbyStatus());
98 public void testHandleStateChange_Null_Ex() {
99 doThrow(new MyException()).when(engmgr).deactivate();
101 // should not throw an exception
102 notifier.update(mgmt, null);
103 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
107 public void testHandleStateChange_HotOrCold() {
108 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.HOT_STANDBY);
109 notifier.update(mgmt, null);
110 verify(engmgr).deactivate();
111 assertEquals(PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY, notifier.getPreviousStandbyStatus());
113 // repeat - nothing else should be done
114 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.COLD_STANDBY);
115 notifier.update(mgmt, null);
116 verify(engmgr, times(1)).deactivate();
117 assertEquals(PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY, notifier.getPreviousStandbyStatus());
121 public void testHandleStateChange_HotOrCold_Ex() {
122 doThrow(new MyException()).when(engmgr).deactivate();
124 // should not throw an exception
125 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.HOT_STANDBY);
126 notifier.update(mgmt, null);
127 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
131 public void testHandleStateChange_ProvidingService() {
132 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
133 notifier.update(mgmt, null);
134 verify(engmgr, never()).activate();
135 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
137 ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
138 verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));
140 // execute the timer task
141 captor.getValue().run();
143 verify(engmgr).activate();
144 assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus());
146 // repeat - nothing else should be done
147 notifier.update(mgmt, null);
148 verify(engmgr, never()).deactivate();
149 verify(engmgr, times(1)).activate();
150 verify(timer, times(1)).schedule(captor.capture(), eq(WAIT_INTERVAL));
151 assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus());
155 public void testHandleStateChange_ProvidingService_BeforeActivation() {
156 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
157 notifier.update(mgmt, null);
159 // repeat - nothing else should be done
160 notifier.update(mgmt, null);
161 verify(engmgr, never()).deactivate();
162 verify(engmgr, never()).activate();
164 verify(timer, times(1)).schedule(any(), eq(WAIT_INTERVAL));
165 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
169 public void testHandleStateChange_ProvidingService_Ex() {
170 notifier = new MyNotifier() {
172 protected Timer makeTimer() {
173 throw new MyException();
177 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
178 notifier.update(mgmt, null);
180 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
184 public void testHandleStateChange_Unsupported() {
185 when(mgmt.getStandbyStatus()).thenReturn(UNSUPPORTED_STATUS);
186 notifier.update(mgmt, null);
188 verify(engmgr).deactivate();
189 assertEquals(PmStandbyStateChangeNotifier.UNSUPPORTED, notifier.getPreviousStandbyStatus());
191 // repeat - nothing else should be done
192 notifier.update(mgmt, null);
193 verify(engmgr, times(1)).deactivate();
194 assertEquals(PmStandbyStateChangeNotifier.UNSUPPORTED, notifier.getPreviousStandbyStatus());
198 public void testHandleStateChange_Unsupported_Ex() {
199 doThrow(new MyException()).when(engmgr).deactivate();
201 // should not throw an exception
202 when(mgmt.getStandbyStatus()).thenReturn(UNSUPPORTED_STATUS);
203 notifier.update(mgmt, null);
204 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
208 public void testCancelTimer() {
209 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
210 notifier.update(mgmt, null);
212 when(mgmt.getStandbyStatus()).thenReturn(null);
213 notifier.update(mgmt, null);
215 verify(timer).cancel();
219 public void testDelayActivateClass() {
220 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
221 notifier.update(mgmt, null);
222 verify(engmgr, never()).activate();
223 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
225 ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
226 verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));
228 // execute the timer task
229 captor.getValue().run();
231 verify(engmgr).activate();
232 assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus());
236 public void testDelayActivateClass_Ex() {
237 when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
238 notifier.update(mgmt, null);
239 verify(engmgr, never()).activate();
240 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
242 ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
243 verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));
245 doThrow(new MyException()).when(engmgr).activate();
247 // execute the timer task
248 captor.getValue().run();
250 assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
254 public void testGetPolicyEngineManager() {
255 // use real object with real method - no exception expected
256 new PmStandbyStateChangeNotifier().getPolicyEngineManager();
260 public void testMakeTimer() {
261 // use real object with real method
262 new PmStandbyStateChangeNotifier().makeTimer().cancel();
265 private class MyNotifier extends PmStandbyStateChangeNotifier {
267 protected PolicyEngine getPolicyEngineManager() {
272 protected Timer makeTimer() {
277 private static class MyException extends RuntimeException {
278 private static final long serialVersionUID = 1L;
280 public MyException() {
281 super("expected exception");