555183227f0d49bb92e87eb8e0ccd1c592c2c3dc
[policy/clamp.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.clamp.acm.participant.policy.endtoend;
23
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26
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.ParticipantRegisterAckListener;
36 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantUpdateListener;
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.ParticipantRegister;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdate;
46 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdateAck;
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;
54
55 @ExtendWith(SpringExtension.class)
56 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
57 @ActiveProfiles("test")
58 class ParticipantMessagesTest {
59
60     private static final Object lockit = new Object();
61     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
62     private static final String TOPIC = "my-topic";
63
64     @Autowired
65     private ParticipantHandler participantHandler;
66
67     @Test
68     void testSendParticipantRegisterMessage() {
69         final ParticipantRegister participantRegisterMsg = new ParticipantRegister();
70         participantRegisterMsg.setParticipantId(CommonTestData.getParticipantId());
71         participantRegisterMsg.setTimestamp(Instant.now());
72
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();
79         }
80     }
81
82     @Test
83     void testReceiveParticipantRegisterAckMessage() {
84         final ParticipantRegisterAck participantRegisterAckMsg = new ParticipantRegisterAck();
85         participantRegisterAckMsg.setMessage("ParticipantRegisterAck message");
86         participantRegisterAckMsg.setResponseTo(UUID.randomUUID());
87         participantRegisterAckMsg.setResult(true);
88
89         synchronized (lockit) {
90             ParticipantRegisterAckListener participantRegisterAckListener =
91                 new ParticipantRegisterAckListener(participantHandler);
92             assertThatCode(() -> participantRegisterAckListener.onTopicEvent(INFRA, TOPIC, null,
93                 participantRegisterAckMsg)).doesNotThrowAnyException();
94         }
95     }
96
97     @Test
98     void testSendParticipantDeregisterMessage() {
99         final ParticipantDeregister participantDeregisterMsg = new ParticipantDeregister();
100         participantDeregisterMsg.setParticipantId(CommonTestData.getParticipantId());
101         participantDeregisterMsg.setTimestamp(Instant.now());
102
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();
109         }
110     }
111
112     @Test
113     void testReceiveParticipantDeregisterAckMessage() {
114         final ParticipantDeregisterAck participantDeregisterAckMsg = new ParticipantDeregisterAck();
115         participantDeregisterAckMsg.setMessage("ParticipantDeregisterAck message");
116         participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
117         participantDeregisterAckMsg.setResult(true);
118
119         synchronized (lockit) {
120             ParticipantDeregisterAckListener participantDeregisterAckListener =
121                 new ParticipantDeregisterAckListener(participantHandler);
122             assertThatCode(() -> participantDeregisterAckListener.onTopicEvent(INFRA, TOPIC, null,
123                 participantDeregisterAckMsg)).doesNotThrowAnyException();
124         }
125     }
126
127     @Test
128     void testReceiveParticipantUpdateMessage() {
129         ParticipantUpdate participantUpdateMsg = TestListenerUtils.createParticipantUpdateMsg();
130
131         synchronized (lockit) {
132             ParticipantUpdateListener participantUpdateListener = new ParticipantUpdateListener(participantHandler);
133             participantUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateMsg);
134         }
135
136         // Verify the result of GET participants with what is stored
137         assertEquals(CommonTestData.getParticipantId(), participantHandler.getParticipantId());
138     }
139
140     @Test
141     void testSendParticipantUpdateAckMessage() {
142         final ParticipantUpdateAck participantUpdateAckMsg = new ParticipantUpdateAck();
143         participantUpdateAckMsg.setMessage("ParticipantUpdateAck message");
144         participantUpdateAckMsg.setResponseTo(UUID.randomUUID());
145         participantUpdateAckMsg.setResult(true);
146
147         synchronized (lockit) {
148             ParticipantMessagePublisher participantMessagePublisher = new ParticipantMessagePublisher();
149             participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
150             assertThatCode(() -> participantMessagePublisher.sendParticipantUpdateAck(participantUpdateAckMsg))
151                 .doesNotThrowAnyException();
152         }
153     }
154
155     @Test
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();
162         }
163     }
164 }