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