From cee84a9344f35356f86384b0d48127f5a83e3776 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Tue, 26 Mar 2019 22:31:43 +0000 Subject: Add Legacy Op Policy Persistence Legacy operational policies now fully supported for serialization and persistence from provider through to database and back. Unit test coverage completed also Issue-ID: POLICY-1095 Change-Id: I65755859c94b50edee537d2685f51a7838c6541f Signed-off-by: liamfallon --- .../impl/DatabasePolicyModelsProviderImpl.java | 18 +-- .../impl/DatabasePolicyModelsProviderTest.java | 28 +++- .../PolicyLegacyOperationalPersistenceTest.java | 143 ++++++++++++++++++ .../provider/impl/PolicyToscaPersistenceTest.java | 159 +++++++++++++++++++++ 4 files changed, 335 insertions(+), 13 deletions(-) create mode 100644 models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyOperationalPersistenceTest.java create mode 100644 models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java (limited to 'models-provider') diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java index 970aa8fef..43f75d2a9 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java @@ -41,7 +41,7 @@ import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; -import org.onap.policy.models.tosca.legacy.provider.LegacyToscaProvider; +import org.onap.policy.models.tosca.legacy.provider.LegacyProvider; import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider; import org.slf4j.Logger; @@ -190,53 +190,53 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { @Override public LegacyOperationalPolicy getOperationalPolicy(@NonNull final String policyId) throws PfModelException { assertInitilized(); - return new LegacyToscaProvider().getOperationalPolicy(pfDao, policyId); + return new LegacyProvider().getOperationalPolicy(pfDao, policyId); } @Override public LegacyOperationalPolicy createOperationalPolicy( @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException { assertInitilized(); - return new LegacyToscaProvider().createOperationalPolicy(pfDao, legacyOperationalPolicy); + return new LegacyProvider().createOperationalPolicy(pfDao, legacyOperationalPolicy); } @Override public LegacyOperationalPolicy updateOperationalPolicy( @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException { assertInitilized(); - return new LegacyToscaProvider().updateOperationalPolicy(pfDao, legacyOperationalPolicy); + return new LegacyProvider().updateOperationalPolicy(pfDao, legacyOperationalPolicy); } @Override public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId) throws PfModelException { assertInitilized(); - return new LegacyToscaProvider().deleteOperationalPolicy(pfDao, policyId); + return new LegacyProvider().deleteOperationalPolicy(pfDao, policyId); } @Override public LegacyGuardPolicy getGuardPolicy(@NonNull final String policyId) throws PfModelException { assertInitilized(); - return new LegacyToscaProvider().getGuardPolicy(pfDao, policyId); + return new LegacyProvider().getGuardPolicy(pfDao, policyId); } @Override public LegacyGuardPolicy createGuardPolicy(@NonNull final LegacyGuardPolicy legacyGuardPolicy) throws PfModelException { assertInitilized(); - return new LegacyToscaProvider().createGuardPolicy(pfDao, legacyGuardPolicy); + return new LegacyProvider().createGuardPolicy(pfDao, legacyGuardPolicy); } @Override public LegacyGuardPolicy updateGuardPolicy(@NonNull final LegacyGuardPolicy legacyGuardPolicy) throws PfModelException { assertInitilized(); - return new LegacyToscaProvider().updateGuardPolicy(pfDao, legacyGuardPolicy); + return new LegacyProvider().updateGuardPolicy(pfDao, legacyGuardPolicy); } @Override public LegacyGuardPolicy deleteGuardPolicy(@NonNull final String policyId) throws PfModelException { assertInitilized(); - return new LegacyToscaProvider().deleteGuardPolicy(pfDao, policyId); + return new LegacyProvider().deleteGuardPolicy(pfDao, policyId); } @Override 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 c5dd8f03d..e4ecb9d1d 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 @@ -316,10 +316,30 @@ public class DatabasePolicyModelsProviderTest { assertEquals("policy not found: NULL:0.0.0", npe.getMessage()); } - assertNull(databaseProvider.getOperationalPolicy("policy_id")); - assertNull(databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy())); - assertNull(databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy())); - assertNull(databaseProvider.deleteOperationalPolicy("policy_id")); + try { + assertNull(databaseProvider.getOperationalPolicy("policy_id")); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("no policy found for policy ID: policy_id", npe.getMessage()); + } + try { + assertNull(databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy())); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("name is marked @NonNull but is null", npe.getMessage()); + } + try { + assertNull(databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy())); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("no policy found for policy ID: null", npe.getMessage()); + } + try { + assertNull(databaseProvider.deleteOperationalPolicy("policy_id")); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("no policy found for policy ID: policy_id", npe.getMessage()); + } assertNull(databaseProvider.getGuardPolicy("policy_id")); assertNull(databaseProvider.createGuardPolicy(new LegacyGuardPolicy())); 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 new file mode 100644 index 000000000..90d00fc58 --- /dev/null +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyOperationalPersistenceTest.java @@ -0,0 +1,143 @@ +/*- + * ============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.Gson; + +import java.util.Base64; + +import lombok.NonNull; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +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.legacy.concepts.LegacyOperationalPolicy; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Test persistence of monitoring policies to and from the database. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class PolicyLegacyOperationalPersistenceTest { + // Logger for this class + private static final Logger LOGGER = LoggerFactory.getLogger(PolicyLegacyOperationalPersistenceTest.class); + + private Gson gson; + + private PolicyModelsProvider databaseProvider; + + // @formatter:off + private String[] policyInputResourceNames = { + "policies/vCPE.policy.operational.input.json", + "policies/vDNS.policy.operational.input.json", + "policies/vFirewall.policy.operational.input.json" + }; + + private String[] policyOutputResourceNames = { + "policies/vCPE.policy.operational.output.json", + "policies/vDNS.policy.operational.output.json", + "policies/vFirewall.policy.operational.output.json" + }; + // @formatter:on + + /** + * Initialize provider. + * + * @throws PfModelException on exceptions in the tests + */ + @Before + public void setupParameters() throws PfModelException { + PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters(); + parameters.setDatabaseUrl("jdbc:h2:mem:testdb"); + parameters.setDatabaseUser("policy"); + parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes())); + parameters.setPersistenceUnit("ToscaConceptTest"); + + databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + databaseProvider.init(); + } + + /** + * Set up GSON. + */ + @Before + public void setupGson() { + gson = new Gson(); + } + + @After + public void teardown() throws Exception { + databaseProvider.close(); + } + + @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"); + } + } + + /** + * Check persistence of a policy. + * + * @param policyInputString the policy as a string + * @param policyOutputString the expected output string + * @throws Exception any exception thrown + */ + public void testJsonStringPolicyPersistence(@NonNull final String policyInputString, + final String policyOutputString) throws Exception { + LegacyOperationalPolicy lop = gson.fromJson(policyInputString, LegacyOperationalPolicy.class); + + assertNotNull(lop); + + LegacyOperationalPolicy createdLop = databaseProvider.createOperationalPolicy(lop); + assertEquals(createdLop, lop); + + LegacyOperationalPolicy gotLop = databaseProvider.getOperationalPolicy(lop.getPolicyId()); + assertEquals(gotLop, lop); + + String actualRetrievedJson = gson.toJson(gotLop); + + // All of this dash/underscore stuff is to avoid a checkstyle error around escaping unicode characters + assertEquals( + policyOutputString.replaceAll("\\s+", "").replaceAll("u0027", "_-_-_-_").replaceAll("\\\\_-_-_-_", "'"), + actualRetrievedJson.replaceAll("\\s+", "").replaceAll("u0027", "_-_-_-_").replaceAll("\\\\_-_-_-_", + "'")); + } +} 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 new file mode 100644 index 000000000..a4b1dcd03 --- /dev/null +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java @@ -0,0 +1,159 @@ +/*- + * ============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.assertTrue; +import static org.junit.Assert.fail; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import java.util.Base64; + +import lombok.NonNull; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.base.PfValidationResult; +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.simple.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.serialization.ToscaServiceTemplateMessageBodyHandler; +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 PolicyToscaPersistenceTest { + // Logger for this class + private static final Logger LOGGER = LoggerFactory.getLogger(PolicyToscaPersistenceTest.class); + + private Gson gson; + + 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 + */ + @Before + public void setupParameters() throws PfModelException { + PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters(); + parameters.setDatabaseUrl("jdbc:h2:mem:testdb"); + parameters.setDatabaseUser("policy"); + parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes())); + parameters.setPersistenceUnit("ToscaConceptTest"); + + databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + databaseProvider.init(); + } + + /** + * Set up GSON. + */ + @Before + public void setupGson() { + gson = new ToscaServiceTemplateMessageBodyHandler().getGson(); + } + + @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 = gson.fromJson(policyString, ToscaServiceTemplate.class); + + assertNotNull(serviceTemplate); + LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString()); + assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid()); + + databaseProvider.createPolicies(serviceTemplate); + + for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) { + ToscaPolicy incomingPolicy = serviceTemplate.getTopologyTemplate().getPolicies().get(policyKey); + ToscaPolicy databasePolicy = + databaseProvider.getPolicies(policyKey).getTopologyTemplate().getPolicies().get(policyKey); + assertEquals(incomingPolicy, databasePolicy); + } + } +} -- cgit 1.2.3-korg