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;
25 import static org.junit.Assert.fail;
27 import java.util.Base64;
29 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.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.yaml.snakeyaml.Yaml;
48 * Test persistence of monitoring policies to and from the database.
50 * @author Liam Fallon (liam.fallon@est.tech)
52 public class PolicyLegacyOperationalPersistenceTest {
53 // Logger for this class
54 private static final Logger LOGGER = LoggerFactory.getLogger(PolicyLegacyOperationalPersistenceTest.class);
56 private StandardCoder standardCoder;
58 private PolicyModelsProvider databaseProvider;
61 private String[] policyInputResourceNames = {
62 "policies/vCPE.policy.operational.input.json",
63 "policies/vDNS.policy.operational.input.json",
64 "policies/vFirewall.policy.operational.input.json"
67 private String[] policyOutputResourceNames = {
68 "policies/vCPE.policy.operational.output.json",
69 "policies/vDNS.policy.operational.output.json",
70 "policies/vFirewall.policy.operational.output.json"
75 * Initialize provider.
77 * @throws PfModelException on exceptions in the tests
78 * @throws CoderException on JSON encoding and decoding errors
81 public void setupParameters() throws Exception {
82 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
84 PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
85 parameters.setDatabaseDriver("org.h2.Driver");
86 parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
87 parameters.setDatabaseUser("policy");
88 parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
89 parameters.setPersistenceUnit("ToscaConceptTest");
91 databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
97 * Set up standard coder.
100 public void setupStandardCoder() {
101 standardCoder = new StandardCoder();
105 public void teardown() throws Exception {
106 databaseProvider.close();
110 public void testPolicyPersistence() {
112 for (int i = 0; i < policyInputResourceNames.length; i++) {
113 String policyInputString = ResourceUtils.getResourceAsString(policyInputResourceNames[i]);
114 String policyOutputString = ResourceUtils.getResourceAsString(policyOutputResourceNames[i]);
115 testJsonStringPolicyPersistence(policyInputString, policyOutputString);
117 } catch (Exception exc) {
118 LOGGER.warn("error processing policies", exc);
119 fail("test should not throw an exception");
124 * Check persistence of a policy.
126 * @param policyInputString the policy as a string
127 * @param policyOutputString the expected output string
128 * @throws Exception any exception thrown
130 public void testJsonStringPolicyPersistence(@NonNull final String policyInputString,
131 final String policyOutputString) throws Exception {
132 LegacyOperationalPolicy lop = standardCoder.decode(policyInputString, LegacyOperationalPolicy.class);
136 LegacyOperationalPolicy createdLop = databaseProvider.createOperationalPolicy(lop);
137 assertEquals(createdLop, lop);
139 LegacyOperationalPolicy gotLop = databaseProvider.getOperationalPolicy(lop.getPolicyId(), null);
140 assertEquals(gotLop, lop);
142 LegacyOperationalPolicy updatedLop = databaseProvider.updateOperationalPolicy(lop);
143 assertEquals(gotLop, updatedLop);
145 LegacyOperationalPolicy deletedLop = databaseProvider.deleteOperationalPolicy(lop.getPolicyId(), "1");
146 assertEquals(gotLop, deletedLop);
148 String actualRetrievedJson = standardCoder.encode(gotLop);
150 // All of this dash/underscore stuff is to avoid a checkstyle error around escaping unicode characters
152 policyOutputString.replaceAll("\\s+", "").replaceAll("u0027", "_-_-_-_").replaceAll("\\\\_-_-_-_", "'"),
153 actualRetrievedJson.replaceAll("\\s+", "").replaceAll("u0027", "_-_-_-_").replaceAll("\\\\_-_-_-_",
157 private void createPolicyTypes() throws CoderException, PfModelException {
159 Object yamlObject = new Yaml().load(
160 ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.Operational.yaml"));
161 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
163 ToscaServiceTemplate toscaServiceTemplatePolicyType =
164 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
166 assertNotNull(toscaServiceTemplatePolicyType);
167 databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);