6d52d7a9403ebe427e07ad70e6564c25973e252b
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 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.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.boot.test.context.SpringBootTest;
52 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
53 import org.springframework.test.context.ActiveProfiles;
54 import org.springframework.test.context.junit.jupiter.SpringExtension;
55
56 @ExtendWith(SpringExtension.class)
57 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
58 @ActiveProfiles("test")
59 class ParticipantMessagesTest {
60
61     private static final Object lockit = new Object();
62     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
63     private static final String TOPIC = "my-topic";
64
65     @Autowired
66     private ParticipantHandler participantHandler;
67
68     @Test
69     void testSendParticipantRegisterMessage() {
70         final ParticipantRegister participantRegisterMsg = new ParticipantRegister();
71         participantRegisterMsg.setParticipantId(CommonTestData.getParticipantId());
72         participantRegisterMsg.setTimestamp(Instant.now());
73         participantRegisterMsg.setParticipantType(getParticipantType());
74
75         synchronized (lockit) {
76             ParticipantMessagePublisher participantMessagePublisher =
77                 new ParticipantMessagePublisher();
78             participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
79             assertThatCode(() -> participantMessagePublisher.sendParticipantRegister(participantRegisterMsg))
80                 .doesNotThrowAnyException();
81         }
82     }
83
84     @Test
85     void testReceiveParticipantRegisterAckMessage() {
86         final ParticipantRegisterAck participantRegisterAckMsg = new ParticipantRegisterAck();
87         participantRegisterAckMsg.setMessage("ParticipantRegisterAck message");
88         participantRegisterAckMsg.setResponseTo(UUID.randomUUID());
89         participantRegisterAckMsg.setResult(true);
90
91         synchronized (lockit) {
92             ParticipantRegisterAckListener participantRegisterAckListener =
93                 new ParticipantRegisterAckListener(participantHandler);
94             assertThatCode(() -> participantRegisterAckListener.onTopicEvent(INFRA, TOPIC, null,
95                 participantRegisterAckMsg)).doesNotThrowAnyException();
96         }
97     }
98
99     @Test
100     void testSendParticipantDeregisterMessage() {
101         final ParticipantDeregister participantDeregisterMsg = new ParticipantDeregister();
102         participantDeregisterMsg.setParticipantId(CommonTestData.getParticipantId());
103         participantDeregisterMsg.setTimestamp(Instant.now());
104         participantDeregisterMsg.setParticipantType(getParticipantType());
105
106         synchronized (lockit) {
107             ParticipantMessagePublisher participantMessagePublisher =
108                 new ParticipantMessagePublisher();
109             participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
110             assertThatCode(() -> participantMessagePublisher.sendParticipantDeregister(participantDeregisterMsg))
111                 .doesNotThrowAnyException();
112         }
113     }
114
115     @Test
116     void testReceiveParticipantDeregisterAckMessage() {
117         final ParticipantDeregisterAck participantDeregisterAckMsg = new ParticipantDeregisterAck();
118         participantDeregisterAckMsg.setMessage("ParticipantDeregisterAck message");
119         participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
120         participantDeregisterAckMsg.setResult(true);
121
122         synchronized (lockit) {
123             ParticipantDeregisterAckListener participantDeregisterAckListener =
124                 new ParticipantDeregisterAckListener(participantHandler);
125             assertThatCode(() -> participantDeregisterAckListener.onTopicEvent(INFRA, TOPIC, null,
126                 participantDeregisterAckMsg)).doesNotThrowAnyException();
127         }
128     }
129
130     @Test
131     void testReceiveParticipantUpdateMessage() {
132         ParticipantUpdate participantUpdateMsg = TestListenerUtils.createParticipantUpdateMsg();
133
134         synchronized (lockit) {
135             ParticipantUpdateListener participantUpdateListener = new ParticipantUpdateListener(participantHandler);
136             participantUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateMsg);
137         }
138
139         // Verify the result of GET participants with what is stored
140         assertEquals(CommonTestData.getParticipantId(), participantHandler.getParticipantId());
141     }
142
143     @Test
144     void testSendParticipantUpdateAckMessage() {
145         final ParticipantUpdateAck participantUpdateAckMsg = new ParticipantUpdateAck();
146         participantUpdateAckMsg.setMessage("ParticipantUpdateAck message");
147         participantUpdateAckMsg.setResponseTo(UUID.randomUUID());
148         participantUpdateAckMsg.setResult(true);
149
150         synchronized (lockit) {
151             ParticipantMessagePublisher participantMessagePublisher = new ParticipantMessagePublisher();
152             participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
153             assertThatCode(() -> participantMessagePublisher.sendParticipantUpdateAck(participantUpdateAckMsg))
154                 .doesNotThrowAnyException();
155         }
156     }
157
158     @Test
159     void testParticipantStatusHeartbeat() {
160         final ParticipantStatus heartbeat = participantHandler.makeHeartbeat(true);
161         synchronized (lockit) {
162             ParticipantMessagePublisher publisher = new ParticipantMessagePublisher();
163             publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
164             assertThatCode(() -> publisher.sendHeartbeat(heartbeat)).doesNotThrowAnyException();
165         }
166     }
167
168     private ToscaConceptIdentifier getParticipantType() {
169         return new ToscaConceptIdentifier("org.onap.policy.acm.PolicyAutomationCompositionParticipant", "2.3.1");
170     }
171 }