093ac190c5fc2940b88f5aea6034d6fdc96dc546
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.participant.policy.endtoend;
22
23 import static org.assertj.core.api.Assertions.assertThatCode;
24
25 import java.time.Instant;
26 import java.util.Collections;
27 import java.util.UUID;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.extension.ExtendWith;
30 import org.mockito.Mockito;
31 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
32 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck;
33 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
34 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck;
35 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseDetails;
36 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
37 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
39 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantDeregisterAckListener;
40 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher;
41 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantRegisterAckListener;
42 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantUpdateListener;
43 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
44 import org.onap.policy.clamp.controlloop.participant.policy.main.utils.TestListenerUtils;
45 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
46 import org.onap.policy.common.endpoints.event.comm.TopicSink;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.boot.test.context.SpringBootTest;
50 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
51 import org.springframework.test.context.TestPropertySource;
52 import org.springframework.test.context.junit.jupiter.SpringExtension;
53
54 @ExtendWith(SpringExtension.class)
55 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
56 @TestPropertySource(locations = {"classpath:application_test.properties"})
57 class ParticipantMessagesTest {
58
59     private static final Object lockit = new Object();
60     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
61     private static final String TOPIC = "my-topic";
62
63     @Autowired
64     private ParticipantHandler participantHandler;
65
66     @Test
67     void testSendParticipantRegisterMessage() throws Exception {
68         final ParticipantRegister participantRegisterMsg = new ParticipantRegister();
69         participantRegisterMsg.setParticipantId(getParticipantId());
70         participantRegisterMsg.setTimestamp(Instant.now());
71         participantRegisterMsg.setParticipantType(getParticipantType());
72
73         synchronized (lockit) {
74             ParticipantMessagePublisher participantMessagePublisher =
75                     new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
76             participantMessagePublisher.sendParticipantRegister(participantRegisterMsg);
77         }
78     }
79
80     @Test
81     void testReceiveParticipantRegisterAckMessage() throws Exception {
82         final ParticipantRegisterAck participantRegisterAckMsg = new ParticipantRegisterAck();
83         participantRegisterAckMsg.setMessage("ParticipantRegisterAck message");
84         participantRegisterAckMsg.setResponseTo(UUID.randomUUID());
85         participantRegisterAckMsg.setResult(true);
86
87         synchronized (lockit) {
88             ParticipantRegisterAckListener participantRegisterAckListener =
89                 new ParticipantRegisterAckListener(participantHandler);
90             participantRegisterAckListener.onTopicEvent(INFRA, TOPIC, null, participantRegisterAckMsg);
91         }
92     }
93
94     @Test
95     void testSendParticipantDeregisterMessage() throws Exception {
96         final ParticipantDeregister participantDeregisterMsg = new ParticipantDeregister();
97         participantDeregisterMsg.setParticipantId(getParticipantId());
98         participantDeregisterMsg.setTimestamp(Instant.now());
99         participantDeregisterMsg.setParticipantType(getParticipantType());
100
101         synchronized (lockit) {
102             ParticipantMessagePublisher participantMessagePublisher =
103                     new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
104             participantMessagePublisher.sendParticipantDeregister(participantDeregisterMsg);
105         }
106     }
107
108     @Test
109     void testReceiveParticipantDeregisterAckMessage() throws Exception {
110         final ParticipantDeregisterAck participantDeregisterAckMsg = new ParticipantDeregisterAck();
111         participantDeregisterAckMsg.setMessage("ParticipantDeregisterAck message");
112         participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
113         participantDeregisterAckMsg.setResult(true);
114
115         synchronized (lockit) {
116             ParticipantDeregisterAckListener participantDeregisterAckListener =
117                     new ParticipantDeregisterAckListener(participantHandler);
118             participantDeregisterAckListener.onTopicEvent(INFRA, TOPIC, null, participantDeregisterAckMsg);
119         }
120     }
121
122     @Test
123     void testReceiveParticipantUpdateMessage() throws Exception {
124         ParticipantUpdate participantUpdateMsg = TestListenerUtils.createParticipantUpdateMsg();
125
126         synchronized (lockit) {
127             ParticipantUpdateListener participantUpdateListener = new ParticipantUpdateListener(participantHandler);
128             participantUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateMsg);
129         }
130     }
131
132     @Test
133     void testSendParticipantUpdateAckMessage() throws Exception {
134         final ParticipantUpdateAck participantUpdateAckMsg = new ParticipantUpdateAck();
135         participantUpdateAckMsg.setMessage("ParticipantUpdateAck message");
136         participantUpdateAckMsg.setResponseTo(UUID.randomUUID());
137         participantUpdateAckMsg.setResult(true);
138
139         synchronized (lockit) {
140             ParticipantMessagePublisher participantMessagePublisher =
141                     new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
142             participantMessagePublisher.sendParticipantUpdateAck(participantUpdateAckMsg);
143         }
144     }
145
146     @Test
147     void testParticipantStatusHeartbeat() throws Exception {
148         final ParticipantStatus heartbeat = new ParticipantStatus();
149         heartbeat.setMessage("ParticipantStatus message");
150         heartbeat.setResponse(new ParticipantResponseDetails());
151         heartbeat.setParticipantId(getParticipantId());
152         synchronized (lockit) {
153             ParticipantMessagePublisher publisher =
154                     new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
155             assertThatCode(() -> publisher.sendHeartbeat(heartbeat)).doesNotThrowAnyException();
156         }
157     }
158
159
160     private ToscaConceptIdentifier getParticipantId() {
161         return new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
162     }
163
164     private ToscaConceptIdentifier getParticipantType() {
165         return new ToscaConceptIdentifier("org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");
166     }
167 }