c8f889fde7019b28b612e3bd13833e46742229e5
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5
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.assertThatIllegalStateException;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertTrue;
31
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
36 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup;
37 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler;
38 import org.onap.policy.apex.services.onappf.parameters.CommonTestData;
39 import org.onap.policy.common.utils.services.Registry;
40 import org.onap.policy.models.pdp.concepts.PdpStatus;
41
42 /**
43  * Class to perform unit test of {@link ApexStarterActivator}}.
44  *
45  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
46  */
47 public class TestApexStarterActivator {
48
49     private ApexStarterActivator activator;
50
51     /**
52      * Initializes an activator.
53      *
54      * @throws Exception if an error occurs
55      */
56     @Before
57     public void setUp() throws Exception {
58         Registry.newRegistry();
59         final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json"};
60         final ApexStarterCommandLineArguments arguments =
61                 new ApexStarterCommandLineArguments(apexStarterConfigParameters);
62         final ApexStarterParameterGroup parGroup = new ApexStarterParameterHandler().getParameters(arguments);
63         activator = new ApexStarterActivator(parGroup);
64     }
65
66     /**
67      * Method for cleanup after each test.
68      *
69      * @throws Exception if an error occurs
70      */
71     @After
72     public void teardown() throws Exception {
73         if (activator != null && activator.isAlive()) {
74             activator.terminate();
75         }
76     }
77
78     @Test
79     public void testApexStarterActivator() throws ApexStarterException {
80         assertFalse(activator.isAlive());
81         activator.initialize();
82         assertTrue(activator.isAlive());
83         assertTrue(activator.getParameterGroup().isValid());
84         assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, activator.getParameterGroup().getName());
85
86         // ensure items were added to the registry
87         assertNotNull(Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class));
88
89         // repeat - should throw an exception
90         assertThatIllegalStateException().isThrownBy(() -> activator.initialize());
91         assertTrue(activator.isAlive());
92         assertTrue(activator.getParameterGroup().isValid());
93     }
94
95     @Test
96     public void testTerminate() throws Exception {
97         activator.initialize();
98         activator.terminate();
99         assertFalse(activator.isAlive());
100
101         // ensure items have been removed from the registry
102         assertNull(Registry.getOrDefault(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class, null));
103
104         // repeat - should throw an exception
105         assertThatIllegalStateException().isThrownBy(() -> activator.terminate());
106         assertFalse(activator.isAlive());
107     }
108 }