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

Java tutorial

Introduction

Here is the source code for uk.co.sdev.async.http.ning.CompletableFutureClientTest.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 uk.co.sdev.async.HandOff;
import uk.co.sdev.async.Tuple2;
import uk.co.sdev.async.http.CompletableFutureClientTestSupport;
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.Futures.parallel;
import static uk.co.sdev.async.Futures.withFallback;

public class CompletableFutureClientTest extends CompletableFutureClientTestSupport {

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

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

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

    private CompletableFuture<Optional<Bar>> getOptionalBar() throws IOException {
        return completableFutureClient.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().handle(withFallback(new Foo(0))), getBar().handle(withFallback(new Bar(""))))
                .whenComplete((tuple, t) -> 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().handle(withFallback(new Foo(34))), getBar().handle(withFallback(new Bar(""))))
                .whenComplete((tuple, t) -> 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()).whenComplete((tuple, t) -> 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().handle(withFallback(Optional.<Foo>empty())),
                getOptionalBar().handle(withFallback(Optional.<Bar>empty())))
                        .whenComplete((tuple, t) -> 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<>();

        CompletableFuture<Foo> futureFoo = completableFutureClient.get("http://localhost:9101/foo", request -> {
            ;
        }, (ResponseMapper<Foo>) response -> {
            throw new Exception();
        }).handle(withFallback(new Foo(0))).whenComplete((foo, t) -> {
            System.out.println("Foo:" + foo.getValue());
        });

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

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