43d36f84d5e4d0d8f13fea59f876d7843b083e57
[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.assertTrue;
26
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager;
31 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
32 import org.onap.policy.apex.services.onappf.parameters.CommonTestData;
33 import org.onap.policy.common.utils.services.Registry;
34
35 /**
36  * Class to perform unit test of {@link ApexStarterMain}}.
37  *
38  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
39  */
40 public class TestApexStarterMain {
41     private ApexStarterMain apexStarter;
42
43     /**
44      * Set up.
45      */
46     @Before
47     public void setUp() {
48         Registry.newRegistry();
49     }
50
51     /**
52      * Shuts "main" down.
53      *
54      * @throws Exception if an error occurs
55      */
56     @After
57     public void tearDown() throws Exception {
58         // shut down activator
59         final ApexStarterActivator activator = Registry.getOrDefault(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR,
60                 ApexStarterActivator.class, null);
61         if (activator != null && activator.isAlive()) {
62             activator.terminate();
63         }
64     }
65
66     @Test
67     public void testApexStarter() throws ApexStarterException {
68         final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParametersNoop.json"};
69         apexStarter = new ApexStarterMain(apexStarterConfigParameters);
70         assertTrue(apexStarter.getParameters().isValid());
71         assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, apexStarter.getParameters().getName());
72
73         // ensure items were added to the registry
74         assertNotNull(Registry.get(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, ApexStarterActivator.class));
75         assertNotNull(Registry.get(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER,
76                 ApexPolicyStatisticsManager.class));
77         apexStarter.shutdown();
78     }
79
80     @Test
81     public void testApexStarter_NoArguments() {
82         final String[] apexStarterConfigParameters = {};
83         apexStarter = new ApexStarterMain(apexStarterConfigParameters);
84         assertTrue(apexStarter.getParameters() == null);
85     }
86
87     @Test
88     public void testApexStarter_InvalidArguments() {
89         final String[] apexStarterConfigParameters = { "src/test/resources/ApexStarterConfigParameters.json" };
90         apexStarter = new ApexStarterMain(apexStarterConfigParameters);
91         assertTrue(apexStarter.getParameters() == null);
92     }
93
94     @Test
95     public void testApexStarter_Help() {
96         final String[] apexStarterConfigParameters = { "-h" };
97         ApexStarterMain.main(apexStarterConfigParameters);
98     }
99
100     @Test
101     public void testApexStarter_InvalidParameters() {
102         final String[] apexStarterConfigParameters =
103         { "-c", "src/test/resources/ApexStarterConfigParameters_InvalidName.json" };
104         apexStarter = new ApexStarterMain(apexStarterConfigParameters);
105         assertTrue(apexStarter.getParameters() == null);
106     }
107 }