Implement serialization/deserialization for TOSCA concepts
[policy/models.git] / models-provider / src / test / java / org / onap / policy / models / provider / impl / MonitoringPolicyPersistenceTest.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 com.google.gson.Gson;
28 import com.google.gson.JsonSyntaxException;
29
30 import java.io.IOException;
31 import java.sql.Connection;
32 import java.sql.DriverManager;
33
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;
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 MonitoringPolicyPersistenceTest {
56     // Logger for this class
57     private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPolicyPersistenceTest.class);
58
59     private Gson gson;
60
61     private Connection connection;
62     private PfDao pfDao;
63
64     /**
65      * Set up the DAO towards the database.
66      *
67      * @throws Exception on database errors
68      */
69     @Before
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");
74
75         final DaoParameters daoParameters = new DaoParameters();
76         daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
77
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");
81
82         pfDao = new PfDaoFactory().createPfDao(daoParameters);
83         pfDao.init(daoParameters);
84     }
85
86     /**
87      * Set up GSON.
88      */
89     @Before
90     public void setupGson() {
91         gson = new ToscaServiceTemplateMessageBodyHandler().getGson();
92     }
93
94     @After
95     public void teardown() throws Exception {
96         pfDao.close();
97         connection.close();
98     }
99
100     @Test
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);
105
106         assertNotNull(serviceTemplate);
107         LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
108         assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
109
110         ToscaPolicy policyBeforeDb = serviceTemplate.getTopologyTemplate().getPolicies().get("onap.restart.tca");
111
112         pfDao.create(policyBeforeDb);
113
114         ToscaPolicy policyAfterDb = pfDao.get(ToscaPolicy.class, new PfConceptKey("onap.restart.tca:1.0.0"));
115
116         assertEquals(policyBeforeDb, policyAfterDb);
117     }
118 }