Changes to make Policy-API container crash with non zero exitCode
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / startstop / TestMain.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.api.main.startstop;
24
25 import static org.assertj.core.api.Assertions.assertThatCode;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertTrue;
29
30 import org.junit.Test;
31 import org.onap.policy.api.main.exception.PolicyApiRuntimeException;
32 import org.onap.policy.api.main.parameters.CommonTestData;
33 import org.onap.policy.common.utils.network.NetworkUtil;
34 import org.onap.policy.common.utils.resources.MessageConstants;
35
36 /**
37  * Class to perform unit test of Main.
38  *
39  */
40 public class TestMain {
41     private static final CommonTestData COMMON_TEST_DATA = new CommonTestData();
42
43     @Test
44     public void testMain() throws Exception {
45         COMMON_TEST_DATA.makeParameters("src/test/resources/parameters/ApiConfigParameters_Https.json",
46                 "src/test/resources/parameters/ApiConfigParametersXXX.json", NetworkUtil.allocPort());
47         final String[] apiConfigParameters = {"-c", "src/test/resources/parameters/ApiConfigParametersXXX.json"};
48         final Main main = new Main(apiConfigParameters);
49         assertTrue(main.getParameters().isValid());
50         assertEquals(CommonTestData.API_GROUP_NAME, main.getParameters().getName());
51         main.shutdown();
52     }
53
54     @Test
55     public void testMain_NoArguments() {
56         final String[] apiConfigParameters = {};
57         assertThatThrownBy(() -> new Main(apiConfigParameters)).isInstanceOf(PolicyApiRuntimeException.class)
58             .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_API));
59     }
60
61     @Test
62     public void testMain_InvalidArguments() {
63         final String[] apiConfigParameters = {"parameters/ApiConfigParameters.json"};
64         assertThatThrownBy(() -> new Main(apiConfigParameters)).isInstanceOf(PolicyApiRuntimeException.class)
65             .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_API));
66     }
67
68     @Test
69     public void testMain_Help() {
70         final String[] apiConfigParameters = {"-h"};
71         assertThatCode(() -> Main.main(apiConfigParameters)).doesNotThrowAnyException();
72     }
73
74     @Test
75     public void testMain_InvalidParameters() {
76         final String[] apiConfigParameters = {"-c", "parameters/ApiConfigParameters_InvalidName.json"};
77         assertThatThrownBy(() -> new Main(apiConfigParameters)).isInstanceOf(PolicyApiRuntimeException.class)
78             .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_API));
79     }
80 }