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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.runtime.main.startstop;
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;
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;
38 * Class to perform unit test of {@link Main}}.
41 public class MainTest {
43 public static final String POLICY_CLAMP_FAILURE_MSG =
44 String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP);
50 public static void setUp() {
51 Registry.newRegistry();
57 * @throws Exception if an error occurs
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()) {
70 public void testMain_Help() {
71 final String[] configParameters = {"-h"};
72 Main main = new Main(configParameters);
73 assertFalse(main.isRunning());
77 public void testMain_Version() {
78 final String[] configParameters = {"-v"};
79 Main main = new Main(configParameters);
80 assertFalse(main.isRunning());
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());
89 // ensure items were added to the registry
90 assertNotNull(Registry.get(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR, ClRuntimeActivator.class));
92 assertThatCode(() -> main.shutdown()).doesNotThrowAnyException();
94 assertFalse(main.isRunning());
98 public void testMain_NoParameter() {
99 assertThatConfigParameterThrownException(new String[] {});
103 public void testMain_FilePathNotDefined() {
104 assertThatConfigParameterThrownException(new String[] {"-c"});
108 public void testMain_TooManyCommand() {
109 assertThatConfigParameterThrownException(new String[] {"-h", "d"});
113 public void testMain_WrongParameter() {
114 assertThatConfigParameterThrownException(new String[] {"-d"});
117 private void assertThatConfigParameterThrownException(final String[] configParameters) {
118 assertThatThrownBy(() -> Main.main(configParameters)).isInstanceOf(ControlLoopRuntimeException.class)
119 .hasMessage(POLICY_CLAMP_FAILURE_MSG);
123 public void testParticipant_NoFileWithThisName() {
124 assertThatConfigFileThrownException("src/test/resources/parameters/NoFileWithThisName.json");
128 public void testParticipant_NotValidFile() {
129 assertThatConfigFileThrownException("src/test/resources/parameters");
133 public void testParticipant_FileEmpty() {
134 assertThatConfigFileThrownException("src/test/resources/parameters/EmptyParameters.json");
138 public void testParticipant_NoParameters() {
139 assertThatConfigFileThrownException("src/test/resources/parameters/NoParameters.json");
143 public void testParticipant_InvalidParameters() {
144 assertThatConfigFileThrownException("src/test/resources/parameters/InvalidParameters.json");
148 public void testParticipant_WrongJsonFormat() {
149 assertThatConfigFileThrownException("src/test/resources/parameters/Unreadable.json");
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));