From 258c5e32e5ef1020a6cac9b06e323693f7a901b9 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Wed, 15 Jan 2020 09:20:05 +0100 Subject: Clean up TOSCA provider unit tests SOme outstanding comment on TOSCA provider unit tests cleaned up. Issue-ID: POLICY-2315 Change-Id: I235a5fc9996c81f421f17236afb413df7435de19 Signed-off-by: liamfallon --- .../AuthorativeToscaProviderReferenceTest.java | 20 ++++----- .../provider/impl/PolicyToscaPersistenceTest.java | 25 ++++++----- .../provider/impl/PolicyTypePersistenceTest.java | 15 +++---- .../src/test/resources/logback-test.xml | 50 ++++++++++++++++++++++ 4 files changed, 79 insertions(+), 31 deletions(-) create mode 100644 models-provider/src/test/resources/logback-test.xml (limited to 'models-provider') diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/AuthorativeToscaProviderReferenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/AuthorativeToscaProviderReferenceTest.java index 57a4d8105..5f904dfb4 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/AuthorativeToscaProviderReferenceTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/AuthorativeToscaProviderReferenceTest.java @@ -23,9 +23,6 @@ package org.onap.policy.models.provider.impl; import static org.junit.Assert.assertEquals; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; @@ -35,6 +32,8 @@ import org.eclipse.persistence.config.PersistenceUnitProperties; import org.junit.AfterClass; import org.junit.BeforeClass; 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.resources.ResourceUtils; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.dao.DaoParameters; @@ -53,7 +52,7 @@ import org.yaml.snakeyaml.Yaml; */ public class AuthorativeToscaProviderReferenceTest { private static PfDao pfDao; - private static final Gson gson = new GsonBuilder().setPrettyPrinting().create(); + private static final StandardCoder coder = new StandardCoder(); // @formatter:off private static final String[] EXAMPLE_POLICY_TYPES = { @@ -141,17 +140,18 @@ public class AuthorativeToscaProviderReferenceTest { * Populate the database. * * @throws PfModelException on database exceptions + * @throws CoderException on JSON encoding/decoding errors */ @BeforeClass - public static void beforeFillDatabase() throws PfModelException { + public static void beforeFillDatabase() throws PfModelException, CoderException { for (String policyTypeDataString : EXAMPLE_POLICY_TYPES) { String[] policyTypeDataArray = policyTypeDataString.split("#"); String policyTypeYamlDefinition = ResourceUtils.getResourceAsString(policyTypeDataArray[2]); Object yamlObject = new Yaml().load(policyTypeYamlDefinition); - String policyTypeJsonDefinition = gson.toJson(yamlObject); + String policyTypeJsonDefinition = coder.encode(yamlObject); - ToscaServiceTemplate serviceTemplate = gson.fromJson(policyTypeJsonDefinition, ToscaServiceTemplate.class); + ToscaServiceTemplate serviceTemplate = coder.decode(policyTypeJsonDefinition, ToscaServiceTemplate.class); policyTypeMap.put(new ToscaEntityKey(policyTypeDataArray[0], policyTypeDataArray[1]), serviceTemplate); new AuthorativeToscaProvider().createPolicyTypes(pfDao, serviceTemplate); } @@ -163,15 +163,15 @@ public class AuthorativeToscaProviderReferenceTest { } @Test - public void testPolicyTypeRead() throws PfModelException { + public void testPolicyTypeRead() throws PfModelException, CoderException { for (Entry policyTypeMapEntry : policyTypeMap.entrySet()) { ToscaServiceTemplate serviceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao, policyTypeMapEntry.getKey().getName(), policyTypeMapEntry.getKey().getVersion()); assertEquals(1, serviceTemplate.getPolicyTypes().size()); - String originalJson = gson.toJson(policyTypeMapEntry.getValue()); - String databaseJson = gson.toJson(serviceTemplate); + String originalJson = coder.encode(policyTypeMapEntry.getValue()); + String databaseJson = coder.encode(serviceTemplate); // TODO: This test has no chance of passing yet but must eventually pass to prove that the policy types // TODO: that were retrieved are the same as the policy types that were stored diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java index cfbbdb9d4..f4196811f 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java @@ -23,8 +23,6 @@ package org.onap.policy.models.provider.impl; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import com.google.gson.GsonBuilder; - import java.net.URISyntaxException; import java.util.Base64; import java.util.List; @@ -114,7 +112,7 @@ public class PolicyToscaPersistenceTest { private void testYamlStringPolicyPersistence(final String policyString) throws Exception { Object yamlObject = new Yaml().load(policyString); - String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject); + String yamlAsJsonString = new StandardCoder().encode(yamlObject); testJsonStringPolicyPersistence(yamlAsJsonString); } @@ -138,19 +136,20 @@ public class PolicyToscaPersistenceTest { ToscaServiceTemplate gotToscaServiceTemplate = databaseProvider.getPolicies(policy.getName(), policy.getVersion()); - assertEquals(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0) - .get(policy.getName()).getType(), policy.getType()); + assertEquals(policy.getType(), gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0) + .get(policy.getName()).getType()); gotToscaServiceTemplate = databaseProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build()); - assertEquals(getToscaPolicyFromMapList(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies(), - policy.getName()).getType(), policy.getType()); + assertEquals(policy.getType(), + getToscaPolicyFromMapList(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies(), + policy.getName()).getType()); gotToscaServiceTemplate = databaseProvider.getFilteredPolicies( ToscaPolicyFilter.builder().name(policy.getName()).version(policy.getVersion()).build()); - assertEquals(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0) - .get(policy.getName()).getType(), policy.getType()); + assertEquals(policy.getType(), gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0) + .get(policy.getName()).getType()); } } } @@ -159,8 +158,8 @@ public class PolicyToscaPersistenceTest { String policyName) { ToscaPolicy toscaPolicy = new ToscaPolicy(); for (Map policyMap : toscaPolicyMapList) { - if (policyMap.get(policyName) != null) { - toscaPolicy = policyMap.get(policyName); + toscaPolicy = policyMap.get(policyName); + if (toscaPolicy != null) { break; } } @@ -170,8 +169,8 @@ public class PolicyToscaPersistenceTest { private void createPolicyTypes() throws CoderException, PfModelException, URISyntaxException { Set policyTypeResources = ResourceUtils.getDirectoryContents("policytypes"); - for (String policyTyoeResource : policyTypeResources) { - Object yamlObject = new Yaml().load(ResourceUtils.getResourceAsString(policyTyoeResource)); + for (String policyTypeResource : policyTypeResources) { + Object yamlObject = new Yaml().load(ResourceUtils.getResourceAsString(policyTypeResource)); String yamlAsJsonString = new StandardCoder().encode(yamlObject); ToscaServiceTemplate toscaServiceTemplatePolicyType = diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java index fc8452b98..0ecf8a046 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java @@ -24,8 +24,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import com.google.gson.GsonBuilder; - import java.util.Base64; import java.util.List; import java.util.Set; @@ -100,7 +98,7 @@ public class PolicyTypePersistenceTest { private void testYamlStringPolicyTypePersistence(final String policyTypeString) throws Exception { Object yamlObject = new Yaml().load(policyTypeString); - String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject); + String yamlAsJsonString = new StandardCoder().encode(yamlObject); testJsonStringPolicyTypePersistence(yamlAsJsonString); } @@ -117,13 +115,14 @@ public class PolicyTypePersistenceTest { assertNotNull(serviceTemplate); ToscaPolicyType inPolicyType = serviceTemplate.getPolicyTypes().values().iterator().next(); - try { - databaseProvider.createPolicyTypes(serviceTemplate); - } catch (Exception e) { - e.printStackTrace(); - } + databaseProvider.createPolicyTypes(serviceTemplate); + checkPolicyTypePersistence(inPolicyType); + databaseProvider.updatePolicyTypes(serviceTemplate); + checkPolicyTypePersistence(inPolicyType); + } + private void checkPolicyTypePersistence(ToscaPolicyType inPolicyType) throws PfModelException { List policyTypeList = databaseProvider.getPolicyTypeList(inPolicyType.getName(), inPolicyType.getVersion()); diff --git a/models-provider/src/test/resources/logback-test.xml b/models-provider/src/test/resources/logback-test.xml new file mode 100644 index 000000000..fe59c89f1 --- /dev/null +++ b/models-provider/src/test/resources/logback-test.xml @@ -0,0 +1,50 @@ + + + + + + PolicyModels + + + + + + + %d %contextName [%t] %level %logger{36} - %msg%n + + + + + + + + + ${LOG_DIR}/policy-models.log + + %d %-5relative [procId=${processId}] [%thread] %-5level + %logger{26} - %msg %n %ex{full} + + + + + + + -- cgit 1.2.3-korg