diff options
author | liamfallon <liam.fallon@est.tech> | 2020-01-12 17:49:25 +0000 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2020-01-13 15:23:33 +0000 |
commit | b321d781b99a869e179b8003260f92969a81b43e (patch) | |
tree | a5fc97d476b3295b71b4d65ec25518a33efaee88 /models-provider | |
parent | 93fb25deaadd3df511844fdf735c35cff51b4364 (diff) |
Read resource dirs in models prvider unit tests
Update the unit tests in models-provider so that we can check that the
TOSCA artifact that is read from the database is the same as the TOSCA
artifact that is stored.
This review does some groundwork for that check.
Issue-ID: POLICY-2315
Change-Id: Iabc7b247736b862713585a493752bc254cfe1c3d
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-provider')
6 files changed, 296 insertions, 411 deletions
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 new file mode 100644 index 000000000..57a4d8105 --- /dev/null +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/AuthorativeToscaProviderReferenceTest.java @@ -0,0 +1,181 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019-2020 Nordix Foundation. + * Modifications 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +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; +import java.util.Properties; + +import org.eclipse.persistence.config.PersistenceUnitProperties; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.dao.DaoParameters; +import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.dao.PfDaoFactory; +import org.onap.policy.models.dao.impl.DefaultPfDao; +import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider; +import org.yaml.snakeyaml.Yaml; + +/** + * Test of the {@link AuthorativeToscaProvider} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class AuthorativeToscaProviderReferenceTest { + private static PfDao pfDao; + private static final Gson gson = new GsonBuilder().setPrettyPrinting().create(); + + // @formatter:off + private static final String[] EXAMPLE_POLICY_TYPES = { + "onap.policies.controlloop.guard.Blacklist#1.0.0" + + "#policytypes/onap.policies.controlloop.guard.Blacklist.yaml", + "onap.policies.controlloop.guard.coordination.FirstBlocksSecond#1.0.0" + + "#policytypes/onap.policies.controlloop.guard.coordination.FirstBlocksSecond.yaml", + "onap.policies.controlloop.guard.FrequencyLimiter#1.0.0" + + "#policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml", + "onap.policies.controlloop.guard.MinMax#1.0.0" + + "#policytypes/onap.policies.controlloop.guard.MinMax.yaml", + "onap.policies.controlloop.operational.Common#1.0.0" + + "#policytypes/onap.policies.controlloop.operational.Common.yaml", + "onap.policies.controlloop.Operational#1.0.0" + + "#policytypes/onap.policies.controlloop.Operational.yaml", + "onap.policies.drools.Controller#1.0.0" + + "#policytypes/onap.policies.drools.Controller.yaml", + "onap.policies.monitoring.cdap.tca.hi.lo.app#1.0.0" + + "#policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml", + "onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server#1.0.0" + + "#policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml", + "onap.policies.Naming#1.0.0" + + "#policytypes/onap.policies.Naming.yaml", + "onap.policies.native.Apex#1.0.0" + + "#policytypes/onap.policies.native.Apex.yaml", + "onap.policies.native.Drools#1.0.0" + + "#policytypes/onap.policies.native.Drools.yaml", + "onap.policies.native.Xacml#1.0.0" + + "#policytypes/onap.policies.native.Xacml.yaml", + "onap.policies.optimization.resource.AffinityPolicy#1.0.0" + + "#policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml", + "onap.policies.optimization.resource.DistancePolicy#1.0.0" + + "#policytypes/onap.policies.optimization.resource.DistancePolicy.yaml", + "onap.policies.optimization.resource.HpaPolicy#1.0.0" + + "#policytypes/onap.policies.optimization.resource.HpaPolicy.yaml", + "onap.policies.optimization.resource.OptimizationPolicy#1.0.0" + + "#policytypes/onap.policies.optimization.resource.OptimizationPolicy.yaml", + "onap.policies.optimization.resource.PciPolicy#1.0.0" + + "#policytypes/onap.policies.optimization.resource.PciPolicy.yaml", + "onap.policies.optimization.resource.Vim_fit#1.0.0" + + "#policytypes/onap.policies.optimization.resource.Vim_fit.yaml", + "onap.policies.optimization.resource.VnfPolicy#1.0.0" + + "#policytypes/onap.policies.optimization.resource.VnfPolicy.yaml", + "onap.policies.optimization.Resource#1.0.0" + + "#policytypes/onap.policies.optimization.Resource.yaml", + "onap.policies.optimization.service.QueryPolicy#1.0.0" + + "#policytypes/onap.policies.optimization.service.QueryPolicy.yaml", + "onap.policies.optimization.service.SubscriberPolicy#1.0.0" + + "#policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml", + "onap.policies.optimization.Service#1.0.0" + + "#policytypes/onap.policies.optimization.Service.yaml", + "onap.policies.Optimization#1.0.0" + + "#policytypes/onap.policies.Optimization.yaml"}; + // @formatter:on + + private static final Map<ToscaEntityKey, ToscaServiceTemplate> policyTypeMap = new LinkedHashMap<>(); + + /** + * Set up the DAO towards the database. + * + * @throws Exception on database errors + */ + @BeforeClass + public static void beforeSetupDao() throws Exception { + final DaoParameters daoParameters = new DaoParameters(); + daoParameters.setPluginClass(DefaultPfDao.class.getName()); + + daoParameters.setPersistenceUnit("ToscaConceptTest"); + + Properties jdbcProperties = new Properties(); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy"); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY"); + + // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver"); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb"); + + daoParameters.setJdbcProperties(jdbcProperties); + + pfDao = new PfDaoFactory().createPfDao(daoParameters); + pfDao.init(daoParameters); + } + + /** + * Populate the database. + * + * @throws PfModelException on database exceptions + */ + @BeforeClass + public static void beforeFillDatabase() throws PfModelException { + 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); + + ToscaServiceTemplate serviceTemplate = gson.fromJson(policyTypeJsonDefinition, ToscaServiceTemplate.class); + policyTypeMap.put(new ToscaEntityKey(policyTypeDataArray[0], policyTypeDataArray[1]), serviceTemplate); + new AuthorativeToscaProvider().createPolicyTypes(pfDao, serviceTemplate); + } + } + + @AfterClass + public static void afterTeardown() { + pfDao.close(); + } + + @Test + public void testPolicyTypeRead() throws PfModelException { + for (Entry<ToscaEntityKey, ToscaServiceTemplate> 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); + + // 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 + // assertEquals(originalJson, databaseJson); + } + } +} diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyGuardPersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyGuardPersistenceTest.java index ef83ea669..83665b078 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyGuardPersistenceTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyGuardPersistenceTest.java @@ -22,10 +22,11 @@ package org.onap.policy.models.provider.impl; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; +import java.net.URISyntaxException; import java.util.Base64; import java.util.Map; +import java.util.Set; import lombok.NonNull; @@ -42,8 +43,6 @@ import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.Yaml; /** @@ -52,9 +51,6 @@ import org.yaml.snakeyaml.Yaml; * @author Liam Fallon (liam.fallon@est.tech) */ public class PolicyLegacyGuardPersistenceTest { - // Logger for this class - private static final Logger LOGGER = LoggerFactory.getLogger(PolicyLegacyGuardPersistenceTest.class); - private StandardCoder standardCoder; private PolicyModelsProvider databaseProvider; @@ -107,16 +103,11 @@ public class PolicyLegacyGuardPersistenceTest { } @Test - public void testPolicyPersistence() { - try { - for (int i = 0; i < policyInputResourceNames.length; i++) { - String policyInputString = ResourceUtils.getResourceAsString(policyInputResourceNames[i]); - String policyOutputString = ResourceUtils.getResourceAsString(policyOutputResourceNames[i]); - testJsonStringPolicyPersistence(policyInputString, policyOutputString); - } - } catch (Exception exc) { - LOGGER.warn("error processing policies", exc); - fail("test should not throw an exception"); + public void testPolicyPersistence() throws Exception { + for (int i = 0; i < policyInputResourceNames.length; i++) { + String policyInputString = ResourceUtils.getResourceAsString(policyInputResourceNames[i]); + String policyOutputString = ResourceUtils.getResourceAsString(policyOutputResourceNames[i]); + testJsonStringPolicyPersistence(policyInputString, policyOutputString); } } @@ -135,23 +126,19 @@ public class PolicyLegacyGuardPersistenceTest { Map<String, LegacyGuardPolicyOutput> createdGopm = databaseProvider.createGuardPolicy(gip); assertEquals(gip.getPolicyId(), createdGopm.keySet().iterator().next()); - assertEquals(gip.getContent(), - createdGopm.get(gip.getPolicyId()).getProperties().values().iterator().next()); + assertEquals(gip.getContent(), createdGopm.get(gip.getPolicyId()).getProperties().values().iterator().next()); Map<String, LegacyGuardPolicyOutput> gotGopm = databaseProvider.getGuardPolicy(gip.getPolicyId(), null); assertEquals(gip.getPolicyId(), gotGopm.keySet().iterator().next()); - assertEquals(gip.getContent(), - gotGopm.get(gip.getPolicyId()).getProperties().values().iterator().next()); + assertEquals(gip.getContent(), gotGopm.get(gip.getPolicyId()).getProperties().values().iterator().next()); Map<String, LegacyGuardPolicyOutput> updatedGopm = databaseProvider.updateGuardPolicy(gip); assertEquals(gip.getPolicyId(), updatedGopm.keySet().iterator().next()); - assertEquals(gip.getContent(), - updatedGopm.get(gip.getPolicyId()).getProperties().values().iterator().next()); + assertEquals(gip.getContent(), updatedGopm.get(gip.getPolicyId()).getProperties().values().iterator().next()); Map<String, LegacyGuardPolicyOutput> deletedGopm = databaseProvider.deleteGuardPolicy(gip.getPolicyId(), "1"); assertEquals(gip.getPolicyId(), deletedGopm.keySet().iterator().next()); - assertEquals(gip.getContent(), - deletedGopm.get(gip.getPolicyId()).getProperties().values().iterator().next()); + assertEquals(gip.getContent(), deletedGopm.get(gip.getPolicyId()).getProperties().values().iterator().next()); String actualRetrievedJson = standardCoder.encode(gotGopm); @@ -159,24 +146,18 @@ public class PolicyLegacyGuardPersistenceTest { assertEquals(policyOutputString.replaceAll("\\s+", ""), actualRetrievedJson.replaceAll("\\s+", "")); } - private void createPolicyTypes() throws CoderException, PfModelException { - Object yamlObject = new Yaml().load( - ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml")); - String yamlAsJsonString = new StandardCoder().encode(yamlObject); - - ToscaServiceTemplate toscaServiceTemplatePolicyType = - standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); + private void createPolicyTypes() throws CoderException, PfModelException, URISyntaxException { + Set<String> policyTypeResources = ResourceUtils.getDirectoryContents("policytypes"); - assertNotNull(toscaServiceTemplatePolicyType); - databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType); + for (String policyTyoeResource : policyTypeResources) { + Object yamlObject = new Yaml().load(ResourceUtils.getResourceAsString(policyTyoeResource)); + String yamlAsJsonString = new StandardCoder().encode(yamlObject); - yamlObject = new Yaml().load( - ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.MinMax.yaml")); - yamlAsJsonString = new StandardCoder().encode(yamlObject); + ToscaServiceTemplate toscaServiceTemplatePolicyType = + standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); - toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); - - assertNotNull(toscaServiceTemplatePolicyType); - databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType); + assertNotNull(toscaServiceTemplatePolicyType); + databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType); + } } } diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyOperationalPersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyOperationalPersistenceTest.java index e6ecf5ade..29e3b5a47 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyOperationalPersistenceTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyOperationalPersistenceTest.java @@ -22,9 +22,10 @@ package org.onap.policy.models.provider.impl; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; +import java.net.URISyntaxException; import java.util.Base64; +import java.util.Set; import lombok.NonNull; @@ -40,8 +41,6 @@ import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.Yaml; /** @@ -50,9 +49,6 @@ import org.yaml.snakeyaml.Yaml; * @author Liam Fallon (liam.fallon@est.tech) */ public class PolicyLegacyOperationalPersistenceTest { - // Logger for this class - private static final Logger LOGGER = LoggerFactory.getLogger(PolicyLegacyOperationalPersistenceTest.class); - private StandardCoder standardCoder; private PolicyModelsProvider databaseProvider; @@ -107,16 +103,11 @@ public class PolicyLegacyOperationalPersistenceTest { } @Test - public void testPolicyPersistence() { - try { - for (int i = 0; i < policyInputResourceNames.length; i++) { - String policyInputString = ResourceUtils.getResourceAsString(policyInputResourceNames[i]); - String policyOutputString = ResourceUtils.getResourceAsString(policyOutputResourceNames[i]); - testJsonStringPolicyPersistence(policyInputString, policyOutputString); - } - } catch (Exception exc) { - LOGGER.warn("error processing policies", exc); - fail("test should not throw an exception"); + public void testPolicyPersistence() throws Exception { + for (int i = 0; i < policyInputResourceNames.length; i++) { + String policyInputString = ResourceUtils.getResourceAsString(policyInputResourceNames[i]); + String policyOutputString = ResourceUtils.getResourceAsString(policyOutputResourceNames[i]); + testJsonStringPolicyPersistence(policyInputString, policyOutputString); } } @@ -154,16 +145,18 @@ public class PolicyLegacyOperationalPersistenceTest { "'")); } - private void createPolicyTypes() throws CoderException, PfModelException { + private void createPolicyTypes() throws CoderException, PfModelException, URISyntaxException { + Set<String> policyTypeResources = ResourceUtils.getDirectoryContents("policytypes"); - Object yamlObject = new Yaml().load( - ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.Operational.yaml")); - String yamlAsJsonString = new StandardCoder().encode(yamlObject); + for (String policyTyoeResource : policyTypeResources) { + Object yamlObject = new Yaml().load(ResourceUtils.getResourceAsString(policyTyoeResource)); + String yamlAsJsonString = new StandardCoder().encode(yamlObject); - ToscaServiceTemplate toscaServiceTemplatePolicyType = - standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); + ToscaServiceTemplate toscaServiceTemplatePolicyType = + standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); - assertNotNull(toscaServiceTemplatePolicyType); - databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType); + assertNotNull(toscaServiceTemplatePolicyType); + databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType); + } } } diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyPersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyPersistenceTest.java deleted file mode 100644 index 8e0dcb31c..000000000 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyPersistenceTest.java +++ /dev/null @@ -1,229 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.models.provider.impl; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; - -import com.google.gson.GsonBuilder; - -import java.util.Base64; -import java.util.List; -import java.util.Map; - -import lombok.NonNull; - -import org.junit.After; -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.resources.ResourceUtils; -import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.provider.PolicyModelsProvider; -import org.onap.policy.models.provider.PolicyModelsProviderFactory; -import org.onap.policy.models.provider.PolicyModelsProviderParameters; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; -import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.yaml.snakeyaml.Yaml; - -/** - * Test persistence of monitoring policies to and from the database. - * - * @author Liam Fallon (liam.fallon@est.tech) - */ -public class PolicyPersistenceTest { - // Logger for this class - private static final Logger LOGGER = LoggerFactory.getLogger(PolicyPersistenceTest.class); - - private StandardCoder standardCoder; - - private PolicyModelsProvider databaseProvider; - - // @formatter:off - private String[] policyResourceNames = { - "policies/vCPE.policy.monitoring.input.tosca.json", - "policies/vCPE.policy.monitoring.input.tosca.yaml", - "policies/vCPE.policy.operational.input.tosca.yaml", - "policies/vDNS.policy.guard.frequency.input.tosca.json", - "policies/vDNS.policy.guard.frequency.input.tosca.yaml", - "policies/vDNS.policy.monitoring.input.tosca.json", - "policies/vDNS.policy.monitoring.input.tosca.yaml", - "policies/vDNS.policy.operational.input.tosca.yaml", - "policies/vFirewall.policy.monitoring.input.tosca.json", - "policies/vFirewall.policy.monitoring.input.tosca.yaml", - "policies/vFirewall.policy.operational.input.tosca.json", - "policies/vFirewall.policy.operational.input.tosca.yaml" - }; - // @formatter:on - - /** - * Initialize provider. - * - * @throws PfModelException on exceptions in the tests - * @throws CoderException on JSON encoding and decoding errors - */ - @Before - public void setupParameters() throws Exception { - // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB - - PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters(); - parameters.setDatabaseDriver("org.h2.Driver"); - parameters.setDatabaseUrl("jdbc:h2:mem:testdb"); - parameters.setDatabaseUser("policy"); - parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes())); - parameters.setPersistenceUnit("ToscaConceptTest"); - - databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); - - createPolicyTypes(); - } - - /** - * Set up GSON. - */ - @Before - public void setupGson() { - standardCoder = new StandardCoder(); - } - - @After - public void teardown() throws Exception { - databaseProvider.close(); - } - - @Test - public void testPolicyPersistence() { - try { - for (String policyResourceName : policyResourceNames) { - String policyString = ResourceUtils.getResourceAsString(policyResourceName); - - if (policyResourceName.endsWith("yaml")) { - testYamlStringPolicyPersistence(policyString); - } else { - testJsonStringPolicyPersistence(policyString); - } - } - } catch (Exception exc) { - LOGGER.warn("error processing policies", exc); - fail("test should not throw an exception"); - } - } - - private void testYamlStringPolicyPersistence(final String policyString) throws Exception { - Object yamlObject = new Yaml().load(policyString); - String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject); - - testJsonStringPolicyPersistence(yamlAsJsonString); - } - - /** - * Check persistence of a policy. - * - * @param policyString the policy as a string - * @throws Exception any exception thrown - */ - public void testJsonStringPolicyPersistence(@NonNull final String policyString) throws Exception { - ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); - - assertNotNull(serviceTemplate); - - databaseProvider.createPolicies(serviceTemplate); - databaseProvider.updatePolicies(serviceTemplate); - - for (Map<String, ToscaPolicy> policyMap : serviceTemplate.getToscaTopologyTemplate().getPolicies()) { - for (ToscaPolicy policy : policyMap.values()) { - ToscaServiceTemplate gotToscaServiceTemplate = - databaseProvider.getPolicies(policy.getName(), policy.getVersion()); - - assertEquals(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0) - .get(policy.getName()).getType(), policy.getType()); - - gotToscaServiceTemplate = databaseProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build()); - - assertEquals(getToscaPolicyFromMapList( - gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies(), policy.getName()).getType(), - policy.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()); - } - } - } - - private ToscaPolicy getToscaPolicyFromMapList(List<Map<String, ToscaPolicy>> toscaPolicyMapList, - String policyName) { - ToscaPolicy toscaPolicy = new ToscaPolicy(); - for (Map<String, ToscaPolicy> policyMap: toscaPolicyMapList) { - if (policyMap.get(policyName) != null) { - toscaPolicy = policyMap.get(policyName); - break; - } - } - return toscaPolicy; - } - - private void createPolicyTypes() throws CoderException, PfModelException { - Object yamlObject = new Yaml().load( - ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml")); - String yamlAsJsonString = new StandardCoder().encode(yamlObject); - - ToscaServiceTemplate toscaServiceTemplatePolicyType = - standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); - - assertNotNull(toscaServiceTemplatePolicyType); - databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType); - - yamlObject = new Yaml().load( - ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.Operational.yaml")); - yamlAsJsonString = new StandardCoder().encode(yamlObject); - - toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); - - assertNotNull(toscaServiceTemplatePolicyType); - databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType); - - yamlObject = new Yaml().load( - ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.operational.Common.yaml")); - yamlAsJsonString = new StandardCoder().encode(yamlObject); - - toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); - - assertNotNull(toscaServiceTemplatePolicyType); - databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType); - - yamlObject = new Yaml().load( - ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml")); - yamlAsJsonString = new StandardCoder().encode(yamlObject); - - toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); - - assertNotNull(toscaServiceTemplatePolicyType); - databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType); - } -} 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 1aedcaf83..cfbbdb9d4 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 @@ -22,11 +22,14 @@ package org.onap.policy.models.provider.impl; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; import com.google.gson.GsonBuilder; +import java.net.URISyntaxException; import java.util.Base64; +import java.util.List; +import java.util.Map; +import java.util.Set; import lombok.NonNull; @@ -41,9 +44,8 @@ import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.Yaml; /** @@ -52,30 +54,10 @@ import org.yaml.snakeyaml.Yaml; * @author Liam Fallon (liam.fallon@est.tech) */ public class PolicyToscaPersistenceTest { - // Logger for this class - private static final Logger LOGGER = LoggerFactory.getLogger(PolicyToscaPersistenceTest.class); - private StandardCoder standardCoder; private PolicyModelsProvider databaseProvider; - // @formatter:off - private String[] policyResourceNames = { - "policies/vCPE.policy.monitoring.input.tosca.json", - "policies/vCPE.policy.monitoring.input.tosca.yaml", - "policies/vCPE.policy.operational.input.tosca.yaml", - "policies/vDNS.policy.guard.frequency.input.tosca.json", - "policies/vDNS.policy.guard.frequency.input.tosca.yaml", - "policies/vDNS.policy.monitoring.input.tosca.json", - "policies/vDNS.policy.monitoring.input.tosca.yaml", - "policies/vDNS.policy.operational.input.tosca.yaml", - "policies/vFirewall.policy.monitoring.input.tosca.json", - "policies/vFirewall.policy.monitoring.input.tosca.yaml", - "policies/vFirewall.policy.operational.input.tosca.json", - "policies/vFirewall.policy.operational.input.tosca.yaml" - }; - // @formatter:on - /** * Initialize provider. * @@ -84,6 +66,8 @@ public class PolicyToscaPersistenceTest { */ @Before public void setupParameters() throws Exception { + // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB + PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters(); parameters.setDatabaseDriver("org.h2.Driver"); parameters.setDatabaseUrl("jdbc:h2:mem:testdb"); @@ -97,7 +81,7 @@ public class PolicyToscaPersistenceTest { } /** - * Set up the standard coder. + * Set up standard coder. */ @Before public void setupStandardCoder() { @@ -110,20 +94,21 @@ public class PolicyToscaPersistenceTest { } @Test - public void testPolicyPersistence() { - try { - for (String policyResourceName : policyResourceNames) { - String policyString = ResourceUtils.getResourceAsString(policyResourceName); - - if (policyResourceName.endsWith("yaml")) { - testYamlStringPolicyPersistence(policyString); - } else { - testJsonStringPolicyPersistence(policyString); - } + public void testPolicyPersistence() throws Exception { + Set<String> policyResources = ResourceUtils.getDirectoryContents("policies"); + + for (String policyResource : policyResources) { + if (!policyResource.contains("\\.tosca\\.")) { + continue; + } + + String policyString = ResourceUtils.getResourceAsString(policyResource); + + if (policyResource.endsWith("yaml")) { + testYamlStringPolicyPersistence(policyString); + } else { + testJsonStringPolicyPersistence(policyString); } - } catch (Exception exc) { - LOGGER.warn("error processing policy types", exc); - fail("test should not throw an exception"); } } @@ -146,51 +131,54 @@ public class PolicyToscaPersistenceTest { assertNotNull(serviceTemplate); databaseProvider.createPolicies(serviceTemplate); + databaseProvider.updatePolicies(serviceTemplate); - for (String policyKey : serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).keySet()) { - ToscaPolicy incomingPolicy = serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey); - ToscaPolicy databasePolicy = - databaseProvider.getPolicies(incomingPolicy.getName(), incomingPolicy.getVersion()) - .getToscaTopologyTemplate().getPolicies().get(0).get(policyKey); - assertEquals(incomingPolicy.getType(), databasePolicy.getType()); - } - } + for (Map<String, ToscaPolicy> policyMap : serviceTemplate.getToscaTopologyTemplate().getPolicies()) { + for (ToscaPolicy policy : policyMap.values()) { + ToscaServiceTemplate gotToscaServiceTemplate = + databaseProvider.getPolicies(policy.getName(), policy.getVersion()); - private void createPolicyTypes() throws CoderException, PfModelException { - Object yamlObject = new Yaml().load( - ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml")); - String yamlAsJsonString = new StandardCoder().encode(yamlObject); + assertEquals(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0) + .get(policy.getName()).getType(), policy.getType()); - ToscaServiceTemplate toscaServiceTemplatePolicyType = - standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); + gotToscaServiceTemplate = databaseProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build()); - assertNotNull(toscaServiceTemplatePolicyType); - databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType); + assertEquals(getToscaPolicyFromMapList(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies(), + policy.getName()).getType(), policy.getType()); - yamlObject = new Yaml().load( - ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.Operational.yaml")); - yamlAsJsonString = new StandardCoder().encode(yamlObject); + gotToscaServiceTemplate = databaseProvider.getFilteredPolicies( + ToscaPolicyFilter.builder().name(policy.getName()).version(policy.getVersion()).build()); - toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); - - assertNotNull(toscaServiceTemplatePolicyType); - databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType); + assertEquals(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0) + .get(policy.getName()).getType(), policy.getType()); + } + } + } - yamlObject = new Yaml().load( - ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.operational.Common.yaml")); - yamlAsJsonString = new StandardCoder().encode(yamlObject); + private ToscaPolicy getToscaPolicyFromMapList(List<Map<String, ToscaPolicy>> toscaPolicyMapList, + String policyName) { + ToscaPolicy toscaPolicy = new ToscaPolicy(); + for (Map<String, ToscaPolicy> policyMap : toscaPolicyMapList) { + if (policyMap.get(policyName) != null) { + toscaPolicy = policyMap.get(policyName); + break; + } + } + return toscaPolicy; + } - toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); + private void createPolicyTypes() throws CoderException, PfModelException, URISyntaxException { + Set<String> policyTypeResources = ResourceUtils.getDirectoryContents("policytypes"); - assertNotNull(toscaServiceTemplatePolicyType); - databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType); - yamlObject = new Yaml().load( - ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml")); - yamlAsJsonString = new StandardCoder().encode(yamlObject); + for (String policyTyoeResource : policyTypeResources) { + Object yamlObject = new Yaml().load(ResourceUtils.getResourceAsString(policyTyoeResource)); + String yamlAsJsonString = new StandardCoder().encode(yamlObject); - toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); + ToscaServiceTemplate toscaServiceTemplatePolicyType = + standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); - assertNotNull(toscaServiceTemplatePolicyType); - databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType); + assertNotNull(toscaServiceTemplatePolicyType); + databaseProvider.createPolicyTypes(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 75f21d2f8..fc8452b98 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 @@ -23,12 +23,12 @@ package org.onap.policy.models.provider.impl; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import com.google.gson.GsonBuilder; import java.util.Base64; import java.util.List; +import java.util.Set; import lombok.NonNull; @@ -44,8 +44,6 @@ import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.Yaml; /** @@ -54,35 +52,10 @@ import org.yaml.snakeyaml.Yaml; * @author Liam Fallon (liam.fallon@est.tech) */ public class PolicyTypePersistenceTest { - // Logger for this class - private static final Logger LOGGER = LoggerFactory.getLogger(PolicyTypePersistenceTest.class); - private StandardCoder standardCoder; private PolicyModelsProvider databaseProvider; - // @formatter:off - private String[] policyTypeResourceNames = { - "policytypes/onap.policies.controlloop.Operational.yaml", - "policytypes/onap.policies.optimization.resource.DistancePolicy.yaml", - "policytypes/onap.policies.optimization.resource.VnfPolicy.yaml", - "policytypes/onap.policies.optimization.resource.PciPolicy.yaml", - "policytypes/onap.policies.optimization.resource.OptimizationPolicy.yaml", - "policytypes/onap.policies.controlloop.guard.Blacklist.yaml", - "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml", - "policytypes/onap.policies.optimization.resource.HpaPolicy.yaml", - "policytypes/onap.policies.optimization.resource.Vim_fit.yaml", - "policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml", - "policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml", - "policytypes/onap.policies.optimization.service.QueryPolicy.yaml", - "policytypes/onap.policies.controlloop.guard.MinMax.yaml", - "policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml", - "policytypes/onap.policies.controlloop.guard.coordination.FirstBlocksSecond.yaml", - "policytypes/onap.policies.Optimization.yaml", - "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml" - }; - // @formatter:on - /** * Initialize provider. * @@ -90,6 +63,8 @@ public class PolicyTypePersistenceTest { */ @Before public void setupParameters() throws PfModelException { + // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB + PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters(); parameters.setDatabaseDriver("org.h2.Driver"); parameters.setDatabaseUrl("jdbc:h2:mem:testdb"); @@ -101,10 +76,10 @@ public class PolicyTypePersistenceTest { } /** - * Set up GSON. + * Set up standard coder. */ @Before - public void setupGson() { + public void setupStandardCoder() { standardCoder = new StandardCoder(); } @@ -114,20 +89,12 @@ public class PolicyTypePersistenceTest { } @Test - public void testPolicyTypePersistence() { - try { - for (String policyTypeResourceName : policyTypeResourceNames) { - String policyTypeString = ResourceUtils.getResourceAsString(policyTypeResourceName); - - if (policyTypeResourceName.endsWith("yaml")) { - testYamlStringPolicyTypePersistence(policyTypeString); - } else { - testJsonStringPolicyTypePersistence(policyTypeString); - } - } - } catch (Exception exc) { - LOGGER.warn("error processing policy types", exc); - fail("test should not throw an exception"); + public void testPolicyTypePersistence() throws Exception { + Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes"); + + for (String policyTypeFilePath : policyTypeDirectoryContents) { + String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath); + testYamlStringPolicyTypePersistence(policyTypeString); } } @@ -150,7 +117,11 @@ public class PolicyTypePersistenceTest { assertNotNull(serviceTemplate); ToscaPolicyType inPolicyType = serviceTemplate.getPolicyTypes().values().iterator().next(); - databaseProvider.createPolicyTypes(serviceTemplate); + try { + databaseProvider.createPolicyTypes(serviceTemplate); + } catch (Exception e) { + e.printStackTrace(); + } databaseProvider.updatePolicyTypes(serviceTemplate); List<ToscaPolicyType> policyTypeList = @@ -172,7 +143,7 @@ public class PolicyTypePersistenceTest { assertTrue(policyTypeList.size() <= 3); assertEquals(inPolicyType.getName(), policyTypeList.get(0).getName()); - for (ToscaPolicyType policyType: databaseProvider.getPolicyTypeList(null, null)) { + for (ToscaPolicyType policyType : databaseProvider.getPolicyTypeList(null, null)) { databaseProvider.deletePolicyType(policyType.getName(), policyType.getVersion()); } } |