From c9392e1a126de54ae8a0e1f3316729e878115300 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 19 Jun 2019 17:31:24 -0400 Subject: Fix sonar issues in integrity audit Mostly used Eclipse Refactor->Extract Method to reduce cyclomatic complexity. Also combined a few "if" statements to reduce nesting levels. Also addressed some sonar issues in the tests (e.g., use "<>" where appropriate). Did not attempt to increase junit coverage. Change-Id: I9d6c1305ce455f0d64249b548d123bb9bf37292a Issue-ID: POLICY-1791 Signed-off-by: Jim Hahn --- .../java/org/onap/policy/common/ia/DbDaoTest.java | 51 +++++++++++----------- 1 file changed, 25 insertions(+), 26 deletions(-) (limited to 'integrity-audit/src/test/java/org/onap/policy/common/ia/DbDaoTest.java') diff --git a/integrity-audit/src/test/java/org/onap/policy/common/ia/DbDaoTest.java b/integrity-audit/src/test/java/org/onap/policy/common/ia/DbDaoTest.java index 357e638c..ef21e831 100644 --- a/integrity-audit/src/test/java/org/onap/policy/common/ia/DbDaoTest.java +++ b/integrity-audit/src/test/java/org/onap/policy/common/ia/DbDaoTest.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -60,6 +60,11 @@ import org.onap.policy.common.utils.time.TestTime; * tasks. */ public class DbDaoTest extends IntegrityAuditTestBase { + private static final String SELECT_ENTITIES = + "Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"; + private static final String SITE_B = "SiteB"; + private static final String ENTITY_CLASS_NAME = "org.onap.policy.common.ia.jpa.IntegrityAuditEntity"; + private static String resourceName = "pdp0"; private DbDao dbDao; @@ -103,8 +108,7 @@ public class DbDaoTest extends IntegrityAuditTestBase { dbDao = new DbDao(resourceName, A_SEQ_PU, properties); // Find the proper entry in the database - Query iaequery = em.createQuery( - "Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + Query iaequery = em.createQuery(SELECT_ENTITIES); iaequery.setParameter("rn", DbDaoTest.resourceName); iaequery.setParameter("pu", DbDaoTest.A_SEQ_PU); @@ -131,13 +135,12 @@ public class DbDaoTest extends IntegrityAuditTestBase { // Change site_name in properties to test that an update was made to // an existing entry in the table - properties.put(IntegrityAuditProperties.SITE_NAME, "SiteB"); + properties.put(IntegrityAuditProperties.SITE_NAME, SITE_B); dbDao = new DbDao(resourceName, A_SEQ_PU, properties); try (EntityTransCloser et = new EntityTransCloser(em.getTransaction())) { // Find the proper entry in the database - Query iaequery = em.createQuery( - "Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + Query iaequery = em.createQuery(SELECT_ENTITIES); iaequery.setParameter("rn", DbDaoTest.resourceName); iaequery.setParameter("pu", DbDaoTest.A_SEQ_PU); @@ -158,7 +161,7 @@ public class DbDaoTest extends IntegrityAuditTestBase { et.commit(); // Assert that the site_name for the existing entry was updated - assertEquals("SiteB", iae.getSite()); + assertEquals(SITE_B, iae.getSite()); } } } @@ -187,7 +190,6 @@ public class DbDaoTest extends IntegrityAuditTestBase { dbDao = new DbDao(resourceName, A_SEQ_PU, properties); IntegrityAuditEntity iae = dbDao.getMyIntegrityAuditEntity(); - // assertEquals("integrityAuditPU", iae.getPersistenceUnit()); assertEquals(A_SEQ_PU, iae.getPersistenceUnit()); } @@ -200,8 +202,7 @@ public class DbDaoTest extends IntegrityAuditTestBase { dbDao = new DbDao(resourceName, A_SEQ_PU, properties); // Find the proper database entry - Query iaequery = em - .createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + Query iaequery = em.createQuery(SELECT_ENTITIES); iaequery.setParameter("rn", DbDaoTest.resourceName); iaequery.setParameter("pu", DbDaoTest.A_SEQ_PU); @@ -239,8 +240,7 @@ public class DbDaoTest extends IntegrityAuditTestBase { dbDao.setDesignated(resourceName, A_SEQ_PU, true); // Find the proper entry in the database - Query iaequery = em.createQuery( - "Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + Query iaequery = em.createQuery(SELECT_ENTITIES); iaequery.setParameter("rn", resourceName); iaequery.setParameter("pu", A_SEQ_PU); @@ -275,13 +275,12 @@ public class DbDaoTest extends IntegrityAuditTestBase { try (EntityTransCloser et = new EntityTransCloser(em.getTransaction())) { TestTime testTime = getTestTime(); - + // Create an entry dbDao = new DbDao(resourceName, A_SEQ_PU, properties); // Find the proper entry in the database - Query iaequery = em.createQuery( - "Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + Query iaequery = em.createQuery(SELECT_ENTITIES); iaequery.setParameter("rn", resourceName); iaequery.setParameter("pu", A_SEQ_PU); @@ -301,7 +300,7 @@ public class DbDaoTest extends IntegrityAuditTestBase { // ensure dates are different by sleeping for a bit testTime.sleep(1); - iae.setSite("SiteB"); + iae.setSite(SITE_B); iae.setLastUpdated(testTime.getDate()); final Date newDate = iae.getLastUpdated(); @@ -328,7 +327,7 @@ public class DbDaoTest extends IntegrityAuditTestBase { new DbDao("pdp2", A_SEQ_PU, properties).destroy(); // Obtain a hash with the persisted objects - Map entries = dbDao.getAllMyEntries("org.onap.policy.common.ia.jpa.IntegrityAuditEntity"); + Map entries = dbDao.getAllMyEntries(ENTITY_CLASS_NAME); // Assert there were 3 entries for that class assertEquals(3, entries.size()); @@ -349,11 +348,11 @@ public class DbDaoTest extends IntegrityAuditTestBase { // Obtain all entity keys CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(); - Root rootEntry = cq.from(Class.forName("org.onap.policy.common.ia.jpa.IntegrityAuditEntity")); + Root rootEntry = cq.from(Class.forName(ENTITY_CLASS_NAME)); CriteriaQuery all = cq.select(rootEntry); TypedQuery allQuery = em.createQuery(all); List objectList = allQuery.getResultList(); - HashSet resultSet = new HashSet(); + HashSet resultSet = new HashSet<>(); PersistenceUnitUtil util = emf.getPersistenceUnitUtil(); for (Object o : objectList) { Object key = util.getIdentifier(o); @@ -362,7 +361,7 @@ public class DbDaoTest extends IntegrityAuditTestBase { // Obtain a hash with the persisted objects Map entries = - dbDao.getAllMyEntries("org.onap.policy.common.ia.jpa.IntegrityAuditEntity", resultSet); + dbDao.getAllMyEntries(ENTITY_CLASS_NAME, resultSet); // Assert there were 3 entries for that class assertEquals(3, entries.size()); @@ -383,7 +382,7 @@ public class DbDaoTest extends IntegrityAuditTestBase { // Obtain a hash with the persisted objects Map entries = dbDao.getAllEntries("integrityAuditPU", properties, - "org.onap.policy.common.ia.jpa.IntegrityAuditEntity"); + ENTITY_CLASS_NAME); // Assert there were 3 entries for that class assertEquals(3, entries.size()); @@ -405,11 +404,11 @@ public class DbDaoTest extends IntegrityAuditTestBase { // Obtain all entity keys CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(); - Root rootEntry = cq.from(Class.forName("org.onap.policy.common.ia.jpa.IntegrityAuditEntity")); + Root rootEntry = cq.from(Class.forName(ENTITY_CLASS_NAME)); CriteriaQuery all = cq.select(rootEntry); TypedQuery allQuery = em.createQuery(all); List objectList = allQuery.getResultList(); - HashSet resultSet = new HashSet(); + HashSet resultSet = new HashSet<>(); PersistenceUnitUtil util = emf.getPersistenceUnitUtil(); for (Object o : objectList) { Object key = util.getIdentifier(o); @@ -418,7 +417,7 @@ public class DbDaoTest extends IntegrityAuditTestBase { // Obtain a hash with the persisted objects Map entries = dbDao.getAllEntries("integrityAuditPU", properties, - "org.onap.policy.common.ia.jpa.IntegrityAuditEntity", resultSet); + ENTITY_CLASS_NAME, resultSet); // Assert there were 3 entries for that class assertEquals(3, entries.size()); @@ -439,7 +438,7 @@ public class DbDaoTest extends IntegrityAuditTestBase { // Obtain a hash with the persisted objects Map entries = - dbDao.getAllEntries(A_SEQ_PU, properties, "org.onap.policy.common.ia.jpa.IntegrityAuditEntity"); + dbDao.getAllEntries(A_SEQ_PU, properties, ENTITY_CLASS_NAME); // Assert there were 3 entries for that class assertEquals(3, entries.size()); -- cgit 1.2.3-korg