f24329c29cdce6438326cc46cfd4534fd4993241
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2020 Nordix Foundation
5  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.services.onappf;
24
25 import static org.assertj.core.api.Assertions.assertThatCode;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager;
35 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
36 import org.onap.policy.apex.services.onappf.exception.ApexStarterRunTimeException;
37 import org.onap.policy.apex.services.onappf.parameters.CommonTestData;
38 import org.onap.policy.common.utils.resources.MessageConstants;
39 import org.onap.policy.common.utils.services.Registry;
40
41 /**
42  * Class to perform unit test of {@link ApexStarterMain}}.
43  *
44  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
45  */
46 public class TestApexStarterMain {
47     private ApexStarterMain apexStarter;
48
49     /**
50      * Set up.
51      */
52     @Before
53     public void setUp() {
54         Registry.newRegistry();
55     }
56
57     /**
58      * Shuts "main" down.
59      *
60      * @throws Exception if an error occurs
61      */
62     @After
63     public void tearDown() throws Exception {
64         // shut down activator
65         final ApexStarterActivator activator =
66             Registry.getOrDefault(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, ApexStarterActivator.class, null);
67         if (activator != null && activator.isAlive()) {
68             activator.terminate();
69         }
70     }
71
72     @Test
73     public void testApexStarter() throws ApexStarterException {
74         final String[] apexStarterConfigParameters = {"-c", "src/test/resources/ApexStarterConfigParametersNoop.json"};
75         apexStarter = new ApexStarterMain(apexStarterConfigParameters);
76         assertTrue(apexStarter.getParameters().isValid());
77         assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, apexStarter.getParameters().getName());
78
79         // ensure items were added to the registry
80         assertNotNull(Registry.get(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, ApexStarterActivator.class));
81         assertNotNull(
82             Registry.get(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER, ApexPolicyStatisticsManager.class));
83         apexStarter.shutdown();
84     }
85
86     @Test
87     public void testApexStarter_NoArguments() {
88         final String[] apexStarterConfigParameters = {};
89         assertThatThrownBy(() -> new ApexStarterMain(apexStarterConfigParameters))
90             .isInstanceOf(ApexStarterRunTimeException.class)
91             .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_APEX_PDP));
92     }
93
94     @Test
95     public void testApexStarter_InvalidArguments() {
96         final String[] apexStarterConfigParameters = {"src/test/resources/ApexStarterConfigParameters.json"};
97         assertThatThrownBy(() -> new ApexStarterMain(apexStarterConfigParameters))
98             .isInstanceOf(ApexStarterRunTimeException.class)
99             .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_APEX_PDP));
100     }
101
102     @Test
103     public void testApexStarter_Help() {
104         final String[] apexStarterConfigParameters = {"-h"};
105         assertThatCode(() -> ApexStarterMain.main(apexStarterConfigParameters)).doesNotThrowAnyException();
106     }
107
108     @Test
109     public void testApexStarter_InvalidParameters() {
110         final String[] apexStarterConfigParameters =
111             {"-c", "src/test/resources/ApexStarterConfigParameters_InvalidName.json"};
112         assertThatThrownBy(() -> new ApexStarterMain(apexStarterConfigParameters))
113             .isInstanceOf(ApexStarterRunTimeException.class)
114             .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_APEX_PDP));
115     }
116 }