Skip to content

Commit

Permalink
Added test for response code 400.
Browse files Browse the repository at this point in the history
  • Loading branch information
amatkivskiy committed Sep 26, 2016
1 parent bb3a91d commit c8ca688
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
Expand Up @@ -21,8 +21,8 @@ public static MockResponse createMockedResponse(String fileName) throws IOExcept
return new MockResponse().setBody(buffer);
}

public static MockResponse createEmptyMockedResponse() throws IOException {
return new MockResponse().setBody("");
public static MockResponse createStringMockedResponse(String response) throws IOException {
return new MockResponse().setBody(response);
}

public static HttpUrl getRequestUrl(MockWebServer webServer) throws InterruptedException {
Expand All @@ -41,7 +41,7 @@ public static void assertSuccessfulResult(TestSubscriber subscriber) {
subscriber.assertCompleted();
}

public static <T extends Exception> void assertErrorResult(TestSubscriber subscriber, Class<T> exceptionClass) {
public static <T extends Exception> void assertErrorTypeResult(TestSubscriber subscriber, Class<T> exceptionClass) {
subscriber.awaitTerminalEvent();
subscriber.assertNotCompleted();
subscriber.assertError(exceptionClass);
Expand Down
Expand Up @@ -16,10 +16,10 @@
import retrofit.RetrofitError;
import rx.observers.TestSubscriber;

import static com.amatkivskiy.gitter.sdk.rx.TestUtils.assertErrorResult;
import static com.amatkivskiy.gitter.sdk.rx.TestUtils.assertErrorTypeResult;
import static com.amatkivskiy.gitter.sdk.rx.TestUtils.assertSuccessfulResult;
import static com.amatkivskiy.gitter.sdk.rx.TestUtils.createEmptyMockedResponse;
import static com.amatkivskiy.gitter.sdk.rx.TestUtils.createMockedResponse;
import static com.amatkivskiy.gitter.sdk.rx.TestUtils.createStringMockedResponse;
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;
Expand Down Expand Up @@ -86,7 +86,7 @@ public void testGetRoomUsersResponseCorrect() throws Exception {
@Test
public void testGetRoomUsersEmptyHttpResponseReturnsNull() throws Exception {
// ARRANGE
this.mockWebServer.enqueue(createEmptyMockedResponse());
this.mockWebServer.enqueue(createStringMockedResponse(""));
TestSubscriber<List<UserResponse>> testSubscriber = TestSubscriber.create();

// ACT
Expand All @@ -106,6 +106,20 @@ public void testNullRoomIdFails() throws Exception {
this.gitterApiClient.getRoomUsers(null).subscribe(testSubscriber);

// ASSERT
assertErrorResult(testSubscriber, RetrofitError.class);
assertErrorTypeResult(testSubscriber, RetrofitError.class);
}

@Test
public void testWrongRoomIdFails() throws Exception {
// ARRANGE
this.mockWebServer.enqueue(createStringMockedResponse("{\"error\":\"Bad Request\"}")
.setResponseCode(400));
TestSubscriber<List<UserResponse>> testSubscriber = TestSubscriber.create();

// ACT
this.gitterApiClient.getRoomUsers("wrong_id").subscribe(testSubscriber);

// ASSERT
assertErrorTypeResult(testSubscriber, RetrofitError.class);
}
}

0 comments on commit c8ca688

Please sign in to comment.