f77b678c06fe19b31c10edc2e779f09304338807
[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.policy.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 import org.onap.policy.common.utils.services.Registry;
36
37 public class TestMain {
38
39     /**
40      * Set up.
41      */
42     @BeforeClass
43     public static void setUp() {
44         Registry.newRegistry();
45     }
46
47     /**
48      * Shuts "main" down.
49      *
50      * @throws Exception if an error occurs
51      */
52     @AfterClass
53     public static void tearDown() throws Exception {
54         // shut down activator
55         final ParticipantPolicyActivator activator =
56             Registry.getOrDefault(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR,
57                             ParticipantPolicyActivator.class, null);
58         if (activator != null && activator.isAlive()) {
59             activator.shutdown();
60         }
61     }
62
63     @Test
64     public void testMain_Help() {
65         final String[] configParameters = {"-h"};
66         Main main = new Main(configParameters);
67         assertFalse(main.isRunning());
68     }
69
70     @Test
71     public void testMain_Version() {
72         final String[] configParameters = {"-v"};
73         Main main = new Main(configParameters);
74         assertFalse(main.isRunning());
75     }
76
77     @Test
78     public void testMain_Valid() {
79         final String[] configParameters = {"-c", "src/test/resources/parameters/TestParameters.json"};
80         Main main = new Main(configParameters);
81         assertTrue(main.isRunning());
82
83         assertThatCode(() -> main.shutdown()).doesNotThrowAnyException();
84
85         assertFalse(main.isRunning());
86     }
87
88     @Test
89     public void testMain_NoParameter() {
90         assertThatConfigParameterThrownException(new String[] {});
91     }
92
93     @Test
94     public void testMain_FilePathNotDefined() {
95         assertThatConfigParameterThrownException(new String[] {"-c"});
96     }
97
98     @Test
99     public void testMain_TooManyCommand() {
100         assertThatConfigParameterThrownException(new String[] {"-h", "d"});
101     }
102
103     @Test
104     public void testMain_WrongParameter() {
105         assertThatConfigParameterThrownException(new String[] {"-d"});
106     }
107
108     private void assertThatConfigParameterThrownException(final String[] configParameters) {
109         assertThatThrownBy(() -> Main.main(configParameters)).isInstanceOf(ControlLoopRuntimeException.class)
110                 .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP));
111     }
112
113     @Test
114     public void testParticipant_NoFileWithThisName() {
115         assertThatConfigFileThrownException("src/test/resources/parameters/NoFileWithThisName.json");
116     }
117
118     @Test
119     public void testParticipant_NotValidFile() {
120         assertThatConfigFileThrownException("src/test/resources/parameters");
121     }
122
123     @Test
124     public void testParticipant_NoParameters() {
125         assertThatConfigFileThrownException("src/test/resources/parameters/NoParameters.json");
126     }
127
128     @Test
129     public void testParticipant_InvalidParameters() {
130         assertThatConfigFileThrownException("src/test/resources/parameters/InvalidParameters.json");
131     }
132
133     @Test
134     public void testParticipant_WrongJsonFormat() {
135         assertThatConfigFileThrownException("src/test/resources/parameters/Unreadable.json");
136     }
137
138     private void assertThatConfigFileThrownException(final String configFilePath) {
139         final String[] configParameters = new String[] {"-c", configFilePath};
140         assertThatThrownBy(() -> new Main(configParameters)).isInstanceOf(ControlLoopRuntimeException.class)
141                 .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP));
142     }
143 }