a7acbdf4a3b7167a2f5df6f3f17d191ce34d0511
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2024 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
26 import java.time.Instant;
27 import java.util.Collections;
28 import java.util.UUID;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.api.extension.ExtendWith;
31 import org.mockito.Mockito;
32 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantDeregisterAckListener;
33 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
34 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantPrimeListener;
35 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantRegisterAckListener;
36 import org.onap.policy.clamp.acm.participant.intermediary.handler.ParticipantHandler;
37 import org.onap.policy.clamp.acm.participant.policy.main.parameters.CommonTestData;
38 import org.onap.policy.clamp.acm.participant.policy.main.utils.TestListenerUtils;
39 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregister;
40 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregisterAck;
41 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrimeAck;
42 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegister;
43 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegisterAck;
44 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
45 import org.onap.policy.common.endpoints.event.comm.TopicSink;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
49 import org.springframework.test.context.ActiveProfiles;
50 import org.springframework.test.context.junit.jupiter.SpringExtension;
51
52 @ExtendWith(SpringExtension.class)
53 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
54 @ActiveProfiles("test")
55 class ParticipantMessagesTest {
56
57     private static final Object lockit = new Object();
58     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
59     private static final String TOPIC = "my-topic";
60
61     @Autowired
62     private ParticipantHandler participantHandler;
63
64     @Test
65     void testSendParticipantRegisterMessage() {
66         final var participantRegisterMsg = new ParticipantRegister();
67         participantRegisterMsg.setParticipantId(CommonTestData.getParticipantId());
68         participantRegisterMsg.setTimestamp(Instant.now());
69
70         synchronized (lockit) {
71             var participantMessagePublisher = new ParticipantMessagePublisher();
72             participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
73             assertThatCode(() -> participantMessagePublisher.sendParticipantRegister(participantRegisterMsg))
74                 .doesNotThrowAnyException();
75         }
76     }
77
78     @Test
79     void testReceiveParticipantRegisterAckMessage() {
80         final var participantRegisterAckMsg = new ParticipantRegisterAck();
81         participantRegisterAckMsg.setMessage("ParticipantRegisterAck message");
82         participantRegisterAckMsg.setResponseTo(UUID.randomUUID());
83         participantRegisterAckMsg.setResult(true);
84
85         synchronized (lockit) {
86             var participantRegisterAckListener = new ParticipantRegisterAckListener(participantHandler);
87             assertThatCode(() -> participantRegisterAckListener.onTopicEvent(INFRA, TOPIC, null,
88                 participantRegisterAckMsg)).doesNotThrowAnyException();
89         }
90     }
91
92     @Test
93     void testSendParticipantDeregisterMessage() {
94         final var participantDeregisterMsg = new ParticipantDeregister();
95         participantDeregisterMsg.setParticipantId(CommonTestData.getParticipantId());
96         participantDeregisterMsg.setTimestamp(Instant.now());
97
98         synchronized (lockit) {
99             var participantMessagePublisher = new ParticipantMessagePublisher();
100             participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
101             assertThatCode(() -> participantMessagePublisher.sendParticipantDeregister(participantDeregisterMsg))
102                 .doesNotThrowAnyException();
103         }
104     }
105
106     @Test
107     void testReceiveParticipantDeregisterAckMessage() {
108         final var participantDeregisterAckMsg = new ParticipantDeregisterAck();
109         participantDeregisterAckMsg.setMessage("ParticipantDeregisterAck message");
110         participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
111         participantDeregisterAckMsg.setResult(true);
112
113         synchronized (lockit) {
114             var participantDeregisterAckListener = new ParticipantDeregisterAckListener(participantHandler);
115             assertThatCode(() -> participantDeregisterAckListener.onTopicEvent(INFRA, TOPIC, null,
116                 participantDeregisterAckMsg)).doesNotThrowAnyException();
117         }
118     }
119
120     @Test
121     void testReceiveParticipantUpdateMessage() {
122         var participantPrimeMsg = TestListenerUtils.createParticipantPrimeMsg();
123
124         synchronized (lockit) {
125             var participantPrimeListener = new ParticipantPrimeListener(participantHandler);
126             assertThatCode(() -> participantPrimeListener.onTopicEvent(INFRA, TOPIC, null, participantPrimeMsg))
127                     .doesNotThrowAnyException();
128         }
129     }
130
131     @Test
132     void testSendParticipantPrimeAckMessage() {
133         final var participantPrimeAckMsg = new ParticipantPrimeAck();
134         participantPrimeAckMsg.setMessage("ParticipantPrimeAck message");
135         participantPrimeAckMsg.setResponseTo(UUID.randomUUID());
136         participantPrimeAckMsg.setResult(true);
137
138         synchronized (lockit) {
139             var participantMessagePublisher = new ParticipantMessagePublisher();
140             participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
141             assertThatCode(() -> participantMessagePublisher.sendParticipantPrimeAck(participantPrimeAckMsg))
142                 .doesNotThrowAnyException();
143         }
144     }
145 }