Add PDP Group Deploy and Delete REST API stubs
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / startstop / TestPapActivator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property.
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.pap.main.startstop;
23
24 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertSame;
28 import static org.junit.Assert.assertTrue;
29
30 import java.io.FileInputStream;
31 import java.util.Properties;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.pap.main.PolicyPapException;
36 import org.onap.policy.pap.main.parameters.CommonTestData;
37 import org.onap.policy.pap.main.parameters.PapParameterGroup;
38 import org.onap.policy.pap.main.parameters.PapParameterHandler;
39
40
41 /**
42  * Class to perform unit test of {@link PapActivator}}.
43  *
44  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
45  */
46 public class TestPapActivator {
47
48     private PapActivator activator;
49
50     /**
51      * Initializes an activator.
52      *
53      * @throws Exception if an error occurs
54      */
55     @Before
56     public void setUp() throws Exception {
57         final String[] papConfigParameters =
58             {"-c", "parameters/PapConfigParameters.json", "-p", "parameters/topic.properties"};
59         final PapCommandLineArguments arguments = new PapCommandLineArguments(papConfigParameters);
60         final PapParameterGroup parGroup = new PapParameterHandler().getParameters(arguments);
61
62         Properties props = new Properties();
63         String propFile = arguments.getFullConfigurationFilePath();
64         try (FileInputStream stream = new FileInputStream(propFile)) {
65             props.load(stream);
66         }
67
68         activator = new PapActivator(parGroup, props);
69     }
70
71     /**
72      * Method for cleanup after each test.
73      * @throws Exception if an error occurs
74      */
75     @After
76     public void teardown() throws Exception {
77         if (activator != null && activator.isAlive()) {
78             activator.terminate();
79         }
80     }
81
82     @Test
83     public void testPapActivator() throws PolicyPapException {
84         assertFalse(activator.isAlive());
85         activator.initialize();
86         assertTrue(activator.isAlive());
87         assertTrue(activator.getParameterGroup().isValid());
88         assertEquals(CommonTestData.PAP_GROUP_NAME, activator.getParameterGroup().getName());
89
90         // repeat - should throw an exception
91         assertThatIllegalStateException().isThrownBy(() -> activator.initialize());
92         assertTrue(activator.isAlive());
93         assertTrue(activator.getParameterGroup().isValid());
94     }
95
96     @Test
97     public void testGetCurrent_testSetCurrent() {
98         assertSame(activator, PapActivator.getCurrent());
99     }
100
101     @Test
102     public void testTerminate() throws Exception {
103         activator.initialize();
104         activator.terminate();
105         assertFalse(activator.isAlive());
106
107         // repeat - should throw an exception
108         assertThatIllegalStateException().isThrownBy(() -> activator.terminate());
109         assertFalse(activator.isAlive());
110     }
111 }