diff --git a/pom.xml b/pom.xml index 92deb02..c291a2e 100644 --- a/pom.xml +++ b/pom.xml @@ -72,7 +72,7 @@ 3.1.0 - ApplicationTest.class + **/test/* @@ -143,6 +143,12 @@ 1.18.0 provided - + + junit + junit + 4.12 + test + + \ No newline at end of file diff --git a/src/main/java/com/github/prominence/openweathermap/api/AirPollutionRequester.java b/src/main/java/com/github/prominence/openweathermap/api/AirPollutionRequester.java index fea1f27..8aaea2c 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/AirPollutionRequester.java +++ b/src/main/java/com/github/prominence/openweathermap/api/AirPollutionRequester.java @@ -24,13 +24,11 @@ package com.github.prominence.openweathermap.api; import com.github.prominence.openweathermap.api.constants.TimeFrame; -import com.github.prominence.openweathermap.api.exception.DataNotFoundException; -import com.github.prominence.openweathermap.api.exception.InvalidAuthTokenException; -import com.github.prominence.openweathermap.api.model.Coordinates; import com.github.prominence.openweathermap.api.model.response.AirPollution; +import com.github.prominence.openweathermap.api.model.Coordinates; +import com.github.prominence.openweathermap.api.utils.TimeFrameUtils; import com.github.prominence.openweathermap.api.utils.JSONUtils; import com.github.prominence.openweathermap.api.utils.RequestUtils; -import com.github.prominence.openweathermap.api.utils.TimeFrameUtils; import java.io.IOException; import java.io.InputStream; @@ -44,8 +42,11 @@ public class AirPollutionRequester extends AuthenticationTokenBasedRequester { private TimeFrame timeFrame; private Date date; - AirPollutionRequester(String authToken) { + AirPollutionRequester(String authToken, float latitude, float longitude, Date date, TimeFrame timeFrame) { super(authToken); + this.coordinates = new Coordinates(latitude, longitude); + this.date = date; + this.timeFrame = timeFrame; } public AirPollutionRequester setCoordinates(Coordinates coordinates) { @@ -68,15 +69,16 @@ public class AirPollutionRequester extends AuthenticationTokenBasedRequester { return this; } - public AirPollution retrieve() throws InvalidAuthTokenException, DataNotFoundException { + public AirPollution retrieve() { if (coordinates == null || timeFrame == null || date == null) { - throw new IllegalArgumentException("You must execute 'setCoordinates', 'setTimeFrame' and 'setDate' and least once."); + throw new IllegalArgumentException("Coordinates, date or time frame weren't set."); } String requestParameters = String.format("%s,%s/%s.json", coordinates.getLatitude(), coordinates.getLongitude(), TimeFrameUtils.formatDate(date, timeFrame)); AirPollution airPollution = null; + // currently only CO try (InputStream response = executeRequest("pollution/v1/co/", requestParameters)) { airPollution = (AirPollution) JSONUtils.parseJSON(response, AirPollution.class); } catch (IOException ex) { @@ -86,13 +88,20 @@ public class AirPollutionRequester extends AuthenticationTokenBasedRequester { return airPollution; } - private InputStream executeRequest(String requestType, String requestSpecificParameters) throws MalformedURLException, InvalidAuthTokenException, DataNotFoundException { + private InputStream executeRequest(String requestType, String requestSpecificParameters) { String url = OPEN_WEATHER_BASE_URL + requestType + requestSpecificParameters + "?appid=" + authToken; - return RequestUtils.executeGetRequest(new URL(url)); + InputStream getRequest = null; + try { + getRequest = RequestUtils.executeGetRequest(new URL(url)); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + + return getRequest; } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/BasicRequester.java b/src/main/java/com/github/prominence/openweathermap/api/BasicRequester.java index a4bd14f..e8e6437 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/BasicRequester.java +++ b/src/main/java/com/github/prominence/openweathermap/api/BasicRequester.java @@ -23,8 +23,6 @@ package com.github.prominence.openweathermap.api; import com.github.prominence.openweathermap.api.constants.Unit; -import com.github.prominence.openweathermap.api.exception.DataNotFoundException; -import com.github.prominence.openweathermap.api.exception.InvalidAuthTokenException; import com.github.prominence.openweathermap.api.model.Coordinates; import java.net.MalformedURLException; @@ -41,27 +39,27 @@ abstract class BasicRequester extends AuthenticationTokenBasedRequester { super(authToken); } - public T getByCityId(String id) throws InvalidAuthTokenException, DataNotFoundException { + public T getByCityId(String id) { return executeRequest("?id=" + id); } - public T getByCityName(String name) throws InvalidAuthTokenException, DataNotFoundException { + public T getByCityName(String name) { return executeRequest("?q=" + name); } - public T getByCoordinates(double latitude, double longitude) throws InvalidAuthTokenException, DataNotFoundException { + public T getByCoordinates(double latitude, double longitude) { return executeRequest("?lat=" + latitude + "&lon=" + longitude); } - public T getByCoordinates(Coordinates coordinates) throws InvalidAuthTokenException, DataNotFoundException { + public T getByCoordinates(Coordinates coordinates) { return getByCoordinates(coordinates.getLatitude(), coordinates.getLongitude()); } - public T getByZIPCode(String zipCode, String countryCode) throws InvalidAuthTokenException, DataNotFoundException { + public T getByZIPCode(String zipCode, String countryCode) { return executeRequest("?zip=" + zipCode + "," + countryCode); } - protected URL buildURL(String requestSpecificParameters) throws MalformedURLException { + protected URL buildURL(String requestSpecificParameters) { StringBuilder urlBuilder = new StringBuilder(OPEN_WEATHER_API_URL); urlBuilder.append(getRequestType()); @@ -96,7 +94,14 @@ abstract class BasicRequester extends AuthenticationTokenBasedRequester { }); } - return new URL(urlBuilder.toString()); + URL url = null; + try { + url = new URL(urlBuilder.toString()); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + + return url; } protected Map getAdditionalParameters() { @@ -104,6 +109,7 @@ abstract class BasicRequester extends AuthenticationTokenBasedRequester { } protected abstract String getRequestType(); - protected abstract T executeRequest(String requestSpecificParameters) throws InvalidAuthTokenException, DataNotFoundException; + + protected abstract T executeRequest(String requestSpecificParameters); } diff --git a/src/main/java/com/github/prominence/openweathermap/api/DailyForecastRequester.java b/src/main/java/com/github/prominence/openweathermap/api/DailyForecastRequester.java index 9ea8f0b..27dd954 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/DailyForecastRequester.java +++ b/src/main/java/com/github/prominence/openweathermap/api/DailyForecastRequester.java @@ -23,8 +23,6 @@ package com.github.prominence.openweathermap.api; import com.github.prominence.openweathermap.api.constants.Unit; -import com.github.prominence.openweathermap.api.exception.DataNotFoundException; -import com.github.prominence.openweathermap.api.exception.InvalidAuthTokenException; import com.github.prominence.openweathermap.api.model.response.DailyForecast; import com.github.prominence.openweathermap.api.utils.JSONUtils; import com.github.prominence.openweathermap.api.utils.RequestUtils; @@ -64,12 +62,12 @@ public class DailyForecastRequester extends BasicRequester { } @Override - protected DailyForecast executeRequest(String requestSpecificParameters) throws InvalidAuthTokenException, DataNotFoundException { + protected DailyForecast executeRequest(String requestSpecificParameters) { DailyForecast forecastResponse = null; try { InputStream requestResult = RequestUtils.executeGetRequest(buildURL(requestSpecificParameters)); - forecastResponse = (DailyForecast)JSONUtils.parseJSON(requestResult, DailyForecast.class); + forecastResponse = (DailyForecast) JSONUtils.parseJSON(requestResult, DailyForecast.class); char temperatureUnit = Unit.getTemperatureUnit(unitSystem); String windUnit = Unit.getWindUnit(unitSystem); diff --git a/src/main/java/com/github/prominence/openweathermap/api/HourlyForecastRequester.java b/src/main/java/com/github/prominence/openweathermap/api/HourlyForecastRequester.java index 220684e..9e3757b 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/HourlyForecastRequester.java +++ b/src/main/java/com/github/prominence/openweathermap/api/HourlyForecastRequester.java @@ -23,8 +23,6 @@ package com.github.prominence.openweathermap.api; import com.github.prominence.openweathermap.api.constants.Unit; -import com.github.prominence.openweathermap.api.exception.DataNotFoundException; -import com.github.prominence.openweathermap.api.exception.InvalidAuthTokenException; import com.github.prominence.openweathermap.api.model.response.HourlyForecast; import com.github.prominence.openweathermap.api.utils.JSONUtils; import com.github.prominence.openweathermap.api.utils.RequestUtils; @@ -57,13 +55,13 @@ public class HourlyForecastRequester extends BasicRequester { return "forecast"; } - protected HourlyForecast executeRequest(String requestSpecificParameters) throws InvalidAuthTokenException, DataNotFoundException { + protected HourlyForecast executeRequest(String requestSpecificParameters) { HourlyForecast forecastResponse = null; try { InputStream requestResult = RequestUtils.executeGetRequest(buildURL(requestSpecificParameters)); - forecastResponse = (HourlyForecast)JSONUtils.parseJSON(requestResult, HourlyForecast.class); + forecastResponse = (HourlyForecast) JSONUtils.parseJSON(requestResult, HourlyForecast.class); char temperatureUnit = Unit.getTemperatureUnit(unitSystem); String windUnit = Unit.getWindUnit(unitSystem); diff --git a/src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapManager.java b/src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapManager.java index f92a55c..e2fe544 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapManager.java +++ b/src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapManager.java @@ -22,6 +22,11 @@ package com.github.prominence.openweathermap.api; +import com.github.prominence.openweathermap.api.constants.TimeFrame; +import com.github.prominence.openweathermap.api.model.Coordinates; + +import java.util.Date; + public class OpenWeatherMapManager { private String authToken; @@ -42,11 +47,19 @@ public class OpenWeatherMapManager { return new DailyForecastRequester(authToken); } - public UltravioletIndexRequester getUltravioletIndexRequester() { - return new UltravioletIndexRequester(authToken); + public UltravioletIndexRequester getUltravioletIndexRequester(float latitude, float longitude) { + return new UltravioletIndexRequester(authToken, latitude, longitude); } - public AirPollutionRequester getAirPollutionRequester() { - return new AirPollutionRequester(authToken); + public UltravioletIndexRequester getUltravioletIndexRequester(Coordinates coordinates) { + return new UltravioletIndexRequester(authToken, coordinates.getLatitude(), coordinates.getLongitude()); + } + + public AirPollutionRequester getAirPollutionRequester(float latitude, float longitude, Date date, TimeFrame timeFrame) { + return new AirPollutionRequester(authToken, latitude, longitude, date, timeFrame); + } + + public AirPollutionRequester getAirPollutionRequester(Coordinates coordinates, Date date, TimeFrame timeFrame) { + return new AirPollutionRequester(authToken, coordinates.getLatitude(), coordinates.getLongitude(), date, timeFrame); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/UltravioletIndexRequester.java b/src/main/java/com/github/prominence/openweathermap/api/UltravioletIndexRequester.java index fc98e4b..bf0fdb6 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/UltravioletIndexRequester.java +++ b/src/main/java/com/github/prominence/openweathermap/api/UltravioletIndexRequester.java @@ -23,10 +23,8 @@ package com.github.prominence.openweathermap.api; import com.alibaba.fastjson.TypeReference; -import com.github.prominence.openweathermap.api.exception.DataNotFoundException; -import com.github.prominence.openweathermap.api.exception.InvalidAuthTokenException; -import com.github.prominence.openweathermap.api.model.Coordinates; import com.github.prominence.openweathermap.api.model.response.UltravioletIndex; +import com.github.prominence.openweathermap.api.model.Coordinates; import com.github.prominence.openweathermap.api.utils.JSONUtils; import com.github.prominence.openweathermap.api.utils.RequestUtils; @@ -41,8 +39,9 @@ public class UltravioletIndexRequester extends AuthenticationTokenBasedRequester private Coordinates coordinates; - UltravioletIndexRequester(String authToken) { + UltravioletIndexRequester(String authToken, float latitude, float longitude) { super(authToken); + this.coordinates = new Coordinates(latitude, longitude); } public UltravioletIndexRequester setCoordinates(Coordinates coordinates) { @@ -55,22 +54,22 @@ public class UltravioletIndexRequester extends AuthenticationTokenBasedRequester return this; } - public UltravioletIndex getCurrentUVIndex() throws InvalidAuthTokenException, DataNotFoundException { + public UltravioletIndex getCurrentUVIndex() { String requestParameters = String.format("lat=%f&lon=%f", coordinates.getLatitude(), coordinates.getLongitude()); return getSingleObject(requestParameters); } - public List getUVIndexForecast(int amountOfDays) throws InvalidAuthTokenException, DataNotFoundException { + public List getUVIndexForecast(int amountOfDays) { String requestParameters = String.format("lat=%f&lon=%f&cnt=%d", coordinates.getLatitude(), coordinates.getLongitude(), amountOfDays); - return getListOfObjects(requestParameters); + return getListOfObjects(requestParameters); } - public List getUVIndexByPeriod(Date from, Date to) throws InvalidAuthTokenException, DataNotFoundException { + public List getUVIndexByPeriod(Date from, Date to) { String requestParameters = String.format("lat=%f&lon=%f&start=%d&end=%d", coordinates.getLatitude(), coordinates.getLongitude(), from.getTime() / 1000, to.getTime() / 1000); return getListOfObjects(requestParameters); } - private UltravioletIndex getSingleObject(String requestParameters) throws InvalidAuthTokenException, DataNotFoundException { + private UltravioletIndex getSingleObject(String requestParameters) { UltravioletIndex ultravioletIndex = null; try (InputStream response = executeRequest("uvi", requestParameters)) { @@ -82,10 +81,11 @@ public class UltravioletIndexRequester extends AuthenticationTokenBasedRequester return ultravioletIndex; } - private List getListOfObjects(String requestParameters) throws InvalidAuthTokenException, DataNotFoundException { + private List getListOfObjects(String requestParameters) { List ultravioletIndex = null; - TypeReference> typeRef = new TypeReference>() {}; + TypeReference> typeRef = new TypeReference>() { + }; try (InputStream response = executeRequest("uvi/forecast", requestParameters)) { ultravioletIndex = (List) JSONUtils.parseJSON(response, typeRef); @@ -96,7 +96,7 @@ public class UltravioletIndexRequester extends AuthenticationTokenBasedRequester return ultravioletIndex; } - private InputStream executeRequest(String requestType, String requestSpecificParameters) throws MalformedURLException, InvalidAuthTokenException, DataNotFoundException { + private InputStream executeRequest(String requestType, String requestSpecificParameters) { StringBuilder urlBuilder = new StringBuilder(OPEN_WEATHER_API_URL); urlBuilder.append(requestType); @@ -106,7 +106,14 @@ public class UltravioletIndexRequester extends AuthenticationTokenBasedRequester urlBuilder.append("&appid="); urlBuilder.append(authToken); - return RequestUtils.executeGetRequest(new URL(urlBuilder.toString())); + InputStream getRequest = null; + try { + getRequest = RequestUtils.executeGetRequest(new URL(urlBuilder.toString())); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + + return getRequest; } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/WeatherRequester.java b/src/main/java/com/github/prominence/openweathermap/api/WeatherRequester.java index e874cb8..6ed4c0c 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/WeatherRequester.java +++ b/src/main/java/com/github/prominence/openweathermap/api/WeatherRequester.java @@ -23,8 +23,6 @@ package com.github.prominence.openweathermap.api; import com.github.prominence.openweathermap.api.constants.Unit; -import com.github.prominence.openweathermap.api.exception.DataNotFoundException; -import com.github.prominence.openweathermap.api.exception.InvalidAuthTokenException; import com.github.prominence.openweathermap.api.model.response.Weather; import com.github.prominence.openweathermap.api.utils.JSONUtils; import com.github.prominence.openweathermap.api.utils.RequestUtils; @@ -57,13 +55,13 @@ public class WeatherRequester extends BasicRequester { return "weather"; } - protected Weather executeRequest(String requestSpecificParameters) throws InvalidAuthTokenException, DataNotFoundException { + protected Weather executeRequest(String requestSpecificParameters) { Weather weather = null; try { InputStream requestResult = RequestUtils.executeGetRequest(buildURL(requestSpecificParameters)); - weather = (Weather)JSONUtils.parseJSON(requestResult, Weather.class); + weather = (Weather) JSONUtils.parseJSON(requestResult, Weather.class); weather.getWind().setUnit(Unit.getWindUnit(unitSystem)); weather.getWeatherInfo().setTemperatureUnit(Unit.getTemperatureUnit(unitSystem)); diff --git a/src/main/java/com/github/prominence/openweathermap/api/exception/DataNotFoundException.java b/src/main/java/com/github/prominence/openweathermap/api/exception/DataNotFoundException.java index a84297d..54a8b1e 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/exception/DataNotFoundException.java +++ b/src/main/java/com/github/prominence/openweathermap/api/exception/DataNotFoundException.java @@ -22,7 +22,7 @@ package com.github.prominence.openweathermap.api.exception; -public class DataNotFoundException extends Exception { +public class DataNotFoundException extends RuntimeException { public DataNotFoundException() { super("Data for provided parameters wasn't found. Please, check your request."); diff --git a/src/main/java/com/github/prominence/openweathermap/api/exception/InvalidAuthTokenException.java b/src/main/java/com/github/prominence/openweathermap/api/exception/InvalidAuthTokenException.java index 19a358e..b121d24 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/exception/InvalidAuthTokenException.java +++ b/src/main/java/com/github/prominence/openweathermap/api/exception/InvalidAuthTokenException.java @@ -22,7 +22,7 @@ package com.github.prominence.openweathermap.api.exception; -public class InvalidAuthTokenException extends Exception { +public class InvalidAuthTokenException extends RuntimeException { public InvalidAuthTokenException() { super("Check your authentication token! You can get it here: https://home.openweathermap.org/api_keys/."); diff --git a/src/main/java/com/github/prominence/openweathermap/api/model/Coordinates.java b/src/main/java/com/github/prominence/openweathermap/api/model/Coordinates.java index 9c6ca55..d4a4f56 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/model/Coordinates.java +++ b/src/main/java/com/github/prominence/openweathermap/api/model/Coordinates.java @@ -33,13 +33,13 @@ import lombok.Setter; public class Coordinates { @JSONField(name = "lat") - // City geo location, latitude + // City geo location, latitude. Valid range: [-90, 90] @Getter @Setter private float latitude; @JSONField(name = "lon") - // City geo location, longitude + // City geo location, longitude. Valid range: [-180, 180] @Getter @Setter private float longitude; 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 b667f2d..b43eb50 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 @@ -32,9 +32,10 @@ import java.net.URL; public final class RequestUtils { - private RequestUtils() {} + private RequestUtils() { + } - public static InputStream executeGetRequest(URL requestUrl) throws InvalidAuthTokenException, DataNotFoundException { + public static InputStream executeGetRequest(URL requestUrl) { InputStream resultStream = null; try { @@ -48,9 +49,10 @@ public final class RequestUtils { case HttpURLConnection.HTTP_UNAUTHORIZED: throw new InvalidAuthTokenException(); case HttpURLConnection.HTTP_NOT_FOUND: + case HttpURLConnection.HTTP_BAD_REQUEST: throw new DataNotFoundException(); } - } catch (IOException | ClassCastException ex) { + } catch (IOException ex) { ex.printStackTrace(); } diff --git a/src/test/java/com/github/prominence/openweathermap/api/test/AirPollutionRequestedIntegrationTest.java b/src/test/java/com/github/prominence/openweathermap/api/test/AirPollutionRequestedIntegrationTest.java new file mode 100644 index 0000000..818b00b --- /dev/null +++ b/src/test/java/com/github/prominence/openweathermap/api/test/AirPollutionRequestedIntegrationTest.java @@ -0,0 +1,51 @@ +/* + * 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.test; + +import com.github.prominence.openweathermap.api.AirPollutionRequester; +import com.github.prominence.openweathermap.api.constants.TimeFrame; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.Date; + +public class AirPollutionRequestedIntegrationTest extends ApiTest { + + private static AirPollutionRequester airPollutionRequester; + + @BeforeClass + public static void setup() { + airPollutionRequester = getManager().getAirPollutionRequester(0f, 10f, new Date(116, 11, 25), TimeFrame.DAY); + } + + @Test + public void whenRequestAirPollutionState_thenReturnNotNull() { + assert airPollutionRequester.retrieve() != null; + } + + @Test(expected = IllegalArgumentException.class) + public void whenRequestAirPollutionStateWithoutAnyParam_thenThrowAnException() { + AirPollutionRequester requester = getManager().getAirPollutionRequester(0f, 10f, null, TimeFrame.DAY); + requester.retrieve(); + } +} diff --git a/src/test/java/com/github/prominence/openweathermap/api/test/ApiTest.java b/src/test/java/com/github/prominence/openweathermap/api/test/ApiTest.java new file mode 100644 index 0000000..a74c14f --- /dev/null +++ b/src/test/java/com/github/prominence/openweathermap/api/test/ApiTest.java @@ -0,0 +1,41 @@ +/* + * 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.test; + +import com.github.prominence.openweathermap.api.OpenWeatherMapManager; +import org.junit.BeforeClass; + +public class ApiTest { + + private static OpenWeatherMapManager manager; + + @BeforeClass + public static void retrieveApiKey() { + String apiKey = System.getenv("OPENWEATHER_API_KEY"); + manager = new OpenWeatherMapManager(apiKey); + } + + protected static OpenWeatherMapManager getManager() { + return manager; + } +} diff --git a/src/test/java/com/github/prominence/openweathermap/api/test/HourlyForecastRequesterIntegrationTest.java b/src/test/java/com/github/prominence/openweathermap/api/test/HourlyForecastRequesterIntegrationTest.java new file mode 100644 index 0000000..8ec149b --- /dev/null +++ b/src/test/java/com/github/prominence/openweathermap/api/test/HourlyForecastRequesterIntegrationTest.java @@ -0,0 +1,101 @@ +/* + * 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.test; + +import com.github.prominence.openweathermap.api.HourlyForecastRequester; +import com.github.prominence.openweathermap.api.exception.DataNotFoundException; +import com.github.prominence.openweathermap.api.model.Coordinates; +import org.junit.BeforeClass; +import org.junit.Test; + +public class HourlyForecastRequesterIntegrationTest extends ApiTest { + + private static HourlyForecastRequester hourlyForecastRequester; + + @BeforeClass + public static void setup() { + hourlyForecastRequester = getManager().getHourlyForecastRequester(); + } + + @Test + public void whenRequestForecastForMinskByCity() { + assert hourlyForecastRequester.getByCityName("Minsk") != null; + } + + @Test(expected = DataNotFoundException.class) + public void whenRequestForecastByWrongCity_thenThrowAnException() { + hourlyForecastRequester.getByCityId("IncorrectCity"); + } + + @Test + public void whenRequestForecastForMinskById_thenReturnNotNull() { + assert hourlyForecastRequester.getByCityId("625144") != null; + } + + @Test(expected = DataNotFoundException.class) + public void whenRequestForecastByWrongId_thenThrowAnException() { + hourlyForecastRequester.getByCityId("IncorrectId"); + } + + @Test + public void whenRequestForecastByCoordinates_thenReturnNotNull() { + // given + float latitude = 53.9f; + float longitude = 27.56667f; + Coordinates coordinates = new Coordinates(latitude, longitude); + + // expected + assert hourlyForecastRequester.getByCoordinates(coordinates) != null; + assert hourlyForecastRequester.getByCoordinates(latitude, longitude) != null; + } + + @Test(expected = NullPointerException.class) // not good, will be reimplemented in further versions + public void whenRequestByNullCoordinates_thenThrowAnException() { + hourlyForecastRequester.getByCoordinates(null); + } + + @Test(expected = DataNotFoundException.class) + public void whenRequestForecastByWrongCoordinates_thenThrowAnException() { + hourlyForecastRequester.getByCoordinates(91f, 180f); + } + + @Test + public void whenRequestForecastByMaxCoordinates_thenReturnNotNull() { + assert hourlyForecastRequester.getByCoordinates(90f, 180f) != null; + } + + @Test + public void whenRequestForecastByMinCoordinates_thenReturnNotNull() { + assert hourlyForecastRequester.getByCoordinates(-90f, -180f) != null; + } + + @Test + public void whenRequestForecastByZipCode_thenReturnNotNull() { + assert hourlyForecastRequester.getByZIPCode("220015", "BY") != null; + } + + @Test(expected = DataNotFoundException.class) + public void whenRequestForecastByWrongZipCode_thenThrowAnException() { + hourlyForecastRequester.getByZIPCode("wrongZipCode", "BY"); + } +} diff --git a/src/test/java/com/github/prominence/openweathermap/api/test/UltravioletIndexRequestedIntegrationTest.java b/src/test/java/com/github/prominence/openweathermap/api/test/UltravioletIndexRequestedIntegrationTest.java new file mode 100644 index 0000000..ca30886 --- /dev/null +++ b/src/test/java/com/github/prominence/openweathermap/api/test/UltravioletIndexRequestedIntegrationTest.java @@ -0,0 +1,76 @@ +/* + * 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.test; + +import com.github.prominence.openweathermap.api.UltravioletIndexRequester; +import com.github.prominence.openweathermap.api.exception.DataNotFoundException; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.Date; + +public class UltravioletIndexRequestedIntegrationTest extends ApiTest { + + private static UltravioletIndexRequester minskUltravioletIndexRequester; + + @BeforeClass + public static void setup() { + minskUltravioletIndexRequester = getManager().getUltravioletIndexRequester(53.9f, 27.56667f); + } + + @Test + public void whenRequestUVI_thenReturnNotNull() { + assert minskUltravioletIndexRequester.getCurrentUVIndex() != null; + } + + @Test + public void whenRequestUVIForecastFor5Days_thenReturnNotNull() { + assert minskUltravioletIndexRequester.getUVIndexForecast(5) != null; + } + + @Test(expected = DataNotFoundException.class) + public void whenRequestUVIForecastForWrongAmountOfDays_thenThrowAnException() { + minskUltravioletIndexRequester.getUVIndexForecast(-2); + } + + @Test(expected = DataNotFoundException.class) + public void whenRequestUVIForecastForLargeAmountOfDays_thenThrowAnException() { + assert minskUltravioletIndexRequester.getUVIndexForecast(10000) != null; + } + + @Test // check it later + public void whenRequestUVIForPeriod_thenReturnNotNull() { + assert minskUltravioletIndexRequester.getUVIndexByPeriod(new Date(), new Date()) != null; + } + + @Test(expected = NullPointerException.class) // not good, will be reimplemented in further versions + public void whenRequestUVIForNullPeriod_thenThrowAnException() { + minskUltravioletIndexRequester.getUVIndexByPeriod(null, null); + } + + @Test(expected = NullPointerException.class) // not good, will be reimplemented in further versions + public void whenRequestUVIForNullCoordinates_thenThrowAnException() { + UltravioletIndexRequester requester = getManager().getUltravioletIndexRequester(null); + requester.getCurrentUVIndex(); + } +} diff --git a/src/test/java/com/github/prominence/openweathermap/api/test/WeatherRequestIntegrationTest.java b/src/test/java/com/github/prominence/openweathermap/api/test/WeatherRequestIntegrationTest.java new file mode 100644 index 0000000..38f0f87 --- /dev/null +++ b/src/test/java/com/github/prominence/openweathermap/api/test/WeatherRequestIntegrationTest.java @@ -0,0 +1,101 @@ +/* + * 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.test; + +import com.github.prominence.openweathermap.api.WeatherRequester; +import com.github.prominence.openweathermap.api.exception.DataNotFoundException; +import com.github.prominence.openweathermap.api.model.Coordinates; +import org.junit.BeforeClass; +import org.junit.Test; + +public class WeatherRequestIntegrationTest extends ApiTest { + + private static WeatherRequester weatherRequester; + + @BeforeClass + public static void setup() { + weatherRequester = getManager().getWeatherRequester(); + } + + @Test + public void whenRequestWeatherForMinskByName_thenReturnNotNull() { + assert weatherRequester.getByCityName("Minsk") != null; + } + + @Test(expected = DataNotFoundException.class) + public void whenRequestWeatherByWrongCity_thenThrowAnException() { + weatherRequester.getByCityId("IncorrectCity"); + } + + @Test + public void whenRequestWeatherForMinskById_thenReturnNotNull() { + assert weatherRequester.getByCityId("625144") != null; + } + + @Test(expected = DataNotFoundException.class) + public void whenRequestWeatherByWrongId_thenThrowAnException() { + weatherRequester.getByCityId("IncorrectId"); + } + + @Test + public void whenRequestWeatherByCoordinates_thenReturnNotNull() { + // given + float latitude = 53.9f; + float longitude = 27.56667f; + Coordinates coordinates = new Coordinates(latitude, longitude); + + // expected + assert weatherRequester.getByCoordinates(coordinates) != null; + assert weatherRequester.getByCoordinates(latitude, longitude) != null; + } + + @Test(expected = NullPointerException.class) // not good, will be reimplemented in further versions + public void whenRequestByNullCoordinates_thenThrowAnException() { + weatherRequester.getByCoordinates(null); + } + + @Test(expected = DataNotFoundException.class) + public void whenRequestWeatherByWrongCoordinates_thenThrowAnException() { + weatherRequester.getByCoordinates(91f, 180f); + } + + @Test + public void whenRequestWeatherByMaxCoordinates_thenReturnNotNull() { + assert weatherRequester.getByCoordinates(90f, 180f) != null; + } + + @Test + public void whenRequestWeatherByMinCoordinates_thenReturnNotNull() { + assert weatherRequester.getByCoordinates(-90f, -180f) != null; + } + + @Test + public void whenRequestWeatherByZipCode_thenReturnNotNull() { + assert weatherRequester.getByZIPCode("220015", "BY") != null; + } + + @Test(expected = DataNotFoundException.class) + public void whenRequestWeatherByWrongZipCode_thenThrowAnException() { + weatherRequester.getByZIPCode("wrongZipCode", "BY"); + } +}