2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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.controlloop.participant.policy.endtoend;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.junit.Assert.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.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
34 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck;
35 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
36 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck;
37 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
40 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantDeregisterAckListener;
41 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher;
42 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantRegisterAckListener;
43 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantUpdateListener;
44 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
45 import org.onap.policy.clamp.controlloop.participant.policy.main.utils.TestListenerUtils;
46 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
47 import org.onap.policy.common.endpoints.event.comm.TopicSink;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
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.TestPropertySource;
53 import org.springframework.test.context.junit.jupiter.SpringExtension;
55 @ExtendWith(SpringExtension.class)
56 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
57 @TestPropertySource(locations = {"classpath:application_test.properties"})
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() throws Exception {
69 final ParticipantRegister participantRegisterMsg = new ParticipantRegister();
70 participantRegisterMsg.setParticipantId(getParticipantId());
71 participantRegisterMsg.setTimestamp(Instant.now());
72 participantRegisterMsg.setParticipantType(getParticipantType());
74 synchronized (lockit) {
75 ParticipantMessagePublisher participantMessagePublisher =
76 new ParticipantMessagePublisher();
77 participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
78 assertThatCode(() -> participantMessagePublisher.sendParticipantRegister(participantRegisterMsg))
79 .doesNotThrowAnyException();
84 void testReceiveParticipantRegisterAckMessage() throws Exception {
85 final ParticipantRegisterAck participantRegisterAckMsg = new ParticipantRegisterAck();
86 participantRegisterAckMsg.setMessage("ParticipantRegisterAck message");
87 participantRegisterAckMsg.setResponseTo(UUID.randomUUID());
88 participantRegisterAckMsg.setResult(true);
90 synchronized (lockit) {
91 ParticipantRegisterAckListener participantRegisterAckListener =
92 new ParticipantRegisterAckListener(participantHandler);
93 assertThatCode(() -> participantRegisterAckListener.onTopicEvent(INFRA, TOPIC, null,
94 participantRegisterAckMsg)).doesNotThrowAnyException();
99 void testSendParticipantDeregisterMessage() throws Exception {
100 final ParticipantDeregister participantDeregisterMsg = new ParticipantDeregister();
101 participantDeregisterMsg.setParticipantId(getParticipantId());
102 participantDeregisterMsg.setTimestamp(Instant.now());
103 participantDeregisterMsg.setParticipantType(getParticipantType());
105 synchronized (lockit) {
106 ParticipantMessagePublisher participantMessagePublisher =
107 new ParticipantMessagePublisher();
108 participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
109 assertThatCode(() -> participantMessagePublisher.sendParticipantDeregister(participantDeregisterMsg))
110 .doesNotThrowAnyException();
115 void testReceiveParticipantDeregisterAckMessage() throws Exception {
116 final ParticipantDeregisterAck participantDeregisterAckMsg = new ParticipantDeregisterAck();
117 participantDeregisterAckMsg.setMessage("ParticipantDeregisterAck message");
118 participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
119 participantDeregisterAckMsg.setResult(true);
121 synchronized (lockit) {
122 ParticipantDeregisterAckListener participantDeregisterAckListener =
123 new ParticipantDeregisterAckListener(participantHandler);
124 assertThatCode(() -> participantDeregisterAckListener.onTopicEvent(INFRA, TOPIC, null,
125 participantDeregisterAckMsg)).doesNotThrowAnyException();
130 void testReceiveParticipantUpdateMessage() throws Exception {
131 ParticipantUpdate participantUpdateMsg = TestListenerUtils.createParticipantUpdateMsg();
133 synchronized (lockit) {
134 ParticipantUpdateListener participantUpdateListener = new ParticipantUpdateListener(participantHandler);
135 participantUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateMsg);
138 // Verify the result of GET participants with what is stored
139 assertEquals("org.onap.PM_Policy", participantHandler.getParticipantId().getName());
143 void testSendParticipantUpdateAckMessage() throws Exception {
144 final ParticipantUpdateAck participantUpdateAckMsg = new ParticipantUpdateAck();
145 participantUpdateAckMsg.setMessage("ParticipantUpdateAck message");
146 participantUpdateAckMsg.setResponseTo(UUID.randomUUID());
147 participantUpdateAckMsg.setResult(true);
149 synchronized (lockit) {
150 ParticipantMessagePublisher participantMessagePublisher = new ParticipantMessagePublisher();
151 participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
152 assertThatCode(() -> participantMessagePublisher.sendParticipantUpdateAck(participantUpdateAckMsg))
153 .doesNotThrowAnyException();
158 void testParticipantStatusHeartbeat() throws Exception {
159 final ParticipantStatus heartbeat = participantHandler.makeHeartbeat(true);
160 synchronized (lockit) {
161 ParticipantMessagePublisher publisher = new ParticipantMessagePublisher();
162 publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
163 assertThatCode(() -> publisher.sendHeartbeat(heartbeat)).doesNotThrowAnyException();
167 private ToscaConceptIdentifier getParticipantId() {
168 return new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
171 private ToscaConceptIdentifier getParticipantType() {
172 return new ToscaConceptIdentifier("org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");