2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2023-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.sim.main.handler;
 
  23 import static org.mockito.Mockito.mock;
 
  24 import static org.mockito.Mockito.verify;
 
  26 import java.util.List;
 
  28 import java.util.UUID;
 
  29 import org.junit.jupiter.api.Test;
 
  30 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
 
  31 import org.onap.policy.clamp.acm.participant.sim.model.SimConfig;
 
  32 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
 
  33 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
 
  34 import org.onap.policy.clamp.models.acm.concepts.DeployState;
 
  35 import org.onap.policy.clamp.models.acm.concepts.LockState;
 
  36 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
 
  37 import org.onap.policy.models.base.PfModelException;
 
  39 class AutomationCompositionElementHandlerV1Test {
 
  42     void testDeploy() throws PfModelException {
 
  43         var config = new SimConfig();
 
  44         config.setDeployTimerMs(1);
 
  45         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
 
  46         var simulatorService = new SimulatorService(intermediaryApi);
 
  47         var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService);
 
  48         simulatorService.setConfig(config);
 
  49         var instanceId = UUID.randomUUID();
 
  50         var element = new AcElementDeploy();
 
  51         element.setId(UUID.randomUUID());
 
  52         acElementHandler.deploy(instanceId, element, Map.of());
 
  53         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
 
  54                 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
 
  56         config.setDeploySuccess(false);
 
  57         acElementHandler.deploy(instanceId, element, Map.of());
 
  58         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
 
  59                 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Deploy failed!");
 
  63     void testUndeploy() throws PfModelException {
 
  64         var config = new SimConfig();
 
  65         config.setUndeployTimerMs(1);
 
  66         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
 
  67         var simulatorService = new SimulatorService(intermediaryApi);
 
  68         var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService);
 
  69         simulatorService.setConfig(config);
 
  70         var instanceId = UUID.randomUUID();
 
  71         var elementId = UUID.randomUUID();
 
  72         acElementHandler.undeploy(instanceId, elementId);
 
  73         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED,
 
  74                 null, StateChangeResult.NO_ERROR, "Undeployed");
 
  76         config.setUndeploySuccess(false);
 
  77         acElementHandler.undeploy(instanceId, elementId);
 
  78         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DEPLOYED,
 
  79                 null, StateChangeResult.FAILED, "Undeploy failed!");
 
  83     void testLock() throws PfModelException {
 
  84         var config = new SimConfig();
 
  85         config.setLockTimerMs(1);
 
  86         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
 
  87         var simulatorService = new SimulatorService(intermediaryApi);
 
  88         var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService);
 
  89         simulatorService.setConfig(config);
 
  90         var instanceId = UUID.randomUUID();
 
  91         var elementId = UUID.randomUUID();
 
  92         acElementHandler.lock(instanceId, elementId);
 
  93         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED,
 
  94                 StateChangeResult.NO_ERROR, "Locked");
 
  96         config.setLockSuccess(false);
 
  97         acElementHandler.lock(instanceId, elementId);
 
  98         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED,
 
  99                 StateChangeResult.FAILED, "Lock failed!");
 
 103     void testUnlock() throws PfModelException {
 
 104         var config = new SimConfig();
 
 105         config.setUnlockTimerMs(1);
 
 106         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
 
 107         var simulatorService = new SimulatorService(intermediaryApi);
 
 108         var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService);
 
 109         simulatorService.setConfig(config);
 
 110         var instanceId = UUID.randomUUID();
 
 111         var elementId = UUID.randomUUID();
 
 112         acElementHandler.unlock(instanceId, elementId);
 
 113         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED,
 
 114                 StateChangeResult.NO_ERROR, "Unlocked");
 
 116         config.setUnlockSuccess(false);
 
 117         acElementHandler.unlock(instanceId, elementId);
 
 118         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED,
 
 119                 StateChangeResult.FAILED, "Unlock failed!");
 
 123     void testUpdate() throws PfModelException {
 
 124         var config = new SimConfig();
 
 125         config.setUpdateTimerMs(1);
 
 126         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
 
 127         var simulatorService = new SimulatorService(intermediaryApi);
 
 128         var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService);
 
 129         simulatorService.setConfig(config);
 
 130         var instanceId = UUID.randomUUID();
 
 131         var element = new AcElementDeploy();
 
 132         element.setId(UUID.randomUUID());
 
 133         acElementHandler.update(instanceId, element, Map.of());
 
 134         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
 
 135                 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated");
 
 137         config.setUpdateSuccess(false);
 
 138         acElementHandler.update(instanceId, element, Map.of());
 
 139         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
 
 140                 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!");
 
 144     void testDelete() throws PfModelException {
 
 145         var config = new SimConfig();
 
 146         config.setDeleteTimerMs(1);
 
 147         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
 
 148         var simulatorService = new SimulatorService(intermediaryApi);
 
 149         var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService);
 
 150         simulatorService.setConfig(config);
 
 151         var instanceId = UUID.randomUUID();
 
 152         var elementId = UUID.randomUUID();
 
 153         acElementHandler.delete(instanceId, elementId);
 
 154         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DELETED,
 
 155                 null, StateChangeResult.NO_ERROR, "Deleted");
 
 157         config.setDeleteSuccess(false);
 
 158         acElementHandler.delete(instanceId, elementId);
 
 159         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED,
 
 160                 null, StateChangeResult.FAILED, "Delete failed!");
 
 164     void testPrime() throws PfModelException {
 
 165         var config = new SimConfig();
 
 166         config.setPrimeTimerMs(1);
 
 167         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
 
 168         var simulatorService = new SimulatorService(intermediaryApi);
 
 169         var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService);
 
 170         simulatorService.setConfig(config);
 
 171         var compositionId = UUID.randomUUID();
 
 172         acElementHandler.prime(compositionId, List.of());
 
 173         verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR,
 
 176         config.setPrimeSuccess(false);
 
 177         acElementHandler.prime(compositionId, List.of());
 
 178         verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED,
 
 179                 StateChangeResult.FAILED, "Prime failed!");
 
 183     void testDeprime() throws PfModelException {
 
 184         var config = new SimConfig();
 
 185         config.setDeprimeTimerMs(1);
 
 186         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
 
 187         var simulatorService = new SimulatorService(intermediaryApi);
 
 188         var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService);
 
 189         simulatorService.setConfig(config);
 
 190         var compositionId = UUID.randomUUID();
 
 191         acElementHandler.deprime(compositionId);
 
 192         verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED,
 
 193                 StateChangeResult.NO_ERROR, "Deprimed");
 
 195         config.setDeprimeSuccess(false);
 
 196         acElementHandler.deprime(compositionId);
 
 197         verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED,
 
 202     void testMigrate() throws PfModelException {
 
 203         var config = new SimConfig();
 
 204         config.setUpdateTimerMs(1);
 
 205         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
 
 206         var simulatorService = new SimulatorService(intermediaryApi);
 
 207         var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService);
 
 208         simulatorService.setConfig(config);
 
 209         var instanceId = UUID.randomUUID();
 
 210         var element = new AcElementDeploy();
 
 211         element.setId(UUID.randomUUID());
 
 212         acElementHandler.migrate(instanceId, element, UUID.randomUUID(), Map.of());
 
 213         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
 
 214                 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
 
 216         config.setMigrateSuccess(false);
 
 217         acElementHandler.migrate(instanceId, element, UUID.randomUUID(), Map.of());
 
 218         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
 
 219                 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!");