More sonars in models
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / impl / StartConfigPartialTest.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.impl;
22
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;
32
33 import org.junit.Before;
34 import org.junit.Test;
35
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";
43
44     private StartConfigPartial<String> config;
45
46     /**
47      * Creates a config whose doXxx() methods do nothing.
48      */
49     @Before
50     public void setUp() {
51         config = new StartConfigPartial<>(MY_NAME) {
52             @Override
53             protected void doConfigure(String parameters) {
54                 // do nothing
55             }
56
57             @Override
58             protected void doStart() {
59                 // do nothing
60             }
61
62             @Override
63             protected void doStop() {
64                 // do nothing
65             }
66
67             @Override
68             protected void doShutdown() {
69                 // do nothing
70             }
71         };
72
73         config = spy(config);
74     }
75
76     @Test
77     public void testConfigImpl_testGetFullName() {
78         assertEquals(MY_NAME, config.getFullName());
79     }
80
81     @Test
82     public void testIsAlive() {
83         assertFalse(config.isAlive());
84     }
85
86     @Test
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());
93
94         assertFalse(config.isConfigured());
95         config.configure(PARAMS);
96         verify(config).doConfigure(PARAMS);
97         assertTrue(config.isConfigured());
98
99         // should not be able to re-configure while running
100         config.start();
101         assertThatIllegalStateException().isThrownBy(() -> config.configure(PARAMS2)).withMessageContaining(MY_NAME);
102         verify(config, never()).doConfigure(PARAMS2);
103
104         // should be able to re-configure after stopping
105         config.stop();
106         config.configure(PARAMS2);
107         verify(config).doConfigure(PARAMS2);
108         assertTrue(config.isConfigured());
109
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());
114     }
115
116     @Test
117     public void testStart() {
118         assertFalse(config.isAlive());
119
120         // can't start if not configured yet
121         assertThatIllegalStateException().isThrownBy(() -> config.start()).withMessageContaining(MY_NAME);
122         assertFalse(config.isAlive());
123
124         config.configure(PARAMS);
125
126         config.start();
127         verify(config).doStart();
128         assertTrue(config.isAlive());
129         assertTrue(config.isConfigured());
130
131         // ok to restart when running, but shouldn't invoke doStart() again
132         config.start();
133         verify(config).doStart();
134         assertTrue(config.isAlive());
135         assertTrue(config.isConfigured());
136
137         // should never have invoked these
138         verify(config, never()).doStop();
139         verify(config, never()).doShutdown();
140
141         // throw exception when started again, but should remain stopped
142         config.stop();
143         doThrow(EXPECTED_EXCEPTION).when(config).doStart();
144         assertThatIllegalArgumentException().isThrownBy(() -> config.start()).isEqualTo(EXPECTED_EXCEPTION);
145         assertFalse(config.isAlive());
146         assertTrue(config.isConfigured());
147     }
148
149     @Test
150     public void testStop() {
151         config.configure(PARAMS);
152
153         // ok to stop if not running, but shouldn't invoke doStop()
154         config.stop();
155         verify(config, never()).doStop();
156         assertFalse(config.isAlive());
157         assertTrue(config.isConfigured());
158
159         config.start();
160
161         // now stop should have an effect
162         config.stop();
163         verify(config).doStop();
164         assertFalse(config.isAlive());
165         assertTrue(config.isConfigured());
166
167         // should have only invoked this once
168         verify(config).doStart();
169
170         // should never have invoked these
171         verify(config, never()).doShutdown();
172
173         // throw exception when stopped again, but should go ahead and stop
174         config.start();
175         doThrow(EXPECTED_EXCEPTION).when(config).doStop();
176         assertThatIllegalArgumentException().isThrownBy(() -> config.stop()).isEqualTo(EXPECTED_EXCEPTION);
177         assertFalse(config.isAlive());
178         assertTrue(config.isConfigured());
179     }
180
181     @Test
182     public void testShutdown() {
183         config.configure(PARAMS);
184
185         // ok to shutdown if not running, but shouldn't invoke doShutdown()
186         config.shutdown();
187         verify(config, never()).doShutdown();
188         assertFalse(config.isAlive());
189         assertTrue(config.isConfigured());
190
191         config.start();
192
193         // now stop should have an effect
194         config.shutdown();
195         verify(config).doShutdown();
196         assertFalse(config.isAlive());
197         assertTrue(config.isConfigured());
198
199         // should have only invoked this once
200         verify(config).doStart();
201
202         // should never have invoked these
203         verify(config, never()).doStop();
204
205         // throw exception when shut down again, but should go ahead and shut down
206         config.start();
207         doThrow(EXPECTED_EXCEPTION).when(config).doShutdown();
208         assertThatIllegalArgumentException().isThrownBy(() -> config.shutdown()).isEqualTo(EXPECTED_EXCEPTION);
209         assertFalse(config.isAlive());
210         assertTrue(config.isConfigured());
211     }
212 }