2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2021-2022 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.assertNull;
 
  28 import static org.junit.jupiter.api.Assertions.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.acm.participant.intermediary.comm.ParticipantMessagePublisher;
 
  36 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
 
  37 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
 
  38 import org.onap.policy.clamp.models.acm.concepts.ParticipantHealthStatus;
 
  39 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
 
  40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantAckMessage;
 
  41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessage;
 
  42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
 
  43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
 
  44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdate;
 
  45 import org.onap.policy.common.utils.coder.CoderException;
 
  46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
 
  48 class ParticipantHandlerTest {
 
  50     private final 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.getAcElementDefinitionCommonProperties(id)).isEmpty();
 
  68     void handleUpdateTest() {
 
  69         var parameters = CommonTestData.getParticipantParameters();
 
  70         var automationCompositionHander = commonTestData.getMockAutomationCompositionHandler();
 
  71         var publisher = new ParticipantMessagePublisher();
 
  72         var emptyParticipantHandler =
 
  73                 new ParticipantHandler(parameters, publisher, automationCompositionHander);
 
  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.setAutomationCompositionId(CommonTestData.AC_ID_1);
 
  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         assertThat(heartbeatF.getAutomationCompositionInfoList()).isEmpty();
 
  93         participantHandler.handleParticipantUpdate(participantUpdateMsg);
 
  94         assertThat(participantHandler.getAcElementDefinitionCommonProperties(id)).isEmpty();
 
  96         var heartbeatT = participantHandler.makeHeartbeat(true);
 
  97         assertEquals(id, heartbeatT.getParticipantId());
 
  98         assertThat(heartbeatT.getParticipantDefinitionUpdates()).isNotEmpty();
 
  99         assertEquals(id, heartbeatT.getParticipantDefinitionUpdates().get(0).getParticipantId());
 
 101         var pum = setListParticipantDefinition(participantUpdateMsg);
 
 102         participantHandler.handleParticipantUpdate(pum);
 
 103         var heartbeatTAfterUpdate = participantHandler.makeHeartbeat(true);
 
 104         assertEquals(id, heartbeatTAfterUpdate.getParticipantId());
 
 107     private ParticipantUpdate setListParticipantDefinition(ParticipantUpdate participantUpdateMsg) {
 
 108         var id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
 
 109         List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
 
 110         var def = new ParticipantDefinition();
 
 111         def.setParticipantId(id);
 
 112         def.setParticipantType(id);
 
 113         participantDefinitionUpdates.add(def);
 
 114         participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates);
 
 115         return participantUpdateMsg;
 
 119     void handleParticipantTest() {
 
 120         var participantHandler = commonTestData.getMockParticipantHandler();
 
 121         var id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
 
 122         var p = participantHandler.getParticipant(id.getName(), id.getVersion());
 
 123         assertEquals(ParticipantState.UNKNOWN, p.getParticipantState());
 
 125         participantHandler.updateParticipantState(id, ParticipantState.PASSIVE);
 
 126         var p2 = participantHandler.getParticipant(id.getName(), id.getVersion());
 
 127         assertEquals(ParticipantState.PASSIVE, p2.getParticipantState());
 
 129         var participantRegisterAckMsg = new ParticipantRegisterAck();
 
 130         participantRegisterAckMsg.setState(ParticipantState.TERMINATED);
 
 131         participantHandler.handleParticipantRegisterAck(participantRegisterAckMsg);
 
 132         assertEquals(ParticipantHealthStatus.HEALTHY, participantHandler.makeHeartbeat(false).getHealthStatus());
 
 134         var emptyid = new ToscaConceptIdentifier("", ID_VERSION);
 
 135         assertNull(participantHandler.updateParticipantState(emptyid, ParticipantState.PASSIVE));
 
 137         var sameid = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
 
 138         var participant = participantHandler.updateParticipantState(sameid, ParticipantState.PASSIVE);
 
 139         assertEquals(participant.getDefinition(), sameid);
 
 144     void checkAppliesTo() {
 
 145         var participantHandler = commonTestData.getMockParticipantHandler();
 
 146         var participantAckMsg =
 
 147                 new ParticipantAckMessage(ParticipantMessageType.AUTOMATION_COMPOSITION_UPDATE);
 
 148         assertTrue(participantHandler.appliesTo(participantAckMsg));
 
 151                 new ParticipantMessage(ParticipantMessageType.PARTICIPANT_STATUS);
 
 152         assertTrue(participantHandler.appliesTo(participantMsg));
 
 154         var emptyid = new ToscaConceptIdentifier("", ID_VERSION);
 
 155         participantMsg.setParticipantType(emptyid);
 
 156         assertFalse(participantHandler.appliesTo(participantMsg));
 
 161     void getAutomationCompositionInfoListTest() throws CoderException {
 
 162         var participantHandler = commonTestData.getParticipantHandlerAutomationCompositions();
 
 163         participantHandler.sendHeartbeat();
 
 164         assertEquals(CommonTestData.AC_ID_1, participantHandler.makeHeartbeat(false)
 
 165                 .getAutomationCompositionInfoList()
 
 167                 .getAutomationCompositionId());