f97ff9ac19f831d7919cca022eeb2ca69fd3e596
[policy/models.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.controlloop.actorserviceprovider.pipeline;
23
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;
30
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;
39
40 @ExtendWith(MockitoExtension.class)
41 class FutureManagerTest {
42
43     private static final String EXPECTED_EXCEPTION = "expected exception";
44
45     @Mock
46     private Future<String> future1;
47
48     @Mock
49     private Future<String> future2;
50
51     @Mock
52     private Future<String> future3;
53
54     private FutureManager mgr;
55
56     /**
57      * Initializes fields, including {@link #mgr}.
58      */
59     @BeforeEach
60     void setUp() {
61         mgr = new FutureManager();
62     }
63
64     @Test
65      void testStop() {
66         mgr.add(future1);
67         mgr.add(future2);
68         mgr.add(future3);
69
70         // arrange for one to throw an exception
71         when(future2.cancel(anyBoolean())).thenThrow(new IllegalStateException(EXPECTED_EXCEPTION));
72
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());
77
78         assertTrue(mgr.isRunning());
79
80         // stop the controller
81
82         // stop the controller
83         mgr.stop();
84
85         // all controllers should now be stopped
86         assertFalse(mgr.isRunning());
87
88         // everything should have been invoked
89         verify(future1).cancel(anyBoolean());
90         verify(future2).cancel(anyBoolean());
91         verify(future3).cancel(anyBoolean());
92
93         // re-invoking stop should have no effect on the listeners
94         mgr.stop();
95
96         verify(future1).cancel(anyBoolean());
97         verify(future2).cancel(anyBoolean());
98         verify(future3).cancel(anyBoolean());
99     }
100
101     @Test
102      void testAdd() {
103         // still running - this should not be invoked
104         mgr.add(future1);
105         verify(future1, never()).cancel(anyBoolean());
106
107         // re-add should have no impact
108         mgr.add(future1);
109         verify(future1, never()).cancel(anyBoolean());
110
111         mgr.stop();
112
113         verify(future1).cancel(anyBoolean());
114
115         // new additions should be invoked immediately
116         mgr.add(future2);
117         verify(future2).cancel(anyBoolean());
118
119         // should work with exceptions, too
120         when(future3.cancel(anyBoolean())).thenThrow(new IllegalStateException(EXPECTED_EXCEPTION));
121         mgr.add(future3);
122     }
123
124     @Test
125      void testRemove() {
126         mgr.add(future1);
127         mgr.add(future2);
128
129         verify(future1, never()).cancel(anyBoolean());
130         verify(future2, never()).cancel(anyBoolean());
131
132         // remove the second
133         mgr.remove(future2);
134
135         // should be able to remove it again
136         mgr.remove(future2);
137
138         mgr.stop();
139
140         // first should have run, but not the second
141         verify(future1).cancel(anyBoolean());
142
143         verify(future2, never()).cancel(anyBoolean());
144     }
145 }