10563cde2fdbad9836806b09d35b910315bba157
[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
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.ExtendWith;
27 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
28 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange;
29 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
30 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopStateChangeListener;
31 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopUpdateListener;
32 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
33 import org.onap.policy.clamp.controlloop.participant.policy.main.utils.TestListenerUtils;
34 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.boot.test.context.SpringBootTest;
37 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
38 import org.springframework.test.context.TestPropertySource;
39 import org.springframework.test.context.junit.jupiter.SpringExtension;
40
41 @ExtendWith(SpringExtension.class)
42 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
43 @TestPropertySource(locations = {"classpath:application_test.properties"})
44 class ParticipantPolicyTest {
45
46     private static final Object lockit = new Object();
47     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
48     private static final String TOPIC = "my-topic";
49
50     @Autowired
51     private ParticipantHandler participantHandler;
52
53     @Test
54     void testUpdatePolicyTypes() {
55         ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
56         controlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
57
58         synchronized (lockit) {
59             ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
60
61             clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg);
62         }
63         // Verify the result of GET participants with what is stored
64         assertEquals("org.onap.PM_Policy", participantHandler.getParticipantId().getName());
65     }
66
67     @Test
68     void testUpdatePolicies() throws Exception {
69         ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
70         controlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
71
72         synchronized (lockit) {
73             ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
74
75             clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg);
76         }
77         // Verify the result of GET participants with what is stored
78         assertEquals("org.onap.PM_Policy", participantHandler.getParticipantId().getName());
79     }
80
81     @Test
82     void testDeletePoliciesAndPolicyTypes() throws Exception {
83         ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
84         controlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
85
86         synchronized (lockit) {
87             ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
88
89             clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg);
90         }
91         // Verify the result of GET participants with what is stored
92         assertEquals("org.onap.PM_Policy", participantHandler.getParticipantId().getName());
93
94         ControlLoopStateChangeListener clStateChangeListener = new ControlLoopStateChangeListener(participantHandler);
95         ControlLoopStateChange controlLoopStateChangeMsg =
96                 TestListenerUtils.createControlLoopStateChangeMsg(ControlLoopOrderedState.UNINITIALISED);
97         controlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
98         clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, controlLoopStateChangeMsg);
99
100         // Verify the result of GET participants with what is stored
101         assertEquals("org.onap.PM_Policy", participantHandler.getParticipantId().getName());
102     }
103 }