Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from Gitteroid/feature/testing_plus_ci
Setup testing and CI.
- Loading branch information
Showing
11 changed files
with
568 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
language: java | ||
jdk: oraclejdk7 | ||
before_install: | ||
- chmod +x gradlew | ||
script: | ||
- ./gradlew clean build | ||
after_success: | ||
- ./gradlew jacocoTestReport coveralls | ||
before_cache: | ||
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock | ||
cache: | ||
directories: | ||
- $HOME/.gradle/caches/ | ||
- $HOME/.gradle/wrapper/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../sdk/test/GitterApiClientBuilderTest.java → ...itter/sdk/GitterApiClientBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
library/rx/src/test/java/com/amatkivskiy/gitter/sdk/rx/TestBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.amatkivskiy.gitter.sdk.rx; | ||
|
||
import com.amatkivskiy.gitter.sdk.rx.client.RxGitterApiClient; | ||
|
||
public class TestBuilder extends RxGitterApiClient.Builder { | ||
private final String baseUrl; | ||
|
||
public TestBuilder(String baseUrl) { | ||
this.baseUrl = baseUrl; | ||
} | ||
|
||
@Override | ||
protected String getFullEndpointUrl() { | ||
return this.baseUrl + apiVersion + "/"; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
library/rx/src/test/java/com/amatkivskiy/gitter/sdk/rx/TestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.amatkivskiy.gitter.sdk.rx; | ||
|
||
import java.io.IOException; | ||
|
||
import okhttp3.HttpUrl; | ||
import okhttp3.mockwebserver.MockResponse; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
import okio.Buffer; | ||
import rx.observers.TestSubscriber; | ||
|
||
public class TestUtils { | ||
public static MockWebServer setupMockWebServer() throws IOException { | ||
MockWebServer server = new MockWebServer(); | ||
server.start(); | ||
|
||
return server; | ||
} | ||
|
||
public static MockResponse createMockedResponse(String fileName) throws IOException { | ||
Buffer buffer = new Buffer().readFrom(ClassLoader.getSystemClassLoader().getResourceAsStream(fileName)); | ||
return new MockResponse().setBody(buffer); | ||
} | ||
|
||
public static MockResponse createStringMockedResponse(String response) throws IOException { | ||
return new MockResponse().setBody(response); | ||
} | ||
|
||
public static HttpUrl getRequestUrl(MockWebServer webServer) throws InterruptedException { | ||
String path = webServer.takeRequest().getPath(); | ||
HttpUrl requestUrl = new HttpUrl.Builder().host(webServer.getHostName()) | ||
.port(webServer.getPort()) | ||
.scheme("https") | ||
.build(); | ||
|
||
return HttpUrl.parse(requestUrl + path); | ||
} | ||
|
||
public static void assertSuccessfulResult(TestSubscriber subscriber) { | ||
subscriber.awaitTerminalEvent(); | ||
subscriber.assertNoErrors(); | ||
subscriber.assertCompleted(); | ||
} | ||
|
||
public static <T extends Exception> void assertErrorTypeResult(TestSubscriber subscriber, Class<T> exceptionClass) { | ||
subscriber.awaitTerminalEvent(); | ||
subscriber.assertNotCompleted(); | ||
subscriber.assertError(exceptionClass); | ||
} | ||
|
||
public static <T> T getOnNextEvent(TestSubscriber<T> testSubscriber) { | ||
return testSubscriber.getOnNextEvents().get(0); | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
library/rx/src/test/java/com/amatkivskiy/gitter/sdk/rx/room/GetRoomUsersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package com.amatkivskiy.gitter.sdk.rx.room; | ||
|
||
import com.amatkivskiy.gitter.sdk.model.response.UserResponse; | ||
import com.amatkivskiy.gitter.sdk.rx.TestBuilder; | ||
import com.amatkivskiy.gitter.sdk.rx.client.RxGitterApiClient; | ||
|
||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import java.util.List; | ||
|
||
import okhttp3.HttpUrl; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
import retrofit.RetrofitError; | ||
import rx.observers.TestSubscriber; | ||
|
||
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.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; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.nullValue; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class GetRoomUsersTest { | ||
private MockWebServer mockWebServer; | ||
|
||
@Rule | ||
public ExpectedException thrown = ExpectedException.none(); | ||
|
||
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 testGetRoomUsersResponseCorrect() throws Exception { | ||
// ARRANGE | ||
String roomId = "test_room_id"; | ||
this.mockWebServer.enqueue(createMockedResponse("room/room_users_response.json")); | ||
TestSubscriber<List<UserResponse>> testSubscriber = TestSubscriber.create(); | ||
|
||
// ACT | ||
this.gitterApiClient.getRoomUsers(roomId).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)); | ||
// get room id path segment | ||
assertThat(url.pathSegments().get(3), is(roomId)); | ||
|
||
// check received users | ||
assertSuccessfulResult(testSubscriber); | ||
List<UserResponse> users = getOnNextEvent(testSubscriber); | ||
assertThat(users.size(), is(30)); | ||
|
||
// check whether individual user is parsed correctly | ||
UserResponse user = users.get(12); | ||
|
||
assertThat(user.id, is("554b2e8a15522ed4b3e00c38")); | ||
assertThat(user.username, is("amatkivskiy")); | ||
assertThat(user.displayName, is("Andriy Matkivskiy")); | ||
assertThat(user.url, is("/amatkivskiy")); | ||
assertThat(user.avatarUrlSmall, is("https://avatars1.githubusercontent.com/u/3864884?v=3&s=60")); | ||
assertThat(user.avatarUrlMedium, is("https://avatars1.githubusercontent.com/u/3864884?v=3&s=128")); | ||
assertThat(user.v, is(6)); | ||
assertThat(user.gv, is("3")); | ||
} | ||
|
||
@Test | ||
public void testGetRoomUsersEmptyHttpResponseReturnsNull() throws Exception { | ||
// ARRANGE | ||
this.mockWebServer.enqueue(createStringMockedResponse("")); | ||
TestSubscriber<List<UserResponse>> testSubscriber = TestSubscriber.create(); | ||
|
||
// ACT | ||
this.gitterApiClient.getRoomUsers("roomId").subscribe(testSubscriber); | ||
|
||
// ASSERT | ||
assertSuccessfulResult(testSubscriber); | ||
assertThat(getOnNextEvent(testSubscriber), is(nullValue())); | ||
} | ||
|
||
@Test | ||
public void testNullRoomIdFails() throws Exception { | ||
// ARRANGE | ||
TestSubscriber<List<UserResponse>> testSubscriber = TestSubscriber.create(); | ||
|
||
// ACT | ||
this.gitterApiClient.getRoomUsers(null).subscribe(testSubscriber); | ||
|
||
// ASSERT | ||
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); | ||
} | ||
} |
Oops, something went wrong.