Implement validation and hierarchical get
[policy/models.git] / models-provider / src / test / java / org / onap / policy / models / provider / impl / PolicyToscaPersistenceTest.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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25
26 import java.net.URISyntaxException;
27 import java.util.Base64;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
31
32 import lombok.NonNull;
33
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.policy.common.utils.coder.CoderException;
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.ToscaPolicy;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
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 PolicyToscaPersistenceTest {
55     private StandardCoder standardCoder;
56
57     private PolicyModelsProvider databaseProvider;
58
59     /**
60      * Initialize provider.
61      *
62      * @throws PfModelException on exceptions in the tests
63      * @throws CoderException on JSON encoding and decoding errors
64      */
65     @Before
66     public void setupParameters() throws Exception {
67         // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
68
69         PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
70         parameters.setDatabaseDriver("org.h2.Driver");
71         parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
72         parameters.setDatabaseUser("policy");
73         parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
74         parameters.setPersistenceUnit("ToscaConceptTest");
75
76         databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
77
78         createPolicyTypes();
79     }
80
81     /**
82      * Set up standard coder.
83      */
84     @Before
85     public void setupStandardCoder() {
86         standardCoder = new StandardCoder();
87     }
88
89     @After
90     public void teardown() throws Exception {
91         databaseProvider.close();
92     }
93
94     @Test
95     public void testToscaPolicyPersistence() throws Exception {
96         Set<String> policyResources = ResourceUtils.getDirectoryContents("policies");
97
98         for (String policyResource : policyResources) {
99             if (!policyResource.contains("\\.tosca\\.")) {
100                 continue;
101             }
102
103             String policyString = ResourceUtils.getResourceAsString(policyResource);
104
105             if (policyResource.endsWith("yaml")) {
106                 testYamlStringPolicyPersistence(policyString);
107             } else {
108                 testJsonStringPolicyPersistence(policyString);
109             }
110         }
111     }
112
113     private void testYamlStringPolicyPersistence(final String policyString) throws Exception {
114         Object yamlObject = new Yaml().load(policyString);
115         String yamlAsJsonString = new StandardCoder().encode(yamlObject);
116
117         testJsonStringPolicyPersistence(yamlAsJsonString);
118     }
119
120     /**
121      * Check persistence of a policy.
122      *
123      * @param policyString the policy as a string
124      * @throws Exception any exception thrown
125      */
126     public void testJsonStringPolicyPersistence(@NonNull final String policyString) throws Exception {
127         ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
128
129         assertNotNull(serviceTemplate);
130
131         databaseProvider.createPolicies(serviceTemplate);
132         databaseProvider.updatePolicies(serviceTemplate);
133
134         for (Map<String, ToscaPolicy> policyMap : serviceTemplate.getToscaTopologyTemplate().getPolicies()) {
135             for (ToscaPolicy policy : policyMap.values()) {
136                 ToscaServiceTemplate gotToscaServiceTemplate =
137                         databaseProvider.getPolicies(policy.getName(), policy.getVersion());
138
139                 assertEquals(policy.getType(), gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0)
140                         .get(policy.getName()).getType());
141
142                 gotToscaServiceTemplate = databaseProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build());
143
144                 assertEquals(policy.getType(),
145                         getToscaPolicyFromMapList(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies(),
146                                 policy.getName()).getType());
147
148                 gotToscaServiceTemplate = databaseProvider.getFilteredPolicies(
149                         ToscaPolicyFilter.builder().name(policy.getName()).version(policy.getVersion()).build());
150
151                 assertEquals(policy.getType(), gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0)
152                         .get(policy.getName()).getType());
153             }
154         }
155     }
156
157     private ToscaPolicy getToscaPolicyFromMapList(List<Map<String, ToscaPolicy>> toscaPolicyMapList,
158             String policyName) {
159         ToscaPolicy toscaPolicy = new ToscaPolicy();
160         for (Map<String, ToscaPolicy> policyMap : toscaPolicyMapList) {
161             toscaPolicy = policyMap.get(policyName);
162             if (toscaPolicy != null) {
163                 break;
164             }
165         }
166         return toscaPolicy;
167     }
168
169     private void createPolicyTypes() throws CoderException, PfModelException, URISyntaxException {
170         Set<String> policyTypeResources = ResourceUtils.getDirectoryContents("policytypes");
171
172         for (String policyTypeResource : policyTypeResources) {
173             Object yamlObject = new Yaml().load(ResourceUtils.getResourceAsString(policyTypeResource));
174             String yamlAsJsonString = new StandardCoder().encode(yamlObject);
175
176             ToscaServiceTemplate toscaServiceTemplatePolicyType =
177                     standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
178
179             assertNotNull(toscaServiceTemplatePolicyType);
180             databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);
181         }
182     }
183 }