summaryrefslogtreecommitdiffstats
path: root/models-provider/src/test
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@est.tech>2019-03-20 16:08:21 +0000
committerliamfallon <liam.fallon@est.tech>2019-03-20 16:08:21 +0000
commit8fc237cc606b6e9c8c7d7e7a2c811fc671a4b40e (patch)
tree1dc4369a5f94683a2140f28968a1bd32566743df /models-provider/src/test
parentd614f2f3871ea6b46b0e8b0bbd8be70414710164 (diff)
Implement persistence test for policies
The unit test MonitoringPolicySerializationTest tests persistence for policies and shows how persistence works. Issue-ID: POLICY-1195 Change-Id: I933eb538238f9ccd41ce69614e0c9afcac869c29 Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-provider/src/test')
-rw-r--r--models-provider/src/test/java/org/onap/policy/models/provider/impl/MonitoringPolicyPersistenceTest.java118
-rw-r--r--models-provider/src/test/resources/META-INF/persistence.xml73
2 files changed, 191 insertions, 0 deletions
diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/MonitoringPolicyPersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/MonitoringPolicyPersistenceTest.java
new file mode 100644
index 000000000..b26e762af
--- /dev/null
+++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/MonitoringPolicyPersistenceTest.java
@@ -0,0 +1,118 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 Nordix Foundation.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.provider.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonSyntaxException;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DriverManager;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.policy.common.utils.resources.ResourceUtils;
+import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.base.PfValidationResult;
+import org.onap.policy.models.dao.DaoParameters;
+import org.onap.policy.models.dao.PfDao;
+import org.onap.policy.models.dao.PfDaoFactory;
+import org.onap.policy.models.dao.impl.DefaultPfDao;
+import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy;
+import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.simple.serialization.ToscaServiceTemplateMessageBodyHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Test persistence of monitoring policies to and from the database.
+ *
+ * @author Liam Fallon (liam.fallon@est.tech)
+ */
+public class MonitoringPolicyPersistenceTest {
+ // Logger for this class
+ private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPolicyPersistenceTest.class);
+
+ private Gson gson;
+
+ private Connection connection;
+ private PfDao pfDao;
+
+ /**
+ * Set up the DAO towards the database.
+ *
+ * @throws Exception on database errors
+ */
+ @Before
+ public void setupDao() throws Exception {
+ // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database
+ // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance
+ connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY");
+
+ final DaoParameters daoParameters = new DaoParameters();
+ daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
+
+ // Use the persistence unit ToscaConceptTest to test towards the h2 database
+ // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance
+ daoParameters.setPersistenceUnit("ToscaConceptTest");
+
+ pfDao = new PfDaoFactory().createPfDao(daoParameters);
+ pfDao.init(daoParameters);
+ }
+
+ /**
+ * Set up GSON.
+ */
+ @Before
+ public void setupGson() {
+ gson = new ToscaServiceTemplateMessageBodyHandler().getGson();
+ }
+
+ @After
+ public void teardown() throws Exception {
+ pfDao.close();
+ connection.close();
+ }
+
+ @Test
+ public void testJsonDeserialization() throws JsonSyntaxException, IOException {
+ ToscaServiceTemplate serviceTemplate =
+ gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
+ ToscaServiceTemplate.class);
+
+ assertNotNull(serviceTemplate);
+ LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
+ assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
+
+ ToscaPolicy policyBeforeDb = serviceTemplate.getTopologyTemplate().getPolicies().get("onap.restart.tca");
+
+ pfDao.create(policyBeforeDb);
+
+ ToscaPolicy policyAfterDb = pfDao.get(ToscaPolicy.class, new PfConceptKey("onap.restart.tca:1.0.0"));
+
+ assertEquals(policyBeforeDb, policyAfterDb);
+ }
+}
diff --git a/models-provider/src/test/resources/META-INF/persistence.xml b/models-provider/src/test/resources/META-INF/persistence.xml
new file mode 100644
index 000000000..491505341
--- /dev/null
+++ b/models-provider/src/test/resources/META-INF/persistence.xml
@@ -0,0 +1,73 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ============LICENSE_START=======================================================
+ Copyright (C) 2019 Nordix Foundation.
+ ================================================================================
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+ SPDX-License-Identifier: Apache-2.0
+ ============LICENSE_END=========================================================
+-->
+
+<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
+ <persistence-unit name="ToscaConceptTest" transaction-type="RESOURCE_LOCAL">
+ <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
+
+ <class>org.onap.policy.models.dao.converters.CDataConditioner</class>
+ <class>org.onap.policy.models.dao.converters.Uuid2String</class>
+ <class>org.onap.policy.models.base.PfConceptKey</class>
+ <class>org.onap.policy.models.tosca.simple.concepts.ToscaPolicy</class>
+
+ <properties>
+ <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
+ <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:testdb" />
+ <property name="javax.persistence.jdbc.user" value="policy" />
+ <property name="javax.persistence.jdbc.password" value="P01icY" />
+ <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
+ <property name="eclipselink.ddl-generation.output-mode" value="database" />
+ <property name="eclipselink.logging.level" value="INFO" />
+ </properties>
+ </persistence-unit>
+
+ <persistence-unit name="ToscaConceptMariaDBTest" transaction-type="RESOURCE_LOCAL">
+ <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
+
+ <class>org.onap.policy.models.dao.converters.CDataConditioner</class>
+ <class>org.onap.policy.models.dao.converters.Uuid2String</class>
+ <class>org.onap.policy.models.base.PfConceptKey</class>
+ <class>org.onap.policy.models.tosca.simple.concepts.ToscaPolicy</class>
+
+ <properties>
+ <property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver" />
+ <property name="javax.persistence.jdbc.url" value="jdbc:mariadb://localhost:3306/policy" />
+ <property name="javax.persistence.jdbc.user" value="policy" />
+ <property name="javax.persistence.jdbc.password" value="P01icY" />
+ <property name="javax.persistence.schema-generation.database.action" value="create" />
+
+ <!-- property name="eclipselink.logging.level" value="ALL" />
+ <property name="eclipselink.logging.level.jpa" value="ALL" />
+ <property name="eclipselink.logging.level.ddl" value="ALL" />
+ <property name="eclipselink.logging.level.connection" value="ALL" />
+ <property name="eclipselink.logging.level.sql" value="ALL" />
+ <property name="eclipselink.logging.level.transaction" value="ALL" />
+ <property name="eclipselink.logging.level.sequencing" value="ALL" />
+ <property name="eclipselink.logging.level.server" value="ALL" />
+ <property name="eclipselink.logging.level.query" value="ALL" />
+ <property name="eclipselink.logging.level.properties" value="ALL" /-->
+
+ <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
+ <property name="eclipselink.ddl-generation.output-mode" value="database" />
+ <property name="eclipselink.logging.level" value="INFO" />
+ </properties>
+ </persistence-unit>
+</persistence>