summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorFrancescoFioraEst <francesco.fiora@est.tech>2023-12-07 08:58:32 +0000
committerFrancesco Fiora <francesco.fiora@est.tech>2023-12-07 11:11:41 +0000
commitb2393969603cb943bc76f2b65da748b871ee9d17 (patch)
tree80610ffcca8752119e8cc105d07002c541fad5ec /models
parent3b28926870225fce8df70030d931d5027943e775 (diff)
Add validation for AC instance element id
Issue-ID: POLICY-4900 Change-Id: I78be5924950eec508856be5850a3a749860e0e49 Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
Diffstat (limited to 'models')
-rwxr-xr-xmodels/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProvider.java33
-rw-r--r--models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProviderTest.java62
2 files changed, 93 insertions, 2 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());
+ }
}