summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-03-29 18:36:56 -0400
committerJim Hahn <jrh3@att.com>2019-04-01 20:23:58 -0400
commit3830d26928b1a654afbd1206528f3afd741cad1a (patch)
treeed50a2f9fd00bab055a48b41ac62876edd358cb7
parentdf456cc61db87de428120e287c941b47a455ff3b (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>
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/comm/PdpModifyRequestMap.java2
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/comm/msgdata/UpdateData.java2
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/internal/PdpDeployPolicies.java40
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/mapper/Mapper.java284
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/comm/PdpModifyRequestMapTest.java26
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/comm/msgdata/UpdateDataTest.java25
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/internal/TestModels.java46
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/mapper/MapperTest.java418
8 files changed, 829 insertions, 14 deletions
diff --git a/main/src/main/java/org/onap/policy/pap/main/comm/PdpModifyRequestMap.java b/main/src/main/java/org/onap/policy/pap/main/comm/PdpModifyRequestMap.java
index f2ebca5c..24443cc2 100644
--- a/main/src/main/java/org/onap/policy/pap/main/comm/PdpModifyRequestMap.java
+++ b/main/src/main/java/org/onap/policy/pap/main/comm/PdpModifyRequestMap.java
@@ -25,7 +25,7 @@ import java.util.HashMap;
import java.util.Map;
import org.onap.policy.models.pdp.concepts.PdpStateChange;
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.msgdata.StateChangeData;
import org.onap.policy.pap.main.comm.msgdata.UpdateData;
import org.onap.policy.pap.main.parameters.PdpModifyRequestMapParams;
diff --git a/main/src/main/java/org/onap/policy/pap/main/comm/msgdata/UpdateData.java b/main/src/main/java/org/onap/policy/pap/main/comm/msgdata/UpdateData.java
index 904c8237..4be057b7 100644
--- a/main/src/main/java/org/onap/policy/pap/main/comm/msgdata/UpdateData.java
+++ b/main/src/main/java/org/onap/policy/pap/main/comm/msgdata/UpdateData.java
@@ -24,7 +24,7 @@ import java.util.ArrayList;
import java.util.List;
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.parameters.PdpModifyRequestMapParams;
diff --git a/main/src/main/java/org/onap/policy/pap/main/internal/PdpDeployPolicies.java b/main/src/main/java/org/onap/policy/pap/main/internal/PdpDeployPolicies.java
new file mode 100644
index 00000000..bd3f7a6a
--- /dev/null
+++ b/main/src/main/java/org/onap/policy/pap/main/internal/PdpDeployPolicies.java
@@ -0,0 +1,40 @@
+/*
+ * ============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.pap.main.internal;
+
+import java.util.List;
+
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+import org.onap.policy.models.pdp.concepts.PolicyIdentOptVersion;
+
+/**
+ * Request deploy or update a set of policies using the <i>simple</i> PDP Group deployment
+ * REST API. Only the "name" and "version" fields of a Policy are used, and only the
+ * "name" field is actually required.
+ */
+@Getter
+@Setter
+@ToString
+public class PdpDeployPolicies {
+ private List<PolicyIdentOptVersion> policies;
+}
diff --git a/main/src/main/java/org/onap/policy/pap/main/mapper/Mapper.java b/main/src/main/java/org/onap/policy/pap/main/mapper/Mapper.java
new file mode 100644
index 00000000..0ae179b4
--- /dev/null
+++ b/main/src/main/java/org/onap/policy/pap/main/mapper/Mapper.java
@@ -0,0 +1,284 @@
+/*
+ * ============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 java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+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.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.pap.main.internal.PdpDeployPolicies;
+
+/**
+ * Classes used to map internal PAP/PDP classes to/from external classes used by the PAP
+ * REST API.
+ */
+public class Mapper {
+
+ private Mapper() {
+ super();
+ }
+
+
+ // these methods map from internal to external
+
+ /**
+ * Converts an internal PdpGroups to the corresponding external class.
+ *
+ * @return an external object, populated with data from this object
+ */
+ public static org.onap.policy.models.pap.concepts.PdpGroups toExternal(PdpGroups source) {
+
+ org.onap.policy.models.pap.concepts.PdpGroups target = new org.onap.policy.models.pap.concepts.PdpGroups();
+
+ target.setGroups(mapIt(source.getGroups(), Mapper::toExternal));
+
+ return target;
+ }
+
+ /**
+ * Converts an internal PdpGroup to the corresponding external class.
+ *
+ * @return an external object, populated with data from this object
+ */
+ public static org.onap.policy.models.pap.concepts.PdpGroup toExternal(PdpGroup source) {
+
+ org.onap.policy.models.pap.concepts.PdpGroup target = new org.onap.policy.models.pap.concepts.PdpGroup();
+
+ target.setDescription(source.getDescription());
+ target.setName(source.getName());
+ target.setPdpGroupState(source.getPdpGroupState());
+ target.setPdpSubgroups(mapIt(source.getPdpSubgroups(), Mapper::toExternal));
+ target.setProperties(mapIt(source.getProperties()));
+ target.setVersion(source.getVersion());
+
+ return target;
+ }
+
+ /**
+ * Converts an internal PdpSubGroup to the corresponding external class.
+ *
+ * @return an external object, populated with data from this object
+ */
+ public static org.onap.policy.models.pap.concepts.PdpSubGroup toExternal(PdpSubGroup source) {
+
+ org.onap.policy.models.pap.concepts.PdpSubGroup target = new org.onap.policy.models.pap.concepts.PdpSubGroup();
+
+ target.setCurrentInstanceCount(source.getCurrentInstanceCount());
+ target.setDesiredInstanceCount(source.getDesiredInstanceCount());
+ target.setPdpInstances(mapIt(source.getPdpInstances(), Mapper::toExternal));
+ target.setPdpType(source.getPdpType());
+ target.setPolicies(mapIt(source.getPolicies(), Mapper::toExternal));
+ target.setProperties(mapIt(source.getProperties()));
+ target.setSupportedPolicyTypes(mapIt(source.getSupportedPolicyTypes(), Mapper::toExternal));
+
+ return target;
+ }
+
+ /**
+ * Converts an internal policy to the corresponding external class.
+ *
+ * @return an external object, populated with data from this object
+ */
+ public static org.onap.policy.models.pap.concepts.Policy toExternal(ToscaPolicy source) {
+
+ org.onap.policy.models.pap.concepts.Policy target = new org.onap.policy.models.pap.concepts.Policy();
+
+ target.setName(source.getName());
+ target.setPolicyVersion(source.getVersion());
+ target.setPolicyType(source.getType());
+ target.setPolicyTypeVersion(source.getTypeVersion());
+
+ // TODO setPolicyTypeImpl
+
+ return target;
+ }
+
+ /**
+ * Converts an internal PDP instance details to the corresponding external class.
+ *
+ * @return an external object, populated with data from this object
+ */
+ public static org.onap.policy.models.pap.concepts.PdpInstanceDetails toExternal(Pdp source) {
+
+ org.onap.policy.models.pap.concepts.PdpInstanceDetails target =
+ new org.onap.policy.models.pap.concepts.PdpInstanceDetails();
+
+ target.setHealthy(source.getHealthy());
+ target.setInstanceId(source.getInstanceId());
+ target.setMessage(source.getMessage());
+ target.setPdpState(source.getPdpState());
+
+ return target;
+ }
+
+ /**
+ * Converts an internal policy identifier to the corresponding external class.
+ *
+ * @return an external object, populated with data from this object
+ */
+ public static org.onap.policy.models.pap.concepts.PolicyIdent toExternal(PolicyIdent source) {
+
+ org.onap.policy.models.pap.concepts.PolicyIdent target = new org.onap.policy.models.pap.concepts.PolicyIdent();
+
+ target.setName(source.getName());
+ target.setVersion(source.getVersion());
+
+ return target;
+ }
+
+ /**
+ * Converts an internal policy type identifier to the corresponding external class.
+ *
+ * @return an external object, populated with data from this object
+ */
+ public static org.onap.policy.models.pap.concepts.PolicyTypeIdent toExternal(PolicyTypeIdent source) {
+
+ org.onap.policy.models.pap.concepts.PolicyTypeIdent target =
+ new org.onap.policy.models.pap.concepts.PolicyTypeIdent();
+
+ target.setName(source.getName());
+ target.setVersion(source.getVersion());
+
+ return target;
+ }
+
+ /**
+ * Converts an internal policy identifier, with optional version, to the corresponding
+ * external class.
+ *
+ * @return an external object, populated with data from this object
+ */
+ public static org.onap.policy.models.pap.concepts.PolicyIdentOptVersion toExternal(PolicyIdentOptVersion source) {
+
+ org.onap.policy.models.pap.concepts.PolicyIdentOptVersion target =
+ new org.onap.policy.models.pap.concepts.PolicyIdentOptVersion();
+
+ target.setName(source.getName());
+ target.setVersion(source.getVersion());
+
+ return target;
+ }
+
+
+ // these methods map from external to internal
+
+ /**
+ * Converts an external PdpDeployPolicies to the corresponding internal class.
+ *
+ * @return an internal object, populated with data from this object
+ */
+ public static PdpDeployPolicies toInternal(org.onap.policy.models.pap.concepts.PdpDeployPolicies source) {
+ PdpDeployPolicies target = new PdpDeployPolicies();
+
+ target.setPolicies(mapIt(source.getPolicies(), Mapper::toInternal));
+
+ return target;
+ }
+
+ /**
+ * Converts an external PDP instance details to the corresponding internal class.
+ *
+ * @return an internal object, populated with data from this object
+ */
+ public static Pdp toInternal(org.onap.policy.models.pap.concepts.PdpInstanceDetails source) {
+ Pdp target = new Pdp();
+
+ target.setHealthy(source.getHealthy());
+ target.setInstanceId(source.getInstanceId());
+ target.setMessage(source.getMessage());
+ target.setPdpState(source.getPdpState());
+
+ return target;
+ }
+
+ /**
+ * Converts an external policy identifier to the corresponding internal class.
+ *
+ * @return an internal object, populated with data from this object
+ */
+ public static PolicyIdent toInternal(org.onap.policy.models.pap.concepts.PolicyIdent source) {
+ PolicyIdent target = new PolicyIdent();
+
+ target.setName(source.getName());
+ target.setVersion(source.getVersion());
+
+ return target;
+ }
+
+ /**
+ * Converts an external policy type identifier to the corresponding internal class.
+ *
+ * @return an internal object, populated with data from this object
+ */
+ public static PolicyTypeIdent toInternal(org.onap.policy.models.pap.concepts.PolicyTypeIdent source) {
+ PolicyTypeIdent target = new PolicyTypeIdent();
+
+ target.setName(source.getName());
+ target.setVersion(source.getVersion());
+
+ return target;
+ }
+
+ /**
+ * Converts an external policy identifier, with optional version, to the corresponding
+ * internal class.
+ *
+ * @return an internal object, populated with data from this object
+ */
+ public static PolicyIdentOptVersion toInternal(org.onap.policy.models.pap.concepts.PolicyIdentOptVersion source) {
+ PolicyIdentOptVersion target = new PolicyIdentOptVersion();
+
+ target.setName(source.getName());
+ target.setVersion(source.getVersion());
+
+ return target;
+ }
+
+ /**
+ * Maps a list of one type of object to a list of another type of object.
+ *
+ * @param source list of items to be mapped
+ * @param mapper function to map an item to an object of the target type
+ * @return a list of objects of the new type
+ */
+ private static <T, R> List<R> mapIt(List<T> source, Function<T, R> mapper) {
+ return source.stream().map(mapper).collect(Collectors.toList());
+ }
+
+ /**
+ * Creates a copy of a map.
+ *
+ * @param map map to be copied
+ * @return a copy of the map
+ */
+ private static Map<String, String> mapIt(Map<String, String> map) {
+ return new LinkedHashMap<>(map);
+ }
+}
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());
+ }
+}