Add copy constructors for models-pap 79/82979/2
authorJim Hahn <jrh3@att.com>
Thu, 21 Mar 2019 16:36:35 +0000 (12:36 -0400)
committerJim Hahn <jrh3@att.com>
Thu, 21 Mar 2019 21:11:31 +0000 (17:11 -0400)
Also added a method to PfUtils to simplify cloning lists.

Change-Id: Iae667be02cced57b0b9578e0a96c5cda38111b97
Issue-ID: POLICY-1542
Signed-off-by: Jim Hahn <jrh3@att.com>
models-base/src/main/java/org/onap/policy/models/base/PfUtils.java
models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java
models-pap/pom.xml
models-pap/src/main/java/org/onap/policy/models/pap/concepts/PdpGroup.java
models-pap/src/main/java/org/onap/policy/models/pap/concepts/PdpInstanceDetails.java
models-pap/src/main/java/org/onap/policy/models/pap/concepts/PdpSubGroup.java
models-pap/src/main/java/org/onap/policy/models/pap/concepts/Policy.java
models-pap/src/test/java/org/onap/policy/models/pap/concepts/TestPdpGroup.java [new file with mode: 0644]
models-pap/src/test/java/org/onap/policy/models/pap/concepts/TestPdpInstanceDetails.java [new file with mode: 0644]
models-pap/src/test/java/org/onap/policy/models/pap/concepts/TestPdpSubGroup.java [new file with mode: 0644]
models-pap/src/test/java/org/onap/policy/models/pap/concepts/TestPolicy.java [new file with mode: 0644]

index a18315c..8e77d3f 100644 (file)
@@ -1,6 +1,7 @@
-/*-
+/*
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.models.base;
 
+import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
 /**
  * Utility class for Policy Framework concept utilities.
  *
@@ -56,4 +61,20 @@ public final class PfUtils {
 
         return 0;
     }
+
+    /**
+     * Convenience method to apply a mapping function to all of the elements of a list,
+     * generating a new list.
+     *
+     * @param source list whose elements are to be mapped, or {@code null}
+     * @param mapFunc mapping function
+     * @return a new list, containing mappings of all of the items in the original list
+     */
+    public static <T> List<T> mapList(List<T> source, Function<T, T> mapFunc) {
+        if (source == null) {
+            return null;
+        }
+
+        return source.stream().map(mapFunc).collect(Collectors.toList());
+    }
 }
index 2b495a1..bdbab5c 100644 (file)
@@ -1,6 +1,7 @@
-/*-
+/*
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,7 +23,10 @@ package org.onap.policy.models.base;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
 
+import java.util.Arrays;
+import java.util.List;
 import org.junit.Test;
 
 /**
@@ -33,11 +37,23 @@ import org.junit.Test;
 public class PfUtilsTest {
 
     @Test
-    public void testPfUtils() {
+    public void testCompareObjects() {
         assertEquals(0, PfUtils.compareObjects(null, null));
         assertEquals(-1, PfUtils.compareObjects("hello", null));
         assertEquals(1, PfUtils.compareObjects(null, "hello"));
         assertFalse(PfUtils.compareObjects("hello", "goodbye") == 0);
         assertEquals(0, PfUtils.compareObjects("hello", "hello"));
     }
+
+    @Test
+    public void testMapList() {
+        assertNull(PfUtils.mapList(null, item -> {
+            throw new RuntimeException("should not be invoked");
+        }));
+
+        List<String> origList = Arrays.asList("abc", "def");
+        List<String> newList = PfUtils.mapList(origList, text -> text + "X");
+
+        assertEquals(Arrays.asList("abcX", "defX"), newList);
+    }
 }
index 1cc5603..a437d54 100644 (file)
@@ -1,6 +1,7 @@
 <!--
   ============LICENSE_START=======================================================
    Copyright (C) 2019 Nordix Foundation.
+   Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
   ================================================================================
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
             <artifactId>pdp-common</artifactId>
             <version>${policy.common.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.onap.policy.models</groupId>
+            <artifactId>policy-models-base</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.assertj</groupId>
+            <artifactId>assertj-core</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
\ No newline at end of file
index 148168d..a42ac21 100644 (file)
@@ -1,6 +1,7 @@
-/*-
+/*
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.models.pap.concepts;
 
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
-
 import lombok.Getter;
+import lombok.NonNull;
 import lombok.Setter;
 import lombok.ToString;
-
+import org.onap.policy.models.base.PfUtils;
 import org.onap.policy.pdp.common.enums.PdpState;
 
 /**
@@ -41,9 +43,29 @@ public class PdpGroup {
 
     private String name;
     private String version;
-    private PdpState pdpGroupstate;
+    private PdpState pdpGroupState;
     private String description;
     private Map<String, String> properties;
     private List<PdpSubGroup> pdpSubgroups;
 
+    /**
+     * Constructs the object.
+     */
+    public PdpGroup() {
+        super();
+    }
+
+    /**
+     * Constructs the object, making a deep copy from the source.
+     *
+     * @param source source from which to copy fields
+     */
+    public PdpGroup(@NonNull PdpGroup source) {
+        this.name = source.name;
+        this.version = source.version;
+        this.pdpGroupState = source.pdpGroupState;
+        this.description = source.description;
+        this.properties = (source.properties == null ? null : new LinkedHashMap<>(source.properties));
+        this.pdpSubgroups = PfUtils.mapList(source.pdpSubgroups, PdpSubGroup::new);
+    }
 }
index 168c20e..4f9041c 100644 (file)
@@ -1,6 +1,7 @@
-/*-
+/*
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,9 +22,9 @@
 package org.onap.policy.models.pap.concepts;
 
 import lombok.Getter;
+import lombok.NonNull;
 import lombok.Setter;
 import lombok.ToString;
-
 import org.onap.policy.pdp.common.enums.PdpHealthStatus;
 import org.onap.policy.pdp.common.enums.PdpState;
 
@@ -41,4 +42,23 @@ public class PdpInstanceDetails {
     private PdpState pdpState;
     private PdpHealthStatus healthy;
     private String message;
+
+    /**
+     * Constructs the object.
+     */
+    public PdpInstanceDetails() {
+        super();
+    }
+
+    /**
+     * Constructs the object, making a deep copy from the source.
+     *
+     * @param source source from which to copy fields
+     */
+    public PdpInstanceDetails(@NonNull PdpInstanceDetails source) {
+        this.instanceId = source.instanceId;
+        this.pdpState = source.pdpState;
+        this.healthy = source.healthy;
+        this.message = source.message;
+    }
 }
index ebb476d..e535233 100644 (file)
@@ -1,6 +1,7 @@
-/*-
+/*
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.models.pap.concepts;
 
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
-
 import lombok.Getter;
+import lombok.NonNull;
 import lombok.Setter;
 import lombok.ToString;
+import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.base.PfUtils;
 
 /**
- * Class to represent a group of all PDP's of the same pdp type running for a particular domain.
+ * Class to represent a group of all PDP's of the same pdp type running for a particular
+ * domain.
  *
  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
  */
@@ -38,11 +43,32 @@ import lombok.ToString;
 public class PdpSubGroup {
 
     private String pdpType;
-    private List<String> supportedPolicyTypes;
+    private List<PfConceptKey> supportedPolicyTypes;
     private List<Policy> policies;
     private int currentInstanceCount;
     private int desiredInstanceCount;
     private Map<String, String> properties;
     private List<PdpInstanceDetails> pdpInstances;
 
+    /**
+     * Constructs the object.
+     */
+    public PdpSubGroup() {
+        super();
+    }
+
+    /**
+     * Constructs the object, making a deep copy from the source.
+     *
+     * @param source source from which to copy fields
+     */
+    public PdpSubGroup(@NonNull PdpSubGroup source) {
+        this.pdpType = source.pdpType;
+        this.supportedPolicyTypes = PfUtils.mapList(source.supportedPolicyTypes, PfConceptKey::new);
+        this.policies = PfUtils.mapList(source.policies, Policy::new);
+        this.currentInstanceCount = source.currentInstanceCount;
+        this.desiredInstanceCount = source.desiredInstanceCount;
+        this.properties = (source.properties == null ? null : new LinkedHashMap<>(source.properties));
+        this.pdpInstances = PfUtils.mapList(source.pdpInstances, PdpInstanceDetails::new);
+    }
 }
index e1281fc..b83510e 100644 (file)
@@ -1,6 +1,7 @@
-/*-
+/*
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,6 +22,7 @@
 package org.onap.policy.models.pap.concepts;
 
 import lombok.Getter;
+import lombok.NonNull;
 import lombok.Setter;
 import lombok.ToString;
 
@@ -39,4 +41,24 @@ public class Policy {
     private String policyType;
     private String policyTypeVersion;
     private String policyTypeImpl;
+
+    /**
+     * Constructs the object.
+     */
+    public Policy() {
+        super();
+    }
+
+    /**
+     * Constructs the object, making a deep copy from the source.
+     *
+     * @param source source from which to copy fields
+     */
+    public Policy(@NonNull Policy source) {
+        this.name = source.name;
+        this.policyVersion = source.policyVersion;
+        this.policyType = source.policyType;
+        this.policyTypeVersion = source.policyTypeVersion;
+        this.policyTypeImpl = source.policyTypeImpl;
+    }
 }
diff --git a/models-pap/src/test/java/org/onap/policy/models/pap/concepts/TestPdpGroup.java b/models-pap/src/test/java/org/onap/policy/models/pap/concepts/TestPdpGroup.java
new file mode 100644 (file)
index 0000000..3dc3c24
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Models
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.pap.concepts;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
+
+import java.util.Arrays;
+import java.util.Map;
+import java.util.TreeMap;
+import org.junit.Test;
+import org.onap.policy.pdp.common.enums.PdpState;
+
+/**
+ * Test the copy constructor, as {@link TestModels} tests the other methods.
+ */
+public class TestPdpGroup {
+
+    @Test
+    public void testCopyConstructor() {
+        assertThatThrownBy(() -> new PdpGroup(null)).isInstanceOf(NullPointerException.class);
+
+        PdpGroup orig = new PdpGroup();
+
+        // verify with null values
+        assertEquals(orig.toString(), new PdpGroup(orig).toString());
+
+        // verify with all values
+        orig.setDescription("my-descript");
+        orig.setName("my-name");
+        orig.setPdpGroupState(PdpState.SAFE);
+
+        PdpSubGroup sub1 = new PdpSubGroup();
+        sub1.setCurrentInstanceCount(10);
+        PdpSubGroup sub2 = new PdpSubGroup();
+        sub2.setCurrentInstanceCount(11);
+        orig.setPdpSubgroups(Arrays.asList(sub1, sub2));
+
+        Map<String, String> props = new TreeMap<>();
+        props.put("key-A", "value-A");
+        props.put("key-B", "value-B");
+        orig.setProperties(props);
+
+        System.out.println("orig=" + orig);
+
+        assertEquals(orig.toString(), new PdpGroup(orig).toString());
+    }
+}
diff --git a/models-pap/src/test/java/org/onap/policy/models/pap/concepts/TestPdpInstanceDetails.java b/models-pap/src/test/java/org/onap/policy/models/pap/concepts/TestPdpInstanceDetails.java
new file mode 100644 (file)
index 0000000..9e48480
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Models
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.pap.concepts;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.onap.policy.pdp.common.enums.PdpHealthStatus;
+import org.onap.policy.pdp.common.enums.PdpState;
+
+/**
+ * Test the copy constructor, as {@link TestModels} tests the other methods.
+ */
+public class TestPdpInstanceDetails {
+
+    @Test
+    public void testCopyConstructor() {
+        assertThatThrownBy(() -> new PdpInstanceDetails(null)).isInstanceOf(NullPointerException.class);
+
+        PdpInstanceDetails orig = new PdpInstanceDetails();
+
+        // verify with null values
+        assertEquals(orig.toString(), new PdpInstanceDetails(orig).toString());
+
+        // verify with all values
+        orig.setHealthy(PdpHealthStatus.TEST_IN_PROGRESS);
+        orig.setInstanceId("my-id");
+        orig.setMessage("my-message");
+        orig.setPdpState(PdpState.SAFE);
+        assertEquals(orig.toString(), new PdpInstanceDetails(orig).toString());
+    }
+}
diff --git a/models-pap/src/test/java/org/onap/policy/models/pap/concepts/TestPdpSubGroup.java b/models-pap/src/test/java/org/onap/policy/models/pap/concepts/TestPdpSubGroup.java
new file mode 100644 (file)
index 0000000..d2e5f2f
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Models
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.pap.concepts;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
+
+import java.util.Arrays;
+import java.util.Map;
+import java.util.TreeMap;
+import org.junit.Test;
+import org.onap.policy.models.base.PfConceptKey;
+
+/**
+ * Test the copy constructor, as {@link TestModels} tests the other methods.
+ */
+public class TestPdpSubGroup {
+
+    @Test
+    public void testCopyConstructor() {
+        assertThatThrownBy(() -> new PdpSubGroup(null)).isInstanceOf(NullPointerException.class);
+
+        PdpSubGroup orig = new PdpSubGroup();
+
+        // verify with null values
+        assertEquals(orig.toString(), new PdpSubGroup(orig).toString());
+
+        // verify with all values
+        orig.setCurrentInstanceCount(10);
+        orig.setDesiredInstanceCount(11);
+
+        PdpInstanceDetails inst1 = new PdpInstanceDetails();
+        inst1.setInstanceId("my-id-A");
+        PdpInstanceDetails inst2 = new PdpInstanceDetails();
+        inst2.setInstanceId("my-id-B");
+        orig.setPdpInstances(Arrays.asList(inst1, inst2));
+
+        orig.setPdpType("my-type");
+
+        Policy pol1 = new Policy();
+        pol1.setName("policy-A");
+        Policy pol2 = new Policy();
+        pol2.setName("policy-B");
+        orig.setPolicies(Arrays.asList(pol1, pol2));
+
+        Map<String, String> props = new TreeMap<>();
+        props.put("key-A", "value-A");
+        props.put("key-B", "value-B");
+        orig.setProperties(props);
+
+        PfConceptKey supp1 = new PfConceptKey("supp-A", "1.2");
+        PfConceptKey supp2 = new PfConceptKey("supp-B", "3.4");
+        orig.setSupportedPolicyTypes(Arrays.asList(supp1, supp2));
+
+        assertEquals(orig.toString(), new PdpSubGroup(orig).toString());
+    }
+}
diff --git a/models-pap/src/test/java/org/onap/policy/models/pap/concepts/TestPolicy.java b/models-pap/src/test/java/org/onap/policy/models/pap/concepts/TestPolicy.java
new file mode 100644 (file)
index 0000000..6a042d3
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Models
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.pap.concepts;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+/**
+ * Test the copy constructor, as {@link TestModels} tests the other methods.
+ */
+public class TestPolicy {
+
+    @Test
+    public void testCopyConstructor() {
+        assertThatThrownBy(() -> new Policy(null)).isInstanceOf(NullPointerException.class);
+
+        Policy orig = new Policy();
+
+        // verify with null values
+        assertEquals(orig.toString(), new Policy(orig).toString());
+
+        // verify with all values
+        orig.setName("my-name");
+        orig.setPolicyType("my-type");
+        orig.setPolicyTypeImpl("my-impl");
+        orig.setPolicyTypeVersion("my-type-vers");
+        assertEquals(orig.toString(), new Policy(orig).toString());
+    }
+}