aboutsummaryrefslogtreecommitdiffstats
path: root/policy-core/src/test/java/org/onap/policy/drools/core/lock/LockRequestFutureTest.java
blob: 2dd90f00de23ccb8311cd023b8b7f1c520d65f90 (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
/*
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2018 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.core.lock;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.onap.policy.drools.core.lock.TestUtils.expectException;
import java.util.concurrent.CancellationException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.drools.core.lock.PolicyResourceLockFeatureAPI.Callback;

public class LockRequestFutureTest {

    private static final int WAIT_SEC = 1;
    private static final String RESOURCE = "my.resource";
    private static final String OWNER = "my.owner";
    private static final String EXPECTED_EXCEPTION = "expected exception";

    private Callback callback;
    private LockRequestFuture fut;

    @Before
    public void setUp() {
        callback = mock(Callback.class);
        fut = new LockRequestFuture(RESOURCE, OWNER, callback);
    }

    @Test
    public void testLockRequestFutureStringStringBoolean_False() throws Exception {
        fut = new LockRequestFuture(RESOURCE, OWNER, false);

        assertTrue(fut.isDone());
        assertEquals(RESOURCE, fut.getResourceId());
        assertEquals(OWNER, fut.getOwner());

        assertFalse(fut.isLocked());
        assertFalse(fut.get(0, TimeUnit.SECONDS));
    }

    @Test
    public void testLockRequestFutureStringStringBoolean_True() throws Exception {
        fut = new LockRequestFuture(RESOURCE, OWNER, true);

        assertTrue(fut.isDone());
        assertEquals(RESOURCE, fut.getResourceId());
        assertEquals(OWNER, fut.getOwner());

        assertTrue(fut.isLocked());
        assertTrue(fut.get(0, TimeUnit.SECONDS));
    }

    @Test
    public void testLockRequestFutureStringStringBoolean_ArgEx() throws Exception {

        // null resource id
        IllegalArgumentException ex =
                        expectException(IllegalArgumentException.class, () -> new LockRequestFuture(null, OWNER, true));
        assertEquals("null resourceId", ex.getMessage());


        // null owner
        ex = expectException(IllegalArgumentException.class, () -> new LockRequestFuture(RESOURCE, null, true));
        assertEquals("null owner", ex.getMessage());
    }

    @Test
    public void testLockRequestFutureStringStringCallback() throws Exception {
        assertFalse(fut.isDone());
        assertEquals(RESOURCE, fut.getResourceId());
        assertEquals(OWNER, fut.getOwner());

        fut.setLocked(true);
        fut.invokeCallback();

        // ensure it invoked the callback
        verify(callback).set(true);
    }

    @Test
    public void testLockRequestFutureStringStringCallback_ArgEx() throws Exception {

        // null resource id
        IllegalArgumentException ex = expectException(IllegalArgumentException.class,
                        () -> new LockRequestFuture(null, OWNER, callback));
        assertEquals("null resourceId", ex.getMessage());


        // null owner
        ex = expectException(IllegalArgumentException.class, () -> new LockRequestFuture(RESOURCE, null, callback));
        assertEquals("null owner", ex.getMessage());


        // null callback is OK
        new LockRequestFuture(RESOURCE, OWNER, null);
    }

    @Test
    public void testGetResourceId() {
        assertEquals(RESOURCE, fut.getResourceId());
    }

    @Test
    public void testGetOwner() {
        assertEquals(OWNER, fut.getOwner());
    }

    @Test
    public void testCancel() throws Exception {
        // not cancelled yet
        assertFalse(fut.isDone());

        // cancel it
        assertTrue(fut.cancel(false));
        assertTrue(fut.isDone());

        // should not block now
        expectException(CancellationException.class, () -> fut.get(0, TimeUnit.SECONDS));

    }

    @Test
    public void testCancel_AlreadyCancelled() throws Exception {

        fut.cancel(true);

        assertFalse(fut.cancel(true));
        assertTrue(fut.isDone());

        expectException(CancellationException.class, () -> fut.get(0, TimeUnit.SECONDS));
    }

    @Test
    public void testCancel_AlreadyAcquired() throws Exception {

        fut.setLocked(true);

        assertFalse(fut.cancel(true));
        assertTrue(fut.isDone());
        assertTrue(fut.get(0, TimeUnit.SECONDS));
    }

    @Test
    public void testCancel_AlreadyDenied() throws Exception {

        fut.setLocked(false);

        assertFalse(fut.cancel(true));
        assertTrue(fut.isDone());
        assertFalse(fut.get(0, TimeUnit.SECONDS));
    }

    @Test
    public void testSetLocked_True() throws Exception {
        assertTrue(fut.setLocked(true));

        assertTrue(fut.isDone());
        assertTrue(fut.get(0, TimeUnit.SECONDS));
    }

    @Test
    public void testSetLocked_False() throws Exception {
        assertTrue(fut.setLocked(false));

        assertTrue(fut.isDone());
        assertFalse(fut.get(0, TimeUnit.SECONDS));
    }

    @Test
    public void testSetLocked_AlreadyCancelled() {

        fut.cancel(true);

        assertFalse(fut.setLocked(true));
        assertFalse(fut.setLocked(false));

        assertTrue(fut.isDone());
        expectException(CancellationException.class, () -> fut.get(0, TimeUnit.SECONDS));
    }

    @Test
    public void testSetLocked_AlreadyAcquired() throws Exception {
        fut.setLocked(true);

        assertTrue(fut.isDone());
        assertTrue(fut.get(0, TimeUnit.SECONDS));

        assertFalse(fut.cancel(true));
        assertFalse(fut.setLocked(true));
        assertFalse(fut.setLocked(false));
    }

    @Test
    public void testSetLocked_AlreadyDenied() throws Exception {
        fut.setLocked(false);

        assertTrue(fut.isDone());
        assertFalse(fut.get(0, TimeUnit.SECONDS));

        assertFalse(fut.cancel(true));
        assertFalse(fut.setLocked(true));
        assertFalse(fut.setLocked(false));
    }

    @Test
    public void testIsCancelled() {
        assertFalse(fut.isCancelled());

        fut.cancel(false);
        assertTrue(fut.isCancelled());
    }

    @Test
    public void testIsCancelled_Acquired() {
        fut.setLocked(true);
        assertFalse(fut.isCancelled());
    }

    @Test
    public void testIsCancelled_Denied() {
        fut.setLocked(false);
        assertFalse(fut.isCancelled());
    }

    @Test
    public void testIsDone_Cancelled() {
        fut.cancel(false);
        assertTrue(fut.isDone());
    }

    @Test
    public void testIsDone_Acquired() {
        fut.setLocked(true);
        assertTrue(fut.isDone());
    }

    @Test
    public void testIsDone_Denied() {
        fut.setLocked(false);
        assertTrue(fut.isDone());
    }

    @Test
    public void testIsDone_Waiting() {
        assertFalse(fut.isDone());
    }

    @Test
    public void testIsLocked_Cancelled() {
        fut.cancel(false);
        assertFalse(fut.isLocked());
    }

    @Test
    public void testIsLocked_Acquired() {
        fut.setLocked(true);
        assertTrue(fut.isLocked());
    }

    @Test
    public void testIsLocked_Denied() {
        fut.setLocked(false);
        assertFalse(fut.isLocked());
    }

    @Test
    public void testIsLocked_Waiting() {
        assertFalse(fut.isLocked());
    }

    @Test
    public void testGet_Cancelled() throws Exception {
        new Thread(() -> fut.cancel(false)).start();
        expectException(CancellationException.class, () -> fut.get());
    }

    @Test
    public void testGet_Acquired() throws Exception {
        new Thread(() -> fut.setLocked(true)).start();
        assertTrue(fut.get());
    }

    @Test
    public void testGet_Denied() throws Exception {
        new Thread(() -> fut.setLocked(false)).start();
        assertFalse(fut.get());
    }

    @Test
    public void testGetLongTimeUnit() throws Exception {
        expectException(TimeoutException.class, () -> fut.get(0, TimeUnit.SECONDS));

        fut.setLocked(true);
        assertTrue(fut.get(0, TimeUnit.SECONDS));
    }

    @Test
    public void testGetLongTimeUnit_Timeout() throws Exception {
        expectException(TimeoutException.class, () -> fut.get(0, TimeUnit.SECONDS));
        expectException(TimeoutException.class, () -> fut.get(2, TimeUnit.MILLISECONDS));
    }

    @Test
    public void testGetLongTimeUnit_Cancelled() throws Exception {
        new Thread(() -> fut.cancel(false)).start();
        expectException(CancellationException.class, () -> fut.get(WAIT_SEC, TimeUnit.SECONDS));
    }

    @Test
    public void testGetLongTimeUnit_Acquired() throws Exception {
        new Thread(() -> fut.setLocked(true)).start();
        assertTrue(fut.get(WAIT_SEC, TimeUnit.SECONDS));
    }

    @Test
    public void testGetLongTimeUnit_Denied() throws Exception {
        new Thread(() -> fut.setLocked(false)).start();
        assertFalse(fut.get(WAIT_SEC, TimeUnit.SECONDS));
    }

    @Test(expected = IllegalStateException.class)
    public void testInvokeCallback() {
        fut.setLocked(true);
        fut.invokeCallback();

        // re-invoke - should throw an exception
        fut.invokeCallback();
    }

    @Test(expected = IllegalStateException.class)
    public void testInvokeCallback_Cancelled() {
        fut.cancel(false);

        // invoke after cancel - should throw an exception
        fut.invokeCallback();
    }

    @Test
    public void testInvokeCallback_Acquired() {
        fut.setLocked(true);
        fut.invokeCallback();

        verify(callback).set(true);
        verify(callback, never()).set(false);
    }

    @Test
    public void testInvokeCallback_Denied() {
        fut.setLocked(false);
        fut.invokeCallback();

        verify(callback).set(false);
        verify(callback, never()).set(true);
    }

    @Test
    public void testInvokeCallback_Ex() {
        doThrow(new RuntimeException(EXPECTED_EXCEPTION)).when(callback).set(anyBoolean());

        fut.setLocked(false);
        fut.invokeCallback();
    }

    @Test
    public void testMakeNullArgException() {
        IllegalArgumentException ex = LockRequestFuture.makeNullArgException(EXPECTED_EXCEPTION);
        assertEquals(EXPECTED_EXCEPTION, ex.getMessage());
    }
}