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;
29 import java.time.Instant;
30 import java.util.List;
31 import java.util.UUID;
32 import org.junit.jupiter.api.Test;
33 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
34 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
35 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
36 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantAckMessage;
37 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessage;
38 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdate;
40 import org.onap.policy.common.utils.coder.CoderException;
42 class ParticipantHandlerTest {
44 private final CommonTestData commonTestData = new CommonTestData();
45 private static final String ID_NAME = "org.onap.PM_CDS_Blueprint";
46 private static final String ID_VERSION = "1.0.1";
49 void handleUpdateTest() {
50 var parameters = CommonTestData.getParticipantParameters();
51 var automationCompositionHander = commonTestData.getMockAutomationCompositionHandler();
52 var publisher = new ParticipantMessagePublisher();
53 var emptyParticipantHandler =
54 new ParticipantHandler(parameters, publisher, automationCompositionHander);
55 var participantUpdateMsg = new ParticipantUpdate();
57 assertThatThrownBy(() ->
58 emptyParticipantHandler.handleParticipantUpdate(participantUpdateMsg))
59 .isInstanceOf(RuntimeException.class);
61 var participantHandler = commonTestData.getMockParticipantHandler();
63 var participantId = CommonTestData.getParticipantId();
64 participantUpdateMsg.setAutomationCompositionId(CommonTestData.AC_ID_1);
65 participantUpdateMsg.setCompositionId(CommonTestData.AC_ID_1);
66 participantUpdateMsg.setParticipantId(participantId);
67 participantUpdateMsg.setMessageId(UUID.randomUUID());
68 participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
70 var heartbeatF = participantHandler.makeHeartbeat(false);
71 assertEquals(participantId, heartbeatF.getParticipantId());
72 assertThat(heartbeatF.getAutomationCompositionInfoList()).isEmpty();
74 participantHandler.handleParticipantUpdate(participantUpdateMsg);
76 var heartbeatT = participantHandler.makeHeartbeat(true);
77 assertEquals(participantId, heartbeatT.getParticipantId());
78 assertThat(heartbeatT.getParticipantDefinitionUpdates()).isNotEmpty();
79 assertEquals(participantId, heartbeatT.getParticipantDefinitionUpdates().get(0).getParticipantId());
81 var pum = setListParticipantDefinition(participantUpdateMsg);
82 participantHandler.handleParticipantUpdate(pum);
83 var heartbeatTAfterUpdate = participantHandler.makeHeartbeat(true);
84 assertEquals(participantId, heartbeatTAfterUpdate.getParticipantId());
87 private ParticipantUpdate setListParticipantDefinition(ParticipantUpdate participantUpdateMsg) {
88 var def = new ParticipantDefinition();
89 def.setParticipantId(CommonTestData.getParticipantId());
90 participantUpdateMsg.setParticipantDefinitionUpdates(List.of(def));
91 return participantUpdateMsg;
95 void checkAppliesTo() {
96 var participantHandler = commonTestData.getMockParticipantHandler();
97 var participantAckMsg =
98 new ParticipantAckMessage(ParticipantMessageType.AUTOMATION_COMPOSITION_UPDATE);
99 assertTrue(participantHandler.appliesTo(participantAckMsg));
102 new ParticipantMessage(ParticipantMessageType.PARTICIPANT_STATUS);
103 assertTrue(participantHandler.appliesTo(participantMsg));
105 var randomId = UUID.randomUUID();
106 participantMsg.setParticipantId(randomId);
107 assertFalse(participantHandler.appliesTo(participantMsg));
112 void getAutomationCompositionInfoListTest() throws CoderException {
113 var participantHandler = commonTestData.getParticipantHandlerAutomationCompositions();
114 participantHandler.sendHeartbeat();
115 assertEquals(CommonTestData.AC_ID_1, participantHandler.makeHeartbeat(false)
116 .getAutomationCompositionInfoList()
118 .getAutomationCompositionId());