1903868e289b37cf3f4ba1c600659609253f1566
[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.dcae.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.dcae.main.parameters.CommonTestData;
32 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.ParticipantDcaeParameterHandler;
33 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.ParticipantDcaeParameters;
34 import org.onap.policy.clamp.controlloop.participant.dcae.main.startstop.ParticipantDcaeActivator;
35 import org.onap.policy.clamp.controlloop.participant.dcae.main.startstop.ParticipantDcaeCommandLineArguments;
36 import org.onap.policy.common.utils.services.Registry;
37
38 /**
39  * Class to perform unit test of {@link ParticipantDcaeActivator}}.
40  *
41  */
42 public class TestParticipantDcaeActivator {
43
44     private static ParticipantDcaeActivator activator;
45
46     /**
47      * Initializes an activator.
48      *
49      * @throws Exception if an error occurs
50      */
51     @BeforeClass
52     public static void setUp() throws Exception {
53         Registry.newRegistry();
54         final String[] participantConfigParameters = { "-c", "src/test/resources/parameters/TestParameters.json"};
55         final ParticipantDcaeCommandLineArguments arguments =
56                 new ParticipantDcaeCommandLineArguments(participantConfigParameters);
57         final ParticipantDcaeParameters parGroup =
58                 new ParticipantDcaeParameterHandler().getParameters(arguments);
59         activator = new ParticipantDcaeActivator(parGroup);
60     }
61
62     /**
63      * Method for cleanup after each test.
64      *
65      * @throws Exception if an error occurs
66      */
67     @AfterClass
68     public static void teardown() throws Exception {
69         // shut down activator
70         if (activator != null && activator.isAlive()) {
71             activator.shutdown();
72         }
73     }
74
75     @Test
76     public void testParticipantActivator() {
77         activator.start();
78         assertTrue(activator.isAlive());
79         assertTrue(activator.getParameters().isValid());
80         assertEquals(CommonTestData.PARTICIPANT_GROUP_NAME, activator.getParameters().getName());
81
82         // repeat - should throw an exception
83         assertThatIllegalStateException().isThrownBy(() -> activator.start());
84         assertTrue(activator.isAlive());
85         assertTrue(activator.getParameters().isValid());
86
87         activator.shutdown();
88         assertFalse(activator.isAlive());
89
90         // repeat - should throw an exception
91         assertThatIllegalStateException().isThrownBy(() -> activator.shutdown());
92         assertFalse(activator.isAlive());
93     }
94 }