Skip to content

Commit

Permalink
Merge pull request #13 from Gitteroid/feature/testing_plus_ci
Browse files Browse the repository at this point in the history
Setup testing and CI.
  • Loading branch information
amatkivskiy committed Sep 27, 2016
2 parents aa615f6 + eb69f01 commit da29b20
Show file tree
Hide file tree
Showing 11 changed files with 568 additions and 5 deletions.
14 changes: 14 additions & 0 deletions .travis.yml
@@ -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/
5 changes: 4 additions & 1 deletion README.md
@@ -1,10 +1,13 @@
[![Build Status](https://travis-ci.org/Gitteroid/GitterJavaSDK.svg?branch=dev)](https://travis-ci.org/Gitteroid/GitterJavaSDK)
# Gitter.im Java SDK

:+1: **_Fully compatible with Android_**

Async : [ ![Download](https://api.bintray.com/packages/amatkivskiy/maven/gitter.sdk.async/images/download.svg) ](https://bintray.com/amatkivskiy/maven/gitter.sdk.async/_latestVersion)

Sync : [ ![Download](https://api.bintray.com/packages/amatkivskiy/maven/gitter.sdk.async/images/download.svg) ](https://bintray.com/amatkivskiy/maven/gitter.sdk.sync/_latestVersion)
Rx : [ ![Download](https://api.bintray.com/packages/amatkivskiy/maven/gitter.sdk.async/images/download.svg) ](https://bintray.com/amatkivskiy/maven/gitter.sdk.rx/_latestVersion)

Rx : [ ![Download](https://api.bintray.com/packages/amatkivskiy/maven/gitter.sdk.async/images/download.svg) ](https://bintray.com/amatkivskiy/maven/gitter.sdk.rx/_latestVersion) [![Coverage Status](https://coveralls.io/repos/github/Gitteroid/GitterJavaSDK/badge.svg?branch=feature%2Ftesting_plus_ci)](https://coveralls.io/github/Gitteroid/GitterJavaSDK?branch=feature%2Ftesting_plus_ci)

[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-GitterJavaSDK-green.svg?style=flat)](http://android-arsenal.com/details/1/2599)

Expand Down
6 changes: 6 additions & 0 deletions build.gradle
Expand Up @@ -2,6 +2,12 @@ buildscript {
repositories {
jcenter()
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.6.3"
}
}

Expand Down
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.11-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.0-all.zip
@@ -1,4 +1,4 @@
package com.amatkivskiy.gitter.sdk.test;
package com.amatkivskiy.gitter.sdk;

import com.amatkivskiy.gitter.sdk.api.builder.GitterApiBuilder;
import org.junit.Assert;
Expand Down
18 changes: 16 additions & 2 deletions library/rx/build.gradle
@@ -1,5 +1,7 @@
apply plugin: 'java'
apply plugin: 'com.novoda.bintray-release'
apply plugin: 'jacoco'
apply plugin: 'com.github.kt3k.coveralls'

buildscript {
repositories {
Expand All @@ -15,6 +17,13 @@ repositories {
maven { url "https://dl.bintray.com/amatkivskiy/maven/"}
}

jacocoTestReport {
reports {
xml.enabled = true // coveralls plugin depends on xml format report
html.enabled = true
}
}

publish {
groupId = 'com.github.amatkivskiy'
artifactId = 'gitter.sdk.rx'
Expand All @@ -27,7 +36,12 @@ publish {
}

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

testCompile 'org.mockito:mockito-core:2.1.0-RC.1'
testCompile 'junit:junit:4.12'
testCompile 'org.hamcrest:hamcrest-core:1.3'
testCompile 'com.squareup.okhttp3:mockwebserver:3.2.0'
}
@@ -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 + "/";
}
}
@@ -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);
}
}
@@ -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);
}
}

0 comments on commit da29b20

Please sign in to comment.