de0181b707c0c28f129b0369c9f37c42b69bd35d
[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.assertTrue;
28
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.common.utils.services.Registry;
33 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException;
34 import org.onap.policy.models.sim.pdp.parameters.CommonTestData;
35
36 /**
37  * Class to perform unit test of {@link PdpSimulatorMain}}.
38  *
39  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
40  */
41 public class TestPdpSimulatorMain {
42     private PdpSimulatorMain pdpSimulator;
43
44     /**
45      * Set up.
46      */
47     @Before
48     public void setUp() {
49         Registry.newRegistry();
50     }
51
52     /**
53      * Shuts "main" down.
54      *
55      * @throws Exception if an error occurs
56      */
57     @After
58     public void tearDown() throws Exception {
59         // shut down activator
60         final PdpSimulatorActivator activator = Registry.getOrDefault(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR,
61                 PdpSimulatorActivator.class, null);
62         if (activator != null && activator.isAlive()) {
63             activator.terminate();
64         }
65     }
66
67     @Test
68     public void testPdpSimulator() throws PdpSimulatorException {
69         final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json" };
70         pdpSimulator = new PdpSimulatorMain(pdpSimulatorConfigParameters);
71         assertTrue(pdpSimulator.getParameters().isValid());
72         assertEquals(CommonTestData.PDP_SIMULATOR_GROUP_NAME, pdpSimulator.getParameters().getName());
73
74         // ensure items were added to the registry
75         assertNotNull(Registry.get(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, PdpSimulatorActivator.class));
76
77         pdpSimulator.shutdown();
78     }
79
80     @Test
81     public void testPdpSimulator_NoArguments() {
82         final String[] pdpSimulatorConfigParameters = {};
83         pdpSimulator = new PdpSimulatorMain(pdpSimulatorConfigParameters);
84         assertTrue(pdpSimulator.getParameters() == null);
85     }
86
87     @Test
88     public void testPdpSimulator_InvalidArguments() {
89         final String[] pdpSimulatorConfigParameters = { "src/test/resourcesPdpSimulatorConfigParameters.json" };
90         pdpSimulator = new PdpSimulatorMain(pdpSimulatorConfigParameters);
91         assertTrue(pdpSimulator.getParameters() == null);
92     }
93
94     @Test
95     public void testPdpSimulator_Help() {
96         final String[] pdpSimulatorConfigParameters = { "-h" };
97         assertThatCode(() -> PdpSimulatorMain.main(pdpSimulatorConfigParameters)).doesNotThrowAnyException();
98     }
99
100     @Test
101     public void testPdpSimulator_InvalidParameters() {
102         final String[] pdpSimulatorConfigParameters =
103             { "-c", "src/test/resources/PdpSimulatorConfigParameters_InvalidName.json" };
104         pdpSimulator = new PdpSimulatorMain(pdpSimulatorConfigParameters);
105         assertTrue(pdpSimulator.getParameters() == null);
106     }
107 }