RestImpl { void getRoomMembers(String roomId, BaseRoom.RoomType roomType, int offset, BaseUser.SortBy sortBy, Sort sort, final PaginatedCallback<User> callback) { checkNotNull(roomId, "roomId == null"); checkNotNull(roomType, "roomType == null"); checkNotNull(sortBy, "sortBy == null"); checkNotNull(sort, "sort == null"); checkNotNull(callback, "callback == null"); HttpUrl httpUrl = requestUrl(baseUrl, getRestApiMethodNameByRoomType(roomType, "members")) .addQueryParameter("roomId", roomId) .addQueryParameter("offset", String.valueOf(offset)) .build(); Request request = requestBuilder(httpUrl) .get() .build(); client.newCall(request).enqueue(new okhttp3.Callback() { @Override public void onFailure(Call call, IOException e) { callback.onError(new RocketChatNetworkErrorException("Network error", e)); } @Override public void onResponse(Call call, Response response) throws IOException { if (!response.isSuccessful()) { logger.info("Response = " + response.body().string()); processCallbackError(response, ERROR_HANDLER(callback)); return; } try { JSONObject json = new JSONObject(response.body().string()); logger.info("Response = " + json.toString()); Type type = Types.newParameterizedType(List.class, User.class); JsonAdapter<List<User>> adapter = moshi.adapter(type); List<User> userList = adapter.fromJson(json.getJSONArray("members").toString()); callback.onSuccess(userList, json.optInt("total")); } catch (JSONException e) { callback.onError(new RocketChatInvalidResponseException(e.getMessage(), e)); } } }); } RestImpl(OkHttpClient client, Moshi moshi, HttpUrl baseUrl, TokenProvider tokenProvider, Logger logger);   }
@Test(expected = NullPointerException.class) public void testGetRoomMembersShouldFailWithNullRoomId() { rest.getRoomMembers(null, BaseRoom.RoomType.PUBLIC, 0, BaseUser.SortBy.USERNAME, Sort.DESC, usersCallback); }
@Test(expected = NullPointerException.class) public void testGetRoomMembersShouldFailWithNullRoomType() { rest.getRoomMembers("roomId", null, 0, BaseUser.SortBy.USERNAME, Sort.DESC, usersCallback); }
@Test(expected = NullPointerException.class) public void testGetRoomMembersShouldFailWithNullSortBy() { rest.getRoomMembers("roomId", BaseRoom.RoomType.PUBLIC, 0, null, Sort.DESC, usersCallback); }
@Test(expected = NullPointerException.class) public void testGetRoomMembersShouldFailWithNullSort() { rest.getRoomMembers("roomId", BaseRoom.RoomType.PUBLIC, 0, BaseUser.SortBy.USERNAME, null, usersCallback); }
@Test(expected = NullPointerException.class) public void testGetRoomMembersShouldFailWithNullCallback() { rest.getRoomMembers("roomId", BaseRoom.RoomType.PUBLIC, 0, BaseUser.SortBy.USERNAME, Sort.DESC, null); }
@Test public void testGetRoomMembersShouldBeSuccessful() { mockServer.expect() .get() .withPath("/api/v1/channels.members?roomId=GENERAL&offset=0") .andReturn(200, "{" + " \"total\":154581," + " \"offset\":0," + " \"success\":true," + " \"members\":[" + " {" + " \"utcOffset\":2," + " \"name\":\"User\"," + " \"_id\":\"hcBGHsKo763bTDAiw\"," + " \"username\":\"user\"," + " \"status\":\"offline\"" + " }" + " ]," + " \"count\":1" + "}") .once(); rest.getRoomMembers("GENERAL", BaseRoom.RoomType.PUBLIC, 0, BaseUser.SortBy.USERNAME, Sort.DESC, usersCallback); verify(usersCallback, timeout(DEFAULT_TIMEOUT).only()) .onSuccess(usersCaptor.capture(), anyInt()); List<User> userList = usersCaptor.getValue(); assertThat(userList, is(notNullValue())); assertThat(userList.size(), is(equalTo(1))); User user = userList.get(0); assertThat(user.id(), is(equalTo("hcBGHsKo763bTDAiw"))); }