aboutsummaryrefslogtreecommitdiffstats
path: root/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/PmStandbyStateChangeNotifierTest.java
blob: a6dba1a6069c6cc68ff5b7e8204571792a7f90e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.drools.activestandby;

import static org.assertj.core.api.Assertions.assertThatCode;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.policy.common.im.StateManagement;
import org.onap.policy.drools.system.PolicyEngine;

@RunWith(MockitoJUnitRunner.class)
public class PmStandbyStateChangeNotifierTest {
    private static final String UNSUPPORTED_STATUS = "unsupported status";
    private static final String PDP_ID = "my-pdp";
    private static final long UPDATE_INTERVAL = 100;
    private static final long WAIT_INTERVAL = 2 * UPDATE_INTERVAL + 2000;

    private static Factory saveFactory;

    @Mock
    private Factory factory;

    @Mock
    private PolicyEngine engmgr;

    @Mock
    private Timer timer;

    @Mock
    private StateManagement mgmt;

    private PmStandbyStateChangeNotifier notifier;

    /**
     * Initializes the properties.
     */
    @BeforeClass
    public static void setUpBeforeClass() {
        Properties props = new Properties();
        props.setProperty(ActiveStandbyProperties.NODE_NAME, PDP_ID);
        props.setProperty(ActiveStandbyProperties.PDP_UPDATE_INTERVAL, String.valueOf(UPDATE_INTERVAL));

        ActiveStandbyProperties.initProperties(props);

        saveFactory = Factory.getInstance();
    }

    @AfterClass
    public static void tearDownAfterClass() {
        Factory.setInstance(saveFactory);
    }

    /**
     * Initializes objects, including the notifier.
     */
    @Before
    public void setUp() {
        Factory.setInstance(factory);
        when(factory.makeTimer()).thenReturn(timer);

        notifier = new MyNotifier();
    }

    @Test
    public void testHandleStateChange_Null() {
        notifier.update(mgmt, null);
        verify(engmgr).deactivate();
        assertEquals(StateManagement.NULL_VALUE, notifier.getPreviousStandbyStatus());

        // repeat - nothing else should be done
        when(mgmt.getStandbyStatus()).thenReturn(StateManagement.NULL_VALUE);
        notifier.update(mgmt, null);
        verify(engmgr, times(1)).deactivate();
        assertEquals(StateManagement.NULL_VALUE, notifier.getPreviousStandbyStatus());
    }

    @Test
    public void testHandleStateChange_Null_Ex() {
        doThrow(new MyException()).when(engmgr).deactivate();

        // should not throw an exception
        notifier.update(mgmt, null);
        assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
    }

    @Test
    public void testHandleStateChange_HotOrCold() {
        when(mgmt.getStandbyStatus()).thenReturn(StateManagement.HOT_STANDBY);
        notifier.update(mgmt, null);
        verify(engmgr).deactivate();
        assertEquals(PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY, notifier.getPreviousStandbyStatus());

        // repeat - nothing else should be done
        when(mgmt.getStandbyStatus()).thenReturn(StateManagement.COLD_STANDBY);
        notifier.update(mgmt, null);
        verify(engmgr, times(1)).deactivate();
        assertEquals(PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY, notifier.getPreviousStandbyStatus());
    }

    @Test
    public void testHandleStateChange_HotOrCold_Ex() {
        doThrow(new MyException()).when(engmgr).deactivate();

        // should not throw an exception
        when(mgmt.getStandbyStatus()).thenReturn(StateManagement.HOT_STANDBY);
        notifier.update(mgmt, null);
        assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
    }

    @Test
    public void testHandleStateChange_ProvidingService() {
        when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
        notifier.update(mgmt, null);
        verify(engmgr, never()).activate();
        assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());

        ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
        verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));

        // execute the timer task
        captor.getValue().run();

        verify(engmgr).activate();
        assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus());

        // repeat - nothing else should be done
        notifier.update(mgmt, null);
        verify(engmgr, never()).deactivate();
        verify(engmgr, times(1)).activate();
        verify(timer, times(1)).schedule(captor.capture(), eq(WAIT_INTERVAL));
        assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus());
    }

    @Test
    public void testHandleStateChange_ProvidingService_BeforeActivation() {
        when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
        notifier.update(mgmt, null);

        // repeat - nothing else should be done
        notifier.update(mgmt, null);
        verify(engmgr, never()).deactivate();
        verify(engmgr, never()).activate();

        verify(timer, times(1)).schedule(any(), eq(WAIT_INTERVAL));
        assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
    }

    @Test
    public void testHandleStateChange_ProvidingService_Ex() {
        when(factory.makeTimer()).thenThrow(new MyException());

        when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
        notifier.update(mgmt, null);

        assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
    }

    @Test
    public void testHandleStateChange_Unsupported() {
        when(mgmt.getStandbyStatus()).thenReturn(UNSUPPORTED_STATUS);
        notifier.update(mgmt, null);

        verify(engmgr).deactivate();
        assertEquals(PmStandbyStateChangeNotifier.UNSUPPORTED, notifier.getPreviousStandbyStatus());

        // repeat - nothing else should be done
        notifier.update(mgmt, null);
        verify(engmgr, times(1)).deactivate();
        assertEquals(PmStandbyStateChangeNotifier.UNSUPPORTED, notifier.getPreviousStandbyStatus());
    }

    @Test
    public void testHandleStateChange_Unsupported_Ex() {
        doThrow(new MyException()).when(engmgr).deactivate();

        // should not throw an exception
        when(mgmt.getStandbyStatus()).thenReturn(UNSUPPORTED_STATUS);
        notifier.update(mgmt, null);
        assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
    }

    @Test
    public void testCancelTimer() {
        when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
        notifier.update(mgmt, null);

        when(mgmt.getStandbyStatus()).thenReturn(null);
        notifier.update(mgmt, null);

        verify(timer).cancel();
    }

    @Test
    public void testDelayActivateClass() {
        when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
        notifier.update(mgmt, null);
        verify(engmgr, never()).activate();
        assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());

        ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
        verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));

        // execute the timer task
        captor.getValue().run();

        verify(engmgr).activate();
        assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus());
    }

    @Test
    public void testDelayActivateClass_Ex() {
        when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE);
        notifier.update(mgmt, null);
        verify(engmgr, never()).activate();
        assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());

        ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class);
        verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL));

        doThrow(new MyException()).when(engmgr).activate();

        // execute the timer task
        captor.getValue().run();

        assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus());
    }

    @Test
    public void testGetPolicyEngineManager() {
        // use real object with real method - no exception expected
        assertThatCode(() -> new PmStandbyStateChangeNotifier().getPolicyEngineManager()).doesNotThrowAnyException();
    }

    private class MyNotifier extends PmStandbyStateChangeNotifier {
        @Override
        protected PolicyEngine getPolicyEngineManager() {
            return engmgr;
        }
    }

    private static class MyException extends RuntimeException {
        private static final long serialVersionUID = 1L;

        public MyException() {
            super("expected exception");
        }
    }
}