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