Merge "Implement validation and hierarchical get"
[policy/models.git] / models-provider / src / test / java / org / onap / policy / models / provider / impl / PolicyTypePersistenceTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.provider.impl;
22
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.Base64;
28 import java.util.Set;
29
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;
41
42 /**
43  * Test persistence of monitoring policies to and from the database.
44  *
45  * @author Liam Fallon (liam.fallon@est.tech)
46  */
47 public class PolicyTypePersistenceTest {
48     private YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
49     private PolicyModelsProvider databaseProvider;
50
51     /**
52      * Initialize provider.
53      *
54      * @throws PfModelException on exceptions in the tests
55      */
56     @Before
57     public void setupParameters() throws PfModelException {
58         // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
59
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");
66
67         databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
68     }
69
70     @After
71     public void teardown() throws Exception {
72         databaseProvider.close();
73     }
74
75     @Test
76     public void testPolicyTypePersistence() throws Exception {
77         Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
78
79         ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
80
81         for (String policyTypeFilePath : policyTypeDirectoryContents) {
82             String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
83
84             ToscaServiceTemplate foundPolicyTypeSt =
85                     yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
86
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());
94
95             if (foundPolicyTypeSt.getDataTypes() != null) {
96                 if (serviceTemplate.getDataTypes() == null) {
97                     serviceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
98                 } else {
99                     serviceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
100                 }
101             }
102
103             if (serviceTemplate.getPolicyTypes() == null) {
104                 serviceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
105             } else {
106                 serviceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
107             }
108         }
109
110         assertThatCode(() -> databaseProvider.createPolicyTypes(serviceTemplate)).doesNotThrowAnyException();
111
112         ToscaEntityKey resourceOptimizationPtKey =
113                 new ToscaEntityKey("onap.policies.optimization.resource.OptimizationPolicy", "1.0.0");
114
115         ToscaServiceTemplate resOptPolicyTypeSt = databaseProvider.getPolicyTypes(resourceOptimizationPtKey.getName(),
116                 resourceOptimizationPtKey.getVersion());
117
118         assertEquals(3, resOptPolicyTypeSt.getPolicyTypesAsMap().size());
119         assertTrue(resOptPolicyTypeSt.getPolicyTypesAsMap().containsKey(resourceOptimizationPtKey));
120
121         ToscaEntityKey resourcePtKey = new ToscaEntityKey("onap.policies.optimization.Resource", "1.0.0");
122         assertTrue(resOptPolicyTypeSt.getPolicyTypesAsMap().containsKey(resourcePtKey));
123
124         ToscaEntityKey optimizationPtKey = new ToscaEntityKey("onap.policies.Optimization", "1.0.0");
125         assertTrue(resOptPolicyTypeSt.getPolicyTypesAsMap().containsKey(optimizationPtKey));
126
127         assertEquals(2, resOptPolicyTypeSt.getDataTypesAsMap().size());
128     }
129 }