Fix database properties
[policy/models.git] / models-provider / src / test / java / org / onap / policy / models / provider / impl / PolicyPersistenceTest.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.fail;
26
27 import com.google.gson.GsonBuilder;
28
29 import java.util.Base64;
30 import java.util.Map;
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.StandardCoder;
38 import org.onap.policy.common.utils.resources.ResourceUtils;
39 import org.onap.policy.models.base.PfModelException;
40 import org.onap.policy.models.provider.PolicyModelsProvider;
41 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
42 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.yaml.snakeyaml.Yaml;
49
50 /**
51  * Test persistence of monitoring policies to and from the database.
52  *
53  * @author Liam Fallon (liam.fallon@est.tech)
54  */
55 public class PolicyPersistenceTest {
56     // Logger for this class
57     private static final Logger LOGGER = LoggerFactory.getLogger(PolicyPersistenceTest.class);
58
59     private StandardCoder standardCoder;
60
61     private PolicyModelsProvider databaseProvider;
62
63     // @formatter:off
64     private String[] policyResourceNames = {
65         "policies/vCPE.policy.monitoring.input.tosca.json",
66         "policies/vCPE.policy.monitoring.input.tosca.yaml",
67         "policies/vCPE.policy.operational.input.tosca.yaml",
68         "policies/vDNS.policy.guard.frequency.input.tosca.json",
69         "policies/vDNS.policy.guard.frequency.input.tosca.yaml",
70         "policies/vDNS.policy.monitoring.input.tosca.json",
71         "policies/vDNS.policy.monitoring.input.tosca.yaml",
72         "policies/vDNS.policy.operational.input.tosca.yaml",
73         "policies/vFirewall.policy.monitoring.input.tosca.json",
74         "policies/vFirewall.policy.monitoring.input.tosca.yaml",
75         "policies/vFirewall.policy.operational.input.tosca.json",
76         "policies/vFirewall.policy.operational.input.tosca.yaml"
77     };
78     // @formatter:on
79
80     /**
81      * Initialize provider.
82      *
83      * @throws PfModelException on exceptions in the tests
84      */
85     @Before
86     public void setupParameters() throws PfModelException {
87         PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
88         parameters.setDatabaseDriver("org.h2.Driver");
89         parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
90         parameters.setDatabaseUser("policy");
91         parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
92         parameters.setPersistenceUnit("ToscaConceptTest");
93
94         databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
95     }
96
97     /**
98      * Set up GSON.
99      */
100     @Before
101     public void setupGson() {
102         standardCoder = new StandardCoder();
103     }
104
105     @After
106     public void teardown() throws Exception {
107         databaseProvider.close();
108     }
109
110     @Test
111     public void testPolicyPersistence() {
112         try {
113             for (String policyResourceName : policyResourceNames) {
114                 String policyString = ResourceUtils.getResourceAsString(policyResourceName);
115
116                 if (policyResourceName.endsWith("yaml")) {
117                     testYamlStringPolicyPersistence(policyString);
118                 } else {
119                     testJsonStringPolicyPersistence(policyString);
120                 }
121             }
122         } catch (Exception exc) {
123             LOGGER.warn("error processing policies", exc);
124             fail("test should not throw an exception");
125         }
126     }
127
128     private void testYamlStringPolicyPersistence(final String policyString) throws Exception {
129         Object yamlObject = new Yaml().load(policyString);
130         String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
131
132         testJsonStringPolicyPersistence(yamlAsJsonString);
133     }
134
135     /**
136      * Check persistence of a policy.
137      *
138      * @param policyString the policy as a string
139      * @throws Exception any exception thrown
140      */
141     public void testJsonStringPolicyPersistence(@NonNull final String policyString) throws Exception {
142         ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
143
144         assertNotNull(serviceTemplate);
145
146         databaseProvider.createPolicies(serviceTemplate);
147         databaseProvider.updatePolicies(serviceTemplate);
148
149         for (Map<String, ToscaPolicy> policyMap : serviceTemplate.getToscaTopologyTemplate().getPolicies()) {
150             for (ToscaPolicy policy : policyMap.values()) {
151                 ToscaServiceTemplate gotToscaServiceTemplate =
152                         databaseProvider.getPolicies(policy.getName(), policy.getVersion());
153
154                 assertEquals(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0)
155                         .get(policy.getName()).getType(), policy.getType());
156
157                 gotToscaServiceTemplate = databaseProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build());
158
159                 assertEquals(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0)
160                         .get(policy.getName()).getType(), policy.getType());
161
162                 gotToscaServiceTemplate = databaseProvider.getFilteredPolicies(
163                         ToscaPolicyFilter.builder().name(policy.getName()).version(policy.getVersion()).build());
164
165                 assertEquals(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0)
166                         .get(policy.getName()).getType(), policy.getType());
167             }
168         }
169     }
170 }