Merge "Add impl of more PDP persistence"
[policy/models.git] / models-provider / src / test / java / org / onap / policy / models / provider / impl / DatabasePolicyModelsProviderTest.java
index 498000a..8a83f44 100644 (file)
 
 package org.onap.policy.models.provider.impl;
 
+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.assertNull;
 import static org.junit.Assert.fail;
 
+import java.util.ArrayList;
 import java.util.Base64;
 
 import org.junit.Before;
 import org.junit.Test;
-import org.onap.policy.models.base.PfConceptKey;
-import org.onap.policy.models.pap.concepts.PdpGroups;
 import org.onap.policy.models.provider.PolicyModelsProvider;
 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
-import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
-import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
-import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -47,7 +45,7 @@ import org.slf4j.LoggerFactory;
  * @author Liam Fallon (liam.fallon@est.tech)
  */
 public class DatabasePolicyModelsProviderTest {
-    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class);
+    private static final Logger LOGGER = LoggerFactory.getLogger(DatabasePolicyModelsProviderTest.class);
 
     PolicyModelsProviderParameters parameters;
 
@@ -70,12 +68,12 @@ public class DatabasePolicyModelsProviderTest {
                 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
 
         parameters.setDatabaseUrl("jdbc://www.acmecorp.nonexist");
-        try {
+
+        assertThatThrownBy(() -> {
+            databaseProvider.close();
             databaseProvider.init();
-            fail("test should throw an exception");
-        } catch (Exception pfme) {
-            assertEquals("could not connect to database with URL \"jdbc://www.acmecorp.nonexist\"", pfme.getMessage());
-        }
+        }).hasMessage("could not connect to database with URL \"jdbc://www.acmecorp.nonexist\"");
+
         parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
 
         try {
@@ -86,13 +84,13 @@ public class DatabasePolicyModelsProviderTest {
         }
 
         parameters.setPersistenceUnit("WileECoyote");
-        try {
+
+        String errorMessage = "could not create Data Access Object (DAO) using url "
+                + "\"jdbc:h2:mem:testdb\" and persistence unit \"WileECoyote\"";
+        assertThatThrownBy(() -> {
             databaseProvider.init();
-            fail("test should throw an exception");
-        } catch (Exception pfme) {
-            assertEquals("could not create Data Access Object (DAO) using url "
-                    + "\"jdbc:h2:mem:testdb\" and persistence unit \"WileECoyote\"", pfme.getMessage());
-        }
+        }).hasMessage(errorMessage);
+
         parameters.setPersistenceUnit("ToscaConceptTest");
 
         try {
@@ -108,229 +106,220 @@ public class DatabasePolicyModelsProviderTest {
             fail("test shold not throw an exception here");
         }
 
-        try {
+        assertThatThrownBy(() -> {
             DatabasePolicyModelsProviderImpl databaseProviderImpl = (DatabasePolicyModelsProviderImpl) databaseProvider;
             databaseProvider.init();
             databaseProviderImpl.setConnection(new DummyConnection());
             databaseProvider.close();
-            fail("test should throw an exception");
-        } catch (Exception pfme) {
-            assertEquals("could not close connection to database with URL \"jdbc:h2:mem:testdb\"", pfme.getMessage());
-        }
+        }).hasMessage("could not close connection to database with URL \"jdbc:h2:mem:testdb\"");
     }
 
     @Test
     public void testProviderMethodsNull() throws Exception {
         PolicyModelsProvider databaseProvider =
                 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
-        databaseProvider.init();
 
-        try {
-            databaseProvider.getPolicyTypes(null);
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("policyTypeKey is marked @NonNull but is null", npe.getMessage());
-        }
-        try {
+        assertThatThrownBy(() -> {
+            databaseProvider.getPolicyTypes(null, null);
+        }).hasMessage("name is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
+            databaseProvider.getPolicyTypes("aaa", null);
+        }).hasMessage("version is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
+            databaseProvider.getPolicyTypes(null, "aaa");
+        }).hasMessage("name is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
             databaseProvider.createPolicyTypes(null);
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage());
-        }
-        try {
+        }).hasMessage("serviceTemplate is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
             databaseProvider.updatePolicyTypes(null);
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage());
-        }
-        try {
-            databaseProvider.deletePolicyTypes(null);
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("policyTypeKey is marked @NonNull but is null", npe.getMessage());
-        }
+        }).hasMessage("serviceTemplate is marked @NonNull but is null");
 
-        try {
-            databaseProvider.getPolicies(null);
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("policyKey is marked @NonNull but is null", npe.getMessage());
-        }
-        try {
+        assertThatThrownBy(() -> {
+            databaseProvider.deletePolicyType(null, null);
+        }).hasMessage("name is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
+            databaseProvider.deletePolicyType("aaa", null);
+        }).hasMessage("version is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
+            databaseProvider.deletePolicyType(null, "aaa");
+        }).hasMessage("name is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
+            databaseProvider.getPolicies(null, null);
+        }).hasMessage("name is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
+            databaseProvider.getPolicies(null, "aaa");
+        }).hasMessage("name is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
+            databaseProvider.getPolicies("aaa", null);
+        }).hasMessage("version is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
             databaseProvider.createPolicies(null);
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage());
-        }
-        try {
+        }).hasMessage("serviceTemplate is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
             databaseProvider.updatePolicies(null);
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage());
-        }
-        try {
-            databaseProvider.deletePolicies(null);
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("policyKey is marked @NonNull but is null", npe.getMessage());
-        }
+        }).hasMessage("serviceTemplate is marked @NonNull but is null");
 
-        try {
+        assertThatThrownBy(() -> {
+            databaseProvider.deletePolicy(null, null);
+        }).hasMessage("name is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
+            databaseProvider.deletePolicy(null, "aaa");
+        }).hasMessage("name is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
+            databaseProvider.deletePolicy("aaa", null);
+        }).hasMessage("version is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
             databaseProvider.getOperationalPolicy(null);
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("policyId is marked @NonNull but is null", npe.getMessage());
-        }
-        try {
+        }).hasMessage("policyId is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
             databaseProvider.createOperationalPolicy(null);
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("legacyOperationalPolicy is marked @NonNull but is null", npe.getMessage());
-        }
-        try {
+        }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
             databaseProvider.updateOperationalPolicy(null);
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("legacyOperationalPolicy is marked @NonNull but is null", npe.getMessage());
-        }
-        try {
+        }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
             databaseProvider.deleteOperationalPolicy(null);
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("policyId is marked @NonNull but is null", npe.getMessage());
-        }
+        }).hasMessage("policyId is marked @NonNull but is null");
 
-        try {
+        assertThatThrownBy(() -> {
             databaseProvider.getGuardPolicy(null);
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("policyId is marked @NonNull but is null", npe.getMessage());
-        }
-        try {
+        }).hasMessage("policyId is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
             databaseProvider.createGuardPolicy(null);
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("legacyGuardPolicy is marked @NonNull but is null", npe.getMessage());
-        }
-        try {
+        }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
             databaseProvider.updateGuardPolicy(null);
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("legacyGuardPolicy is marked @NonNull but is null", npe.getMessage());
-        }
-        try {
+        }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
             databaseProvider.deleteGuardPolicy(null);
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("policyId is marked @NonNull but is null", npe.getMessage());
-        }
+        }).hasMessage("policyId is marked @NonNull but is null");
 
-        try {
-            databaseProvider.getPdpGroups(null);
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("pdpGroupFilter is marked @NonNull but is null", npe.getMessage());
-        }
-        try {
+        assertThatThrownBy(() -> {
             databaseProvider.createPdpGroups(null);
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("pdpGroups is marked @NonNull but is null", npe.getMessage());
-        }
-        try {
+        }).hasMessage("pdpGroups is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
             databaseProvider.updatePdpGroups(null);
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("pdpGroups is marked @NonNull but is null", npe.getMessage());
-        }
-        try {
-            databaseProvider.deletePdpGroups(null);
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("pdpGroupFilter is marked @NonNull but is null", npe.getMessage());
-        }
+        }).hasMessage("pdpGroups is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
+            databaseProvider.deletePdpGroup(null, null);
+        }).hasMessage("name is marked @NonNull but is null");
 
         databaseProvider.close();
+
     }
 
     @Test
     public void testProviderMethodsNotInit() throws Exception {
         PolicyModelsProvider databaseProvider =
                 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
-        try {
-            databaseProvider.getPolicyTypes(new PfConceptKey());
-            fail("test should throw an exception");
-        } catch (Exception npe) {
-            assertEquals("policy models provider is not initilaized", npe.getMessage());
-        }
+
+        databaseProvider.close();
+
+        assertThatThrownBy(() -> {
+            databaseProvider.getPolicyTypes("name", "version");
+        }).hasMessage("policy models provider is not initilaized");
     }
 
     @Test
     public void testProviderMethods() {
         try (PolicyModelsProvider databaseProvider =
                 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) {
-            databaseProvider.init();
 
-            try {
-                databaseProvider.getPolicyTypes(new PfConceptKey());
-                fail("test should throw an exception");
-            } catch (Exception npe) {
-                assertEquals("policy type not found: NULL:0.0.0", npe.getMessage());
-            }
-            try {
+            assertThatThrownBy(() -> {
+                databaseProvider.getPolicyTypes("name", "version");
+            }).hasMessage("policy type not found: name:version");
+
+            assertThatThrownBy(() -> {
                 databaseProvider.createPolicyTypes(new ToscaServiceTemplate());
-            } catch (Exception npe) {
-                assertEquals("no policy types specified on service template", npe.getMessage());
-            }
-            try {
+            }).hasMessage("no policy types specified on service template");
+
+            assertThatThrownBy(() -> {
                 databaseProvider.updatePolicyTypes(new ToscaServiceTemplate());
-            } catch (Exception npe) {
-                assertEquals("no policy types specified on service template", npe.getMessage());
-            }
-            try {
-                databaseProvider.deletePolicyTypes(new PfConceptKey());
-                fail("test should throw an exception");
-            } catch (Exception npe) {
-                assertEquals("policy type not found: NULL:0.0.0", npe.getMessage());
-            }
-
-            try {
-                databaseProvider.getPolicies(new PfConceptKey());
-                fail("test should throw an exception");
-            } catch (Exception npe) {
-                assertEquals("policy not found: NULL:0.0.0", npe.getMessage());
-            }
-            try {
+            }).hasMessage("no policy types specified on service template");
+
+            assertThatThrownBy(() -> {
+                databaseProvider.deletePolicyType("name", "version");
+            }).hasMessage("policy type not found: name:version");
+
+            assertThatThrownBy(() -> {
+                databaseProvider.getPolicies("name", "version");
+            }).hasMessage("policy not found: name:version");
+
+            assertThatThrownBy(() -> {
                 databaseProvider.createPolicies(new ToscaServiceTemplate());
-            } catch (Exception npe) {
-                assertEquals("topology template not specified on service template", npe.getMessage());
-            }
-            try {
+            }).hasMessage("topology template not specified on service template");
+
+            assertThatThrownBy(() -> {
                 databaseProvider.updatePolicies(new ToscaServiceTemplate());
-            } catch (Exception npe) {
-                assertEquals("topology template not specified on service template", npe.getMessage());
-            }
-            try {
-                databaseProvider.deletePolicies(new PfConceptKey());
-                fail("test should throw an exception");
-            } catch (Exception npe) {
-                assertEquals("policy not found: NULL:0.0.0", npe.getMessage());
-            }
-
-            assertNull(databaseProvider.getOperationalPolicy("policy_id"));
-            assertNull(databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy()));
-            assertNull(databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy()));
-            assertNull(databaseProvider.deleteOperationalPolicy("policy_id"));
-
-            assertNull(databaseProvider.getGuardPolicy("policy_id"));
-            assertNull(databaseProvider.createGuardPolicy(new LegacyGuardPolicy()));
-            assertNull(databaseProvider.updateGuardPolicy(new LegacyGuardPolicy()));
-            assertNull(databaseProvider.deleteGuardPolicy("policy_id"));
-
-            assertNotNull(databaseProvider.getPdpGroups("filter"));
-            assertNotNull(databaseProvider.createPdpGroups(new PdpGroups()));
-            assertNotNull(databaseProvider.updatePdpGroups(new PdpGroups()));
-            assertNotNull(databaseProvider.deletePdpGroups("filter"));
+            }).hasMessage("topology template not specified on service template");
+
+            assertThatThrownBy(() -> {
+                databaseProvider.deletePolicy("name", "version");
+            }).hasMessage("policy not found: name:version");
+
+            assertThatThrownBy(() -> {
+                databaseProvider.getOperationalPolicy("policy_id");
+            }).hasMessage("no policy found for policy ID: policy_id");
+
+            assertThatThrownBy(() -> {
+                databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy());
+            }).hasMessage("name is marked @NonNull but is null");
+
+            assertThatThrownBy(() -> {
+                databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy());
+            }).hasMessage("no policy found for policy ID: null");
+
+            assertThatThrownBy(() -> {
+                databaseProvider.deleteOperationalPolicy("policy_id");
+            }).hasMessage("no policy found for policy ID: policy_id");
+
+            assertThatThrownBy(() -> {
+                databaseProvider.getGuardPolicy("policy_id");
+            }).hasMessage("no policy found for policy ID: policy_id");
+
+            assertThatThrownBy(() -> {
+                databaseProvider.createGuardPolicy(new LegacyGuardPolicyInput());
+            }).hasMessage("policy type for guard policy \"null\" unknown");
+
+            assertThatThrownBy(() -> {
+                databaseProvider.updateGuardPolicy(new LegacyGuardPolicyInput());
+            }).hasMessage("policy type for guard policy \"null\" unknown");
+
+            assertThatThrownBy(() -> {
+                databaseProvider.deleteGuardPolicy("policy_id");
+            }).hasMessage("no policy found for policy ID: policy_id");
+
+            assertEquals(0, databaseProvider.getPdpGroups("name", "version").size());
+
+            assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>()));
+            assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>()));
+
+            assertThatThrownBy(() -> {
+                databaseProvider.deletePdpGroup("name", "version");
+            }).hasMessage("delete of PDP group \"name:version\" failed, PDP group does not exist");
 
         } catch (Exception exc) {
             LOGGER.warn("test should not throw an exception", exc);