WeatherWebApi { public List<WeatherInfo> pastWeather(double lat, double log, LocalDate from, LocalDate to) { String path = HOST + String.format(PATH_PAST_WEATHER, lat, log, from, to, WEATHER_KEY); Iterator<String> reader = request.getLines(path).iterator(); List<WeatherInfo> infos = new ArrayList<>(); while (reader.next().startsWith("#")) { } reader.next(); while (reader.hasNext()) { infos.add(WeatherInfo.valueOf(reader.next())); if(reader.hasNext()) reader.next(); } return infos; } WeatherWebApi(Request request); List<WeatherInfo> pastWeather(double lat, double log, LocalDate from, LocalDate to); List<LocationInfo> search(String query);  }
@Test void pastWeather() { List<WeatherInfo> infos = new WeatherWebApi(new MockRequest()) .pastWeather(37.017,-7.933, parse("2019-02-01"), parse("2019-02-28")); assertNotNull(infos); assertFalse(infos.isEmpty()); assertTrue(infos.size() > 0); WeatherInfo wi = infos.get(0); assertEquals(parse("2019-02-01"), wi.date); assertEquals(14, wi.tempC); assertEquals(13.8, wi.precipMM); assertEquals("Patchy rain possible", wi.desc); }
@Test public void pastWeatherNumberOfSunnyDaysInFeb2019() { List<WeatherInfo> infos = new WeatherWebApi(new MockRequest()) .pastWeather(37.017,-7.933, parse("2019-02-01"), parse("2019-02-28")); assertEquals(8, filterWeather(infos, wi -> wi.getDesc().equals("Sunny")).size()); }
@Test public void pastWeatherNumberOfRainnnyDaysInFeb2019() { List<WeatherInfo> infos = new WeatherWebApi(new MockRequest()) .pastWeather(37.017,-7.933, parse("2019-02-01"), parse("2019-02-28")); assertEquals(2, filterWeather(infos, wi -> wi.getDesc().equals("Light rain shower")).size()); }
@Test public void pastWeather() { List<WeatherInfo> infos = new WeatherWebApi(new MockRequest()) .pastWeather(37.017,-7.933, parse("2019-02-01"), parse("2019-02-28")); assertNotNull(infos); assertFalse(infos.isEmpty()); assertTrue(infos.size() > 0); WeatherInfo wi = infos.get(0); assertEquals(parse("2019-02-01"), wi.date); assertEquals(14, wi.tempC); assertEquals(13.8, wi.precipMM); assertEquals("Patchy rain possible", wi.desc); }