238a89f66ccdc6ef4a46bd49db7be71363698fb5
[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.assertj.core.api.Assertions.assertThatIllegalStateException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.io.FileInputStream;
31 import java.util.Properties;
32
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.policy.apex.services.onappf.ApexStarterActivator;
37 import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments;
38 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
39 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
40 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup;
41 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler;
42 import org.onap.policy.apex.services.onappf.parameters.CommonTestData;
43 import org.onap.policy.common.utils.services.Registry;
44 import org.onap.policy.models.pdp.concepts.PdpStatus;
45
46 /**
47  * Class to perform unit test of {@link ApexStarterActivator}}.
48  *
49  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
50  */
51 public class TestApexStarterActivator {
52
53     private ApexStarterActivator activator;
54
55     /**
56      * Initializes an activator.
57      *
58      * @throws Exception if an error occurs
59      */
60     @Before
61     public void setUp() throws Exception {
62         Registry.newRegistry();
63         final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json",
64             "-p", "src/test/resources/topic.properties" };
65         final ApexStarterCommandLineArguments arguments =
66                 new ApexStarterCommandLineArguments(apexStarterConfigParameters);
67         final ApexStarterParameterGroup parGroup = new ApexStarterParameterHandler().getParameters(arguments);
68
69         final Properties props = new Properties();
70         final String propFile = arguments.getFullPropertyFilePath();
71         try (FileInputStream stream = new FileInputStream(propFile)) {
72             props.load(stream);
73         }
74
75         activator = new ApexStarterActivator(parGroup, props);
76     }
77
78     /**
79      * Method for cleanup after each test.
80      *
81      * @throws Exception if an error occurs
82      */
83     @After
84     public void teardown() throws Exception {
85         if (activator != null && activator.isAlive()) {
86             activator.terminate();
87         }
88     }
89
90     @Test
91     public void testApexStarterActivator() throws ApexStarterException {
92         assertFalse(activator.isAlive());
93         activator.initialize();
94         assertTrue(activator.isAlive());
95         assertTrue(activator.getParameterGroup().isValid());
96         assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, activator.getParameterGroup().getName());
97
98         // ensure items were added to the registry
99         assertNotNull(Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class));
100
101         // repeat - should throw an exception
102         assertThatIllegalStateException().isThrownBy(() -> activator.initialize());
103         assertTrue(activator.isAlive());
104         assertTrue(activator.getParameterGroup().isValid());
105     }
106
107     @Test
108     public void testTerminate() throws Exception {
109         activator.initialize();
110         activator.terminate();
111         assertFalse(activator.isAlive());
112
113         // ensure items have been removed from the registry
114         assertNull(Registry.getOrDefault(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class, null));
115
116         // repeat - should throw an exception
117         assertThatIllegalStateException().isThrownBy(() -> activator.terminate());
118         assertFalse(activator.isAlive());
119     }
120 }