uk.co.sdev.async.http.ning.ObservableClientTest.java Source code

Java tutorial

Introduction

Here is the source code for uk.co.sdev.async.http.ning.ObservableClientTest.java

Source

/**
 * Copyright 2014 Andy Godwin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.sdev.async.http.ning;

import com.fasterxml.jackson.core.type.TypeReference;
import org.junit.Test;
import rx.Observable;
import uk.co.sdev.async.HandOff;
import uk.co.sdev.async.Tuple2;
import uk.co.sdev.async.http.ObservableClientTestSupport;
import uk.co.sdev.async.http.model.Bar;
import uk.co.sdev.async.http.model.Foo;

import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static uk.co.sdev.async.Observables.parallel;

public class ObservableClientTest extends ObservableClientTestSupport {

    private Observable<Foo> getFoo() throws IOException {
        return observableClient.get("http://localhost:9101/foo", Foo.class);
    }

    private Observable<Bar> getBar() throws IOException {
        return observableClient.get("http://localhost:9101/bar", Bar.class);
    }

    private Observable<Optional<Foo>> getOptionalFoo() throws IOException {
        return observableClient.get("http://localhost:9101/foo", new TypeReference<Optional<Foo>>() {
        });
    }

    private Observable<Optional<Bar>> getOptionalBar() throws IOException {
        return observableClient.get("http://localhost:9101/bar", new TypeReference<Optional<Bar>>() {
        });
    }

    @Test
    public void shouldWorkWithoutFallbacks() throws Exception {
        Foo foo = new Foo();
        foo.setValue(1234);
        stubGet("/foo", foo);

        Bar bar = new Bar();
        bar.setValue("moose");
        stubGet("/bar", bar);

        HandOff<Tuple2<Foo, Bar>> handOff = new HandOff<>();

        parallel(getFoo().onErrorReturn(t -> new Foo(0)), getBar().onErrorReturn(t -> new Bar("")))
                .subscribe(tuple -> handOff.put(tuple));

        Tuple2<Foo, Bar> tuple = handOff.get(5);

        assertThat(tuple.a().getValue(), equalTo(1234));

        assertThat(tuple.b().getValue(), equalTo("moose"));
    }

    @Test
    public void shouldUseFallbackForFooIfServiceReturns404() throws Exception {
        stubGet("/foo", 404);

        Bar bar = new Bar();
        bar.setValue("moose");
        stubGet("/bar", bar);

        HandOff<Tuple2<Foo, Bar>> handOff = new HandOff<>();

        parallel(getFoo().onErrorReturn(t -> new Foo(34)), getBar().onErrorReturn(t -> new Bar("")))
                .subscribe(tuple -> handOff.put(tuple));

        Tuple2<Foo, Bar> tuple = handOff.get(5);

        assertThat(tuple.a().getValue(), equalTo(34));

        assertThat(tuple.b().getValue(), equalTo("moose"));
    }

    @Test
    public void shouldWorkWithOptionals() throws Exception {
        Foo foo = new Foo();
        foo.setValue(1234);
        stubGet("/foo", foo);

        Bar bar = new Bar();
        bar.setValue("moose");
        stubGet("/bar", bar);

        HandOff<Tuple2<Optional<Foo>, Optional<Bar>>> handOff = new HandOff<>();

        parallel(getOptionalFoo(), getOptionalBar()).subscribe(tuple -> handOff.put(tuple));

        Tuple2<Optional<Foo>, Optional<Bar>> tuple = handOff.get(5);

        assertThat(tuple.a().get().getValue(), equalTo(1234));

        assertThat(tuple.b().get().getValue(), equalTo("moose"));
    }

    @Test
    public void shouldReturnEmptyFooIfServiceReturns500() throws Exception {
        stubGet("/foo", 500);

        Bar bar = new Bar();
        bar.setValue("moose");
        stubGet("/bar", bar);

        HandOff<Tuple2<Optional<Foo>, Optional<Bar>>> handOff = new HandOff<>();

        parallel(getOptionalFoo().onErrorReturn(t -> Optional.<Foo>empty()),
                getOptionalBar().onErrorReturn(t -> Optional.<Bar>empty())).subscribe(tuple -> handOff.put(tuple));

        Tuple2<Optional<Foo>, Optional<Bar>> tuple = handOff.get(5);

        assertThat(tuple.a().isPresent(), is(false));

        assertThat(tuple.b().get().getValue(), equalTo("moose"));
    }

    @Test
    public void getShouldFallbackIfResponseMapperThrowsException() throws Exception {
        stubGet("/foo", new Foo(12));

        HandOff<Foo> handOff = new HandOff<>();

        Observable<Foo> futureFoo = observableClient
                .get("http://localhost:9101/foo", request -> request.getHeaders().add("Accept", "application/json"),
                        (ResponseMapper<Foo>) response -> {
                            throw new Exception();
                        })
                .onErrorReturn(t -> new Foo(0));

        futureFoo.subscribe(foo -> handOff.put(foo), t -> t.printStackTrace());

        Foo foo = handOff.get(2);
        assertThat(foo.getValue(), is(0));
    }
}