Fix some sonars in policy-models
[policy/models.git] / models-sim / policy-models-sim-pdp / src / test / java / org / onap / policy / models / sim / pdp / TestPdpSimulatorMain.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.sim.pdp;
23
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.common.utils.services.Registry;
34 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException;
35 import org.onap.policy.models.sim.pdp.parameters.CommonTestData;
36
37 /**
38  * Class to perform unit test of {@link PdpSimulatorMain}}.
39  *
40  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
41  */
42 public class TestPdpSimulatorMain {
43     private PdpSimulatorMain pdpSimulator;
44
45     /**
46      * Set up.
47      */
48     @Before
49     public void setUp() {
50         Registry.newRegistry();
51     }
52
53     /**
54      * Shuts "main" down.
55      *
56      * @throws Exception if an error occurs
57      */
58     @After
59     public void tearDown() throws Exception {
60         // shut down activator
61         final PdpSimulatorActivator activator = Registry.getOrDefault(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR,
62                 PdpSimulatorActivator.class, null);
63         if (activator != null && activator.isAlive()) {
64             activator.terminate();
65         }
66     }
67
68     @Test
69     public void testPdpSimulator() throws PdpSimulatorException {
70         final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json" };
71         pdpSimulator = new PdpSimulatorMain(pdpSimulatorConfigParameters);
72         assertTrue(pdpSimulator.getParameters().isValid());
73         assertEquals(CommonTestData.PDP_SIMULATOR_GROUP_NAME, pdpSimulator.getParameters().getName());
74
75         // ensure items were added to the registry
76         assertNotNull(Registry.get(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, PdpSimulatorActivator.class));
77
78         pdpSimulator.shutdown();
79     }
80
81     @Test
82     public void testPdpSimulator_NoArguments() {
83         final String[] pdpSimulatorConfigParameters = {};
84         pdpSimulator = new PdpSimulatorMain(pdpSimulatorConfigParameters);
85         assertNull(pdpSimulator.getParameters());
86     }
87
88     @Test
89     public void testPdpSimulator_InvalidArguments() {
90         final String[] pdpSimulatorConfigParameters = { "src/test/resourcesPdpSimulatorConfigParameters.json" };
91         pdpSimulator = new PdpSimulatorMain(pdpSimulatorConfigParameters);
92         assertNull(pdpSimulator.getParameters());
93     }
94
95     @Test
96     public void testPdpSimulator_Help() {
97         final String[] pdpSimulatorConfigParameters = { "-h" };
98         assertThatCode(() -> PdpSimulatorMain.main(pdpSimulatorConfigParameters)).doesNotThrowAnyException();
99     }
100
101     @Test
102     public void testPdpSimulator_InvalidParameters() {
103         final String[] pdpSimulatorConfigParameters =
104             { "-c", "src/test/resources/PdpSimulatorConfigParameters_InvalidName.json" };
105         pdpSimulator = new PdpSimulatorMain(pdpSimulatorConfigParameters);
106         assertNull(pdpSimulator.getParameters());
107     }
108 }