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.simulator.main.parameters;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
28 import java.io.FileNotFoundException;
29 import org.apache.commons.io.DirectoryWalker.CancelException;
30 import org.junit.Test;
31 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
32 import org.onap.policy.clamp.controlloop.participant.simulator.main.startstop.ParticipantSimulatorCommandLineArguments;
33 import org.onap.policy.common.utils.coder.CoderException;
36 * Class to perform unit test of {@link ParticipantParameterHandler}.
38 public class TestParticipantSimulatorParameterHandler {
41 public void testParameterHandlerNoParameterFile() throws ControlLoopException {
42 final String[] emptyArgumentString = { "-c", "src/test/resources/parameters/NoParametersFile.json" };
44 final ParticipantSimulatorCommandLineArguments emptyArguments = new ParticipantSimulatorCommandLineArguments();
45 emptyArguments.parse(emptyArgumentString);
47 assertThatThrownBy(() -> new ParticipantSimulatorParameterHandler().getParameters(emptyArguments))
48 .hasCauseInstanceOf(CoderException.class)
49 .hasRootCauseInstanceOf(FileNotFoundException.class);
53 public void testParameterHandlerInvalidParameters() throws ControlLoopException {
54 final String[] invalidArgumentString = { "-c", "src/test/resources/parameters/InvalidParameters.json" };
56 final ParticipantSimulatorCommandLineArguments invalidArguments =
57 new ParticipantSimulatorCommandLineArguments();
58 invalidArguments.parse(invalidArgumentString);
60 assertThatThrownBy(() -> new ParticipantSimulatorParameterHandler().getParameters(invalidArguments))
61 .hasMessageStartingWith("error reading parameters from")
62 .hasCauseInstanceOf(CoderException.class);
66 public void testParameterHandlerNoParameters() throws CancelException, ControlLoopException {
67 final String[] noArgumentString = { "-c", "src/test/resources/parameters/EmptyParameters.json" };
69 final ParticipantSimulatorCommandLineArguments noArguments = new ParticipantSimulatorCommandLineArguments();
70 noArguments.parse(noArgumentString);
72 assertThatThrownBy(() -> new ParticipantSimulatorParameterHandler().getParameters(noArguments))
73 .hasMessageContaining("no parameters found");
77 public void testParticipantParameterGroup() throws ControlLoopException {
78 final String[] participantConfigParameters = { "-c", "src/test/resources/parameters/TestParameters.json"};
80 final ParticipantSimulatorCommandLineArguments arguments = new ParticipantSimulatorCommandLineArguments();
81 arguments.parse(participantConfigParameters);
83 final ParticipantSimulatorParameters parGroup = new ParticipantSimulatorParameterHandler()
84 .getParameters(arguments);
85 assertTrue(arguments.checkSetConfigurationFilePath());
86 assertEquals(CommonTestData.PARTICIPANT_GROUP_NAME, parGroup.getName());
90 public void testParticipantVersion() throws ControlLoopException {
91 final String[] participantConfigParameters = { "-v" };
92 final ParticipantSimulatorCommandLineArguments arguments = new ParticipantSimulatorCommandLineArguments();
93 assertThat(arguments.parse(participantConfigParameters)).startsWith(
94 "ONAP Tosca defined control loop Participant");
98 public void testParticipantHelp() throws ControlLoopException {
99 final String[] participantConfigParameters = { "-h" };
100 final ParticipantSimulatorCommandLineArguments arguments = new ParticipantSimulatorCommandLineArguments();
101 assertThat(arguments.parse(participantConfigParameters)).startsWith("usage:");
105 public void testParticipant_TooManyArguments() throws ControlLoopException {
106 final String[] participantConfigParameters = { "-c", "src/test/resources/parameters/TestParameters.json",
108 final ParticipantSimulatorCommandLineArguments arguments = new ParticipantSimulatorCommandLineArguments();
109 assertThatThrownBy(() -> arguments.parse(participantConfigParameters))
110 .hasMessageStartingWith("too many command line arguments specified");
114 public void testParticipantInvalidOption() throws ControlLoopException {
115 final String[] participantConfigParameters = { "-d" };
116 final ParticipantSimulatorCommandLineArguments arguments = new ParticipantSimulatorCommandLineArguments();
117 assertThatThrownBy(() -> arguments.parse(participantConfigParameters))
118 .hasMessageStartingWith("invalid command line arguments specified");