MysqlEventRepo { public List<MysqlEvent> getNewEventsForListener(Listener listener, Connection connection) throws DataAccessException { verifyListenerExists(listener, connection); try ( PreparedStatement stmt = connection.prepareStatement(GET_EVENTS_FOR_LISTENER_QUERY) ) { Date lastCheckDate = listener.getLastCheck(); listenerRepo.refreshLastCheck(listener, connection); stmt.setObject(1, listener.getLastEventId()); stmt.setObject(2, lastCheckDate); stmt.setObject(3, listener.getId()); ResultSet rs = stmt.executeQuery(); List<MysqlEvent> result = Lists.newArrayList(); long maxEventId = 0l; while (rs.next()) { maxEventId = Math.max(rs.getLong(EVENT_ID_COL), maxEventId); result.add(extractEvent(rs)); } if (maxEventId > listener.getLastEventId()) { listener.setLastEventId(maxEventId); listenerRepo.update(listener, connection); } return result; } catch (SQLException e) { throw new DataAccessException("Could not get new events for listener with id:" + listener.getId(), e); } } MysqlEventRepo(); List<MysqlEvent> getNewEventsForListener(Listener listener, Connection connection);  }
@Test(groups = INTEGRATION, expectedExceptions = DataAccessException.class) public void testCanNotGetEventsIfListenerDoesNotExist() { Listener listener = new Listener(2, "UnkownTable", 40, new Date(), 0); repo.getNewEventsForListener(listener, connection); }
@Test(groups = INTEGRATION) public void testGetsEventsForListenerThatWereCreatedAfterTheLastCheckOfTheListener() throws SQLException { List<MysqlEvent> newEvents = repo.getNewEventsForListener(listener, connection); assertTrue(newEvents.isEmpty()); insertEvent(new MysqlEvent(1, TEST_TABLE_NAME, INSERT_TYPE, "{}", "{}", new Date()), connection); newEvents = repo.getNewEventsForListener(listener, connection); assertEquals(newEvents.size(), 1); }
@Test(groups = INTEGRATION) public void testDoesNotReturnEventsThatWereCreatedBeforeTheListenerLastCheck() throws SQLException, InterruptedException { insertEvent(new MysqlEvent(1, TEST_TABLE_NAME, INSERT_TYPE, "{}", "{}", new Date()), connection); Thread.sleep(1000L); ListenerRepo listenerRepo = new ListenerRepo(); listener = listenerRepo.insert(new Listener(TEST_TABLE_NAME), connection); List<MysqlEvent> newEventsForListener = repo.getNewEventsForListener(listener, connection); assertTrue(newEventsForListener.isEmpty()); }
@Test(groups = INTEGRATION) public void testNeverReturnsAPreviousReturnedEventTwice() throws SQLException { for (int i = 0; i < 100; i++) { final MysqlEvent event = new MysqlEvent(i + 1, TEST_TABLE_NAME, INSERT_TYPE, "{}", "{}", new Date()); insertEvent(event, connection); List<MysqlEvent> events = repo.getNewEventsForListener(listener, connection); assertEquals(events.size(), 1); assertEquals(events.get(0).getEventId(), event.getEventId()); } }
@Test(groups = INTEGRATION) public void testUpdatesTheListenerLastCheckDate() throws SQLException, InterruptedException { Date initialLastCheck = listener.getLastCheck(); Thread.sleep(1000L); repo.getNewEventsForListener(listener, connection); assertTrue(initialLastCheck.before(listener.getLastCheck())); }
@Test(groups = INTEGRATION) public void testReturnsEventsOrderedByDateAndThenEventId() throws SQLException { MysqlEvent event1 = new MysqlEvent(1, TEST_TABLE_NAME, INSERT_TYPE, "{}", "{}", DateUtils.addDays(TODAY, 2)); MysqlEvent event2 = new MysqlEvent(2, TEST_TABLE_NAME, INSERT_TYPE, "{}", "{}", DateUtils.addDays(TODAY, 2)); MysqlEvent event3 = new MysqlEvent(3, TEST_TABLE_NAME, INSERT_TYPE, "{}", "{}", DateUtils.addDays(TODAY, 1)); MysqlEvent event4 = new MysqlEvent(4, TEST_TABLE_NAME, INSERT_TYPE, "{}", "{}", DateUtils.addDays(TODAY, 3)); insertEventWithDate(event2, connection); insertEventWithDate(event1, connection); insertEventWithDate(event3, connection); insertEventWithDate(event4, connection); List<MysqlEvent> mysqlEvents = repo.getNewEventsForListener(listener, connection); assertEquals(mysqlEvents.size(), 4); assertEquals(mysqlEvents.get(0).getEventId(), 3); assertEquals(mysqlEvents.get(1).getEventId(), 1); assertEquals(mysqlEvents.get(2).getEventId(), 2); assertEquals(mysqlEvents.get(3).getEventId(), 4); }