Using standard success/failure messages in PAP
[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.PolicyPapException;
39 import org.onap.policy.pap.main.PolicyPapRuntimeException;
40 import org.onap.policy.pap.main.parameters.CommonTestData;
41
42 /**
43  * Class to perform unit test of {@link Main}}.
44  *
45  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
46  */
47 public class TestMain {
48     private Main main;
49
50     /**
51      * Set up.
52      */
53     @Before
54     public void setUp() {
55         Registry.newRegistry();
56         HttpServletServerFactoryInstance.getServerFactory().destroy();
57     }
58
59     /**
60      * Shuts "main" down.
61      *
62      * @throws Exception if an error occurs
63      */
64     @After
65     public void tearDown() throws Exception {
66         // shut down activator
67         PapActivator activator = Registry.getOrDefault(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class, null);
68         if (activator != null && activator.isAlive()) {
69             activator.stop();
70         }
71     }
72
73     @Test
74     public void testMain() throws PolicyPapException {
75         final String[] papConfigParameters = {"-c", "parameters/PapConfigParameters.json"};
76         main = new Main(papConfigParameters);
77         assertTrue(main.getParameters().isValid());
78         assertEquals(CommonTestData.PAP_GROUP_NAME, main.getParameters().getName());
79
80         // ensure items were added to the registry
81         assertNotNull(Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class));
82
83         main.shutdown();
84     }
85
86     @Test
87     public void testMain_NoArguments() {
88         final String[] papConfigParameters = {};
89         assertThatThrownBy(() -> new Main(papConfigParameters)).isInstanceOf(PolicyPapRuntimeException.class)
90             .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_PAP));
91     }
92
93     @Test
94     public void testMain_InvalidArguments() {
95         final String[] papConfigParameters = {"parameters/PapConfigParameters.json"};
96         assertThatThrownBy(() -> new Main(papConfigParameters)).isInstanceOf(PolicyPapRuntimeException.class)
97             .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_PAP));
98     }
99
100     @Test
101     public void testMain_Help() {
102         final String[] papConfigParameters = {"-h"};
103         main = new Main(papConfigParameters);
104         assertNull(main.getParameters());
105     }
106
107     @Test
108     public void testMain_InvalidParameters() {
109         final String[] papConfigParameters = {"-c", "parameters/PapConfigParameters_InvalidName.json"};
110         assertThatThrownBy(() -> new Main(papConfigParameters)).isInstanceOf(PolicyPapRuntimeException.class)
111             .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_PAP));
112     }
113 }