2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.actorserviceprovider.pipeline;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.ArgumentMatchers.anyBoolean;
27 import static org.mockito.Mockito.never;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
31 import java.util.concurrent.Future;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.junit.MockitoJUnitRunner;
38 import org.mockito.junit.jupiter.MockitoExtension;
40 @ExtendWith(MockitoExtension.class)
41 class FutureManagerTest {
43 private static final String EXPECTED_EXCEPTION = "expected exception";
46 private Future<String> future1;
49 private Future<String> future2;
52 private Future<String> future3;
54 private FutureManager mgr;
57 * Initializes fields, including {@link #mgr}.
61 mgr = new FutureManager();
70 // arrange for one to throw an exception
71 when(future2.cancel(anyBoolean())).thenThrow(new IllegalStateException(EXPECTED_EXCEPTION));
73 // nothing should have been canceled yet
74 verify(future1, never()).cancel(anyBoolean());
75 verify(future2, never()).cancel(anyBoolean());
76 verify(future3, never()).cancel(anyBoolean());
78 assertTrue(mgr.isRunning());
80 // stop the controller
82 // stop the controller
85 // all controllers should now be stopped
86 assertFalse(mgr.isRunning());
88 // everything should have been invoked
89 verify(future1).cancel(anyBoolean());
90 verify(future2).cancel(anyBoolean());
91 verify(future3).cancel(anyBoolean());
93 // re-invoking stop should have no effect on the listeners
96 verify(future1).cancel(anyBoolean());
97 verify(future2).cancel(anyBoolean());
98 verify(future3).cancel(anyBoolean());
103 // still running - this should not be invoked
105 verify(future1, never()).cancel(anyBoolean());
107 // re-add should have no impact
109 verify(future1, never()).cancel(anyBoolean());
113 verify(future1).cancel(anyBoolean());
115 // new additions should be invoked immediately
117 verify(future2).cancel(anyBoolean());
119 // should work with exceptions, too
120 when(future3.cancel(anyBoolean())).thenThrow(new IllegalStateException(EXPECTED_EXCEPTION));
129 verify(future1, never()).cancel(anyBoolean());
130 verify(future2, never()).cancel(anyBoolean());
135 // should be able to remove it again
140 // first should have run, but not the second
141 verify(future1).cancel(anyBoolean());
143 verify(future2, never()).cancel(anyBoolean());