2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2023 Nordix Foundation.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.acm.participant.policy.endtoend;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import java.time.Instant;
28 import java.util.Collections;
29 import java.util.UUID;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.extension.ExtendWith;
32 import org.mockito.Mockito;
33 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantDeregisterAckListener;
34 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
35 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantPrimeListener;
36 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantRegisterAckListener;
37 import org.onap.policy.clamp.acm.participant.intermediary.handler.ParticipantHandler;
38 import org.onap.policy.clamp.acm.participant.policy.main.parameters.CommonTestData;
39 import org.onap.policy.clamp.acm.participant.policy.main.utils.TestListenerUtils;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregisterAck;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantPrime;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantPrimeAck;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
46 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
47 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
48 import org.onap.policy.common.endpoints.event.comm.TopicSink;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.boot.test.context.SpringBootTest;
51 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
52 import org.springframework.test.context.ActiveProfiles;
53 import org.springframework.test.context.junit.jupiter.SpringExtension;
55 @ExtendWith(SpringExtension.class)
56 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
57 @ActiveProfiles("test")
58 class ParticipantMessagesTest {
60 private static final Object lockit = new Object();
61 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
62 private static final String TOPIC = "my-topic";
65 private ParticipantHandler participantHandler;
68 void testSendParticipantRegisterMessage() {
69 final ParticipantRegister participantRegisterMsg = new ParticipantRegister();
70 participantRegisterMsg.setParticipantId(CommonTestData.getParticipantId());
71 participantRegisterMsg.setTimestamp(Instant.now());
73 synchronized (lockit) {
74 ParticipantMessagePublisher participantMessagePublisher =
75 new ParticipantMessagePublisher();
76 participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
77 assertThatCode(() -> participantMessagePublisher.sendParticipantRegister(participantRegisterMsg))
78 .doesNotThrowAnyException();
83 void testReceiveParticipantRegisterAckMessage() {
84 final ParticipantRegisterAck participantRegisterAckMsg = new ParticipantRegisterAck();
85 participantRegisterAckMsg.setMessage("ParticipantRegisterAck message");
86 participantRegisterAckMsg.setResponseTo(UUID.randomUUID());
87 participantRegisterAckMsg.setResult(true);
89 synchronized (lockit) {
90 ParticipantRegisterAckListener participantRegisterAckListener =
91 new ParticipantRegisterAckListener(participantHandler);
92 assertThatCode(() -> participantRegisterAckListener.onTopicEvent(INFRA, TOPIC, null,
93 participantRegisterAckMsg)).doesNotThrowAnyException();
98 void testSendParticipantDeregisterMessage() {
99 final ParticipantDeregister participantDeregisterMsg = new ParticipantDeregister();
100 participantDeregisterMsg.setParticipantId(CommonTestData.getParticipantId());
101 participantDeregisterMsg.setTimestamp(Instant.now());
103 synchronized (lockit) {
104 ParticipantMessagePublisher participantMessagePublisher =
105 new ParticipantMessagePublisher();
106 participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
107 assertThatCode(() -> participantMessagePublisher.sendParticipantDeregister(participantDeregisterMsg))
108 .doesNotThrowAnyException();
113 void testReceiveParticipantDeregisterAckMessage() {
114 final ParticipantDeregisterAck participantDeregisterAckMsg = new ParticipantDeregisterAck();
115 participantDeregisterAckMsg.setMessage("ParticipantDeregisterAck message");
116 participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
117 participantDeregisterAckMsg.setResult(true);
119 synchronized (lockit) {
120 ParticipantDeregisterAckListener participantDeregisterAckListener =
121 new ParticipantDeregisterAckListener(participantHandler);
122 assertThatCode(() -> participantDeregisterAckListener.onTopicEvent(INFRA, TOPIC, null,
123 participantDeregisterAckMsg)).doesNotThrowAnyException();
128 void testReceiveParticipantUpdateMessage() {
129 ParticipantPrime participantPrimeMsg = TestListenerUtils.createParticipantPrimeMsg();
131 synchronized (lockit) {
132 ParticipantPrimeListener participantPrimeListener = new ParticipantPrimeListener(participantHandler);
133 participantPrimeListener.onTopicEvent(INFRA, TOPIC, null, participantPrimeMsg);
136 // Verify the result of GET participants with what is stored
137 assertEquals(CommonTestData.getParticipantId(), participantHandler.getParticipantId());
141 void testSendParticipantPrimeAckMessage() {
142 final ParticipantPrimeAck participantPrimeAckMsg = new ParticipantPrimeAck();
143 participantPrimeAckMsg.setMessage("ParticipantPrimeAck message");
144 participantPrimeAckMsg.setResponseTo(UUID.randomUUID());
145 participantPrimeAckMsg.setResult(true);
147 synchronized (lockit) {
148 ParticipantMessagePublisher participantMessagePublisher = new ParticipantMessagePublisher();
149 participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
150 assertThatCode(() -> participantMessagePublisher.sendParticipantPrimeAck(participantPrimeAckMsg))
151 .doesNotThrowAnyException();
156 void testParticipantStatusHeartbeat() {
157 final ParticipantStatus heartbeat = participantHandler.makeHeartbeat(true);
158 synchronized (lockit) {
159 ParticipantMessagePublisher publisher = new ParticipantMessagePublisher();
160 publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
161 assertThatCode(() -> publisher.sendHeartbeat(heartbeat)).doesNotThrowAnyException();