diff options
author | FrancescoFioraEst <francesco.fiora@est.tech> | 2023-12-07 08:58:32 +0000 |
---|---|---|
committer | Francesco Fiora <francesco.fiora@est.tech> | 2023-12-07 11:11:41 +0000 |
commit | b2393969603cb943bc76f2b65da748b871ee9d17 (patch) | |
tree | 80610ffcca8752119e8cc105d07002c541fad5ec | |
parent | 3b28926870225fce8df70030d931d5027943e775 (diff) |
Add validation for AC instance element id
Issue-ID: POLICY-4900
Change-Id: I78be5924950eec508856be5850a3a749860e0e49
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
4 files changed, 104 insertions, 14 deletions
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProvider.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProvider.java index 6b90619c9..1ffcc1b6e 100755 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProvider.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProvider.java @@ -40,6 +40,8 @@ import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationCompos import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionElementRepository; import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionRepository; import org.onap.policy.clamp.models.acm.utils.AcmUtils; +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.common.parameters.ValidationStatus; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.springframework.data.domain.Example; @@ -225,4 +227,35 @@ public class AutomationCompositionProvider { ProviderUtils.validate(element, jpaAcElement, "AutomationCompositionElement"); acElementRepository.save(jpaAcElement); } + + /** + * Validate ElementIds. + * + * @param automationComposition the AutomationComposition + * @return the BeanValidationResult + */ + public BeanValidationResult validateElementIds(final AutomationComposition automationComposition) { + var result = new BeanValidationResult( + "UUID elements " + automationComposition.getName(), automationComposition); + + var ids = automationComposition + .getElements().values().stream().map(AutomationCompositionElement::getId).toList(); + var elements = acElementRepository.findAllById(ids.stream().map(UUID::toString).toList()); + if (automationComposition.getInstanceId() == null) { + for (var element : elements) { + result.addResult( + element.getDescription(), element.getElementId(), ValidationStatus.INVALID, "UUID already used"); + } + } else { + var instanceId = automationComposition.getInstanceId().toString(); + for (var element : elements) { + if (!instanceId.equals(element.getInstanceId())) { + result.addResult( + element.getDescription(), element.getElementId(), ValidationStatus.INVALID, + "UUID already used"); + } + } + } + return result; + } } diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProviderTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProviderTest.java index e7ceab830..b09ddda74 100644 --- a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProviderTest.java +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProviderTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021-2022 Nordix Foundation. + * Copyright (C) 2021-2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,8 +22,11 @@ package org.onap.policy.clamp.models.acm.persistence.provider; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyIterable; +import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -34,8 +37,11 @@ import java.util.UUID; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementInfo; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionInfo; import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions; import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationComposition; +import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationCompositionElement; import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionElementRepository; import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionRepository; import org.onap.policy.common.utils.coder.Coder; @@ -98,7 +104,7 @@ class AutomationCompositionProviderTest { } @Test - void testGetAutomationCompositions() throws Exception { + void testGetAutomationCompositions() { var automationCompositionRepository = mock(AutomationCompositionRepository.class); var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository, mock(AutomationCompositionElementRepository.class)); @@ -202,4 +208,56 @@ class AutomationCompositionProviderTest { automationCompositionProvider.updateAutomationCompositionElement(acElement, UUID.randomUUID()); verify(acElementRepository).save(any()); } + + @Test + void testValidateElementIds() { + var acElementRepository = mock(AutomationCompositionElementRepository.class); + var automationCompositionProvider = new AutomationCompositionProvider( + mock(AutomationCompositionRepository.class), acElementRepository); + + var ac = inputAutomationCompositions.getAutomationCompositionList().get(0); + + var result = automationCompositionProvider.validateElementIds(ac); + assertThat(result.isValid()).isTrue(); + + var jpaElement = new JpaAutomationCompositionElement(ac.getElements().values().iterator().next()); + when(acElementRepository.findAllById(anyIterable())) + .thenReturn(List.of(jpaElement)); + + ac.setInstanceId(null); + result = automationCompositionProvider.validateElementIds(ac); + assertThat(result.isValid()).isFalse(); + + ac.setInstanceId(UUID.randomUUID()); + jpaElement.setInstanceId(UUID.randomUUID().toString()); + result = automationCompositionProvider.validateElementIds(ac); + assertThat(result.isValid()).isFalse(); + + ac.setInstanceId(UUID.randomUUID()); + jpaElement.setInstanceId(ac.getInstanceId().toString()); + result = automationCompositionProvider.validateElementIds(ac); + assertThat(result.isValid()).isTrue(); + } + + @Test + void testUpgradeStates() { + var acElementRepository = mock(AutomationCompositionElementRepository.class); + var automationCompositionProvider = new AutomationCompositionProvider( + mock(AutomationCompositionRepository.class), acElementRepository); + + assertDoesNotThrow(() -> automationCompositionProvider.upgradeStates(List.of())); + var acElement = inputAutomationCompositions.getAutomationCompositionList().get(0).getElements().values() + .iterator().next(); + + var acInfo = new AutomationCompositionInfo(); + var acElementInfo = new AutomationCompositionElementInfo(); + acInfo.setElements(List.of(acElementInfo)); + acElementInfo.setAutomationCompositionElementId(acElement.getId()); + + when(acElementRepository.getReferenceById(acElement.getId().toString())) + .thenReturn(new JpaAutomationCompositionElement(acElement)); + + automationCompositionProvider.upgradeStates(List.of(acInfo)); + verify(acElementRepository).saveAll(anyList()); + } } diff --git a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/instantiation/AutomationCompositionInstantiationProvider.java b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/instantiation/AutomationCompositionInstantiationProvider.java index af4e844d5..9f126406b 100755 --- a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/instantiation/AutomationCompositionInstantiationProvider.java +++ b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/instantiation/AutomationCompositionInstantiationProvider.java @@ -237,6 +237,8 @@ public class AutomationCompositionInstantiationProvider { acDefinitionOpt.get().getServiceTemplate(), acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName())); + result.addResult(automationCompositionProvider.validateElementIds(automationComposition)); + if (result.isValid()) { for (var element : automationComposition.getElements().values()) { var name = element.getDefinition().getName(); diff --git a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/instantiation/InstantiationUtils.java b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/instantiation/InstantiationUtils.java index 38c63fa9b..8639dffcd 100644 --- a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/instantiation/InstantiationUtils.java +++ b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/instantiation/InstantiationUtils.java @@ -25,6 +25,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; import java.io.File; +import java.util.ArrayList; +import java.util.UUID; import org.onap.policy.clamp.models.acm.concepts.AutomationComposition; import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationResponse; import org.onap.policy.common.utils.coder.Coder; @@ -56,6 +58,13 @@ public class InstantiationUtils { // add suffix to name automationComposition.setName(automationComposition.getName() + suffix); + var elements = new ArrayList<>(automationComposition.getElements().values()); + automationComposition.getElements().clear(); + for (var element : elements) { + // set unique UUID to the element + element.setId(UUID.randomUUID()); + automationComposition.getElements().put(element.getId(), element); + } return automationComposition; } catch (CoderException e) { fail("Cannot read or decode " + path); @@ -84,18 +93,6 @@ public class InstantiationUtils { } /** - * Assert that Instantiation Response contains proper AutomationCompositions. - * - * @param response InstantiationResponse - * @param affectedAutomationComposition ToscaConceptIdentifier - */ - public static void assertInstantiationResponse(InstantiationResponse response, - ToscaConceptIdentifier affectedAutomationComposition) { - assertThat(response).isNotNull(); - assertEquals(response.getAffectedAutomationComposition(), affectedAutomationComposition); - } - - /** * Assert that Instantiation Response contains AutomationComposition equals to automationComposition. * * @param response InstantiationResponse |