28d3b439e95afdecc168ea2954213a2d44a47397
[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.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;
43
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;
49
50     @Mock
51     private PolicyEngine engmgr;
52
53     @Mock
54     private Timer timer;
55
56     @Mock
57     private StateManagement mgmt;
58
59     private PmStandbyStateChangeNotifier notifier;
60
61     /**
62      * Initializes the properties.
63      */
64     @BeforeClass
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));
69
70         ActiveStandbyProperties.initProperties(props);
71     }
72
73     /**
74      * Initializes objects, including the notifier.
75      */
76
77     @Before
78     public void setUp() {
79         MockitoAnnotations.initMocks(this);
80
81         notifier = new MyNotifier();
82     }
83
84     @Test
85     public void testHandleStateChange_Null() {
86         notifier.update(mgmt, null);
87         verify(engmgr).deactivate();
88         assertEquals(StateManagement.NULL_VALUE, notifier.getPreviousStandbyStatus());
89
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());
95     }
96
97     @Test
98     public void testHandleStateChange_Null_Ex() {
99         doThrow(new MyException()).when(engmgr).deactivate();
100
101         // should not throw an exception
102         notifier.update(mgmt, null);
103         assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
104     }
105
106     @Test
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());
112
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());
118     }
119
120     @Test
121     public void testHandleStateChange_HotOrCold_Ex() {
122         doThrow(new MyException()).when(engmgr).deactivate();
123
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());
128     }
129
130     @Test
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());
136
137         ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
138         verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));
139
140         // execute the timer task
141         captor.getValue().run();
142
143         verify(engmgr).activate();
144         assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus());
145
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());
152     }
153
154     @Test
155     public void testHandleStateChange_ProvidingService_BeforeActivation() {
156         when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
157         notifier.update(mgmt, null);
158
159         // repeat - nothing else should be done
160         notifier.update(mgmt, null);
161         verify(engmgr, never()).deactivate();
162         verify(engmgr, never()).activate();
163
164         verify(timer, times(1)).schedule(any(), eq(WAIT_INTERVAL));
165         assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
166     }
167
168     @Test
169     public void testHandleStateChange_ProvidingService_Ex() {
170         notifier = new MyNotifier() {
171             @Override
172             protected Timer makeTimer() {
173                 throw new MyException();
174             }
175         };
176
177         when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
178         notifier.update(mgmt, null);
179
180         assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
181     }
182
183     @Test
184     public void testHandleStateChange_Unsupported() {
185         when(mgmt.getStandbyStatus()).thenReturn(UNSUPPORTED_STATUS);
186         notifier.update(mgmt, null);
187
188         verify(engmgr).deactivate();
189         assertEquals(PmStandbyStateChangeNotifier.UNSUPPORTED, notifier.getPreviousStandbyStatus());
190
191         // repeat - nothing else should be done
192         notifier.update(mgmt, null);
193         verify(engmgr, times(1)).deactivate();
194         assertEquals(PmStandbyStateChangeNotifier.UNSUPPORTED, notifier.getPreviousStandbyStatus());
195     }
196
197     @Test
198     public void testHandleStateChange_Unsupported_Ex() {
199         doThrow(new MyException()).when(engmgr).deactivate();
200
201         // should not throw an exception
202         when(mgmt.getStandbyStatus()).thenReturn(UNSUPPORTED_STATUS);
203         notifier.update(mgmt, null);
204         assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
205     }
206
207     @Test
208     public void testCancelTimer() {
209         when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
210         notifier.update(mgmt, null);
211
212         when(mgmt.getStandbyStatus()).thenReturn(null);
213         notifier.update(mgmt, null);
214
215         verify(timer).cancel();
216     }
217
218     @Test
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());
224
225         ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
226         verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));
227
228         // execute the timer task
229         captor.getValue().run();
230
231         verify(engmgr).activate();
232         assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus());
233     }
234
235     @Test
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());
241
242         ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
243         verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));
244
245         doThrow(new MyException()).when(engmgr).activate();
246
247         // execute the timer task
248         captor.getValue().run();
249
250         assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
251     }
252
253     @Test
254     public void testGetPolicyEngineManager() {
255         // use real object with real method - no exception expected
256         new PmStandbyStateChangeNotifier().getPolicyEngineManager();
257     }
258
259     @Test
260     public void testMakeTimer() {
261         // use real object with real method
262         new PmStandbyStateChangeNotifier().makeTimer().cancel();
263     }
264
265     private class MyNotifier extends PmStandbyStateChangeNotifier {
266         @Override
267         protected PolicyEngine getPolicyEngineManager() {
268             return engmgr;
269         }
270
271         @Override
272         protected Timer makeTimer() {
273             return timer;
274         }
275     }
276
277     private static class MyException extends RuntimeException {
278         private static final long serialVersionUID = 1L;
279
280         public MyException() {
281             super("expected exception");
282         }
283     }
284 }