2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2023 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.participant.intermediary.handler;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.verify;
32 import java.time.Instant;
33 import java.util.List;
34 import java.util.UUID;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
37 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
38 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantAckMessage;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessage;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantPrime;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
44 import org.onap.policy.common.utils.coder.CoderException;
46 class ParticipantHandlerTest {
48 private final CommonTestData commonTestData = new CommonTestData();
51 void handleUpdateTest() {
52 var parameters = CommonTestData.getParticipantParameters();
53 var automationCompositionHander = commonTestData.getMockAutomationCompositionHandler();
54 var publisher = new ParticipantMessagePublisher();
55 var emptyParticipantHandler =
56 new ParticipantHandler(parameters, publisher, automationCompositionHander);
57 var participantPrimeMsg = new ParticipantPrime();
59 assertThatThrownBy(() ->
60 emptyParticipantHandler.handleParticipantPrime(participantPrimeMsg))
61 .isInstanceOf(RuntimeException.class);
63 var participantHandler = commonTestData.getMockParticipantHandler();
65 var participantId = CommonTestData.getParticipantId();
66 participantPrimeMsg.setAutomationCompositionId(CommonTestData.AC_ID_1);
67 participantPrimeMsg.setCompositionId(CommonTestData.AC_ID_1);
68 participantPrimeMsg.setParticipantId(participantId);
69 participantPrimeMsg.setMessageId(UUID.randomUUID());
70 participantPrimeMsg.setTimestamp(Instant.ofEpochMilli(3000));
72 var heartbeatF = participantHandler.makeHeartbeat(false);
73 assertEquals(participantId, heartbeatF.getParticipantId());
74 assertThat(heartbeatF.getAutomationCompositionInfoList()).isEmpty();
76 participantHandler.handleParticipantPrime(participantPrimeMsg);
78 var heartbeatT = participantHandler.makeHeartbeat(true);
79 assertEquals(participantId, heartbeatT.getParticipantId());
80 assertThat(heartbeatT.getParticipantDefinitionUpdates()).isNotEmpty();
81 assertEquals(participantId, heartbeatT.getParticipantDefinitionUpdates().get(0).getParticipantId());
83 var pum = setListParticipantDefinition(participantPrimeMsg);
84 participantHandler.handleParticipantPrime(pum);
85 var heartbeatTAfterUpdate = participantHandler.makeHeartbeat(true);
86 assertEquals(participantId, heartbeatTAfterUpdate.getParticipantId());
89 private ParticipantPrime setListParticipantDefinition(ParticipantPrime participantPrimeMsg) {
90 var def = new ParticipantDefinition();
91 def.setParticipantId(CommonTestData.getParticipantId());
92 participantPrimeMsg.setParticipantDefinitionUpdates(List.of(def));
93 return participantPrimeMsg;
97 void checkAppliesTo() {
98 var participantHandler = commonTestData.getMockParticipantHandler();
99 var participantAckMsg =
100 new ParticipantAckMessage(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY);
101 assertTrue(participantHandler.appliesTo(participantAckMsg));
104 new ParticipantMessage(ParticipantMessageType.PARTICIPANT_STATUS);
105 assertTrue(participantHandler.appliesTo(participantMsg));
107 var randomId = UUID.randomUUID();
108 participantMsg.setParticipantId(randomId);
109 assertFalse(participantHandler.appliesTo(participantMsg));
114 void getAutomationCompositionInfoListTest() throws CoderException {
115 var participantHandler = commonTestData.getParticipantHandlerAutomationCompositions();
116 participantHandler.sendHeartbeat();
117 assertEquals(CommonTestData.AC_ID_1, participantHandler.makeHeartbeat(false)
118 .getAutomationCompositionInfoList()
120 .getAutomationCompositionId());
125 void testHandleParticipantRegisterAck() {
126 var parameters = CommonTestData.getParticipantParameters();
127 var automationCompositionHandler = commonTestData.getMockAutomationCompositionHandler();
128 var publisher = mock(ParticipantMessagePublisher.class);
129 var participantHandler = new ParticipantHandler(parameters, publisher, automationCompositionHandler);
131 participantHandler.handleParticipantRegisterAck(new ParticipantRegisterAck());
132 verify(publisher).sendParticipantStatus(any());