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 java.util.Base64;
28 import java.util.List;
31 import lombok.NonNull;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.provider.PolicyModelsProvider;
40 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
41 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
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 PolicyTypePersistenceTest {
53 private StandardCoder standardCoder;
55 private PolicyModelsProvider databaseProvider;
58 * Initialize provider.
60 * @throws PfModelException on exceptions in the tests
63 public void setupParameters() throws PfModelException {
64 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
66 PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
67 parameters.setDatabaseDriver("org.h2.Driver");
68 parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
69 parameters.setDatabaseUser("policy");
70 parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
71 parameters.setPersistenceUnit("ToscaConceptTest");
73 databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
77 * Set up standard coder.
80 public void setupStandardCoder() {
81 standardCoder = new StandardCoder();
85 public void teardown() throws Exception {
86 databaseProvider.close();
90 public void testPolicyTypePersistence() throws Exception {
91 Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
93 for (String policyTypeFilePath : policyTypeDirectoryContents) {
94 String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
95 testYamlStringPolicyTypePersistence(policyTypeString);
99 private void testYamlStringPolicyTypePersistence(final String policyTypeString) throws Exception {
100 Object yamlObject = new Yaml().load(policyTypeString);
101 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
103 testJsonStringPolicyTypePersistence(yamlAsJsonString);
107 * Check persistence of a policy.
109 * @param policyTypeString the policy as a string
110 * @throws Exception any exception thrown
112 public void testJsonStringPolicyTypePersistence(@NonNull final String policyTypeString) throws Exception {
113 ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
115 assertNotNull(serviceTemplate);
116 ToscaPolicyType inPolicyType = serviceTemplate.getPolicyTypes().values().iterator().next();
118 databaseProvider.createPolicyTypes(serviceTemplate);
119 checkPolicyTypePersistence(inPolicyType);
121 databaseProvider.updatePolicyTypes(serviceTemplate);
122 checkPolicyTypePersistence(inPolicyType);
125 private void checkPolicyTypePersistence(ToscaPolicyType inPolicyType) throws PfModelException {
126 List<ToscaPolicyType> policyTypeList =
127 databaseProvider.getPolicyTypeList(inPolicyType.getName(), inPolicyType.getVersion());
129 policyTypeList = databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder()
130 .name(inPolicyType.getName()).version(inPolicyType.getVersion()).build());
132 assertEquals(1, policyTypeList.size());
133 assertEquals(inPolicyType.getName(), policyTypeList.get(0).getName());
135 policyTypeList = databaseProvider
136 .getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().name(inPolicyType.getName()).build());
138 assertEquals(1, policyTypeList.size());
139 assertEquals(inPolicyType.getName(), policyTypeList.get(0).getName());
141 policyTypeList = databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build());
142 assertTrue(policyTypeList.size() <= 3);
143 assertEquals(inPolicyType.getName(), policyTypeList.get(0).getName());
145 for (ToscaPolicyType policyType : databaseProvider.getPolicyTypeList(null, null)) {
146 databaseProvider.deletePolicyType(policyType.getName(), policyType.getVersion());