From 88bcb550c2efd5e43ad3d256fe075a6bf7e90538 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Tue, 11 Feb 2020 16:18:32 +0000 Subject: Implement validation and hierarchical get NOTE: This review will require changes to the persistence.xml file in other policy framework components. This change brings in two changes: 1) Validation of the references betwen policies, policy types, and data types This change mans that additions to and changes to policies, policy types, and data types must be structurally correct in the change coming in and with whatever is already in the database 2) Hierarchical return of referenced entitites on policy types, policies and data types. When a policy, policy type, or data type is read from the database, the entities referenced byt the returned policy, policy type, or data type are returned as well. Tests are amended for the stricter validation and structure. Examples are corrected where they are incorrect. Issue-ID: POLICY-1402 Change-Id: Ie6a4cb7ed336562338924079114df405f0ab889f Signed-off-by: liamfallon --- .../AuthorativeToscaProviderReferenceTest.java | 181 ------ .../impl/DatabasePolicyModelsProviderTest.java | 669 +++++++++++---------- .../impl/DummyPolicyModelsProviderTest.java | 80 ++- .../impl/PolicyLegacyGuardPersistenceTest.java | 4 +- .../PolicyLegacyOperationalPersistenceTest.java | 4 +- .../provider/impl/PolicyToscaPersistenceTest.java | 4 +- .../provider/impl/PolicyTypePersistenceTest.java | 108 ++-- .../src/test/resources/META-INF/persistence.xml | 7 +- 8 files changed, 430 insertions(+), 627 deletions(-) delete mode 100644 models-provider/src/test/java/org/onap/policy/models/provider/impl/AuthorativeToscaProviderReferenceTest.java (limited to 'models-provider/src') 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 deleted file mode 100644 index 5f904dfb4..000000000 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/AuthorativeToscaProviderReferenceTest.java +++ /dev/null @@ -1,181 +0,0 @@ -/*- - * ============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 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.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; -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 StandardCoder coder = new StandardCoder(); - - // @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 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 - * @throws CoderException on JSON encoding/decoding errors - */ - @BeforeClass - 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 = coder.encode(yamlObject); - - ToscaServiceTemplate serviceTemplate = coder.decode(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, 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 = 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 - // assertEquals(originalJson, databaseJson); - } - } -} diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java index fd566a52f..aa6802a5a 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java @@ -25,14 +25,15 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; 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 java.util.ArrayList; import java.util.Base64; import java.util.Date; import java.util.List; + import org.junit.Before; import org.junit.Test; +import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.pdp.concepts.Pdp; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroupFilter; @@ -49,8 +50,6 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifi 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.LegacyOperationalPolicy; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * Test the database models provider implementation. @@ -82,8 +81,6 @@ public class DatabasePolicyModelsProviderTest { private static final String ORDER = "DESC"; - private static final Logger LOGGER = LoggerFactory.getLogger(DatabasePolicyModelsProviderTest.class); - PolicyModelsProviderParameters parameters; /** @@ -139,210 +136,210 @@ public class DatabasePolicyModelsProviderTest { @Test public void testProviderMethodsNull() throws Exception { - try (PolicyModelsProvider databaseProvider = - new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) { + PolicyModelsProvider databaseProvider = + new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); - assertThatThrownBy(() -> { - databaseProvider.getFilteredPolicyTypes(null); - }).hasMessageMatching(FILTER_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.getFilteredPolicyTypes(null); + }).hasMessageMatching(FILTER_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.getFilteredPolicyTypeList(null); - }).hasMessageMatching(FILTER_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.getFilteredPolicyTypeList(null); + }).hasMessageMatching(FILTER_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.createPolicyTypes(null); - }).hasMessageMatching(TEMPLATE_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.createPolicyTypes(null); + }).hasMessageMatching(TEMPLATE_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.updatePolicyTypes(null); - }).hasMessageMatching(TEMPLATE_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.updatePolicyTypes(null); + }).hasMessageMatching(TEMPLATE_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.deletePolicyType(null, null); - }).hasMessageMatching(NAME_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.deletePolicyType(null, null); + }).hasMessageMatching(NAME_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.deletePolicyType("aaa", null); - }).hasMessageMatching("^version is marked .*on.*ull but is null$"); + assertThatThrownBy(() -> { + databaseProvider.deletePolicyType("aaa", null); + }).hasMessageMatching("^version is marked .*on.*ull but is null$"); - assertThatThrownBy(() -> { - databaseProvider.deletePolicyType(null, "aaa"); - }).hasMessageMatching(NAME_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.deletePolicyType(null, "aaa"); + }).hasMessageMatching(NAME_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.getFilteredPolicies(null); - }).hasMessageMatching(FILTER_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.getFilteredPolicies(null); + }).hasMessageMatching(FILTER_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.getFilteredPolicyList(null); - }).hasMessageMatching(FILTER_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.getFilteredPolicyList(null); + }).hasMessageMatching(FILTER_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.createPolicies(null); - }).hasMessageMatching(TEMPLATE_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.createPolicies(null); + }).hasMessageMatching(TEMPLATE_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.updatePolicies(null); - }).hasMessageMatching(TEMPLATE_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.updatePolicies(null); + }).hasMessageMatching(TEMPLATE_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.deletePolicy(null, null); - }).hasMessageMatching(NAME_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.deletePolicy(null, null); + }).hasMessageMatching(NAME_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.deletePolicy(null, "aaa"); - }).hasMessageMatching(NAME_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.deletePolicy(null, "aaa"); + }).hasMessageMatching(NAME_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.deletePolicy("aaa", null); - }).hasMessageMatching("^version is marked .*on.*ull but is null$"); + assertThatThrownBy(() -> { + databaseProvider.deletePolicy("aaa", null); + }).hasMessageMatching("^version is marked .*on.*ull but is null$"); - assertThatThrownBy(() -> { - databaseProvider.getOperationalPolicy(null, null); - }).hasMessageMatching(POLICY_ID_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.getOperationalPolicy(null, null); + }).hasMessageMatching(POLICY_ID_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.getOperationalPolicy(null, ""); - }).hasMessageMatching(POLICY_ID_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.getOperationalPolicy(null, ""); + }).hasMessageMatching(POLICY_ID_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.getOperationalPolicy("", null); - }).hasMessage("no policy found for policy: :null"); + assertThatThrownBy(() -> { + databaseProvider.getOperationalPolicy("", null); + }).hasMessage("no policy found for policy: :null"); - assertThatThrownBy(() -> { - databaseProvider.createOperationalPolicy(null); - }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$"); + assertThatThrownBy(() -> { + databaseProvider.createOperationalPolicy(null); + }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$"); - assertThatThrownBy(() -> { - databaseProvider.updateOperationalPolicy(null); - }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$"); + assertThatThrownBy(() -> { + databaseProvider.updateOperationalPolicy(null); + }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$"); - assertThatThrownBy(() -> { - databaseProvider.deleteOperationalPolicy(null, null); - }).hasMessageMatching(POLICY_ID_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.deleteOperationalPolicy(null, null); + }).hasMessageMatching(POLICY_ID_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.deleteOperationalPolicy(null, ""); - }).hasMessageMatching(POLICY_ID_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.deleteOperationalPolicy(null, ""); + }).hasMessageMatching(POLICY_ID_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.deleteOperationalPolicy("", null); - }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$"); + assertThatThrownBy(() -> { + databaseProvider.deleteOperationalPolicy("", null); + }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$"); - assertThatThrownBy(() -> { - databaseProvider.getGuardPolicy(null, null); - }).hasMessageMatching(POLICY_ID_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.getGuardPolicy(null, null); + }).hasMessageMatching(POLICY_ID_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.getGuardPolicy(null, ""); - }).hasMessageMatching(POLICY_ID_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.getGuardPolicy(null, ""); + }).hasMessageMatching(POLICY_ID_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.getGuardPolicy("", null); - }).hasMessage("no policy found for policy: :null"); + assertThatThrownBy(() -> { + databaseProvider.getGuardPolicy("", null); + }).hasMessage("no policy found for policy: :null"); - assertThatThrownBy(() -> { - databaseProvider.createGuardPolicy(null); - }).hasMessageMatching("^legacyGuardPolicy is marked .*on.*ull but is null$"); + assertThatThrownBy(() -> { + databaseProvider.createGuardPolicy(null); + }).hasMessageMatching("^legacyGuardPolicy is marked .*on.*ull but is null$"); - assertThatThrownBy(() -> { - databaseProvider.updateGuardPolicy(null); - }).hasMessageMatching("^legacyGuardPolicy is marked .*on.*ull but is null$"); + assertThatThrownBy(() -> { + databaseProvider.updateGuardPolicy(null); + }).hasMessageMatching("^legacyGuardPolicy is marked .*on.*ull but is null$"); - assertThatThrownBy(() -> { - databaseProvider.deleteGuardPolicy(null, null); - }).hasMessageMatching(POLICY_ID_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.deleteGuardPolicy(null, null); + }).hasMessageMatching(POLICY_ID_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.deleteGuardPolicy(null, ""); - }).hasMessageMatching(POLICY_ID_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.deleteGuardPolicy(null, ""); + }).hasMessageMatching(POLICY_ID_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.deleteGuardPolicy("", null); - }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$"); + assertThatThrownBy(() -> { + databaseProvider.deleteGuardPolicy("", null); + }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$"); - assertThatThrownBy(() -> { - databaseProvider.getFilteredPdpGroups(null); - }).hasMessageMatching(FILTER_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.getFilteredPdpGroups(null); + }).hasMessageMatching(FILTER_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.createPdpGroups(null); - }).hasMessageMatching("^pdpGroups is marked .*on.*ull but is null$"); + assertThatThrownBy(() -> { + databaseProvider.createPdpGroups(null); + }).hasMessageMatching("^pdpGroups is marked .*on.*ull but is null$"); - assertThatThrownBy(() -> { - databaseProvider.updatePdpGroups(null); - }).hasMessageMatching("^pdpGroups is marked .*on.*ull but is null$"); + assertThatThrownBy(() -> { + databaseProvider.updatePdpGroups(null); + }).hasMessageMatching("^pdpGroups is marked .*on.*ull but is null$"); - assertThatThrownBy(() -> { - databaseProvider.updatePdpSubGroup(null, null); - }).hasMessageMatching(GROUP_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.updatePdpSubGroup(null, null); + }).hasMessageMatching(GROUP_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.updatePdpSubGroup(null, new PdpSubGroup()); - }).hasMessageMatching(GROUP_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.updatePdpSubGroup(null, new PdpSubGroup()); + }).hasMessageMatching(GROUP_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.updatePdpSubGroup(NAME, null); - }).hasMessageMatching(SUBGROUP_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.updatePdpSubGroup(NAME, null); + }).hasMessageMatching(SUBGROUP_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.updatePdpSubGroup(NAME, new PdpSubGroup()); - }).hasMessage("parameter \"localName\" is null"); + assertThatThrownBy(() -> { + databaseProvider.updatePdpSubGroup(NAME, new PdpSubGroup()); + }).hasMessage("parameter \"localName\" is null"); - assertThatThrownBy(() -> { - databaseProvider.updatePdp(null, null, null); - }).hasMessageMatching(GROUP_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.updatePdp(null, null, null); + }).hasMessageMatching(GROUP_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.updatePdp(null, null, new Pdp()); - }).hasMessageMatching(GROUP_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.updatePdp(null, null, new Pdp()); + }).hasMessageMatching(GROUP_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.updatePdp(null, "sub", null); - }).hasMessageMatching(GROUP_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.updatePdp(null, "sub", null); + }).hasMessageMatching(GROUP_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.updatePdp(null, "sub", new Pdp()); - }).hasMessageMatching(GROUP_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.updatePdp(null, "sub", new Pdp()); + }).hasMessageMatching(GROUP_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.updatePdp(NAME, null, null); - }).hasMessageMatching(SUBGROUP_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.updatePdp(NAME, null, null); + }).hasMessageMatching(SUBGROUP_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.updatePdp(NAME, null, new Pdp()); - }).hasMessageMatching(SUBGROUP_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.updatePdp(NAME, null, new Pdp()); + }).hasMessageMatching(SUBGROUP_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.updatePdp(NAME, "sub", null); - }).hasMessageMatching("^pdp is marked .*on.*ull but is null$"); + assertThatThrownBy(() -> { + databaseProvider.updatePdp(NAME, "sub", null); + }).hasMessageMatching("^pdp is marked .*on.*ull but is null$"); - assertThatThrownBy(() -> { - databaseProvider.updatePdp(NAME, "sub", new Pdp()); - }).hasMessage("parameter \"localName\" is null"); + assertThatThrownBy(() -> { + databaseProvider.updatePdp(NAME, "sub", new Pdp()); + }).hasMessage("parameter \"localName\" is null"); - assertThatThrownBy(() -> { - databaseProvider.deletePdpGroup(null); - }).hasMessageMatching(NAME_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.deletePdpGroup(null); + }).hasMessageMatching(NAME_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.getFilteredPdpStatistics(NAME, null, "sub", TIMESTAMP, TIMESTAMP, ORDER, 0); - }).hasMessageMatching(GROUP_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.getFilteredPdpStatistics(NAME, null, "sub", TIMESTAMP, TIMESTAMP, ORDER, 0); + }).hasMessageMatching(GROUP_IS_NULL); - assertThatThrownBy(() -> { - databaseProvider.createPdpStatistics(null); - }).hasMessageMatching("^pdpStatisticsList is marked .*on.*ull but is null$"); + assertThatThrownBy(() -> { + databaseProvider.createPdpStatistics(null); + }).hasMessageMatching("^pdpStatisticsList is marked .*on.*ull but is null$"); - assertThatThrownBy(() -> { - databaseProvider.updatePdpStatistics(null); - }).hasMessageMatching("^pdpStatisticsList is marked .*on.*ull but is null$"); + assertThatThrownBy(() -> { + databaseProvider.updatePdpStatistics(null); + }).hasMessageMatching("^pdpStatisticsList is marked .*on.*ull but is null$"); - assertThatThrownBy(() -> { - databaseProvider.deletePdpStatistics(null, TIMESTAMP); - }).hasMessageMatching(NAME_IS_NULL); + assertThatThrownBy(() -> { + databaseProvider.deletePdpStatistics(null, TIMESTAMP); + }).hasMessageMatching(NAME_IS_NULL); - } + databaseProvider.close(); } @Test @@ -358,181 +355,185 @@ public class DatabasePolicyModelsProviderTest { } @Test - public void testProviderMethods() { - try (PolicyModelsProvider databaseProvider = - new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) { - - assertTrue(databaseProvider.getPolicyTypes(NAME, VERSION_100).getPolicyTypes().isEmpty()); - assertTrue(databaseProvider.getPolicyTypeList(NAME, VERSION_100).isEmpty()); - assertEquals(0, databaseProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build()) - .getPolicyTypes().size()); - assertEquals(0, databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build()).size()); - - assertThatThrownBy(() -> { - databaseProvider.createPolicyTypes(new ToscaServiceTemplate()); - }).hasMessage("no policy types specified on service template"); - - assertThatThrownBy(() -> { - databaseProvider.updatePolicyTypes(new ToscaServiceTemplate()); - }).hasMessage("no policy types specified on service template"); - - assertTrue(databaseProvider.deletePolicyType(NAME, VERSION_100).getPolicyTypes().isEmpty()); - - assertTrue(databaseProvider.deletePolicyType(NAME, VERSION_100).getPolicyTypes().isEmpty()); - - assertTrue( - databaseProvider.getPolicies(NAME, VERSION_100).getToscaTopologyTemplate().getPolicies().isEmpty()); - assertTrue(databaseProvider.getPolicyList(NAME, VERSION_100).isEmpty()); - assertEquals(0, databaseProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build()) - .getToscaTopologyTemplate().getPolicies().size()); - assertEquals(0, databaseProvider.getFilteredPolicyList(ToscaPolicyFilter.builder().build()).size()); - - assertThatThrownBy(() -> { - databaseProvider.createPolicies(new ToscaServiceTemplate()); - }).hasMessage("topology template not specified on service template"); - - assertThatThrownBy(() -> { - databaseProvider.updatePolicies(new ToscaServiceTemplate()); - }).hasMessage("topology template not specified on service template"); - - assertTrue(databaseProvider.deletePolicy("Policy", "0.0.0").getToscaTopologyTemplate().getPolicies() - .isEmpty()); - - assertThatThrownBy(() -> { - databaseProvider.getOperationalPolicy(POLICY_ID, null); - }).hasMessage("no policy found for policy: policy_id:null"); - - assertThatThrownBy(() -> { - databaseProvider.getOperationalPolicy(POLICY_ID, "10"); - }).hasMessage("no policy found for policy: policy_id:10"); - - assertThatThrownBy(() -> { - databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy()); - }).hasMessageMatching(NAME_IS_NULL); - - assertThatThrownBy(() -> { - databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy()); - }).hasMessageMatching(NAME_IS_NULL); - - assertThatThrownBy(() -> { - databaseProvider.deleteOperationalPolicy(POLICY_ID, "55"); - }).hasMessage("no policy found for policy: policy_id:55"); - - assertThatThrownBy(() -> { - databaseProvider.getGuardPolicy(POLICY_ID, null); - }).hasMessage("no policy found for policy: policy_id:null"); - - assertThatThrownBy(() -> { - databaseProvider.getGuardPolicy(POLICY_ID, "6"); - }).hasMessage("no policy found for policy: policy_id:6"); - - assertThatThrownBy(() -> { - databaseProvider.createGuardPolicy(new LegacyGuardPolicyInput()); - }).hasMessage("policy type for guard policy \"null\" unknown"); - - assertThatThrownBy(() -> { - databaseProvider.updateGuardPolicy(new LegacyGuardPolicyInput()); - }).hasMessage("policy type for guard policy \"null\" unknown"); - - assertThatThrownBy(() -> { - databaseProvider.deleteGuardPolicy(POLICY_ID, "33"); - }).hasMessage("no policy found for policy: policy_id:33"); - - assertEquals(0, databaseProvider.getPdpGroups(NAME).size()); - assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size()); - - assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>())); - assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>())); - - PdpGroup pdpGroup = new PdpGroup(); - pdpGroup.setName(GROUP); - pdpGroup.setVersion("1.2.3"); - pdpGroup.setPdpGroupState(PdpState.ACTIVE); - pdpGroup.setPdpSubgroups(new ArrayList<>()); - List groupList = new ArrayList<>(); - groupList.add(pdpGroup); - - PdpSubGroup pdpSubGroup = new PdpSubGroup(); - pdpSubGroup.setPdpType("type"); - pdpSubGroup.setDesiredInstanceCount(123); - pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>()); - pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier("type", "7.8.9")); - pdpGroup.getPdpSubgroups().add(pdpSubGroup); - - Pdp pdp = new Pdp(); - pdp.setInstanceId("type-0"); - pdp.setMessage("Hello"); - pdp.setPdpState(PdpState.ACTIVE); - pdp.setHealthy(PdpHealthStatus.UNKNOWN); - pdpSubGroup.setPdpInstances(new ArrayList<>()); - pdpSubGroup.getPdpInstances().add(pdp); - - PdpStatistics pdpStatistics = new PdpStatistics(); - pdpStatistics.setPdpInstanceId(NAME); - pdpStatistics.setTimeStamp(new Date()); - pdpStatistics.setPdpGroupName(GROUP); - pdpStatistics.setPdpSubGroupName("type"); - ArrayList statisticsArrayList = new ArrayList<>(); - statisticsArrayList.add(pdpStatistics); - - assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0) - .getDesiredInstanceCount()); - assertEquals(1, databaseProvider.getPdpGroups(GROUP).size()); - - pdpSubGroup.setDesiredInstanceCount(234); - databaseProvider.updatePdpSubGroup(GROUP, pdpSubGroup); - assertEquals(234, - databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getDesiredInstanceCount()); - - assertEquals("Hello", databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getPdpInstances() - .get(0).getMessage()); - pdp.setMessage("Howdy"); - databaseProvider.updatePdp(GROUP, "type", pdp); - assertEquals("Howdy", databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getPdpInstances() - .get(0).getMessage()); - - assertThatThrownBy(() -> { - databaseProvider.deletePdpGroup(NAME); - }).hasMessage("delete of PDP group \"name:0.0.0\" failed, PDP group does not exist"); - - assertEquals(pdpGroup.getName(), databaseProvider.deletePdpGroup(GROUP).getName()); - - assertEquals(0, databaseProvider.getPdpStatistics(null, null).size()); - assertEquals(1, databaseProvider.createPdpStatistics(statisticsArrayList).size()); - assertEquals(1, databaseProvider.updatePdpStatistics(statisticsArrayList).size()); - - assertEquals(NAME, databaseProvider.getPdpStatistics(null, null).get(0).getPdpInstanceId()); - assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(null, GROUP, null, null, null, ORDER, 0).get(0) - .getPdpInstanceId()); - assertEquals(0, - databaseProvider.getFilteredPdpStatistics(null, GROUP, null, new Date(), null, ORDER, 0).size()); - assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(null, GROUP, null, null, new Date(), ORDER, 0) - .get(0).getPdpInstanceId()); - assertEquals(0, databaseProvider - .getFilteredPdpStatistics(null, GROUP, null, new Date(), new Date(), ORDER, 0).size()); - - assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, null, null, null, ORDER, 0).get(0) - .getPdpInstanceId()); - assertEquals(0, databaseProvider - .getFilteredPdpStatistics(NAME, GROUP, null, new Date(), new Date(), ORDER, 0).size()); - - assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 0) - .get(0).getPdpInstanceId()); - assertEquals(0, databaseProvider - .getFilteredPdpStatistics(NAME, GROUP, "type", new Date(), new Date(), ORDER, 0).size()); - - assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 1) - .get(0).getPdpInstanceId()); - assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 5) - .get(0).getPdpInstanceId()); - assertEquals(0, databaseProvider - .getFilteredPdpStatistics(NAME, GROUP, "type", new Date(), new Date(), ORDER, 5).size()); - - assertEquals(NAME, databaseProvider.deletePdpStatistics(NAME, null).get(0).getPdpInstanceId()); - assertEquals(0, databaseProvider.getPdpStatistics(null, null).size()); - } catch (Exception exc) { - LOGGER.warn("test should not throw an exception", exc); - fail("test should not throw an exception"); - } + public void testProviderMethods() throws PfModelException { + PolicyModelsProvider databaseProvider = + new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + + assertThatThrownBy(() -> databaseProvider.getPolicyTypes(NAME, VERSION_100)) + .hasMessage("service template not found in database"); + + assertTrue(databaseProvider.getPolicyTypeList(NAME, VERSION_100).isEmpty()); + + assertThatThrownBy(() -> databaseProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build())) + .hasMessage("service template not found in database"); + + assertTrue(databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build()).isEmpty()); + + assertThatThrownBy(() -> { + databaseProvider.createPolicyTypes(new ToscaServiceTemplate()); + }).hasMessage("no policy types specified on service template"); + + assertThatThrownBy(() -> { + databaseProvider.updatePolicyTypes(new ToscaServiceTemplate()); + }).hasMessage("no policy types specified on service template"); + + assertThatThrownBy(() -> databaseProvider.deletePolicyType(NAME, VERSION_100)) + .hasMessage("service template not found in database"); + + assertThatThrownBy(() -> databaseProvider.getPolicies(NAME, VERSION_100)) + .hasMessage("service template not found in database"); + + assertTrue(databaseProvider.getPolicyList(NAME, VERSION_100).isEmpty()); + + assertThatThrownBy(() -> databaseProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build())) + .hasMessage("service template not found in database"); + + assertTrue(databaseProvider.getFilteredPolicyList(ToscaPolicyFilter.builder().build()).isEmpty()); + + assertThatThrownBy(() -> { + databaseProvider.createPolicies(new ToscaServiceTemplate()); + }).hasMessage("topology template not specified on service template"); + + assertThatThrownBy(() -> { + databaseProvider.updatePolicies(new ToscaServiceTemplate()); + }).hasMessage("topology template not specified on service template"); + + assertThatThrownBy(() -> databaseProvider.deletePolicy("Policy", "0.0.0").getToscaTopologyTemplate()) + .hasMessage("service template not found in database"); + + assertThatThrownBy(() -> { + databaseProvider.getOperationalPolicy(POLICY_ID, null); + }).hasMessage("no policy found for policy: policy_id:null"); + + assertThatThrownBy(() -> { + databaseProvider.getOperationalPolicy(POLICY_ID, "10"); + }).hasMessage("no policy found for policy: policy_id:10"); + + assertThatThrownBy(() -> { + databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy()); + }).hasMessageMatching(NAME_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy()); + }).hasMessageMatching(NAME_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.deleteOperationalPolicy(POLICY_ID, "55"); + }).hasMessage("no policy found for policy: policy_id:55"); + + assertThatThrownBy(() -> { + databaseProvider.getGuardPolicy(POLICY_ID, null); + }).hasMessage("no policy found for policy: policy_id:null"); + + assertThatThrownBy(() -> { + databaseProvider.getGuardPolicy(POLICY_ID, "6"); + }).hasMessage("no policy found for policy: policy_id:6"); + + assertThatThrownBy(() -> { + databaseProvider.createGuardPolicy(new LegacyGuardPolicyInput()); + }).hasMessage("policy type for guard policy \"null\" unknown"); + + assertThatThrownBy(() -> { + databaseProvider.updateGuardPolicy(new LegacyGuardPolicyInput()); + }).hasMessage("policy type for guard policy \"null\" unknown"); + + assertThatThrownBy(() -> { + databaseProvider.deleteGuardPolicy(POLICY_ID, "33"); + }).hasMessage("no policy found for policy: policy_id:33"); + + assertEquals(0, databaseProvider.getPdpGroups(NAME).size()); + assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size()); + + assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>())); + assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>())); + + PdpGroup pdpGroup = new PdpGroup(); + pdpGroup.setName(GROUP); + pdpGroup.setVersion("1.2.3"); + pdpGroup.setPdpGroupState(PdpState.ACTIVE); + pdpGroup.setPdpSubgroups(new ArrayList<>()); + List groupList = new ArrayList<>(); + groupList.add(pdpGroup); + + PdpSubGroup pdpSubGroup = new PdpSubGroup(); + pdpSubGroup.setPdpType("type"); + pdpSubGroup.setDesiredInstanceCount(123); + pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>()); + pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier("type", "7.8.9")); + pdpGroup.getPdpSubgroups().add(pdpSubGroup); + + Pdp pdp = new Pdp(); + pdp.setInstanceId("type-0"); + pdp.setMessage("Hello"); + pdp.setPdpState(PdpState.ACTIVE); + pdp.setHealthy(PdpHealthStatus.UNKNOWN); + pdpSubGroup.setPdpInstances(new ArrayList<>()); + pdpSubGroup.getPdpInstances().add(pdp); + + PdpStatistics pdpStatistics = new PdpStatistics(); + pdpStatistics.setPdpInstanceId(NAME); + pdpStatistics.setTimeStamp(new Date()); + pdpStatistics.setPdpGroupName(GROUP); + pdpStatistics.setPdpSubGroupName("type"); + ArrayList statisticsArrayList = new ArrayList<>(); + statisticsArrayList.add(pdpStatistics); + + assertEquals(123, + databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getDesiredInstanceCount()); + assertEquals(1, databaseProvider.getPdpGroups(GROUP).size()); + + pdpSubGroup.setDesiredInstanceCount(234); + databaseProvider.updatePdpSubGroup(GROUP, pdpSubGroup); + assertEquals(234, + databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getDesiredInstanceCount()); + + assertEquals("Hello", databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getPdpInstances() + .get(0).getMessage()); + pdp.setMessage("Howdy"); + databaseProvider.updatePdp(GROUP, "type", pdp); + assertEquals("Howdy", databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getPdpInstances() + .get(0).getMessage()); + + assertThatThrownBy(() -> { + databaseProvider.deletePdpGroup(NAME); + }).hasMessage("delete of PDP group \"name:0.0.0\" failed, PDP group does not exist"); + + assertEquals(pdpGroup.getName(), databaseProvider.deletePdpGroup(GROUP).getName()); + + assertEquals(0, databaseProvider.getPdpStatistics(null, null).size()); + assertEquals(1, databaseProvider.createPdpStatistics(statisticsArrayList).size()); + assertEquals(1, databaseProvider.updatePdpStatistics(statisticsArrayList).size()); + + assertEquals(NAME, databaseProvider.getPdpStatistics(null, null).get(0).getPdpInstanceId()); + assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(null, GROUP, null, null, null, ORDER, 0).get(0) + .getPdpInstanceId()); + assertEquals(0, + databaseProvider.getFilteredPdpStatistics(null, GROUP, null, new Date(), null, ORDER, 0).size()); + assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(null, GROUP, null, null, new Date(), ORDER, 0) + .get(0).getPdpInstanceId()); + assertEquals(0, + databaseProvider.getFilteredPdpStatistics(null, GROUP, null, new Date(), new Date(), ORDER, 0).size()); + + assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, null, null, null, ORDER, 0).get(0) + .getPdpInstanceId()); + assertEquals(0, + databaseProvider.getFilteredPdpStatistics(NAME, GROUP, null, new Date(), new Date(), ORDER, 0).size()); + + assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 0).get(0) + .getPdpInstanceId()); + assertEquals(0, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", new Date(), new Date(), ORDER, 0) + .size()); + + assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 1).get(0) + .getPdpInstanceId()); + assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 5).get(0) + .getPdpInstanceId()); + assertEquals(0, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", new Date(), new Date(), ORDER, 5) + .size()); + + assertEquals(NAME, databaseProvider.deletePdpStatistics(NAME, null).get(0).getPdpInstanceId()); + assertEquals(0, databaseProvider.getPdpStatistics(null, null).size()); + + databaseProvider.close(); } } diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java index 452bbd42a..500d9d4c0 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java @@ -78,47 +78,45 @@ public class DummyPolicyModelsProviderTest { parameters.setDatabaseUrl("jdbc:dummy"); parameters.setPersistenceUnit("dummy"); - try (PolicyModelsProvider dummyProvider = - new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) { - dummyProvider.init(); - - assertNotNull(dummyProvider.getPolicyTypes("name", VERSION)); - assertNotNull(dummyProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build())); - assertNotNull(dummyProvider.getPolicyTypeList("name", VERSION)); - assertNotNull(dummyProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build())); - assertNotNull(dummyProvider.createPolicyTypes(new ToscaServiceTemplate())); - assertNotNull(dummyProvider.updatePolicyTypes(new ToscaServiceTemplate())); - assertNotNull(dummyProvider.deletePolicyType("name", VERSION)); - - assertNotNull(dummyProvider.getPolicies("name", VERSION)); - assertNotNull(dummyProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build())); - assertNotNull(dummyProvider.getPolicyList("name", VERSION)); - assertNotNull(dummyProvider.getFilteredPolicyList(ToscaPolicyFilter.builder().build())); - assertNotNull(dummyProvider.createPolicies(new ToscaServiceTemplate())); - assertNotNull(dummyProvider.updatePolicies(new ToscaServiceTemplate())); - assertNotNull(dummyProvider.deletePolicy("name", VERSION)); - - assertNotNull(dummyProvider.getOperationalPolicy(POLICY_ID, "1")); - assertNotNull(dummyProvider.createOperationalPolicy(new LegacyOperationalPolicy())); - assertNotNull(dummyProvider.updateOperationalPolicy(new LegacyOperationalPolicy())); - assertNotNull(dummyProvider.deleteOperationalPolicy(POLICY_ID, "1")); - - assertNotNull(dummyProvider.getGuardPolicy(POLICY_ID, "1")); - assertNotNull(dummyProvider.createGuardPolicy(new LegacyGuardPolicyInput())); - assertNotNull(dummyProvider.updateGuardPolicy(new LegacyGuardPolicyInput())); - assertNotNull(dummyProvider.deleteGuardPolicy(POLICY_ID, "1")); - - assertTrue(dummyProvider.getPdpGroups("name").isEmpty()); - assertTrue(dummyProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).isEmpty()); - assertTrue(dummyProvider.createPdpGroups(new ArrayList<>()).isEmpty()); - assertTrue(dummyProvider.updatePdpGroups(new ArrayList<>()).isEmpty()); - assertNull(dummyProvider.deletePdpGroup("name")); - - dummyProvider.updatePdpSubGroup("name", new PdpSubGroup()); - dummyProvider.updatePdp("name", "type", new Pdp()); - dummyProvider.updatePdpStatistics(new ArrayList<>()); - assertTrue(dummyProvider.getPdpStatistics("name", null).isEmpty()); - } + PolicyModelsProvider dummyProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + dummyProvider.init(); + + assertNotNull(dummyProvider.getPolicyTypes("name", VERSION)); + assertNotNull(dummyProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build())); + assertNotNull(dummyProvider.getPolicyTypeList("name", VERSION)); + assertNotNull(dummyProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build())); + assertNotNull(dummyProvider.createPolicyTypes(new ToscaServiceTemplate())); + assertNotNull(dummyProvider.updatePolicyTypes(new ToscaServiceTemplate())); + assertNotNull(dummyProvider.deletePolicyType("name", VERSION)); + + assertNotNull(dummyProvider.getPolicies("name", VERSION)); + assertNotNull(dummyProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build())); + assertNotNull(dummyProvider.getPolicyList("name", VERSION)); + assertNotNull(dummyProvider.getFilteredPolicyList(ToscaPolicyFilter.builder().build())); + assertNotNull(dummyProvider.createPolicies(new ToscaServiceTemplate())); + assertNotNull(dummyProvider.updatePolicies(new ToscaServiceTemplate())); + assertNotNull(dummyProvider.deletePolicy("name", VERSION)); + + assertNotNull(dummyProvider.getOperationalPolicy(POLICY_ID, "1")); + assertNotNull(dummyProvider.createOperationalPolicy(new LegacyOperationalPolicy())); + assertNotNull(dummyProvider.updateOperationalPolicy(new LegacyOperationalPolicy())); + assertNotNull(dummyProvider.deleteOperationalPolicy(POLICY_ID, "1")); + + assertNotNull(dummyProvider.getGuardPolicy(POLICY_ID, "1")); + assertNotNull(dummyProvider.createGuardPolicy(new LegacyGuardPolicyInput())); + assertNotNull(dummyProvider.updateGuardPolicy(new LegacyGuardPolicyInput())); + assertNotNull(dummyProvider.deleteGuardPolicy(POLICY_ID, "1")); + + assertTrue(dummyProvider.getPdpGroups("name").isEmpty()); + assertTrue(dummyProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).isEmpty()); + assertTrue(dummyProvider.createPdpGroups(new ArrayList<>()).isEmpty()); + assertTrue(dummyProvider.updatePdpGroups(new ArrayList<>()).isEmpty()); + assertNull(dummyProvider.deletePdpGroup("name")); + + dummyProvider.updatePdpSubGroup("name", new PdpSubGroup()); + dummyProvider.updatePdp("name", "type", new Pdp()); + dummyProvider.updatePdpStatistics(new ArrayList<>()); + assertTrue(dummyProvider.getPdpStatistics("name", null).isEmpty()); } @Test 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 83665b078..ecb50cdc3 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -103,7 +103,7 @@ public class PolicyLegacyGuardPersistenceTest { } @Test - public void testPolicyPersistence() throws Exception { + public void testLegacyGuardPolicyPersistence() throws Exception { for (int i = 0; i < policyInputResourceNames.length; i++) { String policyInputString = ResourceUtils.getResourceAsString(policyInputResourceNames[i]); String policyOutputString = ResourceUtils.getResourceAsString(policyOutputResourceNames[i]); 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 29e3b5a47..0eee4b2b6 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -103,7 +103,7 @@ public class PolicyLegacyOperationalPersistenceTest { } @Test - public void testPolicyPersistence() throws Exception { + public void testLegacyOperationalPolicyPersistence() throws Exception { for (int i = 0; i < policyInputResourceNames.length; i++) { String policyInputString = ResourceUtils.getResourceAsString(policyInputResourceNames[i]); String policyOutputString = ResourceUtils.getResourceAsString(policyOutputResourceNames[i]); 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 f4196811f..0cdc2ad49 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -92,7 +92,7 @@ public class PolicyToscaPersistenceTest { } @Test - public void testPolicyPersistence() throws Exception { + public void testToscaPolicyPersistence() throws Exception { Set policyResources = ResourceUtils.getDirectoryContents("policies"); for (String policyResource : policyResources) { 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 0ecf8a046..22722186c 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,29 +20,24 @@ package org.onap.policy.models.provider.impl; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.Base64; -import java.util.List; import java.util.Set; -import lombok.NonNull; - import org.junit.After; import org.junit.Before; import org.junit.Test; -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.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.ToscaPolicyType; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; +import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityKey; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; -import org.yaml.snakeyaml.Yaml; /** * Test persistence of monitoring policies to and from the database. @@ -50,8 +45,7 @@ import org.yaml.snakeyaml.Yaml; * @author Liam Fallon (liam.fallon@est.tech) */ public class PolicyTypePersistenceTest { - private StandardCoder standardCoder; - + private YamlJsonTranslator yamlTranslator = new YamlJsonTranslator(); private PolicyModelsProvider databaseProvider; /** @@ -73,14 +67,6 @@ public class PolicyTypePersistenceTest { databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); } - /** - * Set up standard coder. - */ - @Before - public void setupStandardCoder() { - standardCoder = new StandardCoder(); - } - @After public void teardown() throws Exception { databaseProvider.close(); @@ -90,60 +76,54 @@ public class PolicyTypePersistenceTest { public void testPolicyTypePersistence() throws Exception { Set policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes"); + ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(); + for (String policyTypeFilePath : policyTypeDirectoryContents) { String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath); - testYamlStringPolicyTypePersistence(policyTypeString); - } - } - - private void testYamlStringPolicyTypePersistence(final String policyTypeString) throws Exception { - Object yamlObject = new Yaml().load(policyTypeString); - String yamlAsJsonString = new StandardCoder().encode(yamlObject); - testJsonStringPolicyTypePersistence(yamlAsJsonString); - } - - /** - * Check persistence of a policy. - * - * @param policyTypeString the policy as a string - * @throws Exception any exception thrown - */ - public void testJsonStringPolicyTypePersistence(@NonNull final String policyTypeString) throws Exception { - ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyTypeString, ToscaServiceTemplate.class); - - assertNotNull(serviceTemplate); - ToscaPolicyType inPolicyType = serviceTemplate.getPolicyTypes().values().iterator().next(); - - databaseProvider.createPolicyTypes(serviceTemplate); - checkPolicyTypePersistence(inPolicyType); - - databaseProvider.updatePolicyTypes(serviceTemplate); - checkPolicyTypePersistence(inPolicyType); - } + ToscaServiceTemplate foundPolicyTypeSt = + yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class); + + serviceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom()); + serviceTemplate.setDescription(foundPolicyTypeSt.getDescription()); + serviceTemplate.setMetadata(foundPolicyTypeSt.getMetadata()); + serviceTemplate.setName(foundPolicyTypeSt.getName()); + serviceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion()); + serviceTemplate.setToscaTopologyTemplate(foundPolicyTypeSt.getToscaTopologyTemplate()); + serviceTemplate.setVersion(foundPolicyTypeSt.getVersion()); + + if (foundPolicyTypeSt.getDataTypes() != null) { + if (serviceTemplate.getDataTypes() == null) { + serviceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes()); + } else { + serviceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes()); + } + } + + if (serviceTemplate.getPolicyTypes() == null) { + serviceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes()); + } else { + serviceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes()); + } + } - private void checkPolicyTypePersistence(ToscaPolicyType inPolicyType) throws PfModelException { - List policyTypeList = - databaseProvider.getPolicyTypeList(inPolicyType.getName(), inPolicyType.getVersion()); + assertThatCode(() -> databaseProvider.createPolicyTypes(serviceTemplate)).doesNotThrowAnyException(); - policyTypeList = databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder() - .name(inPolicyType.getName()).version(inPolicyType.getVersion()).build()); + ToscaEntityKey resourceOptimizationPtKey = + new ToscaEntityKey("onap.policies.optimization.resource.OptimizationPolicy", "1.0.0"); - assertEquals(1, policyTypeList.size()); - assertEquals(inPolicyType.getName(), policyTypeList.get(0).getName()); + ToscaServiceTemplate resOptPolicyTypeSt = databaseProvider.getPolicyTypes(resourceOptimizationPtKey.getName(), + resourceOptimizationPtKey.getVersion()); - policyTypeList = databaseProvider - .getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().name(inPolicyType.getName()).build()); + assertEquals(3, resOptPolicyTypeSt.getPolicyTypesAsMap().size()); + assertTrue(resOptPolicyTypeSt.getPolicyTypesAsMap().containsKey(resourceOptimizationPtKey)); - assertEquals(1, policyTypeList.size()); - assertEquals(inPolicyType.getName(), policyTypeList.get(0).getName()); + ToscaEntityKey resourcePtKey = new ToscaEntityKey("onap.policies.optimization.Resource", "1.0.0"); + assertTrue(resOptPolicyTypeSt.getPolicyTypesAsMap().containsKey(resourcePtKey)); - policyTypeList = databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build()); - assertTrue(policyTypeList.size() <= 3); - assertEquals(inPolicyType.getName(), policyTypeList.get(0).getName()); + ToscaEntityKey optimizationPtKey = new ToscaEntityKey("onap.policies.Optimization", "1.0.0"); + assertTrue(resOptPolicyTypeSt.getPolicyTypesAsMap().containsKey(optimizationPtKey)); - for (ToscaPolicyType policyType : databaseProvider.getPolicyTypeList(null, null)) { - databaseProvider.deletePolicyType(policyType.getName(), policyType.getVersion()); - } + assertEquals(2, resOptPolicyTypeSt.getDataTypesAsMap().size()); } } diff --git a/models-provider/src/test/resources/META-INF/persistence.xml b/models-provider/src/test/resources/META-INF/persistence.xml index 4306413a5..77062ce23 100644 --- a/models-provider/src/test/resources/META-INF/persistence.xml +++ b/models-provider/src/test/resources/META-INF/persistence.xml @@ -27,8 +27,13 @@ org.onap.policy.models.dao.converters.Uuid2String org.onap.policy.models.base.PfConceptKey org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType - org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType + org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes + org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy + org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType + org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes + org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate + org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup org.onap.policy.models.pdp.persistence.concepts.JpaPdpSubGroup org.onap.policy.models.pdp.persistence.concepts.JpaPdp -- cgit 1.2.3-korg