927c1c3f362881baeb710957968b50f0eaecf2c6
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 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.apex.services.onappf;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager;
32 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
33 import org.onap.policy.apex.services.onappf.parameters.CommonTestData;
34 import org.onap.policy.common.utils.services.Registry;
35
36 /**
37  * Class to perform unit test of {@link ApexStarterMain}}.
38  *
39  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
40  */
41 public class TestApexStarterMain {
42     private ApexStarterMain apexStarter;
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 ApexStarterActivator activator = Registry.getOrDefault(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR,
61                 ApexStarterActivator.class, null);
62         if (activator != null && activator.isAlive()) {
63             activator.terminate();
64         }
65     }
66
67     @Test
68     public void testApexStarter() throws ApexStarterException {
69         final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParametersNoop.json"};
70         apexStarter = new ApexStarterMain(apexStarterConfigParameters);
71         assertTrue(apexStarter.getParameters().isValid());
72         assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, apexStarter.getParameters().getName());
73
74         // ensure items were added to the registry
75         assertNotNull(Registry.get(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, ApexStarterActivator.class));
76         assertNotNull(Registry.get(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER,
77                 ApexPolicyStatisticsManager.class));
78         apexStarter.shutdown();
79     }
80
81     @Test
82     public void testApexStarter_NoArguments() {
83         final String[] apexStarterConfigParameters = {};
84         apexStarter = new ApexStarterMain(apexStarterConfigParameters);
85         assertNull(apexStarter.getParameters());
86     }
87
88     @Test
89     public void testApexStarter_InvalidArguments() {
90         final String[] apexStarterConfigParameters = { "src/test/resources/ApexStarterConfigParameters.json" };
91         apexStarter = new ApexStarterMain(apexStarterConfigParameters);
92         assertNull(apexStarter.getParameters());
93     }
94
95     @Test
96     public void testApexStarter_Help() {
97         final String[] apexStarterConfigParameters = { "-h" };
98         ApexStarterMain.main(apexStarterConfigParameters);
99     }
100
101     @Test
102     public void testApexStarter_InvalidParameters() {
103         final String[] apexStarterConfigParameters =
104         { "-c", "src/test/resources/ApexStarterConfigParameters_InvalidName.json" };
105         apexStarter = new ApexStarterMain(apexStarterConfigParameters);
106         assertNull(apexStarter.getParameters());
107     }
108 }