From cdc5a4b38037454df7abdf32722b57b99da1cf73 Mon Sep 17 00:00:00 2001 From: Alexey Zinchenko Date: Wed, 24 Mar 2021 02:15:01 +0300 Subject: [PATCH] Added tests for forecast functionality. --- .../api/OpenWeatherMapClient.java | 7 - ...FiveDayThreeHourStepForecastRequester.java | 5 + ...DayThreeHourStepForecastRequesterImpl.java | 6 + .../weather/CurrentWeatherRequester.java | 3 + ...yThreeHourStepForecastIntegrationTest.java | 421 +++++++++++++++++- 5 files changed, 429 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapClient.java b/src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapClient.java index 02134a0..fc94941 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapClient.java +++ b/src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapClient.java @@ -31,7 +31,6 @@ import com.github.prominence.openweathermap.api.request.weather.CurrentWeatherRe import static com.github.prominence.openweathermap.api.enums.SubscriptionPlan.*; public class OpenWeatherMapClient { - private final String apiKey; public OpenWeatherMapClient(String apiKey) { @@ -47,10 +46,4 @@ public class OpenWeatherMapClient { public FiveDayThreeHourStepForecastRequester forecast5Day3HourStep() { return new FiveDayThreeHourStepForecastRequesterImpl(apiKey); } - - // TODO: - // * Forecast: hourly, daily. Probably better to cover all free-plan functionality? - // * Air Pollution - // * Ultraviolet index - // DOCS } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastRequester.java b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastRequester.java index 7c36de1..9ac7d48 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastRequester.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastRequester.java @@ -24,6 +24,9 @@ package com.github.prominence.openweathermap.api.request.forecast.free; import com.github.prominence.openweathermap.api.model.Coordinate; +/** + * An interface for API methods. + */ public interface FiveDayThreeHourStepForecastRequester { FiveDayThreeHourStepForecastRequestCustomizer byCityName(String cityName); @@ -37,4 +40,6 @@ public interface FiveDayThreeHourStepForecastRequester { FiveDayThreeHourStepForecastRequestCustomizer byCoordinate(Coordinate coordinate); FiveDayThreeHourStepForecastRequestCustomizer byZipCodeAndCountry(String zipCode, String countryCode); + + FiveDayThreeHourStepForecastRequestCustomizer byZipCodeInUSA(String zipCode); } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastRequesterImpl.java b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastRequesterImpl.java index 2474b9b..863158b 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastRequesterImpl.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastRequesterImpl.java @@ -70,4 +70,10 @@ public class FiveDayThreeHourStepForecastRequesterImpl implements FiveDayThreeHo urlBuilder.addRequestParameter("zip", zipCode + "," + countryCode); return new FiveDayThreeHourStepForecastRequestCustomizerImpl(urlBuilder); } + + @Override + public FiveDayThreeHourStepForecastRequestCustomizer byZipCodeInUSA(String zipCode) { + urlBuilder.addRequestParameter("zip", zipCode); + return new FiveDayThreeHourStepForecastRequestCustomizerImpl(urlBuilder); + } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherRequester.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherRequester.java index 702f5ea..bb4b7e9 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherRequester.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherRequester.java @@ -25,6 +25,9 @@ package com.github.prominence.openweathermap.api.request.weather; import com.github.prominence.openweathermap.api.request.weather.multiple.MultipleLocationsCurrentWeatherRequester; import com.github.prominence.openweathermap.api.request.weather.single.SingleLocationCurrentWeatherRequester; +/** + * An interface for API methods. + */ public interface CurrentWeatherRequester { SingleLocationCurrentWeatherRequester single(); diff --git a/src/test/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastIntegrationTest.java b/src/test/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastIntegrationTest.java index 0f0c56f..51ed656 100644 --- a/src/test/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastIntegrationTest.java +++ b/src/test/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastIntegrationTest.java @@ -23,16 +23,23 @@ package com.github.prominence.openweathermap.api.request.forecast.free; import com.github.prominence.openweathermap.api.ApiTest; +import com.github.prominence.openweathermap.api.OpenWeatherMapClient; import com.github.prominence.openweathermap.api.enums.Language; import com.github.prominence.openweathermap.api.enums.UnitSystem; +import com.github.prominence.openweathermap.api.exception.InvalidAuthTokenException; +import com.github.prominence.openweathermap.api.exception.NoDataFoundException; +import com.github.prominence.openweathermap.api.model.Coordinate; import com.github.prominence.openweathermap.api.model.forecast.Forecast; +import com.github.prominence.openweathermap.api.model.forecast.WeatherForecast; import org.junit.Assert; import org.junit.Test; -public class FiveDayThreeHourStepForecastIntegrationTest extends ApiTest { +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +public class FiveDayThreeHourStepForecastIntegrationTest extends ApiTest { @Test - public void whenGetJavaObject_thenReturnNotNull() { + public void whenGetForecastByCityNameRequestAsJava_thenReturnNotNull() { final Forecast forecast = getClient() .forecast5Day3HourStep() .byCityName("Minsk") @@ -43,19 +50,421 @@ public class FiveDayThreeHourStepForecastIntegrationTest extends ApiTest { .asJava(); Assert.assertNotNull(forecast); - System.out.println(forecast); + Assert.assertNotNull(forecast.getLocation()); + Assert.assertNotNull(forecast.getWeatherForecasts()); + for (WeatherForecast weatherForecast : forecast.getWeatherForecasts()) { + Assert.assertNotNull(weatherForecast.getState()); + Assert.assertNotNull(weatherForecast.getDescription()); + Assert.assertNotNull(weatherForecast.getForecastTime()); + Assert.assertNotNull(weatherForecast.getTemperature()); + Assert.assertNotNull(weatherForecast.getAtmosphericPressure()); + Assert.assertNotNull(weatherForecast.getHumidity()); + Assert.assertNotNull(weatherForecast.getWind()); + } } @Test - public void whenGetJavaObject_thenFieldsAreFilled() { + public void whenGetForecastByCityNameRequestAsJSON_thenReturnNotNull() { + final String forecastJson = getClient() + .forecast5Day3HourStep() + .byCityName("Minsk") + .language(Language.ENGLISH) + .unitSystem(UnitSystem.METRIC) + .count(15) + .retrieve() + .asJSON(); + + Assert.assertTrue(forecastJson.startsWith("{")); + } + + @Test + public void whenGetForecastByCityNameRequestAsXML_thenReturnNotNull() { + final String forecastXml = getClient() + .forecast5Day3HourStep() + .byCityName("Minsk") + .language(Language.ENGLISH) + .unitSystem(UnitSystem.METRIC) + .count(15) + .retrieve() + .asXML(); + + Assert.assertTrue(forecastXml.startsWith("<")); + } + + @Test + public void whenGetForecastByCityNameAndCountryCodeRequestAsJava_thenReturnNotNull() { final Forecast forecast = getClient() .forecast5Day3HourStep() - .byCityName("London") + .byCityName("Minsk", "BY") .language(Language.ENGLISH) + .unitSystem(UnitSystem.METRIC) + .count(15) .retrieve() .asJava(); Assert.assertNotNull(forecast); - System.out.println(forecast); + Assert.assertNotNull(forecast.getLocation()); + Assert.assertNotNull(forecast.getWeatherForecasts()); + for (WeatherForecast weatherForecast : forecast.getWeatherForecasts()) { + Assert.assertNotNull(weatherForecast.getState()); + Assert.assertNotNull(weatherForecast.getDescription()); + Assert.assertNotNull(weatherForecast.getForecastTime()); + Assert.assertNotNull(weatherForecast.getTemperature()); + Assert.assertNotNull(weatherForecast.getAtmosphericPressure()); + Assert.assertNotNull(weatherForecast.getHumidity()); + Assert.assertNotNull(weatherForecast.getWind()); + } + } + + @Test + public void whenGetForecastByCityNameAndCountryCodeRequestAsJSON_thenReturnNotNull() { + final String forecastJson = getClient() + .forecast5Day3HourStep() + .byCityName("Minsk", "by") + .language(Language.ENGLISH) + .unitSystem(UnitSystem.METRIC) + .count(15) + .retrieve() + .asJSON(); + + Assert.assertTrue(forecastJson.startsWith("{")); + } + + @Test + public void whenGetForecastByCityNameAndCountryCodeRequestAsXML_thenReturnNotNull() { + final String forecastXml = getClient() + .forecast5Day3HourStep() + .byCityName("Minsk", "by") + .language(Language.ENGLISH) + .unitSystem(UnitSystem.METRIC) + .count(15) + .retrieve() + .asXML(); + + Assert.assertTrue(forecastXml.startsWith("<")); + } + + @Test + public void whenGetForecastByCityNameAndStateCodeAndCountryCodeRequestAsJava_thenReturnNotNull() { + final Forecast forecast = getClient() + .forecast5Day3HourStep() + .byCityName("New York", "NY", "US") + .language(Language.CHINESE_TRADITIONAL) + .unitSystem(UnitSystem.STANDARD) + .count(15) + .retrieve() + .asJava(); + + Assert.assertNotNull(forecast); + Assert.assertNotNull(forecast.getLocation()); + Assert.assertNotNull(forecast.getWeatherForecasts()); + for (WeatherForecast weatherForecast : forecast.getWeatherForecasts()) { + Assert.assertNotNull(weatherForecast.getState()); + Assert.assertNotNull(weatherForecast.getDescription()); + Assert.assertNotNull(weatherForecast.getForecastTime()); + Assert.assertNotNull(weatherForecast.getTemperature()); + Assert.assertNotNull(weatherForecast.getAtmosphericPressure()); + Assert.assertNotNull(weatherForecast.getHumidity()); + Assert.assertNotNull(weatherForecast.getWind()); + } + } + + @Test + public void whenGetForecastByCityNameAndStateCodeAndCountryCodeRequestAsJSON_thenReturnNotNull() { + final String forecastJson = getClient() + .forecast5Day3HourStep() + .byCityName("New York", "NY", "US") + .language(Language.SPANISH) + .unitSystem(UnitSystem.IMPERIAL) + .count(15) + .retrieve() + .asJSON(); + + Assert.assertTrue(forecastJson.startsWith("{")); + } + + @Test + public void whenGetForecastByCityNameAndStateCodeAndCountryCodeRequestAsXML_thenReturnNotNull() { + final String forecastXml = getClient() + .forecast5Day3HourStep() + .byCityName("New York", "NY", "US") + .language(Language.ENGLISH) + .unitSystem(UnitSystem.METRIC) + .retrieve() + .asXML(); + + Assert.assertTrue(forecastXml.startsWith("<")); + } + + @Test + public void whenGetForecastByCityIdRequestAsJava_thenReturnNotNull() { + final Forecast forecast = getClient() + .forecast5Day3HourStep() + .byCityId(350001514) + .language(Language.ENGLISH) + .unitSystem(UnitSystem.METRIC) + .count(15) + .retrieve() + .asJava(); + + Assert.assertNotNull(forecast); + Assert.assertNotNull(forecast.getLocation()); + Assert.assertNotNull(forecast.getWeatherForecasts()); + for (WeatherForecast weatherForecast : forecast.getWeatherForecasts()) { + Assert.assertNotNull(weatherForecast.getState()); + Assert.assertNotNull(weatherForecast.getDescription()); + Assert.assertNotNull(weatherForecast.getForecastTime()); + Assert.assertNotNull(weatherForecast.getTemperature()); + Assert.assertNotNull(weatherForecast.getAtmosphericPressure()); + Assert.assertNotNull(weatherForecast.getHumidity()); + Assert.assertNotNull(weatherForecast.getWind()); + } + } + + @Test + public void whenGetForecastByCityIdRequestAsJSON_thenReturnNotNull() { + final String forecastJson = getClient() + .forecast5Day3HourStep() + .byCityId(350001514) + .language(Language.SPANISH) + .unitSystem(UnitSystem.IMPERIAL) + .count(15) + .retrieve() + .asJSON(); + + Assert.assertTrue(forecastJson.startsWith("{")); + } + + @Test + public void whenGetForecastByCityIdRequestAsXML_thenReturnNotNull() { + final String forecastXml = getClient() + .forecast5Day3HourStep() + .byCityId(350001514) + .language(Language.ENGLISH) + .unitSystem(UnitSystem.METRIC) + .retrieve() + .asXML(); + + Assert.assertTrue(forecastXml.startsWith("<")); + } + + @Test + public void whenGetForecastByCoordinatesRequestAsJava_thenReturnNotNull() { + final Forecast forecast = getClient() + .forecast5Day3HourStep() + .byCoordinate(Coordinate.forValues(5, 5)) + .language(Language.ENGLISH) + .unitSystem(UnitSystem.METRIC) + .count(15) + .retrieve() + .asJava(); + + Assert.assertNotNull(forecast); + Assert.assertNotNull(forecast.getLocation()); + Assert.assertNotNull(forecast.getWeatherForecasts()); + for (WeatherForecast weatherForecast : forecast.getWeatherForecasts()) { + Assert.assertNotNull(weatherForecast.getState()); + Assert.assertNotNull(weatherForecast.getDescription()); + Assert.assertNotNull(weatherForecast.getForecastTime()); + Assert.assertNotNull(weatherForecast.getTemperature()); + Assert.assertNotNull(weatherForecast.getAtmosphericPressure()); + Assert.assertNotNull(weatherForecast.getHumidity()); + Assert.assertNotNull(weatherForecast.getWind()); + } + } + + @Test + public void whenGetForecastByCoordinatesRequestAsJSON_thenReturnNotNull() { + final String forecastJson = getClient() + .forecast5Day3HourStep() + .byCoordinate(Coordinate.forValues(5, 5)) + .language(Language.SPANISH) + .unitSystem(UnitSystem.IMPERIAL) + .count(15) + .retrieve() + .asJSON(); + + Assert.assertTrue(forecastJson.startsWith("{")); + } + + @Test + public void whenGetForecastByCoordinatesRequestAsXML_thenReturnNotNull() { + final String forecastXml = getClient() + .forecast5Day3HourStep() + .byCoordinate(Coordinate.forValues(5, 5)) + .language(Language.ENGLISH) + .unitSystem(UnitSystem.METRIC) + .retrieve() + .asXML(); + + Assert.assertTrue(forecastXml.startsWith("<")); + } + + @Test + public void whenGetForecastByZipCodeInUSARequestAsJava_thenReturnNotNull() { + final Forecast forecast = getClient() + .forecast5Day3HourStep() + .byZipCodeInUSA("10005") + .language(Language.ENGLISH) + .unitSystem(UnitSystem.METRIC) + .count(15) + .retrieve() + .asJava(); + + Assert.assertNotNull(forecast); + Assert.assertNotNull(forecast.getLocation()); + Assert.assertNotNull(forecast.getWeatherForecasts()); + for (WeatherForecast weatherForecast : forecast.getWeatherForecasts()) { + Assert.assertNotNull(weatherForecast.getState()); + Assert.assertNotNull(weatherForecast.getDescription()); + Assert.assertNotNull(weatherForecast.getForecastTime()); + Assert.assertNotNull(weatherForecast.getTemperature()); + Assert.assertNotNull(weatherForecast.getAtmosphericPressure()); + Assert.assertNotNull(weatherForecast.getHumidity()); + Assert.assertNotNull(weatherForecast.getWind()); + } + } + + @Test + public void whenGetForecastByZipCodeInUSARequestAsJSON_thenReturnNotNull() { + final String forecastJson = getClient() + .forecast5Day3HourStep() + .byZipCodeInUSA("10005") + .language(Language.SPANISH) + .unitSystem(UnitSystem.IMPERIAL) + .count(15) + .retrieve() + .asJSON(); + + Assert.assertTrue(forecastJson.startsWith("{")); + } + + @Test + public void whenGetForecastByZipCodeInUSARequestAsXML_thenReturnNotNull() { + final String forecastXml = getClient() + .forecast5Day3HourStep() + .byZipCodeInUSA("10005") + .language(Language.ENGLISH) + .unitSystem(UnitSystem.METRIC) + .retrieve() + .asXML(); + + Assert.assertTrue(forecastXml.startsWith("<")); + } + + @Test + public void whenGetForecastByZipCodeAndCountryCodeRequestAsJava_thenReturnNotNull() { + final Forecast forecast = getClient() + .forecast5Day3HourStep() + .byZipCodeAndCountry("220015", "by") + .language(Language.ENGLISH) + .unitSystem(UnitSystem.METRIC) + .count(15) + .retrieve() + .asJava(); + + Assert.assertNotNull(forecast); + Assert.assertNotNull(forecast.getLocation()); + Assert.assertNotNull(forecast.getWeatherForecasts()); + for (WeatherForecast weatherForecast : forecast.getWeatherForecasts()) { + Assert.assertNotNull(weatherForecast.getState()); + Assert.assertNotNull(weatherForecast.getDescription()); + Assert.assertNotNull(weatherForecast.getForecastTime()); + Assert.assertNotNull(weatherForecast.getTemperature()); + Assert.assertNotNull(weatherForecast.getAtmosphericPressure()); + Assert.assertNotNull(weatherForecast.getHumidity()); + Assert.assertNotNull(weatherForecast.getWind()); + } + } + + @Test + public void whenGetForecastByZipCodeAndCountryCodeRequestAsJSON_thenReturnNotNull() { + final String forecastJson = getClient() + .forecast5Day3HourStep() + .byZipCodeAndCountry("220015", "by") + .language(Language.SPANISH) + .unitSystem(UnitSystem.IMPERIAL) + .count(15) + .retrieve() + .asJSON(); + + Assert.assertTrue(forecastJson.startsWith("{")); + } + + @Test + public void whenGetForecastByZipCodeAndCountryCodeRequestAsXML_thenReturnNotNull() { + final String forecastXml = getClient() + .forecast5Day3HourStep() + .byZipCodeAndCountry("220015", "by") + .language(Language.ENGLISH) + .unitSystem(UnitSystem.METRIC) + .retrieve() + .asXML(); + + Assert.assertTrue(forecastXml.startsWith("<")); + } + + @Test + public void whenGetForecastByCityNameAsyncRequestAsJava_thenReturnNotNull() throws ExecutionException, InterruptedException { + final CompletableFuture forecastFuture = getClient() + .forecast5Day3HourStep() + .byCityName("Minsk") + .language(Language.ENGLISH) + .unitSystem(UnitSystem.METRIC) + .count(15) + .retrieveAsync() + .asJava(); + + Assert.assertNotNull(forecastFuture); + System.out.println(forecastFuture.get()); + } + + @Test + public void whenGetForecastByCityNameAsyncRequestAsJSON_thenReturnNotNull() throws ExecutionException, InterruptedException { + final CompletableFuture forecastFuture = getClient() + .forecast5Day3HourStep() + .byCityId(350001514) + .language(Language.ENGLISH) + .unitSystem(UnitSystem.METRIC) + .count(15) + .retrieveAsync() + .asJSON(); + + Assert.assertNotNull(forecastFuture); + System.out.println(forecastFuture.get()); + } + + @Test + public void whenGetForecastByCityNameAsyncRequestAsXML_thenReturnNotNull() throws ExecutionException, InterruptedException { + final CompletableFuture forecastFuture = getClient() + .forecast5Day3HourStep() + .byCityId(350001514) + .language(Language.ENGLISH) + .unitSystem(UnitSystem.METRIC) + .count(15) + .retrieveAsync() + .asXML(); + + Assert.assertNotNull(forecastFuture); + System.out.println(forecastFuture.get()); + } + + @Test(expected = InvalidAuthTokenException.class) + public void whenRequestCurrentWeatherWithInvalidApiKey_thenThrowAnException() { + OpenWeatherMapClient client = new OpenWeatherMapClient("invalidKey"); + client + .forecast5Day3HourStep() + .byCityId(350001514) + .retrieve() + .asJSON(); + } + + @Test(expected = NoDataFoundException.class) + public void whenRequestCurrentWeatherForInvalidLocation_thenThrowAnException() { + getClient() + .forecast5Day3HourStep() + .byCityName("invalidCity") + .retrieve() + .asJava(); } }