Fix write failure on PDP statistics
[policy/models.git] / models-pdp / src / test / java / org / onap / policy / models / pdp / persistence / provider / PdpStatisticsProviderTest.java
index effebe4..724bb3c 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2020 Nordix Foundation.
+ *  Copyright (C) 2020-2021 Nordix Foundation.
+ *  Modifications Copyright (C) 2020 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.
@@ -24,14 +25,15 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.Assert.assertEquals;
 
+import java.time.Instant;
 import java.util.ArrayList;
-import java.util.Date;
 import java.util.List;
 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.Validated;
 import org.onap.policy.models.dao.DaoParameters;
 import org.onap.policy.models.dao.PfDao;
 import org.onap.policy.models.dao.PfDaoFactory;
@@ -40,14 +42,14 @@ import org.onap.policy.models.pdp.concepts.PdpEngineWorkerStatistics;
 import org.onap.policy.models.pdp.concepts.PdpStatistics;
 
 public class PdpStatisticsProviderTest {
-    private static final String DAO_IS_NULL = "dao is marked @NonNull but is null";
-    private static final String LIST_IS_NULL = "pdpStatisticsList is marked @NonNull but is null";
+    private static final String DAO_IS_NULL = "dao is marked .*ull but is null";
+    private static final String LIST_IS_NULL = "pdpStatisticsList is marked .*ull but is null";
     private static final String GROUP0 = "group0";
     private static final String NAME = "name";
     private static final String GROUP = "group";
     private static final String SUBGROUP = "subgroup";
-    private static final Date TIMESTAMP1 = new Date(1078884319);
-    private static final Date TIMESTAMP2 = new Date(1078884350);
+    private static final Instant TIMESTAMP1 = Instant.ofEpochSecond(1078884319);
+    private static final Instant TIMESTAMP2 = Instant.ofEpochSecond(1078884350);
     private static final String ORDER = "DESC";
 
     private PfDao pfDao;
@@ -72,9 +74,13 @@ public class PdpStatisticsProviderTest {
         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");
+        if (System.getProperty("USE-MARIADB") != null) {
+            jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.mariadb.jdbc.Driver");
+            jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:mariadb://localhost:3306/policy");
+        } else {
+            jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
+            jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
+        }
 
         daoParameters.setJdbcProperties(jdbcProperties);
 
@@ -133,29 +139,31 @@ public class PdpStatisticsProviderTest {
 
         assertThatThrownBy(() -> {
             new PdpStatisticsProvider().createPdpStatistics(pfDao, null);
-        }).hasMessage(LIST_IS_NULL);
+        }).hasMessageMatching(LIST_IS_NULL);
 
         assertThatThrownBy(() -> {
             new PdpStatisticsProvider().updatePdpStatistics(pfDao, null);
-        }).hasMessage(LIST_IS_NULL);
+        }).hasMessageMatching(LIST_IS_NULL);
 
         assertThatThrownBy(() -> {
             new PdpStatisticsProvider().createPdpStatistics(pfDao, pdpStatisticsNullList);
-        }).hasMessageContaining("pdp statictics \"NULL\" is not valid");
+        }).hasMessageContaining("pdp statistics").hasMessageContaining("key")
+                        .hasMessageContaining(Validated.IS_A_NULL_KEY);
 
         assertThatThrownBy(() -> {
             new PdpStatisticsProvider().updatePdpStatistics(pfDao, pdpStatisticsNullList);
-        }).hasMessageContaining("pdp statistics \"NULL:0.0.0:0\" is not valid");
+        }).hasMessageContaining("pdp statistics").hasMessageContaining("key")
+                        .hasMessageContaining(Validated.IS_A_NULL_KEY);
     }
 
     @Test
     public void testGetPdpStatistics() throws Exception {
         assertThatThrownBy(() -> {
             new PdpStatisticsProvider().createPdpStatistics(null, null);
-        }).hasMessage(DAO_IS_NULL);
+        }).hasMessageMatching(DAO_IS_NULL);
         assertThatThrownBy(() -> {
             new PdpStatisticsProvider().getPdpStatistics(null, null, null);
-        }).hasMessage(DAO_IS_NULL);
+        }).hasMessageMatching(DAO_IS_NULL);
 
         List<PdpStatistics> getPdpStatisticsList;
         getPdpStatisticsList = new PdpStatisticsProvider().getPdpStatistics(pfDao, NAME, TIMESTAMP1);
@@ -174,12 +182,12 @@ public class PdpStatisticsProviderTest {
         assertThatThrownBy(() -> {
             new PdpStatisticsProvider().getFilteredPdpStatistics(null, NAME, GROUP, SUBGROUP, TIMESTAMP1, TIMESTAMP2,
                     ORDER, 1);
-        }).hasMessage(DAO_IS_NULL);
+        }).hasMessageMatching(DAO_IS_NULL);
 
         assertThatThrownBy(() -> {
             new PdpStatisticsProvider().getFilteredPdpStatistics(pfDao, NAME, null, null, TIMESTAMP1, TIMESTAMP2, ORDER,
                     1);
-        }).hasMessage("pdpGroupName is marked @NonNull but is null");
+        }).hasMessageMatching("pdpGroupName is marked .*ull but is null");
 
 
         List<PdpStatistics> createdPdpStatisticsList;
@@ -203,7 +211,7 @@ public class PdpStatisticsProviderTest {
     public void testUpdatePdpStatistics() throws Exception {
         assertThatThrownBy(() -> {
             new PdpStatisticsProvider().updatePdpStatistics(null, null);
-        }).hasMessage(DAO_IS_NULL);
+        }).hasMessageMatching(DAO_IS_NULL);
 
         pdpStatisticsTestList.get(0).setPdpGroupName(GROUP0);
         testListStr = pdpStatisticsTestList.toString();
@@ -218,11 +226,11 @@ public class PdpStatisticsProviderTest {
     public void testDeletePdpStatistics() throws Exception {
         assertThatThrownBy(() -> {
             new PdpStatisticsProvider().deletePdpStatistics(null, null, null);
-        }).hasMessage(DAO_IS_NULL);
+        }).hasMessageMatching(DAO_IS_NULL);
 
         assertThatThrownBy(() -> {
             new PdpStatisticsProvider().deletePdpStatistics(pfDao, null, null);
-        }).hasMessage("name is marked @NonNull but is null");
+        }).hasMessageMatching("name is marked .*ull but is null");
 
         List<PdpStatistics> deletedPdpStatisticsList =
                 new PdpStatisticsProvider().deletePdpStatistics(pfDao, NAME, null);