2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2021-2022 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.times;
 
  28 import static org.mockito.Mockito.verify;
 
  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.supervision.SupervisionHandler;
 
  35 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
 
  36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
 
  37 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionAck;
 
  38 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
 
  39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregisterAck;
 
  40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
 
  41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
 
  42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
 
  43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
 
  44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdateAck;
 
  45 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
 
  46 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
 
  47 import org.onap.policy.common.endpoints.event.comm.TopicSink;
 
  48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
 
  50 class SupervisionMessagesTest {
 
  52     private static final String NOT_ACTIVE = "Not Active!";
 
  53     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
 
  54     private static final String TOPIC = "my-topic";
 
  57     void testSendParticipantRegisterAck() {
 
  58         var acRegisterAckPublisher = new ParticipantRegisterAckPublisher();
 
  59         var topicSink = mock(TopicSink.class);
 
  60         acRegisterAckPublisher.active(List.of(topicSink));
 
  61         acRegisterAckPublisher.send(new ParticipantRegisterAck());
 
  62         verify(topicSink).send(anyString());
 
  63         acRegisterAckPublisher.stop();
 
  67     void testSendParticipantRegisterAckNoActive() {
 
  68         var acRegisterAckPublisher = new ParticipantRegisterAckPublisher();
 
  69         assertThatThrownBy(() -> acRegisterAckPublisher.send(new ParticipantRegisterAck()))
 
  70                 .hasMessageMatching(NOT_ACTIVE);
 
  74     void testReceiveParticipantDeregister() {
 
  75         final var participantDeregisterMsg = new ParticipantDeregister();
 
  76         var supervisionHandler = mock(SupervisionHandler.class);
 
  77         var participantDeregisterListener = new ParticipantDeregisterListener(supervisionHandler);
 
  78         participantDeregisterListener.onTopicEvent(INFRA, TOPIC, null, participantDeregisterMsg);
 
  79         verify(supervisionHandler).handleParticipantMessage(participantDeregisterMsg);
 
  83     void testSendParticipantDeregisterAck() {
 
  84         var acDeregisterAckPublisher = new ParticipantDeregisterAckPublisher();
 
  85         var topicSink = mock(TopicSink.class);
 
  86         acDeregisterAckPublisher.active(Collections.singletonList(topicSink));
 
  87         acDeregisterAckPublisher.send(new ParticipantDeregisterAck());
 
  88         verify(topicSink).send(anyString());
 
  89         acDeregisterAckPublisher.stop();
 
  92     void testSendParticipantDeregisterAckNoActive() {
 
  93         var acDeregisterAckPublisher = new ParticipantDeregisterAckPublisher();
 
  94         assertThatThrownBy(() -> acDeregisterAckPublisher.send(new ParticipantDeregisterAck()))
 
  95                 .hasMessageMatching(NOT_ACTIVE);
 
  99     void testReceiveParticipantUpdateAckMessage() {
 
 100         final var participantUpdateAckMsg = new ParticipantUpdateAck();
 
 101         var supervisionHandler = mock(SupervisionHandler.class);
 
 102         var participantUpdateAckListener = new ParticipantUpdateAckListener(supervisionHandler);
 
 103         participantUpdateAckListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateAckMsg);
 
 104         verify(supervisionHandler).handleParticipantMessage(participantUpdateAckMsg);
 
 108     void testSendAutomationCompositionStateChangePublisherNotActive() {
 
 109         var publisher = new AutomationCompositionStateChangePublisher();
 
 110         assertThatThrownBy(() -> publisher.send(getAutomationComposition(), 0)).hasMessage(NOT_ACTIVE);
 
 113     private AutomationComposition getAutomationComposition() {
 
 114         var automationComposition = new AutomationComposition();
 
 115         automationComposition.setName("NAME");
 
 116         automationComposition.setVersion("0.0.1");
 
 117         automationComposition.setState(AutomationCompositionState.UNINITIALISED);
 
 118         return automationComposition;
 
 122     void testSendAutomationCompositionStateChangePublisher() {
 
 123         var publisher = new AutomationCompositionStateChangePublisher();
 
 124         var topicSink = mock(TopicSink.class);
 
 125         publisher.active(List.of(topicSink));
 
 126         publisher.send(getAutomationComposition(), 0);
 
 127         verify(topicSink).send(anyString());
 
 132     void testParticipantUpdatePublisherDecomisioning() {
 
 133         var publisher = new ParticipantUpdatePublisher(mock(AcDefinitionProvider.class));
 
 134         var topicSink = mock(TopicSink.class);
 
 135         publisher.active(List.of(topicSink));
 
 136         publisher.sendDecomisioning();
 
 137         verify(topicSink).send(anyString());
 
 141     void testParticipantUpdatePublisherComissioning() {
 
 142         var publisher = new ParticipantUpdatePublisher(mock(AcDefinitionProvider.class));
 
 143         var topicSink = mock(TopicSink.class);
 
 144         publisher.active(List.of(topicSink));
 
 145         publisher.sendComissioningBroadcast("NAME", "1.0.0");
 
 146         verify(topicSink, times(0)).send(anyString());
 
 150     void testParticipantStatusReqPublisher() {
 
 151         var publisher = new ParticipantStatusReqPublisher();
 
 152         var topicSink = mock(TopicSink.class);
 
 153         publisher.active(List.of(topicSink));
 
 154         publisher.send(getParticipantId());
 
 155         verify(topicSink).send(anyString());
 
 159     void testParticipantRegisterAckPublisher() {
 
 160         var publisher = new ParticipantRegisterAckPublisher();
 
 161         var topicSink = mock(TopicSink.class);
 
 162         publisher.active(List.of(topicSink));
 
 163         publisher.send(UUID.randomUUID(), getParticipantId(), getParticipantType());
 
 164         verify(topicSink).send(anyString());
 
 167     private ToscaConceptIdentifier getParticipantId() {
 
 168         return new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
 
 171     private ToscaConceptIdentifier getParticipantType() {
 
 172         return new ToscaConceptIdentifier("org.onap.policy.acm.PolicyAutomationCompositionParticipant", "2.3.1");
 
 176     void testParticipantDeregisterAckPublisher() {
 
 177         var publisher = new ParticipantDeregisterAckPublisher();
 
 178         var topicSink = mock(TopicSink.class);
 
 179         publisher.active(List.of(topicSink));
 
 180         publisher.send(UUID.randomUUID());
 
 181         verify(topicSink).send(anyString());
 
 185     void testParticipantRegisterListener() {
 
 186         final var participantRegister = new ParticipantRegister();
 
 187         var supervisionHandler = mock(SupervisionHandler.class);
 
 188         var participantRegisterListener = new ParticipantRegisterListener(supervisionHandler);
 
 189         participantRegisterListener.onTopicEvent(INFRA, TOPIC, null, participantRegister);
 
 190         verify(supervisionHandler).handleParticipantMessage(participantRegister);
 
 194     void testParticipantStatusListener() {
 
 195         final var participantStatus = new ParticipantStatus();
 
 196         var supervisionHandler = mock(SupervisionHandler.class);
 
 197         var participantStatusListener = new ParticipantStatusListener(supervisionHandler);
 
 198         participantStatusListener.onTopicEvent(INFRA, TOPIC, null, participantStatus);
 
 199         verify(supervisionHandler).handleParticipantMessage(participantStatus);
 
 203     void testAutomationCompositionUpdateAckListener() {
 
 204         final var automationCompositionAck =
 
 205                 new AutomationCompositionAck(ParticipantMessageType.AUTOMATION_COMPOSITION_UPDATE);
 
 206         var supervisionHandler = mock(SupervisionHandler.class);
 
 207         var acUpdateAckListener = new AutomationCompositionUpdateAckListener(supervisionHandler);
 
 208         acUpdateAckListener.onTopicEvent(INFRA, TOPIC, null, automationCompositionAck);
 
 209         verify(supervisionHandler).handleAutomationCompositionUpdateAckMessage(automationCompositionAck);
 
 213     void testAutomationCompositionStateChangeAckListener() {
 
 214         final var automationCompositionAck =
 
 215                 new AutomationCompositionAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
 
 216         var supervisionHandler = mock(SupervisionHandler.class);
 
 217         var acStateChangeAckListener = new AutomationCompositionStateChangeAckListener(supervisionHandler);
 
 218         acStateChangeAckListener.onTopicEvent(INFRA, TOPIC, null, automationCompositionAck);
 
 219         verify(supervisionHandler).handleAutomationCompositionStateChangeAckMessage(automationCompositionAck);