c17d2c53d577e50e7e1f29bab40c9fdca31c1caa
[policy/clamp.git] /
1 /*-
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
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.Assert.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.utils.TestListenerUtils;
39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregisterAck;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdate;
45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdateAck;
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;
54
55 @ExtendWith(SpringExtension.class)
56 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
57 @TestPropertySource(locations = {"classpath:application_test.properties"})
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() throws Exception {
69         final ParticipantRegister participantRegisterMsg = new ParticipantRegister();
70         participantRegisterMsg.setParticipantId(getParticipantId());
71         participantRegisterMsg.setTimestamp(Instant.now());
72         participantRegisterMsg.setParticipantType(getParticipantType());
73
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();
80         }
81     }
82
83     @Test
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);
89
90         synchronized (lockit) {
91             ParticipantRegisterAckListener participantRegisterAckListener =
92                 new ParticipantRegisterAckListener(participantHandler);
93             assertThatCode(() -> participantRegisterAckListener.onTopicEvent(INFRA, TOPIC, null,
94                             participantRegisterAckMsg)).doesNotThrowAnyException();
95         }
96     }
97
98     @Test
99     void testSendParticipantDeregisterMessage() throws Exception {
100         final ParticipantDeregister participantDeregisterMsg = new ParticipantDeregister();
101         participantDeregisterMsg.setParticipantId(getParticipantId());
102         participantDeregisterMsg.setTimestamp(Instant.now());
103         participantDeregisterMsg.setParticipantType(getParticipantType());
104
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();
111         }
112     }
113
114     @Test
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);
120
121         synchronized (lockit) {
122             ParticipantDeregisterAckListener participantDeregisterAckListener =
123                             new ParticipantDeregisterAckListener(participantHandler);
124             assertThatCode(() -> participantDeregisterAckListener.onTopicEvent(INFRA, TOPIC, null,
125                             participantDeregisterAckMsg)).doesNotThrowAnyException();
126         }
127     }
128
129     @Test
130     void testReceiveParticipantUpdateMessage() throws Exception {
131         ParticipantUpdate participantUpdateMsg = TestListenerUtils.createParticipantUpdateMsg();
132
133         synchronized (lockit) {
134             ParticipantUpdateListener participantUpdateListener = new ParticipantUpdateListener(participantHandler);
135             participantUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateMsg);
136         }
137
138         // Verify the result of GET participants with what is stored
139         assertEquals("org.onap.PM_Policy", participantHandler.getParticipantId().getName());
140     }
141
142     @Test
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);
148
149         synchronized (lockit) {
150             ParticipantMessagePublisher participantMessagePublisher = new ParticipantMessagePublisher();
151             participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
152             assertThatCode(() -> participantMessagePublisher.sendParticipantUpdateAck(participantUpdateAckMsg))
153                             .doesNotThrowAnyException();
154         }
155     }
156
157     @Test
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();
164         }
165     }
166
167     private ToscaConceptIdentifier getParticipantId() {
168         return new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
169     }
170
171     private ToscaConceptIdentifier getParticipantType() {
172         return new ToscaConceptIdentifier("org.onap.policy.acm.PolicyAutomationCompositionParticipant", "2.3.1");
173     }
174 }