2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.provider.impl;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
26 import java.net.URISyntaxException;
27 import java.util.Base64;
30 import lombok.NonNull;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.common.utils.coder.CoderException;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.provider.PolicyModelsProvider;
40 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
41 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
43 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
44 import org.yaml.snakeyaml.Yaml;
47 * Test persistence of monitoring policies to and from the database.
49 * @author Liam Fallon (liam.fallon@est.tech)
51 public class PolicyLegacyOperationalPersistenceTest {
52 private StandardCoder standardCoder;
54 private PolicyModelsProvider databaseProvider;
57 private String[] policyInputResourceNames = {
58 "policies/vCPE.policy.operational.input.json",
59 "policies/vDNS.policy.operational.input.json",
60 "policies/vFirewall.policy.operational.input.json"
63 private String[] policyOutputResourceNames = {
64 "policies/vCPE.policy.operational.output.json",
65 "policies/vDNS.policy.operational.output.json",
66 "policies/vFirewall.policy.operational.output.json"
71 * Initialize provider.
73 * @throws PfModelException on exceptions in the tests
74 * @throws CoderException on JSON encoding and decoding errors
77 public void setupParameters() throws Exception {
78 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
80 PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
81 parameters.setDatabaseDriver("org.h2.Driver");
82 parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
83 parameters.setDatabaseUser("policy");
84 parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
85 parameters.setPersistenceUnit("ToscaConceptTest");
87 databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
93 * Set up standard coder.
96 public void setupStandardCoder() {
97 standardCoder = new StandardCoder();
101 public void teardown() throws Exception {
102 databaseProvider.close();
106 public void testPolicyPersistence() throws Exception {
107 for (int i = 0; i < policyInputResourceNames.length; i++) {
108 String policyInputString = ResourceUtils.getResourceAsString(policyInputResourceNames[i]);
109 String policyOutputString = ResourceUtils.getResourceAsString(policyOutputResourceNames[i]);
110 testJsonStringPolicyPersistence(policyInputString, policyOutputString);
115 * Check persistence of a policy.
117 * @param policyInputString the policy as a string
118 * @param policyOutputString the expected output string
119 * @throws Exception any exception thrown
121 public void testJsonStringPolicyPersistence(@NonNull final String policyInputString,
122 final String policyOutputString) throws Exception {
123 LegacyOperationalPolicy lop = standardCoder.decode(policyInputString, LegacyOperationalPolicy.class);
127 LegacyOperationalPolicy createdLop = databaseProvider.createOperationalPolicy(lop);
128 assertEquals(createdLop, lop);
130 LegacyOperationalPolicy gotLop = databaseProvider.getOperationalPolicy(lop.getPolicyId(), null);
131 assertEquals(gotLop, lop);
133 LegacyOperationalPolicy updatedLop = databaseProvider.updateOperationalPolicy(lop);
134 assertEquals(gotLop, updatedLop);
136 LegacyOperationalPolicy deletedLop = databaseProvider.deleteOperationalPolicy(lop.getPolicyId(), "1");
137 assertEquals(gotLop, deletedLop);
139 String actualRetrievedJson = standardCoder.encode(gotLop);
141 // All of this dash/underscore stuff is to avoid a checkstyle error around escaping unicode characters
143 policyOutputString.replaceAll("\\s+", "").replaceAll("u0027", "_-_-_-_").replaceAll("\\\\_-_-_-_", "'"),
144 actualRetrievedJson.replaceAll("\\s+", "").replaceAll("u0027", "_-_-_-_").replaceAll("\\\\_-_-_-_",
148 private void createPolicyTypes() throws CoderException, PfModelException, URISyntaxException {
149 Set<String> policyTypeResources = ResourceUtils.getDirectoryContents("policytypes");
151 for (String policyTyoeResource : policyTypeResources) {
152 Object yamlObject = new Yaml().load(ResourceUtils.getResourceAsString(policyTyoeResource));
153 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
155 ToscaServiceTemplate toscaServiceTemplatePolicyType =
156 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
158 assertNotNull(toscaServiceTemplatePolicyType);
159 databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);