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;
27 import com.google.gson.Gson;
28 import com.google.gson.JsonSyntaxException;
30 import java.io.IOException;
31 import java.sql.Connection;
32 import java.sql.DriverManager;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
38 import org.onap.policy.models.base.PfConceptKey;
39 import org.onap.policy.models.base.PfValidationResult;
40 import org.onap.policy.models.dao.DaoParameters;
41 import org.onap.policy.models.dao.PfDao;
42 import org.onap.policy.models.dao.PfDaoFactory;
43 import org.onap.policy.models.dao.impl.DefaultPfDao;
44 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy;
45 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
46 import org.onap.policy.models.tosca.simple.serialization.ToscaServiceTemplateMessageBodyHandler;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * Test persistence of monitoring policies to and from the database.
53 * @author Liam Fallon (liam.fallon@est.tech)
55 public class MonitoringPolicyPersistenceTest {
56 // Logger for this class
57 private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPolicyPersistenceTest.class);
61 private Connection connection;
65 * Set up the DAO towards the database.
67 * @throws Exception on database errors
70 public void setupDao() throws Exception {
71 // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database
72 // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance
73 connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY");
75 final DaoParameters daoParameters = new DaoParameters();
76 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
78 // Use the persistence unit ToscaConceptTest to test towards the h2 database
79 // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance
80 daoParameters.setPersistenceUnit("ToscaConceptTest");
82 pfDao = new PfDaoFactory().createPfDao(daoParameters);
83 pfDao.init(daoParameters);
90 public void setupGson() {
91 gson = new ToscaServiceTemplateMessageBodyHandler().getGson();
95 public void teardown() throws Exception {
101 public void testJsonDeserialization() throws JsonSyntaxException, IOException {
102 ToscaServiceTemplate serviceTemplate =
103 gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
104 ToscaServiceTemplate.class);
106 assertNotNull(serviceTemplate);
107 LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
108 assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
110 ToscaPolicy policyBeforeDb = serviceTemplate.getTopologyTemplate().getPolicies().get("onap.restart.tca");
112 pfDao.create(policyBeforeDb);
114 ToscaPolicy policyAfterDb = pfDao.get(ToscaPolicy.class, new PfConceptKey("onap.restart.tca:1.0.0"));
116 assertEquals(policyBeforeDb, policyAfterDb);