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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pap.main.startstop;
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;
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;
47 * Class to perform unit test of {@link Main}}.
49 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
51 public class TestMain {
52 private static final String CONFIG_FILE = "src/test/resources/parameters/TestConfigParams.json";
54 private static int port;
59 * Allocates a new DB name, server port, and creates a config file.
62 public static void setUpBeforeClass() throws Exception {
63 CommonTestData.newDb();
64 port = NetworkUtil.allocPort();
66 String json = new CommonTestData().getPapParameterGroupAsString(port);
68 File file = new File(CONFIG_FILE);
71 try (FileOutputStream output = new FileOutputStream(file)) {
72 output.write(json.getBytes(StandardCharsets.UTF_8));
80 public void setUp() throws Exception {
81 Registry.newRegistry();
82 HttpServletServerFactoryInstance.getServerFactory().destroy();
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()) {
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());
103 // ensure items were added to the registry
104 assertNotNull(Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class));
109 public void testMain() {
110 final String[] papConfigParameters = {"-c", CONFIG_FILE};
111 testMainBody(papConfigParameters);
115 public void testMainCustomGroup() {
116 final String[] papConfigParameters = {
120 "parameters/PapDbGroup1.json"
122 testMainBody(papConfigParameters);
126 public void testMainPapDb() {
127 final String[] papConfigParameters = {
133 testMainBody(papConfigParameters);
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));
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));
151 public void testMain_Help() {
152 final String[] papConfigParameters = {"-h"};
153 main = new Main(papConfigParameters);
154 assertNull(main.getParameters());
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));