Read resource dirs in models prvider unit tests
[policy/models.git] / models-provider / src / test / java / org / onap / policy / models / provider / impl / PolicyTypePersistenceTest.java
1 /*-
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
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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27 import com.google.gson.GsonBuilder;
28
29 import java.util.Base64;
30 import java.util.List;
31 import java.util.Set;
32
33 import lombok.NonNull;
34
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;
48
49 /**
50  * Test persistence of monitoring policies to and from the database.
51  *
52  * @author Liam Fallon (liam.fallon@est.tech)
53  */
54 public class PolicyTypePersistenceTest {
55     private StandardCoder standardCoder;
56
57     private PolicyModelsProvider databaseProvider;
58
59     /**
60      * Initialize provider.
61      *
62      * @throws PfModelException on exceptions in the tests
63      */
64     @Before
65     public void setupParameters() throws PfModelException {
66         // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
67
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");
74
75         databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
76     }
77
78     /**
79      * Set up standard coder.
80      */
81     @Before
82     public void setupStandardCoder() {
83         standardCoder = new StandardCoder();
84     }
85
86     @After
87     public void teardown() throws Exception {
88         databaseProvider.close();
89     }
90
91     @Test
92     public void testPolicyTypePersistence() throws Exception {
93         Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
94
95         for (String policyTypeFilePath : policyTypeDirectoryContents) {
96             String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
97             testYamlStringPolicyTypePersistence(policyTypeString);
98         }
99     }
100
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);
104
105         testJsonStringPolicyTypePersistence(yamlAsJsonString);
106     }
107
108     /**
109      * Check persistence of a policy.
110      *
111      * @param policyTypeString the policy as a string
112      * @throws Exception any exception thrown
113      */
114     public void testJsonStringPolicyTypePersistence(@NonNull final String policyTypeString) throws Exception {
115         ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
116
117         assertNotNull(serviceTemplate);
118         ToscaPolicyType inPolicyType = serviceTemplate.getPolicyTypes().values().iterator().next();
119
120         try {
121             databaseProvider.createPolicyTypes(serviceTemplate);
122         } catch (Exception e) {
123             e.printStackTrace();
124         }
125         databaseProvider.updatePolicyTypes(serviceTemplate);
126
127         List<ToscaPolicyType> policyTypeList =
128                 databaseProvider.getPolicyTypeList(inPolicyType.getName(), inPolicyType.getVersion());
129
130         policyTypeList = databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder()
131                 .name(inPolicyType.getName()).version(inPolicyType.getVersion()).build());
132
133         assertEquals(1, policyTypeList.size());
134         assertEquals(inPolicyType.getName(), policyTypeList.get(0).getName());
135
136         policyTypeList = databaseProvider
137                 .getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().name(inPolicyType.getName()).build());
138
139         assertEquals(1, policyTypeList.size());
140         assertEquals(inPolicyType.getName(), policyTypeList.get(0).getName());
141
142         policyTypeList = databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build());
143         assertTrue(policyTypeList.size() <= 3);
144         assertEquals(inPolicyType.getName(), policyTypeList.get(0).getName());
145
146         for (ToscaPolicyType policyType : databaseProvider.getPolicyTypeList(null, null)) {
147             databaseProvider.deletePolicyType(policyType.getName(), policyType.getVersion());
148         }
149     }
150 }