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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.services.onappf;
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;
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;
42 * Class to perform unit test of {@link ApexStarterMain}}.
44 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
46 public class TestApexStarterMain {
47 private ApexStarterMain apexStarter;
54 Registry.newRegistry();
60 * @throws Exception if an error occurs
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();
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());
79 // ensure items were added to the registry
80 assertNotNull(Registry.get(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, ApexStarterActivator.class));
82 Registry.get(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER, ApexPolicyStatisticsManager.class));
83 apexStarter.shutdown();
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));
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));
103 public void testApexStarter_Help() {
104 final String[] apexStarterConfigParameters = {"-h"};
105 assertThatCode(() -> ApexStarterMain.main(apexStarterConfigParameters)).doesNotThrowAnyException();
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));