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.Mockito.doThrow;
26 import static org.mockito.Mockito.never;
27 import static org.mockito.Mockito.verify;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.junit.MockitoJUnitRunner;
35 @RunWith(MockitoJUnitRunner.class)
36 public class ListenerManagerTest {
38 private static final String EXPECTED_EXCEPTION = "expected exception";
41 private Runnable runnable1;
44 private Runnable runnable2;
47 private Runnable runnable3;
49 private ListenerManager mgr;
52 * Initializes fields, including {@link #mgr}.
56 mgr = new ListenerManager();
60 public void testStop_testIsRunning() {
65 // arrange for one to throw an exception
66 doThrow(new IllegalStateException(EXPECTED_EXCEPTION)).when(runnable2).run();
68 // nothing should have been canceled yet
69 verify(runnable1, never()).run();
70 verify(runnable2, never()).run();
71 verify(runnable3, never()).run();
73 assertTrue(mgr.isRunning());
75 // stop the controller
78 // all controllers should now be stopped
79 assertFalse(mgr.isRunning());
81 // everything should have been invoked
82 verify(runnable1).run();
83 verify(runnable2).run();
84 verify(runnable3).run();
86 // re-invoking stop should have no effect on the listeners
89 verify(runnable1).run();
90 verify(runnable2).run();
91 verify(runnable3).run();
95 public void testAdd() {
96 // still running - this should not be invoked
98 verify(runnable1, never()).run();
102 verify(runnable1).run();
104 // new additions should be invoked immediately
106 verify(runnable2).run();
108 // should work with exceptions, too
109 doThrow(new IllegalStateException(EXPECTED_EXCEPTION)).when(runnable3).run();
114 public void testRemove() {
118 verify(runnable1, never()).run();
119 verify(runnable2, never()).run();
122 mgr.remove(runnable2);
124 // should be able to remove it again
125 mgr.remove(runnable2);
129 // first should have run, but not the second
130 verify(runnable1).run();
132 verify(runnable2, never()).run();