2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2024 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.api.impl;
 
  23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
  24 import static org.mockito.ArgumentMatchers.any;
 
  25 import static org.mockito.Mockito.mock;
 
  26 import static org.mockito.Mockito.verify;
 
  29 import java.util.UUID;
 
  30 import org.junit.jupiter.api.Test;
 
  31 import org.mockito.Answers;
 
  32 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto;
 
  33 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionElementDto;
 
  34 import org.onap.policy.clamp.acm.participant.intermediary.api.InstanceElementDto;
 
  35 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
 
  36 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
 
  37 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
 
  38 import org.onap.policy.clamp.models.acm.concepts.DeployState;
 
  39 import org.onap.policy.clamp.models.acm.concepts.LockState;
 
  40 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
 
  41 import org.onap.policy.models.base.PfModelException;
 
  42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
 
  44 class AcElementListenerV1Test {
 
  47     void deployTest() throws PfModelException {
 
  48         var acElementListenerV1 = mock(AcElementListenerV1.class, Answers.CALLS_REAL_METHODS);
 
  49         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
 
  51         var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
 
  52         acElementListenerV1.deploy(compositionElement, instanceElement);
 
  53         verify(acElementListenerV1).deploy(any(), any(), any());
 
  57     void undeployTest() throws PfModelException {
 
  58         var acElementListenerV1 = mock(AcElementListenerV1.class, Answers.CALLS_REAL_METHODS);
 
  59         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
 
  61         var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
 
  62         acElementListenerV1.undeploy(compositionElement, instanceElement);
 
  63         verify(acElementListenerV1).undeploy(instanceElement.instanceId(), instanceElement.elementId());
 
  67     void lockTest() throws PfModelException {
 
  68         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
 
  69         var acElementListenerV1 = createAcElementListenerV1(intermediaryApi);
 
  70         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
 
  72         var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
 
  73         acElementListenerV1.lock(compositionElement, instanceElement);
 
  74         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
 
  75             instanceElement.elementId(), null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
 
  79     void deleteTest() throws PfModelException {
 
  80         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
 
  81         var acElementListenerV1 = createAcElementListenerV1(intermediaryApi);
 
  82         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
 
  84         var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
 
  85         acElementListenerV1.delete(compositionElement, instanceElement);
 
  86         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
 
  87             instanceElement.elementId(), DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
 
  91     void updateTest() throws PfModelException {
 
  92         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
 
  93         var acElementListenerV1 = createAcElementListenerV1(intermediaryApi);
 
  94         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
 
  96         var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
 
  97         acElementListenerV1.update(compositionElement, instanceElement, instanceElement);
 
  98         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
 
  99             instanceElement.elementId(), DeployState.DEPLOYED, null,
 
 100             StateChangeResult.NO_ERROR, "Update not supported");
 
 104     void unlockTest() throws PfModelException {
 
 105         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
 
 106         var acElementListenerV1 = createAcElementListenerV1(intermediaryApi);
 
 107         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
 
 109         var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
 
 110         acElementListenerV1.unlock(compositionElement, instanceElement);
 
 111         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
 
 112             instanceElement.elementId(), null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
 
 116     void primeTest() throws PfModelException {
 
 117         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
 
 118         var acElementListenerV1 = createAcElementListenerV1(intermediaryApi);
 
 119         var compositionId = UUID.randomUUID();
 
 120         var toscaConceptIdentifier = new ToscaConceptIdentifier();
 
 121         var composition = new CompositionDto(compositionId, Map.of(toscaConceptIdentifier, Map.of()), Map.of());
 
 122         acElementListenerV1.prime(composition);
 
 123         verify(intermediaryApi)
 
 124             .updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
 
 128     void deprimeTest() throws PfModelException {
 
 129         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
 
 130         var acElementListenerV1 = createAcElementListenerV1(intermediaryApi);
 
 131         var compositionId = UUID.randomUUID();
 
 132         var toscaConceptIdentifier = new ToscaConceptIdentifier();
 
 133         var composition = new CompositionDto(compositionId, Map.of(toscaConceptIdentifier, Map.of()), Map.of());
 
 134         acElementListenerV1.deprime(composition);
 
 135         verify(intermediaryApi)
 
 136             .updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR, "Deprimed");
 
 140     void handleRestartComposition() {
 
 141         var acElementListenerV1 = createAcElementListenerV1(mock(ParticipantIntermediaryApi.class));
 
 142         assertThatThrownBy(() -> acElementListenerV1.handleRestartComposition(null, null))
 
 143                 .isInstanceOf(PfModelException.class);
 
 147     void handleRestartInstance() {
 
 148         var acElementListenerV1 = createAcElementListenerV1(mock(ParticipantIntermediaryApi.class));
 
 149         assertThatThrownBy(() -> acElementListenerV1.handleRestartInstance(null, null,
 
 150                 null, null)).isInstanceOf(PfModelException.class);
 
 154     void migrateTest() throws PfModelException {
 
 155         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
 
 156         var acElementListenerV1 = createAcElementListenerV1(intermediaryApi);
 
 157         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
 
 159         var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
 
 160         acElementListenerV1.migrate(compositionElement, compositionElement, instanceElement, instanceElement);
 
 161         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
 
 162             instanceElement.elementId(), DeployState.DEPLOYED, null,
 
 163             StateChangeResult.NO_ERROR, "Migrated");
 
 167     void migratePrecheckTest() throws PfModelException {
 
 168         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
 
 169         var acElementListenerV1 = createAcElementListenerV1(intermediaryApi);
 
 170         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
 
 172         var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
 
 173         acElementListenerV1.migratePrecheck(compositionElement, compositionElement, instanceElement, instanceElement);
 
 174         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
 
 175                 instanceElement.elementId(), DeployState.DEPLOYED, null,
 
 176                 StateChangeResult.NO_ERROR, "Migration Precheck completed");
 
 180     void reviewTest() throws PfModelException {
 
 181         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
 
 182         var acElementListenerV1 = createAcElementListenerV1(intermediaryApi);
 
 183         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
 
 185         var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
 
 186         acElementListenerV1.review(compositionElement, instanceElement);
 
 187         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
 
 188                 instanceElement.elementId(), DeployState.DEPLOYED, null,
 
 189                 StateChangeResult.NO_ERROR, "Review completed");
 
 193     void prepareTest() throws PfModelException {
 
 194         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
 
 195         var acElementListenerV1 = createAcElementListenerV1(intermediaryApi);
 
 196         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
 
 198         var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
 
 199         acElementListenerV1.prepare(compositionElement, instanceElement);
 
 200         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
 
 201                 instanceElement.elementId(), DeployState.UNDEPLOYED, null,
 
 202                 StateChangeResult.NO_ERROR, "Prepare completed");
 
 205     private AcElementListenerV1 createAcElementListenerV1(ParticipantIntermediaryApi intermediaryApi) {
 
 206         return new AcElementListenerV1(intermediaryApi) {
 
 208             public void deploy(UUID instanceId, AcElementDeploy element, Map<String, Object> properties) {
 
 209                 // dummy implementation
 
 213             public void undeploy(UUID instanceId, UUID elementId) {
 
 214                 // dummy implementation