From 611f63a4bb71d677cf2665b1794e91148ba42a51 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 17 Jun 2020 08:37:15 -0400 Subject: Cleanup various sonar issues in policy-common Addressed the following issues: - unused imports - unused method parameters - use assertEquals, assertSame instead of assertTrue - provide the parametrized type for this generic Also fixed some checkstyle issues: - removed blank lines between "import" groups Issue-ID: POLICY-2650 Change-Id: I004bb650ac10c49ccd0fc405f6959896fec39f9b Signed-off-by: Jim Hahn --- .../onap/policy/common/im/IntegrityMonitor.java | 106 +++++++++++---------- 1 file changed, 58 insertions(+), 48 deletions(-) (limited to 'integrity-monitor/src/main/java/org/onap/policy/common/im/IntegrityMonitor.java') 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 d12a9bde..33c51e21 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 @@ -43,6 +43,7 @@ import javax.persistence.FlushModeType; import javax.persistence.LockModeType; import javax.persistence.Persistence; import javax.persistence.Query; +import javax.persistence.TypedQuery; import org.apache.commons.lang3.StringUtils; import org.eclipse.persistence.config.PersistenceUnitProperties; import org.onap.policy.common.im.jmx.ComponentAdmin; @@ -287,15 +288,15 @@ public class IntegrityMonitor { } protected void createOrUpdateForwardProgress(String resourceName) { - Query fquery = em.createQuery(QUERY_STRING); + TypedQuery fquery = em.createQuery(QUERY_STRING, ForwardProgressEntity.class); fquery.setParameter("rn", resourceName); - @SuppressWarnings("rawtypes") - List fpList = fquery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); + List fpList = + fquery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); ForwardProgressEntity fpx = null; if (!fpList.isEmpty()) { // ignores multiple results - fpx = (ForwardProgressEntity) fpList.get(0); + fpx = fpList.get(0); // refresh the object from DB in case cached data was returned em.refresh(fpx); if (logger.isDebugEnabled()) { @@ -318,15 +319,17 @@ public class IntegrityMonitor { } protected void createOrUpdateResourceReg(String resourceName, String jmxUrl, EntityTransaction et) { - Query rquery = em.createQuery("Select r from ResourceRegistrationEntity r where r.resourceName=:rn"); + TypedQuery rquery = + em.createQuery("Select r from ResourceRegistrationEntity r where r.resourceName=:rn", + ResourceRegistrationEntity.class); rquery.setParameter("rn", resourceName); - @SuppressWarnings("rawtypes") - List rrList = rquery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); + List rrList = + rquery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); ResourceRegistrationEntity rrx = null; if (!rrList.isEmpty()) { // ignores multiple results - rrx = (ResourceRegistrationEntity) rrList.get(0); + rrx = rrList.get(0); // refresh the object from DB in case cached data was returned em.refresh(rrx); if (logger.isDebugEnabled()) { @@ -555,41 +558,44 @@ public class IntegrityMonitor { AtomicReference stateManagementEntity = new AtomicReference<>(); String errorMsg = - withinTransaction(dep + ": ForwardProgressEntity DB operation failed with exception: ", () -> { - Query query = - em.createQuery("Select p from ForwardProgressEntity p where p.resourceName=:resource"); - query.setParameter(LC_RESOURCE_STRING, dep); + withinTransaction(dep + ": ForwardProgressEntity DB operation failed with exception: ", () -> { + TypedQuery query = em.createQuery( + "Select p from ForwardProgressEntity p where p.resourceName=:resource", + ForwardProgressEntity.class); + query.setParameter(LC_RESOURCE_STRING, dep); - @SuppressWarnings("rawtypes") - List fpList = - query.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); + List fpList = query.setLockMode(LockModeType.NONE) + .setFlushMode(FlushModeType.COMMIT).getResultList(); - if (!fpList.isEmpty()) { - // exists - forwardProgressEntity.set((ForwardProgressEntity) fpList.get(0)); - // refresh the object from DB in case cached data was - // returned - em.refresh(forwardProgressEntity.get()); - logger.debug("Found entry in ForwardProgressEntity table for dependent Resource={}", dep); - return null; - - } else { - return dep + ": resource not found in ForwardProgressEntity database table"; - } - }); + if (!fpList.isEmpty()) { + // exists + forwardProgressEntity.set(fpList.get(0)); + // refresh the object from DB in case cached data was + // returned + em.refresh(forwardProgressEntity.get()); + logger.debug("Found entry in ForwardProgressEntity table for dependent Resource={}", + dep); + return null; + + } else { + return dep + ": resource not found in ForwardProgressEntity database table"; + } + }); if (errorMsg == null) { errorMsg = withinTransaction(dep + ": StateManagementEntity DB read failed with exception: ", () -> { // query if StateManagement entry exists for dependent resource - Query query = em.createQuery("Select p from StateManagementEntity p where p.resourceName=:resource"); + TypedQuery query = + em.createQuery("Select p from StateManagementEntity p where p.resourceName=:resource", + StateManagementEntity.class); query.setParameter(LC_RESOURCE_STRING, dep); - @SuppressWarnings("rawtypes") - List smList = query.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); + List smList = + query.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); if (!smList.isEmpty()) { // exist - stateManagementEntity.set((StateManagementEntity) smList.get(0)); + stateManagementEntity.set(smList.get(0)); // refresh the object from DB in case cached data was // returned em.refresh(stateManagementEntity.get()); @@ -713,15 +719,15 @@ public class IntegrityMonitor { } private String fpCheck2(String dep) { - Query fquery = em.createQuery(QUERY_STRING); + TypedQuery fquery = em.createQuery(QUERY_STRING, ForwardProgressEntity.class); fquery.setParameter("rn", dep); - @SuppressWarnings("rawtypes") - List fpList = fquery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); + List fpList = + fquery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); ForwardProgressEntity fpx; if (!fpList.isEmpty()) { // ignores multiple results - fpx = (ForwardProgressEntity) fpList.get(0); + fpx = fpList.get(0); // refresh the object from DB in case cached data was returned em.refresh(fpx); if (logger.isDebugEnabled()) { @@ -794,16 +800,18 @@ public class IntegrityMonitor { private String getJmxUrlFromDb(String dep, AtomicReference jmxUrl) { // query if ResourceRegistration entry exists for resourceName - Query rquery = em.createQuery("Select r from ResourceRegistrationEntity r where r.resourceName=:rn"); + TypedQuery rquery = + em.createQuery("Select r from ResourceRegistrationEntity r where r.resourceName=:rn", + ResourceRegistrationEntity.class); rquery.setParameter("rn", dep); - @SuppressWarnings("rawtypes") - List rrList = rquery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); + List rrList = + rquery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); ResourceRegistrationEntity rrx = null; if (!rrList.isEmpty()) { // ignores multiple results - rrx = (ResourceRegistrationEntity) rrList.get(0); + rrx = rrList.get(0); // refresh the object from DB in case cached data was // returned em.refresh(rrx); @@ -1197,15 +1205,15 @@ public class IntegrityMonitor { try { // query if ForwardProgress entry exists for resourceName - Query fquery = em.createQuery(QUERY_STRING); + TypedQuery fquery = em.createQuery(QUERY_STRING, ForwardProgressEntity.class); fquery.setParameter("rn", resourceName); - @SuppressWarnings("rawtypes") - List fpList = fquery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); + List fpList = + fquery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); ForwardProgressEntity fpx; if (!fpList.isEmpty()) { // ignores multiple results - fpx = (ForwardProgressEntity) fpList.get(0); + fpx = fpList.get(0); // refresh the object from DB in case cached data was returned em.refresh(fpx); if (logger.isDebugEnabled()) { @@ -1544,14 +1552,16 @@ public class IntegrityMonitor { try { // query if StateManagement entry exists for fpe resource - Query query = em.createQuery("Select p from StateManagementEntity p where p.resourceName=:resource"); + TypedQuery query = + em.createQuery("Select p from StateManagementEntity p where p.resourceName=:resource", + StateManagementEntity.class); query.setParameter(LC_RESOURCE_STRING, fpe.getResourceName()); - @SuppressWarnings("rawtypes") - List smList = query.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); + List smList = + query.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); if (!smList.isEmpty()) { // exists - sme = (StateManagementEntity) smList.get(0); + sme = smList.get(0); // refresh the object from DB in case cached data was // returned em.refresh(sme); -- cgit 1.2.3-korg