Fix failure in PIPEngineGetHistoryTest 33/45133/1
authorJim Hahn <jrh3@att.com>
Fri, 27 Apr 2018 15:23:00 +0000 (11:23 -0400)
committerJim Hahn <jrh3@att.com>
Fri, 27 Apr 2018 15:25:21 +0000 (11:25 -0400)
Turns out that the getAttributesTest() method depends on
testGetCountFromDb() being executed first, but junit does
not guarantee this.  Modified the code so that it is
independent of the order in which the tests are executed.

Change-Id: Ie081061e4e83895f4ac89242cf00d843b6e069d4
Issue-ID: POLICY-765
Signed-off-by: Jim Hahn <jrh3@att.com>
controlloop/common/guard/src/test/java/org/onap/policy/guard/PIPEngineGetHistoryTest.java
controlloop/common/guard/src/test/resources/META-INF/persistence.xml

index f1fd599..e728d41 100644 (file)
@@ -57,9 +57,11 @@ import java.util.Properties;
 import java.util.UUID;
 
 import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
 import javax.persistence.Persistence;
 import javax.persistence.Query;
-
+import org.junit.AfterClass;
+import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.onap.policy.drools.system.PolicyEngine;
@@ -67,6 +69,9 @@ import org.onap.policy.drools.system.PolicyEngine;
 public class PIPEngineGetHistoryTest {
     static PIPEngineGetHistory pegh;
     private static final String ISSUER = "issuerIntw:mid:end";
+    
+    private static EntityManagerFactory emf;
+    private static EntityManager em;
 
     /**
      * Set up test class.
@@ -79,6 +84,43 @@ public class PIPEngineGetHistoryTest {
         } catch (Exception e) {
             fail("PIPEngineGetHistory constructor failed");
         }
+
+        // Set PU
+        System.setProperty(Util.PU_KEY, Util.JUNITPU);
+
+        // Enter dummy props to avoid nullPointerException
+        PolicyEngine.manager.setEnvironmentProperty(Util.ONAP_KEY_URL, "a");
+        PolicyEngine.manager.setEnvironmentProperty(Util.ONAP_KEY_USER, "b");
+        PolicyEngine.manager.setEnvironmentProperty(Util.ONAP_KEY_PASS, "c");
+
+        // Connect to in-mem db
+        emf = Persistence.createEntityManagerFactory(Util.JUNITPU);
+        em = emf.createEntityManager();
+
+        // Create necessary table
+        String sql = "CREATE TABLE `operationshistory10` (" + "`CLNAME` varchar(255)," + "`requestID` varchar(100),"
+                + "`actor` varchar(50) ," + "`operation` varchar(50)," + "`target` varchar(50),"
+                + "`starttime` timestamp," + "`outcome` varchar(50)," + "`message` varchar(255),"
+                + "`subrequestId` varchar(100)," + "`endtime` timestamp" + ")";
+        Query nq = em.createNativeQuery(sql);
+        em.getTransaction().begin();
+        nq.executeUpdate();
+        em.getTransaction().commit();
+    }
+    
+    @AfterClass
+    public static void tearDown() {
+        emf.close();
+    }
+
+    @Before
+    public void setUp() {
+        // clear the table
+        String sql = "DELETE FROM `operationshistory10`";
+        Query nq = em.createNativeQuery(sql);
+        em.getTransaction().begin();
+        nq.executeUpdate();
+        em.getTransaction().commit();
     }
 
     @Test
@@ -124,31 +166,6 @@ public class PIPEngineGetHistoryTest {
 
     @Test
     public void testGetCountFromDb() {
-        // Set PU
-        System.setProperty(Util.PU_KEY, Util.JUNITPU);
-
-        // Enter dummy props to avoid nullPointerException
-        PolicyEngine.manager.setEnvironmentProperty(Util.ONAP_KEY_URL, "a");
-        PolicyEngine.manager.setEnvironmentProperty(Util.ONAP_KEY_USER, "b");
-        PolicyEngine.manager.setEnvironmentProperty(Util.ONAP_KEY_PASS, "c");
-
-        // Connect to in-mem db
-        EntityManager em = null;
-        try {
-            em = Persistence.createEntityManagerFactory(Util.JUNITPU).createEntityManager();
-        } catch (Exception e) {
-            fail(e.getLocalizedMessage());
-        }
-
-        String sql = "CREATE TABLE `operationshistory10` (" + "`CLNAME` varchar(255)," + "`requestID` varchar(100),"
-                + "`actor` varchar(50) ," + "`operation` varchar(50)," + "`target` varchar(50),"
-                + "`starttime` timestamp," + "`outcome` varchar(50)," + "`message` varchar(255),"
-                + "`subrequestId` varchar(100)," + "`endtime` timestamp" + ")";
-        // Create necessary table
-        Query nq = em.createNativeQuery(sql);
-        em.getTransaction().begin();
-        nq.executeUpdate();
-        em.getTransaction().commit();
 
         // Use reflection to run getCountFromDB
         Method method = null;
@@ -172,7 +189,6 @@ public class PIPEngineGetHistoryTest {
         em.getTransaction().begin();
         nq2.executeUpdate();
         em.getTransaction().commit();
-        em.close();
 
         try {
             count = (int) method.invoke(null, "actor", "op", "target", "1 MINUTE");
index a8596b7..e4f597f 100644 (file)
@@ -26,7 +26,6 @@
        <persistence-unit name="TestOperationsHistoryPU"
                transaction-type="RESOURCE_LOCAL">
                <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
-               <class>org.onap.policy.controlloop.eventmanager.OperationsHistoryDbEntry</class>
                <properties>
                        <property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
                        <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />