package org.onap.policy.clamp.acm.runtime.supervision;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.times;
import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
+import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
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.MigrationState;
import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionDeployAck;
import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessageType;
import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
import org.onap.policy.clamp.models.acm.persistence.provider.MessageProvider;
+import org.onap.policy.models.base.PfModelRuntimeException;
class SupervisionAcHandlerTest {
private static final String AC_INSTANTIATION_CREATE_JSON = "src/test/resources/rest/acm/AutomationComposition.json";
@Test
void testAutomationCompositionDeployAckValidation() {
+ var automationComposition =
+ InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Crud");
+ assertNotNull(automationComposition);
+
var automationCompositionProvider = mock(AutomationCompositionProvider.class);
var messageProvider = mock(MessageProvider.class);
var handler = new SupervisionAcHandler(automationCompositionProvider,
mock(AcElementPropertiesPublisher.class), mock(AutomationCompositionMigrationPublisher.class),
mock(AcPreparePublisher.class), messageProvider, mock(EncryptionUtils.class));
- var automationComposition =
- InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Crud");
automationComposition.setInstanceId(IDENTIFIER);
var automationCompositionAckMessage =
getAutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATECHANGE_ACK,
void testHandleAcMigrationWithStage() {
var automationComposition =
InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Crud");
+ assertNotNull(automationComposition);
automationComposition.setInstanceId(IDENTIFIER);
var automationCompositionProvider = mock(AutomationCompositionProvider.class);
when(automationCompositionProvider.findAutomationComposition(IDENTIFIER))
void testHandleAutomationCompositionStateChangeAckMessage() {
var automationComposition =
InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Crud");
+ assertNotNull(automationComposition);
automationComposition.setInstanceId(IDENTIFIER);
var automationCompositionProvider = mock(AutomationCompositionProvider.class);
when(automationCompositionProvider.findAutomationComposition(IDENTIFIER))
void testHandleAutomationCompositionUpdateAckMessage() {
var automationComposition =
InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Crud");
+ assertNotNull(automationComposition);
automationComposition.setInstanceId(IDENTIFIER);
var automationCompositionProvider = mock(AutomationCompositionProvider.class);
when(automationCompositionProvider.findAutomationComposition(IDENTIFIER))
void testHandleAcUpdateAckFailedMessage() {
var automationComposition =
InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Crud");
+ assertNotNull(automationComposition);
automationComposition.setDeployState(DeployState.DEPLOYING);
automationComposition.setStateChangeResult(StateChangeResult.NO_ERROR);
var automationCompositionProvider = mock(AutomationCompositionProvider.class);
void testDeploy() {
var automationComposition =
InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Deploy");
+ assertNotNull(automationComposition);
automationComposition.setDeployState(DeployState.UNDEPLOYED);
deploy(automationComposition);
}
void testDeployFailed() {
var automationComposition =
InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Deploy");
+ assertNotNull(automationComposition);
automationComposition.setDeployState(DeployState.UNDEPLOYING);
automationComposition.getElements().values()
.forEach(element -> element.setDeployState(DeployState.UNDEPLOYING));
void testDeployFailedSimple() {
var automationComposition =
InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_SIMPLE_JSON, "Deploy");
+ assertNotNull(automationComposition);
automationComposition.setDeployState(DeployState.UNDEPLOYED);
automationComposition.getElements().values().iterator().next().setDeployState(DeployState.UNDEPLOYED);
deploy(automationComposition);
void testUndeploy() {
var automationComposition =
InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Undeploy");
+ assertNotNull(automationComposition);
automationComposition.setDeployState(DeployState.DEPLOYED);
automationComposition.setStateChangeResult(StateChangeResult.NO_ERROR);
automationComposition.getElements().values()
void testUndeployFailed() {
var automationComposition =
InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "UnDeploy");
+ assertNotNull(automationComposition);
automationComposition.setDeployState(DeployState.DEPLOYING);
automationComposition.setStateChangeResult(StateChangeResult.FAILED);
automationComposition.getElements().values()
void testUndeployFailedSimple() {
var automationComposition =
InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_SIMPLE_JSON, "UnDeploy");
+ assertNotNull(automationComposition);
automationComposition.setDeployState(DeployState.DEPLOYING);
automationComposition.setStateChangeResult(StateChangeResult.FAILED);
automationComposition.getElements().values().iterator().next().setDeployState(DeployState.UNDEPLOYED);
void testUnlock() {
var automationComposition =
InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "UnLock");
+ assertNotNull(automationComposition);
automationComposition.setLockState(LockState.LOCKED);
automationComposition.getElements().values()
.forEach(element -> element.setLockState(LockState.LOCKED));
void testUnlockFailed() {
var automationComposition =
InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "UnLock");
+ assertNotNull(automationComposition);
automationComposition.setStateChangeResult(StateChangeResult.FAILED);
automationComposition.setLockState(LockState.LOCKING);
automationComposition.getElements().values().forEach(element -> element.setLockState(LockState.LOCKING));
void testUnlockSimple() {
var automationComposition =
InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_SIMPLE_JSON, "UnLock");
+ assertNotNull(automationComposition);
automationComposition.setStateChangeResult(StateChangeResult.FAILED);
automationComposition.setLockState(LockState.LOCKING);
automationComposition.getElements().values().iterator().next().setLockState(LockState.UNLOCKED);
void testLock() {
var automationComposition =
InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Lock");
+ assertNotNull(automationComposition);
automationComposition.setLockState(LockState.UNLOCKED);
automationComposition.getElements().values().forEach(element -> element.setLockState(LockState.UNLOCKED));
lock(automationComposition);
void testLockFailed() {
var automationComposition =
InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Lock");
+ assertNotNull(automationComposition);
automationComposition.setLockState(LockState.UNLOCKING);
automationComposition.setStateChangeResult(StateChangeResult.FAILED);
automationComposition.getElements().values().forEach(element -> element.setLockState(LockState.UNLOCKING));
void testLockSimple() {
var automationComposition =
InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_SIMPLE_JSON, "Lock");
+ assertNotNull(automationComposition);
automationComposition.setLockState(LockState.UNLOCKING);
automationComposition.setStateChangeResult(StateChangeResult.FAILED);
automationComposition.getElements().values().iterator().next().setLockState(LockState.LOCKED);
var automationCompositionProvider = mock(AutomationCompositionProvider.class);
var automationComposition =
InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Crud");
+ assertNotNull(automationComposition);
automationComposition.setDeployState(DeployState.DELETING);
when(automationCompositionProvider.findAutomationComposition(IDENTIFIER))
.thenReturn(Optional.of(automationComposition));
var acDefinition = CommonTestData.createAcDefinition(serviceTemplate, AcTypeState.PRIMED);
var automationComposition =
InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Migrate");
+ assertNotNull(automationComposition);
handler.prepare(automationComposition, acDefinition);
verify(acPreparePublisher, timeout(1000)).sendPrepare(any(AutomationComposition.class), anyInt(),
any(UUID.class));
acPreparePublisher, mock(MessageProvider.class), mock(EncryptionUtils.class));
var automationComposition =
InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Migrate");
+ assertNotNull(automationComposition);
handler.review(automationComposition, new AutomationCompositionDefinition());
verify(acPreparePublisher, timeout(1000)).sendReview(any(AutomationComposition.class), any(UUID.class));
}
+
+ @Test
+ void testDeployWithInvalidElement_NewState() {
+ var elementNew = new AutomationCompositionElement();
+ elementNew.setDeployState(DeployState.UNDEPLOYED);
+ elementNew.setMigrationState(MigrationState.NEW);
+
+ var automationComposition =
+ InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Deploy");
+ assertNotNull(automationComposition);
+ automationComposition.setDeployState(DeployState.DEPLOYING);
+ automationComposition.getElements().put(elementNew.getParticipantId(), elementNew);
+
+ var mockHandler = mock(SupervisionAcHandler.class);
+ var acDefinition = new AutomationCompositionDefinition();
+ doCallRealMethod().when(mockHandler).deploy(automationComposition, acDefinition);
+
+ assertThrows(PfModelRuntimeException.class,
+ () -> mockHandler.deploy(automationComposition, acDefinition));
+ }
+
+ @Test
+ void testDeployWithInvalidElement_RemovedState() {
+ var elementRemoved = new AutomationCompositionElement();
+ elementRemoved.setDeployState(DeployState.UNDEPLOYED);
+ elementRemoved.setMigrationState(MigrationState.REMOVED);
+
+ var automationComposition =
+ InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Deploy");
+ assertNotNull(automationComposition);
+ automationComposition.setDeployState(DeployState.DEPLOYING);
+ automationComposition.getElements().put(elementRemoved.getParticipantId(), elementRemoved);
+
+ var mockHandler = mock(SupervisionAcHandler.class);
+ var acDefinition = new AutomationCompositionDefinition();
+ doCallRealMethod().when(mockHandler).deploy(automationComposition, acDefinition);
+
+ assertThrows(PfModelRuntimeException.class,
+ () -> mockHandler.deploy(automationComposition, acDefinition));
+ }
}