JsonSlackDAO extends AbstractJSONChatApiDAO { @Override public Map<String, Room> getRooms() { WebResource roomResource = resource.path("channels.list"); roomResource.queryParam("exclude_archived", includeArchivedRooms ? "0" : "1"); String jsonStr = getJsonResultWithRetries(roomResource, apiRetries); Collection<Room> roomCol = deserializeJsonStr(jsonStr, "channels", Room.class, objMapper); if (includePrivateRooms) { roomResource = resource.path("groups.list"); roomResource.queryParam("exclude_archived", includeArchivedRooms ? "0" : "1"); jsonStr = getJsonResultWithRetries(roomResource, apiRetries); Collection<Room> privateRoomCol = deserializeJsonStr(jsonStr, "groups", Room.class, objMapper); roomCol.addAll(privateRoomCol); } Map<String, Room> result = Maps.newHashMapWithExpectedSize(roomCol.size()); for (Room room : roomCol) { result.put(room.getRoomId(), room); } 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 testGetRooms() throws Exception { WebResource mockChanResource = mock(WebResource.class); when(mockResource.path("channels.list")).thenReturn(mockChanResource); URI channelListURI = Resources.getResource("slack_api_responses/channels.list.txt").toURI(); Path channelsPath = Paths.get(channelListURI); String channelsResponseStr = new String(Files.readAllBytes(channelsPath)); doReturn(channelsResponseStr).when(underTest).getJsonResultWithRetries(mockChanResource, apiRetries); Map<String, Room> rooms = underTest.getRooms(); assertEquals(1, rooms.size()); for (Room room : rooms.values()) { assertNotNull(room); } }
@Test public void testGetRooms_withArchivedRooms() throws Exception { chatConfig.includeArchivedRooms = true; underTest = spy(new JsonSlackDAO(config, mockClient)); WebResource mockChanResource = mock(WebResource.class); when(mockResource.path("channels.list")).thenReturn(mockChanResource); URI channelListURI = Resources.getResource("slack_api_responses/channels.list.archived.txt").toURI(); Path channelsPath = Paths.get(channelListURI); String channelsResponseStr = new String(Files.readAllBytes(channelsPath)); doReturn(channelsResponseStr).when(underTest).getJsonResultWithRetries(mockChanResource, apiRetries); Map<String, Room> rooms = underTest.getRooms(); assertEquals(2, rooms.size()); for (Room room : rooms.values()) { assertNotNull(room); } }
@Test public void testGetRooms_withPrivateRooms() throws Exception { chatConfig.includePrivateRooms = true; underTest = spy(new JsonSlackDAO(config, mockClient)); WebResource mockChanResource = mock(WebResource.class); when(mockResource.path("channels.list")).thenReturn(mockChanResource); URI channelListURI = Resources.getResource("slack_api_responses/channels.list.txt").toURI(); Path channelsPath = Paths.get(channelListURI); String channelsResponseStr = new String(Files.readAllBytes(channelsPath)); doReturn(channelsResponseStr).when(underTest).getJsonResultWithRetries(mockChanResource, apiRetries); WebResource mockGroupsResource = mock(WebResource.class); when(mockResource.path("groups.list")).thenReturn(mockGroupsResource); URI groupsListURI = Resources.getResource("slack_api_responses/groups.list.txt").toURI(); Path groupsPath = Paths.get(groupsListURI); String groupsResponseStr = new String(Files.readAllBytes(groupsPath)); doReturn(groupsResponseStr).when(underTest).getJsonResultWithRetries(mockGroupsResource, apiRetries); Map<String, Room> rooms = underTest.getRooms(); assertEquals(2, rooms.size()); for (Room room : rooms.values()) { assertNotNull(room); } }
@Test public void testGetRooms_withPrivateAndArhivedRooms() throws Exception { chatConfig.includePrivateRooms = true; chatConfig.includeArchivedRooms = true; underTest = spy(new JsonSlackDAO(config, mockClient)); WebResource mockChanResource = mock(WebResource.class); when(mockResource.path("channels.list")).thenReturn(mockChanResource); URI channelListURI = Resources.getResource("slack_api_responses/channels.list.archived.txt").toURI(); Path channelsPath = Paths.get(channelListURI); String channelsResponseStr = new String(Files.readAllBytes(channelsPath)); doReturn(channelsResponseStr).when(underTest).getJsonResultWithRetries(mockChanResource, apiRetries); WebResource mockGroupsResource = mock(WebResource.class); when(mockResource.path("groups.list")).thenReturn(mockGroupsResource); URI groupsListURI = Resources.getResource("slack_api_responses/groups.list.archived.txt").toURI(); Path groupsPath = Paths.get(groupsListURI); String groupsResponseStr = new String(Files.readAllBytes(groupsPath)); doReturn(groupsResponseStr).when(underTest).getJsonResultWithRetries(mockGroupsResource, apiRetries); Map<String, Room> rooms = underTest.getRooms(); assertEquals(4, rooms.size()); for (Room room : rooms.values()) { assertNotNull(room); } }