aboutsummaryrefslogtreecommitdiffstats
path: root/feature-test-transaction/src/test/java/org/onap/policy/drools/testtransaction/TestTransactionAdditionalTest.java
blob: 7a32e549b397494bd324039488d571c35db3965d (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*-
 * ============LICENSE_START=======================================================
 * feature-test-transaction
 * ================================================================================
 * Copyright (C) 2017-2020 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.testtransaction;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
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.Arrays;
import java.util.Collections;
import java.util.EventObject;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.drools.controller.DroolsController;
import org.onap.policy.drools.core.PolicyContainer;
import org.onap.policy.drools.system.PolicyController;

public class TestTransactionAdditionalTest {

    private static final int MAX_SLEEP_COUNT = 3;
    private static final String EXPECTED = "expected exception";
    private static final String CONTROLLER1 = "controller-a";
    private static final String CONTROLLER2 = "controller-b";
    private static final String CONTROLLER3 = "controller-c";
    private static final String SESSION1 = "session-a";
    private static final String SESSION2 = "session-b";
    private static final List<String> sessions = Arrays.asList(SESSION1, SESSION2);
    private static final List<Object> facts = Arrays.asList(0L);

    private Thread theThread;
    private PolicyController controller;
    private PolicyController controller2;
    private PolicyController controller3;
    private Runnable theAction;
    private long waitJoinMs;
    private long doSleepMs;
    private DroolsController drools;
    private PolicyContainer container;
    private Map<String, TtControllerTask> name2task;
    private TtControllerTask task;
    private TtControllerTask task2;
    private TtControllerTask task3;
    private TestTransTImplTester impl;

    /**
     * Initialize objects for each test.
     */
    @Before
    public void setUp() {
        theThread = mock(Thread.class);
        controller = mock(PolicyController.class);
        controller2 = mock(PolicyController.class);
        controller3 = mock(PolicyController.class);
        theAction = null;
        waitJoinMs = -1;
        doSleepMs = -1;
        drools = mock(DroolsController.class);
        container = mock(PolicyContainer.class);
        task2 = mock(TtControllerTask.class);
        task3 = mock(TtControllerTask.class);
        name2task = new TreeMap<>();

        when(drools.getSessionNames()).thenReturn(sessions);
        when(drools.isBrained()).thenReturn(true);
        when(drools.factQuery(anyString(), anyString(), anyString(), anyBoolean())).thenReturn(facts);
        when(drools.getContainer()).thenReturn(container);

        when(controller.getName()).thenReturn(CONTROLLER1);
        when(controller.getDrools()).thenReturn(drools);
        when(controller.isAlive()).thenReturn(true);

        when(controller2.getName()).thenReturn(CONTROLLER2);
        when(controller2.getDrools()).thenReturn(drools);
        when(controller2.isAlive()).thenReturn(true);

        when(controller3.getName()).thenReturn(CONTROLLER3);
        when(controller3.getDrools()).thenReturn(drools);
        when(controller3.isAlive()).thenReturn(true);

        task = new TestTransControllerTaskTester(controller);

        name2task.put(CONTROLLER1, task);
        name2task.put(CONTROLLER2, task2);
        name2task.put(CONTROLLER3, task3);

        impl = new TestTransTImplTester();
    }

    @Test
    public void testTestTransactionImpl() {
        assertNotNull(TestTransactionConstants.getManager());
    }

    @Test
    public void testTestTransactionImplRegister_testTestTransactionImplUnregister() {
        task = mock(TtControllerTask.class);
        when(task.isAlive()).thenReturn(true);
        name2task.put(CONTROLLER1, task);

        impl.register(controller);
        impl.register(controller2);

        // re-register
        impl.register(controller);

        // re-register when task is not running

        // give controller3 same name as controller1 -> task3 replaces task
        when(controller3.getName()).thenReturn(CONTROLLER1);
        name2task.put(CONTROLLER1, task3);
        when(task.isAlive()).thenReturn(false);
        impl.register(controller3);

        impl.unregister(controller);
        verify(task, never()).stop();
        verify(task2, never()).stop();
        verify(task3).stop();

        impl.unregister(controller2);
        verify(task2).stop();

        // unregister again - stop() should not be called again
        impl.unregister(controller3);
        verify(task3).stop();

        // unregister original controller - no stop() should be called again
        impl.unregister(controller);
        verify(task, never()).stop();
        verify(task2).stop();
        verify(task3).stop();
    }

    @Test
    public void testTestTransactionControllerTaskFactory() throws Exception {
        task = new TtControllerTask(controller) {
            @Override
            protected Thread makeThread(Runnable action) {
                return theThread;
            }

            @Override
            protected void joinThread(long waitTimeMs) throws InterruptedException {
                // do nothing
            }
        };

        task.doSleep(1);
        assertEquals(Thread.currentThread(), task.getCurrentThread());
    }

    @Test
    public void testTestTransactionControllerTask() {
        assertEquals(task, theAction);
        assertTrue(task.isAlive());
        assertEquals(controller, task.getController());
        assertEquals(theThread, task.getThread());

        verify(theThread).start();
    }

    @Test
    public void testTestTransactionControllerTaskGetController() {
        assertEquals(controller, task.getController());
    }

    @Test
    public void testTestTransactionControllerTaskGetThread() {
        assertEquals(theThread, task.getThread());
    }

    @Test
    public void testTestTransactionControllerTaskStop() throws Exception {
        task.stop();
        assertFalse(task.isAlive());
        verify(theThread).interrupt();
        assertTrue(waitJoinMs > 0);

        // throw interrupt during join()
        setUp();
        task = new TestTransControllerTaskTester(controller) {
            @Override
            protected void joinThread(long waitTimeMs) throws InterruptedException {
                waitJoinMs = waitTimeMs;
                throw new InterruptedException(EXPECTED);
            }
        };
        task.stop();
        assertFalse(task.isAlive());
        verify(theThread, times(2)).interrupt();
        assertTrue(waitJoinMs > 0);
    }

    @Test
    public void testTestTransactionControllerTaskRun() {
        task.run();
        assertFalse(task.isAlive());
        verify(theThread, never()).interrupt();
        verify(controller, times(MAX_SLEEP_COUNT + 1)).isAlive();
        assertTrue(doSleepMs > 0);

        // not brained
        setUp();
        when(drools.isBrained()).thenReturn(false);
        task.run();
        assertFalse(task.isAlive());
        verify(controller, never()).isAlive();
        assertEquals(-1, doSleepMs);

        // controller not running
        setUp();
        when(controller.isAlive()).thenReturn(false);
        task.run();
        assertFalse(task.isAlive());
        assertEquals(-1, doSleepMs);

        // controller is locked
        setUp();
        when(controller.isLocked()).thenReturn(true);
        task.run();
        assertFalse(task.isAlive());
        assertEquals(-1, doSleepMs);

        // un-brain during sleep
        setUp();
        task = new TestTransControllerTaskTester(controller) {
            @Override
            protected void doSleep(long sleepMs) throws InterruptedException {
                when(drools.isBrained()).thenReturn(false);
                super.doSleep(sleepMs);
            }
        };
        task.run();
        assertFalse(task.isAlive());
        // only hit top of the loop twice
        verify(controller, times(2)).isAlive();
        assertTrue(doSleepMs > 0);

        // stop during sleep
        setUp();
        task = new TestTransControllerTaskTester(controller) {
            @Override
            protected void doSleep(long sleepMs) throws InterruptedException {
                task.stop();
                super.doSleep(sleepMs);
            }
        };
        task.run();
        assertFalse(task.isAlive());
        // only hit top of the loop twice
        verify(controller, times(2)).isAlive();
        assertTrue(doSleepMs > 0);

        // isInterrupted() returns true the first time, interrupt next time
        setUp();
        AtomicInteger count = new AtomicInteger(1);
        when(theThread.isInterrupted()).thenAnswer(args -> {
            if (count.decrementAndGet() >= 0) {
                return true;
            } else {
                throw new InterruptedException(EXPECTED);
            }
        });
        task.run();
        assertFalse(task.isAlive());
        verify(controller, times(2)).isAlive();
        // doSleep() should not be called
        assertEquals(-1, doSleepMs);

        // interrupt during sleep
        setUp();
        task = new TestTransControllerTaskTester(controller) {
            @Override
            protected void doSleep(long sleepMs) throws InterruptedException {
                super.doSleep(sleepMs);
                throw new InterruptedException(EXPECTED);
            }
        };
        task.run();
        assertFalse(task.isAlive());
        verify(theThread).interrupt();
        // only hit top of the loop once
        verify(controller).isAlive();
        assertTrue(doSleepMs > 0);

        // stop() during factQuery()
        setUp();
        when(drools.factQuery(anyString(), anyString(), anyString(), anyBoolean())).thenAnswer(args -> {
            task.stop();
            return facts;
        });
        task.run();
        assertFalse(task.isAlive());
        // only hit top of the loop once
        verify(controller).isAlive();

        // exception during isBrained() check
        setUp();
        when(drools.isBrained()).thenThrow(new IllegalArgumentException(EXPECTED));
        task.run();
        assertFalse(task.isAlive());

        // other exception during isBrained() check
        setUp();
        when(drools.isBrained()).thenThrow(new RuntimeException(EXPECTED));
        task.run();
        assertFalse(task.isAlive());
    }

    @Test
    public void testTestTransactionControllerTaskInjectTxIntoSessions() {
        task.run();
        verify(container, times(MAX_SLEEP_COUNT * sessions.size())).insert(anyString(), any(EventObject.class));

        // null facts
        setUp();
        when(drools.factQuery(anyString(), anyString(), anyString(), anyBoolean())).thenReturn(null);
        task.run();
        verify(container, never()).insert(anyString(), any());

        // empty fact list
        setUp();
        when(drools.factQuery(anyString(), anyString(), anyString(), anyBoolean())).thenReturn(Collections.emptyList());
        task.run();
        verify(container, never()).insert(anyString(), any());
    }

    @Test
    public void testTestTransactionControllerTaskToString() {
        assertTrue(task.toString().startsWith("TTControllerTask ["));
    }

    /**
     * TestTransaction with overridden methods.
     */
    private class TestTransTImplTester extends TtImpl {

        @Override
        protected TtControllerTask makeControllerTask(PolicyController controller) {
            return name2task.get(controller.getName());
        }
    }

    /**
     * Controller task with overridden methods.
     */
    private class TestTransControllerTaskTester extends TtControllerTask {
        private int sleepCount = MAX_SLEEP_COUNT;

        public TestTransControllerTaskTester(PolicyController controller) {
            super(controller);
        }

        @Override
        protected Thread makeThread(Runnable action) {
            theAction = action;
            return theThread;
        }

        @Override
        protected void joinThread(long waitTimeMs) throws InterruptedException {
            waitJoinMs = waitTimeMs;
        }

        @Override
        protected void doSleep(long sleepMs) throws InterruptedException {
            doSleepMs = sleepMs;

            if (--sleepCount <= 0) {
                when(controller.isAlive()).thenReturn(false);
            }
        }

        @Override
        protected Thread getCurrentThread() {
            return thread;
        }
    }
}