afb0f6cb4812574e1783cd29cda8fbcebbb5b315
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.clamp.controlloop.participant.policy.main.startstop;
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.assertTrue;
27
28 import org.junit.AfterClass;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.CommonTestData;
32 import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameterHandler;
33 import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameters;
34 import org.onap.policy.common.utils.services.Registry;
35
36 public class TestParticipantPolicyActivator {
37
38     private static ParticipantPolicyActivator activator;
39
40     /**
41      * Initializes an activator.
42      *
43      * @throws Exception if an error occurs
44      */
45     @BeforeClass
46     public static void setUp() throws Exception {
47         Registry.newRegistry();
48         final String[] participantConfigParameters = { "-c", "src/test/resources/parameters/TestParameters.json"};
49         final ParticipantPolicyCommandLineArguments arguments =
50                 new ParticipantPolicyCommandLineArguments(participantConfigParameters);
51         final ParticipantPolicyParameters parGroup =
52                 new ParticipantPolicyParameterHandler().getParameters(arguments);
53         activator = new ParticipantPolicyActivator(parGroup);
54     }
55
56     /**
57      * Method for cleanup after each test.
58      *
59      * @throws Exception if an error occurs
60      */
61     @AfterClass
62     public static void teardown() throws Exception {
63         // shut down activator
64         if (activator != null && activator.isAlive()) {
65             activator.shutdown();
66         }
67     }
68
69     @Test
70     public void testParticipantActivator() {
71         activator.start();
72         assertTrue(activator.isAlive());
73         assertTrue(activator.getParameters().isValid());
74         assertEquals(CommonTestData.PARTICIPANT_GROUP_NAME, activator.getParameters().getName());
75
76         // repeat - should throw an exception
77         assertThatIllegalStateException().isThrownBy(() -> activator.start());
78         assertTrue(activator.isAlive());
79         assertTrue(activator.getParameters().isValid());
80
81         activator.shutdown();
82         assertFalse(activator.isAlive());
83
84         // repeat - should throw an exception
85         assertThatIllegalStateException().isThrownBy(() -> activator.shutdown());
86         assertFalse(activator.isAlive());
87     }
88 }