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.assertj.core.api.Assertions.assertThatCode;
25 import java.time.Instant;
26 import java.util.Collections;
27 import java.util.UUID;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.extension.ExtendWith;
30 import org.mockito.Mockito;
31 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
32 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck;
33 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
34 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck;
35 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseDetails;
36 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
37 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
39 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantDeregisterAckListener;
40 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher;
41 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantRegisterAckListener;
42 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantUpdateListener;
43 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
44 import org.onap.policy.clamp.controlloop.participant.policy.main.utils.TestListenerUtils;
45 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
46 import org.onap.policy.common.endpoints.event.comm.TopicSink;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.boot.test.context.SpringBootTest;
50 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
51 import org.springframework.test.context.TestPropertySource;
52 import org.springframework.test.context.junit.jupiter.SpringExtension;
54 @ExtendWith(SpringExtension.class)
55 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
56 @TestPropertySource(locations = {"classpath:application_test.properties"})
57 class ParticipantMessagesTest {
59 private static final Object lockit = new Object();
60 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
61 private static final String TOPIC = "my-topic";
64 private ParticipantHandler participantHandler;
67 void testSendParticipantRegisterMessage() throws Exception {
68 final ParticipantRegister participantRegisterMsg = new ParticipantRegister();
69 participantRegisterMsg.setParticipantId(getParticipantId());
70 participantRegisterMsg.setTimestamp(Instant.now());
71 participantRegisterMsg.setParticipantType(getParticipantType());
73 synchronized (lockit) {
74 ParticipantMessagePublisher participantMessagePublisher =
75 new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
76 participantMessagePublisher.sendParticipantRegister(participantRegisterMsg);
81 void testReceiveParticipantRegisterAckMessage() throws Exception {
82 final ParticipantRegisterAck participantRegisterAckMsg = new ParticipantRegisterAck();
83 participantRegisterAckMsg.setMessage("ParticipantRegisterAck message");
84 participantRegisterAckMsg.setResponseTo(UUID.randomUUID());
85 participantRegisterAckMsg.setResult(true);
87 synchronized (lockit) {
88 ParticipantRegisterAckListener participantRegisterAckListener =
89 new ParticipantRegisterAckListener(participantHandler);
90 participantRegisterAckListener.onTopicEvent(INFRA, TOPIC, null, participantRegisterAckMsg);
95 void testSendParticipantDeregisterMessage() throws Exception {
96 final ParticipantDeregister participantDeregisterMsg = new ParticipantDeregister();
97 participantDeregisterMsg.setParticipantId(getParticipantId());
98 participantDeregisterMsg.setTimestamp(Instant.now());
99 participantDeregisterMsg.setParticipantType(getParticipantType());
101 synchronized (lockit) {
102 ParticipantMessagePublisher participantMessagePublisher =
103 new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
104 participantMessagePublisher.sendParticipantDeregister(participantDeregisterMsg);
109 void testReceiveParticipantDeregisterAckMessage() throws Exception {
110 final ParticipantDeregisterAck participantDeregisterAckMsg = new ParticipantDeregisterAck();
111 participantDeregisterAckMsg.setMessage("ParticipantDeregisterAck message");
112 participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
113 participantDeregisterAckMsg.setResult(true);
115 synchronized (lockit) {
116 ParticipantDeregisterAckListener participantDeregisterAckListener =
117 new ParticipantDeregisterAckListener(participantHandler);
118 participantDeregisterAckListener.onTopicEvent(INFRA, TOPIC, null, participantDeregisterAckMsg);
123 void testReceiveParticipantUpdateMessage() throws Exception {
124 ParticipantUpdate participantUpdateMsg = TestListenerUtils.createParticipantUpdateMsg();
126 synchronized (lockit) {
127 ParticipantUpdateListener participantUpdateListener = new ParticipantUpdateListener(participantHandler);
128 participantUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateMsg);
133 void testSendParticipantUpdateAckMessage() throws Exception {
134 final ParticipantUpdateAck participantUpdateAckMsg = new ParticipantUpdateAck();
135 participantUpdateAckMsg.setMessage("ParticipantUpdateAck message");
136 participantUpdateAckMsg.setResponseTo(UUID.randomUUID());
137 participantUpdateAckMsg.setResult(true);
139 synchronized (lockit) {
140 ParticipantMessagePublisher participantMessagePublisher =
141 new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
142 participantMessagePublisher.sendParticipantUpdateAck(participantUpdateAckMsg);
147 void testParticipantStatusHeartbeat() throws Exception {
148 final ParticipantStatus heartbeat = new ParticipantStatus();
149 heartbeat.setMessage("ParticipantStatus message");
150 heartbeat.setResponse(new ParticipantResponseDetails());
151 heartbeat.setParticipantId(getParticipantId());
152 synchronized (lockit) {
153 ParticipantMessagePublisher publisher =
154 new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
155 assertThatCode(() -> publisher.sendHeartbeat(heartbeat)).doesNotThrowAnyException();
160 private ToscaConceptIdentifier getParticipantId() {
161 return new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
164 private ToscaConceptIdentifier getParticipantType() {
165 return new ToscaConceptIdentifier("org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");