|
| 1 | +package com.baeldung.weather; |
| 2 | + |
| 3 | +import io.reactivex.Flowable; |
| 4 | +import io.reactivex.functions.Function; |
| 5 | +import io.vertx.core.json.JsonObject; |
| 6 | +import io.vertx.reactivex.core.Vertx; |
| 7 | +import io.vertx.reactivex.core.buffer.Buffer; |
| 8 | +import io.vertx.reactivex.core.file.FileSystem; |
| 9 | +import io.vertx.reactivex.core.http.HttpClient; |
| 10 | +import io.vertx.reactivex.core.http.HttpClientResponse; |
| 11 | +import org.junit.After; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | +import org.reactivestreams.Publisher; |
| 15 | +import org.slf4j.Logger; |
| 16 | +import org.slf4j.LoggerFactory; |
| 17 | + |
| 18 | +import java.time.ZonedDateTime; |
| 19 | + |
| 20 | +import static com.baeldung.weather.MetaWeatherClient.getDataByPlaceId; |
| 21 | +import static com.baeldung.weather.MetaWeatherClient.searchByCityName; |
| 22 | + |
| 23 | +public class VertxWithRxJavaTest { |
| 24 | + |
| 25 | + private Vertx vertx; |
| 26 | + private HttpClient httpClient; |
| 27 | + private FileSystem fileSystem; |
| 28 | + private static Logger log = LoggerFactory.getLogger(VertxWithRxJavaTest.class); |
| 29 | + |
| 30 | + @Before |
| 31 | + public void setUp() { |
| 32 | + vertx = io.vertx.reactivex.core.Vertx.vertx(); |
| 33 | + httpClient = vertx.createHttpClient(); |
| 34 | + fileSystem = vertx.fileSystem(); |
| 35 | + } |
| 36 | + |
| 37 | + @After |
| 38 | + public void tearDown() { |
| 39 | + vertx.close(); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void shouldDisplayLightLenghts() throws InterruptedException { |
| 44 | + |
| 45 | + // read the file that contains one city name per line |
| 46 | + fileSystem |
| 47 | + .rxReadFile("cities.txt").toFlowable() |
| 48 | + .doOnNext(buffer -> log.info("File buffer ---\n{}\n---", buffer)) |
| 49 | + .flatMap(buffer -> Flowable.fromArray(buffer.toString().split("\\r?\\n"))) |
| 50 | + .doOnNext(city -> log.info("City from file: '{}'", city)) |
| 51 | + .filter(city -> !city.startsWith("#")) |
| 52 | + .doOnNext(city -> log.info("City that survived filtering: '{}'", city)) |
| 53 | + .flatMap(city -> searchByCityName(httpClient, city)) |
| 54 | + .flatMap(HttpClientResponse::toFlowable) |
| 55 | + .doOnNext(buffer -> log.info("JSON of city detail: '{}'", buffer)) |
| 56 | + .map(extractingWoeid()) |
| 57 | + .flatMap(cityId -> getDataByPlaceId(httpClient, cityId)) |
| 58 | + .flatMap(toBufferFlowable()) |
| 59 | + .doOnNext(buffer -> log.info("JSON of place detail: '{}'", buffer)) |
| 60 | + .map(Buffer::toJsonObject) |
| 61 | + .map(toCityAndDayLength()) |
| 62 | + .subscribe(System.out::println, Throwable::printStackTrace); |
| 63 | + |
| 64 | + Thread.sleep(20000); // enough to give time to complete the execution |
| 65 | + } |
| 66 | + |
| 67 | + private static Function<HttpClientResponse, Publisher<? extends Buffer>> toBufferFlowable() { |
| 68 | + return response -> response |
| 69 | + .toObservable() |
| 70 | + .reduce( |
| 71 | + Buffer.buffer(), |
| 72 | + Buffer::appendBuffer).toFlowable(); |
| 73 | + } |
| 74 | + |
| 75 | + private static Function<Buffer, Long> extractingWoeid() { |
| 76 | + return cityBuffer -> cityBuffer |
| 77 | + .toJsonArray() |
| 78 | + .getJsonObject(0) |
| 79 | + .getLong("woeid"); |
| 80 | + } |
| 81 | + |
| 82 | + private static Function<JsonObject, CityAndDayLength> toCityAndDayLength() { |
| 83 | + return json -> { |
| 84 | + ZonedDateTime sunRise = ZonedDateTime.parse(json.getString("sun_rise")); |
| 85 | + ZonedDateTime sunSet = ZonedDateTime.parse(json.getString("sun_set")); |
| 86 | + String cityName = json.getString("title"); |
| 87 | + return new CityAndDayLength( |
| 88 | + cityName, sunSet.toEpochSecond() - sunRise.toEpochSecond()); |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments