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