2a6f4b005fb98e936fa0e659f75ecd433a70eb6b
[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.endtoend;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
29 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
30 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
31 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
32 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopStateChangeListener;
33 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopUpdateListener;
34 import org.onap.policy.clamp.controlloop.participant.policy.main.utils.TestListenerUtils;
35 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.boot.test.context.SpringBootTest;
38 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
39 import org.springframework.test.context.TestPropertySource;
40 import org.springframework.test.context.junit.jupiter.SpringExtension;
41
42 @ExtendWith(SpringExtension.class)
43 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
44 @TestPropertySource(properties = "participant.file=src/test/resources/parameters/TestParameters.json")
45 class ParticipantPolicyTest {
46
47     private static final Object lockit = new Object();
48     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
49     private static final String TOPIC = "my-topic";
50
51     @Autowired
52     private ParticipantIntermediaryApi participantIntermediaryApi;
53
54     @Test
55     void testUpdatePolicyTypes() {
56         ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
57         participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
58
59         // Verify that the ToscaServicetemplate has policy_types
60         assertNotNull(participantControlLoopUpdateMsg.getControlLoopDefinition().getPolicyTypes());
61
62         synchronized (lockit) {
63             ControlLoopUpdateListener clUpdateListener =
64                     new ControlLoopUpdateListener(participantIntermediaryApi.getParticipantHandler());
65
66             clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
67         }
68         // Verify the result of GET participants with what is stored
69         assertEquals("org.onap.PM_Policy",
70                 participantIntermediaryApi.getParticipantHandler().getParticipantId().getName());
71     }
72
73     @Test
74     void testUpdatePolicies() throws Exception {
75         ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
76         participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
77
78         // Add policies to the toscaServiceTemplate
79         TestListenerUtils.addPoliciesToToscaServiceTemplate(participantControlLoopUpdateMsg.getControlLoopDefinition());
80
81         // Verify that the ToscaServicetemplate has policies
82         assertNotNull(
83                 participantControlLoopUpdateMsg.getControlLoopDefinition().getToscaTopologyTemplate().getPolicies());
84
85         synchronized (lockit) {
86             ControlLoopUpdateListener clUpdateListener =
87                     new ControlLoopUpdateListener(participantIntermediaryApi.getParticipantHandler());
88
89             clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
90         }
91         // Verify the result of GET participants with what is stored
92         assertEquals("org.onap.PM_Policy",
93                 participantIntermediaryApi.getParticipantHandler().getParticipantId().getName());
94     }
95
96     @Test
97     void testDeletePoliciesAndPolicyTypes() throws Exception {
98         ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
99         participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
100
101         // Add policies to the toscaServiceTemplate
102         TestListenerUtils.addPoliciesToToscaServiceTemplate(participantControlLoopUpdateMsg.getControlLoopDefinition());
103
104         // Verify that the ToscaServicetemplate has policies
105         assertNotNull(
106                 participantControlLoopUpdateMsg.getControlLoopDefinition().getToscaTopologyTemplate().getPolicies());
107
108         synchronized (lockit) {
109             ControlLoopUpdateListener clUpdateListener =
110                     new ControlLoopUpdateListener(participantIntermediaryApi.getParticipantHandler());
111
112             clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
113         }
114         // Verify the result of GET participants with what is stored
115         assertEquals("org.onap.PM_Policy",
116                 participantIntermediaryApi.getParticipantHandler().getParticipantId().getName());
117
118         ControlLoopStateChangeListener clStateChangeListener =
119                 new ControlLoopStateChangeListener(participantIntermediaryApi.getParticipantHandler());
120         ParticipantControlLoopStateChange participantControlLoopStateChangeMsg =
121                 TestListenerUtils.createControlLoopStateChangeMsg(ControlLoopOrderedState.UNINITIALISED);
122         participantControlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
123         clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopStateChangeMsg);
124
125         // Verify the result of GET participants with what is stored
126         assertEquals("org.onap.PM_Policy",
127                 participantIntermediaryApi.getParticipantHandler().getParticipantId().getName());
128     }
129 }