@Override public void handleReadyNotification(final NotificationEvent notificationKey, final DateTime eventDate, final UUID userToken, final Long accountRecordId, final Long tenantRecordId) { myself.handleReadyNotification(notificationKey, eventDate, userToken, accountRecordId, tenantRecordId); } };
@Override public int hashCode() { int result = getUuidKey() != null ? getUuidKey().hashCode() : 0; result = 31 * result + (action != null ? action.hashCode() : 0); return result; } }
@Override public boolean equals(final Object o) { if (this == o) { return true; } if (!(o instanceof OverdueAsyncBusNotificationKey)) { return false; } final OverdueAsyncBusNotificationKey that = (OverdueAsyncBusNotificationKey) o; if (action != that.action) { return false; } if (getUuidKey() != null ? !getUuidKey().equals(that.getUuidKey()) : that.getUuidKey() != null) { return false; } return true; }
@Override @BeforeMethod(groups = "slow") public void beforeMethod() throws Exception { //super.beforeMethod(); // We override the parent method on purpose, because we want to register a different OverdueCheckNotifier final Account account = Mockito.mock(Account.class); Mockito.when(accountApi.getAccountById(Mockito.<UUID>any(), Mockito.<InternalTenantContext>any())).thenReturn(account); mockDispatcher = new OverdueDispatcherMock(internalCallContextFactory); notifierForMock = new OverdueCheckNotifier(notificationQueueService, overdueProperties, internalCallContextFactory, mockDispatcher); notifierForMock.initialize(); notifierForMock.start(); }
@Test(groups = "slow") public void test() throws Exception { final UUID accountId = new UUID(0L, 1L); final Account account = Mockito.mock(Account.class); Mockito.when(account.getId()).thenReturn(accountId); final DateTime now = clock.getUTCNow(); final DateTime readyTime = now.plusMillis(2000); final OverdueCheckNotificationKey notificationKey = new OverdueCheckNotificationKey(accountId); checkPoster.insertOverdueNotification(accountId, readyTime, OverdueCheckNotifier.OVERDUE_CHECK_NOTIFIER_QUEUE, notificationKey, internalCallContext); // Move time in the future after the notification effectiveDate clock.setDeltaFromReality(3000); await().atMost(5, SECONDS).until(new Callable<Boolean>() { @Override public Boolean call() throws Exception { return mockDispatcher.getEventCount() == 1; } }); Assert.assertEquals(mockDispatcher.getEventCount(), 1); Assert.assertEquals(mockDispatcher.getLatestAccountId(), accountId); } }
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)); }
protected void createFutureNotification(final Account account, final DateTime timeOfNextCheck, final InternalCallContext context) { final OverdueCheckNotificationKey notificationKey = new OverdueCheckNotificationKey(account.getId()); checkPoster.insertOverdueNotification(account.getId(), timeOfNextCheck, OverdueCheckNotifier.OVERDUE_CHECK_NOTIFIER_QUEUE, notificationKey, context); }
@Override public void handleReadyNotification(final NotificationEvent notificationKey, final DateTime eventDate, final UUID userToken, final Long accountRecordId, final Long tenantRecordId) { try { if (!(notificationKey instanceof OverdueCheckNotificationKey)) { log.error("Overdue service received Unexpected notificationKey {}", notificationKey.getClass().getName()); return; } final OverdueCheckNotificationKey key = (OverdueCheckNotificationKey) notificationKey; dispatcher.processOverdueForAccount(key.getUuidKey(), createCallContext(userToken, accountRecordId, tenantRecordId)); } catch (IllegalArgumentException e) { log.error("The key returned from the NextBillingNotificationQueue is not a valid UUID", e); } } }
@Override public void handleReadyNotification(final NotificationEvent notificationKey, final DateTime eventDate, final UUID userToken, final Long accountRecordId, final Long tenantRecordId) { try { if (!(notificationKey instanceof OverdueAsyncBusNotificationKey)) { log.error("Overdue service received Unexpected notificationKey {}", notificationKey.getClass().getName()); return; } final OverdueAsyncBusNotificationKey key = (OverdueAsyncBusNotificationKey) notificationKey; switch (key.getAction()) { case CLEAR: dispatcher.clearOverdueForAccount(key.getUuidKey(), createCallContext(userToken, accountRecordId, tenantRecordId)); break; case REFRESH: dispatcher.processOverdueForAccount(key.getUuidKey(), createCallContext(userToken, accountRecordId, tenantRecordId)); break; default: throw new RuntimeException("Unexpected action " + key.getAction() + " for account " + key.getUuidKey()); } } catch (IllegalArgumentException e) { log.error("The key returned from the queue " + OVERDUE_ASYNC_BUS_NOTIFIER_QUEUE + " does not contain a valid UUID", e); } }
private void insertBusEventIntoNotificationQueue(final UUID accountId, final BusEvent event, final OverdueAsyncBusNotificationAction action) { final OverdueAsyncBusNotificationKey notificationKey = new OverdueAsyncBusNotificationKey(accountId, action); asyncPoster.insertOverdueNotification(accountId, clock.getUTCNow(), OverdueAsyncBusNotifier.OVERDUE_ASYNC_BUS_NOTIFIER_QUEUE, notificationKey, createCallContext(event.getUserToken(), event.getSearchKey1(), event.getSearchKey2())); }
@LifecycleHandlerType(LifecycleHandlerType.LifecycleLevel.INIT_SERVICE) public void initialize() { registerForBus(); checkNotifier.initialize(); asyncNotifier.initialize(); }
@Test(groups = "fast") public void testOverdueNotificationKeyJsonWithNoKey() throws Exception { final String uuidString = "bab0fca4-c628-4997-8980-14d6c3a98c48"; final String json = "{\"uuidKey\":\"" + uuidString + "\"}"; final Class<?> claz = Class.forName(OverdueCheckNotificationKey.class.getName()); final OverdueCheckNotificationKey obj = (OverdueCheckNotificationKey) mapper.readValue(json, claz); assertEquals(obj.getUuidKey().toString(), uuidString); } }
@Override public Boolean call() throws Exception { return mockDispatcher.getEventCount() == 1; } });
protected void clearFutureNotification(final Account account, final InternalCallContext context) { // Need to clear the override table here too (when we add it) checkPoster.clearOverdueCheckNotifications(account.getId(), OverdueCheckNotifier.OVERDUE_CHECK_NOTIFIER_QUEUE, OverdueCheckNotificationKey.class, context); }
@Override public void initialize() { final OverdueNotifier myself = this; final NotificationQueueHandler notificationQueueHandler = new NotificationQueueHandler() { @Override public void handleReadyNotification(final NotificationEvent notificationKey, final DateTime eventDate, final UUID userToken, final Long accountRecordId, final Long tenantRecordId) { myself.handleReadyNotification(notificationKey, eventDate, userToken, accountRecordId, tenantRecordId); } }; try { overdueQueue = notificationQueueService.createNotificationQueue(DefaultOverdueService.OVERDUE_SERVICE_NAME, getQueueName(), notificationQueueHandler); } catch (NotificationQueueAlreadyExists e) { throw new RuntimeException(e); } }
@Override public Collection<NotificationEventWithMetadata<OverdueCheckNotificationKey>> inTransaction(final EntitySqlDaoWrapperFactory<EntitySqlDao> entitySqlDaoWrapperFactory) throws Exception { return ((OverdueCheckPoster)checkPoster).getFutureNotificationsForAccountInTransaction(entitySqlDaoWrapperFactory, overdueQueue, account.getId(), OverdueCheckNotificationKey.class, internalCallContext); } });
@Override public Void inTransaction(final EntitySqlDaoWrapperFactory<EntitySqlDao> entitySqlDaoWrapperFactory) throws Exception { final Collection<NotificationEventWithMetadata<T>> futureNotifications = getFutureNotificationsForAccountInTransaction(entitySqlDaoWrapperFactory, checkOverdueQueue, accountId, clazz, context); for (final NotificationEventWithMetadata<T> notification : futureNotifications) { checkOverdueQueue.removeNotificationFromTransaction(entitySqlDaoWrapperFactory.getSqlDao(), notification.getRecordId()); } return null; } });
@Override @AfterMethod(groups = "slow") public void afterMethod() throws Exception { notifierForMock.stop(); super.afterMethod(); }