4a882d4224ac39e4f01babb47e4356fbba44a092
[policy/models.git] /
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.Mockito.doThrow;
26 import static org.mockito.Mockito.never;
27 import static org.mockito.Mockito.verify;
28
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33
34 public class ListenerManagerTest {
35
36     private static final String EXPECTED_EXCEPTION = "expected exception";
37
38     @Mock
39     private Runnable runnable1;
40
41     @Mock
42     private Runnable runnable2;
43
44     @Mock
45     private Runnable runnable3;
46
47     private ListenerManager mgr;
48
49     /**
50      * Initializes fields, including {@link #mgr}.
51      */
52     @Before
53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55
56         mgr = new ListenerManager();
57     }
58
59     @Test
60     public void testStop_testIsRunning() {
61         mgr.add(runnable1);
62         mgr.add(runnable2);
63         mgr.add(runnable3);
64
65         // arrange for one to throw an exception
66         doThrow(new IllegalStateException(EXPECTED_EXCEPTION)).when(runnable2).run();
67
68         // nothing should have been canceled yet
69         verify(runnable1, never()).run();
70         verify(runnable2, never()).run();
71         verify(runnable3, never()).run();
72
73         assertTrue(mgr.isRunning());
74
75         // stop the controller
76         mgr.stop();
77
78         // all controllers should now be stopped
79         assertFalse(mgr.isRunning());
80
81         // everything should have been invoked
82         verify(runnable1).run();
83         verify(runnable2).run();
84         verify(runnable3).run();
85
86         // re-invoking stop should have no effect on the listeners
87         mgr.stop();
88
89         verify(runnable1).run();
90         verify(runnable2).run();
91         verify(runnable3).run();
92     }
93
94     @Test
95     public void testAdd() {
96         // still running - this should not be invoked
97         mgr.add(runnable1);
98         verify(runnable1, never()).run();
99
100         mgr.stop();
101
102         verify(runnable1).run();
103
104         // new additions should be invoked immediately
105         mgr.add(runnable2);
106         verify(runnable2).run();
107
108         // should work with exceptions, too
109         doThrow(new IllegalStateException(EXPECTED_EXCEPTION)).when(runnable3).run();
110         mgr.add(runnable3);
111     }
112
113     @Test
114     public void testRemove() {
115         mgr.add(runnable1);
116         mgr.add(runnable2);
117
118         verify(runnable1, never()).run();
119         verify(runnable2, never()).run();
120
121         // remove the second
122         mgr.remove(runnable2);
123
124         // should be able to remove it again
125         mgr.remove(runnable2);
126
127         mgr.stop();
128
129         // first should have run, but not the second
130         verify(runnable1).run();
131
132         verify(runnable2, never()).run();
133     }
134 }