diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/OpenWeatherMapClient.java b/src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapClient.java similarity index 83% rename from src/main/java/com/github/prominence/openweathermap/api/request/OpenWeatherMapClient.java rename to src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapClient.java index b920160..6fc95c6 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/OpenWeatherMapClient.java +++ b/src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapClient.java @@ -20,8 +20,9 @@ * SOFTWARE. */ -package com.github.prominence.openweathermap.api.request; +package com.github.prominence.openweathermap.api; +import com.github.prominence.openweathermap.api.request.weather.CurrentWeatherRequester; import com.github.prominence.openweathermap.api.request.weather.CurrentWeatherRequesterImpl; public class OpenWeatherMapClient { @@ -32,7 +33,12 @@ public class OpenWeatherMapClient { this.apiKey = apiKey; } - public CurrentWeatherRequesterImpl currentWeather() { + public CurrentWeatherRequester currentWeather() { return new CurrentWeatherRequesterImpl(apiKey); } + + // TODO: + // * Forecast: hourly, daily + // * Air Pollution + // * Ultraviolet index } diff --git a/src/main/java/com/github/prominence/openweathermap/api/model/CoordinateRectangle.java b/src/main/java/com/github/prominence/openweathermap/api/model/CoordinateRectangle.java index 7aec701..d47ff57 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/model/CoordinateRectangle.java +++ b/src/main/java/com/github/prominence/openweathermap/api/model/CoordinateRectangle.java @@ -60,7 +60,7 @@ public class CoordinateRectangle { return latitudeTop; } - public String getFormattedString() { + public String getFormattedRequestString() { return longitudeLeft + "," + latitudeBottom + "," + longitudeRight + "," + latitudeTop; } @@ -82,6 +82,6 @@ public class CoordinateRectangle { @Override public String toString() { - return "Rectangle: " + getFormattedString(); + return "Rectangle: " + getFormattedRequestString(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/RequestUrlBuilder.java b/src/main/java/com/github/prominence/openweathermap/api/request/RequestUrlBuilder.java index f00dc3e..068a691 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/RequestUrlBuilder.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/RequestUrlBuilder.java @@ -22,6 +22,10 @@ package com.github.prominence.openweathermap.api.request; +import com.github.prominence.openweathermap.api.enums.Accuracy; +import com.github.prominence.openweathermap.api.enums.Language; +import com.github.prominence.openweathermap.api.enums.UnitSystem; + import java.util.HashMap; import java.util.Map; import java.util.stream.Collectors; @@ -45,8 +49,16 @@ public class RequestUrlBuilder { requestParameters.put(key, value); } - public void setAPIKey(String key) { - requestParameters.put(API_KEY_PARAM_NAME, key); + public void applyCustomization(Accuracy accuracy, Language language, UnitSystem unitSystem) { + if (accuracy != null) { + addRequestParameter("type", accuracy.getValue()); + } + if (language != null) { + addRequestParameter("lang", language.getValue()); + } + if (unitSystem != null && unitSystem != UnitSystem.STANDARD) { + addRequestParameter("units", unitSystem.getValue()); + } } public String buildUrl() { diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleLocationsCurrentWeatherRequesterImpl.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleLocationsCurrentWeatherRequesterImpl.java index 46e2121..c93f5ed 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleLocationsCurrentWeatherRequesterImpl.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleLocationsCurrentWeatherRequesterImpl.java @@ -36,7 +36,7 @@ public class MultipleLocationsCurrentWeatherRequesterImpl implements MultipleLoc @Override public MultipleResultCurrentWeatherRequestCustomizer byRectangle(CoordinateRectangle rectangle, int zoom) { - String coordinates = rectangle.getFormattedString() + "," + zoom; + String coordinates = rectangle.getFormattedRequestString() + "," + zoom; urlBuilder.append("box/city"); urlBuilder.addRequestParameter("bbox", coordinates); @@ -45,7 +45,7 @@ public class MultipleLocationsCurrentWeatherRequesterImpl implements MultipleLoc @Override public MultipleResultCurrentWeatherRequestCustomizer byRectangle(CoordinateRectangle rectangle, int zoom, boolean useServerClustering) { - String coordinates = rectangle.getFormattedString() + "," + zoom; + String coordinates = rectangle.getFormattedRequestString() + "," + zoom; urlBuilder.append("box/city"); urlBuilder.addRequestParameter("bbox", coordinates); urlBuilder.addRequestParameter("cluster", useServerClustering ? "yes" : "no"); diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherRequestCustomizerImpl.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherRequestCustomizerImpl.java index 5d577bb..a65eddd 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherRequestCustomizerImpl.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherRequestCustomizerImpl.java @@ -41,13 +41,13 @@ public class MultipleResultCurrentWeatherRequestCustomizerImpl implements Multip @Override public MultipleResultCurrentWeatherRequestTerminator retrieve() { - applyCustomization(); + urlBuilder.applyCustomization(accuracy, language, unitSystem); return new MultipleResultCurrentWeatherRequestTerminatorImpl(urlBuilder, unitSystem); } @Override public MultipleResultCurrentWeatherAsyncRequestTerminator retrieveAsync() { - applyCustomization(); + urlBuilder.applyCustomization(accuracy, language, unitSystem); return new MultipleResultCurrentWeatherAsyncRequestTerminatorImpl(urlBuilder, unitSystem); } @@ -68,16 +68,4 @@ public class MultipleResultCurrentWeatherRequestCustomizerImpl implements Multip this.unitSystem = unitSystem; return this; } - - private void applyCustomization() { - if (accuracy != null) { - urlBuilder.addRequestParameter("type", accuracy.getValue()); - } - if (language != null) { - urlBuilder.addRequestParameter("lang", language.getValue()); - } - if (unitSystem != null && unitSystem != UnitSystem.STANDARD) { - urlBuilder.addRequestParameter("units", unitSystem.getValue()); - } - } } \ No newline at end of file diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleLocationCurrentWeatherRequesterImpl.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleLocationCurrentWeatherRequesterImpl.java index a95c6ad..6c8bc14 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleLocationCurrentWeatherRequesterImpl.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleLocationCurrentWeatherRequesterImpl.java @@ -34,28 +34,28 @@ public class SingleLocationCurrentWeatherRequesterImpl implements SingleLocation urlBuilder.append("weather"); } - public SingleResultCurrentWeatherRequestCustomizerImpl byCityName(String cityName) { + public SingleResultCurrentWeatherRequestCustomizer byCityName(String cityName) { urlBuilder.addRequestParameter("q", cityName); return new SingleResultCurrentWeatherRequestCustomizerImpl(urlBuilder); } - public SingleResultCurrentWeatherRequestCustomizerImpl byCityName(String cityName, String countryCode) { + public SingleResultCurrentWeatherRequestCustomizer byCityName(String cityName, String countryCode) { urlBuilder.addRequestParameter("q", cityName + "," + countryCode); return new SingleResultCurrentWeatherRequestCustomizerImpl(urlBuilder); } - public SingleResultCurrentWeatherRequestCustomizerImpl byCityId(long cityId) { + public SingleResultCurrentWeatherRequestCustomizer byCityId(long cityId) { urlBuilder.addRequestParameter("id", cityId); return new SingleResultCurrentWeatherRequestCustomizerImpl(urlBuilder); } - public SingleResultCurrentWeatherRequestCustomizerImpl byCoordinate(Coordinate coordinate) { + public SingleResultCurrentWeatherRequestCustomizer byCoordinate(Coordinate coordinate) { urlBuilder.addRequestParameter("lat", String.valueOf(coordinate.getLatitude())); urlBuilder.addRequestParameter("lon", String.valueOf(coordinate.getLongitude())); return new SingleResultCurrentWeatherRequestCustomizerImpl(urlBuilder); } - public SingleResultCurrentWeatherRequestCustomizerImpl byZipCodeAndCountry(String zipCode, String countryCode) { + public SingleResultCurrentWeatherRequestCustomizer byZipCodeAndCountry(String zipCode, String countryCode) { urlBuilder.addRequestParameter("zip", zipCode + "," + countryCode); return new SingleResultCurrentWeatherRequestCustomizerImpl(urlBuilder); } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherRequestCustomizerImpl.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherRequestCustomizerImpl.java index 5aceadd..3fe71b8 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherRequestCustomizerImpl.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherRequestCustomizerImpl.java @@ -41,13 +41,13 @@ public class SingleResultCurrentWeatherRequestCustomizerImpl implements SingleRe @Override public SingleResultCurrentWeatherRequestTerminator retrieve() { - applyCustomization(); + urlBuilder.applyCustomization(accuracy, language, unitSystem); return new SingleResultCurrentWeatherRequestTerminatorImpl(urlBuilder, unitSystem); } @Override public SingleResultCurrentWeatherAsyncRequestTerminator retrieveAsync() { - applyCustomization(); + urlBuilder.applyCustomization(accuracy, language, unitSystem); return new SingleResultCurrentWeatherAsyncRequestTerminatorImpl(urlBuilder, unitSystem); } @@ -68,16 +68,4 @@ public class SingleResultCurrentWeatherRequestCustomizerImpl implements SingleRe this.unitSystem = unitSystem; return this; } - - private void applyCustomization() { - if (accuracy != null) { - urlBuilder.addRequestParameter("type", accuracy.getValue()); - } - if (language != null) { - urlBuilder.addRequestParameter("lang", language.getValue()); - } - if (unitSystem != null && unitSystem != UnitSystem.STANDARD) { - urlBuilder.addRequestParameter("units", unitSystem.getValue()); - } - } } \ No newline at end of file diff --git a/src/main/java/com/github/prominence/openweathermap/api/utils/RequestUtils.java b/src/main/java/com/github/prominence/openweathermap/api/utils/RequestUtils.java index 5ee37cc..974bc21 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/utils/RequestUtils.java +++ b/src/main/java/com/github/prominence/openweathermap/api/utils/RequestUtils.java @@ -124,5 +124,4 @@ public final class RequestUtils { return result.toString(); } - } diff --git a/src/test/java/com/github/prominence/openweathermap/api/ApiTest.java b/src/test/java/com/github/prominence/openweathermap/api/ApiTest.java index 8f66ebd..767ee05 100644 --- a/src/test/java/com/github/prominence/openweathermap/api/ApiTest.java +++ b/src/test/java/com/github/prominence/openweathermap/api/ApiTest.java @@ -22,7 +22,6 @@ package com.github.prominence.openweathermap.api; -import com.github.prominence.openweathermap.api.request.OpenWeatherMapClient; import org.junit.BeforeClass; public class ApiTest { diff --git a/src/test/java/com/github/prominence/openweathermap/api/CurrentWeatherIntegrationTest.java b/src/test/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherIntegrationTest.java similarity index 98% rename from src/test/java/com/github/prominence/openweathermap/api/CurrentWeatherIntegrationTest.java rename to src/test/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherIntegrationTest.java index 86aae0f..24523a9 100644 --- a/src/test/java/com/github/prominence/openweathermap/api/CurrentWeatherIntegrationTest.java +++ b/src/test/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherIntegrationTest.java @@ -20,14 +20,15 @@ * SOFTWARE. */ -package com.github.prominence.openweathermap.api; +package com.github.prominence.openweathermap.api.request.weather; +import com.github.prominence.openweathermap.api.ApiTest; import com.github.prominence.openweathermap.api.enums.Accuracy; import com.github.prominence.openweathermap.api.enums.Language; import com.github.prominence.openweathermap.api.enums.UnitSystem; import com.github.prominence.openweathermap.api.exception.NoDataFoundException; import com.github.prominence.openweathermap.api.exception.InvalidAuthTokenException; -import com.github.prominence.openweathermap.api.request.OpenWeatherMapClient; +import com.github.prominence.openweathermap.api.OpenWeatherMapClient; import com.github.prominence.openweathermap.api.model.Coordinate; import com.github.prominence.openweathermap.api.model.CoordinateRectangle; import com.github.prominence.openweathermap.api.model.Weather; diff --git a/src/test/java/com/github/prominence/openweathermap/api/request/weather/multiple/CurrentWeatherIntegrationTest.java b/src/test/java/com/github/prominence/openweathermap/api/request/weather/multiple/CurrentWeatherIntegrationTest.java new file mode 100644 index 0000000..2bf55e2 --- /dev/null +++ b/src/test/java/com/github/prominence/openweathermap/api/request/weather/multiple/CurrentWeatherIntegrationTest.java @@ -0,0 +1,236 @@ +/* + * Copyright (c) 2019 Alexey Zinchenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.github.prominence.openweathermap.api.request.weather.multiple; + +import com.github.prominence.openweathermap.api.ApiTest; +import com.github.prominence.openweathermap.api.enums.Accuracy; +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.CoordinateRectangle; +import com.github.prominence.openweathermap.api.model.Weather; +import com.github.prominence.openweathermap.api.OpenWeatherMapClient; +import org.junit.Test; + +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +public class CurrentWeatherIntegrationTest extends ApiTest { + + @Test + public void whenGetMultipleCurrentWeatherByCoordinateRequestAsJava_thenReturnNotNull() { + final List weatherList = getClient() + .currentWeather() + .multiple() + .byRectangle(new CoordinateRectangle(12, 32, 15, 37), 10) + .accuracy(Accuracy.ACCURATE) + .language(Language.ROMANIAN) + .unitSystem(UnitSystem.METRIC) + .retrieve() + .asJava(); + + assert weatherList != null; + assert weatherList.size() > 0; + System.out.println(weatherList); + } + + @Test + public void whenGetMultipleCurrentWeatherByCoordinateAndServerClusteringRequestAsJava_thenReturnNotNull() { + final List weatherList = getClient() + .currentWeather() + .multiple() + .byRectangle(new CoordinateRectangle(12, 32, 15, 37), 10, true) + .accuracy(Accuracy.ACCURATE) + .language(Language.ROMANIAN) + .unitSystem(UnitSystem.METRIC) + .retrieve() + .asJava(); + + assert weatherList != null; + assert weatherList.size() > 0; + System.out.println(weatherList); + } + + @Test + public void whenGetMultipleCurrentWeatherByCitiesInCycleRequestAsJava_thenReturnNotNull() { + final List weatherList = getClient() + .currentWeather() + .multiple() + .byCitiesInCycle(new Coordinate(55.5, 37.5), 10) + .language(Language.GERMAN) + .unitSystem(UnitSystem.IMPERIAL) + .retrieve() + .asJava(); + + assert weatherList != null; + assert weatherList.size() > 0; + System.out.println(weatherList); + } + + @Test + public void whenGetMultipleCurrentWeatherByCitiesInCycleAndServerClusteringRequestAsJava_thenReturnNotNull() { + final List weatherList = getClient() + .currentWeather() + .multiple() + .byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true) + .language(Language.GERMAN) + .unitSystem(UnitSystem.IMPERIAL) + .retrieve() + .asJava(); + + assert weatherList != null; + assert weatherList.size() > 0; + System.out.println(weatherList); + } + + @Test + public void whenGetMultipleCurrentWeatherByCitiesInCycleRequestAsJson_thenReturnNotNull() { + final String weather = getClient() + .currentWeather() + .multiple() + .byCitiesInCycle(new Coordinate(55.5, 37.5), 10) + .language(Language.GERMAN) + .unitSystem(UnitSystem.IMPERIAL) + .retrieve() + .asJSON(); + + assert weather != null; + System.out.println(weather); + } + + @Test + public void whenGetMultipleCurrentWeatherByCitiesInCycleRequestAsXml_thenReturnNotNull() { + final String weather = getClient() + .currentWeather() + .multiple() + .byCitiesInCycle(new Coordinate(55.5, 37.5), 10) + .language(Language.GERMAN) + .unitSystem(UnitSystem.IMPERIAL) + .retrieve() + .asXML(); + + assert weather != null; + System.out.println(weather); + } + + @Test + public void whenGetMultipleCurrentWeatherByCitiesInCycleRequestAsHtml_thenReturnNotNull() { + final String weather = getClient() + .currentWeather() + .multiple() + .byCitiesInCycle(new Coordinate(55.5, 37.5), 10) + .language(Language.GERMAN) + .unitSystem(UnitSystem.IMPERIAL) + .retrieve() + .asHTML(); + + assert weather != null; + System.out.println(weather); + } + + @Test + public void whenGetMultipleCurrentWeatherByCoordinateAndServerClusteringAsyncRequestAsJava_thenReturnNotNull() throws ExecutionException, InterruptedException { + final CompletableFuture> weatherListFuture = getClient() + .currentWeather() + .multiple() + .byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true) + .language(Language.GERMAN) + .unitSystem(UnitSystem.IMPERIAL) + .retrieveAsync() + .asJava(); + + assert weatherListFuture != null; + List weatherList = weatherListFuture.get(); + assert weatherList.size() > 0; + System.out.println(weatherList); + } + + @Test + public void whenGetMultipleCurrentWeatherByCoordinateAndServerClusteringAsyncRequestAsXml_thenReturnNotNull() throws ExecutionException, InterruptedException { + final CompletableFuture weatherFuture = getClient() + .currentWeather() + .multiple() + .byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true) + .language(Language.GERMAN) + .unitSystem(UnitSystem.IMPERIAL) + .retrieveAsync() + .asXML(); + + assert weatherFuture != null; + System.out.println(weatherFuture.get()); + } + + @Test + public void whenGetMultipleCurrentWeatherByCoordinateAndServerClusteringAsyncRequestAsJson_thenReturnNotNull() throws ExecutionException, InterruptedException { + final CompletableFuture weatherFuture = getClient() + .currentWeather() + .multiple() + .byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true) + .language(Language.GERMAN) + .unitSystem(UnitSystem.IMPERIAL) + .retrieveAsync() + .asJSON(); + + assert weatherFuture != null; + System.out.println(weatherFuture.get()); + } + + @Test + public void whenGetMultipleCurrentWeatherByCoordinateAndServerClusteringAsyncRequestAsHtml_thenReturnNotNull() throws ExecutionException, InterruptedException { + final CompletableFuture weatherFuture = getClient() + .currentWeather() + .multiple() + .byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true) + .language(Language.GERMAN) + .unitSystem(UnitSystem.IMPERIAL) + .retrieveAsync() + .asHTML(); + + assert weatherFuture != null; + System.out.println(weatherFuture.get()); + } + + @Test(expected = InvalidAuthTokenException.class) + public void whenRequestCurrentWeatherWithInvalidApiKey_thenThrowAnException() { + OpenWeatherMapClient client = new OpenWeatherMapClient("invalidKey"); + client + .currentWeather() + .single() + .byCityName("London") + .retrieve() + .asJSON(); + } + + @Test(expected = NoDataFoundException.class) + public void whenRequestCurrentWeatherForInvalidLocation_thenThrowAnException() { + getClient() + .currentWeather() + .single() + .byCityName("InvalidCity") + .retrieve() + .asJava(); + } +} diff --git a/src/test/java/com/github/prominence/openweathermap/api/request/weather/single/CurrentWeatherIntegrationTest.java b/src/test/java/com/github/prominence/openweathermap/api/request/weather/single/CurrentWeatherIntegrationTest.java new file mode 100644 index 0000000..e73a47e --- /dev/null +++ b/src/test/java/com/github/prominence/openweathermap/api/request/weather/single/CurrentWeatherIntegrationTest.java @@ -0,0 +1,240 @@ +/* + * Copyright (c) 2019 Alexey Zinchenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.github.prominence.openweathermap.api.request.weather.single; + +import com.github.prominence.openweathermap.api.ApiTest; +import com.github.prominence.openweathermap.api.enums.Accuracy; +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.Weather; +import com.github.prominence.openweathermap.api.OpenWeatherMapClient; +import org.junit.Test; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +public class CurrentWeatherIntegrationTest extends ApiTest { + + @Test + public void whenGetSingleCurrentWeatherByCoordinateRequestAsJava_thenReturnNotNull() { + final Weather weather = getClient() + .currentWeather() + .single() + .byCoordinate(new Coordinate(5, 5)) + .accuracy(Accuracy.ACCURATE) + .unitSystem(UnitSystem.METRIC) + .retrieve() + .asJava(); + + assert weather != null; + System.out.println(weather); + } + + @Test + public void whenGetSingleCurrentWeatherByCityIdRequestAsJava_thenReturnNotNull() { + final Weather weather = getClient() + .currentWeather() + .single() + .byCityId(350001514) + .language(Language.GERMAN) + .retrieve() + .asJava(); + + assert weather != null; + System.out.println(weather); + } + + @Test + public void whenGetSingleCurrentWeatherByCityNameRequestAsJava_thenReturnNotNull() { + final Weather weather = getClient() + .currentWeather() + .single() + .byCityName("Minsk") + .language(Language.RUSSIAN) + .unitSystem(UnitSystem.METRIC) + .retrieve() + .asJava(); + + assert weather != null; + System.out.println(weather); + } + + @Test + public void whenGetSingleCurrentWeatherByCityNameAndCountryCodeRequestAsJava_thenReturnNotNull() { + final Weather weather = getClient() + .currentWeather() + .single() + .byCityName("Moscow", "ru") + .language(Language.RUSSIAN) + .unitSystem(UnitSystem.METRIC) + .retrieve() + .asJava(); + + assert weather != null; + System.out.println(weather); + } + + @Test + public void whenGetSingleCurrentWeatherByZipCodeAndCountryRequestAsJava_thenReturnNotNull() { + final Weather weather = getClient() + .currentWeather() + .single() + .byZipCodeAndCountry("220015", "by") + .language(Language.RUSSIAN) + .unitSystem(UnitSystem.METRIC) + .retrieve() + .asJava(); + + assert weather != null; + System.out.println(weather); + } + + @Test + public void whenGetAnySingleCurrentRequestWeatherAsJson_thenReturnNotNull() { + final String weatherJson = getClient() + .currentWeather() + .single() + .byZipCodeAndCountry("220015", "by") + .language(Language.RUSSIAN) + .unitSystem(UnitSystem.METRIC) + .retrieve() + .asJSON(); + + assert weatherJson != null; + System.out.println(weatherJson); + } + + @Test + public void whenGetAnySingleCurrentRequestWeatherAsXml_thenReturnNotNull() { + final String weatherXml = getClient() + .currentWeather() + .single() + .byZipCodeAndCountry("220015", "by") + .language(Language.RUSSIAN) + .unitSystem(UnitSystem.METRIC) + .retrieve() + .asXML(); + + assert weatherXml != null; + System.out.println(weatherXml); + } + + @Test + public void whenGetAnySingleCurrentWeatherRequestAsHtml_thenReturnNotNull() { + final String weatherHtml = getClient() + .currentWeather() + .single() + .byZipCodeAndCountry("220015", "by") + .language(Language.RUSSIAN) + .unitSystem(UnitSystem.METRIC) + .retrieve() + .asHTML(); + + assert weatherHtml != null; + System.out.println(weatherHtml); + } + + @Test + public void whenGetAnySingleCurrentWeatherAsyncRequestAsXml_thenReturnNotNull() throws ExecutionException, InterruptedException { + final CompletableFuture weatherXmlFuture = getClient() + .currentWeather() + .single() + .byZipCodeAndCountry("220015", "by") + .language(Language.RUSSIAN) + .unitSystem(UnitSystem.METRIC) + .retrieveAsync() + .asXML(); + + assert weatherXmlFuture != null; + System.out.println(weatherXmlFuture.get()); + } + + @Test + public void whenGetAnySingleCurrentWeatherAsyncRequestAsJava_thenReturnNotNull() throws ExecutionException, InterruptedException { + final CompletableFuture weatherFuture = getClient() + .currentWeather() + .single() + .byZipCodeAndCountry("220015", "by") + .language(Language.RUSSIAN) + .unitSystem(UnitSystem.METRIC) + .retrieveAsync() + .asJava(); + + assert weatherFuture != null; + System.out.println(weatherFuture.get()); + } + + @Test + public void whenGetAnySingleCurrentWeatherAsyncRequestAsJson_thenReturnNotNull() throws ExecutionException, InterruptedException { + final CompletableFuture weatherFuture = getClient() + .currentWeather() + .single() + .byZipCodeAndCountry("220015", "by") + .language(Language.RUSSIAN) + .unitSystem(UnitSystem.METRIC) + .retrieveAsync() + .asJSON(); + + assert weatherFuture != null; + System.out.println(weatherFuture.get()); + } + + @Test + public void whenGetAnySingleCurrentWeatherAsyncRequestAsHtml_thenReturnNotNull() throws ExecutionException, InterruptedException { + final CompletableFuture weatherFuture = getClient() + .currentWeather() + .single() + .byZipCodeAndCountry("220015", "by") + .language(Language.RUSSIAN) + .unitSystem(UnitSystem.METRIC) + .retrieveAsync() + .asHTML(); + + assert weatherFuture != null; + System.out.println(weatherFuture.get()); + } + + @Test(expected = InvalidAuthTokenException.class) + public void whenRequestCurrentWeatherWithInvalidApiKey_thenThrowAnException() { + OpenWeatherMapClient client = new OpenWeatherMapClient("invalidKey"); + client + .currentWeather() + .multiple() + .byCitiesInCycle(new Coordinate(34.53, 66.74), 10) + .retrieve() + .asJSON(); + } + + @Test(expected = NoDataFoundException.class) + public void whenRequestCurrentWeatherForInvalidLocation_thenThrowAnException() { + getClient() + .currentWeather() + .multiple() + .byCitiesInCycle(new Coordinate(90.00, 66.74), 10) + .retrieve() + .asJava(); + } +}