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.participant.policy.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;
37 public class TestMain {
43 public static void setUp() {
44 Registry.newRegistry();
50 * @throws Exception if an error occurs
53 public static void tearDown() throws Exception {
54 // shut down activator
55 final ParticipantPolicyActivator activator =
56 Registry.getOrDefault(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR,
57 ParticipantPolicyActivator.class, null);
58 if (activator != null && activator.isAlive()) {
64 public void testMain_Help() {
65 final String[] configParameters = {"-h"};
66 Main main = new Main(configParameters);
67 assertFalse(main.isRunning());
71 public void testMain_Version() {
72 final String[] configParameters = {"-v"};
73 Main main = new Main(configParameters);
74 assertFalse(main.isRunning());
78 public void testMain_Valid() {
79 final String[] configParameters = {"-c", "src/test/resources/parameters/TestParameters.json"};
80 Main main = new Main(configParameters);
81 assertTrue(main.isRunning());
83 assertThatCode(() -> main.shutdown()).doesNotThrowAnyException();
85 assertFalse(main.isRunning());
89 public void testMain_NoParameter() {
90 assertThatConfigParameterThrownException(new String[] {});
94 public void testMain_FilePathNotDefined() {
95 assertThatConfigParameterThrownException(new String[] {"-c"});
99 public void testMain_TooManyCommand() {
100 assertThatConfigParameterThrownException(new String[] {"-h", "d"});
104 public void testMain_WrongParameter() {
105 assertThatConfigParameterThrownException(new String[] {"-d"});
108 private void assertThatConfigParameterThrownException(final String[] configParameters) {
109 assertThatThrownBy(() -> Main.main(configParameters)).isInstanceOf(ControlLoopRuntimeException.class)
110 .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP));
114 public void testParticipant_NoFileWithThisName() {
115 assertThatConfigFileThrownException("src/test/resources/parameters/NoFileWithThisName.json");
119 public void testParticipant_NotValidFile() {
120 assertThatConfigFileThrownException("src/test/resources/parameters");
124 public void testParticipant_NoParameters() {
125 assertThatConfigFileThrownException("src/test/resources/parameters/NoParameters.json");
129 public void testParticipant_InvalidParameters() {
130 assertThatConfigFileThrownException("src/test/resources/parameters/InvalidParameters.json");
134 public void testParticipant_WrongJsonFormat() {
135 assertThatConfigFileThrownException("src/test/resources/parameters/Unreadable.json");
138 private void assertThatConfigFileThrownException(final String configFilePath) {
139 final String[] configParameters = new String[] {"-c", configFilePath};
140 assertThatThrownBy(() -> new Main(configParameters)).isInstanceOf(ControlLoopRuntimeException.class)
141 .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP));