RestImpl { void getUserGroupList(final SimpleListCallback<Subscription> callback) { checkNotNull(callback, "callback == null"); HttpUrl httpUrl = requestUrl(baseUrl, getRestApiMethodNameByRoomType(BaseRoom.RoomType.PRIVATE, "list")) .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()); Type type = Types.newParameterizedType(List.class, Subscription.class); JsonAdapter<List<Subscription>> adapter = moshi.adapter(type); List<Subscription> subscriptionList = adapter.fromJson(json.getJSONArray("groups").toString()); callback.onSuccess(subscriptionList); } 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 testUserGroupListShouldFailWithNullCallback() { rest.getUserGroupList(null); }
@Test public void testUserGroupListShouldBeSuccessful() { mockServer.expect() .get() .withPath("/api/v1/groups.list") .andReturn(200, "{" + " \"groups\": [" + " {" + " \"_id\": \"ByehQjC44FwMeiLbX\"," + " \"name\": \"test-test\"," + " \"t\": \"p\"," + " \"usernames\": [" + " \"testing1\"" + " ]," + " \"msgs\": 0," + " \"u\": {" + " \"_id\": \"aobEdbYhXfu5hkeqG\"," + " \"username\": \"testing1\"" + " }," + " \"ts\": \"2016-12-09T15:08:58.042Z\"," + " \"ro\": false," + " \"sysMes\": true," + " \"_updatedAt\": \"2016-12-09T15:22:40.656Z\"" + " }," + " {" + " \"_id\": \"t7qapfhZjANMRAi5w\"," + " \"name\": \"testing\"," + " \"t\": \"p\"," + " \"usernames\": [" + " \"testing2\"" + " ]," + " \"msgs\": 0," + " \"u\": {" + " \"_id\": \"y65tAmHs93aDChMWu\"," + " \"username\": \"testing2\"" + " }," + " \"ts\": \"2016-12-01T15:08:58.042Z\"," + " \"ro\": false," + " \"sysMes\": true," + " \"_updatedAt\": \"2016-12-09T15:22:40.656Z\"" + " }" + " ]," + " \"success\": true" + "}") .once(); rest.getUserGroupList(subscriptionsCallback); verify(subscriptionsCallback, timeout(DEFAULT_TIMEOUT).only()) .onSuccess(subscriptionsCaptor.capture()); List<Subscription> subscriptionList = subscriptionsCaptor.getValue(); assertThat(subscriptionList, is(notNullValue())); assertThat(subscriptionList.size(), is(equalTo(2))); Subscription subscription = subscriptionList.get(0); assertThat(subscription.roomId(), is(equalTo("ByehQjC44FwMeiLbX"))); assertThat(subscription.name(), is(equalTo("test-test"))); assertThat(subscription.type(), is(equalTo(BaseRoom.RoomType.PRIVATE))); }