From 7508b3c9be6312b951af7faf1e8bdd32a87d4375 Mon Sep 17 00:00:00 2001 From: FrancescoFioraEst Date: Tue, 26 Mar 2024 10:32:39 +0000 Subject: Add support for testing in ACM Participant Simulator Support backward compatibility between participants and ACM-R. Issue-ID: POLICY-4952 Change-Id: I115ab3f0ccf0a2d6ad9ec4b998ef175e9b8ca1b8 Signed-off-by: FrancescoFioraEst --- .../AutomationCompositionElementHandlerTest.java | 408 --------------------- .../AutomationCompositionElementHandlerV1Test.java | 285 ++++++++++++++ .../AutomationCompositionElementHandlerV2Test.java | 328 +++++++++++++++++ .../sim/main/handler/SimulatorServiceTest.java | 126 +++++++ .../acm/participant/sim/rest/AcSimRestTest.java | 47 ++- 5 files changed, 772 insertions(+), 422 deletions(-) delete mode 100644 participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerTest.java create mode 100644 participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerV1Test.java create mode 100644 participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerV2Test.java create mode 100644 participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/SimulatorServiceTest.java (limited to 'participant/participant-impl/participant-impl-simulator/src/test/java') diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerTest.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerTest.java deleted file mode 100644 index d63405369..000000000 --- a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerTest.java +++ /dev/null @@ -1,408 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2023-2024 Nordix Foundation. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.clamp.acm.participant.sim.main.handler; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.Map; -import java.util.UUID; -import org.junit.jupiter.api.Test; -import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto; -import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionElementDto; -import org.onap.policy.clamp.acm.participant.intermediary.api.InstanceElementDto; -import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi; -import org.onap.policy.clamp.acm.participant.sim.comm.CommonTestData; -import org.onap.policy.clamp.acm.participant.sim.model.SimConfig; -import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy; -import org.onap.policy.clamp.models.acm.concepts.AcTypeState; -import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition; -import org.onap.policy.clamp.models.acm.concepts.DeployState; -import org.onap.policy.clamp.models.acm.concepts.LockState; -import org.onap.policy.clamp.models.acm.concepts.StateChangeResult; -import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; -import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; - -class AutomationCompositionElementHandlerTest { - - @Test - void testDeploy() throws PfModelException { - var config = new SimConfig(); - config.setDeployTimerMs(1); - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); - var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), - Map.of(), Map.of()); - var instanceId = UUID.randomUUID(); - var elementId = UUID.randomUUID(); - var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of()); - acElementHandler.deploy(compositionElement, instanceElement); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DEPLOYED, - null, StateChangeResult.NO_ERROR, "Deployed"); - - config.setDeploySuccess(false); - acElementHandler.deploy(compositionElement, instanceElement); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED, - null, StateChangeResult.FAILED, "Deploy failed!"); - } - - @Test - void testUndeploy() throws PfModelException { - var config = new SimConfig(); - config.setUndeployTimerMs(1); - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); - var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), - Map.of(), Map.of()); - var instanceId = UUID.randomUUID(); - var elementId = UUID.randomUUID(); - var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of()); - acElementHandler.undeploy(compositionElement, instanceElement); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED, - null, StateChangeResult.NO_ERROR, "Undeployed"); - - config.setUndeploySuccess(false); - acElementHandler.undeploy(compositionElement, instanceElement); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DEPLOYED, - null, StateChangeResult.FAILED, "Undeploy failed!"); - } - - @Test - void testLock() throws PfModelException { - var config = new SimConfig(); - config.setLockTimerMs(1); - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); - var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), - Map.of(), Map.of()); - var instanceId = UUID.randomUUID(); - var elementId = UUID.randomUUID(); - var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of()); - acElementHandler.lock(compositionElement, instanceElement); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED, - StateChangeResult.NO_ERROR, "Locked"); - - config.setLockSuccess(false); - acElementHandler.lock(compositionElement, instanceElement); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED, - StateChangeResult.FAILED, "Lock failed!"); - } - - @Test - void testUnlock() throws PfModelException { - var config = new SimConfig(); - config.setUnlockTimerMs(1); - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); - var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), - Map.of(), Map.of()); - var instanceId = UUID.randomUUID(); - var elementId = UUID.randomUUID(); - var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of()); - acElementHandler.unlock(compositionElement, instanceElement); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED, - StateChangeResult.NO_ERROR, "Unlocked"); - - config.setUnlockSuccess(false); - acElementHandler.unlock(compositionElement, instanceElement); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED, - StateChangeResult.FAILED, "Unlock failed!"); - } - - @Test - void testUpdate() throws PfModelException { - var config = new SimConfig(); - config.setUpdateTimerMs(1); - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); - var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), - Map.of(), Map.of()); - var instanceId = UUID.randomUUID(); - var element = new AcElementDeploy(); - element.setId(UUID.randomUUID()); - var instanceElement = new InstanceElementDto(instanceId, element.getId(), null, Map.of(), Map.of()); - var instanceElementUpdated = new InstanceElementDto(instanceId, element.getId(), null, - Map.of("key", "value"), Map.of()); - acElementHandler.update(compositionElement, instanceElement, instanceElementUpdated); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), - DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated"); - - config.setUpdateSuccess(false); - acElementHandler.update(compositionElement, instanceElement, instanceElementUpdated); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), - DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!"); - } - - @Test - void testDelete() throws PfModelException { - var config = new SimConfig(); - config.setDeleteTimerMs(1); - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); - var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), - Map.of(), Map.of()); - var instanceId = UUID.randomUUID(); - var elementId = UUID.randomUUID(); - var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of()); - acElementHandler.delete(compositionElement, instanceElement); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DELETED, - null, StateChangeResult.NO_ERROR, "Deleted"); - - config.setDeleteSuccess(false); - acElementHandler.delete(compositionElement, instanceElement); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED, - null, StateChangeResult.FAILED, "Delete failed!"); - } - - @Test - void testgetAutomationCompositions() throws PfModelException { - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - - var map = CommonTestData.getTestAutomationCompositionMap(); - when(intermediaryApi.getAutomationCompositions()).thenReturn(map); - var result = acElementHandler.getAutomationCompositions(); - assertEquals(map.values().iterator().next(), result.getAutomationCompositionList().get(0)); - } - - @Test - void testgetAutomationComposition() throws PfModelException { - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - - var instance = CommonTestData.getTestAutomationCompositionMap().values().iterator().next(); - when(intermediaryApi.getAutomationComposition(instance.getInstanceId())).thenReturn(instance); - var result = acElementHandler.getAutomationComposition(instance.getInstanceId()); - assertEquals(instance, result); - } - - @Test - void testsetOutProperties() { - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - - var instanceId = UUID.randomUUID(); - var elementId = UUID.randomUUID(); - var useState = "useState"; - var operationalState = "operationalState"; - Map map = Map.of("id", "1234"); - - acElementHandler.setOutProperties(instanceId, elementId, useState, operationalState, map); - verify(intermediaryApi).sendAcElementInfo(instanceId, elementId, useState, operationalState, map); - } - - @Test - void testgetDataList() { - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - - var map = CommonTestData.getTestAutomationCompositionMap(); - when(intermediaryApi.getAutomationCompositions()).thenReturn(map); - var result = acElementHandler.getDataList(); - var data = result.getList().get(0); - var automationcomposition = map.values().iterator().next(); - assertEquals(automationcomposition.getInstanceId(), data.getAutomationCompositionId()); - var element = automationcomposition.getElements().values().iterator().next(); - assertEquals(element.getId(), data.getAutomationCompositionElementId()); - } - - @Test - void testPrime() throws PfModelException { - var config = new SimConfig(); - config.setPrimeTimerMs(1); - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); - var compositionId = UUID.randomUUID(); - var composition = new CompositionDto(compositionId, Map.of(), Map.of()); - acElementHandler.prime(composition); - verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, - "Primed"); - - config.setPrimeSuccess(false); - acElementHandler.prime(composition); - verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED, - StateChangeResult.FAILED, "Prime failed!"); - } - - @Test - void testDeprime() throws PfModelException { - var config = new SimConfig(); - config.setDeprimeTimerMs(1); - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); - var compositionId = UUID.randomUUID(); - var composition = new CompositionDto(compositionId, Map.of(), Map.of()); - acElementHandler.deprime(composition); - verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED, - StateChangeResult.NO_ERROR, "Deprimed"); - - config.setDeprimeSuccess(false); - acElementHandler.deprime(composition); - verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED, - "Deprime failed!"); - } - - @Test - void testHandleRestartComposition() throws PfModelException { - var config = new SimConfig(); - config.setPrimeTimerMs(1); - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); - var compositionId = UUID.randomUUID(); - var composition = new CompositionDto(compositionId, Map.of(), Map.of()); - acElementHandler.handleRestartComposition(composition, AcTypeState.PRIMING); - verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, - "Primed"); - - acElementHandler.handleRestartComposition(composition, AcTypeState.PRIMED); - verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, - "Restarted"); - - acElementHandler.handleRestartComposition(composition, AcTypeState.DEPRIMING); - verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED, - StateChangeResult.NO_ERROR, "Deprimed"); - } - - @Test - void testHandleRestartInstance() throws PfModelException { - var config = new SimConfig(); - config.setDeployTimerMs(1); - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); - var instanceId = UUID.randomUUID(); - var elementId = UUID.randomUUID(); - var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of()); - var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), - Map.of(), Map.of()); - acElementHandler.handleRestartInstance(compositionElement, instanceElement, - DeployState.DEPLOYING, LockState.NONE); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, - DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed"); - - acElementHandler.handleRestartInstance(compositionElement, instanceElement, - DeployState.DEPLOYED, LockState.LOCKED); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, - DeployState.DEPLOYED, LockState.LOCKED, StateChangeResult.NO_ERROR, "Restarted"); - - acElementHandler.handleRestartInstance(compositionElement, instanceElement, - DeployState.UPDATING, LockState.LOCKED); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, - DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated"); - - acElementHandler.handleRestartInstance(compositionElement, instanceElement, DeployState.UNDEPLOYING, - LockState.LOCKED); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, - DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed"); - - acElementHandler.handleRestartInstance(compositionElement, instanceElement, - DeployState.DELETING, LockState.NONE); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, - DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted"); - - acElementHandler.handleRestartInstance(compositionElement, instanceElement, - DeployState.DEPLOYED, LockState.LOCKING); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, - LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked"); - - acElementHandler.handleRestartInstance(compositionElement, instanceElement, DeployState.DEPLOYED, - LockState.UNLOCKING); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, - LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked"); - } - - @Test - void testGetCompositionDataList() { - var acElementDefinition = new AutomationCompositionElementDefinition(); - var toscaConceptIdentifier = new ToscaConceptIdentifier("code", "1.0.0"); - acElementDefinition.setAcElementDefinitionId(toscaConceptIdentifier); - acElementDefinition.setAutomationCompositionElementToscaNodeTemplate(new ToscaNodeTemplate()); - Map outProperties = Map.of("code", "value"); - Map inProperties = Map.of("key", "value"); - acElementDefinition.getAutomationCompositionElementToscaNodeTemplate().setProperties(inProperties); - acElementDefinition.setOutProperties(outProperties); - var elementsDefinitions = Map.of(toscaConceptIdentifier, acElementDefinition); - var compositionId = UUID.randomUUID(); - var map = Map.of(compositionId, elementsDefinitions); - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - when(intermediaryApi.getAcElementsDefinitions()).thenReturn(map); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - - var result = acElementHandler.getCompositionDataList(); - assertThat(result.getList()).hasSize(1); - assertEquals(result.getList().get(0).getCompositionId(), compositionId); - assertEquals(result.getList().get(0).getCompositionDefinitionElementId(), toscaConceptIdentifier); - assertEquals(result.getList().get(0).getOutProperties(), outProperties); - assertEquals(result.getList().get(0).getIntProperties(), inProperties); - } - - @Test - void testSetCompositionData() { - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - - var compositionId = UUID.randomUUID(); - acElementHandler.setCompositionOutProperties(compositionId, null, Map.of()); - verify(intermediaryApi).sendAcDefinitionInfo(compositionId, null, Map.of()); - } - - @Test - void testMigrate() throws PfModelException { - var config = new SimConfig(); - config.setUpdateTimerMs(1); - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); - var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), - Map.of(), Map.of()); - var compositionElementTraget = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), - Map.of(), Map.of()); - var instanceId = UUID.randomUUID(); - var element = new AcElementDeploy(); - element.setId(UUID.randomUUID()); - var instanceElement = new InstanceElementDto(instanceId, element.getId(), null, Map.of(), Map.of()); - var instanceElementMigrated = new InstanceElementDto(instanceId, element.getId(), - null, Map.of("key", "value"), Map.of()); - acElementHandler - .migrate(compositionElement, compositionElementTraget, instanceElement, instanceElementMigrated); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), - DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated"); - - config.setMigrateSuccess(false); - acElementHandler - .migrate(compositionElement, compositionElementTraget, instanceElement, instanceElementMigrated); - verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), - DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!"); - } -} diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerV1Test.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerV1Test.java new file mode 100644 index 000000000..1061b3ba1 --- /dev/null +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerV1Test.java @@ -0,0 +1,285 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2023-2024 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.acm.participant.sim.main.handler; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import java.util.List; +import java.util.Map; +import java.util.UUID; +import org.junit.jupiter.api.Test; +import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi; +import org.onap.policy.clamp.acm.participant.sim.model.SimConfig; +import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy; +import org.onap.policy.clamp.models.acm.concepts.AcTypeState; +import org.onap.policy.clamp.models.acm.concepts.DeployState; +import org.onap.policy.clamp.models.acm.concepts.LockState; +import org.onap.policy.clamp.models.acm.concepts.StateChangeResult; +import org.onap.policy.models.base.PfModelException; + +class AutomationCompositionElementHandlerV1Test { + + @Test + void testDeploy() throws PfModelException { + var config = new SimConfig(); + config.setDeployTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var instanceId = UUID.randomUUID(); + var element = new AcElementDeploy(); + element.setId(UUID.randomUUID()); + acElementHandler.deploy(instanceId, element, Map.of()); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed"); + + config.setDeploySuccess(false); + acElementHandler.deploy(instanceId, element, Map.of()); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Deploy failed!"); + } + + @Test + void testUndeploy() throws PfModelException { + var config = new SimConfig(); + config.setUndeployTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + acElementHandler.undeploy(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED, + null, StateChangeResult.NO_ERROR, "Undeployed"); + + config.setUndeploySuccess(false); + acElementHandler.undeploy(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DEPLOYED, + null, StateChangeResult.FAILED, "Undeploy failed!"); + } + + @Test + void testLock() throws PfModelException { + var config = new SimConfig(); + config.setLockTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + acElementHandler.lock(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED, + StateChangeResult.NO_ERROR, "Locked"); + + config.setLockSuccess(false); + acElementHandler.lock(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED, + StateChangeResult.FAILED, "Lock failed!"); + } + + @Test + void testUnlock() throws PfModelException { + var config = new SimConfig(); + config.setUnlockTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + acElementHandler.unlock(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED, + StateChangeResult.NO_ERROR, "Unlocked"); + + config.setUnlockSuccess(false); + acElementHandler.unlock(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED, + StateChangeResult.FAILED, "Unlock failed!"); + } + + @Test + void testUpdate() throws PfModelException { + var config = new SimConfig(); + config.setUpdateTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var instanceId = UUID.randomUUID(); + var element = new AcElementDeploy(); + element.setId(UUID.randomUUID()); + acElementHandler.update(instanceId, element, Map.of()); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated"); + + config.setUpdateSuccess(false); + acElementHandler.update(instanceId, element, Map.of()); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!"); + } + + @Test + void testDelete() throws PfModelException { + var config = new SimConfig(); + config.setDeleteTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + acElementHandler.delete(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DELETED, + null, StateChangeResult.NO_ERROR, "Deleted"); + + config.setDeleteSuccess(false); + acElementHandler.delete(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED, + null, StateChangeResult.FAILED, "Delete failed!"); + } + + @Test + void testPrime() throws PfModelException { + var config = new SimConfig(); + config.setPrimeTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var compositionId = UUID.randomUUID(); + acElementHandler.prime(compositionId, List.of()); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, + "Primed"); + + config.setPrimeSuccess(false); + acElementHandler.prime(compositionId, List.of()); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED, + StateChangeResult.FAILED, "Prime failed!"); + } + + @Test + void testDeprime() throws PfModelException { + var config = new SimConfig(); + config.setDeprimeTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var compositionId = UUID.randomUUID(); + acElementHandler.deprime(compositionId); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED, + StateChangeResult.NO_ERROR, "Deprimed"); + + config.setDeprimeSuccess(false); + acElementHandler.deprime(compositionId); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED, + "Deprime failed!"); + } + + @Test + void testHandleRestartComposition() throws PfModelException { + var config = new SimConfig(); + config.setPrimeTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var compositionId = UUID.randomUUID(); + acElementHandler.handleRestartComposition(compositionId, List.of(), AcTypeState.PRIMING); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, + "Primed"); + + acElementHandler.handleRestartComposition(compositionId, List.of(), AcTypeState.PRIMED); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, + "Restarted"); + + acElementHandler.handleRestartComposition(compositionId, List.of(), AcTypeState.DEPRIMING); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED, + StateChangeResult.NO_ERROR, "Deprimed"); + } + + @Test + void testHandleRestartInstance() throws PfModelException { + var config = new SimConfig(); + config.setDeployTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var instanceId = UUID.randomUUID(); + var element = new AcElementDeploy(); + element.setId(UUID.randomUUID()); + acElementHandler.handleRestartInstance(instanceId, element, Map.of(), DeployState.DEPLOYING, LockState.NONE); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed"); + + acElementHandler.handleRestartInstance(instanceId, element, Map.of(), DeployState.DEPLOYED, LockState.LOCKED); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, LockState.LOCKED, StateChangeResult.NO_ERROR, "Restarted"); + + acElementHandler.handleRestartInstance(instanceId, element, Map.of(), DeployState.UPDATING, LockState.LOCKED); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated"); + + acElementHandler.handleRestartInstance(instanceId, element, Map.of(), DeployState.UNDEPLOYING, + LockState.LOCKED); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed"); + + acElementHandler.handleRestartInstance(instanceId, element, Map.of(), DeployState.DELETING, LockState.NONE); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted"); + + acElementHandler.handleRestartInstance(instanceId, element, Map.of(), DeployState.DEPLOYED, LockState.LOCKING); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), null, + LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked"); + + acElementHandler.handleRestartInstance(instanceId, element, Map.of(), DeployState.DEPLOYED, + LockState.UNLOCKING); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), null, + LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked"); + } + + @Test + void testMigrate() throws PfModelException { + var config = new SimConfig(); + config.setUpdateTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var instanceId = UUID.randomUUID(); + var element = new AcElementDeploy(); + element.setId(UUID.randomUUID()); + acElementHandler.migrate(instanceId, element, UUID.randomUUID(), Map.of()); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated"); + + config.setMigrateSuccess(false); + acElementHandler.migrate(instanceId, element, UUID.randomUUID(), Map.of()); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!"); + } +} diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerV2Test.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerV2Test.java new file mode 100644 index 000000000..c521d3755 --- /dev/null +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerV2Test.java @@ -0,0 +1,328 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2023-2024 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.acm.participant.sim.main.handler; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import java.util.Map; +import java.util.UUID; +import org.junit.jupiter.api.Test; +import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto; +import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionElementDto; +import org.onap.policy.clamp.acm.participant.intermediary.api.InstanceElementDto; +import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi; +import org.onap.policy.clamp.acm.participant.sim.model.SimConfig; +import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy; +import org.onap.policy.clamp.models.acm.concepts.AcTypeState; +import org.onap.policy.clamp.models.acm.concepts.DeployState; +import org.onap.policy.clamp.models.acm.concepts.LockState; +import org.onap.policy.clamp.models.acm.concepts.StateChangeResult; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; + +class AutomationCompositionElementHandlerV2Test { + + @Test + void testDeploy() throws PfModelException { + var config = new SimConfig(); + config.setDeployTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), + Map.of(), Map.of()); + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of()); + acElementHandler.deploy(compositionElement, instanceElement); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DEPLOYED, + null, StateChangeResult.NO_ERROR, "Deployed"); + + config.setDeploySuccess(false); + acElementHandler.deploy(compositionElement, instanceElement); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED, + null, StateChangeResult.FAILED, "Deploy failed!"); + } + + @Test + void testUndeploy() throws PfModelException { + var config = new SimConfig(); + config.setUndeployTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), + Map.of(), Map.of()); + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of()); + acElementHandler.undeploy(compositionElement, instanceElement); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED, + null, StateChangeResult.NO_ERROR, "Undeployed"); + + config.setUndeploySuccess(false); + acElementHandler.undeploy(compositionElement, instanceElement); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DEPLOYED, + null, StateChangeResult.FAILED, "Undeploy failed!"); + } + + @Test + void testLock() throws PfModelException { + var config = new SimConfig(); + config.setLockTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), + Map.of(), Map.of()); + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of()); + acElementHandler.lock(compositionElement, instanceElement); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED, + StateChangeResult.NO_ERROR, "Locked"); + + config.setLockSuccess(false); + acElementHandler.lock(compositionElement, instanceElement); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED, + StateChangeResult.FAILED, "Lock failed!"); + } + + @Test + void testUnlock() throws PfModelException { + var config = new SimConfig(); + config.setUnlockTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), + Map.of(), Map.of()); + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of()); + acElementHandler.unlock(compositionElement, instanceElement); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED, + StateChangeResult.NO_ERROR, "Unlocked"); + + config.setUnlockSuccess(false); + acElementHandler.unlock(compositionElement, instanceElement); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED, + StateChangeResult.FAILED, "Unlock failed!"); + } + + @Test + void testUpdate() throws PfModelException { + var config = new SimConfig(); + config.setUpdateTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), + Map.of(), Map.of()); + var instanceId = UUID.randomUUID(); + var element = new AcElementDeploy(); + element.setId(UUID.randomUUID()); + var instanceElement = new InstanceElementDto(instanceId, element.getId(), null, Map.of(), Map.of()); + var instanceElementUpdated = new InstanceElementDto(instanceId, element.getId(), null, + Map.of("key", "value"), Map.of()); + acElementHandler.update(compositionElement, instanceElement, instanceElementUpdated); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated"); + + config.setUpdateSuccess(false); + acElementHandler.update(compositionElement, instanceElement, instanceElementUpdated); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!"); + } + + @Test + void testDelete() throws PfModelException { + var config = new SimConfig(); + config.setDeleteTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), + Map.of(), Map.of()); + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of()); + acElementHandler.delete(compositionElement, instanceElement); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DELETED, + null, StateChangeResult.NO_ERROR, "Deleted"); + + config.setDeleteSuccess(false); + acElementHandler.delete(compositionElement, instanceElement); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED, + null, StateChangeResult.FAILED, "Delete failed!"); + } + + @Test + void testPrime() throws PfModelException { + var config = new SimConfig(); + config.setPrimeTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var compositionId = UUID.randomUUID(); + var composition = new CompositionDto(compositionId, Map.of(), Map.of()); + acElementHandler.prime(composition); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, + "Primed"); + + config.setPrimeSuccess(false); + acElementHandler.prime(composition); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED, + StateChangeResult.FAILED, "Prime failed!"); + } + + @Test + void testDeprime() throws PfModelException { + var config = new SimConfig(); + config.setDeprimeTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var compositionId = UUID.randomUUID(); + var composition = new CompositionDto(compositionId, Map.of(), Map.of()); + acElementHandler.deprime(composition); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED, + StateChangeResult.NO_ERROR, "Deprimed"); + + config.setDeprimeSuccess(false); + acElementHandler.deprime(composition); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED, + "Deprime failed!"); + } + + @Test + void testHandleRestartComposition() throws PfModelException { + var config = new SimConfig(); + config.setPrimeTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var compositionId = UUID.randomUUID(); + var composition = new CompositionDto(compositionId, Map.of(), Map.of()); + acElementHandler.handleRestartComposition(composition, AcTypeState.PRIMING); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, + "Primed"); + + acElementHandler.handleRestartComposition(composition, AcTypeState.PRIMED); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, + "Restarted"); + + acElementHandler.handleRestartComposition(composition, AcTypeState.DEPRIMING); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED, + StateChangeResult.NO_ERROR, "Deprimed"); + } + + @Test + void testHandleRestartInstance() throws PfModelException { + var config = new SimConfig(); + config.setDeployTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of()); + var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), + Map.of(), Map.of()); + acElementHandler.handleRestartInstance(compositionElement, instanceElement, + DeployState.DEPLOYING, LockState.NONE); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed"); + + acElementHandler.handleRestartInstance(compositionElement, instanceElement, + DeployState.DEPLOYED, LockState.LOCKED); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, + DeployState.DEPLOYED, LockState.LOCKED, StateChangeResult.NO_ERROR, "Restarted"); + + acElementHandler.handleRestartInstance(compositionElement, instanceElement, + DeployState.UPDATING, LockState.LOCKED); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated"); + + acElementHandler.handleRestartInstance(compositionElement, instanceElement, DeployState.UNDEPLOYING, + LockState.LOCKED); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, + DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed"); + + acElementHandler.handleRestartInstance(compositionElement, instanceElement, + DeployState.DELETING, LockState.NONE); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, + DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted"); + + acElementHandler.handleRestartInstance(compositionElement, instanceElement, + DeployState.DEPLOYED, LockState.LOCKING); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, + LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked"); + + acElementHandler.handleRestartInstance(compositionElement, instanceElement, DeployState.DEPLOYED, + LockState.UNLOCKING); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, + LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked"); + } + + + + @Test + void testMigrate() throws PfModelException { + var config = new SimConfig(); + config.setUpdateTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), + Map.of(), Map.of()); + var compositionElementTraget = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), + Map.of(), Map.of()); + var instanceId = UUID.randomUUID(); + var element = new AcElementDeploy(); + element.setId(UUID.randomUUID()); + var instanceElement = new InstanceElementDto(instanceId, element.getId(), null, Map.of(), Map.of()); + var instanceElementMigrated = new InstanceElementDto(instanceId, element.getId(), + null, Map.of("key", "value"), Map.of()); + acElementHandler + .migrate(compositionElement, compositionElementTraget, instanceElement, instanceElementMigrated); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated"); + + config.setMigrateSuccess(false); + acElementHandler + .migrate(compositionElement, compositionElementTraget, instanceElement, instanceElementMigrated); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!"); + } +} diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/SimulatorServiceTest.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/SimulatorServiceTest.java new file mode 100644 index 000000000..714f71781 --- /dev/null +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/SimulatorServiceTest.java @@ -0,0 +1,126 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.acm.participant.sim.main.handler; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Map; +import java.util.UUID; +import org.junit.jupiter.api.Test; +import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi; +import org.onap.policy.clamp.acm.participant.sim.comm.CommonTestData; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; + +class SimulatorServiceTest { + + @Test + void testGetAutomationCompositions() { + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + + var map = CommonTestData.getTestAutomationCompositionMap(); + when(intermediaryApi.getAutomationCompositions()).thenReturn(map); + var result = simulatorService.getAutomationCompositions(); + assertEquals(map.values().iterator().next(), result.getAutomationCompositionList().get(0)); + } + + @Test + void testGetAutomationComposition() { + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + + var instance = CommonTestData.getTestAutomationCompositionMap().values().iterator().next(); + when(intermediaryApi.getAutomationComposition(instance.getInstanceId())).thenReturn(instance); + var result = simulatorService.getAutomationComposition(instance.getInstanceId()); + assertEquals(instance, result); + } + + @Test + void testSetOutProperties() { + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + var useState = "useState"; + var operationalState = "operationalState"; + Map map = Map.of("id", "1234"); + + simulatorService.setOutProperties(instanceId, elementId, useState, operationalState, map); + verify(intermediaryApi).sendAcElementInfo(instanceId, elementId, useState, operationalState, map); + } + + @Test + void testGetDataList() { + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + + var map = CommonTestData.getTestAutomationCompositionMap(); + when(intermediaryApi.getAutomationCompositions()).thenReturn(map); + var result = simulatorService.getDataList(); + var data = result.getList().get(0); + var automationcomposition = map.values().iterator().next(); + assertEquals(automationcomposition.getInstanceId(), data.getAutomationCompositionId()); + var element = automationcomposition.getElements().values().iterator().next(); + assertEquals(element.getId(), data.getAutomationCompositionElementId()); + } + + @Test + void testGetCompositionDataList() { + var acElementDefinition = new AutomationCompositionElementDefinition(); + var toscaConceptIdentifier = new ToscaConceptIdentifier("code", "1.0.0"); + acElementDefinition.setAcElementDefinitionId(toscaConceptIdentifier); + acElementDefinition.setAutomationCompositionElementToscaNodeTemplate(new ToscaNodeTemplate()); + Map outProperties = Map.of("code", "value"); + Map inProperties = Map.of("key", "value"); + acElementDefinition.getAutomationCompositionElementToscaNodeTemplate().setProperties(inProperties); + acElementDefinition.setOutProperties(outProperties); + var elementsDefinitions = Map.of(toscaConceptIdentifier, acElementDefinition); + var compositionId = UUID.randomUUID(); + var map = Map.of(compositionId, elementsDefinitions); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + when(intermediaryApi.getAcElementsDefinitions()).thenReturn(map); + var simulatorService = new SimulatorService(intermediaryApi); + + var result = simulatorService.getCompositionDataList(); + assertThat(result.getList()).hasSize(1); + assertEquals(result.getList().get(0).getCompositionId(), compositionId); + assertEquals(result.getList().get(0).getCompositionDefinitionElementId(), toscaConceptIdentifier); + assertEquals(result.getList().get(0).getOutProperties(), outProperties); + assertEquals(result.getList().get(0).getIntProperties(), inProperties); + } + + @Test + void testSetCompositionData() { + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + + var compositionId = UUID.randomUUID(); + simulatorService.setCompositionOutProperties(compositionId, null, Map.of()); + verify(intermediaryApi).sendAcDefinitionInfo(compositionId, null, Map.of()); + } +} diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/rest/AcSimRestTest.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/rest/AcSimRestTest.java index 3592a1998..f62d6cd2d 100644 --- a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/rest/AcSimRestTest.java +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/rest/AcSimRestTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2023 Nordix Foundation. + * Copyright (C) 2023-2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,11 +32,12 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.onap.policy.clamp.acm.participant.sim.comm.CommonTestData; -import org.onap.policy.clamp.acm.participant.sim.main.handler.AutomationCompositionElementHandler; +import org.onap.policy.clamp.acm.participant.sim.main.handler.SimulatorService; import org.onap.policy.clamp.acm.participant.sim.model.InternalData; import org.onap.policy.clamp.acm.participant.sim.model.InternalDatas; import org.onap.policy.clamp.acm.participant.sim.model.SimConfig; import org.onap.policy.clamp.acm.participant.sim.parameters.ParticipantSimParameters; +import org.onap.policy.clamp.models.acm.concepts.AutomationComposition; import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.StandardCoder; @@ -70,7 +71,7 @@ class AcSimRestTest { private MockMvc mockMvc; @MockBean - private AutomationCompositionElementHandler automationCompositionElementHandler; + private SimulatorService simulatorService; @Autowired private WebApplicationContext context; @@ -81,10 +82,10 @@ class AcSimRestTest { } @Test - void testgetConfig() throws Exception { + void testGetConfig() throws Exception { var requestBuilder = MockMvcRequestBuilders.get(CONFIG_URL).accept(MediaType.APPLICATION_JSON_VALUE); - doReturn(new SimConfig()).when(automationCompositionElementHandler).getConfig(); + doReturn(new SimConfig()).when(simulatorService).getConfig(); mockMvc.perform(requestBuilder).andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) @@ -92,7 +93,7 @@ class AcSimRestTest { } @Test - void testsetConfig() throws Exception { + void testSetConfig() throws Exception { var requestBuilder = MockMvcRequestBuilders.put(CONFIG_URL).accept(MediaType.APPLICATION_JSON_VALUE) .content(CODER.encode(new SimConfig())).contentType(MediaType.APPLICATION_JSON_VALUE); @@ -100,13 +101,13 @@ class AcSimRestTest { } @Test - void testgetAutomationCompositions() throws Exception { + void testGetAutomationCompositions() throws Exception { var requestBuilder = MockMvcRequestBuilders.get(INSTANCE_URL).accept(MediaType.APPLICATION_JSON_VALUE); var automationCompositions = new AutomationCompositions(); automationCompositions.getAutomationCompositionList().add(CommonTestData.getTestAutomationComposition()); - doReturn(automationCompositions).when(automationCompositionElementHandler).getAutomationCompositions(); + doReturn(automationCompositions).when(simulatorService).getAutomationCompositions(); var result = mockMvc.perform(requestBuilder).andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andReturn(); @@ -117,13 +118,31 @@ class AcSimRestTest { } @Test - void testgetDatas() throws Exception { + void testGetAutomationComposition() throws Exception { + var automationComposition = CommonTestData.getTestAutomationComposition(); + + var requestBuilder = MockMvcRequestBuilders + .get(INSTANCE_URL + "/" + automationComposition.getInstanceId()) + .accept(MediaType.APPLICATION_JSON_VALUE); + + doReturn(automationComposition).when(simulatorService) + .getAutomationComposition(automationComposition.getInstanceId()); + + var result = mockMvc.perform(requestBuilder).andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andReturn(); + var body = result.getResponse().getContentAsString(); + var acsResult = CODER.decode(body, AutomationComposition.class); + assertEquals(automationComposition, acsResult); + } + + @Test + void testGetDatas() throws Exception { var internalDatas = new InternalDatas(); var internalData = new InternalData(); internalData.setAutomationCompositionId(UUID.randomUUID()); internalDatas.getList().add(internalData); - doReturn(internalDatas).when(automationCompositionElementHandler).getDataList(); + doReturn(internalDatas).when(simulatorService).getDataList(); var requestBuilder = MockMvcRequestBuilders.get(DATAS_URL).accept(MediaType.APPLICATION_JSON_VALUE); var result = mockMvc.perform(requestBuilder).andExpect(status().isOk()) @@ -135,7 +154,7 @@ class AcSimRestTest { } @Test - void testsetDatas() throws Exception { + void testSetDatas() throws Exception { var requestBuilder = MockMvcRequestBuilders.put(DATAS_URL).accept(MediaType.APPLICATION_JSON_VALUE) .content(CODER.encode(new InternalData())).contentType(MediaType.APPLICATION_JSON_VALUE); @@ -143,13 +162,13 @@ class AcSimRestTest { } @Test - void testgetCompositionDatas() throws Exception { + void testGetCompositionDatas() throws Exception { var internalDatas = new InternalDatas(); var internalData = new InternalData(); internalData.setCompositionId(UUID.randomUUID()); internalDatas.getList().add(internalData); - doReturn(internalDatas).when(automationCompositionElementHandler).getCompositionDataList(); + doReturn(internalDatas).when(simulatorService).getCompositionDataList(); var requestBuilder = MockMvcRequestBuilders.get(COMPOSITION_DATAS_URL).accept(MediaType.APPLICATION_JSON_VALUE); var result = mockMvc.perform(requestBuilder).andExpect(status().isOk()) @@ -160,7 +179,7 @@ class AcSimRestTest { } @Test - void testsetCompositionDatas() throws Exception { + void testSetCompositionDatas() throws Exception { var requestBuilder = MockMvcRequestBuilders.put(COMPOSITION_DATAS_URL).accept(MediaType.APPLICATION_JSON_VALUE) .content(CODER.encode(new InternalData())).contentType(MediaType.APPLICATION_JSON_VALUE); -- cgit