From 0f51fa4e072aeb29f4e323cb6013fb4a873aae5c Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 31 Jan 2018 18:10:34 -0500 Subject: Fix sonar generic Exceptions in policy/common IntegrityMonitor.java: Renamed variables, Ex: dep_groups => depGroups. Renamed enum constants, Ex: pdp_xacml => PDP_XACML. Merged "if" tests. Cast values to "long" before multiplying. Re-throw interrupt(). Write exception via logger instead of e.printStackTrace(). Moved constructor to top of the file. Removed most logger.isDebugEnabled() checks. Returned generic List instead of ArrayList. Used entrySet() instead of keySet(). Removed useless parentheses. Removed superfluous exceptions from "throws" declaration. DbAudit.java: Modified DbAudit to throw DbAuditException. Replaced references to HashSet and HashMap with generic Set and Map. Modified DbAudit to iterate over entrySet() instead of keySet(). ComponentAdminException: Created ComponentAdminException class. Modified ComponentAdmin methods to throw new exception class. Extracted "stateManager" String constant. Eliminated logger.isDebugEnabled() calls. Updated the license data. Eliminated double-checked locking problem from PropertyUtil: Modified code to use Initialization On Demand Holder idiom. Change-Id: Ic01288542041da26df483ce85ecaf292ac138f85 Issue-ID: POLICY-246 Signed-off-by: Jim Hahn --- .../java/org/onap/policy/common/ia/DbAudit.java | 68 +++++++++++++--------- .../main/java/org/onap/policy/common/ia/DbDAO.java | 12 ++-- .../common/ia/test/DbAuditCompareEntriesTest.java | 26 +++++---- .../org/onap/policy/common/ia/test/DbDAOTest.java | 21 ++++--- 4 files changed, 73 insertions(+), 54 deletions(-) (limited to 'integrity-audit/src') diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java index 0f354571..21bf5c8d 100644 --- a/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java +++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java @@ -25,7 +25,10 @@ import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import java.util.Properties; +import java.util.Set; import javax.persistence.Table; @@ -111,7 +114,7 @@ public class DbAudit { } // Obtain all persistence class names for the PU we are auditing - HashSet classNameSet = dbDAO.getPersistenceClassNames(); + Set classNameSet = dbDAO.getPersistenceClassNames(); if(classNameSet == null || classNameSet.isEmpty()){ String msg = "DbAudit: For node " + resourceName + " Found no persistence class names"; @@ -139,7 +142,7 @@ public class DbAudit { * This is the map of mismatched entries indexed by className. For * each class name there is a list of mismatched entries */ - HashMap> misMatchedMap = new HashMap<>(); + Map> misMatchedMap = new HashMap<>(); // We need to keep track of how long the audit is taking long startTime = System.currentTimeMillis(); @@ -155,7 +158,7 @@ public class DbAudit { } // all instances of the class for myIae - HashMap myEntries = dbDAO.getAllMyEntries(clazzName); + Map myEntries = dbDAO.getAllMyEntries(clazzName); //get a map of the objects indexed by id. Does not necessarily have any entries if (logger.isDebugEnabled()) { @@ -184,7 +187,7 @@ public class DbAudit { theirProperties.put(IntegrityAuditProperties.NODE_TYPE, iae.getNodeType()); //get a map of the instances for their iae indexed by id - HashMap theirEntries = dbDAO.getAllEntries(persistenceUnit, theirProperties, clazzName); + Map theirEntries = dbDAO.getAllEntries(persistenceUnit, theirProperties, clazzName); if (logger.isDebugEnabled()) { logger.debug("dbAudit: For persistenceUnit=" + persistenceUnit + ", clazzName=" + clazzName @@ -197,9 +200,9 @@ public class DbAudit { * Collect the IDs for the class where a mismatch occurred. We will check * them again for all nodes later. */ - HashSet misMatchedKeySet = compareEntries(myEntries, theirEntries); + Set misMatchedKeySet = compareEntries(myEntries, theirEntries); if(!misMatchedKeySet.isEmpty()){ - HashSet misMatchedEntry = misMatchedMap.get(clazzName); + Set misMatchedEntry = misMatchedMap.get(clazzName); if(misMatchedEntry == null){ misMatchedMap.put(clazzName, misMatchedKeySet); }else{ @@ -259,8 +262,8 @@ public class DbAudit { } // all instances of the class for myIae - HashSet keySet = misMatchedMap.get(clazzName); - HashMap myEntries = dbDAO.getAllMyEntries(clazzName, keySet); + Set keySet = misMatchedMap.get(clazzName); + Map myEntries = dbDAO.getAllMyEntries(clazzName, keySet); //get a map of the objects indexed by id if (logger.isDebugEnabled()) { @@ -289,14 +292,14 @@ public class DbAudit { theirProperties.put(IntegrityAuditProperties.NODE_TYPE, iae.getNodeType()); //get a map of the instances for their iae indexed by id - HashMap theirEntries = dbDAO.getAllEntries(persistenceUnit, theirProperties, clazzName, keySet); + Map theirEntries = dbDAO.getAllEntries(persistenceUnit, theirProperties, clazzName, keySet); /* * Compare myEntries with theirEntries and get back a set of mismatched IDs. * Collect the IDs for the class where a mismatch occurred. We will now * write an error log for each. */ - HashSet misMatchedKeySet = compareEntries(myEntries, theirEntries); + Set misMatchedKeySet = compareEntries(myEntries, theirEntries); if(!misMatchedKeySet.isEmpty()){ String keysString = ""; for(Object key: misMatchedKeySet){ @@ -349,22 +352,29 @@ public class DbAudit { * @throws InterruptedException * @throws DbDaoTransactionException */ - public void dbAuditSimulate(String resourceName, String persistenceUnit) throws InterruptedException, - DbDaoTransactionException { + public void dbAuditSimulate(String resourceName, String persistenceUnit) throws DbAuditException { - logger.info("dbAuditSimulate: Starting audit simulation for resourceName=" - + resourceName + ", persistenceUnit=" + persistenceUnit); + try { + logger.info("dbAuditSimulate: Starting audit simulation for resourceName=" + + resourceName + ", persistenceUnit=" + persistenceUnit); - for (int i = 0; i < AuditThread.AUDIT_SIMULATION_ITERATIONS; i++) { - dbDAO.setLastUpdated(); - logger.info("dbAuditSimulate: i=" + i + ", sleeping " - + AuditThread.AUDIT_SIMULATION_SLEEP_INTERVAL + "ms"); - Thread.sleep(AuditThread.AUDIT_SIMULATION_SLEEP_INTERVAL); - } - - logger.info("dbAuditSimulate: Finished audit simulation for resourceName=" - + resourceName + ", persistenceUnit=" + persistenceUnit); + for (int i = 0; i < AuditThread.AUDIT_SIMULATION_ITERATIONS; i++) { + dbDAO.setLastUpdated(); + logger.info("dbAuditSimulate: i=" + i + ", sleeping " + + AuditThread.AUDIT_SIMULATION_SLEEP_INTERVAL + "ms"); + Thread.sleep(AuditThread.AUDIT_SIMULATION_SLEEP_INTERVAL); + } + logger.info("dbAuditSimulate: Finished audit simulation for resourceName=" + + resourceName + ", persistenceUnit=" + persistenceUnit); + + } catch(DbDaoTransactionException e) { + throw new DbAuditException(e); + + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new DbAuditException(e); + } } /** @@ -373,7 +383,7 @@ public class DbAudit { * @param theirEntries * @return */ - public HashSet compareEntries(HashMap myEntries, HashMap theirEntries){ + public Set compareEntries(Map myEntries, Map theirEntries){ /* * Compare the entries for the same key in each of the hashmaps. The comparison will be done by serializing the objects * (create a byte array) and then do a byte array comparison. The audit will walk the local repository hash map comparing @@ -385,8 +395,9 @@ public class DbAudit { * */ HashSet misMatchedKeySet = new HashSet<>(); - for(Object key: myEntries.keySet()){ - byte[] mySerializedEntry = SerializationUtils.serialize((Serializable) myEntries.get(key)); + for(Entry ent: myEntries.entrySet()) { + Object key = ent.getKey(); + byte[] mySerializedEntry = SerializationUtils.serialize((Serializable) ent.getValue()); byte[] theirSerializedEntry = SerializationUtils.serialize((Serializable) theirEntries.get(key)); if(!Arrays.equals(mySerializedEntry, theirSerializedEntry)){ logger.debug("compareEntries: For myEntries.key=" + key + ", entries do not match"); @@ -396,9 +407,10 @@ public class DbAudit { } } //now compare it in the other direction to catch entries in their set that is not in my set - for(Object key: theirEntries.keySet()){ + for(Entry ent: theirEntries.entrySet()) { + Object key = ent.getKey(); byte[] mySerializedEntry = SerializationUtils.serialize((Serializable) myEntries.get(key)); - byte[] theirSerializedEntry = SerializationUtils.serialize((Serializable) theirEntries.get(key)); + byte[] theirSerializedEntry = SerializationUtils.serialize((Serializable) ent.getValue()); if(!Arrays.equals(mySerializedEntry, theirSerializedEntry)){ logger.debug("compareEntries: For theirEntries.key=" + key + ", entries do not match"); misMatchedKeySet.add(key); 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 99503854..b30c7730 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 @@ -24,7 +24,9 @@ import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Properties; +import java.util.Set; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; @@ -119,7 +121,7 @@ public class DbDAO { * @param className * @return */ - public HashMap getAllMyEntries(String className) { + public Map getAllMyEntries(String className) { logger.debug("getAllMyEntries: Entering, className=" + className); HashMap resultMap = new HashMap<>(); @@ -152,7 +154,7 @@ public class DbDAO { * @param keySet * @return */ - public HashMap getAllMyEntries(String className, HashSet keySet){ + public Map getAllMyEntries(String className, Set keySet){ logger.debug("getAllMyEntries: Entering, className=" + className + ",\n keySet=" + keySet); @@ -180,7 +182,7 @@ public class DbDAO { * @param className * @return */ - public HashMap getAllEntries(String persistenceUnit, Properties properties, String className){ + public Map getAllEntries(String persistenceUnit, Properties properties, String className){ logger.debug("getAllEntries: Entering, persistenceUnit=" + persistenceUnit + ",\n className=" + className); @@ -221,7 +223,7 @@ public class DbDAO { * @return */ - public HashMap getAllEntries(String persistenceUnit, Properties properties, String className, HashSet keySet){ + public Map getAllEntries(String persistenceUnit, Properties properties, String className, Set keySet){ logger.debug("getAllEntries: Entering, persistenceUnit=" + persistenceUnit + ",\n properties= " + properties + ",\n className=" + className + ",\n keySet= " + keySet); EntityManagerFactory theEmf = Persistence.createEntityManagerFactory(persistenceUnit, properties); @@ -364,7 +366,7 @@ public class DbDAO { * getPersistenceClassNames() gets all the persistence class names. * @return */ - public HashSet getPersistenceClassNames(){ + public Set getPersistenceClassNames(){ logger.debug("DbDAO: getPersistenceClassNames() entry"); HashSet returnList = new HashSet<>(); final Metamodel mm = emf.getMetamodel(); diff --git a/integrity-audit/src/test/java/org/onap/policy/common/ia/test/DbAuditCompareEntriesTest.java b/integrity-audit/src/test/java/org/onap/policy/common/ia/test/DbAuditCompareEntriesTest.java index d8388c58..3bedcb0b 100644 --- a/integrity-audit/src/test/java/org/onap/policy/common/ia/test/DbAuditCompareEntriesTest.java +++ b/integrity-audit/src/test/java/org/onap/policy/common/ia/test/DbAuditCompareEntriesTest.java @@ -26,7 +26,9 @@ import java.io.FileOutputStream; import java.util.Date; import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Properties; +import java.util.Set; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; @@ -129,7 +131,7 @@ public class DbAuditCompareEntriesTest { String className = null; //There is only one entry IntegrityAuditEntity, but we will check anyway - HashSet classNameSet = dbDAO.getPersistenceClassNames(); + Set classNameSet = dbDAO.getPersistenceClassNames(); for(String c : classNameSet){ if (c.equals("org.onap.policy.common.ia.jpa.IntegrityAuditEntity")){ className = c; @@ -175,7 +177,7 @@ public class DbAuditCompareEntriesTest { myEntries.put("pdp1", entry1); theirEntries.put("pdp1", entry2); - HashSet result = dbAudit.compareEntries(myEntries, theirEntries); + Set result = dbAudit.compareEntries(myEntries, theirEntries); /* * Assert that there are no mismatches returned @@ -235,7 +237,7 @@ public class DbAuditCompareEntriesTest { myEntries.put("pdp1", entry1); theirEntries.put("pdp1", entry2); - HashSet result = dbAudit.compareEntries(myEntries, theirEntries); + Set result = dbAudit.compareEntries(myEntries, theirEntries); /* * Assert that there was one mismatch @@ -322,7 +324,7 @@ public class DbAuditCompareEntriesTest { theirEntries.put("0", entry2); theirEntries.put("2", entry4); - HashSet mismatchResult = dbAudit.compareEntries(myEntries, theirEntries); + Set mismatchResult = dbAudit.compareEntries(myEntries, theirEntries); /* * Assert 3 mismatches/missing entries were found @@ -343,8 +345,8 @@ public class DbAuditCompareEntriesTest { dbDAO = new DbDAO(resourceName, persistenceUnit, properties); DbAudit dbAudit = new DbAudit(dbDAO); - HashSet classNameSet = dbDAO.getPersistenceClassNames(); - HashSet mismatchResult = new HashSet(); + Set classNameSet = dbDAO.getPersistenceClassNames(); + Set mismatchResult = new HashSet(); for(String c : classNameSet) { if (c.equals("org.onap.policy.common.ia.jpa.IntegrityAuditEntity")){ String resourceName1 = resourceName; @@ -537,10 +539,10 @@ public class DbAuditCompareEntriesTest { new DbDAO("pdp2", persistenceUnit, properties2); // Pull all entries and compare - HashSet classNameSet = dbDAO.getPersistenceClassNames(); - HashMap myEntries; - HashMap theirEntries; - HashSet mismatchResult = new HashSet(); + Set classNameSet = dbDAO.getPersistenceClassNames(); + Map myEntries; + Map theirEntries; + Set mismatchResult = new HashSet(); String className; for(String c : classNameSet) { className = c; @@ -572,7 +574,7 @@ public class DbAuditCompareEntriesTest { String className = null; //There is only one entry IntegrityAuditEntity, but we will check anyway - HashSet classNameSet = dbDAO.getPersistenceClassNames(); + Set classNameSet = dbDAO.getPersistenceClassNames(); for(String c : classNameSet){ if (c.equals("org.onap.policy.common.ia.test.jpa.IaTestEntity")){ className = c; @@ -608,7 +610,7 @@ public class DbAuditCompareEntriesTest { myEntries.put("0", iate); theirEntries.put("0", iate2); - HashSet result = dbAudit.compareEntries(myEntries, theirEntries); + Set result = dbAudit.compareEntries(myEntries, theirEntries); /* * Assert that there are no mismatches returned diff --git a/integrity-audit/src/test/java/org/onap/policy/common/ia/test/DbDAOTest.java b/integrity-audit/src/test/java/org/onap/policy/common/ia/test/DbDAOTest.java index 4fd51f6e..ac3bc587 100644 --- a/integrity-audit/src/test/java/org/onap/policy/common/ia/test/DbDAOTest.java +++ b/integrity-audit/src/test/java/org/onap/policy/common/ia/test/DbDAOTest.java @@ -20,7 +20,10 @@ package org.onap.policy.common.ia.test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; /* * All JUnits are designed to run in the local development environment @@ -28,10 +31,11 @@ import static org.junit.Assert.*; * tasks. */ import java.util.Date; -import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Properties; +import java.util.Set; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; @@ -47,7 +51,6 @@ import javax.persistence.criteria.Root; import org.junit.After; import org.junit.Before; import org.junit.Test; - import org.onap.policy.common.ia.DbDAO; import org.onap.policy.common.ia.DbDaoTransactionException; import org.onap.policy.common.ia.IntegrityAuditProperties; @@ -487,7 +490,7 @@ public class DbDAOTest { try { // Obtain a hash with the persisted objects - HashMap entries = d.getAllMyEntries("org.onap.policy.common.ia.jpa.IntegrityAuditEntity"); + Map entries = d.getAllMyEntries("org.onap.policy.common.ia.jpa.IntegrityAuditEntity"); // Assert there were 3 entries for that class assertEquals(3, entries.size()); @@ -543,7 +546,7 @@ public class DbDAOTest { } // Obtain a hash with the persisted objects - HashMap entries = d.getAllMyEntries("org.onap.policy.common.ia.jpa.IntegrityAuditEntity", resultSet); + Map entries = d.getAllMyEntries("org.onap.policy.common.ia.jpa.IntegrityAuditEntity", resultSet); // Assert there were 3 entries for that class assertEquals(3, entries.size()); @@ -591,7 +594,7 @@ public class DbDAOTest { try { // Obtain a hash with the persisted objects - HashMap entries = d.getAllEntries("integrityAuditPU", properties, "org.onap.policy.common.ia.jpa.IntegrityAuditEntity"); + Map entries = d.getAllEntries("integrityAuditPU", properties, "org.onap.policy.common.ia.jpa.IntegrityAuditEntity"); // Assert there were 3 entries for that class assertEquals(3, entries.size()); @@ -647,7 +650,7 @@ public class DbDAOTest { } // Obtain a hash with the persisted objects - HashMap entries = d.getAllEntries("integrityAuditPU", properties, "org.onap.policy.common.ia.jpa.IntegrityAuditEntity", resultSet); + Map entries = d.getAllEntries("integrityAuditPU", properties, "org.onap.policy.common.ia.jpa.IntegrityAuditEntity", resultSet); // Assert there were 3 entries for that class assertEquals(3, entries.size()); @@ -695,7 +698,7 @@ public class DbDAOTest { try { // Obtain a hash with the persisted objects - HashMap entries = d.getAllEntries(persistenceUnit, properties, "org.onap.policy.common.ia.jpa.IntegrityAuditEntity"); + Map entries = d.getAllEntries(persistenceUnit, properties, "org.onap.policy.common.ia.jpa.IntegrityAuditEntity"); // Assert there were 3 entries for that class assertEquals(3, entries.size()); @@ -733,7 +736,7 @@ public class DbDAOTest { e.printStackTrace(); } // Retrieve persistence class names - HashSet result = d.getPersistenceClassNames(); + Set result = d.getPersistenceClassNames(); assertEquals(1, result.size()); } } -- cgit 1.2.3-korg