aboutsummaryrefslogtreecommitdiffstats
path: root/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2018-02-13 14:16:47 -0500
committerJim Hahn <jrh3@att.com>2018-02-13 14:26:22 -0500
commite871132b09476d7772fb8dbc597fb82248b89f6b (patch)
treec78deaa56f3c6af84b8a7362c6066e5f6ce18f10 /integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java
parent86664073b5a778c56e831d64b3a1883818af0ffe (diff)
Speed up integrity-audit tests
Added additional DbDAO constructors to facilitate JUnit testing. Added DbDAO destroy() method to close the EntityManagerFactory. Pulled out common code into IntegrityAuditTestBase and subclassed the tests from there. Added hooks to IntegrityAudit so that the AuditThread timers could be set to smaller values so that all of the junit tests could be run in much less time. Added similar hooks to DbAudit. Modified integrity-audit tests to use new utility classes to auto-close JPA managers. Modified integrity-audit tests to use new utility class to scan logger items without the need to scan the actual log file. Added code to new test superclass to truncate the four ONAP logs. Modified hooks in IntegrityAuditEntity to adjust serialization so that dates are not serialized/de-serialized when used in junit tests. Deleted TestingUtils. Added a test for invalid nodeType property. Fixed issue wherein AuditThread doesn't stop when interrupted. Change-Id: I5101995b6b68655b2810777bc4d2ec80c7cbc363 Issue-ID: POLICY-582 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java')
-rw-r--r--integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java77
1 files changed, 61 insertions, 16 deletions
diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java
index 4f7a15af..f1dbfec0 100644
--- a/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java
+++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* Integrity Audit
* ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 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.
@@ -21,10 +21,11 @@
package org.onap.policy.common.ia;
import java.util.Properties;
-
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CountDownLatch;
import org.onap.policy.common.ia.IntegrityAuditProperties.NodeTypeEnum;
-import org.onap.policy.common.logging.flexlogger.FlexLogger;
+import org.onap.policy.common.logging.flexlogger.FlexLogger;
import org.onap.policy.common.logging.flexlogger.Logger;
/**
@@ -46,16 +47,16 @@ public class IntegrityAudit {
/*
- * This is the audit period in seconds. For example, if it had a value of 3600, the audit
- * can only run once per hour. If it has a value of 60, it can run once per minute.
+ * This is the audit period in milliseconds. For example, if it had a value of 3600000, the audit
+ * can only run once per hour. If it has a value of 6000, it can run once per minute.
*
* Values:
- * integrityAuditPeriodSeconds < 0 (negative number) indicates the audit is off
- * integrityAuditPeriodSecconds == 0 indicates the audit is to run continuously
- * integrityAuditPeriodSeconds > 0 indicates the audit is to run at most once during the indicated period
+ * integrityAuditPeriodMillis < 0 (negative number) indicates the audit is off
+ * integrityAuditPeriodMillis == 0 indicates the audit is to run continuously
+ * integrityAuditPeriodMillis > 0 indicates the audit is to run at most once during the indicated period
*
*/
- private int integrityAuditPeriodSeconds;
+ private int integrityAuditPeriodMillis;
/**
* IntegrityAudit constructor
@@ -84,10 +85,12 @@ public class IntegrityAudit {
this.resourceName = resourceName;
if(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS) != null){ //It is allowed to be null
- this.integrityAuditPeriodSeconds= Integer.parseInt(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS).trim());
+ this.integrityAuditPeriodMillis= 1000 * Integer.parseInt(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS).trim());
+ } else if(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_MILLISECONDS) != null){ //It is allowed to be null
+ this.integrityAuditPeriodMillis= Integer.parseInt(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_MILLISECONDS).trim());
} else{
//If it is null, set it to the default value
- this.integrityAuditPeriodSeconds = IntegrityAuditProperties.DEFAULT_AUDIT_PERIOD_SECONDS;
+ this.integrityAuditPeriodMillis = 1000 * IntegrityAuditProperties.DEFAULT_AUDIT_PERIOD_SECONDS;
}
logger.info("Constructor: Exiting");
@@ -97,7 +100,7 @@ public class IntegrityAudit {
* Used during JUnit testing by AuditPeriodTest.java
*/
public int getIntegrityAuditPeriodSeconds() {
- return integrityAuditPeriodSeconds;
+ return (integrityAuditPeriodMillis / 1000);
}
/**
@@ -190,6 +193,15 @@ public class IntegrityAudit {
parmsAreBad = true;
}
}
+ else if(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_MILLISECONDS) != null){ //It is allowed to be null
+ try{
+ Integer.parseInt(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_MILLISECONDS).trim());
+ }catch(NumberFormatException nfe){
+ badparams.append(", auditPeriodMilliSeconds="
+ + properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_MILLISECONDS).trim());
+ parmsAreBad = true;
+ }
+ }
} // End else
logger.debug("parmsAreBad: exit:"
+ "\nresourceName: " + resourceName
@@ -203,22 +215,36 @@ public class IntegrityAudit {
* @throws Exception
*/
public void startAuditThread() throws Exception {
+ startAuditThread(null);
+ }
+ /**
+ * Starts the audit thread
+ * @param queue
+ * @return {@code true} if the thread was started, {@code false} otherwise
+ * @throws Exception
+ */
+ protected boolean startAuditThread(BlockingQueue<CountDownLatch> queue) throws Exception {
logger.info("startAuditThread: Entering");
- if (integrityAuditPeriodSeconds >= 0) {
+ boolean success = false;
+
+ if (integrityAuditPeriodMillis >= 0) {
this.auditThread = new AuditThread(this.resourceName,
this.persistenceUnit, this.properties,
- integrityAuditPeriodSeconds, this);
+ integrityAuditPeriodMillis, this, queue);
logger.info("startAuditThread: Audit started and will run every "
- + integrityAuditPeriodSeconds + " seconds");
+ + integrityAuditPeriodMillis/1000 + " seconds");
this.auditThread.start();
+ success = true;
} else {
logger.info("startAuditThread: Suppressing integrity audit, integrityAuditPeriodSeconds="
- + integrityAuditPeriodSeconds);
+ + integrityAuditPeriodMillis/1000);
}
logger.info("startAuditThread: Exiting");
+
+ return success;
}
/**
* Stops the audit thread
@@ -252,4 +278,23 @@ public class IntegrityAudit {
public static void setUnitTesting(boolean isUnitTesting) {
IntegrityAudit.isUnitTesting = isUnitTesting;
}
+
+ /**
+ * Waits a bit for the AuditThread to complete. Used by JUnit tests.
+ *
+ * @param twaitms
+ * wait time, in milliseconds
+ * @return {@code true} if the thread stopped within the given time,
+ * {@code false} otherwise
+ * @throws InterruptedException
+ */
+ protected boolean joinAuditThread(long twaitms) throws InterruptedException {
+ if(this.auditThread == null) {
+ return true;
+
+ } else {
+ this.auditThread.join(twaitms);
+ return ! this.auditThread.isAlive();
+ }
+ }
}