RestImpl { void getRoomFiles(String roomId, BaseRoom.RoomType roomType, int offset, Attachment.SortBy sortBy, Sort sort, final PaginatedCallback<Attachment> 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, "files")) .addQueryParameter("roomId", roomId) .addQueryParameter("offset", String.valueOf(offset)) .addQueryParameter("sort", "{\"" + sortBy.getPropertyName() + "\":" + sort.getDirection() + "}") .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()); JSONArray filesJSONArray = json.getJSONArray("files"); int length = filesJSONArray.length(); List<Attachment> attachments = new ArrayList<>(length); for (int i = 0; i < length; ++i) { attachments.add(new Attachment(filesJSONArray.getJSONObject(i), baseUrl.url().toString())); } callback.onSuccess(attachments, 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 testGetRoomFilesShouldFailWithNullRoomId() { rest.getRoomFiles(null, BaseRoom.RoomType.PUBLIC, 0, Attachment.SortBy.UPLOADED_DATE, Sort.DESC, attachmentCallback); }
@Test(expected = NullPointerException.class) public void testGetRoomFilesShouldFailWithNullRoomType() { rest.getRoomFiles("roomId", null, 0, Attachment.SortBy.UPLOADED_DATE, Sort.DESC, attachmentCallback); }
@Test(expected = NullPointerException.class) public void testGetRoomFilesShouldFailWithNullSortBy() { rest.getRoomFiles("roomId", BaseRoom.RoomType.PUBLIC, 0, null, Sort.DESC, attachmentCallback); }
@Test(expected = NullPointerException.class) public void testGetRoomFilesShouldFailWithNullSort() { rest.getRoomFiles("roomId", BaseRoom.RoomType.PUBLIC, 0, Attachment.SortBy.UPLOADED_DATE, null, attachmentCallback); }
@Test(expected = NullPointerException.class) public void testGetRoomFilesShouldFailWithNullCallback() { rest.getRoomFiles("roomId", BaseRoom.RoomType.PUBLIC, 0, Attachment.SortBy.UPLOADED_DATE, Sort.DESC, null); }
@Test public void testGetRoomFilesShouldFailOnInvalidJson() { mockServer.expect() .get() .withPath("/api/v1/channels.files") .andReturn(DEFAULT_TIMEOUT, "NOT A JSON") .once(); rest.getRoomFiles("general", BaseRoom.RoomType.PUBLIC, 0, Attachment.SortBy.UPLOADED_DATE, Sort.DESC, attachmentCallback); verify(attachmentCallback, timeout(100).only()) .onError(exceptionCaptor.capture()); RocketChatException exception = exceptionCaptor.getValue(); assertThat(exception.getMessage(), is(equalTo("A JSONObject text must begin with '{' at character 0"))); assertThat(exception.getCause(), is(instanceOf(JSONException.class))); }
@Test public void testGetRoomFilesShouldBeSuccessful() { mockServer.expect() .get() .withPath("/api/v1/channels.files?roomId=general&offset=0&sort={%22uploadedAt%22:-1}") .andReturn(200, "{\"total\":5000," + " \"offset\":0," + " \"success\":true," + " \"count\":1," + " \"files\":[" + " {" + " \"extension\":\"txt\"," + " \"description\":\"\"," + " \"store\":\"GoogleCloudStorage:Uploads\"," + " \"type\":\"text/plain\"," + " \"rid\":\"GENERAL\"," + " \"userId\":\"mTYz5v78fEETuyvxH\"," + " \"url\":\"/ufs/GoogleCloudStorage:Uploads/B5HXEJQvoqXjfMyKD/%E6%96%B0%E5%BB%BA%E6%96%87%E6%9C%AC%E6%96%87%E6%A1%A3%20(2).txt\"," + " \"token\":\"C8BB59192B\"," + " \"path\":\"/ufs/GoogleCloudStorage:Uploads/B5HXEJQvoqXjfMyKD/%E6%96%B0%E5%BB%BA%E6%96%87%E6%9C%AC%E6%96%87%E6%A1%A3%20(2).txt\"," + " \"GoogleStorage\":{" + " \"path\":\"eoRXMCHBbQCdDnrke/uploads/GENERAL/mTYz5v78fEETuyvxH/B5HXEJQvoqXjfMyKD\"" + " }," + " \"instanceId\":\"kPnqzFNvmxkMWdcKC\"," + " \"size\":469," + " \"name\":\"sample.txt\"," + " \"progress\":1," + " \"uploadedAt\":\"2017-10-23T05:13:44.875Z\"," + " \"uploading\":false," + " \"etag\":\"mXWYhuiWiCxXpDYdg\"," + " \"_id\":\"B5HXEJQvoqXjfMyKD\"," + " \"complete\":true," + " \"_updatedAt\":\"2017-10-23T05:13:43.220Z\"" + " }" + " ]" + "}") .once(); rest.getRoomFiles("general", BaseRoom.RoomType.PUBLIC, 0, Attachment.SortBy.UPLOADED_DATE, Sort.DESC, attachmentCallback); verify(attachmentCallback, timeout(DEFAULT_TIMEOUT).only()) .onSuccess(attachmentsCaptor.capture(), anyInt()); List<Attachment> attachmentList = attachmentsCaptor.getValue(); assertThat(attachmentList, is(notNullValue())); assertThat(attachmentList.size(), is(equalTo(1))); Attachment attachment = attachmentList.get(0); assertThat(attachment.getId(), is(equalTo("B5HXEJQvoqXjfMyKD"))); assertThat(attachment.getName(), is(equalTo("sample.txt"))); }