From 2fa29d8632a6dc9fb855a732320b679d724f384f Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 9 Jan 2019 15:39:46 -0500 Subject: Remove policy/common integrity-audit suppressions Changed "DbDAO" to "DbDao". Change-Id: I51a6706cceef6857404ae7c12ce43e553ea0da29 Issue-ID: POLICY-1133 Signed-off-by: Jim Hahn --- .../common/ia/DbAuditCompareEntriesTest.java | 24 +- .../org/onap/policy/common/ia/DbAuditTest.java | 28 +- .../java/org/onap/policy/common/ia/DbDAOTest.java | 460 --------------------- .../java/org/onap/policy/common/ia/DbDaoTest.java | 460 +++++++++++++++++++++ .../common/ia/IntegrityAuditDesignationTest.java | 12 +- 5 files changed, 492 insertions(+), 492 deletions(-) delete mode 100644 integrity-audit/src/test/java/org/onap/policy/common/ia/DbDAOTest.java create mode 100644 integrity-audit/src/test/java/org/onap/policy/common/ia/DbDaoTest.java (limited to 'integrity-audit/src/test') diff --git a/integrity-audit/src/test/java/org/onap/policy/common/ia/DbAuditCompareEntriesTest.java b/integrity-audit/src/test/java/org/onap/policy/common/ia/DbAuditCompareEntriesTest.java index 6335482a..a5d0ef1b 100644 --- a/integrity-audit/src/test/java/org/onap/policy/common/ia/DbAuditCompareEntriesTest.java +++ b/integrity-audit/src/test/java/org/onap/policy/common/ia/DbAuditCompareEntriesTest.java @@ -1,8 +1,8 @@ -/*- +/* * ============LICENSE_START======================================================= * Integrity Audit * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 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. @@ -52,7 +52,7 @@ public class DbAuditCompareEntriesTest extends IntegrityAuditTestBase { private static Logger logger = FlexLogger.getLogger(DbAuditCompareEntriesTest.class); - private DbDAO dbDao; + private DbDao dbDao; private static String resourceName = "pdp1"; @BeforeClass @@ -105,7 +105,7 @@ public class DbAuditCompareEntriesTest extends IntegrityAuditTestBase { public void testSuccessfulComparison() throws Exception { logger.info("testSuccessfulComparison: Entering"); - dbDao = new DbDAO(resourceName, A_SEQ_PU, makeProperties()); + dbDao = new DbDao(resourceName, A_SEQ_PU, makeProperties()); final DbAudit dbAudit = new DbAudit(dbDao); String className = null; @@ -175,7 +175,7 @@ public class DbAuditCompareEntriesTest extends IntegrityAuditTestBase { public void testComparisonError() throws Exception { logger.info("testComparisonError: Entering"); - dbDao = new DbDAO(resourceName, A_SEQ_PU, makeProperties()); + dbDao = new DbDao(resourceName, A_SEQ_PU, makeProperties()); final DbAudit dbAudit = new DbAudit(dbDao); final String resourceName1 = resourceName; @@ -235,7 +235,7 @@ public class DbAuditCompareEntriesTest extends IntegrityAuditTestBase { public void testCompareMissingEntries() throws Exception { logger.info("testCompareMissingEntries: Entering"); - dbDao = new DbDAO(resourceName, A_SEQ_PU, makeProperties()); + dbDao = new DbDao(resourceName, A_SEQ_PU, makeProperties()); final DbAudit dbAudit = new DbAudit(dbDao); final String resourceName1 = resourceName; @@ -321,7 +321,7 @@ public class DbAuditCompareEntriesTest extends IntegrityAuditTestBase { public void testCompareAllHashEntities() throws Exception { logger.info("testCompareAllHashEntities: Entering"); - dbDao = new DbDAO(resourceName, A_SEQ_PU, makeProperties()); + dbDao = new DbDao(resourceName, A_SEQ_PU, makeProperties()); DbAudit dbAudit = new DbAudit(dbDao); Set classNameSet = dbDao.getPersistenceClassNames(); @@ -442,13 +442,13 @@ public class DbAuditCompareEntriesTest extends IntegrityAuditTestBase { truncateTables(properties2); // Add entries into DB1 - dbDao = new DbDAO(resourceName, A_SEQ_PU, properties); - new DbDAO("pdp2", A_SEQ_PU, properties).destroy(); + dbDao = new DbDao(resourceName, A_SEQ_PU, properties); + new DbDao("pdp2", A_SEQ_PU, properties).destroy(); DbAudit dbAudit = new DbAudit(dbDao); // Add entries into DB2 - DbDAO dbDao3 = new DbDAO(resourceName, A_SEQ_PU, properties2); - new DbDAO("pdp2", A_SEQ_PU, properties2).destroy(); + DbDao dbDao3 = new DbDao(resourceName, A_SEQ_PU, properties2); + new DbDao("pdp2", A_SEQ_PU, properties2).destroy(); // Pull all entries and compare Set classNameSet = dbDao.getPersistenceClassNames(); @@ -491,7 +491,7 @@ public class DbAuditCompareEntriesTest extends IntegrityAuditTestBase { public void testEmbeddedClass() throws Exception { logger.info("testEmbeddedClasses: Entering"); - dbDao = new DbDAO(resourceName, A_SEQ_PU, properties); + dbDao = new DbDao(resourceName, A_SEQ_PU, properties); final DbAudit dbAudit = new DbAudit(dbDao); String className = null; diff --git a/integrity-audit/src/test/java/org/onap/policy/common/ia/DbAuditTest.java b/integrity-audit/src/test/java/org/onap/policy/common/ia/DbAuditTest.java index 8e848787..836aa288 100644 --- a/integrity-audit/src/test/java/org/onap/policy/common/ia/DbAuditTest.java +++ b/integrity-audit/src/test/java/org/onap/policy/common/ia/DbAuditTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Integrity Audit * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 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. @@ -57,7 +57,7 @@ public class DbAuditTest extends IntegrityAuditTestBase { private EntityManagerFactory emf2; private EntityManager em2; - private DbDAO dbDao; + private DbDao dbDao; @BeforeClass public static void setUpBeforeClass() throws Exception { @@ -135,7 +135,7 @@ public class DbAuditTest extends IntegrityAuditTestBase { logger.info("noEntitiesTest: Entering"); - dbDao = new DbDAO(resourceName, A_SEQ_PU, properties); + dbDao = new DbDao(resourceName, A_SEQ_PU, properties); dbDao.deleteAllIntegrityAuditEntities(); try { DbAudit dbAudit = new DbAudit(dbDao); @@ -161,7 +161,7 @@ public class DbAuditTest extends IntegrityAuditTestBase { final ExtractAppender log = watch(debugLogger, "DbAudit: Found only (one) IntegrityAuditEntity entry:"); // Add one entry in the database - dbDao = new DbDAO(resourceName, A_SEQ_PU, properties); + dbDao = new DbDao(resourceName, A_SEQ_PU, properties); DbAudit dbAudit = new DbAudit(dbDao); dbAudit.dbAudit(resourceName, A_SEQ_PU, nodeType); @@ -206,24 +206,24 @@ public class DbAuditTest extends IntegrityAuditTestBase { /* * Create entries in DB1 & DB2 for the resource of interest */ - dbDao = new DbDAO(resourceName, A_SEQ_PU, properties); + dbDao = new DbDao(resourceName, A_SEQ_PU, properties); - new DbDAO(resourceName, A_SEQ_PU, properties2).destroy(); + new DbDao(resourceName, A_SEQ_PU, properties2).destroy(); /* * Entries in DB1, pointing to DB2, except for pdp3 */ - new DbDAO("pdp2", A_SEQ_PU, properties, dbUrl2).destroy(); - new DbDAO("pdp1", A_SEQ_PU, properties, dbUrl2).destroy(); - new DbDAO("pdp3", A_SEQ_PU, properties).destroy(); // mismatched URL - new DbDAO("pdp4", A_SEQ_PU, properties, dbUrl2).destroy(); + new DbDao("pdp2", A_SEQ_PU, properties, dbUrl2).destroy(); + new DbDao("pdp1", A_SEQ_PU, properties, dbUrl2).destroy(); + new DbDao("pdp3", A_SEQ_PU, properties).destroy(); // mismatched URL + new DbDao("pdp4", A_SEQ_PU, properties, dbUrl2).destroy(); /* * Identical entries in DB2, all pointing to DB2, including pdp3, but leaving out pdp4 */ - new DbDAO("pdp2", A_SEQ_PU, properties2).destroy(); - new DbDAO("pdp1", A_SEQ_PU, properties2).destroy(); - new DbDAO("pdp3", A_SEQ_PU, properties2).destroy(); + new DbDao("pdp2", A_SEQ_PU, properties2).destroy(); + new DbDao("pdp1", A_SEQ_PU, properties2).destroy(); + new DbDao("pdp3", A_SEQ_PU, properties2).destroy(); /* * Run the DB Audit, once it finds a mismatch and sleeps, update DB1 to have the same entry @@ -233,7 +233,7 @@ public class DbAuditTest extends IntegrityAuditTestBase { dbAudit.dbAudit(resourceName, A_SEQ_PU, nodeType); // update pdp3 entry in DB1 to point to DB2 - new DbDAO("pdp3", A_SEQ_PU, properties, dbUrl2).destroy(); + new DbDao("pdp3", A_SEQ_PU, properties, dbUrl2).destroy(); /* * Run the audit again and correct the mismatch, the result should be one entry in the 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 deleted file mode 100644 index 6e93529c..00000000 --- a/integrity-audit/src/test/java/org/onap/policy/common/ia/DbDAOTest.java +++ /dev/null @@ -1,460 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Integrity Audit - * ================================================================================ - * 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. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.ia; - -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 - * where they have write privileges and can execute time-sensitive - * tasks. - */ - -import java.util.Date; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.Set; - -import javax.persistence.PersistenceUnitUtil; -import javax.persistence.Query; -import javax.persistence.TypedQuery; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Root; - -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onap.policy.common.ia.jpa.IntegrityAuditEntity; -import org.onap.policy.common.utils.jpa.EntityTransCloser; -import org.onap.policy.common.utils.time.TestTime; - -/* - * All JUnits are designed to run in the local development environment - * where they have write privileges and can execute time-sensitive - * tasks. - */ -public class DbDAOTest extends IntegrityAuditTestBase { - private static String resourceName = "pdp0"; - - private DbDAO dbDao; - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - IntegrityAuditTestBase.setUpBeforeClass(DEFAULT_DB_URL_PREFIX + DbDAOTest.class.getSimpleName()); - } - - @AfterClass - public static void tearDownAfterClass() { - IntegrityAuditTestBase.tearDownAfterClass(); - } - - @Override - @Before - public void setUp() { - super.setUp(); - dbDao = null; - } - - /** - * Tear down after test cases. - */ - @Override - @After - public void tearDown() { - if (dbDao != null) { - dbDao.destroy(); - } - - super.tearDown(); - } - - /* Tests registering a new IntegrityAuditEntity object in the DB */ - @Test - public void testNewRegistration() throws Exception { - Properties properties = makeProperties(); - - try (EntityTransCloser et = new EntityTransCloser(em.getTransaction())) { - 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"); - iaequery.setParameter("rn", DbDAOTest.resourceName); - iaequery.setParameter("pu", DbDAOTest.A_SEQ_PU); - - @SuppressWarnings("rawtypes") - List iaeList = iaequery.getResultList(); - - // Assert that the IntegrityAuditEntity object was found - assertNotNull(iaeList); - - // flush to the DB - em.flush(); - et.commit(); - } - } - - /* - * Tests updating an IntegrityAuditEntity if it has already been registered - */ - @Test - public void testUpdateRegistration() throws Exception { - Properties properties = makeProperties(); - - dbDao = new DbDAO(resourceName, A_SEQ_PU, properties); - - // 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"); - 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"); - iaequery.setParameter("rn", DbDAOTest.resourceName); - iaequery.setParameter("pu", DbDAOTest.A_SEQ_PU); - - @SuppressWarnings("rawtypes") - List iaeList = iaequery.getResultList(); - IntegrityAuditEntity iae = null; - if (!iaeList.isEmpty()) { - // ignores multiple results - iae = (IntegrityAuditEntity) iaeList.get(0); - - em.refresh(iae); - em.persist(iae); - - // flush to the DB - em.flush(); - - // commit transaction - et.commit(); - - // Assert that the site_name for the existing entry was updated - assertEquals("SiteB", iae.getSite()); - } - } - } - - /* Tests obtaining all Integrity Audit Entities from a table */ - @Test - public void testGetIntegrityAuditEntities() throws Exception { - Properties properties = makeProperties(); - - // Add some entries to the DB - dbDao = new DbDAO(resourceName, A_SEQ_PU, properties); - new DbDAO("pdp1", A_SEQ_PU, properties).destroy(); - properties.put(IntegrityAuditProperties.NODE_TYPE, "pdp_drools"); - new DbDAO("pdp2", A_SEQ_PU, properties).destroy(); - - List entities; - // Obtain entries based on persistenceUnit and nodeType - entities = dbDao.getIntegrityAuditEntities(A_SEQ_PU, "pdp_xacml"); - assertEquals(2, entities.size()); - } - - /* Tests retrieving a DbDAO instance's IntegrityAuditEntity */ - @Test - public void testGetMyIntegrityAuditEntity() throws Exception { - Properties properties = makeProperties(); - - dbDao = new DbDAO(resourceName, A_SEQ_PU, properties); - IntegrityAuditEntity iae = dbDao.getMyIntegrityAuditEntity(); - // assertEquals("integrityAuditPU", iae.getPersistenceUnit()); - assertEquals(A_SEQ_PU, iae.getPersistenceUnit()); - } - - /* Tests obtaining an IntegrityAuditEntity by ID */ - @Test - public void testGetIntegrityAuditEntity() throws Exception { - Properties properties = makeProperties(); - - // Obtain an entry from the database based on ID - 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"); - iaequery.setParameter("rn", DbDAOTest.resourceName); - iaequery.setParameter("pu", DbDAOTest.A_SEQ_PU); - - @SuppressWarnings("rawtypes") - List iaeList = iaequery.getResultList(); - IntegrityAuditEntity iae = null; - 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); - - // Obtain ID for an IntegrityAuditEntity - PersistenceUnitUtil util = emf.getPersistenceUnitUtil(); - Object iaeId = util.getIdentifier(iae); - - // Obtain the same IntegrityAuditEntity based on ID - IntegrityAuditEntity iaeDuplicate = dbDao.getIntegrityAuditEntity((long) iaeId); - Object duplicateId = util.getIdentifier(iaeDuplicate); - - // Assert that the proper entry was retrieved based on ID - assertEquals((long) iaeId, (long) duplicateId); - } - } - - /* Tests setting an IntegrityAuditEntity as the designated node */ - @Test - public void testSetDesignated() throws Exception { - Properties properties = makeProperties(); - - try (EntityTransCloser et = new EntityTransCloser(em.getTransaction())) { - // Create an entry and set it's designated field to true - dbDao = new DbDAO(resourceName, A_SEQ_PU, properties); - 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"); - iaequery.setParameter("rn", resourceName); - iaequery.setParameter("pu", A_SEQ_PU); - - @SuppressWarnings("rawtypes") - List iaeList = iaequery.getResultList(); - IntegrityAuditEntity iae = null; - - if (!iaeList.isEmpty()) { - // ignores multiple results - iae = (IntegrityAuditEntity) iaeList.get(0); - em.refresh(iae); - - // Check if the node is designated - boolean result = iae.isDesignated(); - - // Assert that it is designated - assertTrue(result); - } - - // flush to the DB - em.flush(); - - // close the transaction - et.commit(); - } - } - - /* Tests that the lastUpdated column in the database is updated properly */ - @Test - public void testSetLastUpdated() throws Exception { - Properties properties = makeProperties(); - - 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"); - iaequery.setParameter("rn", resourceName); - iaequery.setParameter("pu", A_SEQ_PU); - - @SuppressWarnings("rawtypes") - List iaeList = iaequery.getResultList(); - IntegrityAuditEntity iae = null; - - 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); - - // Obtain old update value and set new update value - final Date oldDate = iae.getLastUpdated(); - - // ensure dates are different by sleeping for a bit - testTime.sleep(1); - - iae.setSite("SiteB"); - iae.setLastUpdated(testTime.getDate()); - final Date newDate = iae.getLastUpdated(); - - em.persist(iae); - // flush to the DB - em.flush(); - // close the transaction - et.commit(); - - // Assert that the old and new update times are different - assertNotEquals(oldDate, newDate); - } - } - } - - /* Tests that all the entries from a class can be retrieved */ - @Test - public void testGetAllMyEntriesString() throws Exception { - Properties properties = makeProperties(); - - // create entries for the IntegrityAuditEntity table - dbDao = new DbDAO(resourceName, A_SEQ_PU, properties); - new DbDAO("pdp1", A_SEQ_PU, properties).destroy(); - 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"); - - // Assert there were 3 entries for that class - assertEquals(3, entries.size()); - } - - /* - * Tests retrieving all entities in a Persistence Unit using the class name and a hashset of IDs - */ - @Test - public void testGetAllMyEntriesStringHashSet() throws Exception { - Properties properties = makeProperties(); - - // create entries for the IntegrityAuditEntity table - dbDao = new DbDAO(resourceName, A_SEQ_PU, properties); - new DbDAO("pdp1", A_SEQ_PU, properties).destroy(); - new DbDAO("pdp2", A_SEQ_PU, properties).destroy(); - - // 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")); - CriteriaQuery all = cq.select(rootEntry); - TypedQuery allQuery = em.createQuery(all); - List objectList = allQuery.getResultList(); - HashSet resultSet = new HashSet(); - PersistenceUnitUtil util = emf.getPersistenceUnitUtil(); - for (Object o : objectList) { - Object key = util.getIdentifier(o); - resultSet.add(key); - } - - // Obtain a hash with the persisted objects - Map entries = - dbDao.getAllMyEntries("org.onap.policy.common.ia.jpa.IntegrityAuditEntity", resultSet); - - // Assert there were 3 entries for that class - assertEquals(3, entries.size()); - } - - /* - * Tests retrieving all entities in a Persistence Unit using the persistence unit, properties, - * and class name - */ - @Test - public void testGetAllEntriesStringPropertiesString() throws Exception { - Properties properties = makeProperties(); - - // create entries for the IntegrityAuditEntity table - dbDao = new DbDAO(resourceName, A_SEQ_PU, properties); - new DbDAO("pdp1", A_SEQ_PU, properties).destroy(); - new DbDAO("pdp2", A_SEQ_PU, properties).destroy(); - - // Obtain a hash with the persisted objects - Map entries = dbDao.getAllEntries("integrityAuditPU", properties, - "org.onap.policy.common.ia.jpa.IntegrityAuditEntity"); - - // Assert there were 3 entries for that class - assertEquals(3, entries.size()); - } - - /* - * Tests retrieving all entities in a Persistence Unit using the persistence unit, properties, - * class name, and a hashset of IDs - */ - @Test - public void testGetAllEntriesStringPropertiesStringHashSet() throws Exception { - Properties properties = makeProperties(); - - // create entries for the IntegrityAuditEntity table - dbDao = new DbDAO(resourceName, A_SEQ_PU, properties); - new DbDAO("pdp1", A_SEQ_PU, properties).destroy(); - new DbDAO("pdp2", A_SEQ_PU, properties).destroy(); - - // 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")); - CriteriaQuery all = cq.select(rootEntry); - TypedQuery allQuery = em.createQuery(all); - List objectList = allQuery.getResultList(); - HashSet resultSet = new HashSet(); - PersistenceUnitUtil util = emf.getPersistenceUnitUtil(); - for (Object o : objectList) { - Object key = util.getIdentifier(o); - resultSet.add(key); - } - - // Obtain a hash with the persisted objects - Map entries = dbDao.getAllEntries("integrityAuditPU", properties, - "org.onap.policy.common.ia.jpa.IntegrityAuditEntity", resultSet); - - // Assert there were 3 entries for that class - assertEquals(3, entries.size()); - } - - /* - * Tests getting all the entries from a class based on persistenceUnit, properties, and - * className - */ - @Test - public void testGetAllEntries() throws Exception { - Properties properties = makeProperties(); - - // create entries for the IntegrityAuditEntity table - dbDao = new DbDAO(resourceName, A_SEQ_PU, properties); - new DbDAO("pdp1", A_SEQ_PU, properties).destroy(); - new DbDAO("pdp2", A_SEQ_PU, properties).destroy(); - - // Obtain a hash with the persisted objects - Map entries = - dbDao.getAllEntries(A_SEQ_PU, properties, "org.onap.policy.common.ia.jpa.IntegrityAuditEntity"); - - // Assert there were 3 entries for that class - assertEquals(3, entries.size()); - } - - /** - * Tests obtaining all class names of persisted classes. - */ - public void testGetPersistenceClassNames() throws Exception { - Properties properties = makeProperties(); - - dbDao = new DbDAO(resourceName, A_SEQ_PU, properties); - - // Retrieve persistence class names - Set result = dbDao.getPersistenceClassNames(); - assertEquals(1, result.size()); - } -} 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 new file mode 100644 index 00000000..357e638c --- /dev/null +++ b/integrity-audit/src/test/java/org/onap/policy/common/ia/DbDaoTest.java @@ -0,0 +1,460 @@ +/* + * ============LICENSE_START======================================================= + * Integrity Audit + * ================================================================================ + * Copyright (C) 2017-2019 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. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.ia; + +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 + * where they have write privileges and can execute time-sensitive + * tasks. + */ + +import java.util.Date; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; + +import javax.persistence.PersistenceUnitUtil; +import javax.persistence.Query; +import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.common.ia.jpa.IntegrityAuditEntity; +import org.onap.policy.common.utils.jpa.EntityTransCloser; +import org.onap.policy.common.utils.time.TestTime; + +/* + * All JUnits are designed to run in the local development environment + * where they have write privileges and can execute time-sensitive + * tasks. + */ +public class DbDaoTest extends IntegrityAuditTestBase { + private static String resourceName = "pdp0"; + + private DbDao dbDao; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + IntegrityAuditTestBase.setUpBeforeClass(DEFAULT_DB_URL_PREFIX + DbDaoTest.class.getSimpleName()); + } + + @AfterClass + public static void tearDownAfterClass() { + IntegrityAuditTestBase.tearDownAfterClass(); + } + + @Override + @Before + public void setUp() { + super.setUp(); + dbDao = null; + } + + /** + * Tear down after test cases. + */ + @Override + @After + public void tearDown() { + if (dbDao != null) { + dbDao.destroy(); + } + + super.tearDown(); + } + + /* Tests registering a new IntegrityAuditEntity object in the DB */ + @Test + public void testNewRegistration() throws Exception { + Properties properties = makeProperties(); + + try (EntityTransCloser et = new EntityTransCloser(em.getTransaction())) { + 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"); + iaequery.setParameter("rn", DbDaoTest.resourceName); + iaequery.setParameter("pu", DbDaoTest.A_SEQ_PU); + + @SuppressWarnings("rawtypes") + List iaeList = iaequery.getResultList(); + + // Assert that the IntegrityAuditEntity object was found + assertNotNull(iaeList); + + // flush to the DB + em.flush(); + et.commit(); + } + } + + /* + * Tests updating an IntegrityAuditEntity if it has already been registered + */ + @Test + public void testUpdateRegistration() throws Exception { + Properties properties = makeProperties(); + + dbDao = new DbDao(resourceName, A_SEQ_PU, properties); + + // 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"); + 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"); + iaequery.setParameter("rn", DbDaoTest.resourceName); + iaequery.setParameter("pu", DbDaoTest.A_SEQ_PU); + + @SuppressWarnings("rawtypes") + List iaeList = iaequery.getResultList(); + IntegrityAuditEntity iae = null; + if (!iaeList.isEmpty()) { + // ignores multiple results + iae = (IntegrityAuditEntity) iaeList.get(0); + + em.refresh(iae); + em.persist(iae); + + // flush to the DB + em.flush(); + + // commit transaction + et.commit(); + + // Assert that the site_name for the existing entry was updated + assertEquals("SiteB", iae.getSite()); + } + } + } + + /* Tests obtaining all Integrity Audit Entities from a table */ + @Test + public void testGetIntegrityAuditEntities() throws Exception { + Properties properties = makeProperties(); + + // Add some entries to the DB + dbDao = new DbDao(resourceName, A_SEQ_PU, properties); + new DbDao("pdp1", A_SEQ_PU, properties).destroy(); + properties.put(IntegrityAuditProperties.NODE_TYPE, "pdp_drools"); + new DbDao("pdp2", A_SEQ_PU, properties).destroy(); + + List entities; + // Obtain entries based on persistenceUnit and nodeType + entities = dbDao.getIntegrityAuditEntities(A_SEQ_PU, "pdp_xacml"); + assertEquals(2, entities.size()); + } + + /* Tests retrieving a DbDao instance's IntegrityAuditEntity */ + @Test + public void testGetMyIntegrityAuditEntity() throws Exception { + Properties properties = makeProperties(); + + dbDao = new DbDao(resourceName, A_SEQ_PU, properties); + IntegrityAuditEntity iae = dbDao.getMyIntegrityAuditEntity(); + // assertEquals("integrityAuditPU", iae.getPersistenceUnit()); + assertEquals(A_SEQ_PU, iae.getPersistenceUnit()); + } + + /* Tests obtaining an IntegrityAuditEntity by ID */ + @Test + public void testGetIntegrityAuditEntity() throws Exception { + Properties properties = makeProperties(); + + // Obtain an entry from the database based on ID + 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"); + iaequery.setParameter("rn", DbDaoTest.resourceName); + iaequery.setParameter("pu", DbDaoTest.A_SEQ_PU); + + @SuppressWarnings("rawtypes") + List iaeList = iaequery.getResultList(); + IntegrityAuditEntity iae = null; + 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); + + // Obtain ID for an IntegrityAuditEntity + PersistenceUnitUtil util = emf.getPersistenceUnitUtil(); + Object iaeId = util.getIdentifier(iae); + + // Obtain the same IntegrityAuditEntity based on ID + IntegrityAuditEntity iaeDuplicate = dbDao.getIntegrityAuditEntity((long) iaeId); + Object duplicateId = util.getIdentifier(iaeDuplicate); + + // Assert that the proper entry was retrieved based on ID + assertEquals((long) iaeId, (long) duplicateId); + } + } + + /* Tests setting an IntegrityAuditEntity as the designated node */ + @Test + public void testSetDesignated() throws Exception { + Properties properties = makeProperties(); + + try (EntityTransCloser et = new EntityTransCloser(em.getTransaction())) { + // Create an entry and set it's designated field to true + dbDao = new DbDao(resourceName, A_SEQ_PU, properties); + 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"); + iaequery.setParameter("rn", resourceName); + iaequery.setParameter("pu", A_SEQ_PU); + + @SuppressWarnings("rawtypes") + List iaeList = iaequery.getResultList(); + IntegrityAuditEntity iae = null; + + if (!iaeList.isEmpty()) { + // ignores multiple results + iae = (IntegrityAuditEntity) iaeList.get(0); + em.refresh(iae); + + // Check if the node is designated + boolean result = iae.isDesignated(); + + // Assert that it is designated + assertTrue(result); + } + + // flush to the DB + em.flush(); + + // close the transaction + et.commit(); + } + } + + /* Tests that the lastUpdated column in the database is updated properly */ + @Test + public void testSetLastUpdated() throws Exception { + Properties properties = makeProperties(); + + 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"); + iaequery.setParameter("rn", resourceName); + iaequery.setParameter("pu", A_SEQ_PU); + + @SuppressWarnings("rawtypes") + List iaeList = iaequery.getResultList(); + IntegrityAuditEntity iae = null; + + 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); + + // Obtain old update value and set new update value + final Date oldDate = iae.getLastUpdated(); + + // ensure dates are different by sleeping for a bit + testTime.sleep(1); + + iae.setSite("SiteB"); + iae.setLastUpdated(testTime.getDate()); + final Date newDate = iae.getLastUpdated(); + + em.persist(iae); + // flush to the DB + em.flush(); + // close the transaction + et.commit(); + + // Assert that the old and new update times are different + assertNotEquals(oldDate, newDate); + } + } + } + + /* Tests that all the entries from a class can be retrieved */ + @Test + public void testGetAllMyEntriesString() throws Exception { + Properties properties = makeProperties(); + + // create entries for the IntegrityAuditEntity table + dbDao = new DbDao(resourceName, A_SEQ_PU, properties); + new DbDao("pdp1", A_SEQ_PU, properties).destroy(); + 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"); + + // Assert there were 3 entries for that class + assertEquals(3, entries.size()); + } + + /* + * Tests retrieving all entities in a Persistence Unit using the class name and a hashset of IDs + */ + @Test + public void testGetAllMyEntriesStringHashSet() throws Exception { + Properties properties = makeProperties(); + + // create entries for the IntegrityAuditEntity table + dbDao = new DbDao(resourceName, A_SEQ_PU, properties); + new DbDao("pdp1", A_SEQ_PU, properties).destroy(); + new DbDao("pdp2", A_SEQ_PU, properties).destroy(); + + // 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")); + CriteriaQuery all = cq.select(rootEntry); + TypedQuery allQuery = em.createQuery(all); + List objectList = allQuery.getResultList(); + HashSet resultSet = new HashSet(); + PersistenceUnitUtil util = emf.getPersistenceUnitUtil(); + for (Object o : objectList) { + Object key = util.getIdentifier(o); + resultSet.add(key); + } + + // Obtain a hash with the persisted objects + Map entries = + dbDao.getAllMyEntries("org.onap.policy.common.ia.jpa.IntegrityAuditEntity", resultSet); + + // Assert there were 3 entries for that class + assertEquals(3, entries.size()); + } + + /* + * Tests retrieving all entities in a Persistence Unit using the persistence unit, properties, + * and class name + */ + @Test + public void testGetAllEntriesStringPropertiesString() throws Exception { + Properties properties = makeProperties(); + + // create entries for the IntegrityAuditEntity table + dbDao = new DbDao(resourceName, A_SEQ_PU, properties); + new DbDao("pdp1", A_SEQ_PU, properties).destroy(); + new DbDao("pdp2", A_SEQ_PU, properties).destroy(); + + // Obtain a hash with the persisted objects + Map entries = dbDao.getAllEntries("integrityAuditPU", properties, + "org.onap.policy.common.ia.jpa.IntegrityAuditEntity"); + + // Assert there were 3 entries for that class + assertEquals(3, entries.size()); + } + + /* + * Tests retrieving all entities in a Persistence Unit using the persistence unit, properties, + * class name, and a hashset of IDs + */ + @Test + public void testGetAllEntriesStringPropertiesStringHashSet() throws Exception { + Properties properties = makeProperties(); + + // create entries for the IntegrityAuditEntity table + dbDao = new DbDao(resourceName, A_SEQ_PU, properties); + new DbDao("pdp1", A_SEQ_PU, properties).destroy(); + new DbDao("pdp2", A_SEQ_PU, properties).destroy(); + + // 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")); + CriteriaQuery all = cq.select(rootEntry); + TypedQuery allQuery = em.createQuery(all); + List objectList = allQuery.getResultList(); + HashSet resultSet = new HashSet(); + PersistenceUnitUtil util = emf.getPersistenceUnitUtil(); + for (Object o : objectList) { + Object key = util.getIdentifier(o); + resultSet.add(key); + } + + // Obtain a hash with the persisted objects + Map entries = dbDao.getAllEntries("integrityAuditPU", properties, + "org.onap.policy.common.ia.jpa.IntegrityAuditEntity", resultSet); + + // Assert there were 3 entries for that class + assertEquals(3, entries.size()); + } + + /* + * Tests getting all the entries from a class based on persistenceUnit, properties, and + * className + */ + @Test + public void testGetAllEntries() throws Exception { + Properties properties = makeProperties(); + + // create entries for the IntegrityAuditEntity table + dbDao = new DbDao(resourceName, A_SEQ_PU, properties); + new DbDao("pdp1", A_SEQ_PU, properties).destroy(); + new DbDao("pdp2", A_SEQ_PU, properties).destroy(); + + // Obtain a hash with the persisted objects + Map entries = + dbDao.getAllEntries(A_SEQ_PU, properties, "org.onap.policy.common.ia.jpa.IntegrityAuditEntity"); + + // Assert there were 3 entries for that class + assertEquals(3, entries.size()); + } + + /** + * Tests obtaining all class names of persisted classes. + */ + public void testGetPersistenceClassNames() throws Exception { + Properties properties = makeProperties(); + + dbDao = new DbDao(resourceName, A_SEQ_PU, properties); + + // Retrieve persistence class names + Set result = dbDao.getPersistenceClassNames(); + assertEquals(1, result.size()); + } +} diff --git a/integrity-audit/src/test/java/org/onap/policy/common/ia/IntegrityAuditDesignationTest.java b/integrity-audit/src/test/java/org/onap/policy/common/ia/IntegrityAuditDesignationTest.java index bd3baf49..084c648d 100644 --- a/integrity-audit/src/test/java/org/onap/policy/common/ia/IntegrityAuditDesignationTest.java +++ b/integrity-audit/src/test/java/org/onap/policy/common/ia/IntegrityAuditDesignationTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Integrity Audit * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 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. @@ -261,7 +261,7 @@ public class IntegrityAuditDesignationTest extends IntegrityAuditTestBase { /* * Populate DB for pdp2, which will simulate it having registered but then having died. */ - new DbDAO("pdp2", A_SEQ_PU, makeProperties()).destroy(); + new DbDao("pdp2", A_SEQ_PU, makeProperties()).destroy(); /* * Sleep long enough to allow @@ -431,7 +431,7 @@ public class IntegrityAuditDesignationTest extends IntegrityAuditTestBase { /* * Populate DB for pdp3, which will simulate it having registered but then having died. */ - new DbDAO("pdp3", A_SEQ_PU, makeProperties()).destroy(); + new DbDao("pdp3", A_SEQ_PU, makeProperties()).destroy(); /* * Start audit for pdp4. @@ -488,12 +488,12 @@ public class IntegrityAuditDesignationTest extends IntegrityAuditTestBase { /* * Populate DB for "pdp1", which will simulate it having registered but then having died. */ - new DbDAO("pdp1", A_SEQ_PU, makeProperties()).destroy(); + new DbDao("pdp1", A_SEQ_PU, makeProperties()).destroy(); /* * Populate DB for pdp2, which will simulate it having registered but then having died. */ - new DbDAO("pdp2", A_SEQ_PU, makeProperties()).destroy(); + new DbDao("pdp2", A_SEQ_PU, makeProperties()).destroy(); /* * Start audit for pdp3. @@ -503,7 +503,7 @@ public class IntegrityAuditDesignationTest extends IntegrityAuditTestBase { /* * Populate DB for pdp4, which will simulate it having registered but then having died. */ - new DbDAO("pdp4", A_SEQ_PU, makeProperties()).destroy(); + new DbDao("pdp4", A_SEQ_PU, makeProperties()).destroy(); /* * Sleep long enough to allow -- cgit 1.2.3-korg