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;
28 import java.util.List;
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.yaml.snakeyaml.Yaml;
50 * Test persistence of monitoring policies to and from the database.
52 * @author Liam Fallon (liam.fallon@est.tech)
54 public class PolicyToscaPersistenceTest {
55 private StandardCoder standardCoder;
57 private PolicyModelsProvider databaseProvider;
60 * Initialize provider.
62 * @throws PfModelException on exceptions in the tests
63 * @throws CoderException on JSON encoding and decoding errors
66 public void setupParameters() throws Exception {
67 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
69 PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
70 parameters.setDatabaseDriver("org.h2.Driver");
71 parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
72 parameters.setDatabaseUser("policy");
73 parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
74 parameters.setPersistenceUnit("ToscaConceptTest");
76 databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
82 * Set up standard coder.
85 public void setupStandardCoder() {
86 standardCoder = new StandardCoder();
90 public void teardown() throws Exception {
91 databaseProvider.close();
95 public void testPolicyPersistence() throws Exception {
96 Set<String> policyResources = ResourceUtils.getDirectoryContents("policies");
98 for (String policyResource : policyResources) {
99 if (!policyResource.contains("\\.tosca\\.")) {
103 String policyString = ResourceUtils.getResourceAsString(policyResource);
105 if (policyResource.endsWith("yaml")) {
106 testYamlStringPolicyPersistence(policyString);
108 testJsonStringPolicyPersistence(policyString);
113 private void testYamlStringPolicyPersistence(final String policyString) throws Exception {
114 Object yamlObject = new Yaml().load(policyString);
115 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
117 testJsonStringPolicyPersistence(yamlAsJsonString);
121 * Check persistence of a policy.
123 * @param policyString the policy as a string
124 * @throws Exception any exception thrown
126 public void testJsonStringPolicyPersistence(@NonNull final String policyString) throws Exception {
127 ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
129 assertNotNull(serviceTemplate);
131 databaseProvider.createPolicies(serviceTemplate);
132 databaseProvider.updatePolicies(serviceTemplate);
134 for (Map<String, ToscaPolicy> policyMap : serviceTemplate.getToscaTopologyTemplate().getPolicies()) {
135 for (ToscaPolicy policy : policyMap.values()) {
136 ToscaServiceTemplate gotToscaServiceTemplate =
137 databaseProvider.getPolicies(policy.getName(), policy.getVersion());
139 assertEquals(policy.getType(), gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0)
140 .get(policy.getName()).getType());
142 gotToscaServiceTemplate = databaseProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build());
144 assertEquals(policy.getType(),
145 getToscaPolicyFromMapList(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies(),
146 policy.getName()).getType());
148 gotToscaServiceTemplate = databaseProvider.getFilteredPolicies(
149 ToscaPolicyFilter.builder().name(policy.getName()).version(policy.getVersion()).build());
151 assertEquals(policy.getType(), gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0)
152 .get(policy.getName()).getType());
157 private ToscaPolicy getToscaPolicyFromMapList(List<Map<String, ToscaPolicy>> toscaPolicyMapList,
159 ToscaPolicy toscaPolicy = new ToscaPolicy();
160 for (Map<String, ToscaPolicy> policyMap : toscaPolicyMapList) {
161 toscaPolicy = policyMap.get(policyName);
162 if (toscaPolicy != null) {
169 private void createPolicyTypes() throws CoderException, PfModelException, URISyntaxException {
170 Set<String> policyTypeResources = ResourceUtils.getDirectoryContents("policytypes");
172 for (String policyTypeResource : policyTypeResources) {
173 Object yamlObject = new Yaml().load(ResourceUtils.getResourceAsString(policyTypeResource));
174 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
176 ToscaServiceTemplate toscaServiceTemplatePolicyType =
177 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
179 assertNotNull(toscaServiceTemplatePolicyType);
180 databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);