4a89d257f5c72f85899f141f46e761a374780a3c
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.policy.drools.activestandby;
22
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;
31
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;
44
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;
50
51     private static Factory saveFactory;
52
53     @Mock
54     private Factory factory;
55
56     @Mock
57     private PolicyEngine engmgr;
58
59     @Mock
60     private Timer timer;
61
62     @Mock
63     private StateManagement mgmt;
64
65     private PmStandbyStateChangeNotifier notifier;
66
67     /**
68      * Initializes the properties.
69      */
70     @BeforeClass
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));
75
76         ActiveStandbyProperties.initProperties(props);
77
78         saveFactory = Factory.getInstance();
79     }
80
81     @AfterClass
82     public static void tearDownAfterClass() {
83         Factory.setInstance(saveFactory);
84     }
85
86     /**
87      * Initializes objects, including the notifier.
88      */
89     @Before
90     public void setUp() {
91         MockitoAnnotations.initMocks(this);
92
93         Factory.setInstance(factory);
94         when(factory.makeTimer()).thenReturn(timer);
95
96         notifier = new MyNotifier();
97     }
98
99     @Test
100     public void testHandleStateChange_Null() {
101         notifier.update(mgmt, null);
102         verify(engmgr).deactivate();
103         assertEquals(StateManagement.NULL_VALUE, notifier.getPreviousStandbyStatus());
104
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());
110     }
111
112     @Test
113     public void testHandleStateChange_Null_Ex() {
114         doThrow(new MyException()).when(engmgr).deactivate();
115
116         // should not throw an exception
117         notifier.update(mgmt, null);
118         assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
119     }
120
121     @Test
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());
127
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());
133     }
134
135     @Test
136     public void testHandleStateChange_HotOrCold_Ex() {
137         doThrow(new MyException()).when(engmgr).deactivate();
138
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());
143     }
144
145     @Test
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());
151
152         ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
153         verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));
154
155         // execute the timer task
156         captor.getValue().run();
157
158         verify(engmgr).activate();
159         assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus());
160
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());
167     }
168
169     @Test
170     public void testHandleStateChange_ProvidingService_BeforeActivation() {
171         when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
172         notifier.update(mgmt, null);
173
174         // repeat - nothing else should be done
175         notifier.update(mgmt, null);
176         verify(engmgr, never()).deactivate();
177         verify(engmgr, never()).activate();
178
179         verify(timer, times(1)).schedule(any(), eq(WAIT_INTERVAL));
180         assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
181     }
182
183     @Test
184     public void testHandleStateChange_ProvidingService_Ex() {
185         when(factory.makeTimer()).thenThrow(new MyException());
186
187         when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
188         notifier.update(mgmt, null);
189
190         assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
191     }
192
193     @Test
194     public void testHandleStateChange_Unsupported() {
195         when(mgmt.getStandbyStatus()).thenReturn(UNSUPPORTED_STATUS);
196         notifier.update(mgmt, null);
197
198         verify(engmgr).deactivate();
199         assertEquals(PmStandbyStateChangeNotifier.UNSUPPORTED, notifier.getPreviousStandbyStatus());
200
201         // repeat - nothing else should be done
202         notifier.update(mgmt, null);
203         verify(engmgr, times(1)).deactivate();
204         assertEquals(PmStandbyStateChangeNotifier.UNSUPPORTED, notifier.getPreviousStandbyStatus());
205     }
206
207     @Test
208     public void testHandleStateChange_Unsupported_Ex() {
209         doThrow(new MyException()).when(engmgr).deactivate();
210
211         // should not throw an exception
212         when(mgmt.getStandbyStatus()).thenReturn(UNSUPPORTED_STATUS);
213         notifier.update(mgmt, null);
214         assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
215     }
216
217     @Test
218     public void testCancelTimer() {
219         when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
220         notifier.update(mgmt, null);
221
222         when(mgmt.getStandbyStatus()).thenReturn(null);
223         notifier.update(mgmt, null);
224
225         verify(timer).cancel();
226     }
227
228     @Test
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());
234
235         ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
236         verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));
237
238         // execute the timer task
239         captor.getValue().run();
240
241         verify(engmgr).activate();
242         assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus());
243     }
244
245     @Test
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());
251
252         ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
253         verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));
254
255         doThrow(new MyException()).when(engmgr).activate();
256
257         // execute the timer task
258         captor.getValue().run();
259
260         assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
261     }
262
263     @Test
264     public void testGetPolicyEngineManager() {
265         // use real object with real method - no exception expected
266         new PmStandbyStateChangeNotifier().getPolicyEngineManager();
267     }
268
269     private class MyNotifier extends PmStandbyStateChangeNotifier {
270         @Override
271         protected PolicyEngine getPolicyEngineManager() {
272             return engmgr;
273         }
274     }
275
276     private static class MyException extends RuntimeException {
277         private static final long serialVersionUID = 1L;
278
279         public MyException() {
280             super("expected exception");
281         }
282     }
283 }