2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actorserviceprovider.pipeline;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.ArgumentMatchers.anyBoolean;
26 import static org.mockito.Mockito.never;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
30 import java.util.concurrent.Future;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.junit.MockitoJUnitRunner;
37 @RunWith(MockitoJUnitRunner.class)
38 public class FutureManagerTest {
40 private static final String EXPECTED_EXCEPTION = "expected exception";
43 private Future<String> future1;
46 private Future<String> future2;
49 private Future<String> future3;
51 private FutureManager mgr;
54 * Initializes fields, including {@link #mgr}.
58 mgr = new FutureManager();
62 public void testStop() {
67 // arrange for one to throw an exception
68 when(future2.cancel(anyBoolean())).thenThrow(new IllegalStateException(EXPECTED_EXCEPTION));
70 // nothing should have been canceled yet
71 verify(future1, never()).cancel(anyBoolean());
72 verify(future2, never()).cancel(anyBoolean());
73 verify(future3, never()).cancel(anyBoolean());
75 assertTrue(mgr.isRunning());
77 // stop the controller
79 // stop the controller
82 // all controllers should now be stopped
83 assertFalse(mgr.isRunning());
85 // everything should have been invoked
86 verify(future1).cancel(anyBoolean());
87 verify(future2).cancel(anyBoolean());
88 verify(future3).cancel(anyBoolean());
90 // re-invoking stop should have no effect on the listeners
93 verify(future1).cancel(anyBoolean());
94 verify(future2).cancel(anyBoolean());
95 verify(future3).cancel(anyBoolean());
99 public void testAdd() {
100 // still running - this should not be invoked
102 verify(future1, never()).cancel(anyBoolean());
104 // re-add should have no impact
106 verify(future1, never()).cancel(anyBoolean());
110 verify(future1).cancel(anyBoolean());
112 // new additions should be invoked immediately
114 verify(future2).cancel(anyBoolean());
116 // should work with exceptions, too
117 when(future3.cancel(anyBoolean())).thenThrow(new IllegalStateException(EXPECTED_EXCEPTION));
122 public void testRemove() {
126 verify(future1, never()).cancel(anyBoolean());
127 verify(future2, never()).cancel(anyBoolean());
132 // should be able to remove it again
137 // first should have run, but not the second
138 verify(future1).cancel(anyBoolean());
140 verify(future2, never()).cancel(anyBoolean());