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