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 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;
50 @ExtendWith(SpringExtension.class)
51 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
52 @TestPropertySource(locations = {"classpath:application_test.properties"})
53 class ParticipantMessagesTest {
55 private static final Object lockit = new Object();
56 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
57 private static final String TOPIC = "my-topic";
60 private ParticipantHandler participantHandler;
63 void testSendParticipantRegisterMessage() throws Exception {
64 final ParticipantRegister participantRegisterMsg = new ParticipantRegister();
65 participantRegisterMsg.setParticipantId(getParticipantId());
66 participantRegisterMsg.setTimestamp(Instant.now());
67 participantRegisterMsg.setParticipantType(getParticipantType());
69 synchronized (lockit) {
70 ParticipantMessagePublisher participantMessagePublisher =
71 new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
72 participantMessagePublisher.sendParticipantRegister(participantRegisterMsg);
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);
83 synchronized (lockit) {
84 ParticipantRegisterAckListener participantRegisterAckListener =
85 new ParticipantRegisterAckListener(participantHandler);
86 participantRegisterAckListener.onTopicEvent(INFRA, TOPIC, null, participantRegisterAckMsg);
91 void testSendParticipantDeregisterMessage() throws Exception {
92 final ParticipantDeregister participantDeregisterMsg = new ParticipantDeregister();
93 participantDeregisterMsg.setParticipantId(getParticipantId());
94 participantDeregisterMsg.setTimestamp(Instant.now());
95 participantDeregisterMsg.setParticipantType(getParticipantType());
97 synchronized (lockit) {
98 ParticipantMessagePublisher participantMessagePublisher =
99 new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
100 participantMessagePublisher.sendParticipantDeregister(participantDeregisterMsg);
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);
111 synchronized (lockit) {
112 ParticipantDeregisterAckListener participantDeregisterAckListener =
113 new ParticipantDeregisterAckListener(participantHandler);
114 participantDeregisterAckListener.onTopicEvent(INFRA, TOPIC, null, participantDeregisterAckMsg);
119 void testReceiveParticipantUpdateMessage() throws Exception {
120 ParticipantUpdate participantUpdateMsg = TestListenerUtils.createParticipantUpdateMsg();
122 synchronized (lockit) {
123 ParticipantUpdateListener participantUpdateListener = new ParticipantUpdateListener(participantHandler);
124 participantUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateMsg);
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);
135 synchronized (lockit) {
136 ParticipantMessagePublisher participantMessagePublisher =
137 new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
138 participantMessagePublisher.sendParticipantUpdateAck(participantUpdateAckMsg);
142 private ToscaConceptIdentifier getParticipantId() {
143 return new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
146 private ToscaConceptIdentifier getParticipantType() {
147 return new ToscaConceptIdentifier("org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");