aboutsummaryrefslogtreecommitdiffstats
path: root/integrity-audit
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-06-24 08:41:18 -0400
committerJim Hahn <jrh3@att.com>2019-06-24 12:54:43 -0400
commitb52c7f21f7defaeaf86ff3921fe7bc18670ce2c1 (patch)
tree2c658433bf5a26cfd05e95feb9994f197662e94b /integrity-audit
parentd6aa3faba917b45e01f5001fe3a768feddedc798 (diff)
Fix more sonar issues in Integrity Audit & Monitor
Removed duplicate code in DbDao by refactoring common code into a new updateIae() method. Removed duplicate code in IntegrityMonitor by refactoring common code into a new withinTransaction() method. Removed duplicate code in StateManagementEntity, StateElement by replacing with lombok Getter & Setter annotations. Removed duplicate code in StateManagement by refactoring common code into new setState() and getState() methods. Also removed logger.isDebugEnabled() tests. Added coverage for StateChangeNotifier. Change-Id: I2e29b836dafc5de569a2267206a6a34105e44021 Issue-ID: POLICY-1791 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'integrity-audit')
-rw-r--r--integrity-audit/src/main/java/org/onap/policy/common/ia/DbDao.java195
1 files changed, 75 insertions, 120 deletions
diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/DbDao.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbDao.java
index 4833733c..0235715d 100644
--- a/integrity-audit/src/main/java/org/onap/policy/common/ia/DbDao.java
+++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbDao.java
@@ -26,6 +26,7 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
+import java.util.function.BiConsumer;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
@@ -330,27 +331,10 @@ public class DbDao {
* @throws DbDaoTransactionException if an error occurs
*/
public IntegrityAuditEntity getMyIntegrityAuditEntity() throws DbDaoTransactionException {
- try {
- EntityManager em = emf.createEntityManager();
-
- // Start a transaction
- EntityTransaction et = em.getTransaction();
-
- et.begin();
- // if IntegrityAuditEntity entry exists for resourceName and PU, retrieve it
- Query iaequery = em.createQuery(
- SELECT_STRING);
- iaequery.setParameter("rn", this.resourceName);
- iaequery.setParameter("pu", this.persistenceUnit);
+ return updateIae("getMyIntegrityAuditEntity", this.resourceName, this.persistenceUnit, (em,iae) -> {
- @SuppressWarnings("rawtypes")
- List iaeList = iaequery.getResultList();
- IntegrityAuditEntity iae = null;
-
- if (!iaeList.isEmpty()) {
- // ignores multiple results
- iae = (IntegrityAuditEntity) iaeList.get(0);
+ if (iae != null) {
// refresh the object from DB in case cached data was returned
em.refresh(iae);
logger.info(RESOURCE_MESSAGE + this.resourceName + WITH_PERSISTENCE_MESSAGE + this.persistenceUnit
@@ -360,18 +344,7 @@ public class DbDao {
logger.error("Attempting to setLastUpdated" + " on an entry that does not exist: resource "
+ this.resourceName + WITH_PERSISTENCE_MESSAGE + this.persistenceUnit);
}
-
- // close the transaction
- et.commit();
- // close the EntityManager
- em.close();
-
- return iae;
- } catch (Exception e) {
- String msg = DBDAO_MESSAGE + "setLastUpdated() " + ENCOUNTERED_MESSAGE;
- logger.error(msg + e);
- throw new DbDaoTransactionException(e);
- }
+ });
}
@@ -430,31 +403,14 @@ public class DbDao {
* default
*/
private void register(String altDbUrl) throws DbDaoTransactionException {
- try {
- EntityManager em = emf.createEntityManager();
-
- // Start a transaction
- EntityTransaction et = em.getTransaction();
- et.begin();
-
- // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not
- // found, create a new entry
- Query iaequery = em.createQuery(
- SELECT_STRING);
- iaequery.setParameter("rn", this.resourceName);
- iaequery.setParameter("pu", this.persistenceUnit);
-
- @SuppressWarnings("rawtypes")
- List iaeList = iaequery.getResultList();
- IntegrityAuditEntity iae;
+ updateIae("register", this.resourceName, this.persistenceUnit, (em,iae) -> {
+ IntegrityAuditEntity iae2 = iae;
// If it already exists, we just want to update the properties and lastUpdated date
- if (!iaeList.isEmpty()) {
- // ignores multiple results
- iae = (IntegrityAuditEntity) iaeList.get(0);
+ if (iae2 != null) {
// refresh the object from DB in case cached data was returned
- em.refresh(iae);
+ em.refresh(iae2);
logger.info(RESOURCE_MESSAGE + this.resourceName + WITH_PERSISTENCE_MESSAGE + this.persistenceUnit
+ " exists and entry be updated");
} else {
@@ -462,34 +418,31 @@ public class DbDao {
// designated values
logger.info("Adding resource " + resourceName + WITH_PERSISTENCE_MESSAGE + this.persistenceUnit
+ " to IntegrityAuditEntity table");
- iae = new IntegrityAuditEntity();
- iae.setResourceName(this.resourceName);
- iae.setPersistenceUnit(this.persistenceUnit);
- iae.setDesignated(false);
+ iae2 = new IntegrityAuditEntity();
+ iae2.setResourceName(this.resourceName);
+ iae2.setPersistenceUnit(this.persistenceUnit);
+ iae2.setDesignated(false);
}
- // update/set properties in entry
- iae.setSite(this.siteName);
- iae.setNodeType(this.nodeType);
- iae.setJdbcDriver(this.dbDriver);
- iae.setJdbcPassword(properties.getProperty(IntegrityAuditProperties.DB_PWD).trim());
- iae.setJdbcUrl(altDbUrl == null ? this.dbUrl : altDbUrl);
- iae.setJdbcUser(dbUser);
-
- em.persist(iae);
- // flush to the DB
- em.flush();
- // commit transaction
- et.commit();
- em.close();
- } catch (Exception e) {
- String msg = DBDAO_MESSAGE + "register() " + "encountered a problem in execution: ";
- logger.error(msg + e);
- throw new DbDaoTransactionException(e);
- }
+ register2(altDbUrl, em, iae2);
+ });
}
+ private void register2(String altDbUrl, EntityManager em, IntegrityAuditEntity iae) {
+ // update/set properties in entry
+ iae.setSite(this.siteName);
+ iae.setNodeType(this.nodeType);
+ iae.setJdbcDriver(this.dbDriver);
+ iae.setJdbcPassword(properties.getProperty(IntegrityAuditProperties.DB_PWD).trim());
+ iae.setJdbcUrl(altDbUrl == null ? this.dbUrl : altDbUrl);
+ iae.setJdbcUser(dbUser);
+
+ em.persist(iae);
+ // flush to the DB
+ em.flush();
+ }
+
public void setDesignated(boolean designated) throws DbDaoTransactionException {
setDesignated(this.resourceName, this.persistenceUnit, designated);
}
@@ -506,6 +459,41 @@ public class DbDao {
throws DbDaoTransactionException {
logger.debug("setDesignated: enter, resourceName: " + resourceName + ", persistenceUnit: " + persistenceUnit
+ ", designated: " + desig);
+
+ updateIae("setDesignated", resourceName, persistenceUnit, (em,iae) -> {
+
+ if (iae != null) {
+ // refresh the object from DB in case cached data was returned
+ em.refresh(iae);
+ logger.info(RESOURCE_MESSAGE + resourceName + WITH_PERSISTENCE_MESSAGE + persistenceUnit
+ + " exists and designated be updated");
+ iae.setDesignated(desig);
+
+ em.persist(iae);
+ // flush to the DB
+ em.flush();
+ } else {
+ // If it does not exist, log an error
+ logger.error("Attempting to setDesignated(" + desig + ") on an entry that does not exist:"
+ + " resource " + resourceName + WITH_PERSISTENCE_MESSAGE + persistenceUnit);
+ }
+ });
+
+ }
+
+ /**
+ * Queries for an audit entity and then updates it using an "updater" function.
+ *
+ * @param methodName name of the method that invoked this
+ * @param resourceName the resource name
+ * @param persistenceUnit the persistence unit
+ * @param updater function to update the entity; the argument will be the entity to be
+ * updated, or {@code null} if the entity is not found
+ * @return the entity that was found, or {@code null} if the entity is not found
+ * @throws DbDaoTransactionException if an error occurs
+ */
+ private IntegrityAuditEntity updateIae(String methodName, String resourceName, String persistenceUnit,
+ BiConsumer<EntityManager,IntegrityAuditEntity> updater) throws DbDaoTransactionException {
try {
EntityManager em = emf.createEntityManager();
@@ -517,8 +505,7 @@ public class DbDao {
// if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not
// found, create a new entry
- Query iaequery = em.createQuery(
- SELECT_STRING);
+ Query iaequery = em.createQuery(SELECT_STRING);
iaequery.setParameter("rn", resourceName);
iaequery.setParameter("pu", persistenceUnit);
@@ -529,27 +516,23 @@ public class DbDao {
if (!iaeList.isEmpty()) {
// ignores multiple results
iae = (IntegrityAuditEntity) iaeList.get(0);
- // refresh the object from DB in case cached data was returned
- em.refresh(iae);
- logger.info(RESOURCE_MESSAGE + resourceName + WITH_PERSISTENCE_MESSAGE + persistenceUnit
- + " exists and designated be updated");
- iae.setDesignated(desig);
- em.persist(iae);
- // flush to the DB
- em.flush();
} else {
- // If it does not exist, log an error
- logger.error("Attempting to setDesignated(" + desig + ") on an entry that does not exist:"
- + " resource " + resourceName + WITH_PERSISTENCE_MESSAGE + persistenceUnit);
+ // If it does not exist
+ iae = null;
}
+ updater.accept(em, iae);
+
// close the transaction
et.commit();
// close the EntityManager
em.close();
+
+ return iae;
+
} catch (Exception e) {
- String msg = DBDAO_MESSAGE + "setDesignated() " + ENCOUNTERED_MESSAGE;
+ String msg = DBDAO_MESSAGE + methodName + "() " + ENCOUNTERED_MESSAGE;
logger.error(msg + e);
throw new DbDaoTransactionException(e);
}
@@ -564,28 +547,10 @@ public class DbDao {
public void setLastUpdated() throws DbDaoTransactionException {
logger.debug("setLastUpdated: enter, resourceName: " + this.resourceName + ", persistenceUnit: "
+ this.persistenceUnit);
- try {
- EntityManager em = emf.createEntityManager();
- // Start a transaction
- EntityTransaction et = em.getTransaction();
+ updateIae("setLastUpdated", this.resourceName, this.persistenceUnit, (em,iae) -> {
- et.begin();
-
- // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not
- // found, create a new entry
- Query iaequery = em.createQuery(
- SELECT_STRING);
- iaequery.setParameter("rn", this.resourceName);
- iaequery.setParameter("pu", this.persistenceUnit);
-
- @SuppressWarnings("rawtypes")
- List iaeList = iaequery.getResultList();
- IntegrityAuditEntity iae;
-
- if (!iaeList.isEmpty()) {
- // ignores multiple results
- iae = (IntegrityAuditEntity) iaeList.get(0);
+ if (iae != null) {
// refresh the object from DB in case cached data was returned
em.refresh(iae);
logger.info(RESOURCE_MESSAGE + this.resourceName + WITH_PERSISTENCE_MESSAGE + this.persistenceUnit
@@ -600,17 +565,7 @@ public class DbDao {
logger.error("Attempting to setLastUpdated" + " on an entry that does not exist:" + " resource "
+ this.resourceName + WITH_PERSISTENCE_MESSAGE + this.persistenceUnit);
}
-
- // close the transaction
- et.commit();
- // close the EntityManager
- em.close();
- } catch (Exception e) {
- String msg = DBDAO_MESSAGE + "setLastUpdated() " + ENCOUNTERED_MESSAGE;
- logger.error(msg + e);
- throw new DbDaoTransactionException(e);
- }
-
+ });
}
/**