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