Skip to content

Commit

Permalink
Merge e88156b into 883c2b6
Browse files Browse the repository at this point in the history
  • Loading branch information
amatkivskiy committed Oct 4, 2016
2 parents 883c2b6 + e88156b commit 98b6bcc
Show file tree
Hide file tree
Showing 18 changed files with 431 additions and 7 deletions.
4 changes: 2 additions & 2 deletions library/async/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ publish {
}

dependencies {
// compile project(':library:core')
compile 'com.github.amatkivskiy:gitter.sdk.core:1.5'
compile project(':library:core')
// compile 'com.github.amatkivskiy:gitter.sdk.core:1.5'

compile 'com.squareup.okio:okio:1.6.0'
compile 'com.squareup.okhttp:okhttp:2.5.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.amatkivskiy.gitter.sdk.model.response.RepoResponse;
import com.amatkivskiy.gitter.sdk.model.response.SearchUsersResponse;
import com.amatkivskiy.gitter.sdk.model.response.UserResponse;
import com.amatkivskiy.gitter.sdk.model.response.group.GroupResponse;
import com.amatkivskiy.gitter.sdk.model.response.message.MessageResponse;
import com.amatkivskiy.gitter.sdk.model.response.message.UnReadMessagesResponse;
import com.amatkivskiy.gitter.sdk.model.response.room.RoomResponse;
Expand Down Expand Up @@ -128,4 +129,14 @@ void updateMessage(@Path("roomId") String roomId,
@Path("chatMessageId") String chatMessageId,
@Field("text") String text,
Callback<MessageResponse> callback);

// Groups API
@GET("/groups")
void getCurrentUserGroups(@Query("type") String type, Callback<List<GroupResponse>> callback);

@GET("/groups/{groupId}")
void getGroupById(@Path("groupId") String groupId, Callback<GroupResponse> callback);

@GET("/groups/{groupId}/rooms")
void getGroupRooms(@Path("groupId") String groupId, Callback<List<RoomResponse>> callback);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.amatkivskiy.gitter.sdk.model.response.RepoResponse;
import com.amatkivskiy.gitter.sdk.model.response.SearchUsersResponse;
import com.amatkivskiy.gitter.sdk.model.response.UserResponse;
import com.amatkivskiy.gitter.sdk.model.response.group.GroupResponse;
import com.amatkivskiy.gitter.sdk.model.response.message.MessageResponse;
import com.amatkivskiy.gitter.sdk.model.response.message.UnReadMessagesResponse;
import com.amatkivskiy.gitter.sdk.model.response.room.RoomResponse;
Expand Down Expand Up @@ -179,6 +180,23 @@ public void sendMessage(String roomId, String text, Callback<MessageResponse> ca
api.sendMessage(roomId, text, callback);
}

// Groups API
public void getCurrentUserGroups(Callback<List<GroupResponse>> callback) {
api.getCurrentUserGroups(null, callback);
}

public void getCurrentUserAdminGroups(Callback<List<GroupResponse>> callback) {
api.getCurrentUserGroups("admin", callback);
}

public void getGroupById(String groupId, Callback<GroupResponse> callback) {
api.getGroupById(groupId, callback);
}

public void getGroupRooms(String groupId, Callback<List<RoomResponse>> callback) {
api.getGroupRooms(groupId, callback);
}

public static class Builder extends GitterApiBuilder<Builder, AsyncGitterApiClient> {

protected String getFullEndpointUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
import com.amatkivskiy.gitter.sdk.async.api.AsyncGitterAuthenticateApi;
import com.amatkivskiy.gitter.sdk.credentials.GitterDeveloperCredentials;
import com.amatkivskiy.gitter.sdk.credentials.GitterDeveloperCredentialsProvider;
import com.amatkivskiy.gitter.sdk.model.error.GitterApiErrorResponse;
import com.amatkivskiy.gitter.sdk.model.error.GitterApiException;
import com.amatkivskiy.gitter.sdk.model.response.AccessTokenResponse;
import retrofit.Callback;
import retrofit.ErrorHandler;
import retrofit.RetrofitError;

import static com.amatkivskiy.gitter.sdk.Constants.GitterEndpoints.GITTER_AUTHENTICATION_ENDPOINT;

Expand Down Expand Up @@ -50,7 +54,24 @@ public static class Builder extends BaseApiBuilder<Builder, AsyncGitterAuthentic
@Override
public AsyncGitterAuthenticationClient build() {
restAdapterBuilder.setEndpoint(GITTER_AUTHENTICATION_ENDPOINT);
restAdapterBuilder.setErrorHandler(gitterWrappedErrorhandler);
restAdapterBuilder.setErrorHandler(new ErrorHandler() {
@Override
public Throwable handleError(RetrofitError cause) {
Throwable returnThrowable = cause;
if (cause.getKind() == RetrofitError.Kind.HTTP) {
if (cause.getResponse() != null) {
GitterApiErrorResponse errorResponse = (GitterApiErrorResponse) cause.getBodyAs(GitterApiErrorResponse.class);

if (errorResponse != null) {
returnThrowable = new GitterApiException(errorResponse);
returnThrowable.setStackTrace(cause.getStackTrace());
}
}
}

return returnThrowable;
}
});

AsyncGitterAuthenticateApi api = restAdapterBuilder.build().create(AsyncGitterAuthenticateApi.class);
return new AsyncGitterAuthenticationClient(api);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.amatkivskiy.gitter.sdk.model.response.group;

public class BackedBy {
public final String linkPath;
public final Type type;

public BackedBy(String linkPath, Type type) {
this.linkPath = linkPath;
this.type = type;
}

public enum Type {
ONE_TO_ONE,
GH_REPO,
GH_ORG,
GH_USER
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.amatkivskiy.gitter.sdk.model.response.group;

public class GroupResponse {
public final String id;
public final String name;
public final String uri;
public final BackedBy backedBy;
public final String avatarUrl;

public GroupResponse(String id, String name, String uri, BackedBy backedBy, String avatarUrl) {
this.id = id;
this.name = name;
this.uri = uri;
this.backedBy = backedBy;
this.avatarUrl = avatarUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class RoomResponse {
@SerializedName("githubType") public final RoomType githubRoomType;
@SerializedName("security") public final String security;
@SerializedName("noindex") public final boolean noIndex;
@SerializedName("tags") public final List<String> tags = new ArrayList<String>();
@SerializedName("tags") public final List<String> tags = new ArrayList<>();
@SerializedName("v") public final int v;
@SerializedName("roomMember") public final boolean isRoomMember;
@SerializedName("avatarUrl") public final String avatarUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.amatkivskiy.gitter.sdk.model.response.RepoResponse;
import com.amatkivskiy.gitter.sdk.model.response.SearchUsersResponse;
import com.amatkivskiy.gitter.sdk.model.response.UserResponse;
import com.amatkivskiy.gitter.sdk.model.response.group.GroupResponse;
import com.amatkivskiy.gitter.sdk.model.response.message.MessageResponse;
import com.amatkivskiy.gitter.sdk.model.response.message.UnReadMessagesResponse;
import com.amatkivskiy.gitter.sdk.model.response.room.RoomResponse;
Expand Down Expand Up @@ -110,4 +111,14 @@ Observable<MessageResponse> sendMessage(
Observable<MessageResponse> updateMessage(@Path("roomId") String roomId,
@Path("chatMessageId") String chatMessageId,
@Field("text") String text);

// Groups API
@GET("/groups")
Observable<List<GroupResponse>> getCurrentUserGroups(@Query("type") String type);

@GET("/groups/{groupId}")
Observable<GroupResponse> getGroupById(@Path("groupId") String groupId);

@GET("/groups/{groupId}/rooms")
Observable<List<RoomResponse>> getGroupRooms(@Path("groupId") String groupId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.amatkivskiy.gitter.sdk.model.response.RepoResponse;
import com.amatkivskiy.gitter.sdk.model.response.SearchUsersResponse;
import com.amatkivskiy.gitter.sdk.model.response.UserResponse;
import com.amatkivskiy.gitter.sdk.model.response.group.GroupResponse;
import com.amatkivskiy.gitter.sdk.model.response.message.MessageResponse;
import com.amatkivskiy.gitter.sdk.model.response.message.UnReadMessagesResponse;
import com.amatkivskiy.gitter.sdk.model.response.room.RoomResponse;
Expand Down Expand Up @@ -161,6 +162,23 @@ public Observable<UnReadMessagesResponse> getUnReadMessages(String userId, Strin
return api.getUnReadMessages(userId, roomId);
}

// Groups API
public Observable<List<GroupResponse>> getCurrentUserGroups() {
return api.getCurrentUserGroups(null);
}

public Observable<List<GroupResponse>> getCurrentUserAdminGroups() {
return api.getCurrentUserGroups("admin");
}

public Observable<GroupResponse> getGroupById(String groupId) {
return api.getGroupById(groupId);
}

public Observable<List<RoomResponse>> getGroupRooms(String groupId) {
return api.getGroupRooms(groupId);
}

public static class Builder extends GitterApiBuilder<Builder, RxGitterApiClient> {

protected String getFullEndpointUrl() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package com.amatkivskiy.gitter.sdk.rx.group;

import com.amatkivskiy.gitter.sdk.model.response.group.BackedBy;
import com.amatkivskiy.gitter.sdk.model.response.group.GroupResponse;
import com.amatkivskiy.gitter.sdk.model.response.room.RoomResponse;
import com.amatkivskiy.gitter.sdk.model.response.room.RoomType;
import com.amatkivskiy.gitter.sdk.rx.TestBuilder;
import com.amatkivskiy.gitter.sdk.rx.client.RxGitterApiClient;

import org.junit.Before;
import org.junit.Test;

import java.util.List;

import okhttp3.HttpUrl;
import okhttp3.mockwebserver.MockWebServer;
import rx.observers.TestSubscriber;

import static com.amatkivskiy.gitter.sdk.rx.TestUtils.assertSuccessfulResult;
import static com.amatkivskiy.gitter.sdk.rx.TestUtils.createMockedResponse;
import static com.amatkivskiy.gitter.sdk.rx.TestUtils.getOnNextEvent;
import static com.amatkivskiy.gitter.sdk.rx.TestUtils.getRequestUrl;
import static com.amatkivskiy.gitter.sdk.rx.TestUtils.setupMockWebServer;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;

public class GetGroupTest {
private MockWebServer mockWebServer;
private RxGitterApiClient gitterApiClient;

@Before
public void setUp() throws Exception {
// Setup mocked WebsServer to received requests from RxGitterApiClient
this.mockWebServer = setupMockWebServer();

// To redirect all requests to our mocked WebServer we need to pass its server URL.
String url = this.mockWebServer.url("").toString();
this.gitterApiClient = new TestBuilder(url)
.withAccountToken("can_be_any_string")
.build();
}

@Test
public void testGetUserGroupsResponseCorrect() throws Exception {
// ARRANGE
this.mockWebServer.enqueue(createMockedResponse("group/get_groups_response.json"));
TestSubscriber<List<GroupResponse>> testSubscriber = TestSubscriber.create();

// ACT
this.gitterApiClient.getCurrentUserGroups().subscribe(testSubscriber);

// ASSERT
// Assert RxGitterApiClient pass correct params in the request URL
HttpUrl url = getRequestUrl(this.mockWebServer);
// check number of path segments in url
assertThat(url.pathSegments().size(), is(3));
assertThat(url.pathSegments().get(2), is("groups"));

// check received room
assertSuccessfulResult(testSubscriber);
List<GroupResponse> groups = getOnNextEvent(testSubscriber);

assertThat(groups.size(), is(3));

assertGitteroidGroup(groups.get(0));
}

@Test
public void testGetUserAdminGroupsResponseCorrect() throws Exception {
// ARRANGE
this.mockWebServer.enqueue(createMockedResponse("group/get_user_admin_groups.json"));
TestSubscriber<List<GroupResponse>> testSubscriber = TestSubscriber.create();

// ACT
this.gitterApiClient.getCurrentUserAdminGroups().subscribe(testSubscriber);

// ASSERT
// Assert RxGitterApiClient pass correct params in the request URL
HttpUrl url = getRequestUrl(this.mockWebServer);
// check number of path segments in url
assertThat(url.pathSegments().size(), is(3));
assertThat(url.pathSegments().get(2), is("groups"));
assertThat(url.queryParameter("type"), is("admin"));

// check received room
assertSuccessfulResult(testSubscriber);
List<GroupResponse> groups = getOnNextEvent(testSubscriber);

assertThat(groups.size(), is(1));
assertGitteroidGroup(groups.get(0));
}

@Test
public void testGetGroupByIdResponseCorrect() throws Exception {
// ARRANGE
String groupId = "57542c36c43b8c6019771056";
this.mockWebServer.enqueue(createMockedResponse("group/get_group_by_id_response.json"));
TestSubscriber<GroupResponse> testSubscriber = TestSubscriber.create();

// ACT
this.gitterApiClient.getGroupById(groupId).subscribe(testSubscriber);

// ASSERT
// Assert RxGitterApiClient pass correct params in the request URL
HttpUrl url = getRequestUrl(this.mockWebServer);
// check number of path segments in url
assertThat(url.pathSegments().size(), is(4));
assertThat(url.pathSegments().get(2), is("groups"));
assertThat(url.pathSegments().get(3), is(groupId));

// check received room
assertSuccessfulResult(testSubscriber);
GroupResponse group = getOnNextEvent(testSubscriber);

assertGitteroidGroup(group);
}

@Test
public void testGetGroupRoomsResponseCorrect() throws Exception {
// ARRANGE
String groupId = "57542c36c43b8c6019771056";
this.mockWebServer.enqueue(createMockedResponse("group/get_group_rooms_response.json"));
TestSubscriber<List<RoomResponse>> testSubscriber = TestSubscriber.create();

// ACT
this.gitterApiClient.getGroupRooms(groupId).subscribe(testSubscriber);

// ASSERT
// Assert RxGitterApiClient pass correct params in the request URL
HttpUrl url = getRequestUrl(this.mockWebServer);
// check number of path segments in url
assertThat(url.pathSegments().size(), is(5));
assertThat(url.pathSegments().get(2), is("groups"));
assertThat(url.pathSegments().get(3), is(groupId));

// check received room
assertSuccessfulResult(testSubscriber);
List<RoomResponse> rooms = getOnNextEvent(testSubscriber);

assertThat(rooms.size(), is(1));

RoomResponse first = rooms.get(0);
assertThat(first.id, is("5790a3a2c2f0db084a24004d"));
assertThat(first.name, is("gitterHQ/api"));
assertThat(first.topic, is("Gitter API and Libraries"));
assertThat(first.avatarUrl, is("https://avatars-01.gitter.im/group/i/57542c12c43b8c601976fa66"));
assertThat(first.uri, is("gitterHQ/api"));
assertThat(first.oneToOne, is(false));
assertThat(first.userCount, is(40));
assertThat(first.unreadItems, is(0));
assertThat(first.mentions, is(0));
assertThat(first.lastAccessTime, is("2016-10-03T16:39:14.302Z"));
assertThat(first.lurk, is(true));
assertThat(first.activity, is(false));
assertThat(first.url, is("/gitterHQ/api"));
assertThat(first.githubRoomType, is(RoomType.REPO_CHANNEL));
assertThat(first.security, is("PUBLIC"));
assertThat(first.isPremium, is(true));
assertThat(first.noIndex, is(false));
assertThat(first.tags, is(nullValue()));
assertThat(first.isRoomMember, is(true));
assertThat(first.groupId, is("57542c12c43b8c601976fa66"));
assertThat(first.isPublic, is(true));
}

private static void assertGitteroidGroup(GroupResponse first) {
assertThat(first, is(notNullValue()));
assertThat(first.id, is("57542c36c43b8c6019771056"));
assertThat(first.name, is("Gitteroid"));
assertThat(first.uri, is("Gitteroid"));
assertThat(first.avatarUrl, is("https://avatars-04.gitter.im/group/i/57542c36c43b8c6019771056"));
assertThat(first.backedBy, is(notNullValue()));
assertThat(first.backedBy.type, is(BackedBy.Type.GH_ORG));
assertThat(first.backedBy.linkPath, is("Gitteroid"));
}
}
10 changes: 10 additions & 0 deletions library/rx/src/test/resources/group/get_group_by_id_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"id": "57542c36c43b8c6019771056",
"name": "Gitteroid",
"uri": "Gitteroid",
"backedBy": {
"type": "GH_ORG",
"linkPath": "Gitteroid"
},
"avatarUrl": "https://avatars-04.gitter.im/group/i/57542c36c43b8c6019771056"
}
Loading

0 comments on commit 98b6bcc

Please sign in to comment.