diff options
author | Jim Hahn <jrh3@att.com> | 2018-11-27 15:15:57 -0500 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2018-11-27 15:17:07 -0500 |
commit | 1ff3457738007037364a103c28836d582098f764 (patch) | |
tree | b8a9b146aee6178079b7bb60fe6d341fda12e5a7 /integrity-monitor/src | |
parent | dc569fec077a6d6acce415925685699d5907cd2d (diff) |
Remove Factory from IntegrityMonitor
Also removed unneeded method override in junit test.
Change-Id: I7186e9f75443f7b8ba8376aa0e00a5cf4c573696
Issue-ID: POLICY-1287
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'integrity-monitor/src')
4 files changed, 48 insertions, 90 deletions
diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/IntegrityMonitor.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/IntegrityMonitor.java index 8e9dd3dd..9801b530 100644 --- a/integrity-monitor/src/main/java/org/onap/policy/common/im/IntegrityMonitor.java +++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/IntegrityMonitor.java @@ -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; } /* diff --git a/integrity-monitor/src/test/java/org/onap/policy/common/im/AllSeemsWellTest.java b/integrity-monitor/src/test/java/org/onap/policy/common/im/AllSeemsWellTest.java index fe408931..6d67dd1b 100644 --- a/integrity-monitor/src/test/java/org/onap/policy/common/im/AllSeemsWellTest.java +++ b/integrity-monitor/src/test/java/org/onap/policy/common/im/AllSeemsWellTest.java @@ -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 diff --git a/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTest.java b/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTest.java index 3c4fba23..862552b8 100644 --- a/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTest.java +++ b/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTest.java @@ -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); diff --git a/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTestBase.java b/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTestBase.java index f3ad1d32..43078432 100644 --- a/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTestBase.java +++ b/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTestBase.java @@ -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. @@ -121,11 +120,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. */ private static CurrentTime savedTime; @@ -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); @@ -233,15 +223,6 @@ public class IntegrityMonitorTestBase { } /** - * Get saved factory. - * - * @return the original integrity monitor factory - */ - static Factory getSavedFactory() { - return savedFactory; - } - - /** * Stops the IntegrityMonitor instance. */ private static void stopMonitor() { @@ -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> { |