support external configuration of pdp groups
[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 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 org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
35 import org.onap.policy.common.utils.resources.MessageConstants;
36 import org.onap.policy.common.utils.services.Registry;
37 import org.onap.policy.pap.main.PapConstants;
38 import org.onap.policy.pap.main.PolicyPapRuntimeException;
39 import org.onap.policy.pap.main.parameters.CommonTestData;
40
41 /**
42  * Class to perform unit test of {@link Main}}.
43  *
44  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
45  */
46 public class TestMain {
47     private Main main;
48
49     /**
50      * Set up.
51      */
52     @Before
53     public void setUp() {
54         Registry.newRegistry();
55         HttpServletServerFactoryInstance.getServerFactory().destroy();
56     }
57
58     /**
59      * Shuts "main" down.
60      *
61      */
62     @After
63     public void tearDown() {
64         // shut down activator
65         PapActivator activator = Registry.getOrDefault(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class, null);
66         if (activator != null && activator.isAlive()) {
67             activator.stop();
68         }
69     }
70
71     private void testMainBody(String[] papConfigParameters) {
72         main = new Main(papConfigParameters);
73         assertTrue(main.getParameters().isValid());
74         assertEquals(CommonTestData.PAP_GROUP_NAME, main.getParameters().getName());
75
76         // ensure items were added to the registry
77         assertNotNull(Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class));
78         main.shutdown();
79     }
80
81     @Test
82     public void testMain() {
83         final String[] papConfigParameters = {"-c", "parameters/PapConfigParameters.json"};
84         testMainBody(papConfigParameters);
85     }
86
87     @Test
88     public void testMainCustomGroup() {
89         final String[] papConfigParameters = {
90             "-c",
91             "parameters/PapConfigParameters.json",
92             "-g",
93             "parameters/PapDbGroup1.json"
94         };
95         testMainBody(papConfigParameters);
96     }
97
98     @Test
99     public void testMainPapDb() {
100         final String[] papConfigParameters = {
101             "-c",
102             "parameters/PapConfigParameters.json",
103             "-g",
104             "PapDb.json"
105         };
106         testMainBody(papConfigParameters);
107     }
108
109     @Test
110     public void testMain_NoArguments() {
111         final String[] papConfigParameters = {};
112         assertThatThrownBy(() -> new Main(papConfigParameters)).isInstanceOf(PolicyPapRuntimeException.class)
113             .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_PAP));
114     }
115
116     @Test
117     public void testMain_InvalidArguments() {
118         final String[] papConfigParameters = {"parameters/PapConfigParameters.json"};
119         assertThatThrownBy(() -> new Main(papConfigParameters)).isInstanceOf(PolicyPapRuntimeException.class)
120             .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_PAP));
121     }
122
123     @Test
124     public void testMain_Help() {
125         final String[] papConfigParameters = {"-h"};
126         main = new Main(papConfigParameters);
127         assertNull(main.getParameters());
128     }
129
130     @Test
131     public void testMain_InvalidParameters() {
132         final String[] papConfigParameters = {"-c", "parameters/PapConfigParameters_InvalidName.json"};
133         assertThatThrownBy(() -> new Main(papConfigParameters)).isInstanceOf(PolicyPapRuntimeException.class)
134             .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_PAP));
135     }
136 }