@Test public void testFindLastHeartbeat() { Date expectedDate = heartbeatDAO.updateLastHeartbeat(IP); Date actualDate = heartbeatDAO.findLastHeartbeat(IP); Assert.assertEquals(expectedDate, actualDate); }
@Override public Date findLastHeartbeat(String ip) { DBCollection collection = this.mongoClient.getHeartbeatCollection(ip.replace('.', '_')); DBObject fields = BasicDBObjectBuilder.start().add(TICK, Integer.valueOf(1)).get(); DBObject orderBy = BasicDBObjectBuilder.start().add(TICK, Integer.valueOf(-1)).get(); DBCursor cursor = collection.find(null, fields).sort(orderBy).limit(1); try { if (cursor.hasNext()) { DBObject result = cursor.next(); return (Date) result.get(TICK); } } finally { cursor.close(); } return null; }
HeartbeatDAOImpl implements HeartbeatDAO { @Override public Date findLastHeartbeat(String ip) { DBCollection collection = this.mongoClient.getHeartbeatCollection(ip.replace('.', '_')); DBObject fields = BasicDBObjectBuilder.start().add(TICK, Integer.valueOf(1)).get(); DBObject orderBy = BasicDBObjectBuilder.start().add(TICK, Integer.valueOf(-1)).get(); DBCursor cursor = collection.find(null, fields).sort(orderBy).limit(1); try { if (cursor.hasNext()) { DBObject result = cursor.next(); return (Date) result.get(TICK); } } finally { cursor.close(); } return null; } }
HeartbeatDAOImpl implements HeartbeatDAO { @Override public Date findLastHeartbeat(String ip) { DBCollection collection = this.mongoClient.getHeartbeatCollection(ip.replace('.', '_')); DBObject fields = BasicDBObjectBuilder.start().add(TICK, Integer.valueOf(1)).get(); DBObject orderBy = BasicDBObjectBuilder.start().add(TICK, Integer.valueOf(-1)).get(); DBCursor cursor = collection.find(null, fields).sort(orderBy).limit(1); try { if (cursor.hasNext()) { DBObject result = cursor.next(); return (Date) result.get(TICK); } } finally { cursor.close(); } return null; }  }
HeartbeatDAOImpl implements HeartbeatDAO { @Override public Date findLastHeartbeat(String ip) { DBCollection collection = this.mongoClient.getHeartbeatCollection(ip.replace('.', '_')); DBObject fields = BasicDBObjectBuilder.start().add(TICK, Integer.valueOf(1)).get(); DBObject orderBy = BasicDBObjectBuilder.start().add(TICK, Integer.valueOf(-1)).get(); DBCursor cursor = collection.find(null, fields).sort(orderBy).limit(1); try { if (cursor.hasNext()) { DBObject result = cursor.next(); return (Date) result.get(TICK); } } finally { cursor.close(); } return null; }  void setMongoClient(MongoClient mongoClient); @Override Date updateLastHeartbeat(String ip); @Override Date findLastHeartbeat(String ip); }
HeartbeatDAOImpl implements HeartbeatDAO { @Override public Date findLastHeartbeat(String ip) { DBCollection collection = this.mongoClient.getHeartbeatCollection(ip.replace('.', '_')); DBObject fields = BasicDBObjectBuilder.start().add(TICK, Integer.valueOf(1)).get(); DBObject orderBy = BasicDBObjectBuilder.start().add(TICK, Integer.valueOf(-1)).get(); DBCursor cursor = collection.find(null, fields).sort(orderBy).limit(1); try { if (cursor.hasNext()) { DBObject result = cursor.next(); return (Date) result.get(TICK); } } finally { cursor.close(); } return null; }  void setMongoClient(MongoClient mongoClient); @Override Date updateLastHeartbeat(String ip); @Override Date findLastHeartbeat(String ip); static final String TICK; }