5a5ad89313173a0e559210bce42a448242cabf6d
[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.startstop;
22
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.assertTrue;
27
28 import org.junit.Test;
29 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
30 import org.onap.policy.common.utils.resources.MessageConstants;
31
32 /**
33  * Class to perform unit test of {@link Main}}.
34  */
35 public class TestMain {
36
37     @Test
38     public void testMain_Help() {
39         final String[] configParameters = {"-h"};
40         Main main = new Main(configParameters);
41         assertFalse(main.isRunning());
42     }
43
44     @Test
45     public void testMain_Version() {
46         final String[] configParameters = {"-v"};
47         Main main = new Main(configParameters);
48         assertFalse(main.isRunning());
49     }
50
51     @Test
52     public void testMain_Valid() {
53         final String[] configParameters = {"-c", "src/test/resources/parameters/TestParameters.json"};
54         Main main = new Main(configParameters);
55         assertTrue(main.isRunning());
56
57         assertThatCode(() -> main.shutdown()).doesNotThrowAnyException();
58
59         assertFalse(main.isRunning());
60     }
61
62     @Test
63     public void testMain_NoParameter() {
64         assertThatConfigParameterThrownException(new String[] {});
65     }
66
67     @Test
68     public void testMain_FilePathNotDefined() {
69         assertThatConfigParameterThrownException(new String[] {"-c"});
70     }
71
72     @Test
73     public void testMain_TooManyCommand() {
74         assertThatConfigParameterThrownException(new String[] {"-h", "d"});
75     }
76
77     @Test
78     public void testMain_WrongParameter() {
79         assertThatConfigParameterThrownException(new String[] {"-d"});
80     }
81
82     private void assertThatConfigParameterThrownException(final String[] configParameters) {
83         assertThatThrownBy(() -> Main.main(configParameters)).isInstanceOf(ControlLoopRuntimeException.class)
84                 .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP));
85     }
86
87     @Test
88     public void testParticipant_NoFileWithThisName() {
89         assertThatConfigFileThrownException("src/test/resources/parameters/NoFileWithThisName.json");
90     }
91
92     @Test
93     public void testParticipant_NotValidFile() {
94         assertThatConfigFileThrownException("src/test/resources/parameters");
95     }
96
97     @Test
98     public void testParticipant_FileEmpty() {
99         assertThatConfigFileThrownException("src/test/resources/parameters/EmptyParameters.json");
100     }
101
102     @Test
103     public void testParticipant_NoParameters() {
104         assertThatConfigFileThrownException("src/test/resources/parameters/NoParameters.json");
105     }
106
107     @Test
108     public void testParticipant_InvalidParameters() {
109         assertThatConfigFileThrownException("src/test/resources/parameters/InvalidParameters.json");
110     }
111
112     private void assertThatConfigFileThrownException(final String configFilePath) {
113         final String[] configParameters = new String[] {"-c", configFilePath};
114         assertThatThrownBy(() -> new Main(configParameters)).isInstanceOf(ControlLoopRuntimeException.class)
115                 .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP));
116     }
117 }