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.assertNotNull;
+import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-import java.sql.Connection;
-import java.sql.DriverManager;
+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.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.dao.PfDaoFactory;
import org.onap.policy.models.dao.impl.DefaultPfDao;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
+import org.yaml.snakeyaml.Yaml;
/**
* Test the {@link SimpleToscaProvider} class.
* @author Liam Fallon (liam.fallon@est.tech)
*/
public class SimpleToscaProviderTest {
- private Connection connection;
private PfDao pfDao;
private StandardCoder standardCoder;
-
/**
* Set up the DAO towards the database.
*
*/
@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");
+ 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() throws Exception {
pfDao.close();
- connection.close();
}
@Test
public void testPoliciesGet() throws Exception {
- try {
- new SimpleToscaProvider().getPolicies(null, null);
- fail("test should throw an exception here");
- } catch (Exception exc) {
- assertEquals("dao is marked @NonNull but is null", exc.getMessage());
- }
-
- try {
- new SimpleToscaProvider().getPolicies(null, new PfConceptKey());
- fail("test should throw an exception here");
- } catch (Exception exc) {
- assertEquals("dao is marked @NonNull but is null", exc.getMessage());
- }
-
- try {
- new SimpleToscaProvider().getPolicies(pfDao, null);
- fail("test should throw an exception here");
- } catch (Exception exc) {
- assertEquals("policyKey is marked @NonNull but is null", exc.getMessage());
- }
-
ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
ToscaServiceTemplate.class);
+ createPolicyTypes();
+
JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
JpaToscaServiceTemplate gotServiceTemplate =
- new SimpleToscaProvider().getPolicies(pfDao, new PfConceptKey(policyKey));
+ new SimpleToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion());
assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
gotServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
@Test
public void testPolicyCreate() throws Exception {
- try {
- new SimpleToscaProvider().createPolicies(null, null);
- fail("test should throw an exception here");
- } catch (Exception exc) {
- assertEquals("dao is marked @NonNull but is null", exc.getMessage());
- }
-
- try {
- new SimpleToscaProvider().createPolicies(null, new JpaToscaServiceTemplate());
- fail("test should throw an exception here");
- } catch (Exception exc) {
- assertEquals("dao is marked @NonNull but is null", exc.getMessage());
- }
-
- try {
- new SimpleToscaProvider().createPolicies(pfDao, null);
- fail("test should throw an exception here");
- } catch (Exception exc) {
- assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage());
- }
-
ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
ToscaServiceTemplate.class);
+ createPolicyTypes();
+
JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
@Test
public void testPolicyUpdate() throws Exception {
- try {
- new SimpleToscaProvider().updatePolicies(null, null);
- fail("test should throw an exception here");
- } catch (Exception exc) {
- assertEquals("dao is marked @NonNull but is null", exc.getMessage());
- }
-
- try {
- new SimpleToscaProvider().updatePolicies(null, new JpaToscaServiceTemplate());
- fail("test should throw an exception here");
- } catch (Exception exc) {
- assertEquals("dao is marked @NonNull but is null", exc.getMessage());
- }
-
- try {
- new SimpleToscaProvider().updatePolicies(pfDao, null);
- fail("test should throw an exception here");
- } catch (Exception exc) {
- assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage());
- }
-
ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
ToscaServiceTemplate.class);
+ createPolicyTypes();
+
JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
@Test
public void testPoliciesDelete() throws Exception {
- try {
- new SimpleToscaProvider().deletePolicies(null, null);
- fail("test should throw an exception here");
- } catch (Exception exc) {
- assertEquals("dao is marked @NonNull but is null", exc.getMessage());
- }
-
- try {
- new SimpleToscaProvider().deletePolicies(null, new PfConceptKey());
- fail("test should throw an exception here");
- } catch (Exception exc) {
- assertEquals("dao is marked @NonNull but is null", exc.getMessage());
- }
-
- try {
- new SimpleToscaProvider().deletePolicies(pfDao, null);
- fail("test should throw an exception here");
- } catch (Exception exc) {
- assertEquals("policyKey is marked @NonNull but is null", exc.getMessage());
- }
-
ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
ToscaServiceTemplate.class);
+ createPolicyTypes();
+
JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
JpaToscaServiceTemplate deletedServiceTemplate =
- new SimpleToscaProvider().deletePolicies(pfDao, new PfConceptKey(policyKey));
+ new SimpleToscaProvider().deletePolicy(pfDao, new PfConceptKey(policyKey));
assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
- try {
- new SimpleToscaProvider().getPolicies(pfDao, new PfConceptKey(policyKey));
- fail("test should throw an exception here");
- } catch (Exception exc) {
- assertEquals("policy not found: onap.restart.tca:1.0.0", exc.getMessage());
- }
+ assertTrue(new SimpleToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion())
+ .getTopologyTemplate().getPolicies().getConceptMap().isEmpty());
}
@Test
assertEquals("list of policies specified on topology template of service template is empty",
exc.getMessage());
}
+ }
+
+ @Test
+ public void testNonNulls() {
+ assertThatThrownBy(() -> {
+ new SimpleToscaProvider().getPolicyTypes(null, null, null);
+ }).hasMessage("dao is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaProvider().createPolicyTypes(null, null);
+ }).hasMessage("dao is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaProvider().createPolicyTypes(null, new JpaToscaServiceTemplate());
+ }).hasMessage("dao is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaProvider().createPolicyTypes(pfDao, null);
+ }).hasMessage("serviceTemplate is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaProvider().updatePolicyTypes(null, null);
+ }).hasMessage("dao is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaProvider().updatePolicyTypes(null, new JpaToscaServiceTemplate());
+ }).hasMessage("dao is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaProvider().updatePolicyTypes(pfDao, null);
+ }).hasMessage("serviceTemplate is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaProvider().deletePolicyType(null, null);
+ }).hasMessage("dao is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaProvider().deletePolicyType(null, new PfConceptKey());
+ }).hasMessage("dao is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaProvider().deletePolicyType(pfDao, null);
+ }).hasMessage("policyTypeKey is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaProvider().getPolicies(null, null, null);
+ }).hasMessage("dao is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaProvider().createPolicies(null, null);
+ }).hasMessage("dao is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaProvider().createPolicies(null, new JpaToscaServiceTemplate());
+ }).hasMessage("dao is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaProvider().createPolicies(pfDao, null);
+ }).hasMessage("serviceTemplate is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaProvider().updatePolicies(null, null);
+ }).hasMessage("dao is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaProvider().updatePolicies(null, new JpaToscaServiceTemplate());
+ }).hasMessage("dao is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaProvider().updatePolicies(pfDao, null);
+ }).hasMessage("serviceTemplate is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaProvider().deletePolicy(null, null);
+ }).hasMessage("dao is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaProvider().deletePolicy(null, new PfConceptKey());
+ }).hasMessage("dao is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaProvider().deletePolicy(pfDao, null);
+ }).hasMessage("policyKey is marked @NonNull but is null");
+ }
+
+ private void createPolicyTypes() throws CoderException, PfModelException {
+ Object yamlObject = new Yaml().load(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml"));
+ String yamlAsJsonString = new StandardCoder().encode(yamlObject);
+
+ ToscaServiceTemplate toscaServiceTemplatePolicyType =
+ standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
+ assertNotNull(toscaServiceTemplatePolicyType);
+ new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
}
}