From 8fc237cc606b6e9c8c7d7e7a2c811fc671a4b40e Mon Sep 17 00:00:00 2001 From: liamfallon Date: Wed, 20 Mar 2019 16:08:21 +0000 Subject: Implement persistence test for policies The unit test MonitoringPolicySerializationTest tests persistence for policies and shows how persistence works. Issue-ID: POLICY-1195 Change-Id: I933eb538238f9ccd41ce69614e0c9afcac869c29 Signed-off-by: liamfallon --- .../models/tosca/simple/concepts/ToscaPolicyTest.java | 2 +- .../MonitoringPolicySerializationTest.java | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) (limited to 'models-tosca/src/test') diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicyTest.java index 7af5cc754..807f33ed2 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicyTest.java @@ -89,7 +89,7 @@ public class ToscaPolicyTest { PfConceptKey ptKey = new PfConceptKey("policyType", "0.0.1"); ToscaPolicy tp = new ToscaPolicy(tpKey, ptKey); - Map propertyMap = new HashMap<>(); + Map propertyMap = new HashMap<>(); propertyMap.put("Property", "Property Value"); tp.setProperties(propertyMap); assertEquals(propertyMap, tp.getProperties()); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java index 565fd6cb4..95f0ac971 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java @@ -25,6 +25,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.JsonSyntaxException; import java.io.IOException; @@ -66,6 +67,13 @@ public class MonitoringPolicySerializationTest { assertEquals("onap.restart.tca:1.0.0", serviceTemplate.getTopologyTemplate().getPolicies().get("onap.restart.tca").getId()); + + String reserializedString = gson.toJson(serviceTemplate, ToscaServiceTemplate.class); + assertEquals(vcpePolicyJson.replaceAll("\\s+", ""), reserializedString.replaceAll("\\s+", "")); + + ToscaServiceTemplate serviceTemplate2 = gson.fromJson(reserializedString, ToscaServiceTemplate.class); + assertNotNull(serviceTemplate2); + assertEquals(serviceTemplate, serviceTemplate2); } @Test @@ -75,7 +83,7 @@ public class MonitoringPolicySerializationTest { String vcpePolicyYaml = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.yaml"); Object yamlObject = yaml.load(vcpePolicyYaml); - String yamlAsJsonString = new Gson().toJson(yamlObject); + String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject); ToscaServiceTemplate serviceTemplate = gson.fromJson(yamlAsJsonString, ToscaServiceTemplate.class); @@ -85,5 +93,12 @@ public class MonitoringPolicySerializationTest { assertEquals("onap.restart.tca:1.0.0", serviceTemplate.getTopologyTemplate().getPolicies().get("onap.restart.tca").getId()); + + String reserializedString = gson.toJson(serviceTemplate, ToscaServiceTemplate.class); + assertEquals(yamlAsJsonString.replaceAll("\\s+", ""), reserializedString.replaceAll("\\s+", "")); + + ToscaServiceTemplate serviceTemplate2 = gson.fromJson(reserializedString, ToscaServiceTemplate.class); + assertNotNull(serviceTemplate2); + assertEquals(serviceTemplate, serviceTemplate2); } } -- cgit 1.2.3-korg