Replace copyTo methods with copy constructors
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / concepts / PdpGroup.java
index 3c1aec2..6d5f804 100644 (file)
 
 package org.onap.policy.models.pdp.concepts;
 
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import java.util.ArrayList;
 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;
@@ -45,6 +47,8 @@ import org.onap.policy.models.pdp.enums.PdpState;
 @Data
 @NoArgsConstructor
 public class PdpGroup implements PfNameVersion, Comparable<PdpGroup> {
+    private static final String SUBGROUP_FIELD = "pdpSubgroups";
+
     private String name;
     private String description;
     private PdpState pdpGroupState;
@@ -66,7 +70,7 @@ public class PdpGroup implements PfNameVersion, Comparable<PdpGroup> {
         this.description = source.description;
         this.pdpGroupState = source.pdpGroupState;
         this.properties = (source.properties == null ? null : new LinkedHashMap<>(source.properties));
-        this.pdpSubgroups = PfUtils.mapList(source.pdpSubgroups, PdpSubGroup::new);
+        this.pdpSubgroups = PfUtils.mapList(source.pdpSubgroups, PdpSubGroup::new, new ArrayList<>(0));
     }
 
     @Override
@@ -83,11 +87,16 @@ 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);
-        result.validateNotNullList("pdpSubgroups", pdpSubgroups, PdpSubGroup::validatePapRest);
+        result.validateNotNullList(SUBGROUP_FIELD, pdpSubgroups, PdpSubGroup::validatePapRest);
+
+        if (pdpSubgroups != null && pdpSubgroups.isEmpty()) {
+            result.addResult(new ObjectValidationResult(SUBGROUP_FIELD, pdpSubgroups, ValidationStatus.INVALID,
+                            "is empty"));
+        }
 
         checkDuplicateSubgroups(result);
 
@@ -100,33 +109,30 @@ 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(SUBGROUP_FIELD, pdpTypes, ValidationStatus.INVALID,
+                        "duplicate subgroups"));
     }
 
     @Override
+    @JsonIgnore
     public String getVersion() {
         // We need to pass a version for keying in the database
         return PfKey.NULL_KEY_VERSION;
     }
 
     @Override
+    @JsonIgnore
     public void setVersion(String version) {
         // Just ignore any version that is set
     }