JsonSlackDAO extends AbstractJSONChatApiDAO { @Override public List<Message> getMessages(DateTime start, DateTime end, Room room) { String pathName; if (room.isPrivateRoom()) { pathName = "groups.history"; } else { pathName = "channels.history"; } WebResource historyResource = resource.path(pathName); List<Message> result = Lists.newArrayList(); boolean hasNext = true; String startMillisStr = formatDateTime(start); String endMillisStr = formatDateTime(end); while (hasNext) { historyResource = historyResource.queryParam("channel", room.getRoomId()) .queryParam("latest", endMillisStr) .queryParam("oldest", startMillisStr) .queryParam("inclusive", "0") .queryParam("count", "1000"); String jsonStr = getJsonResultWithRetries(historyResource, apiRetries); try { HistoryResult history = objMapper.readValue(jsonStr, HistoryResult.class); if (history.getMessages().isEmpty()) { break; } result.addAll(history.getMessages()); Comparator<Message> comp = (msg1, msg2) -> msg1.getDate().compareTo(msg2.getDate()); DateTime earliestDate = history.getMessages().stream().min(comp).get().getDate(); hasNext = history.isHas_more(); endMillisStr = formatDateTime(earliestDate); } catch (IOException e) { LOG.error("Can't deserialize history", e); return ImmutableList.of(); } } return result; } JsonSlackDAO(ChatAlyticsConfig config, Client client); @Override Map<String, Room> getRooms(); @Override Map<String, User> getUsers(); @Override Map<String, User> getUsersForRoom(Room room); URI getRealtimeWebSocketURI(); @Override List<Message> getMessages(DateTime start, DateTime end, Room room); @Override Map<String, String> getEmojis();  }
@Test public void testGetMessages() throws Exception { WebResource mockHistoryResrc = mock(WebResource.class); when(mockResource.path("channels.history")).thenReturn(mockHistoryResrc); when(mockHistoryResrc.queryParam(anyString(), anyString())).thenReturn(mockHistoryResrc); URI historyURI = Resources.getResource("slack_api_responses/channels.history.txt").toURI(); Path historyPath = Paths.get(historyURI); String historyResponseStr = new String(Files.readAllBytes(historyPath)); doReturn(historyResponseStr).when(underTest).getJsonResultWithRetries(mockHistoryResrc, apiRetries); Room mockRoom = mock(Room.class); when(mockRoom.getRoomId()).thenReturn("C0SDFG423"); DateTime now = DateTime.now(); List<Message> messages = underTest.getMessages(now.minusDays(1), now, mockRoom); assertEquals(7, messages.size()); for (Message message : messages) { assertNotNull(message); } }
@Test public void testGetMessages_privateChannel() throws Exception { WebResource mockHistoryResrc = mock(WebResource.class); when(mockResource.path("groups.history")).thenReturn(mockHistoryResrc); when(mockHistoryResrc.queryParam(anyString(), anyString())).thenReturn(mockHistoryResrc); URI historyURI = Resources.getResource("slack_api_responses/groups.history.txt").toURI(); Path historyPath = Paths.get(historyURI); String historyResponseStr = new String(Files.readAllBytes(historyPath)); doReturn(historyResponseStr).when(underTest).getJsonResultWithRetries(mockHistoryResrc, apiRetries); Room mockRoom = mock(Room.class); when(mockRoom.getRoomId()).thenReturn("G0SDFG423"); when(mockRoom.isPrivateRoom()).thenReturn(true); DateTime now = DateTime.now(); List<Message> messages = underTest.getMessages(now.minusDays(1), now, mockRoom); assertEquals(7, messages.size()); for (Message message : messages) { assertNotNull(message); } }