da71c239d1d0877026d28611a92049a156f992bc
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.runtime.main.startstop;
22
23 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
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
29 import org.junit.Test;
30 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
31 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
32 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterHandler;
33 import org.onap.policy.common.utils.services.Registry;
34
35 /**
36  * Class to perform unit test of {@link ClRuntimeActivator}}.
37  *
38  */
39 public class ClRuntimeActivatorTest {
40
41     @Test
42     public void testStartAndStop() throws Exception {
43         Registry.newRegistry();
44         final String[] configParameters = {"-c", "src/test/resources/parameters/TestParameters.json"};
45         final ClRuntimeCommandLineArguments arguments = new ClRuntimeCommandLineArguments();
46         arguments.parse(configParameters);
47         ClRuntimeParameterGroup parameterGroup = new ClRuntimeParameterHandler().getParameters(arguments);
48         ClRuntimeActivator activator = new ClRuntimeActivator(parameterGroup);
49         activator.isAlive();
50
51         assertFalse(activator.isAlive());
52         activator.start();
53         assertTrue(activator.isAlive());
54         assertTrue(activator.getParameterGroup().isValid());
55         assertEquals(activator.getParameterGroup().getName(),
56                 activator.getParameterGroup().getRestServerParameters().getName());
57
58         // repeat start - should throw an exception
59         assertThatIllegalStateException().isThrownBy(() -> activator.start());
60         assertTrue(activator.isAlive());
61         assertTrue(activator.getParameterGroup().isValid());
62
63         activator.stop();
64         assertFalse(activator.isAlive());
65
66         // repeat stop - should throw an exception
67         assertThatIllegalStateException().isThrownBy(() -> activator.stop());
68         assertFalse(activator.isAlive());
69     }
70
71     @Test
72     public void testNull() {
73         assertThatExceptionOfType(ControlLoopRuntimeException.class).isThrownBy(() -> new ClRuntimeActivator(null));
74     }
75
76     @Test
77     public void testNotValid() {
78         ClRuntimeParameterGroup parameterGroup = new ClRuntimeParameterGroup("name");
79         assertThatExceptionOfType(ControlLoopRuntimeException.class)
80                 .isThrownBy(() -> new ClRuntimeActivator(parameterGroup));
81     }
82 }