From c38fcf1a16f0d6bfdf2d80efa64a94ae68d80f03 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Fri, 12 Apr 2019 15:53:08 -0400 Subject: Check for duplicates in lists Modified duplicate subgroup check to eliminate sonar issue. Added duplicate group check. Modified ModelsTest to exclude PdpMessage, which seemed to cause intermittent junit failures. Change-Id: Id281874506d3a39610739e24cee49360b345724f Issue-ID: POLICY-1542 Signed-off-by: Jim Hahn --- .../onap/policy/models/pdp/concepts/PdpGroup.java | 27 +++++++++------------- .../onap/policy/models/pdp/concepts/PdpGroups.java | 16 ++++++++++++- 2 files changed, 26 insertions(+), 17 deletions(-) (limited to 'models-pdp/src/main/java') diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java index 3c1aec258..49dfd6fbb 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java @@ -25,7 +25,7 @@ import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.Set; +import java.util.stream.Collectors; import lombok.Data; import lombok.NoArgsConstructor; import org.onap.policy.common.parameters.BeanValidationResult; @@ -83,7 +83,7 @@ public class PdpGroup implements PfNameVersion, Comparable { BeanValidationResult result = new BeanValidationResult("group", this); /* - * Don't care about version or state, because we override them. Ok if description is null. + * Don't care about state, because we override it. Ok if description is null. */ result.validateNotNull("name", name); @@ -100,24 +100,19 @@ public class PdpGroup implements PfNameVersion, Comparable { * @param result where to place validation results */ private void checkDuplicateSubgroups(BeanValidationResult result) { - if (pdpSubgroups == null) { + if (pdpSubgroups == null || !result.isValid()) { return; } - Set set = new HashSet<>(); - - for (PdpSubGroup subgrp : pdpSubgroups) { - String pdpType = (subgrp == null ? null : subgrp.getPdpType()); - - if (pdpType == null) { - continue; - } - - if (!set.add(pdpType)) { - result.addResult(new ObjectValidationResult("subgroups", pdpType, ValidationStatus.INVALID, - "duplicate subgroup")); - } + // verify that the same subgroup doesn't appear more than once + List pdpTypes = pdpSubgroups.stream().map(PdpSubGroup::getPdpType).collect(Collectors.toList()); + if (pdpSubgroups.size() == new HashSet<>(pdpTypes).size()) { + return; } + + // different sizes implies duplicates + result.addResult(new ObjectValidationResult("pdpSubgroups", pdpTypes, ValidationStatus.INVALID, + "duplicate subgroups")); } @Override diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroups.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroups.java index a7e05dd6b..ab9afb00b 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroups.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroups.java @@ -21,14 +21,18 @@ package org.onap.policy.models.pdp.concepts; import java.util.Collections; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import lombok.Getter; import lombok.Setter; import lombok.ToString; 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; /** * Request deploy or update a set of groups via the PDP Group deployment REST API. @@ -63,7 +67,17 @@ public class PdpGroups { BeanValidationResult result = new BeanValidationResult("groups", this); result.validateNotNullList("groups", groups, PdpGroup::validatePapRest); + if (!result.isValid()) { + return result; + } + + // verify that the same group doesn't appear more than once + List names = groups.stream().map(PdpGroup::getName).collect(Collectors.toList()); + if (groups.size() == new HashSet<>(names).size()) { + return result; + } - return result; + // different sizes implies duplicates + return new ObjectValidationResult("groups", names, ValidationStatus.INVALID, "duplicate group names"); } } -- cgit 1.2.3-korg