2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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.assertj.core.api.Assertions.assertThatCode;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
27 import java.util.Base64;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
34 import org.onap.policy.common.utils.resources.ResourceUtils;
35 import org.onap.policy.models.base.PfModelException;
36 import org.onap.policy.models.provider.PolicyModelsProvider;
37 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
38 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityKey;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
43 * Test persistence of monitoring policies to and from the database.
45 * @author Liam Fallon (liam.fallon@est.tech)
47 public class PolicyTypePersistenceTest {
48 private YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
49 private PolicyModelsProvider databaseProvider;
52 * Initialize provider.
54 * @throws PfModelException on exceptions in the tests
57 public void setupParameters() throws PfModelException {
58 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
60 PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
61 parameters.setDatabaseDriver("org.h2.Driver");
62 parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
63 parameters.setDatabaseUser("policy");
64 parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
65 parameters.setPersistenceUnit("ToscaConceptTest");
67 databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
71 public void teardown() throws Exception {
72 databaseProvider.close();
76 public void testPolicyTypePersistence() throws Exception {
77 Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
79 ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
81 for (String policyTypeFilePath : policyTypeDirectoryContents) {
82 String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
84 ToscaServiceTemplate foundPolicyTypeSt =
85 yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
87 serviceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom());
88 serviceTemplate.setDescription(foundPolicyTypeSt.getDescription());
89 serviceTemplate.setMetadata(foundPolicyTypeSt.getMetadata());
90 serviceTemplate.setName(foundPolicyTypeSt.getName());
91 serviceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion());
92 serviceTemplate.setToscaTopologyTemplate(foundPolicyTypeSt.getToscaTopologyTemplate());
93 serviceTemplate.setVersion(foundPolicyTypeSt.getVersion());
95 if (foundPolicyTypeSt.getDataTypes() != null) {
96 if (serviceTemplate.getDataTypes() == null) {
97 serviceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
99 serviceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
103 if (serviceTemplate.getPolicyTypes() == null) {
104 serviceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
106 serviceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
110 assertThatCode(() -> databaseProvider.createPolicyTypes(serviceTemplate)).doesNotThrowAnyException();
112 ToscaEntityKey resourceOptimizationPtKey =
113 new ToscaEntityKey("onap.policies.optimization.resource.OptimizationPolicy", "1.0.0");
115 ToscaServiceTemplate resOptPolicyTypeSt = databaseProvider.getPolicyTypes(resourceOptimizationPtKey.getName(),
116 resourceOptimizationPtKey.getVersion());
118 assertEquals(3, resOptPolicyTypeSt.getPolicyTypesAsMap().size());
119 assertTrue(resOptPolicyTypeSt.getPolicyTypesAsMap().containsKey(resourceOptimizationPtKey));
121 ToscaEntityKey resourcePtKey = new ToscaEntityKey("onap.policies.optimization.Resource", "1.0.0");
122 assertTrue(resOptPolicyTypeSt.getPolicyTypesAsMap().containsKey(resourcePtKey));
124 ToscaEntityKey optimizationPtKey = new ToscaEntityKey("onap.policies.Optimization", "1.0.0");
125 assertTrue(resOptPolicyTypeSt.getPolicyTypesAsMap().containsKey(optimizationPtKey));
127 assertEquals(2, resOptPolicyTypeSt.getDataTypesAsMap().size());