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 com.google.gson.GsonBuilder;
29 import java.util.Base64;
32 import lombok.NonNull;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.policy.common.utils.coder.CoderException;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
40 import org.onap.policy.models.base.PfModelException;
41 import org.onap.policy.models.provider.PolicyModelsProvider;
42 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
43 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.yaml.snakeyaml.Yaml;
52 * Test persistence of monitoring policies to and from the database.
54 * @author Liam Fallon (liam.fallon@est.tech)
56 public class PolicyPersistenceTest {
57 // Logger for this class
58 private static final Logger LOGGER = LoggerFactory.getLogger(PolicyPersistenceTest.class);
60 private StandardCoder standardCoder;
62 private PolicyModelsProvider databaseProvider;
65 private String[] policyResourceNames = {
66 "policies/vCPE.policy.monitoring.input.tosca.json",
67 "policies/vCPE.policy.monitoring.input.tosca.yaml",
68 "policies/vCPE.policy.operational.input.tosca.yaml",
69 "policies/vDNS.policy.guard.frequency.input.tosca.json",
70 "policies/vDNS.policy.guard.frequency.input.tosca.yaml",
71 "policies/vDNS.policy.monitoring.input.tosca.json",
72 "policies/vDNS.policy.monitoring.input.tosca.yaml",
73 "policies/vDNS.policy.operational.input.tosca.yaml",
74 "policies/vFirewall.policy.monitoring.input.tosca.json",
75 "policies/vFirewall.policy.monitoring.input.tosca.yaml",
76 "policies/vFirewall.policy.operational.input.tosca.json",
77 "policies/vFirewall.policy.operational.input.tosca.yaml"
82 * Initialize provider.
84 * @throws PfModelException on exceptions in the tests
85 * @throws CoderException on JSON encoding and decoding errors
88 public void setupParameters() throws PfModelException, CoderException {
89 PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
90 parameters.setDatabaseDriver("org.h2.Driver");
91 parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
92 parameters.setDatabaseUser("policy");
93 parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
94 parameters.setPersistenceUnit("ToscaConceptTest");
96 databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
105 public void setupGson() {
106 standardCoder = new StandardCoder();
110 public void teardown() throws Exception {
111 databaseProvider.close();
115 public void testPolicyPersistence() {
117 for (String policyResourceName : policyResourceNames) {
118 String policyString = ResourceUtils.getResourceAsString(policyResourceName);
120 if (policyResourceName.endsWith("yaml")) {
121 testYamlStringPolicyPersistence(policyString);
123 testJsonStringPolicyPersistence(policyString);
126 } catch (Exception exc) {
127 LOGGER.warn("error processing policies", exc);
128 fail("test should not throw an exception");
132 private void testYamlStringPolicyPersistence(final String policyString) throws Exception {
133 Object yamlObject = new Yaml().load(policyString);
134 String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
136 testJsonStringPolicyPersistence(yamlAsJsonString);
140 * Check persistence of a policy.
142 * @param policyString the policy as a string
143 * @throws Exception any exception thrown
145 public void testJsonStringPolicyPersistence(@NonNull final String policyString) throws Exception {
146 ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
148 assertNotNull(serviceTemplate);
150 databaseProvider.createPolicies(serviceTemplate);
151 databaseProvider.updatePolicies(serviceTemplate);
153 for (Map<String, ToscaPolicy> policyMap : serviceTemplate.getToscaTopologyTemplate().getPolicies()) {
154 for (ToscaPolicy policy : policyMap.values()) {
155 ToscaServiceTemplate gotToscaServiceTemplate =
156 databaseProvider.getPolicies(policy.getName(), policy.getVersion());
158 assertEquals(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0)
159 .get(policy.getName()).getType(), policy.getType());
161 gotToscaServiceTemplate = databaseProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build());
163 assertEquals(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0)
164 .get(policy.getName()).getType(), policy.getType());
166 gotToscaServiceTemplate = databaseProvider.getFilteredPolicies(
167 ToscaPolicyFilter.builder().name(policy.getName()).version(policy.getVersion()).build());
169 assertEquals(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0)
170 .get(policy.getName()).getType(), policy.getType());
175 private void createPolicyTypes() throws CoderException, PfModelException {
176 Object yamlObject = new Yaml().load(
177 ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml"));
178 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
180 ToscaServiceTemplate toscaServiceTemplatePolicyType =
181 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
183 assertNotNull(toscaServiceTemplatePolicyType);
184 databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);
186 yamlObject = new Yaml().load(
187 ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.Operational.yaml"));
188 yamlAsJsonString = new StandardCoder().encode(yamlObject);
190 toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
192 assertNotNull(toscaServiceTemplatePolicyType);
193 databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);
195 yamlObject = new Yaml().load(
196 ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml"));
197 yamlAsJsonString = new StandardCoder().encode(yamlObject);
199 toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
201 assertNotNull(toscaServiceTemplatePolicyType);
202 databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);