diff options
author | FrancescoFioraEst <francesco.fiora@est.tech> | 2022-12-15 16:19:44 +0000 |
---|---|---|
committer | Liam Fallon <liam.fallon@est.tech> | 2022-12-16 13:36:34 +0000 |
commit | 11ff808f95a2d9a0e43ccf880195ee63b9de0460 (patch) | |
tree | 9429fa8053b7cf0d1a905d82c2935694fa6d9143 | |
parent | 0b5e99601abc5290d241f2082f12758ea46231ef (diff) |
Fix copy contructor in DocToscaServiceTemplate in ACM
Issue-ID: POLICY-4486
Change-Id: Ic13daae3c9c8ebdf72a30dcdac59b3a944f77893
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
7 files changed, 94 insertions, 27 deletions
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 <V extends Comparable<? super V>> int compareMaps(final Map<String, V> leftMap, + final Map<String, V> 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 <V extends Comparable<? super V>> int compareCollections(final List<Map<String, V>> leftCollection, + final List<Map<String, V>> 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<To public DocToscaNodeTemplate(final DocToscaNodeTemplate copyConcept) { super(copyConcept); this.requirements = - PfUtils.mapList(copyConcept.requirements, map -> 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<To final var other = (DocToscaNodeTemplate) otherConcept; - result = PfUtils.compareCollections(requirements, other.requirements); + result = DocUtil.compareCollections(requirements, other.requirements); if (result != 0) { return result; } - return PfUtils.compareMaps(capabilities, other.capabilities); + return DocUtil.compareMaps(capabilities, other.capabilities); } } diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/document/concepts/DocToscaNodeType.java b/models/src/main/java/org/onap/policy/clamp/models/acm/document/concepts/DocToscaNodeType.java index 9f79b20f6..0193a7bff 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/document/concepts/DocToscaNodeType.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/document/concepts/DocToscaNodeType.java @@ -51,7 +51,7 @@ public class DocToscaNodeType extends DocToscaWithToscaProperties<ToscaNodeType> 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<Tos final var other = (DocToscaRequirement) otherConcept; - result = capability.compareTo(other.capability); + result = PfUtils.compareObjects(capability, other.capability); if (result != 0) { return result; } - result = node.compareTo(other.node); + result = PfUtils.compareObjects(node, other.node); if (result != 0) { return result; } - result = relationship.compareTo(other.relationship); + result = PfUtils.compareObjects(relationship, other.relationship); if (result != 0) { return result; } diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/document/concepts/DocToscaServiceTemplate.java b/models/src/main/java/org/onap/policy/clamp/models/acm/document/concepts/DocToscaServiceTemplate.java index bd47452e4..3c0de41d6 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/document/concepts/DocToscaServiceTemplate.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/document/concepts/DocToscaServiceTemplate.java @@ -90,13 +90,13 @@ public class DocToscaServiceTemplate extends DocToscaEntity<ToscaServiceTemplate public DocToscaServiceTemplate(final DocToscaServiceTemplate copyConcept) { super(copyConcept); this.toscaDefinitionsVersion = copyConcept.toscaDefinitionsVersion; - this.dataTypes = DocUtil.docMapToMap(copyConcept.dataTypes, DocToscaDataType::new, new LinkedHashMap<>()); + 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<ToscaServiceTemplate return result; } - final DocToscaServiceTemplate other = (DocToscaServiceTemplate) otherConcept; + final var other = (DocToscaServiceTemplate) otherConcept; - result = PfUtils.compareMaps(dataTypes, other.dataTypes); + result = DocUtil.compareMaps(dataTypes, other.dataTypes); if (result != 0) { return result; } - result = PfUtils.compareMaps(capabilityTypes, other.capabilityTypes); + result = DocUtil.compareMaps(capabilityTypes, other.capabilityTypes); if (result != 0) { return result; } - result = PfUtils.compareMaps(relationshipTypes, other.relationshipTypes); + result = DocUtil.compareMaps(relationshipTypes, other.relationshipTypes); if (result != 0) { return result; } - result = PfUtils.compareMaps(nodeTypes, other.nodeTypes); + result = DocUtil.compareMaps(nodeTypes, other.nodeTypes); if (result != 0) { return result; } - result = PfUtils.compareMaps(policyTypes, other.policyTypes); + result = DocUtil.compareMaps(policyTypes, other.policyTypes); if (result != 0) { return result; } diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/document/concepts/DocToscaTopologyTemplate.java b/models/src/main/java/org/onap/policy/clamp/models/acm/document/concepts/DocToscaTopologyTemplate.java index 79a68e244..1e0a88594 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/document/concepts/DocToscaTopologyTemplate.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/document/concepts/DocToscaTopologyTemplate.java @@ -70,8 +70,8 @@ public class DocToscaTopologyTemplate extends Validated this.description = copyConcept.description; this.inputs = PfUtils.mapMap(copyConcept.inputs, DocToscaParameter::new); this.nodeTemplates = - DocUtil.docMapToMap(copyConcept.nodeTemplates, DocToscaNodeTemplate::new, new LinkedHashMap<>()); - 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); |