diff options
author | Jim Hahn <jrh3@att.com> | 2019-03-21 12:36:35 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-03-21 17:11:31 -0400 |
commit | 834851e6c460ef8a28f356a64fe7b85d8bbf9a55 (patch) | |
tree | cfc520093e320e0875f0e5ce6d2540b00af09754 | |
parent | 99837f40d77dd41f80eeb4649c43479e898bcaff (diff) |
Add copy constructors for models-pap
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>
11 files changed, 393 insertions, 14 deletions
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java b/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java index a18315ceb..8e77d3fcf 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java @@ -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. @@ -20,6 +21,10 @@ 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()); + } } diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java index 2b495a1e7..bdbab5c36 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java @@ -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); + } } diff --git a/models-pap/pom.xml b/models-pap/pom.xml index 1cc5603cb..a437d545b 100644 --- a/models-pap/pom.xml +++ b/models-pap/pom.xml @@ -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. @@ -37,5 +38,15 @@ <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 diff --git a/models-pap/src/main/java/org/onap/policy/models/pap/concepts/PdpGroup.java b/models-pap/src/main/java/org/onap/policy/models/pap/concepts/PdpGroup.java index 148168d89..a42ac21bd 100644 --- a/models-pap/src/main/java/org/onap/policy/models/pap/concepts/PdpGroup.java +++ b/models-pap/src/main/java/org/onap/policy/models/pap/concepts/PdpGroup.java @@ -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. @@ -20,13 +21,14 @@ 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); + } } diff --git a/models-pap/src/main/java/org/onap/policy/models/pap/concepts/PdpInstanceDetails.java b/models-pap/src/main/java/org/onap/policy/models/pap/concepts/PdpInstanceDetails.java index 168c20e38..4f9041c7f 100644 --- a/models-pap/src/main/java/org/onap/policy/models/pap/concepts/PdpInstanceDetails.java +++ b/models-pap/src/main/java/org/onap/policy/models/pap/concepts/PdpInstanceDetails.java @@ -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; + } } diff --git a/models-pap/src/main/java/org/onap/policy/models/pap/concepts/PdpSubGroup.java b/models-pap/src/main/java/org/onap/policy/models/pap/concepts/PdpSubGroup.java index ebb476d92..e535233bc 100644 --- a/models-pap/src/main/java/org/onap/policy/models/pap/concepts/PdpSubGroup.java +++ b/models-pap/src/main/java/org/onap/policy/models/pap/concepts/PdpSubGroup.java @@ -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. @@ -20,15 +21,19 @@ 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); + } } diff --git a/models-pap/src/main/java/org/onap/policy/models/pap/concepts/Policy.java b/models-pap/src/main/java/org/onap/policy/models/pap/concepts/Policy.java index e1281fc51..b83510e83 100644 --- a/models-pap/src/main/java/org/onap/policy/models/pap/concepts/Policy.java +++ b/models-pap/src/main/java/org/onap/policy/models/pap/concepts/Policy.java @@ -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 index 000000000..3dc3c24b2 --- /dev/null +++ b/models-pap/src/test/java/org/onap/policy/models/pap/concepts/TestPdpGroup.java @@ -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 index 000000000..9e48480f4 --- /dev/null +++ b/models-pap/src/test/java/org/onap/policy/models/pap/concepts/TestPdpInstanceDetails.java @@ -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 index 000000000..d2e5f2fcc --- /dev/null +++ b/models-pap/src/test/java/org/onap/policy/models/pap/concepts/TestPdpSubGroup.java @@ -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 index 000000000..6a042d3be --- /dev/null +++ b/models-pap/src/test/java/org/onap/policy/models/pap/concepts/TestPolicy.java @@ -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()); + } +} |