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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.participant.policy.endtoend;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
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.models.messages.dmaap.participant.ParticipantUpdate;
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.intermediary.comm.ParticipantUpdateListener;
35 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
36 import org.onap.policy.clamp.controlloop.participant.policy.main.utils.TestListenerUtils;
37 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.test.context.SpringBootTest;
40 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
41 import org.springframework.test.context.TestPropertySource;
42 import org.springframework.test.context.junit.jupiter.SpringExtension;
44 @ExtendWith(SpringExtension.class)
45 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
46 @TestPropertySource(locations = {"classpath:application_test.properties"})
47 class ParticipantPolicyTest {
49 private static final Object lockit = new Object();
50 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
51 private static final String TOPIC = "my-topic";
54 private ParticipantHandler participantHandler;
57 void testUpdatePolicyTypes() {
58 ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
59 participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
61 // Verify that the ToscaServicetemplate has policy_types
62 assertNotNull(participantControlLoopUpdateMsg.getControlLoopDefinition().getPolicyTypes());
64 synchronized (lockit) {
65 ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
67 clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
69 // Verify the result of GET participants with what is stored
70 assertEquals("org.onap.PM_Policy", participantHandler.getParticipantId().getName());
74 void testUpdatePolicies() throws Exception {
75 ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
76 participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
78 // Add policies to the toscaServiceTemplate
79 TestListenerUtils.addPoliciesToToscaServiceTemplate(participantControlLoopUpdateMsg.getControlLoopDefinition());
81 // Verify that the ToscaServicetemplate has policies
83 participantControlLoopUpdateMsg.getControlLoopDefinition().getToscaTopologyTemplate().getPolicies());
85 synchronized (lockit) {
86 ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
88 clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
90 // Verify the result of GET participants with what is stored
91 assertEquals("org.onap.PM_Policy", participantHandler.getParticipantId().getName());
95 void testDeletePoliciesAndPolicyTypes() throws Exception {
96 ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
97 participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
99 // Add policies to the toscaServiceTemplate
100 TestListenerUtils.addPoliciesToToscaServiceTemplate(participantControlLoopUpdateMsg.getControlLoopDefinition());
102 // Verify that the ToscaServicetemplate has policies
104 participantControlLoopUpdateMsg.getControlLoopDefinition().getToscaTopologyTemplate().getPolicies());
106 synchronized (lockit) {
107 ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
109 clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
111 // Verify the result of GET participants with what is stored
112 assertEquals("org.onap.PM_Policy", participantHandler.getParticipantId().getName());
114 ControlLoopStateChangeListener clStateChangeListener = new ControlLoopStateChangeListener(participantHandler);
115 ParticipantControlLoopStateChange participantControlLoopStateChangeMsg =
116 TestListenerUtils.createControlLoopStateChangeMsg(ControlLoopOrderedState.UNINITIALISED);
117 participantControlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
118 clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopStateChangeMsg);
120 // Verify the result of GET participants with what is stored
121 assertEquals("org.onap.PM_Policy", participantHandler.getParticipantId().getName());