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.assertTrue;
27 import com.google.gson.GsonBuilder;
29 import java.util.Base64;
30 import java.util.List;
33 import lombok.NonNull;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
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.ToscaPolicyType;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
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 PolicyTypePersistenceTest {
55 private StandardCoder standardCoder;
57 private PolicyModelsProvider databaseProvider;
60 * Initialize provider.
62 * @throws PfModelException on exceptions in the tests
65 public void setupParameters() throws PfModelException {
66 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
68 PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
69 parameters.setDatabaseDriver("org.h2.Driver");
70 parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
71 parameters.setDatabaseUser("policy");
72 parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
73 parameters.setPersistenceUnit("ToscaConceptTest");
75 databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
79 * Set up standard coder.
82 public void setupStandardCoder() {
83 standardCoder = new StandardCoder();
87 public void teardown() throws Exception {
88 databaseProvider.close();
92 public void testPolicyTypePersistence() throws Exception {
93 Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
95 for (String policyTypeFilePath : policyTypeDirectoryContents) {
96 String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
97 testYamlStringPolicyTypePersistence(policyTypeString);
101 private void testYamlStringPolicyTypePersistence(final String policyTypeString) throws Exception {
102 Object yamlObject = new Yaml().load(policyTypeString);
103 String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
105 testJsonStringPolicyTypePersistence(yamlAsJsonString);
109 * Check persistence of a policy.
111 * @param policyTypeString the policy as a string
112 * @throws Exception any exception thrown
114 public void testJsonStringPolicyTypePersistence(@NonNull final String policyTypeString) throws Exception {
115 ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
117 assertNotNull(serviceTemplate);
118 ToscaPolicyType inPolicyType = serviceTemplate.getPolicyTypes().values().iterator().next();
121 databaseProvider.createPolicyTypes(serviceTemplate);
122 } catch (Exception e) {
125 databaseProvider.updatePolicyTypes(serviceTemplate);
127 List<ToscaPolicyType> policyTypeList =
128 databaseProvider.getPolicyTypeList(inPolicyType.getName(), inPolicyType.getVersion());
130 policyTypeList = databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder()
131 .name(inPolicyType.getName()).version(inPolicyType.getVersion()).build());
133 assertEquals(1, policyTypeList.size());
134 assertEquals(inPolicyType.getName(), policyTypeList.get(0).getName());
136 policyTypeList = databaseProvider
137 .getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().name(inPolicyType.getName()).build());
139 assertEquals(1, policyTypeList.size());
140 assertEquals(inPolicyType.getName(), policyTypeList.get(0).getName());
142 policyTypeList = databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build());
143 assertTrue(policyTypeList.size() <= 3);
144 assertEquals(inPolicyType.getName(), policyTypeList.get(0).getName());
146 for (ToscaPolicyType policyType : databaseProvider.getPolicyTypeList(null, null)) {
147 databaseProvider.deletePolicyType(policyType.getName(), policyType.getVersion());