WeatherService { public Iterable<Location> search(String query) { Iterable<LocationInfo> locals = api.search(query); return LazyQueries.map(locals, this::toLocation); } WeatherService(WeatherApi api);  WeatherService(); Iterable<Location> search(String query);  }
@Test public void testPastWeatherInOporto(){ Request r = new HttpRequest(); int[] count = {0}; Function<String, String> counter = path -> { count[0]++; System.out.println("Request " + path); return path; }; Function<String, Iterable<String>> req = counter.andThen(r::getLines); WeatherService service = new WeatherService(new WeatherRestfullApi(req::apply)); Iterable<Location> locals = service.search("oporto"); assertEquals(1, count[0]); Iterable<WeatherInfo> oportoWeather = first(skip(locals, 1)).getPast30daysWeather(); int tempC = max(map(oportoWeather, WeatherInfo::getTempC)); assertEquals(22, tempC); assertEquals(2, count[0]); count(locals); assertEquals(2, count[0]); }
@Test public void testDayWithMaxTemperatureInOporto(){ WeatherService service = new WeatherService(new WeatherRestfullApi()); Iterable<Location> locals = service.search("oporto"); Iterable<WeatherInfo> oportoWeather = first(skip(locals, 1)).getPast30daysWeather(); WeatherInfo maxTemp = max(oportoWeather, comparing(WeatherInfo::getTempC)); WeatherInfo maxDesc = max(oportoWeather, comparing(WeatherInfo::getDesc)); assertEquals(14, maxDesc.getTempC()); WeatherInfo maxDescAndTemp = max( oportoWeather, comparing(WeatherInfo::getDesc).andThen(WeatherInfo::getTempC)); assertEquals(24, maxDescAndTemp.getTempC()); Cmposable<WeatherInfo> cmp = comparing(WeatherInfo::getDesc) .andThen(WeatherInfo::getTempC) .andThen(WeatherInfo::getPrecipMM) .andThen(WeatherInfo::getDate); }
@Test public void testPastWeatherInOporto(){ Request r = new HttpRequest(); int[] count = {0}; Request req = path -> { count[0]++; return r.getLines(path); }; WeatherService service = new WeatherService(new WeatherWebApi(req)); Iterable<Location> locals = service.search("oporto"); Assert.assertEquals(1, count[0]); Iterable<WeatherInfo> oportoWeather = skip(locals, 1).iterator().next().getPast30daysWeather(); int tempC = max(map(oportoWeather, WeatherInfo::getTempC)); Assert.assertEquals(22, tempC); Assert.assertEquals(3, count[0]); count(locals); Assert.assertEquals(9, count[0]); }
@Test public void testDayWithMaxTemperatureInOporto(){ WeatherService service = new WeatherService(new WeatherRestfullApi()); Iterable<Location> locals = service.search("oporto"); Iterable<WeatherInfo> oportoWeather = first(skip(locals, 1)).getPast30daysWeather(); WeatherInfo maxTemp = max(oportoWeather, comparing(WeatherInfo::getTempC)); WeatherInfo maxDesc = max(oportoWeather, comparing(WeatherInfo::getDesc)); assertEquals(14, maxDesc.getTempC()); WeatherInfo maxDescAndTemp = max( oportoWeather, comparing(WeatherInfo::getDesc)); assertEquals(22, maxDescAndTemp.getTempC()); }