Fix issues in api for new sonar rules
[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  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.api.main.startstop;
23
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28
29 import org.junit.Test;
30 import org.onap.policy.api.main.parameters.CommonTestData;
31 import org.onap.policy.common.utils.network.NetworkUtil;
32
33 /**
34  * Class to perform unit test of Main.
35  *
36  */
37 public class TestMain {
38     private static final CommonTestData COMMON_TEST_DATA = new CommonTestData();
39
40     @Test
41     public void testMain() throws Exception {
42         COMMON_TEST_DATA.makeParameters("src/test/resources/parameters/ApiConfigParameters_Https.json",
43                 "src/test/resources/parameters/ApiConfigParametersXXX.json", NetworkUtil.allocPort());
44         final String[] apiConfigParameters = {"-c", "src/test/resources/parameters/ApiConfigParametersXXX.json"};
45         final Main main = new Main(apiConfigParameters);
46         assertTrue(main.getParameters().isValid());
47         assertEquals(CommonTestData.API_GROUP_NAME, main.getParameters().getName());
48         main.shutdown();
49     }
50
51     @Test
52     public void testMain_NoArguments() {
53         final String[] apiConfigParameters = {};
54         final Main main = new Main(apiConfigParameters);
55         assertNull(main.getParameters());
56     }
57
58     @Test
59     public void testMain_InvalidArguments() {
60         final String[] apiConfigParameters = {"parameters/ApiConfigParameters.json"};
61         final Main main = new Main(apiConfigParameters);
62         assertNull(main.getParameters());
63     }
64
65     @Test
66     public void testMain_Help() {
67         final String[] apiConfigParameters = {"-h"};
68         assertThatCode(() -> Main.main(apiConfigParameters)).doesNotThrowAnyException();
69     }
70
71     @Test
72     public void testMain_InvalidParameters() {
73         final String[] apiConfigParameters = {"-c", "parameters/ApiConfigParameters_InvalidName.json"};
74         final Main main = new Main(apiConfigParameters);
75         assertNull(main.getParameters());
76     }
77 }