Refactor xacml-pdp to remove various statics
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / startstop / TestMain.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdpx.main.startstop;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24
25 import org.junit.After;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.onap.policy.pdpx.main.CommonRest;
31 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
32
33 /**
34  * Class to perform unit test of Main.
35  *
36  */
37 public class TestMain extends CommonRest {
38
39     private Main main;
40
41     /**
42      * Sets up properties and configuration.
43      * @throws Exception if an error occurs
44      */
45     @BeforeClass
46     public static void setUpBeforeClass() throws Exception {
47         CommonRest.setUpBeforeClass();
48
49         // don't want the common "main" running
50         CommonRest.stopMain();
51     }
52
53     @Before
54     public void setUp() {
55         main = null;
56     }
57
58     /**
59      * Shuts "main" down.
60      */
61     @After
62     public void tearDown() {
63         if (main != null) {
64             main.shutdown();
65         }
66     }
67
68     @Test
69     public void testMain() throws PolicyXacmlPdpException {
70         final String[] xacmlPdpConfigParameters = {"-c", CONFIG_FILE, "-p", "parameters/topic.properties"};
71         main = new Main(xacmlPdpConfigParameters);
72         main.shutdown();
73         main = null;
74     }
75
76     @Test
77     public void testMain_NoArguments() {
78         final String[] xacmlPdpConfigParameters = {};
79         assertThatThrownBy(() -> new Main(xacmlPdpConfigParameters)).isInstanceOf(PolicyXacmlPdpException.class)
80                         .hasMessage("policy xacml pdp configuration file was not specified as an argument");
81     }
82
83     @Test
84     public void testMain_InvalidArguments() {
85         final String[] xacmlPdpConfigParameters = {"parameters/XacmlPdpConfigParameters.json"};
86         assertThatThrownBy(() -> new Main(xacmlPdpConfigParameters)).isInstanceOf(PolicyXacmlPdpException.class)
87             .hasMessage("too many command line arguments specified : [parameters/XacmlPdpConfigParameters.json]");
88     }
89
90     @Test
91     public void testMain_Help() throws PolicyXacmlPdpException {
92         final String[] xacmlPdpConfigParameters = {"-h"};
93         Assert.assertTrue(new Main(xacmlPdpConfigParameters).getArgumentMessage().contains("-h,--help"));
94
95     }
96
97     @Test
98     public void testMain_InvalidParameters()  {
99         final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters_InvalidName.json"};
100         assertThatThrownBy(() -> new Main(xacmlPdpConfigParameters)).isInstanceOf(PolicyXacmlPdpException.class)
101                         .hasMessageContaining("validation error");
102     }
103 }