Handling supported policy type during PdpGroup Update 94/100594/4
authora.sreekumar <ajith.sreekumar@est.tech>
Tue, 21 Jan 2020 17:07:50 +0000 (17:07 +0000)
committera.sreekumar <ajith.sreekumar@est.tech>
Wed, 22 Jan 2020 15:11:27 +0000 (15:11 +0000)
Change-Id: I469125c232af9d78a55c3dfa71cb701cb3864015
Issue-ID: POLICY-2023
Signed-off-by: a.sreekumar <ajith.sreekumar@est.tech>
models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java
models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroups.java
models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpSubGroup.java
models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpGroupTest.java
models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpSubGroupTest.java

index 6d5f804..b6886be 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019-2020 Nordix Foundation.
  *  Modifications Copyright (C) 2019 AT&T Intellectual Property.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -81,9 +81,10 @@ public class PdpGroup implements PfNameVersion, Comparable<PdpGroup> {
     /**
      * Validates that appropriate fields are populated for an incoming call to the PAP REST API.
      *
+     * @param updateGroupFlow if the operation is pdp group update
      * @return the validation result
      */
-    public ValidationResult validatePapRest() {
+    public ValidationResult validatePapRest(boolean updateGroupFlow) {
         BeanValidationResult result = new BeanValidationResult("group", this);
 
         /*
@@ -91,7 +92,8 @@ public class PdpGroup implements PfNameVersion, Comparable<PdpGroup> {
          */
 
         result.validateNotNull("name", name);
-        result.validateNotNullList(SUBGROUP_FIELD, pdpSubgroups, PdpSubGroup::validatePapRest);
+        result.validateNotNullList(SUBGROUP_FIELD, pdpSubgroups,
+            (PdpSubGroup pdpSubGroup) -> pdpSubGroup.validatePapRest(updateGroupFlow));
 
         if (pdpSubgroups != null && pdpSubgroups.isEmpty()) {
             result.addResult(new ObjectValidationResult(SUBGROUP_FIELD, pdpSubgroups, ValidationStatus.INVALID,
index 06194ea..269130b 100644 (file)
@@ -3,6 +3,7 @@
  * ONAP Policy Models
  * ================================================================================
  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -66,20 +67,28 @@ public class PdpGroups {
      * @return the validation result
      */
     public ValidationResult validatePapRest() {
-        BeanValidationResult result = new BeanValidationResult(GROUPS_FIELD, this);
-
-        result.validateNotNullList(GROUPS_FIELD, groups, PdpGroup::validatePapRest);
+        ValidationResult result = new BeanValidationResult(GROUPS_FIELD, this);
+        ((BeanValidationResult) result).validateNotNullList(GROUPS_FIELD, groups,
+            (PdpGroup pdpGroup) -> pdpGroup.validatePapRest(false));
         if (!result.isValid()) {
             return result;
         }
 
         // verify that the same group doesn't appear more than once
+        return checkForDuplicateGroups(result);
+    }
+
+    /**
+     * Validates that there are no duplicate PdpGroups with the same name.
+     *
+     * @param result the validation result
+     * @return the validation result
+     */
+    public ValidationResult checkForDuplicateGroups(ValidationResult result) {
         List<String> names = groups.stream().map(PdpGroup::getName).collect(Collectors.toList());
-        if (groups.size() == new HashSet<>(names).size()) {
-            return result;
+        if (groups.size() != new HashSet<>(names).size()) {
+            result = new ObjectValidationResult(GROUPS_FIELD, names, ValidationStatus.INVALID, "duplicate group names");
         }
-
-        // different sizes implies duplicates
-        return new ObjectValidationResult(GROUPS_FIELD, names, ValidationStatus.INVALID, "duplicate group names");
+        return result;
     }
 }
index 236cc85..e3e33b4 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019-2020 Nordix Foundation.
  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -78,19 +78,23 @@ public class PdpSubGroup {
      * Validates that appropriate fields are populated for an incoming call to the PAP
      * REST API.
      *
+     * @param updateGroupFlow if the operation is pdp group update
      * @return the validation result
      */
-    public ValidationResult validatePapRest() {
+    public ValidationResult validatePapRest(boolean updateGroupFlow) {
         BeanValidationResult result = new BeanValidationResult("group", this);
 
         result.validateNotNull("pdpType", pdpType);
-        result.validateNotNullList("supportedPolicyTypes", supportedPolicyTypes,
-                        ToscaPolicyTypeIdentifier::validatePapRest);
-        result.validateNotNullList("policies", policies, ToscaPolicyIdentifier::validatePapRest);
+        // When doing PdpGroup Update operation, supported policy types and policies doesn't have to be validated.
+        if (!updateGroupFlow) {
+            result.validateNotNullList("policies", policies, ToscaPolicyIdentifier::validatePapRest);
+            result.validateNotNullList("supportedPolicyTypes", supportedPolicyTypes,
+                ToscaPolicyTypeIdentifier::validatePapRest);
 
-        if (supportedPolicyTypes != null && supportedPolicyTypes.isEmpty()) {
-            result.addResult(new ObjectValidationResult("supportedPolicyTypes", supportedPolicyTypes,
-                            ValidationStatus.INVALID, "empty list"));
+            if (supportedPolicyTypes != null && supportedPolicyTypes.isEmpty()) {
+                result.addResult(new ObjectValidationResult("supportedPolicyTypes", supportedPolicyTypes,
+                    ValidationStatus.INVALID, "empty list"));
+            }
         }
 
         if (desiredInstanceCount <= 0) {
index a282b7d..4f11079 100644 (file)
@@ -3,7 +3,7 @@
  * ONAP Policy Models
  * ================================================================================
  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -120,6 +120,42 @@ public class PdpGroupTest {
         assertEquals(1, mapList.get(0).size());
     }
 
+    @Test
+    public void testValidatePapRest_GroupUpdateFlow() {
+        PdpGroup group = new PdpGroup();
+        group.setName(NAME);
+        // with supported policy type and policies
+        PdpSubGroup subgroup1 = new PdpSubGroup();
+        subgroup1.setDesiredInstanceCount(1);
+        subgroup1.setPdpType(PDP_TYPE1);
+        subgroup1.setSupportedPolicyTypes(Arrays.asList(new ToscaPolicyTypeIdentifier("a-type-name", "3.2.1")));
+        subgroup1.setPolicies(Collections.emptyList());
+        group.setPdpSubgroups(Arrays.asList(subgroup1));
+
+        ValidationResult result = group.validatePapRest(true);
+        assertNotNull(result);
+        assertTrue(result.isValid());
+        assertNull(result.getResult());
+
+        // without supported policy type and policies
+        PdpSubGroup subgroup2 = new PdpSubGroup();
+        subgroup2.setDesiredInstanceCount(1);
+        subgroup2.setPdpType(PDP_TYPE1);
+        group.setPdpSubgroups(Arrays.asList(subgroup2));
+
+        // valid
+        result = group.validatePapRest(true);
+        assertNotNull(result);
+        assertTrue(result.isValid());
+        assertNull(result.getResult());
+
+        // invalid
+        result = group.validatePapRest(false);
+        assertNotNull(result);
+        assertFalse(result.isValid());
+        assertNotNull(result.getResult());
+    }
+
     @Test
     public void testValidatePapRest() {
         PdpGroup group = new PdpGroup();
@@ -140,7 +176,7 @@ public class PdpGroupTest {
         group.setPdpSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup3));
 
         // valid
-        ValidationResult result = group.validatePapRest();
+        ValidationResult result = group.validatePapRest(false);
         assertNotNull(result);
         assertTrue(result.isValid());
         assertNull(result.getResult());
@@ -179,7 +215,7 @@ public class PdpGroupTest {
     }
 
     private void assertInvalid(PdpGroup group) {
-        ValidationResult result = group.validatePapRest();
+        ValidationResult result = group.validatePapRest(false);
         assertNotNull(result);
         assertFalse(result.isValid());
         assertNotNull(result.getResult());
index e24e8e2..ba55426 100644 (file)
@@ -3,7 +3,7 @@
  * ONAP Policy Models
  * ================================================================================
  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -90,6 +90,39 @@ public class PdpSubGroupTest {
         assertEquals(orig.toString(), new PdpSubGroup(orig).toString());
     }
 
+    @Test
+    public void testValidatePapRest_GroupUpdateFlow() throws Exception {
+        PdpSubGroup subgrp = new PdpSubGroup();
+        // with supported policy type and policies
+        subgrp.setDesiredInstanceCount(1);
+        subgrp.setPdpType("pdp-type");
+        subgrp.setSupportedPolicyTypes(
+                        Arrays.asList(makeIdent("type-X", VERSION_300, ToscaPolicyTypeIdentifier.class)));
+        subgrp.setPolicies(Arrays.asList(makeIdent("policy-X", "4.0.0", ToscaPolicyIdentifier.class)));
+
+        ValidationResult result = subgrp.validatePapRest(false);
+        assertNotNull(result);
+        assertTrue(result.isValid());
+        assertNull(result.getResult());
+
+        // without supported policy type and policies
+        PdpSubGroup subgrp2 = new PdpSubGroup();
+        subgrp2.setDesiredInstanceCount(1);
+        subgrp2.setPdpType("pdp-type");
+
+        // valid
+        result = subgrp2.validatePapRest(true);
+        assertNotNull(result);
+        assertTrue(result.isValid());
+        assertNull(result.getResult());
+
+        // invalid
+        result = subgrp2.validatePapRest(false);
+        assertNotNull(result);
+        assertFalse(result.isValid());
+        assertNotNull(result.getResult());
+    }
+
     @Test
     public void testValidatePapRest() throws Exception {
         PdpSubGroup subgrp = new PdpSubGroup();
@@ -101,7 +134,7 @@ public class PdpSubGroupTest {
         subgrp.setPolicies(Arrays.asList(makeIdent("policy-X", "4.0.0", ToscaPolicyIdentifier.class)));
 
         // valid
-        ValidationResult result = subgrp.validatePapRest();
+        ValidationResult result = subgrp.validatePapRest(false);
         assertNotNull(result);
         assertTrue(result.isValid());
         assertNull(result.getResult());
@@ -158,7 +191,7 @@ public class PdpSubGroupTest {
     }
 
     private void assertInvalid(PdpSubGroup sub2) {
-        ValidationResult result = sub2.validatePapRest();
+        ValidationResult result = sub2.validatePapRest(false);
         assertNotNull(result);
         assertFalse(result.isValid());
         assertNotNull(result.getResult());