Remove Factory from IntegrityMonitor 53/73553/3
authorJim Hahn <jrh3@att.com>
Tue, 27 Nov 2018 20:15:57 +0000 (15:15 -0500)
committerJim Hahn <jrh3@att.com>
Tue, 27 Nov 2018 20:17:07 +0000 (15:17 -0500)
Also removed unneeded method override in junit test.

Change-Id: I7186e9f75443f7b8ba8376aa0e00a5cf4c573696
Issue-ID: POLICY-1287
Signed-off-by: Jim Hahn <jrh3@att.com>
integrity-monitor/src/main/java/org/onap/policy/common/im/IntegrityMonitor.java
integrity-monitor/src/test/java/org/onap/policy/common/im/AllSeemsWellTest.java
integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTest.java
integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTestBase.java

index 8e9dd3d..9801b53 100644 (file)
@@ -75,9 +75,6 @@ public class IntegrityMonitor {
      */
     private static final String QUERY_STRING = "Select f from ForwardProgressEntity f where f.resourceName=:rn";
 
-    // may be changed by junit tests
-    private static Factory factory = new Factory();
-
     private static String resourceName = null;
     boolean alarmExists = false;
 
@@ -224,13 +221,13 @@ public class IntegrityMonitor {
         //
         // Create the entity manager factory
         //
-        emf = Persistence.createEntityManagerFactory(factory.getPersistenceUnit(), properties);
+        emf = Persistence.createEntityManagerFactory(getPersistenceUnit(), properties);
         //
         // Did it get created?
         //
         if (emf == null) {
-            logger.error("Error creating IM entity manager factory with persistence unit: {}",
-                    factory.getPersistenceUnit());
+            logger.error("Error creating IM entity manager factory with persistence unit: {}", 
+                            getPersistenceUnit());
             throw new IntegrityMonitorException("Unable to create IM Entity Manager Factory");
         }
 
@@ -1714,13 +1711,13 @@ public class IntegrityMonitor {
             logger.debug("FPManager thread running");
 
             try {
-                factory.runStarted();
+                runStarted();
 
                 while (!stopRequested) {
                     MonitorTime.getInstance().sleep(CYCLE_INTERVAL_MILLIS);
-
-                    this.runOnce();
-                    factory.monitorCompleted();
+                    
+                    runOnce();
+                    monitorCompleted();
                 }
 
             } catch (InterruptedException e) {
@@ -1865,38 +1862,35 @@ public class IntegrityMonitor {
     public Map<String, String> getAllNotWellMap() {
         return allNotWellMap;
     }
+    
+    // these methods may be overridden by junit tests
 
     /**
-     * Wrapper used to access various objects. Overridden by junit tests.
+     * Indicates that the {@link FpManager#run()} method has started. This method
+     * simply returns.
+     * 
+     * @throws InterruptedException can be interrupted
      */
-    public static class Factory {
-
-        /**
-         * Indicates that the {@link FpManager#run()} method has started. This method simply returns.
-         *
-         * @throws InterruptedException can be interrupted
-         */
-        public void runStarted() throws InterruptedException {
-            // does nothing
-        }
+    protected void runStarted() throws InterruptedException {
+        // does nothing
+    }
 
-        /**
-         * Indicates that a monitor activity has completed. This method simply returns.
-         *
-         * @throws InterruptedException can be interrupted
-         */
-        public void monitorCompleted() throws InterruptedException {
-            // does nothing
-        }
+    /**
+     * Indicates that a monitor activity has completed. This method simply returns.
+     * 
+     * @throws InterruptedException can be interrupted
+     */
+    protected void monitorCompleted() throws InterruptedException {
+        // does nothing
+    }
 
-        /**
-         * Get persistence unit.
-         *
-         * @return the persistence unit to be used
-         */
-        public String getPersistenceUnit() {
-            return PERSISTENCE_UNIT;
-        }
+    /**
+     * Get persistence unit.
+     * 
+     * @return the persistence unit to be used
+     */
+    protected String getPersistenceUnit() {
+        return PERSISTENCE_UNIT;
     }
 
     /*
index fe40893..6d67dd1 100644 (file)
@@ -31,7 +31,6 @@ import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
-import org.onap.policy.common.im.IntegrityMonitor.Factory;
 import org.powermock.reflect.Whitebox;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -62,19 +61,21 @@ public class AllSeemsWellTest extends IntegrityMonitorTestBase {
 
     /**
      * Set up for test cases.
+     * @throws Exception if an error occurs
      */
     @Before
-    public void setUp() {
+    public void setUp() throws Exception {
         super.setUpTest();
 
         myProp = makeProperties();
 
         monitorSem = new Semaphore(0);
         junitSem = new Semaphore(0);
-        
-        Factory factory = new TestFactory() {
+
+        IntegrityMonitor im = new IntegrityMonitor(resourceName, myProp) {
+            
             @Override
-            public void runStarted() throws InterruptedException {
+            protected void runStarted() throws InterruptedException {
                 monitorSem.acquire();
                 
                 junitSem.release();
@@ -82,13 +83,13 @@ public class AllSeemsWellTest extends IntegrityMonitorTestBase {
             }
 
             @Override
-            public void monitorCompleted() throws InterruptedException {
+            protected void monitorCompleted() throws InterruptedException {
                 junitSem.release();
                 monitorSem.acquire();
-            }
+            }            
         };
 
-        Whitebox.setInternalState(IntegrityMonitor.class, FACTORY_FIELD, factory);
+        Whitebox.setInternalState(IntegrityMonitor.class, IM_INSTANCE_FIELD, im);
     }
 
     @After
index 3c4fba2..862552b 100644 (file)
@@ -21,7 +21,6 @@
 package org.onap.policy.common.im;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.fail;
 
 import java.util.Date;
@@ -37,7 +36,6 @@ import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
-import org.onap.policy.common.im.IntegrityMonitor.Factory;
 import org.onap.policy.common.im.jpa.ForwardProgressEntity;
 import org.onap.policy.common.im.jpa.ResourceRegistrationEntity;
 import org.onap.policy.common.im.jpa.StateManagementEntity;
@@ -110,11 +108,6 @@ public class IntegrityMonitorTest extends IntegrityMonitorTestBase {
 
         super.tearDownTest();
     }
-    
-    @Test
-    public void testFactory() {
-        assertNotNull(getSavedFactory());
-    }
 
     /*
      * The following test verifies the following test cases: New Install New Install - Bad
@@ -917,10 +910,11 @@ public class IntegrityMonitorTest extends IntegrityMonitorTestBase {
 
         monitorSem = new Semaphore(0);
         junitSem = new Semaphore(0);
-        
-        Factory factory = new TestFactory() {
+
+        IntegrityMonitor im = new IntegrityMonitor(resourceName, myProp) {
+            
             @Override
-            public void runStarted() throws InterruptedException {
+            protected void runStarted() throws InterruptedException {
                 monitorSem.acquire();
                 
                 junitSem.release();
@@ -928,15 +922,13 @@ public class IntegrityMonitorTest extends IntegrityMonitorTestBase {
             }
 
             @Override
-            public void monitorCompleted() throws InterruptedException {
+            protected void monitorCompleted() throws InterruptedException {
                 junitSem.release();
                 monitorSem.acquire();
             }            
         };
-        
-        Whitebox.setInternalState(IntegrityMonitor.class, FACTORY_FIELD, factory);
 
-        IntegrityMonitor im = IntegrityMonitor.getInstance(resourceName, myProp);
+        Whitebox.setInternalState(IntegrityMonitor.class, IM_INSTANCE_FIELD, im);
 
         // wait for the monitor thread to start
         waitCycles(1);
index f3ad1d3..4307843 100644 (file)
@@ -31,7 +31,6 @@ import java.util.concurrent.TimeUnit;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.Persistence;
-import org.onap.policy.common.im.IntegrityMonitor.Factory;
 import org.onap.policy.common.utils.jpa.EntityTransCloser;
 import org.onap.policy.common.utils.test.log.logback.ExtractAppender;
 import org.onap.policy.common.utils.time.CurrentTime;
@@ -53,9 +52,9 @@ public class IntegrityMonitorTestBase {
     private static Logger logger = LoggerFactory.getLogger(IntegrityMonitorTestBase.class);
     
     /**
-     * Name of the factory field within the IntegrityMonitor class.
+     * Name of the instance field within the IntegrityMonitor class.
      */
-    public static final String FACTORY_FIELD = "factory";
+    public static final String IM_INSTANCE_FIELD = "instance";
     
     /**
      * Name of the instance field within the MonitorTime class.
@@ -120,11 +119,6 @@ public class IntegrityMonitorTestBase {
      */
     private static Object savedJmxPort;
 
-    /**
-     * Saved factory, to be restored once all tests complete.
-     */
-    private static Factory savedFactory;
-
     /**
      * Saved time accessor, to be restored once all tests complete.
      */
@@ -151,14 +145,11 @@ public class IntegrityMonitorTestBase {
         IntegrityMonitorTestBase.dbUrl = dbUrl;
 
         // save data that we have to restore at the end of the test
-        savedFactory = Whitebox.getInternalState(IntegrityMonitor.class, FACTORY_FIELD);
         savedJmxPort = systemProps.get(JMX_PORT_PROP);
         savedTime = MonitorTime.getInstance();
 
         systemProps.put(JMX_PORT_PROP, "9797");
 
-        Whitebox.setInternalState(IntegrityMonitor.class, FACTORY_FIELD, new TestFactory());
-
         IntegrityMonitor.setUnitTesting(true);
         
         testTime = new TestTime();
@@ -196,7 +187,6 @@ public class IntegrityMonitorTestBase {
         }
 
         Whitebox.setInternalState(MonitorTime.class, TIME_INSTANCE_FIELD, savedTime);
-        Whitebox.setInternalState(IntegrityMonitor.class, FACTORY_FIELD, savedFactory);
 
         IntegrityMonitor.setUnitTesting(false);
 
@@ -232,15 +222,6 @@ public class IntegrityMonitorTestBase {
         stopMonitor();
     }
 
-    /**
-     * Get saved factory.
-     * 
-     * @return the original integrity monitor factory
-     */
-    static Factory getSavedFactory() {
-        return savedFactory;
-    }
-
     /**
      * Stops the IntegrityMonitor instance.
      */
@@ -318,16 +299,6 @@ public class IntegrityMonitorTestBase {
             System.out.println("action found expected exception: " + e);
         }
     }
-    
-    /**
-     * Factory with overrides for junit testing.
-     */
-    public static class TestFactory extends Factory {
-        @Override
-        public String getPersistenceUnit() {
-            return PERSISTENCE_UNIT;
-        }
-    }
 
     @FunctionalInterface
     protected static interface VoidFunction<T> {