JsonSlackDAO extends AbstractJSONChatApiDAO { @Override public Map<String, User> getUsersForRoom(Room room) { String pathStr; String listElem; if (room.isPrivateRoom()) { pathStr = "groups.info"; listElem = "group"; } else { pathStr = "channels.info"; listElem = "channel"; } WebResource roomResource = resource.path(pathStr); roomResource = roomResource.queryParam("channel", room.getRoomId()); String jsonStr = getJsonResultWithRetries(roomResource, apiRetries); Collection<String> userIdCol = deserializeJsonStr(jsonStr, Lists.newArrayList(listElem, "members"), String.class, objMapper); Map<String, User> result = Maps.newHashMapWithExpectedSize(userIdCol.size()); for (String userId : userIdCol) { WebResource userResource = resource.path("users.info"); userResource = userResource.queryParam("user", userId); jsonStr = getJsonResultWithRetries(userResource, apiRetries); try { JsonNode jsonNode = objMapper.readTree(jsonStr); jsonNode = jsonNode.get("user"); User user = objMapper.readValue(jsonNode.toString(), User.class); result.put(user.getUserId(), user); } catch (IOException e) { throw new RuntimeException("Can't deserialize user with ID:" + userId, e); } } 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 testGetUsersForRoom() throws Exception { WebResource mockChanInfoResrc = mock(WebResource.class); when(mockResource.path("channels.info")).thenReturn(mockChanInfoResrc); when(mockChanInfoResrc.queryParam(anyString(), anyString())).thenReturn(mockChanInfoResrc); URI chanInfoURI = Resources.getResource("slack_api_responses/channels.info.txt").toURI(); Path channelsInfoPath = Paths.get(chanInfoURI); String chanInfoResponseStr = new String(Files.readAllBytes(channelsInfoPath)); doReturn(chanInfoResponseStr).when(underTest).getJsonResultWithRetries(mockChanInfoResrc, apiRetries); WebResource mockUserInfoResource = mock(WebResource.class); when(mockResource.path("users.info")).thenReturn(mockUserInfoResource); WebResource user1InfoResource = mock(WebResource.class); when(mockUserInfoResource.queryParam("user", "U023BECGF")).thenReturn(user1InfoResource); URI userInfoURI = Resources.getResource("slack_api_responses/users.info.1.txt").toURI(); Path usersInfoPath = Paths.get(userInfoURI); String userInfoResponseStr = new String(Files.readAllBytes(usersInfoPath)); doReturn(userInfoResponseStr).when(underTest).getJsonResultWithRetries(user1InfoResource, apiRetries); WebResource user2InfoResource = mock(WebResource.class); when(mockUserInfoResource.queryParam("user", "U023TY454")).thenReturn(user2InfoResource); userInfoURI = Resources.getResource("slack_api_responses/users.info.2.txt").toURI(); usersInfoPath = Paths.get(userInfoURI); userInfoResponseStr = new String(Files.readAllBytes(usersInfoPath)); doReturn(userInfoResponseStr).when(underTest).getJsonResultWithRetries(user2InfoResource, apiRetries); Room mockRoom = mock(Room.class); when(mockRoom.getRoomId()).thenReturn("C0SDFG423"); Map<String, User> usersForRoom = underTest.getUsersForRoom(mockRoom); assertEquals(2, usersForRoom.size()); for (User user : usersForRoom.values()) { assertNotNull(user); } }
@Test public void testGetUsersForRoom_privateRoom() throws Exception { WebResource mockChanInfoResrc = mock(WebResource.class); when(mockResource.path("groups.info")).thenReturn(mockChanInfoResrc); when(mockChanInfoResrc.queryParam(anyString(), anyString())).thenReturn(mockChanInfoResrc); URI chanInfoURI = Resources.getResource("slack_api_responses/groups.info.txt").toURI(); Path channelsInfoPath = Paths.get(chanInfoURI); String chanInfoResponseStr = new String(Files.readAllBytes(channelsInfoPath)); doReturn(chanInfoResponseStr).when(underTest).getJsonResultWithRetries(mockChanInfoResrc, apiRetries); WebResource mockUserInfoResource = mock(WebResource.class); when(mockResource.path("users.info")).thenReturn(mockUserInfoResource); WebResource user1InfoResource = mock(WebResource.class); when(mockUserInfoResource.queryParam("user", "U023BECGF")).thenReturn(user1InfoResource); URI userInfoURI = Resources.getResource("slack_api_responses/users.info.1.txt").toURI(); Path usersInfoPath = Paths.get(userInfoURI); String userInfoResponseStr = new String(Files.readAllBytes(usersInfoPath)); doReturn(userInfoResponseStr).when(underTest).getJsonResultWithRetries(user1InfoResource, apiRetries); WebResource user2InfoResource = mock(WebResource.class); when(mockUserInfoResource.queryParam("user", "U023TY454")).thenReturn(user2InfoResource); userInfoURI = Resources.getResource("slack_api_responses/users.info.2.txt").toURI(); usersInfoPath = Paths.get(userInfoURI); userInfoResponseStr = new String(Files.readAllBytes(usersInfoPath)); doReturn(userInfoResponseStr).when(underTest).getJsonResultWithRetries(user2InfoResource, apiRetries); Room mockRoom = mock(Room.class); when(mockRoom.getRoomId()).thenReturn("G024BE91L"); when(mockRoom.isPrivateRoom()).thenReturn(true); Map<String, User> usersForRoom = underTest.getUsersForRoom(mockRoom); assertEquals(2, usersForRoom.size()); for (User user : usersForRoom.values()) { assertNotNull(user); } }