diff options
author | liamfallon <liam.fallon@est.tech> | 2020-01-27 19:33:25 -0500 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2020-01-28 09:58:17 -0500 |
commit | 252dc4ea8da89739ed18d6509ea72d2321676034 (patch) | |
tree | dc10e683f267261bacf02dd4179bb13d529c2c62 /models-tosca/src/test/java/org | |
parent | f77d0a91298e4e4ec14991366b5e439824919d5d (diff) |
Add service template persistence provider
Persistence needs to be carried out at service template level in order
to guarantee consistency of the service template in the database as much
as possible. Therefire, service template lelve reading, writing, and
delting is required.
Issue-ID: POLICY-1402
Change-Id: I5be57c29381bf6ddc87afdbb64ddae66a5ea5e5f
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-tosca/src/test/java/org')
2 files changed, 174 insertions, 0 deletions
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java index 63ebcbd24..03c7f9b1b 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java @@ -61,6 +61,7 @@ public class SimpleToscaProviderTest { private static final String TEMPLATE_IS_NULL = "^serviceTemplate is marked .*on.*ull but is null$"; private static final String VCPE_INPUT_JSON = "policies/vCPE.policy.monitoring.input.tosca.json"; private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$"; + private PfDao pfDao; private StandardCoder standardCoder; diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaServiceTemplateProviderTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaServiceTemplateProviderTest.java new file mode 100644 index 000000000..791928a0e --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaServiceTemplateProviderTest.java @@ -0,0 +1,173 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. + * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.tosca.simple.provider; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.util.Properties; + +import org.eclipse.persistence.config.PersistenceUnitProperties; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfModelException; +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.JpaToscaDataType; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; + +/** + * Test the {@link SimpleToscaProvider} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class SimpleToscaServiceTemplateProviderTest { + private static final String TEMPLATE_IS_NULL = "^serviceTemplate is marked .*on.*ull but is null$"; + private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$"; + + private PfDao pfDao; + + /** + * Set up the DAO towards the database. + * + * @throws Exception on database errors + */ + @Before + public void setupDao() throws Exception { + final DaoParameters daoParameters = new DaoParameters(); + daoParameters.setPluginClass(DefaultPfDao.class.getName()); + + daoParameters.setPersistenceUnit("ToscaConceptTest"); + + Properties jdbcProperties = new Properties(); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy"); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY"); + + // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver"); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb"); + + daoParameters.setJdbcProperties(jdbcProperties); + + pfDao = new PfDaoFactory().createPfDao(daoParameters); + pfDao.init(daoParameters); + } + + @After + public void teardown() { + pfDao.close(); + } + + @Test + public void testCreateUpdateGetDeleteDataType() throws PfModelException { + JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); + + JpaToscaServiceTemplate overwrittenServiceTemplate = + new SimpleToscaServiceTemplateProvider().write(pfDao, serviceTemplate); + + assertNull(overwrittenServiceTemplate); + + JpaToscaServiceTemplate readServiceTemplate = new SimpleToscaServiceTemplateProvider().read(pfDao); + assertEquals(serviceTemplate, readServiceTemplate); + + assertNull(readServiceTemplate.getDataTypes()); + + PfConceptKey dataType0Key = new PfConceptKey("DataType0", "0.0.1"); + JpaToscaDataType dataType0 = new JpaToscaDataType(); + dataType0.setKey(dataType0Key); + serviceTemplate.setDataTypes(new JpaToscaDataTypes()); + serviceTemplate.getDataTypes().getConceptMap().put(dataType0Key, dataType0); + + overwrittenServiceTemplate = new SimpleToscaServiceTemplateProvider().write(pfDao, serviceTemplate); + assertEquals(readServiceTemplate, overwrittenServiceTemplate); + + readServiceTemplate = new SimpleToscaServiceTemplateProvider().read(pfDao); + assertEquals(serviceTemplate, readServiceTemplate); + + assertEquals(1, readServiceTemplate.getDataTypes().getConceptMap().size()); + assertEquals(dataType0, readServiceTemplate.getDataTypes().get(dataType0Key)); + assertNull(readServiceTemplate.getDataTypes().get(dataType0Key).getDescription()); + + dataType0.setDescription("Updated Description"); + + overwrittenServiceTemplate = new SimpleToscaServiceTemplateProvider().write(pfDao, serviceTemplate); + assertEquals(readServiceTemplate, overwrittenServiceTemplate); + + readServiceTemplate = new SimpleToscaServiceTemplateProvider().read(pfDao); + assertEquals(serviceTemplate, readServiceTemplate); + + assertEquals(dataType0, readServiceTemplate.getDataTypes().get(dataType0Key)); + assertEquals("Updated Description", readServiceTemplate.getDataTypes().get(dataType0Key).getDescription()); + + PfConceptKey policyType0Key = new PfConceptKey("PolicyType0", "0.0.1"); + + JpaToscaPolicyType policyType0 = new JpaToscaPolicyType(); + + policyType0.setKey(policyType0Key); + serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes()); + + serviceTemplate.getPolicyTypes().getConceptMap().put(policyType0Key, policyType0); + + overwrittenServiceTemplate = new SimpleToscaServiceTemplateProvider().write(pfDao, serviceTemplate); + assertEquals(readServiceTemplate, overwrittenServiceTemplate); + + readServiceTemplate = new SimpleToscaServiceTemplateProvider().read(pfDao); + assertEquals(serviceTemplate, readServiceTemplate); + + JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaServiceTemplateProvider().delete(pfDao); + assertEquals(serviceTemplate, deletedServiceTemplate); + + readServiceTemplate = new SimpleToscaServiceTemplateProvider().read(pfDao); + assertNull(readServiceTemplate); + } + + @Test + public void testNonNulls() { + assertThatThrownBy(() -> { + new SimpleToscaServiceTemplateProvider().write(null, null); + }).hasMessageMatching(DAO_IS_NULL); + + assertThatThrownBy(() -> { + new SimpleToscaServiceTemplateProvider().write(pfDao, null); + }).hasMessageMatching(TEMPLATE_IS_NULL); + + assertThatThrownBy(() -> { + new SimpleToscaServiceTemplateProvider().write(null, new JpaToscaServiceTemplate()); + }).hasMessageMatching(DAO_IS_NULL); + + assertThatThrownBy(() -> { + new SimpleToscaServiceTemplateProvider().read(null); + }).hasMessageMatching(DAO_IS_NULL); + + assertThatThrownBy(() -> { + new SimpleToscaServiceTemplateProvider().delete(null); + }).hasMessageMatching(DAO_IS_NULL); + } +} |