2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (C) 2020 AT&T.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.provider.impl;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
27 import java.net.URISyntaxException;
28 import java.util.Base64;
30 import lombok.NonNull;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.common.utils.coder.CoderException;
35 import org.onap.policy.common.utils.coder.StandardCoder;
36 import org.onap.policy.common.utils.resources.ResourceUtils;
37 import org.onap.policy.models.base.PfModelException;
38 import org.onap.policy.models.provider.PolicyModelsProvider;
39 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
40 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
42 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
43 import org.yaml.snakeyaml.Yaml;
46 * Test persistence of monitoring policies to and from the database.
48 * @author Liam Fallon (liam.fallon@est.tech)
50 public class PolicyLegacyOperationalPersistenceTest {
51 private StandardCoder standardCoder;
53 private PolicyModelsProvider databaseProvider;
56 private String[] policyInputResourceNames = {
57 "policies/vCPE.policy.operational.legacy.input.json",
58 "policies/vDNS.policy.operational.legacy.input.json",
59 "policies/vFirewall.policy.operational.legacy.input.json"
62 private String[] policyOutputResourceNames = {
63 "policies/vCPE.policy.operational.legacy.output.json",
64 "policies/vDNS.policy.operational.legacy.output.json",
65 "policies/vFirewall.policy.operational.legacy.output.json"
70 * Initialize provider.
72 * @throws PfModelException on exceptions in the tests
73 * @throws CoderException on JSON encoding and decoding errors
76 public void setupParameters() throws Exception {
77 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
79 PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
80 parameters.setDatabaseDriver("org.h2.Driver");
81 parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
82 parameters.setDatabaseUser("policy");
83 parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
84 parameters.setPersistenceUnit("ToscaConceptTest");
86 databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
92 * Set up standard coder.
95 public void setupStandardCoder() {
96 standardCoder = new StandardCoder();
100 public void teardown() throws Exception {
101 databaseProvider.close();
105 public void testLegacyOperationalPolicyPersistence() throws Exception {
106 for (int i = 0; i < policyInputResourceNames.length; i++) {
107 String policyInputString = ResourceUtils.getResourceAsString(policyInputResourceNames[i]);
108 String policyOutputString = ResourceUtils.getResourceAsString(policyOutputResourceNames[i]);
109 testJsonStringPolicyPersistence(policyInputString, policyOutputString);
114 * Check persistence of a policy.
116 * @param policyInputString the policy as a string
117 * @param policyOutputString the expected output string
118 * @throws Exception any exception thrown
120 public void testJsonStringPolicyPersistence(@NonNull final String policyInputString,
121 final String policyOutputString) throws Exception {
122 LegacyOperationalPolicy lop = standardCoder.decode(policyInputString, LegacyOperationalPolicy.class);
126 LegacyOperationalPolicy createdLop = databaseProvider.createOperationalPolicy(lop);
127 assertEquals(createdLop, lop);
129 LegacyOperationalPolicy gotLop = databaseProvider.getOperationalPolicy(lop.getPolicyId(), null);
130 assertEquals(gotLop, lop);
132 LegacyOperationalPolicy updatedLop = databaseProvider.updateOperationalPolicy(lop);
133 assertEquals(gotLop, updatedLop);
135 LegacyOperationalPolicy deletedLop = databaseProvider.deleteOperationalPolicy(lop.getPolicyId(), "1");
136 assertEquals(gotLop, deletedLop);
138 String actualRetrievedJson = standardCoder.encode(gotLop);
140 // All of this dash/underscore stuff is to avoid a checkstyle error around escaping unicode characters
142 policyOutputString.replaceAll("\\s+", "").replaceAll("u0027", "_-_-_-_").replaceAll("\\\\_-_-_-_", "'"),
143 actualRetrievedJson.replaceAll("\\s+", "").replaceAll("u0027", "_-_-_-_").replaceAll("\\\\_-_-_-_",
147 private void createPolicyTypes() throws CoderException, PfModelException, URISyntaxException {
148 Set<String> policyTypeResources = ResourceUtils.getDirectoryContents("policytypes");
150 for (String policyTypeResource : policyTypeResources) {
151 Object yamlObject = new Yaml().load(ResourceUtils.getResourceAsString(policyTypeResource));
152 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
154 ToscaServiceTemplate toscaServiceTemplatePolicyType =
155 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
157 assertNotNull(toscaServiceTemplatePolicyType);
158 databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);