a83be4fb9561ecbf8853370e3b43e8f99a5de608
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / startstop / TestMain.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019-2020 Nordix Foundation.
4  * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
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.pap.main.startstop;
24
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30
31 import java.io.File;
32 import java.io.FileOutputStream;
33 import java.nio.charset.StandardCharsets;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
39 import org.onap.policy.common.utils.network.NetworkUtil;
40 import org.onap.policy.common.utils.resources.MessageConstants;
41 import org.onap.policy.common.utils.services.Registry;
42 import org.onap.policy.pap.main.PapConstants;
43 import org.onap.policy.pap.main.PolicyPapRuntimeException;
44 import org.onap.policy.pap.main.parameters.CommonTestData;
45
46 /**
47  * Class to perform unit test of {@link Main}}.
48  *
49  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
50  */
51 public class TestMain {
52     private static final String CONFIG_FILE = "src/test/resources/parameters/TestConfigParams.json";
53
54     private static int port;
55
56     private Main main;
57
58     /**
59      * Allocates a new DB name, server port, and creates a config file.
60      */
61     @BeforeClass
62     public static void setUpBeforeClass() throws Exception {
63         CommonTestData.newDb();
64         port = NetworkUtil.allocPort();
65
66         String json = new CommonTestData().getPapParameterGroupAsString(port);
67
68         File file = new File(CONFIG_FILE);
69         file.deleteOnExit();
70
71         try (FileOutputStream output = new FileOutputStream(file)) {
72             output.write(json.getBytes(StandardCharsets.UTF_8));
73         }
74     }
75
76     /**
77      * Set up.
78      */
79     @Before
80     public void setUp() throws Exception {
81         Registry.newRegistry();
82         HttpServletServerFactoryInstance.getServerFactory().destroy();
83     }
84
85     /**
86      * Shuts "main" down.
87      *
88      */
89     @After
90     public void tearDown() {
91         // shut down activator
92         PapActivator activator = Registry.getOrDefault(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class, null);
93         if (activator != null && activator.isAlive()) {
94             activator.stop();
95         }
96     }
97
98     private void testMainBody(String[] papConfigParameters) {
99         main = new Main(papConfigParameters);
100         assertTrue(main.getParameters().isValid());
101         assertEquals(CommonTestData.PAP_GROUP_NAME, main.getParameters().getName());
102
103         // ensure items were added to the registry
104         assertNotNull(Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class));
105         main.shutdown();
106     }
107
108     @Test
109     public void testMain() {
110         final String[] papConfigParameters = {"-c", CONFIG_FILE};
111         testMainBody(papConfigParameters);
112     }
113
114     @Test
115     public void testMainCustomGroup() {
116         final String[] papConfigParameters = {
117             "-c",
118             CONFIG_FILE,
119             "-g",
120             "parameters/PapDbGroup1.json"
121         };
122         testMainBody(papConfigParameters);
123     }
124
125     @Test
126     public void testMainPapDb() {
127         final String[] papConfigParameters = {
128             "-c",
129             CONFIG_FILE,
130             "-g",
131             "PapDb.json"
132         };
133         testMainBody(papConfigParameters);
134     }
135
136     @Test
137     public void testMain_NoArguments() {
138         final String[] papConfigParameters = {};
139         assertThatThrownBy(() -> new Main(papConfigParameters)).isInstanceOf(PolicyPapRuntimeException.class)
140             .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_PAP));
141     }
142
143     @Test
144     public void testMain_InvalidArguments() {
145         final String[] papConfigParameters = {CONFIG_FILE};
146         assertThatThrownBy(() -> new Main(papConfigParameters)).isInstanceOf(PolicyPapRuntimeException.class)
147             .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_PAP));
148     }
149
150     @Test
151     public void testMain_Help() {
152         final String[] papConfigParameters = {"-h"};
153         main = new Main(papConfigParameters);
154         assertNull(main.getParameters());
155     }
156
157     @Test
158     public void testMain_InvalidParameters() {
159         final String[] papConfigParameters = {"-c", "parameters/PapConfigParameters_InvalidName.json"};
160         assertThatThrownBy(() -> new Main(papConfigParameters)).isInstanceOf(PolicyPapRuntimeException.class)
161             .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_PAP));
162     }
163 }