From 11ff808f95a2d9a0e43ccf880195ee63b9de0460 Mon Sep 17 00:00:00 2001 From: FrancescoFioraEst Date: Thu, 15 Dec 2022 16:19:44 +0000 Subject: Fix copy contructor in DocToscaServiceTemplate in ACM Issue-ID: POLICY-4486 Change-Id: Ic13daae3c9c8ebdf72a30dcdac59b3a944f77893 Signed-off-by: FrancescoFioraEst --- .../clamp/models/acm/document/base/DocUtil.java | 67 ++++++++++++++++++++++ .../document/concepts/DocToscaNodeTemplate.java | 8 +-- .../acm/document/concepts/DocToscaNodeType.java | 2 +- .../acm/document/concepts/DocToscaRequirement.java | 6 +- .../document/concepts/DocToscaServiceTemplate.java | 22 +++---- .../concepts/DocToscaTopologyTemplate.java | 13 +++-- .../provider/AcDefinitionProviderTest.java | 3 +- 7 files changed, 94 insertions(+), 27 deletions(-) (limited to 'models') diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/document/base/DocUtil.java b/models/src/main/java/org/onap/policy/clamp/models/acm/document/base/DocUtil.java index 5e31ad239..5c4817871 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/document/base/DocUtil.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/document/base/DocUtil.java @@ -32,6 +32,9 @@ import java.util.function.Function; 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.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.onap.policy.clamp.models.acm.document.concepts.DocToscaEntity; import org.onap.policy.clamp.models.acm.document.concepts.DocToscaServiceTemplate; @@ -317,4 +320,68 @@ public final class DocUtil { } return result; } + + /** + * Compare two maps of the same type, nulls are allowed. + * + * @param leftMap the first map + * @param rightMap the second map + * @return a measure of the comparison + */ + public static > int compareMaps(final Map leftMap, + final Map rightMap) { + if ((MapUtils.isEmpty(leftMap) && MapUtils.isEmpty(rightMap))) { + return 0; + } + if (leftMap == null) { + return 1; + } + + if (rightMap == null) { + return -1; + } + if (leftMap.size() != rightMap.size()) { + return leftMap.hashCode() - rightMap.hashCode(); + } + + for (var leftEntry : leftMap.entrySet()) { + var result = ObjectUtils.compare(leftEntry.getValue(), rightMap.get(leftEntry.getKey())); + if (result != 0) { + return result; + } + } + return 0; + } + + /** + * Compare two lists of Map of the same type, nulls are allowed. + * + * @param leftCollection the first list + * @param rightCollection the second list + * @return a measure of the comparison + */ + public static > int compareCollections(final List> leftCollection, + final List> rightCollection) { + if ((CollectionUtils.isEmpty(leftCollection) && CollectionUtils.isEmpty(rightCollection))) { + return 0; + } + if (leftCollection == null) { + return 1; + } + + if (rightCollection == null) { + return -1; + } + if (leftCollection.size() != rightCollection.size()) { + return leftCollection.hashCode() - rightCollection.hashCode(); + } + + for (var i = 0; i < leftCollection.size(); i++) { + var result = compareMaps(leftCollection.get(i), rightCollection.get(i)); + if (result != 0) { + return result; + } + } + return 0; + } } diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/document/concepts/DocToscaNodeTemplate.java b/models/src/main/java/org/onap/policy/clamp/models/acm/document/concepts/DocToscaNodeTemplate.java index acd42baa0..ce67d5167 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/document/concepts/DocToscaNodeTemplate.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/document/concepts/DocToscaNodeTemplate.java @@ -60,8 +60,8 @@ public class DocToscaNodeTemplate extends DocToscaWithTypeAndStringProperties DocUtil.docMapToMap(map, DocToscaRequirement::new)); - this.capabilities = DocUtil.docMapToMap(copyConcept.capabilities, DocToscaCapabilityAssignment::new); + PfUtils.mapList(copyConcept.requirements, map -> PfUtils.mapMap(map, DocToscaRequirement::new)); + this.capabilities = PfUtils.mapMap(copyConcept.capabilities, DocToscaCapabilityAssignment::new); } @Override @@ -102,11 +102,11 @@ public class DocToscaNodeTemplate extends DocToscaWithTypeAndStringProperties public DocToscaNodeType(final DocToscaNodeType copyConcept) { super(copyConcept); this.requirements = - PfUtils.mapList(copyConcept.requirements, map -> DocUtil.docMapToMap(map, DocToscaRequirement::new)); + PfUtils.mapList(copyConcept.requirements, map -> PfUtils.mapMap(map, DocToscaRequirement::new)); } @Override diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/document/concepts/DocToscaRequirement.java b/models/src/main/java/org/onap/policy/clamp/models/acm/document/concepts/DocToscaRequirement.java index 5bef710d2..a1e821d56 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/document/concepts/DocToscaRequirement.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/document/concepts/DocToscaRequirement.java @@ -102,17 +102,17 @@ public class DocToscaRequirement extends DocToscaWithTypeAndStringProperties()); + this.dataTypes = PfUtils.mapMap(copyConcept.dataTypes, DocToscaDataType::new, new LinkedHashMap<>()); this.capabilityTypes = - DocUtil.docMapToMap(copyConcept.capabilityTypes, DocToscaCapabilityType::new, new LinkedHashMap<>()); - this.nodeTypes = DocUtil.docMapToMap(copyConcept.nodeTypes, DocToscaNodeType::new, new LinkedHashMap<>()); - this.relationshipTypes = DocUtil.docMapToMap(copyConcept.relationshipTypes, DocToscaRelationshipType::new, + PfUtils.mapMap(copyConcept.capabilityTypes, DocToscaCapabilityType::new, new LinkedHashMap<>()); + this.nodeTypes = PfUtils.mapMap(copyConcept.nodeTypes, DocToscaNodeType::new, new LinkedHashMap<>()); + this.relationshipTypes = PfUtils.mapMap(copyConcept.relationshipTypes, DocToscaRelationshipType::new, new LinkedHashMap<>()); - this.policyTypes = DocUtil.docMapToMap(copyConcept.policyTypes, DocToscaPolicyType::new, new LinkedHashMap<>()); + this.policyTypes = PfUtils.mapMap(copyConcept.policyTypes, DocToscaPolicyType::new, new LinkedHashMap<>()); if (copyConcept.toscaTopologyTemplate != null) { this.toscaTopologyTemplate = new DocToscaTopologyTemplate(copyConcept.toscaTopologyTemplate); } @@ -158,29 +158,29 @@ public class DocToscaServiceTemplate extends DocToscaEntity()); - this.policies = DocUtil.docMapToMap(copyConcept.policies, DocToscaPolicy::new, new LinkedHashMap<>()); + PfUtils.mapMap(copyConcept.nodeTemplates, DocToscaNodeTemplate::new, new LinkedHashMap<>()); + this.policies = PfUtils.mapMap(copyConcept.policies, DocToscaPolicy::new, new LinkedHashMap<>()); } @Override @@ -102,7 +102,8 @@ public class DocToscaTopologyTemplate extends Validated nodeTemplates = DocUtil.mapToDocMap(toscaTopologyTemplate.getNodeTemplates(), DocToscaNodeTemplate::new); - policies = DocUtil.listToDocMap(toscaTopologyTemplate.getPolicies(), DocToscaPolicy::new); + policies = + DocUtil.listToDocMap(toscaTopologyTemplate.getPolicies(), DocToscaPolicy::new, new LinkedHashMap<>()); } @Override @@ -112,16 +113,16 @@ public class DocToscaTopologyTemplate extends Validated return result; } - result = PfUtils.compareMaps(inputs, otherConcept.inputs); + result = DocUtil.compareMaps(inputs, otherConcept.inputs); if (result != 0) { return result; } - result = PfUtils.compareMaps(nodeTemplates, otherConcept.nodeTemplates); + result = DocUtil.compareMaps(nodeTemplates, otherConcept.nodeTemplates); if (result != 0) { return result; } - return PfUtils.compareMaps(policies, otherConcept.policies); + return DocUtil.compareMaps(policies, otherConcept.policies); } /** 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 8184ef1a4..59e3767d1 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 @@ -22,7 +22,6 @@ 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.assertNotEquals; import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; @@ -66,7 +65,7 @@ class AcDefinitionProviderTest { var docServiceTemplate = new DocToscaServiceTemplate(inputServiceTemplateProperties); var docServiceTemplateCopy = new DocToscaServiceTemplate(docServiceTemplate); - assertNotEquals(0, docServiceTemplate.compareTo(docServiceTemplateCopy)); + assertThat(docServiceTemplate.compareTo(docServiceTemplateCopy)).isEqualByComparingTo(0); assertThat(docServiceTemplate.compareToWithoutEntities(docServiceTemplateCopy)).isZero(); var acmDefinition = getAcDefinition(docServiceTemplate); -- cgit 1.2.3-korg