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 com.google.gson.GsonBuilder;
28 import java.net.URISyntaxException;
29 import java.util.Base64;
30 import java.util.List;
34 import lombok.NonNull;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.policy.common.utils.coder.CoderException;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.onap.policy.common.utils.resources.ResourceUtils;
42 import org.onap.policy.models.base.PfModelException;
43 import org.onap.policy.models.provider.PolicyModelsProvider;
44 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
45 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
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 PolicyToscaPersistenceTest {
57 private StandardCoder standardCoder;
59 private PolicyModelsProvider databaseProvider;
62 * Initialize provider.
64 * @throws PfModelException on exceptions in the tests
65 * @throws CoderException on JSON encoding and decoding errors
68 public void setupParameters() throws Exception {
69 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
71 PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
72 parameters.setDatabaseDriver("org.h2.Driver");
73 parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
74 parameters.setDatabaseUser("policy");
75 parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
76 parameters.setPersistenceUnit("ToscaConceptTest");
78 databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
84 * Set up standard coder.
87 public void setupStandardCoder() {
88 standardCoder = new StandardCoder();
92 public void teardown() throws Exception {
93 databaseProvider.close();
97 public void testPolicyPersistence() throws Exception {
98 Set<String> policyResources = ResourceUtils.getDirectoryContents("policies");
100 for (String policyResource : policyResources) {
101 if (!policyResource.contains("\\.tosca\\.")) {
105 String policyString = ResourceUtils.getResourceAsString(policyResource);
107 if (policyResource.endsWith("yaml")) {
108 testYamlStringPolicyPersistence(policyString);
110 testJsonStringPolicyPersistence(policyString);
115 private void testYamlStringPolicyPersistence(final String policyString) throws Exception {
116 Object yamlObject = new Yaml().load(policyString);
117 String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
119 testJsonStringPolicyPersistence(yamlAsJsonString);
123 * Check persistence of a policy.
125 * @param policyString the policy as a string
126 * @throws Exception any exception thrown
128 public void testJsonStringPolicyPersistence(@NonNull final String policyString) throws Exception {
129 ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
131 assertNotNull(serviceTemplate);
133 databaseProvider.createPolicies(serviceTemplate);
134 databaseProvider.updatePolicies(serviceTemplate);
136 for (Map<String, ToscaPolicy> policyMap : serviceTemplate.getToscaTopologyTemplate().getPolicies()) {
137 for (ToscaPolicy policy : policyMap.values()) {
138 ToscaServiceTemplate gotToscaServiceTemplate =
139 databaseProvider.getPolicies(policy.getName(), policy.getVersion());
141 assertEquals(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0)
142 .get(policy.getName()).getType(), policy.getType());
144 gotToscaServiceTemplate = databaseProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build());
146 assertEquals(getToscaPolicyFromMapList(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies(),
147 policy.getName()).getType(), policy.getType());
149 gotToscaServiceTemplate = databaseProvider.getFilteredPolicies(
150 ToscaPolicyFilter.builder().name(policy.getName()).version(policy.getVersion()).build());
152 assertEquals(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0)
153 .get(policy.getName()).getType(), policy.getType());
158 private ToscaPolicy getToscaPolicyFromMapList(List<Map<String, ToscaPolicy>> toscaPolicyMapList,
160 ToscaPolicy toscaPolicy = new ToscaPolicy();
161 for (Map<String, ToscaPolicy> policyMap : toscaPolicyMapList) {
162 if (policyMap.get(policyName) != null) {
163 toscaPolicy = policyMap.get(policyName);
170 private void createPolicyTypes() throws CoderException, PfModelException, URISyntaxException {
171 Set<String> policyTypeResources = ResourceUtils.getDirectoryContents("policytypes");
173 for (String policyTyoeResource : policyTypeResources) {
174 Object yamlObject = new Yaml().load(ResourceUtils.getResourceAsString(policyTyoeResource));
175 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
177 ToscaServiceTemplate toscaServiceTemplatePolicyType =
178 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
180 assertNotNull(toscaServiceTemplatePolicyType);
181 databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);