@Override protected <T extends OverdueCheckNotificationKey> boolean cleanupFutureNotificationsFormTransaction(final EntitySqlDaoWrapperFactory<EntitySqlDao> entitySqlDaoWrapperFactory, final Collection<NotificationEventWithMetadata<T>> futureNotifications, final DateTime futureNotificationTime, final NotificationQueue overdueQueue) { boolean shouldInsertNewNotification = true; if (futureNotifications.size() > 0) { // Results are ordered by effective date asc final DateTime earliestExistingNotificationDate = futureNotifications.iterator().next().getEffectiveDate(); final int minIndexToDeleteFrom; if (earliestExistingNotificationDate.isBefore(futureNotificationTime)) { // We don't have to insert a new one. For sanity, delete any other future notification minIndexToDeleteFrom = 1; shouldInsertNewNotification = false; } else { // We win - we are before any other already recorded. Delete all others. minIndexToDeleteFrom = 0; } int index = 0; final Iterator<NotificationEventWithMetadata<T>> it = futureNotifications.iterator(); while (it.hasNext()) { final NotificationEventWithMetadata<T> cur = it.next(); if (minIndexToDeleteFrom <= index) { overdueQueue.removeNotificationFromTransaction(entitySqlDaoWrapperFactory.getSqlDao(), cur.getRecordId()); } index++; } } return shouldInsertNewNotification; }
private void insertOverdueCheckAndVerifyQueueContent(final Account account, final int nbDaysInFuture, final int expectedNbDaysInFuture) throws IOException { final DateTime futureNotificationTime = testReferenceTime.plusDays(nbDaysInFuture); final OverdueCheckNotificationKey notificationKey = new OverdueCheckNotificationKey(account.getId()); checkPoster.insertOverdueNotification(account.getId(), futureNotificationTime, OverdueCheckNotifier.OVERDUE_CHECK_NOTIFIER_QUEUE, notificationKey, internalCallContext); final Collection<NotificationEventWithMetadata<OverdueCheckNotificationKey>> notificationsForKey = getNotificationsForOverdueable(account); Assert.assertEquals(notificationsForKey.size(), 1); final NotificationEventWithMetadata nm = notificationsForKey.iterator().next(); Assert.assertEquals(nm.getEvent(), notificationKey); Assert.assertEquals(nm.getEffectiveDate(), testReferenceTime.plusDays(expectedNbDaysInFuture)); }