ChecksumHistoryDAOImpl extends AbstractHibernateDAO<ChecksumHistory> implements ChecksumHistoryDAO { @Override public int deleteByDateAndCode(Context context, Date retentionDate, ChecksumResultCode resultCode) throws SQLException { String hql = "delete from ChecksumHistory where processEndDate < :processEndDate AND checksumResult.resultCode=:resultCode"; Query query = createQuery(context, hql); query.setTimestamp("processEndDate", retentionDate); query.setParameter("resultCode", resultCode); return query.executeUpdate(); } protected  ChecksumHistoryDAOImpl(); @Override int deleteByDateAndCode(Context context, Date retentionDate, ChecksumResultCode resultCode); @Override void deleteByBitstream(Context context, Bitstream bitstream);  }
@Test public void testDeleteByDateAndCode() throws Exception { System.out.println("deleteByDateAndCode"); GregorianCalendar cal = new GregorianCalendar(); Date retentionDate = cal.getTime(); ChecksumResultCode resultCode = ChecksumResultCode.CHECKSUM_MATCH; HibernateDBConnection dbc = (HibernateDBConnection) CoreHelpers.getDBConnection(context); Query qry = dbc.getSession().createSQLQuery( "INSERT INTO checksum_history" + "(check_id, process_end_date, result, bitstream_id)" + " VALUES (:id, :date, :result, :bitstream)"); int checkId = 0; BitstreamService bss = ContentServiceFactory.getInstance().getBitstreamService(); InputStream is = new ByteArrayInputStream(new byte[0]); Bitstream bs = bss.create(context, is); context.turnOffAuthorisationSystem(); bss.update(context, bs); context.restoreAuthSystemState(); cal.add(Calendar.DATE, -1); Date matchDate = cal.getTime(); checkId++; qry.setInteger("id", checkId); qry.setDate("date", matchDate); qry.setString("result", ChecksumResultCode.CHECKSUM_MATCH.name()); qry.setParameter("bitstream", bs.getID()); qry.executeUpdate(); cal.add(Calendar.DATE, -1); Date noMatchDate = cal.getTime(); checkId++; qry.setInteger("id", checkId); qry.setDate("date", noMatchDate); qry.setString("result", ChecksumResultCode.CHECKSUM_NO_MATCH.name()); qry.setParameter("bitstream", bs.getID()); qry.executeUpdate(); cal.add(Calendar.DATE, +3); Date futureDate = cal.getTime(); checkId++; qry.setInteger("id", checkId); qry.setDate("date", new java.sql.Date(futureDate.getTime())); qry.setString("result", ChecksumResultCode.CHECKSUM_MATCH.name()); qry.setParameter("bitstream", bs.getID()); qry.executeUpdate(); ChecksumHistoryDAOImpl instance = new ChecksumHistoryDAOImpl(); int expResult = 1; int result = instance.deleteByDateAndCode(context, retentionDate, resultCode); assertEquals(expResult, result); qry = dbc.getSession().createQuery( "SELECT COUNT(*) FROM ChecksumHistory WHERE process_end_date = :date"); long count; qry.setDate("date", matchDate); count = (Long) qry.uniqueResult(); assertEquals("Should find no row at matchDate", count, 0); qry.setDate("date", noMatchDate); count = (Long) qry.uniqueResult(); assertEquals("Should find one row at noMatchDate", count, 1); qry.setDate("date", futureDate); count = (Long) qry.uniqueResult(); assertEquals("Should find one row at futureDate", count, 1); }