2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2021-2023 Nordix Foundation.
 
   4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
 
   5  * ================================================================================
 
   6  * Licensed under the Apache License, Version 2.0 (the "License");
 
   7  * you may not use this file except in compliance with the License.
 
   8  * You may obtain a copy of the License at
 
  10  *      http://www.apache.org/licenses/LICENSE-2.0
 
  12  * Unless required by applicable law or agreed to in writing, software
 
  13  * distributed under the License is distributed on an "AS IS" BASIS,
 
  14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  15  * See the License for the specific language governing permissions and
 
  16  * limitations under the License.
 
  18  * SPDX-License-Identifier: Apache-2.0
 
  19  * ============LICENSE_END=========================================================
 
  22 package org.onap.policy.clamp.acm.runtime.supervision.comm;
 
  24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
  25 import static org.mockito.ArgumentMatchers.anyString;
 
  26 import static org.mockito.Mockito.mock;
 
  27 import static org.mockito.Mockito.verify;
 
  28 import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_SERVICE_TEMPLATE_YAML;
 
  30 import java.util.Collections;
 
  31 import java.util.List;
 
  32 import java.util.UUID;
 
  33 import org.junit.jupiter.api.Test;
 
  34 import org.onap.policy.clamp.acm.runtime.instantiation.InstantiationUtils;
 
  35 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionHandler;
 
  36 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionParticipantHandler;
 
  37 import org.onap.policy.clamp.acm.runtime.util.CommonTestData;
 
  38 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
 
  39 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
 
  40 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
 
  41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionAck;
 
  42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
 
  43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregisterAck;
 
  44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
 
  45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
 
  46 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
 
  47 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
 
  48 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdateAck;
 
  49 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
 
  50 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
 
  51 import org.onap.policy.common.endpoints.event.comm.TopicSink;
 
  53 class SupervisionMessagesTest {
 
  55     private static final String NOT_ACTIVE = "Not Active!";
 
  56     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
 
  57     private static final String TOPIC = "my-topic";
 
  60     void testSendParticipantRegisterAck() {
 
  61         var acRegisterAckPublisher = new ParticipantRegisterAckPublisher();
 
  62         var topicSink = mock(TopicSink.class);
 
  63         acRegisterAckPublisher.active(List.of(topicSink));
 
  64         acRegisterAckPublisher.send(new ParticipantRegisterAck());
 
  65         verify(topicSink).send(anyString());
 
  66         acRegisterAckPublisher.stop();
 
  70     void testSendParticipantRegisterAckNoActive() {
 
  71         var acRegisterAckPublisher = new ParticipantRegisterAckPublisher();
 
  72         assertThatThrownBy(() -> acRegisterAckPublisher.send(new ParticipantRegisterAck()))
 
  73                 .hasMessageMatching(NOT_ACTIVE);
 
  77     void testReceiveParticipantDeregister() {
 
  78         final var participantDeregisterMsg = new ParticipantDeregister();
 
  79         var supervisionHandler = mock(SupervisionParticipantHandler.class);
 
  80         var participantDeregisterListener = new ParticipantDeregisterListener(supervisionHandler);
 
  81         participantDeregisterListener.onTopicEvent(INFRA, TOPIC, null, participantDeregisterMsg);
 
  82         verify(supervisionHandler).handleParticipantMessage(participantDeregisterMsg);
 
  86     void testSendParticipantDeregisterAck() {
 
  87         var acDeregisterAckPublisher = new ParticipantDeregisterAckPublisher();
 
  88         var topicSink = mock(TopicSink.class);
 
  89         acDeregisterAckPublisher.active(Collections.singletonList(topicSink));
 
  90         acDeregisterAckPublisher.send(new ParticipantDeregisterAck());
 
  91         verify(topicSink).send(anyString());
 
  92         acDeregisterAckPublisher.stop();
 
  95     void testSendParticipantDeregisterAckNoActive() {
 
  96         var acDeregisterAckPublisher = new ParticipantDeregisterAckPublisher();
 
  97         assertThatThrownBy(() -> acDeregisterAckPublisher.send(new ParticipantDeregisterAck()))
 
  98                 .hasMessageMatching(NOT_ACTIVE);
 
 102     void testReceiveParticipantUpdateAckMessage() {
 
 103         final var participantUpdateAckMsg = new ParticipantUpdateAck();
 
 104         var supervisionHandler = mock(SupervisionHandler.class);
 
 105         var participantUpdateAckListener = new ParticipantUpdateAckListener(supervisionHandler);
 
 106         participantUpdateAckListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateAckMsg);
 
 107         verify(supervisionHandler).handleParticipantMessage(participantUpdateAckMsg);
 
 111     void testSendAutomationCompositionStateChangePublisherNotActive() {
 
 112         var publisher = new AutomationCompositionStateChangePublisher();
 
 113         assertThatThrownBy(() -> publisher.send(getAutomationComposition(), 0)).hasMessage(NOT_ACTIVE);
 
 116     private AutomationComposition getAutomationComposition() {
 
 117         var automationComposition = new AutomationComposition();
 
 118         automationComposition.setName("NAME");
 
 119         automationComposition.setVersion("0.0.1");
 
 120         automationComposition.setState(AutomationCompositionState.UNINITIALISED);
 
 121         return automationComposition;
 
 125     void testSendAutomationCompositionStateChangePublisher() {
 
 126         var publisher = new AutomationCompositionStateChangePublisher();
 
 127         var topicSink = mock(TopicSink.class);
 
 128         publisher.active(List.of(topicSink));
 
 129         publisher.send(getAutomationComposition(), 0);
 
 130         verify(topicSink).send(anyString());
 
 135     void testParticipantUpdatePublisherDecomisioning() {
 
 136         var publisher = new ParticipantUpdatePublisher(mock(AcDefinitionProvider.class));
 
 137         var topicSink = mock(TopicSink.class);
 
 138         publisher.active(List.of(topicSink));
 
 139         publisher.sendDecomisioning(UUID.randomUUID());
 
 140         verify(topicSink).send(anyString());
 
 144     void testParticipantUpdatePublisherComissioning() {
 
 145         var publisher = new ParticipantUpdatePublisher(mock(AcDefinitionProvider.class));
 
 146         var topicSink = mock(TopicSink.class);
 
 147         publisher.active(List.of(topicSink));
 
 148         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
 
 149         serviceTemplate.setName("Name");
 
 150         serviceTemplate.setVersion("1.0.0");
 
 151         var acmDefinition = new AutomationCompositionDefinition();
 
 152         acmDefinition.setCompositionId(UUID.randomUUID());
 
 153         acmDefinition.setServiceTemplate(serviceTemplate);
 
 154         publisher.sendComissioningBroadcast(acmDefinition);
 
 155         verify(topicSink).send(anyString());
 
 159     void testParticipantStatusReqPublisher() {
 
 160         var publisher = new ParticipantStatusReqPublisher();
 
 161         var topicSink = mock(TopicSink.class);
 
 162         publisher.active(List.of(topicSink));
 
 163         publisher.send(CommonTestData.getParticipantId());
 
 164         verify(topicSink).send(anyString());
 
 168     void testParticipantRegisterAckPublisher() {
 
 169         var publisher = new ParticipantRegisterAckPublisher();
 
 170         var topicSink = mock(TopicSink.class);
 
 171         publisher.active(List.of(topicSink));
 
 172         publisher.send(UUID.randomUUID(), CommonTestData.getParticipantId());
 
 173         verify(topicSink).send(anyString());
 
 177     void testParticipantDeregisterAckPublisher() {
 
 178         var publisher = new ParticipantDeregisterAckPublisher();
 
 179         var topicSink = mock(TopicSink.class);
 
 180         publisher.active(List.of(topicSink));
 
 181         publisher.send(UUID.randomUUID());
 
 182         verify(topicSink).send(anyString());
 
 186     void testParticipantRegisterListener() {
 
 187         final var participantRegister = new ParticipantRegister();
 
 188         var supervisionHandler = mock(SupervisionParticipantHandler.class);
 
 189         var participantRegisterListener = new ParticipantRegisterListener(supervisionHandler);
 
 190         participantRegisterListener.onTopicEvent(INFRA, TOPIC, null, participantRegister);
 
 191         verify(supervisionHandler).handleParticipantMessage(participantRegister);
 
 195     void testParticipantStatusListener() {
 
 196         final var participantStatus = new ParticipantStatus();
 
 197         var supervisionHandler = mock(SupervisionParticipantHandler.class);
 
 198         var participantStatusListener = new ParticipantStatusListener(supervisionHandler);
 
 199         participantStatusListener.onTopicEvent(INFRA, TOPIC, null, participantStatus);
 
 200         verify(supervisionHandler).handleParticipantMessage(participantStatus);
 
 204     void testAutomationCompositionUpdateAckListener() {
 
 205         final var automationCompositionAck =
 
 206                 new AutomationCompositionAck(ParticipantMessageType.AUTOMATION_COMPOSITION_UPDATE);
 
 207         var supervisionHandler = mock(SupervisionHandler.class);
 
 208         var acUpdateAckListener = new AutomationCompositionUpdateAckListener(supervisionHandler);
 
 209         acUpdateAckListener.onTopicEvent(INFRA, TOPIC, null, automationCompositionAck);
 
 210         verify(supervisionHandler).handleAutomationCompositionUpdateAckMessage(automationCompositionAck);
 
 214     void testAutomationCompositionStateChangeAckListener() {
 
 215         final var automationCompositionAck =
 
 216                 new AutomationCompositionAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
 
 217         var supervisionHandler = mock(SupervisionHandler.class);
 
 218         var acStateChangeAckListener = new AutomationCompositionStateChangeAckListener(supervisionHandler);
 
 219         acStateChangeAckListener.onTopicEvent(INFRA, TOPIC, null, automationCompositionAck);
 
 220         verify(supervisionHandler).handleAutomationCompositionStateChangeAckMessage(automationCompositionAck);