2 * ============LICENSE_START=======================================================
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
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.impl;
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.doThrow;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.spy;
31 import static org.mockito.Mockito.verify;
33 import org.junit.Before;
34 import org.junit.Test;
36 public class StartConfigPartialTest {
37 private static final IllegalArgumentException EXPECTED_EXCEPTION =
38 new IllegalArgumentException("expected exception");
39 private static final String MY_NAME = "my-name";
40 private static final String PARAMS = "config data";
41 private static final String PARAMS2 = "config data #2";
42 private static final String PARAMSX = "config data exception";
44 private StartConfigPartial<String> config;
47 * Creates a config whose doXxx() methods do nothing.
51 config = new StartConfigPartial<>(MY_NAME) {
53 protected void doConfigure(String parameters) {
58 protected void doStart() {
63 protected void doStop() {
68 protected void doShutdown() {
77 public void testConfigImpl_testGetFullName() {
78 assertEquals(MY_NAME, config.getFullName());
82 public void testIsAlive() {
83 assertFalse(config.isAlive());
87 public void testIsConfigured_testConfigure() {
88 // throw an exception during doConfigure(), but should remain unconfigured
89 assertFalse(config.isConfigured());
90 doThrow(EXPECTED_EXCEPTION).when(config).doConfigure(PARAMSX);
91 assertThatIllegalArgumentException().isThrownBy(() -> config.configure(PARAMSX)).isEqualTo(EXPECTED_EXCEPTION);
92 assertFalse(config.isConfigured());
94 assertFalse(config.isConfigured());
95 config.configure(PARAMS);
96 verify(config).doConfigure(PARAMS);
97 assertTrue(config.isConfigured());
99 // should not be able to re-configure while running
101 assertThatIllegalStateException().isThrownBy(() -> config.configure(PARAMS2)).withMessageContaining(MY_NAME);
102 verify(config, never()).doConfigure(PARAMS2);
104 // should be able to re-configure after stopping
106 config.configure(PARAMS2);
107 verify(config).doConfigure(PARAMS2);
108 assertTrue(config.isConfigured());
110 // should remain configured after exception
111 doThrow(EXPECTED_EXCEPTION).when(config).doConfigure(PARAMSX);
112 assertThatIllegalArgumentException().isThrownBy(() -> config.configure(PARAMSX)).isEqualTo(EXPECTED_EXCEPTION);
113 assertTrue(config.isConfigured());
117 public void testStart() {
118 assertFalse(config.isAlive());
120 // can't start if not configured yet
121 assertThatIllegalStateException().isThrownBy(() -> config.start()).withMessageContaining(MY_NAME);
122 assertFalse(config.isAlive());
124 config.configure(PARAMS);
127 verify(config).doStart();
128 assertTrue(config.isAlive());
129 assertTrue(config.isConfigured());
131 // ok to restart when running, but shouldn't invoke doStart() again
133 verify(config).doStart();
134 assertTrue(config.isAlive());
135 assertTrue(config.isConfigured());
137 // should never have invoked these
138 verify(config, never()).doStop();
139 verify(config, never()).doShutdown();
141 // throw exception when started again, but should remain stopped
143 doThrow(EXPECTED_EXCEPTION).when(config).doStart();
144 assertThatIllegalArgumentException().isThrownBy(() -> config.start()).isEqualTo(EXPECTED_EXCEPTION);
145 assertFalse(config.isAlive());
146 assertTrue(config.isConfigured());
150 public void testStop() {
151 config.configure(PARAMS);
153 // ok to stop if not running, but shouldn't invoke doStop()
155 verify(config, never()).doStop();
156 assertFalse(config.isAlive());
157 assertTrue(config.isConfigured());
161 // now stop should have an effect
163 verify(config).doStop();
164 assertFalse(config.isAlive());
165 assertTrue(config.isConfigured());
167 // should have only invoked this once
168 verify(config).doStart();
170 // should never have invoked these
171 verify(config, never()).doShutdown();
173 // throw exception when stopped again, but should go ahead and stop
175 doThrow(EXPECTED_EXCEPTION).when(config).doStop();
176 assertThatIllegalArgumentException().isThrownBy(() -> config.stop()).isEqualTo(EXPECTED_EXCEPTION);
177 assertFalse(config.isAlive());
178 assertTrue(config.isConfigured());
182 public void testShutdown() {
183 config.configure(PARAMS);
185 // ok to shutdown if not running, but shouldn't invoke doShutdown()
187 verify(config, never()).doShutdown();
188 assertFalse(config.isAlive());
189 assertTrue(config.isConfigured());
193 // now stop should have an effect
195 verify(config).doShutdown();
196 assertFalse(config.isAlive());
197 assertTrue(config.isConfigured());
199 // should have only invoked this once
200 verify(config).doStart();
202 // should never have invoked these
203 verify(config, never()).doStop();
205 // throw exception when shut down again, but should go ahead and shut down
207 doThrow(EXPECTED_EXCEPTION).when(config).doShutdown();
208 assertThatIllegalArgumentException().isThrownBy(() -> config.shutdown()).isEqualTo(EXPECTED_EXCEPTION);
209 assertFalse(config.isAlive());
210 assertTrue(config.isConfigured());