aboutsummaryrefslogtreecommitdiffstats
path: root/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTestBase.java
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2018-06-21 15:43:32 -0400
committerJim Hahn <jrh3@att.com>2018-06-21 16:13:19 -0400
commit37b7d29aa5b61127a794d356eaa3db87c9348a69 (patch)
tree115022fbb5c02e99aeaf2a96e0eb8060ab2531d9 /integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTestBase.java
parent88116de291b4200b0d4dfbdfce492d009293dad8 (diff)
IntegrityMonitor: remove sleep from junit tests
Modified the code to use a CurrentTime object for its "time" operations (e.g., sleep(), currentTimeInMillis()). Modified junit tests to replace the CurrentTime object with TestTime objects so they don't actually do any sleeping. Update license date. Remove unneeded dependency from pom. Don't start FpManager thread within its own constructor. toMillis() should handle -1 as an input. Fix comment in test base superclass. Change time units in test base from DAYS to MILLISECONDS. Change-Id: Id6a4edb1747ca1a683e5d37522872b781294532d Issue-ID: POLICY-908 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTestBase.java')
-rw-r--r--integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTestBase.java88
1 files changed, 64 insertions, 24 deletions
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 e5562306..adde768e 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
@@ -30,8 +30,12 @@ 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;
+import org.onap.policy.common.utils.time.TestTime;
+import org.powermock.reflect.Whitebox;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -46,6 +50,16 @@ import org.slf4j.LoggerFactory;
*/
public class IntegrityMonitorTestBase {
private static Logger logger = LoggerFactory.getLogger(IntegrityMonitorTestBase.class);
+
+ /**
+ * Name of the factory field within the IntegrityMonitor class.
+ */
+ public static final String FACTORY_FIELD = "factory";
+
+ /**
+ * Name of the instance field within the MonitorTime class.
+ */
+ public static final String TIME_INSTANCE_FIELD = "instance";
/**
* Directory containing the slf4j log files.
@@ -60,9 +74,9 @@ public class IntegrityMonitorTestBase {
protected static final long WAIT_MS = 5000L;
/**
- * Milliseconds that monitor should sleep between cycles.
+ * Milliseconds between state refreshes.
*/
- protected static final long CYCLE_INTERVAL_MS = 2L;
+ protected static final long REFRESH_INTERVAL_MS = 3L * IntegrityMonitor.CYCLE_INTERVAL_MILLIS;
public static final String DEFAULT_DB_URL_PREFIX = "jdbc:h2:mem:";
@@ -94,6 +108,11 @@ public class IntegrityMonitorTestBase {
* Entity manager factory pointing to the in-memory DB associated with emf.
*/
protected static EntityManager em;
+
+ /**
+ * Test time used by tests in lieu of CurrentTime.
+ */
+ private static TestTime testTime;
/**
* Saved JMX port from system properties, to be restored once all tests complete.
@@ -101,19 +120,15 @@ public class IntegrityMonitorTestBase {
private static Object savedJmxPort;
/**
- * Saved IM persistence unit, to be restored once all tests complete.
+ * Saved factory, to be restored once all tests complete.
*/
- private static String savedPU;
+ private static Factory savedFactory;
/**
- * Saved monitor cycle interval, to be restored once all tests complete.
+ * Saved time accessor, to be restored once all tests complete.
*/
- private static long savedCycleIntervalMillis;
+ private static CurrentTime savedTime;
- /**
- * Saved property time units, to be restored once all tests complete.
- */
- private static TimeUnit savedPropertyUnits;
/**
* Saves current configuration information and then sets new values.
@@ -136,19 +151,19 @@ 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);
- savedPU = IntegrityMonitor.getPersistenceUnit();
- savedCycleIntervalMillis = IntegrityMonitor.getCycleIntervalMillis();
- savedPropertyUnits = IntegrityMonitor.getPropertyUnits();
+ savedTime = MonitorTime.getInstance();
systemProps.put(JMX_PORT_PROP, "9797");
- IntegrityMonitor.setPersistenceUnit(PERSISTENCE_UNIT);
- IntegrityMonitor.setCycleIntervalMillis(CYCLE_INTERVAL_MS);
- IntegrityMonitor.setPropertyUnits(TimeUnit.MILLISECONDS);
+ Whitebox.setInternalState(IntegrityMonitor.class, FACTORY_FIELD, new TestFactory());
IntegrityMonitor.setUnitTesting(true);
-
+
+ testTime = new TestTime();
+ Whitebox.setInternalState(MonitorTime.class, TIME_INSTANCE_FIELD, testTime);
+
properties = new Properties();
properties.put(IntegrityMonitorProperties.DB_DRIVER, dbDriver);
properties.put(IntegrityMonitorProperties.DB_URL, dbUrl);
@@ -157,7 +172,7 @@ public class IntegrityMonitorTestBase {
properties.put(IntegrityMonitorProperties.SITE_NAME, siteName);
properties.put(IntegrityMonitorProperties.NODE_TYPE, nodeType);
properties.put(IntegrityMonitorProperties.REFRESH_STATE_AUDIT_INTERVAL_MS,
- String.valueOf(100L * CYCLE_INTERVAL_MS));
+ String.valueOf(REFRESH_INTERVAL_MS));
emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT, makeProperties());
@@ -180,12 +195,13 @@ public class IntegrityMonitorTestBase {
systemProps.put(JMX_PORT_PROP, savedJmxPort);
}
- IntegrityMonitor.setPersistenceUnit(savedPU);
- IntegrityMonitor.setCycleIntervalMillis(savedCycleIntervalMillis);
- IntegrityMonitor.setPropertyUnits(savedPropertyUnits);
+ Whitebox.setInternalState(MonitorTime.class, TIME_INSTANCE_FIELD, savedTime);
+ Whitebox.setInternalState(IntegrityMonitor.class, FACTORY_FIELD, savedFactory);
IntegrityMonitor.setUnitTesting(false);
+ stopMonitor();
+
// this should result in the in-memory DB being deleted
em.close();
emf.close();
@@ -217,6 +233,13 @@ public class IntegrityMonitorTestBase {
}
/**
+ * @return the original integrity monitor factory
+ */
+ static Factory getSavedFactory() {
+ return savedFactory;
+ }
+
+ /**
* Stops the IntegrityMonitor instance.
*/
private static void stopMonitor() {
@@ -227,6 +250,13 @@ public class IntegrityMonitorTestBase {
// no need to log, as exception was already logged
}
}
+
+ /**
+ * @return the "current" time, in milliseconds
+ */
+ protected static long getCurrentTestTime() {
+ return testTime.getMillis();
+ }
/**
* Makes a new Property set that's a clone of {@link #properties}.
@@ -240,11 +270,11 @@ public class IntegrityMonitorTestBase {
}
/**
- * Waits for a semaphore to be acquired
+ * Waits for a semaphore to be acquired.
*
- * @param sem the latch
+ * @param sem
* @throws InterruptedException if the thread is interrupted
- * @throws AssertionError if the latch did not reach zero in the allotted time
+ * @throws AssertionError if the semaphore was not acquired within the allotted time
*/
protected void waitSem(Semaphore sem) throws InterruptedException {
assertTrue(sem.tryAcquire(WAIT_MS, TimeUnit.MILLISECONDS));
@@ -284,6 +314,16 @@ 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> {