X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-tosca%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fpolicy%2Fmodels%2Ftosca%2Fsimple%2Fserialization%2FMonitoringPolicySerializationTest.java;h=318eaeede598b42569bb00ce5ee4f17b05c02de9;hb=172b8dcd3a772c03ec1d233cfd6602d901c61b99;hp=f5722ddedd1a191c2a398e207cff243e25f2d6e9;hpb=92bd3a4d89a3f7740094e0c5dac278722fc3f27c;p=policy%2Fmodels.git 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 f5722dded..318eaeede 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -31,16 +31,18 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import java.util.Map; -import org.junit.Before; + import org.junit.Test; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.coder.YamlJsonTranslator; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; +import org.onap.policy.models.tosca.utils.ToscaServiceTemplateUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.Yaml; @@ -55,7 +57,7 @@ public class MonitoringPolicySerializationTest { private static final String VERSION = "version"; - private static final String YAML_VERSION = "tosca_simple_yaml_1_0_0"; + private static final String YAML_VERSION = "tosca_simple_yaml_1_1_0"; private static final String DEFINITION_VERSION = "tosca_definitions_version"; @@ -90,30 +92,40 @@ public class MonitoringPolicySerializationTest { private static final String VFW_MON_INPUT_JSON = "policies/vFirewall.policy.monitoring.input.tosca.json"; private static final String VFW_MON_INPUT_YAML = "policies/vFirewall.policy.monitoring.input.tosca.yaml"; - private StandardCoder standardCoder; - - @Before - public void setUp() { - standardCoder = new StandardCoder(); - } + private StandardCoder standardCoder = new StandardCoder(); + private YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator(); @Test public void testDeserialization() throws Exception { + String policyTypeInputJson = + ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml"); + ToscaServiceTemplate plainPolicyTypes = + yamlJsonTranslator.fromYaml(policyTypeInputJson, ToscaServiceTemplate.class); + + JpaToscaServiceTemplate policyTypeServiceTemplate = new JpaToscaServiceTemplate(); + policyTypeServiceTemplate.fromAuthorative(plainPolicyTypes); + // vCPE JpaToscaServiceTemplate serviceTemplateFromJson = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON); - verifyVcpeMonitoringInputDeserialization(serviceTemplateFromJson); + JpaToscaServiceTemplate mergedServiceTemplate = + ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson); + verifyVcpeMonitoringInputDeserialization(mergedServiceTemplate); JpaToscaServiceTemplate serviceTemplateFromYaml = deserializeMonitoringInputYaml(VCPE_MON_INPUT_YAML); assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0); // vDNS serviceTemplateFromJson = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON); - verifyVdnsMonitoringInputDeserialization(serviceTemplateFromJson); + mergedServiceTemplate = + ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson); + verifyVdnsMonitoringInputDeserialization(mergedServiceTemplate); serviceTemplateFromYaml = deserializeMonitoringInputYaml(VDNS_MON_INPUT_YAML); assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0); // vFirewall serviceTemplateFromJson = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON); - verifyVfwMonitoringInputDeserialization(serviceTemplateFromJson); + mergedServiceTemplate = + ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson); + verifyVfwMonitoringInputDeserialization(mergedServiceTemplate); serviceTemplateFromYaml = deserializeMonitoringInputYaml(VFW_MON_INPUT_YAML); assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0); } @@ -142,8 +154,7 @@ public class MonitoringPolicySerializationTest { } } - private JpaToscaServiceTemplate deserializeMonitoringInputJson(String resourcePath) - throws Exception { + private JpaToscaServiceTemplate deserializeMonitoringInputJson(String resourcePath) throws Exception { String policyJson = ResourceUtils.getResourceAsString(resourcePath); ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyJson, ToscaServiceTemplate.class); @@ -152,8 +163,7 @@ public class MonitoringPolicySerializationTest { return jpaToscaServiceTemplate; } - private JpaToscaServiceTemplate deserializeMonitoringInputYaml(String resourcePath) - throws Exception { + private JpaToscaServiceTemplate deserializeMonitoringInputYaml(String resourcePath) throws Exception { Yaml yaml = new Yaml(); String policyYaml = ResourceUtils.getResourceAsString(resourcePath); @@ -178,11 +188,10 @@ public class MonitoringPolicySerializationTest { assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid()); // Check tosca_definitions_version - assertEquals(YAML_VERSION, - serviceTemplate.getToscaDefinitionsVersion()); + assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion()); - Map policiesConceptMap = serviceTemplate.getTopologyTemplate() - .getPolicies().getConceptMap(); + Map policiesConceptMap = + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap(); // Check policies assertTrue(policiesConceptMap.size() == 1); @@ -211,11 +220,10 @@ public class MonitoringPolicySerializationTest { assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid()); // Check tosca_definitions_version - assertEquals(YAML_VERSION, - serviceTemplate.getToscaDefinitionsVersion()); + assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion()); - Map policiesConceptMap = serviceTemplate.getTopologyTemplate() - .getPolicies().getConceptMap(); + Map policiesConceptMap = + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap(); // Check policies assertTrue(policiesConceptMap.size() == 1); @@ -244,11 +252,10 @@ public class MonitoringPolicySerializationTest { assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid()); // Check tosca_definitions_version - assertEquals(YAML_VERSION, - serviceTemplate.getToscaDefinitionsVersion()); + assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion()); - Map policiesConceptMap = serviceTemplate.getTopologyTemplate() - .getPolicies().getConceptMap(); + Map policiesConceptMap = + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap(); // Check policies assertTrue(policiesConceptMap.size() == 1); @@ -271,11 +278,9 @@ public class MonitoringPolicySerializationTest { private void verifyVcpeMonitoringOutputserialization(String serializedServiceTemplate) { - JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject(); - assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION) - .getAsString()); - JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE) - .getAsJsonObject(); + JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject(); + assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString()); + JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject(); JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray(); assertTrue(policiesJsonArray.size() == 1); JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject(); @@ -283,17 +288,15 @@ public class MonitoringPolicySerializationTest { JsonObject policyVal = policy.get(POLICY1).getAsJsonObject(); assertEquals(TYPE1, policyVal.get("type").getAsString()); assertEquals(VERSION_100, policyVal.get(VERSION).getAsString()); - assertEquals(POLICY1, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID) - .getAsString()); + assertEquals(POLICY1, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString()); JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject(); assertNotNull(properties.get(TCA_POLICY)); } private void verifyVdnsMonitoringOutputserialization(String serializedServiceTemplate) { - JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject(); - assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION) - .getAsString()); + JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject(); + assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString()); JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject(); JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray(); assertTrue(policiesJsonArray.size() == 1); @@ -302,17 +305,15 @@ public class MonitoringPolicySerializationTest { JsonObject policyVal = policy.get(POLICY2).getAsJsonObject(); assertEquals(TYPE1, policyVal.get("type").getAsString()); assertEquals(VERSION_100, policyVal.get(VERSION).getAsString()); - assertEquals(POLICY2, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID) - .getAsString()); + assertEquals(POLICY2, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString()); JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject(); assertNotNull(properties.get(TCA_POLICY)); } private void verifyVfwMonitoringOutputserialization(String serializedServiceTemplate) { - JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject(); - assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION) - .getAsString()); + JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject(); + assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString()); JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject(); JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray(); assertTrue(policiesJsonArray.size() == 1); @@ -321,8 +322,7 @@ public class MonitoringPolicySerializationTest { JsonObject policyVal = policy.get(POLICY3).getAsJsonObject(); assertEquals(TYPE1, policyVal.get("type").getAsString()); assertEquals(VERSION_100, policyVal.get(VERSION).getAsString()); - assertEquals(POLICY3, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID) - .getAsString()); + assertEquals(POLICY3, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString()); JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject(); assertNotNull(properties.get(TCA_POLICY)); }