X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-tosca%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fpolicy%2Fmodels%2Ftosca%2Fauthorative%2Fprovider%2FAuthorativeToscaProviderPolicyTest.java;h=8b79374bc66ed40920e2317b0600c5dc99866a21;hb=88bcb550c2efd5e43ad3d256fe075a6bf7e90538;hp=441bbcb185aa6cba04d2dafdf97becf9f0d9503d;hpb=942cd0a012a2a4e9fd44e680dc6c4c7f4bcf8c62;p=policy%2Fmodels.git diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java index 441bbcb18..8b79374bc 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * 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. @@ -25,14 +26,16 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import java.sql.Connection; -import java.sql.DriverManager; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.Properties; +import org.eclipse.persistence.config.PersistenceUnitProperties; 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.PfConceptKey; @@ -45,6 +48,7 @@ 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.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; +import org.yaml.snakeyaml.Yaml; /** * Test of the {@link AuthorativeToscaProvider} class. @@ -52,7 +56,12 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; * @author Liam Fallon (liam.fallon@est.tech) */ public class AuthorativeToscaProviderPolicyTest { - private Connection connection; + private static final String VERSION = "version"; + private static final String VCPE_JSON = "policies/vCPE.policy.monitoring.input.tosca.json"; + private static final String POLICY_AND_VERSION = "onap.restart.tca:1.0.0"; + private static final String POLICY1 = "onap.restart.tca"; + private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$"; + private static final String VERSION_100 = "1.0.0"; private PfDao pfDao; private StandardCoder standardCoder; @@ -63,17 +72,21 @@ public class AuthorativeToscaProviderPolicyTest { */ @Before public void setupDao() throws Exception { - // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database - // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance - connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY"); - final DaoParameters daoParameters = new DaoParameters(); - daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName()); + daoParameters.setPluginClass(DefaultPfDao.class.getName()); - // Use the persistence unit ToscaConceptTest to test towards the h2 database - // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance 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); } @@ -87,30 +100,30 @@ public class AuthorativeToscaProviderPolicyTest { } @After - public void teardown() throws Exception { + public void teardown() { pfDao.close(); - connection.close(); } @Test public void testPoliciesGet() throws Exception { assertThatThrownBy(() -> { new AuthorativeToscaProvider().getPolicies(null, null, null); - }).hasMessage("dao is marked @NonNull but is null"); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().getPolicyList(null, null, null); - }).hasMessage("dao is marked @NonNull but is null"); + }).hasMessageMatching(DAO_IS_NULL); - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), - ToscaServiceTemplate.class); + createPolicyTypes(); + + ToscaServiceTemplate toscaServiceTemplate = + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_JSON), ToscaServiceTemplate.class); assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); - PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + PfConceptKey policyKey = new PfConceptKey(POLICY_AND_VERSION); ToscaPolicy beforePolicy = toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); @@ -127,12 +140,11 @@ public class AuthorativeToscaProviderPolicyTest { assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); - List gotPolicyList = - new AuthorativeToscaProvider().getPolicyList(pfDao, "onap.restart.tca", "1.0.0"); + List gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, POLICY1, VERSION_100); assertEquals(1, gotPolicyList.size()); assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); - gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, "onap.restart.tca", null); + gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, POLICY1, null); assertEquals(1, gotPolicyList.size()); assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); @@ -140,47 +152,49 @@ public class AuthorativeToscaProviderPolicyTest { assertEquals(1, gotPolicyList.size()); assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); - gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, null, "1.0.0"); + gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, null, VERSION_100); assertEquals(1, gotPolicyList.size()); assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); - } + assertTrue(new AuthorativeToscaProvider().getPolicyList(pfDao, "Nonexistant", VERSION_100).isEmpty()); + } @Test public void testPoliciesGetFiltered() throws Exception { assertThatThrownBy(() -> { new AuthorativeToscaProvider().getFilteredPolicies(null, null); - }).hasMessage("dao is marked @NonNull but is null"); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().getFilteredPolicies(null, ToscaPolicyFilter.builder().build()); - }).hasMessage("dao is marked @NonNull but is null"); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().getFilteredPolicies(pfDao, null); - }).hasMessage("filter is marked @NonNull but is null"); + }).hasMessageMatching("^filter is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { new AuthorativeToscaProvider().getFilteredPolicyList(null, null); - }).hasMessage("dao is marked @NonNull but is null"); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().getFilteredPolicyList(null, ToscaPolicyFilter.builder().build()); - }).hasMessage("dao is marked @NonNull but is null"); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, null); - }).hasMessage("filter is marked @NonNull but is null"); + }).hasMessageMatching("^filter is marked .*on.*ull but is null$"); - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), - ToscaServiceTemplate.class); + createPolicyTypes(); + + ToscaServiceTemplate toscaServiceTemplate = + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_JSON), ToscaServiceTemplate.class); assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); - PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + PfConceptKey policyKey = new PfConceptKey(POLICY_AND_VERSION); ToscaPolicy beforePolicy = toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); @@ -205,14 +219,13 @@ public class AuthorativeToscaProviderPolicyTest { assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicies(pfDao, - ToscaPolicyFilter.builder().name(policyKey.getName()).version("1.0.0").build()); + ToscaPolicyFilter.builder().name(policyKey.getName()).version(VERSION_100).build()); gotPolicy = gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); - List gotPolicyList = - new AuthorativeToscaProvider().getPolicyList(pfDao, "onap.restart.tca", "1.0.0"); + List gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, POLICY1, VERSION_100); assertEquals(1, gotPolicyList.size()); assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); @@ -227,7 +240,7 @@ public class AuthorativeToscaProviderPolicyTest { assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); gotPolicyList = new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, - ToscaPolicyFilter.builder().name(policyKey.getName()).version("1.0.0").build()); + ToscaPolicyFilter.builder().name(policyKey.getName()).version(VERSION_100).build()); assertEquals(1, gotPolicyList.size()); assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); } @@ -236,25 +249,26 @@ public class AuthorativeToscaProviderPolicyTest { public void testPolicyCreate() throws Exception { assertThatThrownBy(() -> { new AuthorativeToscaProvider().createPolicies(null, null); - }).hasMessage("dao is marked @NonNull but is null"); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().createPolicies(null, new ToscaServiceTemplate()); - }).hasMessage("dao is marked @NonNull but is null"); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().createPolicies(pfDao, null); - }).hasMessage("serviceTemplate is marked @NonNull but is null"); + }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$"); - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), - ToscaServiceTemplate.class); + createPolicyTypes(); + + ToscaServiceTemplate toscaServiceTemplate = + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_JSON), ToscaServiceTemplate.class); assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); - PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + PfConceptKey policyKey = new PfConceptKey(POLICY_AND_VERSION); ToscaPolicy beforePolicy = toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); @@ -264,34 +278,34 @@ public class AuthorativeToscaProviderPolicyTest { assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); } - @Test public void testPolicyUpdate() throws Exception { assertThatThrownBy(() -> { new AuthorativeToscaProvider().createPolicies(null, null); - }).hasMessage("dao is marked @NonNull but is null"); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().updatePolicies(null, null); - }).hasMessage("dao is marked @NonNull but is null"); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().updatePolicies(null, new ToscaServiceTemplate()); - }).hasMessage("dao is marked @NonNull but is null"); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().updatePolicies(pfDao, null); - }).hasMessage("serviceTemplate is marked @NonNull but is null"); + }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$"); - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), - ToscaServiceTemplate.class); + createPolicyTypes(); + + ToscaServiceTemplate toscaServiceTemplate = + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_JSON), ToscaServiceTemplate.class); assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); - PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + PfConceptKey policyKey = new PfConceptKey(POLICY_AND_VERSION); ToscaPolicy beforePolicy = toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); @@ -313,41 +327,42 @@ public class AuthorativeToscaProviderPolicyTest { public void testPoliciesDelete() throws Exception { assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicy(null, null, null); - }).hasMessage("dao is marked @NonNull but is null"); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().deletePolicy(null, null, "version"); - }).hasMessage("dao is marked @NonNull but is null"); + new AuthorativeToscaProvider().deletePolicy(null, null, VERSION); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicy(null, "name", null); - }).hasMessage("dao is marked @NonNull but is null"); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().deletePolicy(null, "name", "version"); - }).hasMessage("dao is marked @NonNull but is null"); + new AuthorativeToscaProvider().deletePolicy(null, "name", VERSION); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicy(pfDao, null, null); - }).hasMessage("name is marked @NonNull but is null"); + }).hasMessageMatching("^name is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().deletePolicy(pfDao, null, "version"); - }).hasMessage("name is marked @NonNull but is null"); + new AuthorativeToscaProvider().deletePolicy(pfDao, null, VERSION); + }).hasMessageMatching("^name is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicy(pfDao, "name", null); - }).hasMessage("version is marked @NonNull but is null"); + }).hasMessageMatching("^version is marked .*on.*ull but is null$"); - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), - ToscaServiceTemplate.class); + createPolicyTypes(); + + ToscaServiceTemplate toscaServiceTemplate = + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_JSON), ToscaServiceTemplate.class); assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); - PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + PfConceptKey policyKey = new PfConceptKey(POLICY_AND_VERSION); ToscaPolicy beforePolicy = toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); @@ -364,19 +379,20 @@ public class AuthorativeToscaProviderPolicyTest { assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); assertTrue(beforePolicy.getType().equals(deletedPolicy.getType())); - ToscaServiceTemplate gotServiceTemplate = - new AuthorativeToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion()); - - assertEquals(0, gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).size()); + // @formatter:off + assertThatThrownBy( + () -> new AuthorativeToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion())) + .hasMessageMatching("policies for onap.restart.tca:1.0.0 do not exist"); + // @formatter:on } @Test - public void testAssertPoliciesExist() throws PfModelException { + public void testAssertPoliciesExist() { ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate(); assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicy(pfDao, "name", null); - }).hasMessage("version is marked @NonNull but is null"); + }).hasMessageMatching("^version is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate); @@ -392,4 +408,59 @@ public class AuthorativeToscaProviderPolicyTest { new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate); }).hasMessage("An incoming list of concepts must have at least one entry"); } + + @Test + public void testEntityMaps() 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); + new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType); + + assertEquals(3, toscaServiceTemplatePolicyType.getDataTypesAsMap().size()); + assertEquals(2, toscaServiceTemplatePolicyType.getPolicyTypesAsMap().size()); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), + ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + + PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + + ToscaPolicy beforePolicy = + toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + ToscaPolicy createdPolicy = + createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); + assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + + assertEquals(1, toscaServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size()); + assertEquals(1, createdServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size()); + + Map policyMapItem = createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0); + createdServiceTemplate.getToscaTopologyTemplate().getPolicies().add(policyMapItem); + + assertThatThrownBy(() -> { + createdServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap(); + }).hasMessageContaining("list of map of entities contains more than one entity with key"); + } + + 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); + new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType); + } }