diff options
author | Jim Hahn <jrh3@att.com> | 2019-04-12 15:53:08 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-04-12 21:42:16 -0400 |
commit | c38fcf1a16f0d6bfdf2d80efa64a94ae68d80f03 (patch) | |
tree | 5a4d88b12586e8d70b1055d40786d6dd0e0e82d5 | |
parent | b2ae3e6a44a3aa70ac1cd1d2ef78316bfc6e9d04 (diff) |
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 <jrh3@att.com>
4 files changed, 36 insertions, 18 deletions
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<PdpGroup> { 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<PdpGroup> { * @param result where to place validation results */ private void checkDuplicateSubgroups(BeanValidationResult result) { - if (pdpSubgroups == null) { + if (pdpSubgroups == null || !result.isValid()) { return; } - Set<String> 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<String> 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<String> 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"); } } diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/ModelsTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/ModelsTest.java index 7a6c03c71..743839362 100644 --- a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/ModelsTest.java +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/ModelsTest.java @@ -42,6 +42,10 @@ public class ModelsTest { public void testPdpModels() { final Validator validator = ValidatorBuilder.create().with(new ToStringTester()).with(new SetterTester()) .with(new GetterTester()).build(); - validator.validate(POJO_PACKAGE, new FilterPackageInfo(), new FilterClassName(PdpMessage.class.getName())); + + // exclude Test classes and PdpMessage + validator.validate(POJO_PACKAGE, new FilterPackageInfo(), + new FilterClassName("^((?!Test$).)*$"), + new FilterClassName("^((?!" + PdpMessage.class.getName() + ").)*$")); } } diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpGroupsTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpGroupsTest.java index 22e178da2..e0fafc18d 100644 --- a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpGroupsTest.java +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpGroupsTest.java @@ -82,6 +82,11 @@ public class PdpGroupsTest { groupX.setName(null); groups.setGroups(Arrays.asList(group1, groupX)); assertInvalid(groups); + + // duplicate groups + groups = new PdpGroups(); + groups.setGroups(Arrays.asList(group1, group2, group1)); + assertInvalid(groups); } private void assertInvalid(PdpGroups groups) { |