4b4558b89d4ffc2b7168e367f3a82888a5cb2969
[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 java.time.Instant;
24 import java.util.Collections;
25 import java.util.UUID;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 import org.mockito.Mockito;
29 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
30 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck;
31 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
32 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck;
33 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
34 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
35 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantDeregisterAckListener;
36 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher;
37 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantRegisterAckListener;
38 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantUpdateListener;
39 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
40 import org.onap.policy.clamp.controlloop.participant.policy.main.utils.TestListenerUtils;
41 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
42 import org.onap.policy.common.endpoints.event.comm.TopicSink;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.boot.test.context.SpringBootTest;
46 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
47 import org.springframework.test.context.TestPropertySource;
48 import org.springframework.test.context.junit.jupiter.SpringExtension;
49
50 @ExtendWith(SpringExtension.class)
51 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
52 @TestPropertySource(locations = {"classpath:application_test.properties"})
53 class ParticipantMessagesTest {
54
55     private static final Object lockit = new Object();
56     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
57     private static final String TOPIC = "my-topic";
58
59     @Autowired
60     private ParticipantHandler participantHandler;
61
62     @Test
63     void testSendParticipantRegisterMessage() throws Exception {
64         final ParticipantRegister participantRegisterMsg = new ParticipantRegister();
65         participantRegisterMsg.setParticipantId(getParticipantId());
66         participantRegisterMsg.setTimestamp(Instant.now());
67         participantRegisterMsg.setParticipantType(getParticipantType());
68
69         synchronized (lockit) {
70             ParticipantMessagePublisher participantMessagePublisher =
71                     new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
72             participantMessagePublisher.sendParticipantRegister(participantRegisterMsg);
73         }
74     }
75
76     @Test
77     void testReceiveParticipantRegisterAckMessage() throws Exception {
78         final ParticipantRegisterAck participantRegisterAckMsg = new ParticipantRegisterAck();
79         participantRegisterAckMsg.setMessage("ParticipantRegisterAck message");
80         participantRegisterAckMsg.setResponseTo(UUID.randomUUID());
81         participantRegisterAckMsg.setResult(true);
82
83         synchronized (lockit) {
84             ParticipantRegisterAckListener participantRegisterAckListener =
85                 new ParticipantRegisterAckListener(participantHandler);
86             participantRegisterAckListener.onTopicEvent(INFRA, TOPIC, null, participantRegisterAckMsg);
87         }
88     }
89
90     @Test
91     void testSendParticipantDeregisterMessage() throws Exception {
92         final ParticipantDeregister participantDeregisterMsg = new ParticipantDeregister();
93         participantDeregisterMsg.setParticipantId(getParticipantId());
94         participantDeregisterMsg.setTimestamp(Instant.now());
95         participantDeregisterMsg.setParticipantType(getParticipantType());
96
97         synchronized (lockit) {
98             ParticipantMessagePublisher participantMessagePublisher =
99                     new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
100             participantMessagePublisher.sendParticipantDeregister(participantDeregisterMsg);
101         }
102     }
103
104     @Test
105     void testReceiveParticipantDeregisterAckMessage() throws Exception {
106         final ParticipantDeregisterAck participantDeregisterAckMsg = new ParticipantDeregisterAck();
107         participantDeregisterAckMsg.setMessage("ParticipantDeregisterAck message");
108         participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
109         participantDeregisterAckMsg.setResult(true);
110
111         synchronized (lockit) {
112             ParticipantDeregisterAckListener participantDeregisterAckListener =
113                     new ParticipantDeregisterAckListener(participantHandler);
114             participantDeregisterAckListener.onTopicEvent(INFRA, TOPIC, null, participantDeregisterAckMsg);
115         }
116     }
117
118     @Test
119     void testReceiveParticipantUpdateMessage() throws Exception {
120         ParticipantUpdate participantUpdateMsg = TestListenerUtils.createParticipantUpdateMsg();
121
122         synchronized (lockit) {
123             ParticipantUpdateListener participantUpdateListener = new ParticipantUpdateListener(participantHandler);
124             participantUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateMsg);
125         }
126     }
127
128     @Test
129     void testSendParticipantUpdateAckMessage() throws Exception {
130         final ParticipantUpdateAck participantUpdateAckMsg = new ParticipantUpdateAck();
131         participantUpdateAckMsg.setMessage("ParticipantUpdateAck message");
132         participantUpdateAckMsg.setResponseTo(UUID.randomUUID());
133         participantUpdateAckMsg.setResult(true);
134
135         synchronized (lockit) {
136             ParticipantMessagePublisher participantMessagePublisher =
137                     new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
138             participantMessagePublisher.sendParticipantUpdateAck(participantUpdateAckMsg);
139         }
140     }
141
142     private ToscaConceptIdentifier getParticipantId() {
143         return new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
144     }
145
146     private ToscaConceptIdentifier getParticipantType() {
147         return new ToscaConceptIdentifier("org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");
148     }
149 }