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