diff options
author | 2019-03-29 18:36:56 -0400 | |
---|---|---|
committer | 2019-04-01 20:23:58 -0400 | |
commit | 3830d26928b1a654afbd1206528f3afd741cad1a (patch) | |
tree | ed50a2f9fd00bab055a48b41ac62876edd358cb7 /main/src/test/java/org/onap | |
parent | df456cc61db87de428120e287c941b47a455ff3b (diff) |
Add mapper class
Added class to map between classes in models-pap and models-pdp.
Used constants in appropriate places.
Re-use maker functions.
Added some comment.
Corrected a comment.
Added mapping method for PdpDeployPolicies.
Added PdpDeployPolicies to "internal" package.
Updated to reflect refactoring of ToscaPolicy.
Fixed bug/typo in assertEquals test.
Updated tests to work with models-pdp not based on PfConceptKey.
Change-Id: I31cd4aaf58fa7dc39cea76349cc1337637a1b786
Issue-ID: POLICY-1542
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'main/src/test/java/org/onap')
4 files changed, 503 insertions, 12 deletions
diff --git a/main/src/test/java/org/onap/policy/pap/main/comm/PdpModifyRequestMapTest.java b/main/src/test/java/org/onap/policy/pap/main/comm/PdpModifyRequestMapTest.java index bbe75a44..96a39bc3 100644 --- a/main/src/test/java/org/onap/policy/pap/main/comm/PdpModifyRequestMapTest.java +++ b/main/src/test/java/org/onap/policy/pap/main/comm/PdpModifyRequestMapTest.java @@ -50,13 +50,12 @@ import org.mockito.stubbing.Answer; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.listeners.RequestIdDispatcher; import org.onap.policy.common.endpoints.listeners.TypedMessageListener; -import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.pdp.concepts.PdpMessage; import org.onap.policy.models.pdp.concepts.PdpStateChange; import org.onap.policy.models.pdp.concepts.PdpStatus; import org.onap.policy.models.pdp.concepts.PdpUpdate; import org.onap.policy.models.pdp.enums.PdpState; -import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.pap.main.PapConstants; import org.onap.policy.pap.main.comm.PdpModifyRequestMap.ModifyReqData; import org.onap.policy.pap.main.parameters.PdpModifyRequestMapParams; @@ -288,7 +287,7 @@ public class PdpModifyRequestMapTest { map.addRequest(update); ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPolicies()); - policies.set(0, new ToscaPolicy(new PfConceptKey("policy-3-x", "2.0.0"))); + policies.set(0, makePolicy("policy-3-x", "2.0.0")); PdpUpdate update2 = makeUpdate(); update2.setPolicies(policies); @@ -551,15 +550,28 @@ public class PdpModifyRequestMapTest { upd.setPdpSubgroup("sub1-a"); upd.setPdpType("drools"); - ToscaPolicy policy1 = new ToscaPolicy(new PfConceptKey("policy-1-a", "1.0.0")); - ToscaPolicy policy2 = new ToscaPolicy(new PfConceptKey("policy-2-a", "1.1.0")); - - upd.setPolicies(Arrays.asList(policy1, policy2)); + upd.setPolicies(Arrays.asList(makePolicy("policy-1-a", "1.0.0"), makePolicy("policy-2-a", "1.1.0"))); return upd; } /** + * Creates a new policy. + * + * @param name policy name + * @param version policy version + * @return a new policy + */ + private ToscaPolicy makePolicy(String name, String version) { + ToscaPolicy policy = new ToscaPolicy(); + + policy.setName(name); + policy.setVersion(version); + + return policy; + } + + /** * Makes a state-change message. * * @return a new state-change message diff --git a/main/src/test/java/org/onap/policy/pap/main/comm/msgdata/UpdateDataTest.java b/main/src/test/java/org/onap/policy/pap/main/comm/msgdata/UpdateDataTest.java index 8676c95e..3ff6f4d2 100644 --- a/main/src/test/java/org/onap/policy/pap/main/comm/msgdata/UpdateDataTest.java +++ b/main/src/test/java/org/onap/policy/pap/main/comm/msgdata/UpdateDataTest.java @@ -30,10 +30,9 @@ import java.util.ArrayList; import java.util.Arrays; import org.junit.Before; import org.junit.Test; -import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.pdp.concepts.PdpStatus; import org.onap.policy.models.pdp.concepts.PdpUpdate; -import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.pap.main.comm.TimerManager; import org.onap.policy.pap.main.parameters.PdpModifyRequestMapParams; import org.onap.policy.pap.main.parameters.PdpParameters; @@ -121,7 +120,7 @@ public class UpdateDataTest { @Test public void testUpdateDataCheckResponse_MismatchedPolicies() { ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPolicies()); - policies.set(0, new ToscaPolicy(new PfConceptKey(DIFFERENT, "10.0.0"))); + policies.set(0, makePolicy(DIFFERENT, "10.0.0")); response.setPolicies(policies); @@ -142,14 +141,30 @@ public class UpdateDataTest { upd.setPdpSubgroup("sub1-a"); upd.setPdpType("drools"); - ToscaPolicy policy1 = new ToscaPolicy(new PfConceptKey("policy-1-a", "1.0.0")); - ToscaPolicy policy2 = new ToscaPolicy(new PfConceptKey("policy-2-a", "1.1.0")); + ToscaPolicy policy1 = makePolicy("policy-1-a", "1.0.0"); + ToscaPolicy policy2 = makePolicy("policy-2-a", "1.1.0"); upd.setPolicies(Arrays.asList(policy1, policy2)); return upd; } + /** + * Creates a new policy. + * + * @param name policy name + * @param version policy version + * @return a new policy + */ + private ToscaPolicy makePolicy(String name, String version) { + ToscaPolicy policy = new ToscaPolicy(); + + policy.setName(name); + policy.setVersion(version); + + return policy; + } + private class MyData extends UpdateData { public MyData(PdpUpdate message) { diff --git a/main/src/test/java/org/onap/policy/pap/main/internal/TestModels.java b/main/src/test/java/org/onap/policy/pap/main/internal/TestModels.java new file mode 100644 index 00000000..7cdd06ee --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/internal/TestModels.java @@ -0,0 +1,46 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019 AT&T Intellectual Property. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.internal; + +import com.openpojo.reflection.filters.FilterPackageInfo; +import com.openpojo.validation.Validator; +import com.openpojo.validation.ValidatorBuilder; +import com.openpojo.validation.test.impl.GetterTester; +import com.openpojo.validation.test.impl.SetterTester; + +import org.junit.Test; +import org.onap.policy.common.utils.validation.ToStringTester; + +/** + * Class to perform unit testing of models. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class TestModels { + + @Test + public void testPapModels() { + final Validator validator = ValidatorBuilder.create().with(new ToStringTester()).with(new SetterTester()) + .with(new GetterTester()).build(); + validator.validate(TestModels.class.getPackage().getName(), new FilterPackageInfo()); + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/mapper/MapperTest.java b/main/src/test/java/org/onap/policy/pap/main/mapper/MapperTest.java new file mode 100644 index 00000000..350bd509 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/mapper/MapperTest.java @@ -0,0 +1,418 @@ +/* + * ============LICENSE_START======================================================= + * ONAP PAP + * ================================================================================ + * 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.pap.main.mapper; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import java.util.function.BiConsumer; +import org.junit.Test; +import org.onap.policy.models.pdp.concepts.Pdp; +import org.onap.policy.models.pdp.concepts.PdpGroup; +import org.onap.policy.models.pdp.concepts.PdpGroups; +import org.onap.policy.models.pdp.concepts.PdpSubGroup; +import org.onap.policy.models.pdp.concepts.PolicyIdent; +import org.onap.policy.models.pdp.concepts.PolicyIdentOptVersion; +import org.onap.policy.models.pdp.concepts.PolicyTypeIdent; +import org.onap.policy.models.pdp.enums.PdpHealthStatus; +import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.pap.main.internal.PdpDeployPolicies; +import org.powermock.reflect.Whitebox; + +public class MapperTest { + private static final String MY_NAME = "my-name"; + private static final String MY_VERSION = "10.11.12"; + private static final PdpHealthStatus MY_HEALTH = PdpHealthStatus.TEST_IN_PROGRESS; + private static final String MY_INSTANCE = "my_1"; + private static final String MY_MESSAGE = "my message"; + private static final PdpState MY_STATE = PdpState.SAFE; + private static final int CURRENT_COUNT = 1; + private static final int DESIRED_COUNT = 2; + private static final String PDP_TYPE1 = "drools"; + private static final String PROPKEY1 = "prop-a"; + private static final String PROPVAL1 = "value-a"; + private static final String PROPKEY2 = "prop-b"; + private static final String PROPVAL2 = "value-b"; + private static final String MY_DESCRIPTION = "my description"; + + @Test + public void testConstructor() throws Exception { + Whitebox.invokeConstructor(Mapper.class); + } + + @Test + public void testPdpDeployPolicies() { + + // TO-INTERNAL + + // test populated object + org.onap.policy.models.pap.concepts.PdpDeployPolicies external = + new org.onap.policy.models.pap.concepts.PdpDeployPolicies(); + + external.setPolicies(Arrays.asList(makePolicyIdentOptVersion(1), makePolicyIdentOptVersion(2))); + compareWithExternal(external, Mapper.toInternal(external)); + } + + private org.onap.policy.models.pap.concepts.PolicyIdentOptVersion makePolicyIdentOptVersion(int count) { + + org.onap.policy.models.pap.concepts.PolicyIdentOptVersion type = + new org.onap.policy.models.pap.concepts.PolicyIdentOptVersion(); + + + type.setName(MY_NAME + "-id-" + count); + type.setVersion(count + ".1.2"); + + return type; + } + + @Test + public void testPdpGroups() { + + // TO-EXTERNAL + + // test populated object + PdpGroups internal = new PdpGroups(); + internal.setGroups(Arrays.asList(makeGroup(1), makeGroup(2))); + + compareWithInternal(internal, Mapper.toExternal(internal)); + } + + private PdpGroup makeGroup(int count) { + PdpGroup group = new PdpGroup(); + + group.setDescription(MY_DESCRIPTION + "-" + count); + group.setName(MY_NAME); + group.setVersion(MY_VERSION); + group.setPdpGroupState(MY_STATE); + + int inc = count + 1; + group.setPdpSubgroups(Arrays.asList(makeSubGroup(count * 10), makeSubGroup(inc * 10))); + group.setProperties(makeProperties()); + + return group; + } + + private PdpSubGroup makeSubGroup(int count) { + PdpSubGroup subgroup = new PdpSubGroup(); + + subgroup.setCurrentInstanceCount(CURRENT_COUNT); + subgroup.setDesiredInstanceCount(DESIRED_COUNT); + + int inc = count + 1; + subgroup.setPdpInstances(Arrays.asList(makePdp(count), makePdp(inc))); + subgroup.setPdpType(PDP_TYPE1); + subgroup.setPolicies(Arrays.asList(makePolicy(count, count), makePolicy(inc, inc))); + subgroup.setProperties(makeProperties()); + subgroup.setSupportedPolicyTypes(Arrays.asList(makePolicyType(count), makePolicyType(inc))); + + return subgroup; + } + + private Pdp makePdp(int count) { + Pdp object = new Pdp(); + + object.setHealthy(MY_HEALTH); + object.setInstanceId(MY_INSTANCE + count); + object.setMessage(MY_MESSAGE); + object.setPdpState(MY_STATE); + + return object; + } + + private PolicyTypeIdent makePolicyType(int count) { + PolicyTypeIdent type = new PolicyTypeIdent(); + + type.setName(MY_NAME + "-type-" + count); + type.setVersion(count + ".1.2"); + + return type; + } + + private Map<String, String> makeProperties() { + Map<String, String> map = new TreeMap<>(); + + map.put(PROPKEY1, PROPVAL1); + map.put(PROPKEY2, PROPVAL2); + + return map; + } + + @Test + public void testPdpGroup() { + + // TO-EXTERNAL + + // test populated object + PdpGroup internal = makeGroup(1); + compareWithInternal(internal, Mapper.toExternal(internal)); + } + + @Test + public void testPdpSubGroup() { + + // TO-EXTERNAL + + // test populated object + PdpSubGroup internal = makeSubGroup(1); + compareWithInternal(internal, Mapper.toExternal(internal)); + } + + @Test + public void testPolicy() { + + // TO-EXTERNAL + + // test populated object + ToscaPolicy internal = makePolicy(1, 1); + compareWithInternal(internal, Mapper.toExternal(internal)); + } + + private ToscaPolicy makePolicy(int count, int typeCount) { + ToscaPolicy policy = new ToscaPolicy(); + + policy.setName(MY_NAME + "-policy-" + count); + policy.setVersion("1.2." + count); + policy.setType(MY_NAME + "-type-" + typeCount); + policy.setTypeVersion("1.2." + typeCount); + + return policy; + } + + @Test + public void testPdpInstanceDetails() { + + // TO-EXTERNAL + + // test populated object + Pdp internal = makePdp(1); + compareWithInternal(internal, Mapper.toExternal(internal)); + + + // TO-INTERNAL + + // test populated object + org.onap.policy.models.pap.concepts.PdpInstanceDetails external = + new org.onap.policy.models.pap.concepts.PdpInstanceDetails(); + external.setHealthy(MY_HEALTH); + external.setInstanceId(MY_INSTANCE); + external.setMessage(MY_MESSAGE); + external.setPdpState(MY_STATE); + compareWithExternal(external, Mapper.toInternal(external)); + } + + @Test + public void testPolicyIdent() { + + // TO-EXTERNAL + + // first test default object + PolicyIdent internal = new PolicyIdent(); + org.onap.policy.models.pap.concepts.PolicyIdent external = Mapper.toExternal(internal); + compareWithInternal(internal, external); + + // now test populated object + internal.setName(MY_NAME); + internal.setVersion(MY_VERSION); + external = Mapper.toExternal(internal); + compareWithInternal(internal, external); + + + // TO-INTERNAL + + external = new org.onap.policy.models.pap.concepts.PolicyIdent(); + external.setName(MY_NAME); + external.setVersion(MY_VERSION); + compareWithExternal(external, Mapper.toInternal(external)); + } + + @Test + public void testPolicyTypeIdent() { + + // TO-EXTERNAL + + // first test default object + PolicyTypeIdent internal = new PolicyTypeIdent(); + org.onap.policy.models.pap.concepts.PolicyTypeIdent external = Mapper.toExternal(internal); + compareWithInternal(internal, external); + + // now test populated object + internal.setName(MY_NAME); + internal.setVersion(MY_VERSION); + external = Mapper.toExternal(internal); + compareWithInternal(internal, external); + + + // TO-INTERNAL + + external = new org.onap.policy.models.pap.concepts.PolicyTypeIdent(); + external.setName(MY_NAME); + external.setVersion(MY_VERSION); + compareWithExternal(external, Mapper.toInternal(external)); + } + + @Test + public void testPolicyIdentOptVersion() { + + // TO-EXTERNAL + + PolicyIdentOptVersion internal = new PolicyIdentOptVersion(); + internal.setName(MY_NAME); + internal.setVersion(MY_VERSION); + org.onap.policy.models.pap.concepts.PolicyIdentOptVersion external = Mapper.toExternal(internal); + compareWithInternal(internal, external); + + + // TO-INTERNAL + + external = new org.onap.policy.models.pap.concepts.PolicyIdentOptVersion(); + external.setName(MY_NAME); + external.setVersion(MY_VERSION); + compareWithExternal(external, Mapper.toInternal(external)); + } + + + /** + * Compares the items in two lists. + * + * @param expected the expected items + * @param actual the actual items + * @param compareItem function to compare an expected item with an actual item + */ + private static <L, R> void compareList(List<L> expected, List<R> actual, BiConsumer<L, R> compareItem) { + + assertEquals(expected.size(), actual.size()); + + Iterator<L> expIterator = expected.iterator(); + for (R actualItem : actual) { + L expectedItem = expIterator.next(); + compareItem.accept(expectedItem, actualItem); + } + } + + + // compares actual internal objects with expected external objects + + private static void compareWithExternal(org.onap.policy.models.pap.concepts.PolicyIdent expected, + PolicyIdent actual) { + + assertEquals(expected.getName(), actual.getName()); + assertEquals(expected.getVersion(), actual.getVersion()); + } + + private static void compareWithExternal(org.onap.policy.models.pap.concepts.PolicyTypeIdent expected, + PolicyTypeIdent actual) { + + assertEquals(expected.getName(), actual.getName()); + assertEquals(expected.getVersion(), actual.getVersion()); + } + + private static void compareWithExternal(org.onap.policy.models.pap.concepts.PolicyIdentOptVersion expected, + PolicyIdentOptVersion actual) { + + assertEquals(expected.getName(), actual.getName()); + assertEquals(expected.getVersion(), actual.getVersion()); + } + + private static void compareWithExternal(org.onap.policy.models.pap.concepts.PdpDeployPolicies expected, + PdpDeployPolicies actual) { + + compareList(expected.getPolicies(), actual.getPolicies(), MapperTest::compareWithExternal); + } + + private static void compareWithExternal(org.onap.policy.models.pap.concepts.PdpInstanceDetails expected, + Pdp actual) { + + assertEquals(expected.getHealthy(), actual.getHealthy()); + assertEquals(expected.getInstanceId(), actual.getInstanceId()); + assertEquals(expected.getMessage(), actual.getMessage()); + assertEquals(expected.getPdpState(), actual.getPdpState()); + } + + // compares actual external objects with expected internal objects + + private static void compareWithInternal(PolicyIdent expected, + org.onap.policy.models.pap.concepts.PolicyIdent actual) { + + assertEquals(expected.getName(), actual.getName()); + assertEquals(expected.getVersion(), actual.getVersion()); + } + + private static void compareWithInternal(PolicyTypeIdent expected, + org.onap.policy.models.pap.concepts.PolicyTypeIdent actual) { + + assertEquals(expected.getName(), actual.getName()); + assertEquals(expected.getVersion(), actual.getVersion()); + } + + private static void compareWithInternal(PolicyIdentOptVersion expected, + org.onap.policy.models.pap.concepts.PolicyIdentOptVersion actual) { + + assertEquals(expected.getName(), actual.getName()); + assertEquals(expected.getVersion(), actual.getVersion()); + } + + private static void compareWithInternal(PdpGroups expected, org.onap.policy.models.pap.concepts.PdpGroups actual) { + compareList(expected.getGroups(), actual.getGroups(), MapperTest::compareWithInternal); + } + + private static void compareWithInternal(PdpGroup expected, org.onap.policy.models.pap.concepts.PdpGroup actual) { + assertEquals(expected.getDescription(), actual.getDescription()); + assertEquals(expected.getName(), actual.getName()); + assertEquals(expected.getVersion(), actual.getVersion()); + assertEquals(expected.getPdpGroupState(), actual.getPdpGroupState()); + compareList(expected.getPdpSubgroups(), actual.getPdpSubgroups(), MapperTest::compareWithInternal); + assertEquals(expected.getProperties(), actual.getProperties()); + } + + private static void compareWithInternal(PdpSubGroup expected, + org.onap.policy.models.pap.concepts.PdpSubGroup actual) { + + assertEquals(expected.getCurrentInstanceCount(), actual.getCurrentInstanceCount()); + assertEquals(expected.getDesiredInstanceCount(), actual.getDesiredInstanceCount()); + compareList(expected.getPdpInstances(), actual.getPdpInstances(), MapperTest::compareWithInternal); + assertEquals(expected.getPdpType(), actual.getPdpType()); + compareList(expected.getPolicies(), actual.getPolicies(), MapperTest::compareWithInternal); + assertEquals(expected.getProperties(), actual.getProperties()); + compareList(expected.getSupportedPolicyTypes(), actual.getSupportedPolicyTypes(), + MapperTest::compareWithInternal); + } + + private static void compareWithInternal(ToscaPolicy expected, org.onap.policy.models.pap.concepts.Policy actual) { + assertEquals(expected.getName(), actual.getName()); + assertEquals(expected.getVersion(), actual.getPolicyVersion()); + assertEquals(expected.getType(), actual.getPolicyType()); + assertEquals(expected.getTypeVersion(), actual.getPolicyTypeVersion()); + } + + private static void compareWithInternal(Pdp expected, + org.onap.policy.models.pap.concepts.PdpInstanceDetails actual) { + + assertEquals(expected.getHealthy(), actual.getHealthy()); + assertEquals(expected.getInstanceId(), actual.getInstanceId()); + assertEquals(expected.getMessage(), actual.getMessage()); + assertEquals(expected.getPdpState(), actual.getPdpState()); + } +} |