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