// Path: app/src/main/java/br/ufs/demos/rxmvp/playground/core/behaviours/errors/ContentNotFoundError.java
// public class ContentNotFoundError extends RuntimeException {
// 
//     @Override public String getMessage() {
//         return "No content available";
//     }
// 
// }
// 
// Path: app/src/main/java/br/ufs/demos/rxmvp/playground/core/behaviours/errors/UnexpectedResponseError.java
// public class UnexpectedResponseError extends RuntimeException {
// 
//     public UnexpectedResponseError(String message) {
//         super(message);
//     }
// 
// }
// 
// Path: app/src/main/java/br/ufs/demos/rxmvp/playground/core/infraerrors/RestErrorsHandler.java
// public class RestErrorsHandler<T> implements FlowableTransformer<T, T> {
// 
//     @Override public Publisher<T> apply(Flowable<T> upstream) {
//         return upstream.onErrorResumeNext(this::handleIfRestError);
//     }
// 
//     private Publisher<T> handleIfRestError(Throwable throwable) {
// 
//         if (otherThanNotFound(throwable)) {
//             return Flowable.error(new UnexpectedResponseError("Undesired response for this call"));
//         }
// 
//         if (notFound(throwable)) {
//             return Flowable.error(new ContentNotFoundError());
//         }
// 
//         return Flowable.error(throwable);
//     }
// 
//     private boolean otherThanNotFound(Throwable throwable) {
//         return throwable instanceof HttpException && !notFound(throwable);
//     }
// 
//     private boolean notFound(Throwable throwable) {
// 
//         if (throwable instanceof HttpException) {
//             HttpException error = (HttpException) throwable;
//             return error.code() == 404;
//         }
// 
//         return false;
//     }
// }


import org.junit.Test;
import br.ufs.demos.rxmvp.playground.core.behaviours.errors.ContentNotFoundError;
import br.ufs.demos.rxmvp.playground.core.behaviours.errors.UnexpectedResponseError;
import br.ufs.demos.rxmvp.playground.core.infraerrors.RestErrorsHandler;
import io.reactivex.Flowable;
import okhttp3.MediaType;
import okhttp3.ResponseBody;
import retrofit2.HttpException;
import retrofit2.Response;
package br.ufs.demos.rxmvp.playground.core;



/**
 * Created by bira on 7/10/17.
 */

public class RestErrorsHandlerTests {

    RestErrorsHandler<String> handler = new RestErrorsHandler<>();

    @Test public void shouldHandle_404() {
        ResponseBody body = ResponseBody.create(
                MediaType.parse("application/json"),
                "{\"message\":\"Not found\"}");

        Response<?> error = Response.error(404, body);
        HttpException exp = new HttpException(error);

        Flowable<String> notFound = Flowable.error(exp);

        notFound.compose(handler)
                .test()
                .assertError(ContentNotFoundError.class);
    }

    @Test public void shouldHandle_5xx() {
        ResponseBody body = ResponseBody.create(
                MediaType.parse("application/json"),
                "{\"message\":\"Internal server error\"}");

        Response<?> error = Response.error(503, body);
        HttpException exp = new HttpException(error);

// Path: app/src/main/java/br/ufs/demos/rxmvp/playground/core/behaviours/errors/ContentNotFoundError.java
// public class ContentNotFoundError extends RuntimeException {
// 
//     @Override public String getMessage() {
//         return "No content available";
//     }
// 
// }
// 
// Path: app/src/main/java/br/ufs/demos/rxmvp/playground/core/behaviours/errors/UnexpectedResponseError.java
// public class UnexpectedResponseError extends RuntimeException {
// 
//     public UnexpectedResponseError(String message) {
//         super(message);
//     }
// 
// }
// 
// Path: app/src/main/java/br/ufs/demos/rxmvp/playground/core/infraerrors/RestErrorsHandler.java
// public class RestErrorsHandler<T> implements FlowableTransformer<T, T> {
// 
//     @Override public Publisher<T> apply(Flowable<T> upstream) {
//         return upstream.onErrorResumeNext(this::handleIfRestError);
//     }
// 
//     private Publisher<T> handleIfRestError(Throwable throwable) {
// 
//         if (otherThanNotFound(throwable)) {
//             return Flowable.error(new UnexpectedResponseError("Undesired response for this call"));
//         }
// 
//         if (notFound(throwable)) {
//             return Flowable.error(new ContentNotFoundError());
//         }
// 
//         return Flowable.error(throwable);
//     }
// 
//     private boolean otherThanNotFound(Throwable throwable) {
//         return throwable instanceof HttpException && !notFound(throwable);
//     }
// 
//     private boolean notFound(Throwable throwable) {
// 
//         if (throwable instanceof HttpException) {
//             HttpException error = (HttpException) throwable;
//             return error.code() == 404;
//         }
// 
//         return false;
//     }
// }


// Path: app/src/test/java/br/ufs/demos/rxmvp/playground/core/RestErrorsHandlerTests.java
import org.junit.Test;
import br.ufs.demos.rxmvp.playground.core.behaviours.errors.ContentNotFoundError;
import br.ufs.demos.rxmvp.playground.core.behaviours.errors.UnexpectedResponseError;
import br.ufs.demos.rxmvp.playground.core.infraerrors.RestErrorsHandler;
import io.reactivex.Flowable;
import okhttp3.MediaType;
import okhttp3.ResponseBody;
import retrofit2.HttpException;
import retrofit2.Response;

package br.ufs.demos.rxmvp.playground.core;



/**
 * Created by bira on 7/10/17.
 */

public class RestErrorsHandlerTests {

    RestErrorsHandler<String> handler = new RestErrorsHandler<>();

    @Test public void shouldHandle_404() {
        ResponseBody body = ResponseBody.create(
                MediaType.parse("application/json"),
                "{\"message\":\"Not found\"}");

        Response<?> error = Response.error(404, body);
        HttpException exp = new HttpException(error);

        Flowable<String> notFound = Flowable.error(exp);

        notFound.compose(handler)
                .test()
                .assertError(ContentNotFoundError.class);
    }

    @Test public void shouldHandle_5xx() {
        ResponseBody body = ResponseBody.create(
                MediaType.parse("application/json"),
                "{\"message\":\"Internal server error\"}");

        Response<?> error = Response.error(503, body);
        HttpException exp = new HttpException(error);

        Flowable<String> internalServer = Flowable.error(exp);