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.dcae.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.clamp.controlloop.participant.dcae.main.startstop.Main;
35 import org.onap.policy.clamp.controlloop.participant.dcae.main.startstop.ParticipantDcaeActivator;
36 import org.onap.policy.common.utils.resources.MessageConstants;
37 import org.onap.policy.common.utils.services.Registry;
40 * Class to perform unit test of {@link Main}}.
42 public class TestMain {
48 public static void setUp() {
49 Registry.newRegistry();
55 * @throws Exception if an error occurs
58 public static void tearDown() throws Exception {
59 // shut down activator
60 final ParticipantDcaeActivator activator =
61 Registry.getOrDefault(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR,
62 ParticipantDcaeActivator.class, null);
63 if (activator != null && activator.isAlive()) {
69 public void testMain_Help() {
70 final String[] configParameters = {"-h"};
71 Main main = new Main(configParameters);
72 assertFalse(main.isRunning());
76 public void testMain_Version() {
77 final String[] configParameters = {"-v"};
78 Main main = new Main(configParameters);
79 assertFalse(main.isRunning());
83 public void testMain_Valid() {
84 final String[] configParameters = {"-c", "src/test/resources/parameters/TestParameters.json"};
85 Main main = new Main(configParameters);
86 assertTrue(main.isRunning());
88 // ensure items were added to the registry
89 assertNotNull(Registry.get(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR, ParticipantDcaeActivator.class));
91 assertThatCode(() -> main.shutdown()).doesNotThrowAnyException();
93 assertFalse(main.isRunning());
97 public void testMain_NoParameter() {
98 assertThatConfigParameterThrownException(new String[] {});
102 public void testMain_FilePathNotDefined() {
103 assertThatConfigParameterThrownException(new String[] {"-c"});
107 public void testMain_TooManyCommand() {
108 assertThatConfigParameterThrownException(new String[] {"-h", "d"});
112 public void testMain_WrongParameter() {
113 assertThatConfigParameterThrownException(new String[] {"-d"});
116 private void assertThatConfigParameterThrownException(final String[] configParameters) {
117 assertThatThrownBy(() -> Main.main(configParameters)).isInstanceOf(ControlLoopRuntimeException.class)
118 .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP));
122 public void testParticipant_NoFileWithThisName() {
123 assertThatConfigFileThrownException("src/test/resources/parameters/NoFileWithThisName.json");
127 public void testParticipant_NotValidFile() {
128 assertThatConfigFileThrownException("src/test/resources/parameters");
132 public void testParticipant_NoParameters() {
133 assertThatConfigFileThrownException("src/test/resources/parameters/NoParameters.json");
137 public void testParticipant_InvalidParameters() {
138 assertThatConfigFileThrownException("src/test/resources/parameters/InvalidParameters.json");
142 public void testParticipant_WrongJsonFormat() {
143 assertThatConfigFileThrownException("src/test/resources/parameters/Unreadable.json");
146 private void assertThatConfigFileThrownException(final String configFilePath) {
147 final String[] configParameters = new String[] {"-c", configFilePath};
148 assertThatThrownBy(() -> new Main(configParameters)).isInstanceOf(ControlLoopRuntimeException.class)
149 .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP));