b06383c68cb7226f74ce1d42fa639becbcb4860f
[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.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28
29 import org.junit.AfterClass;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.onap.policy.clamp.controlloop.common.ControlLoopConstants;
33 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
34 import org.onap.policy.common.utils.resources.MessageConstants;
35 import org.onap.policy.common.utils.services.Registry;
36
37 /**
38  * Class to perform unit test of {@link Main}}.
39  *
40  */
41 public class MainTest {
42
43     public static final String POLICY_CLAMP_FAILURE_MSG =
44             String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP);
45
46     /**
47      * Set up.
48      */
49     @BeforeClass
50     public static void setUp() {
51         Registry.newRegistry();
52     }
53
54     /**
55      * Shuts "main" down.
56      *
57      * @throws Exception if an error occurs
58      */
59     @AfterClass
60     public static void tearDown() throws Exception {
61         // shut down activator
62         final ClRuntimeActivator activator =
63                 Registry.getOrDefault(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR, ClRuntimeActivator.class, null);
64         if (activator != null && activator.isAlive()) {
65             activator.shutdown();
66         }
67     }
68
69     @Test
70     public void testMain_Help() {
71         final String[] configParameters = {"-h"};
72         Main main = new Main(configParameters);
73         assertFalse(main.isRunning());
74     }
75
76     @Test
77     public void testMain_Version() {
78         final String[] configParameters = {"-v"};
79         Main main = new Main(configParameters);
80         assertFalse(main.isRunning());
81     }
82
83     @Test
84     public void testMain_Valid() {
85         final String[] configParameters = {"-c", "src/test/resources/parameters/TestParameters.json"};
86         Main main = new Main(configParameters);
87         assertTrue(main.isRunning());
88
89         // ensure items were added to the registry
90         assertNotNull(Registry.get(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR, ClRuntimeActivator.class));
91
92         assertThatCode(() -> main.shutdown()).doesNotThrowAnyException();
93
94         assertFalse(main.isRunning());
95     }
96
97     @Test
98     public void testMain_NoParameter() {
99         assertThatConfigParameterThrownException(new String[] {});
100     }
101
102     @Test
103     public void testMain_FilePathNotDefined() {
104         assertThatConfigParameterThrownException(new String[] {"-c"});
105     }
106
107     @Test
108     public void testMain_TooManyCommand() {
109         assertThatConfigParameterThrownException(new String[] {"-h", "d"});
110     }
111
112     @Test
113     public void testMain_WrongParameter() {
114         assertThatConfigParameterThrownException(new String[] {"-d"});
115     }
116
117     private void assertThatConfigParameterThrownException(final String[] configParameters) {
118         assertThatThrownBy(() -> Main.main(configParameters)).isInstanceOf(ControlLoopRuntimeException.class)
119                 .hasMessage(POLICY_CLAMP_FAILURE_MSG);
120     }
121
122     @Test
123     public void testParticipant_NoFileWithThisName() {
124         assertThatConfigFileThrownException("src/test/resources/parameters/NoFileWithThisName.json");
125     }
126
127     @Test
128     public void testParticipant_NotValidFile() {
129         assertThatConfigFileThrownException("src/test/resources/parameters");
130     }
131
132     @Test
133     public void testParticipant_FileEmpty() {
134         assertThatConfigFileThrownException("src/test/resources/parameters/EmptyParameters.json");
135     }
136
137     @Test
138     public void testParticipant_NoParameters() {
139         assertThatConfigFileThrownException("src/test/resources/parameters/NoParameters.json");
140     }
141
142     @Test
143     public void testParticipant_InvalidParameters() {
144         assertThatConfigFileThrownException("src/test/resources/parameters/InvalidParameters.json");
145     }
146
147     @Test
148     public void testParticipant_WrongJsonFormat() {
149         assertThatConfigFileThrownException("src/test/resources/parameters/Unreadable.json");
150     }
151
152     private void assertThatConfigFileThrownException(final String configFilePath) {
153         final String[] configParameters = new String[] {"-c", configFilePath};
154         assertThatThrownBy(() -> new Main(configParameters)).isInstanceOf(ControlLoopRuntimeException.class)
155                 .hasMessage(String.format(POLICY_CLAMP_FAILURE_MSG));
156     }
157 }