Fix config files to remove outdated configuration for hibernate
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / authorative / provider / AuthorativeToscaProviderPolicyTypeTest.java
index 22b9ab0..42ac127 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019-2020 Nordix Foundation.
- *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ *  Copyright (C) 2019-2021, 2023-2024 Nordix Foundation.
+ *  Modifications Copyright (C) 2019, 2021 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.
@@ -27,13 +27,10 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import com.google.gson.GsonBuilder;
-
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Properties;
-
 import org.apache.commons.lang3.ObjectUtils;
-import org.eclipse.persistence.config.PersistenceUnitProperties;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.BeforeClass;
@@ -45,16 +42,14 @@ 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.authorative.concepts.ToscaEntityFilter;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
 import org.yaml.snakeyaml.Yaml;
 
 /**
  * Test of the {@link AuthorativeToscaProvider} class.
- *
- * @author Liam Fallon (liam.fallon@est.tech)
  */
 public class AuthorativeToscaProviderPolicyTypeTest {
     private static final String VERSION = "version";
@@ -93,13 +88,16 @@ public class AuthorativeToscaProviderPolicyTypeTest {
         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");
-
+        jdbcProperties.setProperty("jakarta.persistence.jdbc.user", "policy");
+        jdbcProperties.setProperty("jakarta.persistence.jdbc.password", "P01icY");
+        if (System.getProperty("USE-MARIADB") != null) {
+            jdbcProperties.setProperty("jakarta.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
+            jdbcProperties.setProperty("jakarta.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/policy");
+        } else {
+            jdbcProperties.setProperty("jakarta.persistence.jdbc.driver", "org.h2.Driver");
+            jdbcProperties.setProperty("jakarta.persistence.jdbc.url",
+                            "jdbc:h2:mem:AuthorativeToscaProviderPolicyTypeTest");
+        }
         daoParameters.setJdbcProperties(jdbcProperties);
 
         pfDao = new PfDaoFactory().createPfDao(daoParameters);
@@ -133,7 +131,7 @@ public class AuthorativeToscaProviderPolicyTypeTest {
 
         assertNotNull(toscaServiceTemplate);
         ToscaServiceTemplate createdServiceTemplate =
-            new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
+                new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
 
         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
 
@@ -142,15 +140,15 @@ public class AuthorativeToscaProviderPolicyTypeTest {
         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
 
-        ToscaServiceTemplate gotServiceTemplate =
-            new AuthorativeToscaProvider().getPolicyTypes(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion());
+        ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
+                policyTypeKey.getName(), policyTypeKey.getVersion());
 
         ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
 
         List<ToscaPolicyType> gotPolicyTypeList =
-            new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
+                new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
         assertEquals(2, gotPolicyTypeList.size());
         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
 
@@ -166,9 +164,8 @@ public class AuthorativeToscaProviderPolicyTypeTest {
         assertEquals(2, gotPolicyTypeList.size());
         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
 
-        assertThatThrownBy(
-            () -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION, VERSION_001))
-                .hasMessageContaining("Policy Framework DAO has not been initialized");
+        assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION,
+                VERSION_001)).hasMessageContaining("Policy Framework DAO has not been initialized");
 
         assertTrue(new AuthorativeToscaProvider().getPolicyTypeList(pfDao, "i.dont.Exist", VERSION_001).isEmpty());
     }
@@ -180,7 +177,8 @@ public class AuthorativeToscaProviderPolicyTypeTest {
         }).hasMessageMatching(DAO_IS_NULL);
 
         assertThatThrownBy(() -> {
-            new AuthorativeToscaProvider().getFilteredPolicyTypes(null, ToscaPolicyTypeFilter.builder().build());
+            new AuthorativeToscaProvider().getFilteredPolicyTypes(null,
+                    ToscaEntityFilter.<ToscaPolicyType>builder().build());
         }).hasMessageMatching(DAO_IS_NULL);
 
         assertThatThrownBy(() -> {
@@ -192,7 +190,8 @@ public class AuthorativeToscaProviderPolicyTypeTest {
         }).hasMessageMatching(DAO_IS_NULL);
 
         assertThatThrownBy(() -> {
-            new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, ToscaPolicyTypeFilter.builder().build());
+            new AuthorativeToscaProvider().getFilteredPolicyTypeList(null,
+                    ToscaEntityFilter.<ToscaPolicyType>builder().build());
         }).hasMessageMatching(DAO_IS_NULL);
 
         assertThatThrownBy(() -> {
@@ -200,17 +199,17 @@ public class AuthorativeToscaProviderPolicyTypeTest {
         }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
 
         assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypeList(new DefaultPfDao(),
-            ToscaPolicyTypeFilter.builder().name("i.dont.Exist").build()))
-                .hasMessageContaining("Policy Framework DAO has not been initialized");
+                ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()))
+                        .hasMessageContaining("Policy Framework DAO has not been initialized");
 
-        assertTrue(new AuthorativeToscaProvider()
-            .getFilteredPolicyTypeList(pfDao, ToscaPolicyTypeFilter.builder().name("i.dont.Exist").build()).isEmpty());
+        assertTrue(new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
+                ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()).isEmpty());
 
         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
 
         assertNotNull(toscaServiceTemplate);
         ToscaServiceTemplate createdServiceTemplate =
-            new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
+                new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
 
         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
 
@@ -219,53 +218,55 @@ public class AuthorativeToscaProviderPolicyTypeTest {
         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
 
-        ToscaServiceTemplate gotServiceTemplate =
-            new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaPolicyTypeFilter.builder().build());
+        ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
+                ToscaEntityFilter.<ToscaPolicyType>builder().build());
 
         ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
-        assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
-        assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
+        checkEqualsNameDescription(beforePolicyType, gotPolicyType);
 
         gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
-            ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
+                ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
 
         gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
-        assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
-        assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
+        checkEqualsNameDescription(beforePolicyType, gotPolicyType);
 
-        gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
-            ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version(VERSION_001).build());
+        gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaEntityFilter
+                .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
 
         gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
-        assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
-        assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
+        checkEqualsNameDescription(beforePolicyType, gotPolicyType);
 
         List<ToscaPolicyType> gotPolicyTypeList =
-            new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
+                new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
         assertEquals(2, gotPolicyTypeList.size());
         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
 
-        gotPolicyTypeList =
-            new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, ToscaPolicyTypeFilter.builder().build());
+        gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
+                ToscaEntityFilter.<ToscaPolicyType>builder().build());
         assertEquals(2, gotPolicyTypeList.size());
         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
 
         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
-            ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
+                ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
         assertEquals(1, gotPolicyTypeList.size());
         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
 
-        gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
-            ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version(VERSION_001).build());
+        gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, ToscaEntityFilter
+                .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
         assertEquals(1, gotPolicyTypeList.size());
         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
 
         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
-            ToscaPolicyTypeFilter.builder().version("1.0.0").build());
+                ToscaEntityFilter.<ToscaPolicyType>builder().version("1.0.0").build());
         assertEquals(1, gotPolicyTypeList.size());
         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
     }
 
+    private void checkEqualsNameDescription(ToscaPolicyType beforePolicyType, ToscaPolicyType gotPolicyType) {
+        assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
+        assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
+    }
+
     @Test
     public void testPolicyTypesCreate() throws Exception {
         assertThatThrownBy(() -> {
@@ -294,7 +295,7 @@ public class AuthorativeToscaProviderPolicyTypeTest {
 
         assertNotNull(toscaServiceTemplate);
         ToscaServiceTemplate createdServiceTemplate =
-            new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
+                new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
 
         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
 
@@ -326,7 +327,7 @@ public class AuthorativeToscaProviderPolicyTypeTest {
 
         assertNotNull(toscaServiceTemplate);
         ToscaServiceTemplate createdServiceTemplate =
-            new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
+                new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
 
         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
 
@@ -336,7 +337,7 @@ public class AuthorativeToscaProviderPolicyTypeTest {
         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
 
         ToscaServiceTemplate updatedServiceTemplate =
-            new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
+                new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
 
         ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
         assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
@@ -377,7 +378,7 @@ public class AuthorativeToscaProviderPolicyTypeTest {
 
         assertNotNull(toscaServiceTemplate);
         ToscaServiceTemplate createdServiceTemplate =
-            new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
+                new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
 
         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
 
@@ -386,8 +387,8 @@ public class AuthorativeToscaProviderPolicyTypeTest {
         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
 
-        ToscaServiceTemplate deletedServiceTemplate =
-            new AuthorativeToscaProvider().deletePolicyType(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion());
+        ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
+                policyTypeKey.getName(), policyTypeKey.getVersion());
 
         ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
         assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
@@ -420,4 +421,10 @@ public class AuthorativeToscaProviderPolicyTypeTest {
             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
         }).hasMessage("An incoming list of concepts must have at least one entry");
     }
+
+    @Test
+    public void testNullParameters() throws Exception {
+        assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(null, null, null))
+                .hasMessageMatching("^dao is marked .*on.*ull but is null$");
+    }
 }