If compositionId is referenced a targetCompositionId in the instance table, de-priming is not allowed
- Added this validation
Issue-ID: POLICY-5462
Change-Id: I96aa13362c791ec2f76d26aa69b482c95e3d5c7e
Signed-off-by: waynedunican <wayne.dunican@est.tech>
.asEntityList(automationCompositionRepository.findByCompositionId(compositionId.toString()));
}
+ /**
+ * Get all automation compositions by targetCompositionId.
+ *
+ * @param targetCompositionId the target composition ID of the AC definition
+ * @return all automation compositions found
+ */
+ @Transactional(readOnly = true)
+ public List<AutomationComposition> getAcInstancesByTargetCompositionId(UUID targetCompositionId) {
+ return ProviderUtils.asEntityList(automationCompositionRepository
+ .findByCompositionTargetId(targetCompositionId.toString()));
+ }
+
/**
* Get all automation compositions in transition.
*
List<JpaAutomationComposition> findBySubStateIn(Collection<SubState> subStates);
+ List<JpaAutomationComposition> findByCompositionTargetId(String compositionTargetId);
+
Page<JpaAutomationComposition> findByStateChangeResultInAndDeployStateIn(
Collection<StateChangeResult> stateChangeResults, Collection<DeployState> deployStates,
Pageable pageable);
assertEquals(inputAutomationCompositions.getAutomationCompositionList(), acList);
}
+ @Test
+ void testGetAcInstancesByTargetCompositionId() {
+ var automationComposition = inputAutomationCompositions.getAutomationCompositionList().get(0);
+ var automationCompositionRepository = mock(AutomationCompositionRepository.class);
+ when(automationCompositionRepository.findByCompositionTargetId(any()))
+ .thenReturn(List.of(inputAutomationCompositionsJpa.get(0)));
+ var automationCompositionProvider = new AutomationCompositionProvider(
+ automationCompositionRepository, mock(AutomationCompositionElementRepository.class),
+ mock(AutomationCompositionRollbackRepository.class));
+ var acList = automationCompositionProvider
+ .getAcInstancesByTargetCompositionId(automationComposition.getCompositionTargetId());
+ assertEquals(inputAutomationCompositions.getAutomationCompositionList().get(0), acList.get(0));
+ }
+
@Test
void testGetAcInstancesInTransition() {
inputAutomationCompositions.getAutomationCompositionList().get(0).setDeployState(DeployState.DEPLOYING);
{
"compositionId": "709c62b3-8918-41b9-a747-e21eb79c6c40",
"instanceId": "809c62b3-8918-41b9-a748-e21eb79c6c89",
+ "compositionTargetId": "709c62b3-8918-41b9-a747-e21ed79d6d41",
"deployState": "UNDEPLOYED",
"lockState": "NONE",
"lastMsg": "2024-05-22 10:04:37.6020187",
return !acProvider.getAcInstancesByCompositionId(compositionId).isEmpty();
}
+ /**
+ * Validates to see if there is any compositionTargetId associated with this compositionId.
+ *
+ * @return true if exists compositionTargetId
+ */
+ private boolean verifyIfCompositionTargetIdExists(UUID compositionId) {
+ return !acProvider.getAcInstancesByTargetCompositionId(compositionId).isEmpty();
+ }
+
/**
* Composition Definition Priming.
*
if (verifyIfInstanceExists(compositionId)) {
throw new PfModelRuntimeException(Status.BAD_REQUEST, "There are instances, Priming/Depriming not allowed");
}
+ if (verifyIfCompositionTargetIdExists(compositionId)) {
+ throw new PfModelRuntimeException(Status.BAD_REQUEST,
+ "This compositionId is referenced as a targetCompositionId in the instance table.");
+ }
var acmDefinition = acDefinitionProvider.getAcDefinition(compositionId);
var stateOrdered = acTypeStateResolver.resolve(acTypeStateUpdate.getPrimeOrder(), acmDefinition.getState(),
acmDefinition.getStateChangeResult());
var acTypeStateUpdate = new AcTypeStateUpdate();
assertThatThrownBy(() -> provider.compositionDefinitionPriming(compositionId, acTypeStateUpdate))
.hasMessageMatching("There are instances, Priming/Depriming not allowed");
+
+ when(acProvider.getAcInstancesByCompositionId(compositionId)).thenReturn(List.of());
+ when(acProvider.getAcInstancesByTargetCompositionId(compositionId))
+ .thenReturn(List.of(new AutomationComposition()));
+ assertThatThrownBy(() -> provider.compositionDefinitionPriming(compositionId, acTypeStateUpdate))
+ .hasMessageMatching("This compositionId is referenced as a targetCompositionId in the instance table.");
}
@Test