Refactor CommandLineArguments classes
[policy/models.git] / models-sim / models-sim-dmaap / src / test / java / org / onap / policy / sim / dmaap / startstop / MainTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property.
4  * Modifications Copyright (C) 2021 Nordix Foundation.
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.sim.dmaap.startstop;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatCode;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
34 import org.onap.policy.models.sim.dmaap.rest.CommonRestServer;
35 import org.onap.policy.models.sim.dmaap.startstop.DmaapSimCommandLineArguments;
36 import org.onap.policy.models.sim.dmaap.startstop.Main;
37 import org.onap.policy.sim.dmaap.parameters.CommonTestData;
38
39 /**
40  * Class to perform unit test of {@link Main}}.
41  *
42  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
43  */
44 public class MainTest extends CommonRestServer {
45     private Main main;
46
47     /**
48      * Sets up.
49      */
50     @Before
51     public void setUp() {
52         main = null;
53         HttpServletServerFactoryInstance.getServerFactory().destroy();
54     }
55
56     /**
57      * Shuts "main" down.
58      *
59      * @throws Exception if an error occurs
60      */
61     @After
62     public void tearDown() throws Exception {
63         if (main != null) {
64             main.shutdown();
65         }
66     }
67
68     @Test
69     public void testMain() throws Exception {
70         CommonRestServer.reconfigure();
71         final String[] NormalParameters = {"-c", CONFIG_FILE};
72         main = new Main(NormalParameters);
73         assertTrue(main.getParameters().isValid());
74         assertEquals(CommonTestData.SIM_GROUP_NAME, main.getParameters().getName());
75
76         main.shutdown();
77     }
78
79     @Test
80     public void testMain_NoArguments() {
81         final String[] NormalParameters = {};
82         main = new Main(NormalParameters);
83         assertNull(main.getParameters());
84     }
85
86     @Test
87     public void testMain_InvalidArguments() throws Exception {
88         CommonRestServer.reconfigure();
89
90         // note: this is missing the "-c" argument, thus the ARGUMENTS are invalid
91         final String[] NormalParameters = {CONFIG_FILE};
92         main = new Main(NormalParameters);
93         assertNull(main.getParameters());
94     }
95
96     @Test
97     public void testMain_Help() {
98         final String[] NormalParameters = {"-h"};
99         assertThatCode(() -> Main.main(NormalParameters)).doesNotThrowAnyException();
100     }
101
102     @Test
103     public void testMain_InvalidParameters() {
104         final String[] NormalParameters = {"-c", "parameters/InvalidParameters.json"};
105         main = new Main(NormalParameters);
106         assertNull(main.getParameters());
107     }
108
109     @Test
110     public void testDmaapSimVersion() {
111         String[] testArgs = {"-v"};
112         DmaapSimCommandLineArguments sutArgs = new DmaapSimCommandLineArguments(testArgs);
113         assertThat(sutArgs.version()).startsWith("ONAP DMaaP simulator Service");
114     }
115 }