f779f3a57be41c9d5ef700aa3a3c9ab4e7975e36
[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.dcae.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.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;
38
39 /**
40  * Class to perform unit test of {@link Main}}.
41  */
42 public class TestMain {
43
44     /**
45      * Set up.
46      */
47     @BeforeClass
48     public static void setUp() {
49         Registry.newRegistry();
50     }
51
52     /**
53      * Shuts "main" down.
54      *
55      * @throws Exception if an error occurs
56      */
57     @AfterClass
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()) {
64             activator.shutdown();
65         }
66     }
67
68     @Test
69     public void testMain_Help() {
70         final String[] configParameters = {"-h"};
71         Main main = new Main(configParameters);
72         assertFalse(main.isRunning());
73     }
74
75     @Test
76     public void testMain_Version() {
77         final String[] configParameters = {"-v"};
78         Main main = new Main(configParameters);
79         assertFalse(main.isRunning());
80     }
81
82     @Test
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());
87
88         // ensure items were added to the registry
89         assertNotNull(Registry.get(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR, ParticipantDcaeActivator.class));
90
91         assertThatCode(() -> main.shutdown()).doesNotThrowAnyException();
92
93         assertFalse(main.isRunning());
94     }
95
96     @Test
97     public void testMain_NoParameter() {
98         assertThatConfigParameterThrownException(new String[] {});
99     }
100
101     @Test
102     public void testMain_FilePathNotDefined() {
103         assertThatConfigParameterThrownException(new String[] {"-c"});
104     }
105
106     @Test
107     public void testMain_TooManyCommand() {
108         assertThatConfigParameterThrownException(new String[] {"-h", "d"});
109     }
110
111     @Test
112     public void testMain_WrongParameter() {
113         assertThatConfigParameterThrownException(new String[] {"-d"});
114     }
115
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));
119     }
120
121     @Test
122     public void testParticipant_NoFileWithThisName() {
123         assertThatConfigFileThrownException("src/test/resources/parameters/NoFileWithThisName.json");
124     }
125
126     @Test
127     public void testParticipant_NotValidFile() {
128         assertThatConfigFileThrownException("src/test/resources/parameters");
129     }
130
131     @Test
132     public void testParticipant_NoParameters() {
133         assertThatConfigFileThrownException("src/test/resources/parameters/NoParameters.json");
134     }
135
136     @Test
137     public void testParticipant_InvalidParameters() {
138         assertThatConfigFileThrownException("src/test/resources/parameters/InvalidParameters.json");
139     }
140
141     @Test
142     public void testParticipant_WrongJsonFormat() {
143         assertThatConfigFileThrownException("src/test/resources/parameters/Unreadable.json");
144     }
145
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));
150     }
151 }