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
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.controlloop.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.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
30 import java.time.Instant;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.UUID;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantAckMessage;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessage;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType;
41 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck;
42 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
43 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher;
44 import org.onap.policy.clamp.controlloop.participant.intermediary.main.parameters.CommonTestData;
45 import org.onap.policy.common.utils.coder.CoderException;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
48 class ParticipantHandlerTest {
50 private CommonTestData commonTestData = new CommonTestData();
51 private static final String ID_NAME = "org.onap.PM_CDS_Blueprint";
52 private static final String ID_VERSION = "1.0.1";
55 void mockParticipantHandlerTest() {
56 var participantHandler = commonTestData.getMockParticipantHandler();
57 assertNull(participantHandler.getParticipant(null, null));
58 assertEquals("org.onap.PM_CDS_Blueprint 1.0.1", participantHandler.getParticipantId().toString());
60 var id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
61 assertEquals(id, participantHandler.getParticipantId());
62 assertEquals(id, participantHandler.getParticipantType());
63 assertThat(participantHandler.getClElementDefinitionCommonProperties(id)).isEmpty();
68 void handleUpdateTest() {
69 var parameters = CommonTestData.getParticipantParameters();
70 var controlLoopHander = commonTestData.getMockControlLoopHandler();
71 var publisher = new ParticipantMessagePublisher();
72 var emptyParticipantHandler =
73 new ParticipantHandler(parameters, publisher, controlLoopHander);
74 var participantUpdateMsg = new ParticipantUpdate();
76 assertThatThrownBy(() ->
77 emptyParticipantHandler.handleParticipantUpdate(participantUpdateMsg))
78 .isInstanceOf(RuntimeException.class);
80 var participantHandler = commonTestData.getMockParticipantHandler();
82 var id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
83 participantUpdateMsg.setControlLoopId(id);
84 participantUpdateMsg.setParticipantId(id);
85 participantUpdateMsg.setParticipantType(id);
86 participantUpdateMsg.setMessageId(UUID.randomUUID());
87 participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
89 var heartbeatF = participantHandler.makeHeartbeat(false);
90 assertEquals(id, heartbeatF.getParticipantId());
91 assertEquals(ParticipantState.UNKNOWN, heartbeatF.getParticipantStatistics().getState());
92 assertThat(heartbeatF.getControlLoopInfoList()).isEmpty();
94 participantHandler.handleParticipantUpdate(participantUpdateMsg);
95 assertThat(participantHandler.getClElementDefinitionCommonProperties(id)).isEmpty();
97 var heartbeatT = participantHandler.makeHeartbeat(true);
98 assertEquals(id, heartbeatT.getParticipantId());
99 assertEquals(ParticipantState.TERMINATED, heartbeatT.getParticipantStatistics().getState());
100 assertThat(heartbeatT.getParticipantDefinitionUpdates()).isNotEmpty();
101 assertEquals(id, heartbeatT.getParticipantDefinitionUpdates().get(0).getParticipantId());
103 var pum = setListParticipantDefinition(participantUpdateMsg);
104 participantHandler.handleParticipantUpdate(pum);
105 var heartbeatTAfterUpdate = participantHandler.makeHeartbeat(true);
106 assertEquals(id, heartbeatTAfterUpdate.getParticipantId());
107 assertEquals(ParticipantState.PASSIVE, heartbeatTAfterUpdate.getParticipantStatistics().getState());
111 private ParticipantUpdate setListParticipantDefinition(ParticipantUpdate participantUpdateMsg) {
112 var id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
113 List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
114 var def = new ParticipantDefinition();
115 def.setParticipantId(id);
116 def.setParticipantType(id);
117 participantDefinitionUpdates.add(def);
118 participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates);
119 return participantUpdateMsg;
123 void handleParticipantTest() {
124 var participantHandler = commonTestData.getMockParticipantHandler();
125 var id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
126 var p = participantHandler.getParticipant(id.getName(), id.getVersion());
127 assertEquals(ParticipantState.UNKNOWN, p.getParticipantState());
129 participantHandler.updateParticipantState(id, ParticipantState.PASSIVE);
130 var p2 = participantHandler.getParticipant(id.getName(), id.getVersion());
131 assertEquals(ParticipantState.PASSIVE, p2.getParticipantState());
133 var participantRegisterAckMsg = new ParticipantRegisterAck();
134 participantRegisterAckMsg.setState(ParticipantState.TERMINATED);
135 participantHandler.handleParticipantRegisterAck(participantRegisterAckMsg);
136 assertEquals(ParticipantHealthStatus.HEALTHY, participantHandler.makeHeartbeat(false).getHealthStatus());
138 var emptyid = new ToscaConceptIdentifier("", ID_VERSION);
139 assertNull(participantHandler.updateParticipantState(emptyid, ParticipantState.PASSIVE));
141 var sameid = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
142 var participant = participantHandler.updateParticipantState(sameid, ParticipantState.PASSIVE);
143 assertEquals(participant.getDefinition(), sameid);
148 void checkAppliesTo() {
149 var participantHandler = commonTestData.getMockParticipantHandler();
150 var participantAckMsg =
151 new ParticipantAckMessage(ParticipantMessageType.CONTROL_LOOP_UPDATE);
152 assertTrue(participantHandler.appliesTo(participantAckMsg));
155 new ParticipantMessage(ParticipantMessageType.PARTICIPANT_STATUS);
156 assertTrue(participantHandler.appliesTo(participantMsg));
158 var emptyid = new ToscaConceptIdentifier("", ID_VERSION);
159 participantMsg.setParticipantType(emptyid);
160 assertFalse(participantHandler.appliesTo(participantMsg));
165 void getControlLoopInfoListTest() throws CoderException {
166 var participantHandler = commonTestData.getParticipantHandlerControlLoops();
167 var id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
168 participantHandler.sendHeartbeat();
169 assertEquals(id, participantHandler.makeHeartbeat(false)
170 .getControlLoopInfoList()
172 .getControlLoopId());