uk.co.sdev.undertow.rx.services.offers.OffersServiceTest.java Source code

Java tutorial

Introduction

Here is the source code for uk.co.sdev.undertow.rx.services.offers.OffersServiceTest.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.undertow.rx.services.offers;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ning.http.client.AsyncHttpClient;
import org.junit.Ignore;
import org.junit.Test;
import rx.Observable;
import uk.co.sdev.undertow.rx.HandOff;
import uk.co.sdev.undertow.rx.WireMockSupport;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class OffersServiceTest extends WireMockSupport {

    private ObjectMapper mapper = new ObjectMapper();

    private AsyncHttpClient client = new AsyncHttpClient();

    @Test
    public void shouldReturnObservableOfOffers() throws Exception {
        OffersService service = new OffersService(mapper, client, "http://localhost:9101/offers");

        stubGet("/offers?count=2", Arrays.asList(new Offer("offer1", "description1", "offer1.jpg"),
                new Offer("offer2", "description2", "offer2.jpg")));

        List<Offer> offers = new ArrayList<>();
        List<Throwable> throwables = new ArrayList<>();

        CountDownLatch finished = new CountDownLatch(1);

        Observable<Offer> offer = service.offers(2);
        new Thread(() -> offer.subscribe(o -> offers.add(o), t -> throwables.add(t), () -> finished.countDown()))
                .start();

        finished.await(5, TimeUnit.SECONDS);

        assertThat(offers.size(), is(2));
        assertThat(offers.get(0).getTitle(), is("offer1"));
        assertThat(offers.get(1).getTitle(), is("offer2"));
    }
}