1cf20e21a017c498520d6dc9902a5eda950f9207
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019-2020 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.assertj.core.api.Assertions.assertThatCode;
24 import static org.junit.Assert.assertEquals;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Matchers.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;
32
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.mockito.ArgumentCaptor;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.onap.policy.common.im.StateManagement;
44 import org.onap.policy.drools.system.PolicyEngine;
45
46 public class PmStandbyStateChangeNotifierTest {
47     private static final String UNSUPPORTED_STATUS = "unsupported status";
48     private static final String PDP_ID = "my-pdp";
49     private static final long UPDATE_INTERVAL = 100;
50     private static final long WAIT_INTERVAL = 2 * UPDATE_INTERVAL + 2000;
51
52     private static Factory saveFactory;
53
54     @Mock
55     private Factory factory;
56
57     @Mock
58     private PolicyEngine engmgr;
59
60     @Mock
61     private Timer timer;
62
63     @Mock
64     private StateManagement mgmt;
65
66     private PmStandbyStateChangeNotifier notifier;
67
68     /**
69      * Initializes the properties.
70      */
71     @BeforeClass
72     public static void setUpBeforeClass() {
73         Properties props = new Properties();
74         props.setProperty(ActiveStandbyProperties.NODE_NAME, PDP_ID);
75         props.setProperty(ActiveStandbyProperties.PDP_UPDATE_INTERVAL, String.valueOf(UPDATE_INTERVAL));
76
77         ActiveStandbyProperties.initProperties(props);
78
79         saveFactory = Factory.getInstance();
80     }
81
82     @AfterClass
83     public static void tearDownAfterClass() {
84         Factory.setInstance(saveFactory);
85     }
86
87     /**
88      * Initializes objects, including the notifier.
89      */
90     @Before
91     public void setUp() {
92         MockitoAnnotations.initMocks(this);
93
94         Factory.setInstance(factory);
95         when(factory.makeTimer()).thenReturn(timer);
96
97         notifier = new MyNotifier();
98     }
99
100     @Test
101     public void testHandleStateChange_Null() {
102         notifier.update(mgmt, null);
103         verify(engmgr).deactivate();
104         assertEquals(StateManagement.NULL_VALUE, notifier.getPreviousStandbyStatus());
105
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());
111     }
112
113     @Test
114     public void testHandleStateChange_Null_Ex() {
115         doThrow(new MyException()).when(engmgr).deactivate();
116
117         // should not throw an exception
118         notifier.update(mgmt, null);
119         assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
120     }
121
122     @Test
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());
128
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());
134     }
135
136     @Test
137     public void testHandleStateChange_HotOrCold_Ex() {
138         doThrow(new MyException()).when(engmgr).deactivate();
139
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());
144     }
145
146     @Test
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());
152
153         ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
154         verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));
155
156         // execute the timer task
157         captor.getValue().run();
158
159         verify(engmgr).activate();
160         assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus());
161
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());
168     }
169
170     @Test
171     public void testHandleStateChange_ProvidingService_BeforeActivation() {
172         when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
173         notifier.update(mgmt, null);
174
175         // repeat - nothing else should be done
176         notifier.update(mgmt, null);
177         verify(engmgr, never()).deactivate();
178         verify(engmgr, never()).activate();
179
180         verify(timer, times(1)).schedule(any(), eq(WAIT_INTERVAL));
181         assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
182     }
183
184     @Test
185     public void testHandleStateChange_ProvidingService_Ex() {
186         when(factory.makeTimer()).thenThrow(new MyException());
187
188         when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
189         notifier.update(mgmt, null);
190
191         assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
192     }
193
194     @Test
195     public void testHandleStateChange_Unsupported() {
196         when(mgmt.getStandbyStatus()).thenReturn(UNSUPPORTED_STATUS);
197         notifier.update(mgmt, null);
198
199         verify(engmgr).deactivate();
200         assertEquals(PmStandbyStateChangeNotifier.UNSUPPORTED, notifier.getPreviousStandbyStatus());
201
202         // repeat - nothing else should be done
203         notifier.update(mgmt, null);
204         verify(engmgr, times(1)).deactivate();
205         assertEquals(PmStandbyStateChangeNotifier.UNSUPPORTED, notifier.getPreviousStandbyStatus());
206     }
207
208     @Test
209     public void testHandleStateChange_Unsupported_Ex() {
210         doThrow(new MyException()).when(engmgr).deactivate();
211
212         // should not throw an exception
213         when(mgmt.getStandbyStatus()).thenReturn(UNSUPPORTED_STATUS);
214         notifier.update(mgmt, null);
215         assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
216     }
217
218     @Test
219     public void testCancelTimer() {
220         when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
221         notifier.update(mgmt, null);
222
223         when(mgmt.getStandbyStatus()).thenReturn(null);
224         notifier.update(mgmt, null);
225
226         verify(timer).cancel();
227     }
228
229     @Test
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());
235
236         ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
237         verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));
238
239         // execute the timer task
240         captor.getValue().run();
241
242         verify(engmgr).activate();
243         assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus());
244     }
245
246     @Test
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());
252
253         ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
254         verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));
255
256         doThrow(new MyException()).when(engmgr).activate();
257
258         // execute the timer task
259         captor.getValue().run();
260
261         assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
262     }
263
264     @Test
265     public void testGetPolicyEngineManager() {
266         // use real object with real method - no exception expected
267         assertThatCode(() -> new PmStandbyStateChangeNotifier().getPolicyEngineManager()).doesNotThrowAnyException();
268     }
269
270     private class MyNotifier extends PmStandbyStateChangeNotifier {
271         @Override
272         protected PolicyEngine getPolicyEngineManager() {
273             return engmgr;
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 }