e94939af88376d3b7aa3459fe44137eebd2176b6
[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.participant.simulator.main.parameters;
22
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;
27
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;
34
35 /**
36  * Class to perform unit test of {@link ParticipantParameterHandler}.
37  */
38 public class TestParticipantSimulatorParameterHandler {
39
40     @Test
41     public void testParameterHandlerNoParameterFile() throws ControlLoopException {
42         final String[] emptyArgumentString = { "-c", "src/test/resources/parameters/NoParametersFile.json" };
43
44         final ParticipantSimulatorCommandLineArguments emptyArguments = new ParticipantSimulatorCommandLineArguments();
45         emptyArguments.parse(emptyArgumentString);
46
47         assertThatThrownBy(() -> new ParticipantSimulatorParameterHandler().getParameters(emptyArguments))
48             .hasCauseInstanceOf(CoderException.class)
49             .hasRootCauseInstanceOf(FileNotFoundException.class);
50     }
51
52     @Test
53     public void testParameterHandlerInvalidParameters() throws ControlLoopException {
54         final String[] invalidArgumentString = { "-c", "src/test/resources/parameters/InvalidParameters.json" };
55
56         final ParticipantSimulatorCommandLineArguments invalidArguments =
57                 new ParticipantSimulatorCommandLineArguments();
58         invalidArguments.parse(invalidArgumentString);
59
60         assertThatThrownBy(() -> new ParticipantSimulatorParameterHandler().getParameters(invalidArguments))
61             .hasMessageStartingWith("error reading parameters from")
62             .hasCauseInstanceOf(CoderException.class);
63     }
64
65     @Test
66     public void testParameterHandlerNoParameters() throws CancelException, ControlLoopException {
67         final String[] noArgumentString = { "-c", "src/test/resources/parameters/EmptyParameters.json" };
68
69         final ParticipantSimulatorCommandLineArguments noArguments = new ParticipantSimulatorCommandLineArguments();
70         noArguments.parse(noArgumentString);
71
72         assertThatThrownBy(() -> new ParticipantSimulatorParameterHandler().getParameters(noArguments))
73             .hasMessageContaining("no parameters found");
74     }
75
76     @Test
77     public void testParticipantParameterGroup() throws ControlLoopException {
78         final String[] participantConfigParameters = { "-c", "src/test/resources/parameters/TestParameters.json"};
79
80         final ParticipantSimulatorCommandLineArguments arguments = new ParticipantSimulatorCommandLineArguments();
81         arguments.parse(participantConfigParameters);
82
83         final ParticipantSimulatorParameters parGroup = new ParticipantSimulatorParameterHandler()
84                 .getParameters(arguments);
85         assertTrue(arguments.checkSetConfigurationFilePath());
86         assertEquals(CommonTestData.PARTICIPANT_GROUP_NAME, parGroup.getName());
87     }
88
89     @Test
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");
95     }
96
97     @Test
98     public void testParticipantHelp() throws ControlLoopException {
99         final String[] participantConfigParameters = { "-h" };
100         final ParticipantSimulatorCommandLineArguments arguments = new ParticipantSimulatorCommandLineArguments();
101         assertThat(arguments.parse(participantConfigParameters)).startsWith("usage:");
102     }
103
104     @Test
105     public void testParticipant_TooManyArguments() throws ControlLoopException {
106         final String[] participantConfigParameters = { "-c", "src/test/resources/parameters/TestParameters.json",
107                                                        "TooMany"};
108         final ParticipantSimulatorCommandLineArguments arguments = new ParticipantSimulatorCommandLineArguments();
109         assertThatThrownBy(() -> arguments.parse(participantConfigParameters))
110             .hasMessageStartingWith("too many command line arguments specified");
111     }
112
113     @Test
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");
119     }
120 }