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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.provider.impl;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
28 import com.google.gson.Gson;
29 import com.google.gson.GsonBuilder;
31 import java.util.Base64;
33 import lombok.NonNull;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.common.utils.resources.ResourceUtils;
39 import org.onap.policy.models.base.PfConceptKey;
40 import org.onap.policy.models.base.PfModelException;
41 import org.onap.policy.models.base.PfValidationResult;
42 import org.onap.policy.models.provider.PolicyModelsProvider;
43 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
44 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
45 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy;
46 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
47 import org.onap.policy.models.tosca.simple.serialization.ToscaServiceTemplateMessageBodyHandler;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.yaml.snakeyaml.Yaml;
53 * Test persistence of monitoring policies to and from the database.
55 * @author Liam Fallon (liam.fallon@est.tech)
57 public class PolicyToscaPersistenceTest {
58 // Logger for this class
59 private static final Logger LOGGER = LoggerFactory.getLogger(PolicyToscaPersistenceTest.class);
63 private PolicyModelsProvider databaseProvider;
66 private String[] policyResourceNames = {
67 "policies/vCPE.policy.monitoring.input.tosca.json",
68 "policies/vCPE.policy.monitoring.input.tosca.yaml",
69 "policies/vCPE.policy.operational.input.tosca.yaml",
70 "policies/vDNS.policy.guard.frequency.input.tosca.json",
71 "policies/vDNS.policy.guard.frequency.input.tosca.yaml",
72 "policies/vDNS.policy.monitoring.input.tosca.json",
73 "policies/vDNS.policy.monitoring.input.tosca.yaml",
74 "policies/vDNS.policy.operational.input.tosca.yaml",
75 "policies/vFirewall.policy.monitoring.input.tosca.json",
76 "policies/vFirewall.policy.monitoring.input.tosca.yaml",
77 "policies/vFirewall.policy.operational.input.tosca.json",
78 "policies/vFirewall.policy.operational.input.tosca.yaml"
83 * Initialize provider.
85 * @throws PfModelException on exceptions in the tests
88 public void setupParameters() throws PfModelException {
89 PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
90 parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
91 parameters.setDatabaseUser("policy");
92 parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
93 parameters.setPersistenceUnit("ToscaConceptTest");
95 databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
96 databaseProvider.init();
103 public void setupGson() {
104 gson = new ToscaServiceTemplateMessageBodyHandler().getGson();
108 public void teardown() throws Exception {
109 databaseProvider.close();
113 public void testPolicyPersistence() {
115 for (String policyResourceName : policyResourceNames) {
116 String policyString = ResourceUtils.getResourceAsString(policyResourceName);
118 if (policyResourceName.endsWith("yaml")) {
119 testYamlStringPolicyPersistence(policyString);
121 testJsonStringPolicyPersistence(policyString);
124 } catch (Exception exc) {
125 LOGGER.warn("error processing policies", exc);
126 fail("test should not throw an exception");
130 private void testYamlStringPolicyPersistence(final String policyString) throws Exception {
131 Object yamlObject = new Yaml().load(policyString);
132 String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
134 testJsonStringPolicyPersistence(yamlAsJsonString);
138 * Check persistence of a policy.
140 * @param policyString the policy as a string
141 * @throws Exception any exception thrown
143 public void testJsonStringPolicyPersistence(@NonNull final String policyString) throws Exception {
144 ToscaServiceTemplate serviceTemplate = gson.fromJson(policyString, ToscaServiceTemplate.class);
146 assertNotNull(serviceTemplate);
147 LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
148 assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
150 databaseProvider.createPolicies(serviceTemplate);
152 for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
153 ToscaPolicy incomingPolicy = serviceTemplate.getTopologyTemplate().getPolicies().get(policyKey);
154 ToscaPolicy databasePolicy =
155 databaseProvider.getPolicies(policyKey).getTopologyTemplate().getPolicies().get(policyKey);
156 assertEquals(incomingPolicy, databasePolicy);