RestImpl { void getRoomFavoriteMessages(String roomId, BaseRoom.RoomType roomType, int offset, final PaginatedCallback<Message> callback) { String userId = tokenProvider.getToken().userId(); checkNotNull(userId, "userId == null"); checkNotNull(roomId, "roomId == null"); checkNotNull(roomType, "roomType == null"); checkNotNull(callback, "callback == null"); HttpUrl httpUrl = requestUrl(baseUrl, getRestApiMethodNameByRoomType(roomType, "messages")) .addQueryParameter("roomId", roomId) .addQueryParameter("offset", String.valueOf(offset)) .addQueryParameter("query", "{\"starred._id\":{\"$in\":[\"" + userId + "\"]}}") .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()) { 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, Message.class); JsonAdapter<List<Message>> adapter = moshi.adapter(type); List<Message> messageList = adapter.fromJson(json.getJSONArray("messages").toString()); callback.onSuccess(messageList, 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 testGetFavoriteMessagesShouldFailWithNullRoomId() { rest.getRoomFavoriteMessages(null, BaseRoom.RoomType.PUBLIC, 0, messagesCallback); }
@Test(expected = NullPointerException.class) public void testGetRoomFavoriteMessagesShouldFailWithNullRoomType() { rest.getRoomFavoriteMessages("roomId", null, 0, messagesCallback); }
@Test(expected = NullPointerException.class) public void testGetRoomFavoriteMessagesShouldFailWithNullCallback() { rest.getRoomFavoriteMessages("roomId", BaseRoom.RoomType.PUBLIC, 0, null); }
@Test public void testGetRoomFavoriteMessages() { mockServer.expect() .get() .withPath("/api/v1/channels.messages?roomId=GENERAL&offset=0&query={\"starred._id\":{\"$in\":[\"userId\"]}}") .andReturn(200, "{ " + " \"total\":20," + " \"offset\":0," + " \"success\":true," + " \"count\":1," + " \"messages\":[ " + " { " + " \"msg\":\"\"," + " \"file\":{ " + " \"name\":\"damaged-a.jpg\"," + " \"_id\":\"omTGDjutznbmEHzHs\"," + " \"type\":\"image/jpeg\"" + " }," + " \"attachments\":[ " + " { " + " \"title_link_download\":true," + " \"image_size\":577146," + " \"image_url\":\"/file-upload/omTGDjutznbmEHzHs/damaged-a.jpg\"," + " \"description\":\"\"," + " \"title_link\":\"/file-upload/omTGDjutznbmEHzHs/damaged-a.jpg\"," + " \"title\":\"damaged-a.jpg\"," + " \"type\":\"file\"," + " \"image_type\":\"image/jpeg\"" + " }" + " ]," + " \"channels\":[]," + " \"starred\":[ " + " { " + " \"_id\":\"cBD6dHc7oBvGjkruM\"" + " }" + " ]," + " \"u\":{ " + " \"name\":\"Petya Sorokin\"," + " \"_id\":\"2AWv5b2vkkg9zRmKX\"," + " \"username\":\"petya.sorokin\"" + " }," + " \"mentions\":[ ]," + " \"groupable\":false," + " \"_id\":\"6G8o7QxDDyPmWdBdF\"," + " \"rid\":\"GENERAL\"," + " \"_updatedAt\":\"2017-10-03T15:19:33.927Z\"," + " \"ts\":\"2017-10-03T13:05:23.185Z\"" + " }" + " ]" + "}") .once(); rest.getRoomFavoriteMessages("GENERAL", BaseRoom.RoomType.PUBLIC, 0, messagesCallback); verify(messagesCallback, timeout(DEFAULT_TIMEOUT).only()) .onSuccess(messagesCaptor.capture(), anyInt()); List<Message> messageList = messagesCaptor.getValue(); assertThat(messageList, is(notNullValue())); assertThat(messageList.size(), is(equalTo(1))); Message message = messageList.get(0); assertThat(message.id(), is(equalTo("6G8o7QxDDyPmWdBdF"))); }