abc3e71b8f52ac4e3ffba188eccb64c846e1476d
[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.handler;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27 import org.junit.AfterClass;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.onap.policy.clamp.controlloop.common.ControlLoopConstants;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
32 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
33 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
34 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopStateChangeListener;
35 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopUpdateListener;
36 import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.CommonTestData;
37 import org.onap.policy.clamp.controlloop.participant.policy.main.startstop.Main;
38 import org.onap.policy.clamp.controlloop.participant.policy.main.startstop.ParticipantPolicyActivator;
39 import org.onap.policy.clamp.controlloop.participant.policy.main.utils.TestListenerUtils;
40 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
41 import org.onap.policy.common.utils.services.Registry;
42
43 public class TestPolicyHandler {
44
45     private static ControlLoopUpdateListener clUpdateListener;
46     private ControlLoopStateChangeListener clStateChangeListener;
47     private static ParticipantControlLoopUpdate participantControlLoopUpdateMsg;
48     private ParticipantControlLoopStateChange participantControlLoopStateChangeMsg;
49     private static final String PARTICIPANTS_ENDPOINT = "participants";
50     private static final String ELEMENTS_ENDPOINT = "elements";
51     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
52     private static final String TOPIC = "my-topic";
53     private static final Object lockit = new Object();
54     static CommonTestData commonTestData = new CommonTestData();
55
56     /**
57      * Setup before class, instantiate Main.
58      *
59      */
60     @BeforeClass
61     public static void setUpBeforeClass() throws Exception {
62         Registry.newRegistry();
63         final String[] configParameters = {"-c", "src/test/resources/parameters/TestParameters.json"};
64         Main main = new Main(configParameters);
65         assertTrue(main.isRunning());
66         TestListenerUtils.initParticipantHandler();
67
68         clUpdateListener = new ControlLoopUpdateListener(
69                 PolicyHandler.getInstance()
70                 .getPolicyProvider()
71                 .getIntermediaryApi()
72                 .getParticipantHandler());
73         participantControlLoopUpdateMsg =
74                 TestListenerUtils.createControlLoopUpdateMsg();
75         participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
76     }
77
78     @Test
79     public void testUpdatePolicyTypes() throws Exception {
80         // Verify that the ToscaServicetemplate has policy_types
81         assertNotNull(participantControlLoopUpdateMsg.getControlLoopDefinition().getPolicyTypes());
82
83         synchronized (lockit) {
84             clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
85         }
86         // Verify the result of GET participants with what is stored
87         assertEquals("org.onap.PM_Policy",
88                 TestListenerUtils.getParticipantHandler().getParticipantId().getName());
89     }
90
91     @Test
92     public void testUpdatePolicies() throws Exception {
93         // Add policies to the toscaServiceTemplate
94         TestListenerUtils.addPoliciesToToscaServiceTemplate(participantControlLoopUpdateMsg.getControlLoopDefinition());
95
96         // Verify that the ToscaServicetemplate has policies
97         assertNotNull(participantControlLoopUpdateMsg.getControlLoopDefinition()
98                 .getToscaTopologyTemplate().getPolicies());
99
100         synchronized (lockit) {
101             clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
102         }
103         // Verify the result of GET participants with what is stored
104         assertEquals("org.onap.PM_Policy",
105                 TestListenerUtils.getParticipantHandler().getParticipantId().getName());
106     }
107
108     @Test
109     public void testDeletePoliciesAndPolicyTypes() throws Exception {
110         // Add policies to the toscaServiceTemplate
111         TestListenerUtils.addPoliciesToToscaServiceTemplate(participantControlLoopUpdateMsg.getControlLoopDefinition());
112
113         // Verify that the ToscaServicetemplate has policies
114         assertNotNull(participantControlLoopUpdateMsg.getControlLoopDefinition()
115                 .getToscaTopologyTemplate().getPolicies());
116
117         synchronized (lockit) {
118             clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
119         }
120         // Verify the result of GET participants with what is stored
121         assertEquals("org.onap.PM_Policy",
122                 TestListenerUtils.getParticipantHandler().getParticipantId().getName());
123
124         clStateChangeListener = new ControlLoopStateChangeListener(TestListenerUtils.getParticipantHandler());
125         participantControlLoopStateChangeMsg =
126                 TestListenerUtils.createControlLoopStateChangeMsg(ControlLoopOrderedState.UNINITIALISED);
127         participantControlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
128         clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopStateChangeMsg);
129
130         // Verify the result of GET participants with what is stored
131         assertEquals("org.onap.PM_Policy",
132                 TestListenerUtils.getParticipantHandler().getParticipantId().getName());
133     }
134 }