From 934f7bd443225a6945b0542fa5cb7c043deac426 Mon Sep 17 00:00:00 2001 From: FrancescoFioraEst Date: Thu, 26 Jan 2023 17:31:46 +0000 Subject: Refactor Prime and Deprime messages in ACM Issue-ID: POLICY-4502 Change-Id: Ib0ecc513285bf971a0c25cec528dcdeec5ad63a2 Signed-off-by: FrancescoFioraEst --- .../models/acm/concepts/ParticipantUtils.java | 25 ---- .../dmaap/participant/ParticipantAckMessage.java | 2 + .../JpaAutomationCompositionDefinition.java | 8 +- .../persistence/concepts/JpaNodeTemplateState.java | 14 ++- .../persistence/provider/AcDefinitionProvider.java | 10 +- .../persistence/provider/ParticipantProvider.java | 22 ++++ .../policy/clamp/models/acm/utils/AcmUtils.java | 136 +++++++++++++++------ .../models/acm/concepts/ParticipantUtilsTest.java | 31 +---- .../provider/AcDefinitionProviderTest.java | 1 + .../provider/ParticipantProviderTest.java | 13 ++ .../clamp/models/acm/utils/AcmUtilsTest.java | 68 +++++++++-- 11 files changed, 219 insertions(+), 111 deletions(-) (limited to 'models') diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/ParticipantUtils.java b/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/ParticipantUtils.java index 015a96aad..54e6db1cc 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/ParticipantUtils.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/ParticipantUtils.java @@ -32,8 +32,6 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; public final class ParticipantUtils { private static final Coder CODER = new StandardCoder(); - private static final String AUTOMATION_COMPOSITION_ELEMENT = - "org.onap.policy.clamp.acm.AutomationCompositionElement"; /** * Get the First StartPhase. @@ -75,27 +73,4 @@ public final class ParticipantUtils { } return 0; } - - /** - * Checks if a NodeTemplate is an AutomationCompositionElement. - * - * @param nodeTemplate the ToscaNodeTemplate - * @param toscaServiceTemplate the ToscaServiceTemplate - * @return true if the NodeTemplate is an AutomationCompositionElement - */ - public static boolean checkIfNodeTemplateIsAutomationCompositionElement(ToscaNodeTemplate nodeTemplate, - ToscaServiceTemplate toscaServiceTemplate) { - if (nodeTemplate.getType().contains(AUTOMATION_COMPOSITION_ELEMENT)) { - return true; - } else { - var nodeType = toscaServiceTemplate.getNodeTypes().get(nodeTemplate.getType()); - if (nodeType != null) { - var derivedFrom = nodeType.getDerivedFrom(); - if (derivedFrom != null) { - return derivedFrom.contains(AUTOMATION_COMPOSITION_ELEMENT); - } - } - } - return false; - } } diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/dmaap/participant/ParticipantAckMessage.java b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/dmaap/participant/ParticipantAckMessage.java index bdb2be861..e73f2e795 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/dmaap/participant/ParticipantAckMessage.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/dmaap/participant/ParticipantAckMessage.java @@ -46,6 +46,8 @@ public class ParticipantAckMessage { private ParticipantMessageType messageType; + private UUID compositionId; + /** * Participant ID, or {@code null} for messages from participants. */ diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionDefinition.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionDefinition.java index fcaa6e6db..11e3f583e 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionDefinition.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionDefinition.java @@ -89,8 +89,9 @@ public class JpaAutomationCompositionDefinition extends Validated @Override public AutomationCompositionDefinition toAuthorative() { var acmDefinition = new AutomationCompositionDefinition(); - acmDefinition.setCompositionId(UUID.fromString(compositionId)); - acmDefinition.setServiceTemplate(serviceTemplate.toAuthorative()); + acmDefinition.setCompositionId(UUID.fromString(this.compositionId)); + acmDefinition.setState(this.state); + acmDefinition.setServiceTemplate(this.serviceTemplate.toAuthorative()); for (var element : this.elements) { var key = element.getNodeTemplateId().getName(); acmDefinition.getElementStateMap().put(key, element.toAuthorative()); @@ -105,11 +106,12 @@ public class JpaAutomationCompositionDefinition extends Validated this.serviceTemplate = new DocToscaServiceTemplate(copyConcept.getServiceTemplate()); setName(this.serviceTemplate.getName()); setVersion(this.serviceTemplate.getVersion()); - elements = new HashSet<>(copyConcept.getElementStateMap().size()); + this.elements = new HashSet<>(copyConcept.getElementStateMap().size()); for (var element : copyConcept.getElementStateMap().values()) { var nodeTemplateStateId = element.getNodeTemplateStateId().toString(); var jpaNodeTemplateState = new JpaNodeTemplateState(nodeTemplateStateId, this.compositionId); jpaNodeTemplateState.fromAuthorative(element); + this.elements.add(jpaNodeTemplateState); } } diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaNodeTemplateState.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaNodeTemplateState.java index a6d13a638..088bf2196 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaNodeTemplateState.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaNodeTemplateState.java @@ -21,6 +21,7 @@ package org.onap.policy.clamp.models.acm.persistence.concepts; import java.util.UUID; +import javax.persistence.AttributeOverride; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; @@ -33,7 +34,9 @@ import org.onap.policy.clamp.models.acm.concepts.AcTypeState; import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState; import org.onap.policy.common.parameters.annotations.NotNull; import org.onap.policy.models.base.PfAuthorative; +import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.Validated; +import org.onap.policy.models.base.validation.annotations.VerifyKey; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; @Entity @@ -54,9 +57,11 @@ public class JpaNodeTemplateState extends Validated implements PfAuthorative findAcDefinition(UUID compositionId) { + public Optional findAcDefinition(UUID compositionId) { var jpaGet = acmDefinitionRepository.findById(compositionId.toString()); - return jpaGet.stream().map(JpaAutomationCompositionDefinition::getServiceTemplate) - .map(DocToscaServiceTemplate::toAuthorative).findFirst(); + return jpaGet.stream().map(JpaAutomationCompositionDefinition::toAuthorative).findFirst(); } /** diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/ParticipantProvider.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/ParticipantProvider.java index 7b8ebc038..8e45c770b 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/ParticipantProvider.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/ParticipantProvider.java @@ -20,7 +20,9 @@ package org.onap.policy.clamp.models.acm.persistence.provider; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.UUID; import javax.ws.rs.core.Response.Status; @@ -30,6 +32,7 @@ import org.onap.policy.clamp.models.acm.concepts.Participant; import org.onap.policy.clamp.models.acm.persistence.concepts.JpaParticipant; import org.onap.policy.clamp.models.acm.persistence.repository.ParticipantRepository; import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -127,4 +130,23 @@ public class ParticipantProvider { return jpaDeleteParticipantOpt.get().toAuthorative(); } + + + /** + * Get a map with SupportedElement as key and the participantId as value. + * + * @return a map + */ + public Map getSupportedElementMap() { + var list = participantRepository.findAll(); + Map map = new HashMap<>(); + for (var participant : list) { + for (var element : participant.getSupportedElements()) { + var supportedElement = new ToscaConceptIdentifier(element.getTypeName(), element.getTypeVersion()); + map.put(supportedElement, UUID.fromString(participant.getParticipantId())); + } + } + return map; + } + } diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/utils/AcmUtils.java b/models/src/main/java/org/onap/policy/clamp/models/acm/utils/AcmUtils.java index 8bd9039fd..9f73cb144 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/utils/AcmUtils.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/utils/AcmUtils.java @@ -22,25 +22,31 @@ package org.onap.policy.clamp.models.acm.utils; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.UUID; import java.util.function.Function; import java.util.function.UnaryOperator; import java.util.stream.Collectors; +import javax.ws.rs.core.Response; import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; +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.AutomationCompositionElement; import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition; +import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState; import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition; import org.onap.policy.clamp.models.acm.concepts.ParticipantUpdates; import org.onap.policy.common.parameters.BeanValidationResult; import org.onap.policy.common.parameters.ObjectValidationResult; import org.onap.policy.common.parameters.ValidationResult; 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.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; @@ -53,6 +59,8 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class AcmUtils { + public static final String AUTOMATION_COMPOSITION_ELEMENT = + "org.onap.policy.clamp.acm.AutomationCompositionElement"; public static final String AUTOMATION_COMPOSITION_NODE_TYPE = "org.onap.policy.clamp.acm.AutomationComposition"; public static final String ENTRY = "entry "; @@ -111,51 +119,107 @@ public final class AcmUtils { } /** - * Prepare ParticipantDefinitionUpdate to set in the message. + * Checks if a NodeTemplate is an AutomationCompositionElement. * - * @param acParticipantId participant id - * @param entryKey key for the entry - * @param entryValue value relates to toscaNodeTemplate - * @param participantDefinitionUpdates list of participantDefinitionUpdates + * @param nodeTemplate the ToscaNodeTemplate + * @param toscaServiceTemplate the ToscaServiceTemplate + * @return true if the NodeTemplate is an AutomationCompositionElement */ - public static void prepareParticipantDefinitionUpdate(UUID acParticipantId, String entryKey, - ToscaNodeTemplate entryValue, - List participantDefinitionUpdates) { - - var acDefinition = new AutomationCompositionElementDefinition(); - acDefinition.setAcElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion())); - acDefinition.setAutomationCompositionElementToscaNodeTemplate(entryValue); - - List automationCompositionElementDefinitionList = new ArrayList<>(); - - if (participantDefinitionUpdates.isEmpty()) { - participantDefinitionUpdates.add(getParticipantDefinition(acDefinition, acParticipantId, - automationCompositionElementDefinitionList)); + public static boolean checkIfNodeTemplateIsAutomationCompositionElement(ToscaNodeTemplate nodeTemplate, + ToscaServiceTemplate toscaServiceTemplate) { + if (nodeTemplate.getType().contains(AUTOMATION_COMPOSITION_ELEMENT)) { + return true; } else { - var participantExists = false; - for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) { - if (acParticipantId != null || participantDefinitionUpdate.getParticipantId() != null) { - if (participantDefinitionUpdate.getParticipantId().equals(acParticipantId)) { - participantDefinitionUpdate.getAutomationCompositionElementDefinitionList().add(acDefinition); - participantExists = true; - } + var nodeType = toscaServiceTemplate.getNodeTypes().get(nodeTemplate.getType()); + if (nodeType != null) { + var derivedFrom = nodeType.getDerivedFrom(); + if (derivedFrom != null) { + return derivedFrom.contains(AUTOMATION_COMPOSITION_ELEMENT); } } - if (!participantExists) { - participantDefinitionUpdates.add(getParticipantDefinition(acDefinition, acParticipantId, - automationCompositionElementDefinitionList)); + } + return false; + } + + /** + * Prepare list of ParticipantDefinition for the Priming message. + * + * @param acElements the extracted AcElements from ServiceTemplate + * @param supportedElementMap supported Element Map + */ + public static List prepareParticipantPriming( + List> acElements, Map supportedElementMap) { + + Map> map = new HashMap<>(); + for (var elementEntry : acElements) { + var type = new ToscaConceptIdentifier(elementEntry.getValue().getType(), + elementEntry.getValue().getTypeVersion()); + var participantId = supportedElementMap.get(type); + if (participantId == null) { + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, + "Element Type " + type + " not supported"); } + var acElementDefinition = new AutomationCompositionElementDefinition(); + acElementDefinition.setAcElementDefinitionId( + new ToscaConceptIdentifier(elementEntry.getKey(), elementEntry.getValue().getVersion())); + acElementDefinition.setAutomationCompositionElementToscaNodeTemplate(elementEntry.getValue()); + map.putIfAbsent(participantId, new ArrayList<>()); + map.get(participantId).add(acElementDefinition); + } + return prepareParticipantPriming(map); + } + + /** + * Prepare ParticipantPriming. + * + * @param map of AutomationCompositionElementDefinition with participantId as key + * @return list of ParticipantDefinition + */ + public static List prepareParticipantPriming( + Map> map) { + List result = new ArrayList<>(); + for (var entry : map.entrySet()) { + var participantDefinition = new ParticipantDefinition(); + participantDefinition.setParticipantId(entry.getKey()); + participantDefinition.setAutomationCompositionElementDefinitionList(entry.getValue()); + result.add(participantDefinition); } + return result; } - private static ParticipantDefinition getParticipantDefinition(AutomationCompositionElementDefinition acDefinition, - UUID acParticipantId, - List automationCompositionElementDefinitionList) { - var participantDefinition = new ParticipantDefinition(); - participantDefinition.setParticipantId(acParticipantId); - automationCompositionElementDefinitionList.add(acDefinition); - participantDefinition.setAutomationCompositionElementDefinitionList(automationCompositionElementDefinitionList); - return participantDefinition; + /** + * Extract AcElements from ServiceTemplate. + * + * @param serviceTemplate the ToscaServiceTemplate + * @return the list of Entry of AutomationCompositionElement + */ + public static List> extractAcElementsFromServiceTemplate( + ToscaServiceTemplate serviceTemplate) { + return serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet().stream().filter( + nodeTemplateEntry -> checkIfNodeTemplateIsAutomationCompositionElement(nodeTemplateEntry.getValue(), + serviceTemplate)) + .collect(Collectors.toList()); + } + + /** + * Create NodeTemplateState Map. + * + * @param acElements extracted AcElements from ServiceTemplate. + * @param state the AcTypeState + * @return the NodeTemplateState Map + */ + public static Map createElementStateMap( + List> acElements, AcTypeState state) { + Map result = new HashMap<>(acElements.size()); + for (var entry : acElements) { + var nodeTemplateState = new NodeTemplateState(); + nodeTemplateState.setNodeTemplateStateId(UUID.randomUUID()); + nodeTemplateState.setState(state); + nodeTemplateState + .setNodeTemplateId(new ToscaConceptIdentifier(entry.getKey(), entry.getValue().getVersion())); + result.put(entry.getKey(), nodeTemplateState); + } + return result; } /** diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/concepts/ParticipantUtilsTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/concepts/ParticipantUtilsTest.java index b3efc2765..6bb7f1eb7 100644 --- a/models/src/test/java/org/onap/policy/clamp/models/acm/concepts/ParticipantUtilsTest.java +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/concepts/ParticipantUtilsTest.java @@ -24,13 +24,11 @@ import static org.assertj.core.api.Assertions.assertThat; import java.util.Map; import org.junit.jupiter.api.Test; +import org.onap.policy.clamp.models.acm.utils.CommonTestData; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; -import org.onap.policy.common.utils.coder.StandardYamlCoder; import org.onap.policy.common.utils.resources.ResourceUtils; -import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; -import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; class ParticipantUtilsTest { @@ -38,12 +36,6 @@ class ParticipantUtilsTest { private static final String TOSCA_TEMPLATE_YAML = "examples/acm/test-pm-subscription-handling.yaml"; private static final String AUTOMATION_COMPOSITION_JSON = "src/test/resources/providers/TestAutomationCompositions.json"; - private static final String AUTOMATION_COMPOSITION_ELEMENT = - "org.onap.policy.clamp.acm.AutomationCompositionElement"; - private static final String POLICY_AUTOMATION_COMPOSITION_ELEMENT = - "org.onap.policy.clamp.acm.PolicyAutomationCompositionElement"; - private static final String PARTICIPANT_AUTOMATION_COMPOSITION_ELEMENT = "org.onap.policy.clamp.acm.Participant"; - private static final StandardYamlCoder YAML_TRANSLATOR = new StandardYamlCoder(); @Test void testFindStartPhase() { @@ -54,30 +46,11 @@ class ParticipantUtilsTest { @Test void testGetFirstStartPhase() throws CoderException { - var serviceTemplate = - YAML_TRANSLATOR.decode(ResourceUtils.getResourceAsStream(TOSCA_TEMPLATE_YAML), ToscaServiceTemplate.class); + var serviceTemplate = CommonTestData.getToscaServiceTemplate(TOSCA_TEMPLATE_YAML); var automationCompositions = CODER.decode(ResourceUtils.getResourceAsString(AUTOMATION_COMPOSITION_JSON), AutomationCompositions.class); var result = ParticipantUtils.getFirstStartPhase(automationCompositions.getAutomationCompositionList().get(0), serviceTemplate); assertThat(result).isZero(); } - - @Test - void testCheckIfNodeTemplateIsAutomationCompositionElement() throws CoderException { - var serviceTemplate = - YAML_TRANSLATOR.decode(ResourceUtils.getResourceAsStream(TOSCA_TEMPLATE_YAML), ToscaServiceTemplate.class); - var nodeTemplate = new ToscaNodeTemplate(); - nodeTemplate.setType(AUTOMATION_COMPOSITION_ELEMENT); - assertThat(ParticipantUtils.checkIfNodeTemplateIsAutomationCompositionElement(nodeTemplate, serviceTemplate)) - .isTrue(); - - nodeTemplate.setType(POLICY_AUTOMATION_COMPOSITION_ELEMENT); - assertThat(ParticipantUtils.checkIfNodeTemplateIsAutomationCompositionElement(nodeTemplate, serviceTemplate)) - .isTrue(); - - nodeTemplate.setType(PARTICIPANT_AUTOMATION_COMPOSITION_ELEMENT); - assertThat(ParticipantUtils.checkIfNodeTemplateIsAutomationCompositionElement(nodeTemplate, serviceTemplate)) - .isFalse(); - } } diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AcDefinitionProviderTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AcDefinitionProviderTest.java index a27a74be4..784f1cfde 100644 --- a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AcDefinitionProviderTest.java +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AcDefinitionProviderTest.java @@ -200,6 +200,7 @@ class AcDefinitionProviderTest { nodeTemplateState.setNodeTemplateStateId(UUID.randomUUID()); nodeTemplateState.setNodeTemplateId(new ToscaConceptIdentifier("name", "1.0.0")); nodeTemplateState.setState(AcTypeState.COMMISSIONED); + nodeTemplateState.setParticipantId(UUID.randomUUID()); acmDefinition.setElementStateMap(Map.of(nodeTemplateState.getNodeTemplateId().getName(), nodeTemplateState)); return acmDefinition; } diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/ParticipantProviderTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/ParticipantProviderTest.java index a40d1cc1f..0c5137824 100644 --- a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/ParticipantProviderTest.java +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/ParticipantProviderTest.java @@ -104,6 +104,9 @@ class ParticipantProviderTest { when(participantRepository.findAll()).thenReturn(jpaParticipantList); assertThat(participantProvider.getParticipants()).hasSize(inputParticipants.size()); + assertThatThrownBy(() -> participantProvider.getParticipantById(inputParticipants.get(0).getParticipantId())) + .hasMessageMatching("Participant Not Found with ID: " + inputParticipants.get(0).getParticipantId()); + when(participantRepository.findById(any())).thenReturn( Optional.ofNullable(jpaParticipantList.get(0))); @@ -128,4 +131,14 @@ class ParticipantProviderTest { var deletedParticipant = participantProvider.deleteParticipant(participantId); assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(deletedParticipant); } + + @Test + void testGetSupportedElementMap() { + var participantRepository = mock(ParticipantRepository.class); + when(participantRepository.findAll()).thenReturn(jpaParticipantList); + var participantProvider = new ParticipantProvider(participantRepository); + + var result = participantProvider.getSupportedElementMap(); + assertThat(result).hasSize(2); + } } diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/utils/AcmUtilsTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/utils/AcmUtilsTest.java index c5acada59..c23c38c5b 100644 --- a/models/src/test/java/org/onap/policy/clamp/models/acm/utils/AcmUtilsTest.java +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/utils/AcmUtilsTest.java @@ -21,11 +21,13 @@ package org.onap.policy.clamp.models.acm.utils; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.fail; import java.io.File; import java.util.ArrayList; @@ -38,7 +40,7 @@ import org.junit.jupiter.api.Test; import org.onap.policy.clamp.models.acm.concepts.AutomationComposition; import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement; import org.onap.policy.clamp.models.acm.concepts.ParticipantUpdates; -import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.clamp.models.acm.document.concepts.DocToscaServiceTemplate; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType; @@ -50,7 +52,10 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; class AcmUtilsTest { - private static final ToscaConceptIdentifier TYPE = new ToscaConceptIdentifier("id", "1.0.0"); + private static final String POLICY_AUTOMATION_COMPOSITION_ELEMENT = + "org.onap.policy.clamp.acm.PolicyAutomationCompositionElement"; + private static final String PARTICIPANT_AUTOMATION_COMPOSITION_ELEMENT = "org.onap.policy.clamp.acm.Participant"; + private static final String TOSCA_TEMPLATE_YAML = "clamp/acm/pmsh/funtional-pmsh-usecase.yaml"; @Test void testCommonUtilsParticipantUpdate() { @@ -71,6 +76,39 @@ class AcmUtilsTest { assertEquals(participantId, participantUpdates.get(1).getParticipantId()); } + @Test + void testCheckIfNodeTemplateIsAutomationCompositionElement() { + var serviceTemplate = CommonTestData.getToscaServiceTemplate(TOSCA_TEMPLATE_YAML); + var nodeTemplate = new ToscaNodeTemplate(); + nodeTemplate.setType(AcmUtils.AUTOMATION_COMPOSITION_ELEMENT); + assertThat(AcmUtils.checkIfNodeTemplateIsAutomationCompositionElement(nodeTemplate, serviceTemplate)).isTrue(); + + nodeTemplate.setType(POLICY_AUTOMATION_COMPOSITION_ELEMENT); + assertThat(AcmUtils.checkIfNodeTemplateIsAutomationCompositionElement(nodeTemplate, serviceTemplate)).isTrue(); + + nodeTemplate.setType(PARTICIPANT_AUTOMATION_COMPOSITION_ELEMENT); + assertThat(AcmUtils.checkIfNodeTemplateIsAutomationCompositionElement(nodeTemplate, serviceTemplate)).isFalse(); + } + + @Test + void testPrepareParticipantPriming() { + var serviceTemplate = CommonTestData.getToscaServiceTemplate(TOSCA_TEMPLATE_YAML); + + var acElements = AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate); + Map map = new HashMap<>(); + var participantId = UUID.randomUUID(); + assertThatThrownBy(() -> AcmUtils.prepareParticipantPriming(acElements, map)).hasMessageMatching( + "Element Type org.onap.policy.clamp.acm.PolicyAutomationCompositionElement 1.0.1 not supported"); + map.put(new ToscaConceptIdentifier("org.onap.policy.clamp.acm.PolicyAutomationCompositionElement", "1.0.1"), + participantId); + map.put(new ToscaConceptIdentifier("org.onap.policy.clamp.acm.K8SMicroserviceAutomationCompositionElement", + "1.0.1"), participantId); + map.put(new ToscaConceptIdentifier("org.onap.policy.clamp.acm.HttpAutomationCompositionElement", "1.0.1"), + participantId); + var result = AcmUtils.prepareParticipantPriming(acElements, map); + assertThat(result).isNotEmpty().hasSize(1); + } + @Test void testCommonUtilsServiceTemplate() { var acElement = new AutomationCompositionElement(); @@ -90,7 +128,7 @@ class AcmUtilsTest { } @Test - void testValidateAutomationComposition() throws Exception { + void testValidateAutomationComposition() { var automationComposition = getDummyAutomationComposition(); var toscaServiceTemplate = getDummyToscaServiceTemplate(); var result = AcmUtils.validateAutomationComposition(automationComposition, toscaServiceTemplate); @@ -102,19 +140,27 @@ class AcmUtilsTest { nodeTemplate.setType("org.onap.policy.clamp.acm.AutomationComposition"); nodeTemplates.put("org.onap.dcae.acm.DCAEMicroserviceAutomationCompositionParticipant", nodeTemplate); toscaServiceTemplate.getToscaTopologyTemplate().setNodeTemplates(nodeTemplates); - var result2 = AcmUtils.validateAutomationComposition(automationComposition, toscaServiceTemplate); - toscaServiceTemplate.setToscaTopologyTemplate(null); - assertFalse(result2.isValid()); + result = AcmUtils.validateAutomationComposition(automationComposition, toscaServiceTemplate); + assertFalse(result.isValid()); + + var doc = new DocToscaServiceTemplate(CommonTestData.getToscaServiceTemplate(TOSCA_TEMPLATE_YAML)); + result = AcmUtils.validateAutomationComposition(automationComposition, doc.toAuthorative()); + assertFalse(result.isValid()); } - private AutomationComposition getDummyAutomationComposition() throws CoderException { + private AutomationComposition getDummyAutomationComposition() { var automationComposition = new AutomationComposition(); - var element = new StandardCoder().decode( - new File("src/test/resources/json/AutomationCompositionElementNoOrderedState.json"), - AutomationCompositionElement.class); automationComposition.setCompositionId(UUID.randomUUID()); Map map = new LinkedHashMap<>(); - map.put(UUID.randomUUID(), element); + try { + var element = new StandardCoder().decode( + new File("src/test/resources/json/AutomationCompositionElementNoOrderedState.json"), + AutomationCompositionElement.class); + map.put(UUID.randomUUID(), element); + } catch (Exception e) { + fail("Cannot read or decode " + e.getMessage()); + return null; + } automationComposition.setElements(map); return automationComposition; } -- cgit 1.2.3-korg