From 3845e41bcdc3ad1895fdf2ce5352d0807b002257 Mon Sep 17 00:00:00 2001 From: Alexey Zinchenko Date: Wed, 26 Jun 2019 20:35:56 +0300 Subject: [PATCH] Renamed exception. Documentation enhancements. Small refactoring for unit systems. --- docs/SNAPSHOT.md | 14 +++--- .../openweathermap/api/enums/UnitSystem.java | 18 +++---- ...ception.java => NoDataFoundException.java} | 6 +-- .../impl/CurrentWeatherResponseMapper.java | 2 +- ...ltCurrentWeatherRequestCustomizerImpl.java | 2 +- ...ltCurrentWeatherRequestCustomizerImpl.java | 2 +- .../api/utils/RequestUtils.java | 10 ++-- .../api/CurrentWeatherIntegrationTest.java | 48 +++++++++---------- .../api/utils/RequestUtilsUnitTest.java | 4 +- 9 files changed, 53 insertions(+), 53 deletions(-) rename src/main/java/com/github/prominence/openweathermap/api/exception/{DataNotFoundException.java => NoDataFoundException.java} (89%) diff --git a/docs/SNAPSHOT.md b/docs/SNAPSHOT.md index 5dedd3a..a47c026 100644 --- a/docs/SNAPSHOT.md +++ b/docs/SNAPSHOT.md @@ -59,7 +59,7 @@ final Weather weather = openWeatherClient .byCoordinate(new Coordinate(5, 5)) .accuracy(Accuracy.ACCURATE) .language(Language.ROMANIAN) - .unitSystem(Unit.METRIC_SYSTEM) + .unitSystem(UnitSystem.METRIC) .retrieve() .asJava(); @@ -68,7 +68,7 @@ final CompletableFuture weatherXmlFuture = openWeatherClient .single() .byZipCodeAndCountry("220015", "by") .language(Language.RUSSIAN) - .unitSystem(Unit.METRIC_SYSTEM) + .unitSystem(UnitSystem.METRIC) .retrieveAsync() .asXML(); ``` @@ -81,7 +81,7 @@ final String weatherListJson = openWeatherClient .byRectangle(new CoordinateRectangle(12, 32, 15, 37), 10, true) .accuracy(Accuracy.ACCURATE) .language(Language.ROMANIAN) - .unitSystem(Unit.METRIC_SYSTEM) + .unitSystem(UnitSystem.METRIC) .retrieve() .asJSON(); @@ -89,7 +89,7 @@ final CompletableFuture> weatherListFuture = openWeatherClient .currentWeather() .multiple() .byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true) - .unitSystem(Unit.IMPERIAL_SYSTEM) + .unitSystem(UnitSystem.IMPERIAL) .retrieveAsync() .asJava(); ``` @@ -164,9 +164,9 @@ Location: Minsk(BY), Weather: слегка облачно, 20.0 ℃, 1019.0 hPa, #### Unit | Constant | Description | |----------------------|------------------------------------------------| -| Unit.METRIC_SYSTEM | Celsius, meter/sec, hPa, mm(rain, snow). | -| Unit.IMPERIAL_SYSTEM | Fahrenheit, miles/hour, hPa, mm(rain, snow). | -| Unit.STANDARD_SYSTEM | Kelvin, meter/sec, hPa, mm(rain, snow) | +| UnitSystem.METRIC | Celsius, meter/sec, hPa, mm(rain, snow). | +| UnitSystem.IMPERIAL | Fahrenheit, miles/hour, hPa, mm(rain, snow). | +| UnitSystem.STANDARD | Kelvin, meter/sec, hPa, mm(rain, snow) | ### Dependencies * com.fasterxml.jackson.core:jackson-databind:2.9.9 diff --git a/src/main/java/com/github/prominence/openweathermap/api/enums/UnitSystem.java b/src/main/java/com/github/prominence/openweathermap/api/enums/UnitSystem.java index a7a276d..b7e4853 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/enums/UnitSystem.java +++ b/src/main/java/com/github/prominence/openweathermap/api/enums/UnitSystem.java @@ -24,9 +24,9 @@ package com.github.prominence.openweathermap.api.enums; public enum UnitSystem { - METRIC_SYSTEM("metric"), - IMPERIAL_SYSTEM("imperial"), - STANDARD_SYSTEM("standard"); + METRIC("metric"), + IMPERIAL("imperial"), + STANDARD("standard"); private final String value; @@ -36,10 +36,10 @@ public enum UnitSystem { public static String getWindUnit(UnitSystem type) { switch (type) { - case IMPERIAL_SYSTEM: + case IMPERIAL: return "miles/hour"; - case STANDARD_SYSTEM: - case METRIC_SYSTEM: + case STANDARD: + case METRIC: default: return "meter/sec"; } @@ -47,11 +47,11 @@ public enum UnitSystem { public static String getTemperatureUnit(UnitSystem type) { switch (type) { - case METRIC_SYSTEM: + case METRIC: return "℃"; - case IMPERIAL_SYSTEM: + case IMPERIAL: return "℉"; - case STANDARD_SYSTEM: + case STANDARD: default: return "K"; } diff --git a/src/main/java/com/github/prominence/openweathermap/api/exception/DataNotFoundException.java b/src/main/java/com/github/prominence/openweathermap/api/exception/NoDataFoundException.java similarity index 89% rename from src/main/java/com/github/prominence/openweathermap/api/exception/DataNotFoundException.java rename to src/main/java/com/github/prominence/openweathermap/api/exception/NoDataFoundException.java index 8eb9aac..a46b888 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/exception/DataNotFoundException.java +++ b/src/main/java/com/github/prominence/openweathermap/api/exception/NoDataFoundException.java @@ -22,13 +22,13 @@ package com.github.prominence.openweathermap.api.exception; -public class DataNotFoundException extends RuntimeException { +public class NoDataFoundException extends RuntimeException { - public DataNotFoundException() { + public NoDataFoundException() { super("Data for provided parameters wasn't found. Please, check requested location."); } - public DataNotFoundException(Throwable throwable) { + public NoDataFoundException(Throwable throwable) { super(throwable.getMessage(), throwable.getCause()); } diff --git a/src/main/java/com/github/prominence/openweathermap/api/impl/CurrentWeatherResponseMapper.java b/src/main/java/com/github/prominence/openweathermap/api/impl/CurrentWeatherResponseMapper.java index 54e8fca..0af4674 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/impl/CurrentWeatherResponseMapper.java +++ b/src/main/java/com/github/prominence/openweathermap/api/impl/CurrentWeatherResponseMapper.java @@ -84,7 +84,7 @@ public class CurrentWeatherResponseMapper implements ResponseMapper { private UnitSystem unitSystem; CurrentWeatherResponseMapper(UnitSystem unitSystem) { - this.unitSystem = unitSystem != null ? unitSystem : UnitSystem.STANDARD_SYSTEM; + this.unitSystem = unitSystem != null ? unitSystem : UnitSystem.STANDARD; } @Override diff --git a/src/main/java/com/github/prominence/openweathermap/api/impl/MultipleResultCurrentWeatherRequestCustomizerImpl.java b/src/main/java/com/github/prominence/openweathermap/api/impl/MultipleResultCurrentWeatherRequestCustomizerImpl.java index e9ae541..1b0a243 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/impl/MultipleResultCurrentWeatherRequestCustomizerImpl.java +++ b/src/main/java/com/github/prominence/openweathermap/api/impl/MultipleResultCurrentWeatherRequestCustomizerImpl.java @@ -78,7 +78,7 @@ public class MultipleResultCurrentWeatherRequestCustomizerImpl implements Multip if (language != null) { urlBuilder.addRequestParameter("lang", language.getValue()); } - if (unitSystem != null && unitSystem != UnitSystem.STANDARD_SYSTEM) { + if (unitSystem != null && unitSystem != UnitSystem.STANDARD) { urlBuilder.addRequestParameter("units", unitSystem.getValue()); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/impl/SingleResultCurrentWeatherRequestCustomizerImpl.java b/src/main/java/com/github/prominence/openweathermap/api/impl/SingleResultCurrentWeatherRequestCustomizerImpl.java index 8e1155c..b4ad417 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/impl/SingleResultCurrentWeatherRequestCustomizerImpl.java +++ b/src/main/java/com/github/prominence/openweathermap/api/impl/SingleResultCurrentWeatherRequestCustomizerImpl.java @@ -78,7 +78,7 @@ public class SingleResultCurrentWeatherRequestCustomizerImpl implements SingleRe if (language != null) { urlBuilder.addRequestParameter("lang", language.getValue()); } - if (unitSystem != null && unitSystem != UnitSystem.STANDARD_SYSTEM) { + if (unitSystem != null && unitSystem != UnitSystem.STANDARD) { urlBuilder.addRequestParameter("units", unitSystem.getValue()); } } 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 b780212..5ee37cc 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 @@ -22,7 +22,7 @@ package com.github.prominence.openweathermap.api.utils; -import com.github.prominence.openweathermap.api.exception.DataNotFoundException; +import com.github.prominence.openweathermap.api.exception.NoDataFoundException; import com.github.prominence.openweathermap.api.exception.InvalidAuthTokenException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,8 +52,8 @@ public final class RequestUtils { * @param requestUrl url for API call execution. * @return InputStream instance containing http response body. * @throws InvalidAuthTokenException in case if authentication token wasn't set or requested functionality is not permitted for its subscription plan. - * @throws DataNotFoundException in case if there is no any data for requested location(s) or request is invalid. - * @throws IllegalStateException in case of unexpected response or error. + * @throws NoDataFoundException in case if there is no any data for requested location(s) or request is invalid. + * @throws IllegalStateException in case of unexpected response or error. */ private static InputStream executeRequest(URL requestUrl) { InputStream resultStream; @@ -70,13 +70,13 @@ public final class RequestUtils { throw new InvalidAuthTokenException(); case HttpURLConnection.HTTP_NOT_FOUND: case HttpURLConnection.HTTP_BAD_REQUEST: - throw new DataNotFoundException(); + throw new NoDataFoundException(); default: throw new IllegalStateException("Unexpected value: " + connection.getResponseCode()); } } catch (IllegalStateException | IOException ex) { logger.error("An error occurred during OpenWeatherMap API response parsing: ", ex); - throw new DataNotFoundException(ex); + throw new NoDataFoundException(ex); } return resultStream; diff --git a/src/test/java/com/github/prominence/openweathermap/api/CurrentWeatherIntegrationTest.java b/src/test/java/com/github/prominence/openweathermap/api/CurrentWeatherIntegrationTest.java index 56e236c..b540f50 100644 --- a/src/test/java/com/github/prominence/openweathermap/api/CurrentWeatherIntegrationTest.java +++ b/src/test/java/com/github/prominence/openweathermap/api/CurrentWeatherIntegrationTest.java @@ -25,7 +25,7 @@ package com.github.prominence.openweathermap.api; 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.DataNotFoundException; +import com.github.prominence.openweathermap.api.exception.NoDataFoundException; import com.github.prominence.openweathermap.api.exception.InvalidAuthTokenException; import com.github.prominence.openweathermap.api.impl.OpenWeatherMapClient; import com.github.prominence.openweathermap.api.model.Coordinate; @@ -46,7 +46,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .single() .byCoordinate(new Coordinate(5, 5)) .accuracy(Accuracy.ACCURATE) - .unitSystem(UnitSystem.METRIC_SYSTEM) + .unitSystem(UnitSystem.METRIC) .retrieve() .asJava(); @@ -75,7 +75,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .single() .byCityName("Minsk") .language(Language.RUSSIAN) - .unitSystem(UnitSystem.METRIC_SYSTEM) + .unitSystem(UnitSystem.METRIC) .retrieve() .asJava(); @@ -90,7 +90,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .single() .byCityName("Moscow", "ru") .language(Language.RUSSIAN) - .unitSystem(UnitSystem.METRIC_SYSTEM) + .unitSystem(UnitSystem.METRIC) .retrieve() .asJava(); @@ -105,7 +105,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .single() .byZipCodeAndCountry("220015", "by") .language(Language.RUSSIAN) - .unitSystem(UnitSystem.METRIC_SYSTEM) + .unitSystem(UnitSystem.METRIC) .retrieve() .asJava(); @@ -120,7 +120,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .single() .byZipCodeAndCountry("220015", "by") .language(Language.RUSSIAN) - .unitSystem(UnitSystem.METRIC_SYSTEM) + .unitSystem(UnitSystem.METRIC) .retrieve() .asJSON(); @@ -135,7 +135,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .single() .byZipCodeAndCountry("220015", "by") .language(Language.RUSSIAN) - .unitSystem(UnitSystem.METRIC_SYSTEM) + .unitSystem(UnitSystem.METRIC) .retrieve() .asXML(); @@ -150,7 +150,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .single() .byZipCodeAndCountry("220015", "by") .language(Language.RUSSIAN) - .unitSystem(UnitSystem.METRIC_SYSTEM) + .unitSystem(UnitSystem.METRIC) .retrieve() .asHTML(); @@ -165,7 +165,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .single() .byZipCodeAndCountry("220015", "by") .language(Language.RUSSIAN) - .unitSystem(UnitSystem.METRIC_SYSTEM) + .unitSystem(UnitSystem.METRIC) .retrieveAsync() .asXML(); @@ -180,7 +180,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .single() .byZipCodeAndCountry("220015", "by") .language(Language.RUSSIAN) - .unitSystem(UnitSystem.METRIC_SYSTEM) + .unitSystem(UnitSystem.METRIC) .retrieveAsync() .asJava(); @@ -195,7 +195,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .single() .byZipCodeAndCountry("220015", "by") .language(Language.RUSSIAN) - .unitSystem(UnitSystem.METRIC_SYSTEM) + .unitSystem(UnitSystem.METRIC) .retrieveAsync() .asJSON(); @@ -210,7 +210,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .single() .byZipCodeAndCountry("220015", "by") .language(Language.RUSSIAN) - .unitSystem(UnitSystem.METRIC_SYSTEM) + .unitSystem(UnitSystem.METRIC) .retrieveAsync() .asHTML(); @@ -226,7 +226,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .byRectangle(new CoordinateRectangle(12, 32, 15, 37), 10) .accuracy(Accuracy.ACCURATE) .language(Language.ROMANIAN) - .unitSystem(UnitSystem.METRIC_SYSTEM) + .unitSystem(UnitSystem.METRIC) .retrieve() .asJava(); @@ -243,7 +243,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .byRectangle(new CoordinateRectangle(12, 32, 15, 37), 10, true) .accuracy(Accuracy.ACCURATE) .language(Language.ROMANIAN) - .unitSystem(UnitSystem.METRIC_SYSTEM) + .unitSystem(UnitSystem.METRIC) .retrieve() .asJava(); @@ -259,7 +259,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .multiple() .byCitiesInCycle(new Coordinate(55.5, 37.5), 10) .language(Language.GERMAN) - .unitSystem(UnitSystem.IMPERIAL_SYSTEM) + .unitSystem(UnitSystem.IMPERIAL) .retrieve() .asJava(); @@ -275,7 +275,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .multiple() .byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true) .language(Language.GERMAN) - .unitSystem(UnitSystem.IMPERIAL_SYSTEM) + .unitSystem(UnitSystem.IMPERIAL) .retrieve() .asJava(); @@ -291,7 +291,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .multiple() .byCitiesInCycle(new Coordinate(55.5, 37.5), 10) .language(Language.GERMAN) - .unitSystem(UnitSystem.IMPERIAL_SYSTEM) + .unitSystem(UnitSystem.IMPERIAL) .retrieve() .asJSON(); @@ -306,7 +306,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .multiple() .byCitiesInCycle(new Coordinate(55.5, 37.5), 10) .language(Language.GERMAN) - .unitSystem(UnitSystem.IMPERIAL_SYSTEM) + .unitSystem(UnitSystem.IMPERIAL) .retrieve() .asXML(); @@ -321,7 +321,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .multiple() .byCitiesInCycle(new Coordinate(55.5, 37.5), 10) .language(Language.GERMAN) - .unitSystem(UnitSystem.IMPERIAL_SYSTEM) + .unitSystem(UnitSystem.IMPERIAL) .retrieve() .asHTML(); @@ -336,7 +336,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .multiple() .byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true) .language(Language.GERMAN) - .unitSystem(UnitSystem.IMPERIAL_SYSTEM) + .unitSystem(UnitSystem.IMPERIAL) .retrieveAsync() .asJava(); @@ -353,7 +353,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .multiple() .byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true) .language(Language.GERMAN) - .unitSystem(UnitSystem.IMPERIAL_SYSTEM) + .unitSystem(UnitSystem.IMPERIAL) .retrieveAsync() .asXML(); @@ -368,7 +368,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .multiple() .byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true) .language(Language.GERMAN) - .unitSystem(UnitSystem.IMPERIAL_SYSTEM) + .unitSystem(UnitSystem.IMPERIAL) .retrieveAsync() .asJSON(); @@ -383,7 +383,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .multiple() .byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true) .language(Language.GERMAN) - .unitSystem(UnitSystem.IMPERIAL_SYSTEM) + .unitSystem(UnitSystem.IMPERIAL) .retrieveAsync() .asHTML(); @@ -402,7 +402,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest { .asJSON(); } - @Test(expected = DataNotFoundException.class) + @Test(expected = NoDataFoundException.class) public void whenRequestCurrentWeatherForInvalidLocation_thenThrowAnException() { getClient() .currentWeather() diff --git a/src/test/java/com/github/prominence/openweathermap/api/utils/RequestUtilsUnitTest.java b/src/test/java/com/github/prominence/openweathermap/api/utils/RequestUtilsUnitTest.java index 4fbcf99..3f4247a 100644 --- a/src/test/java/com/github/prominence/openweathermap/api/utils/RequestUtilsUnitTest.java +++ b/src/test/java/com/github/prominence/openweathermap/api/utils/RequestUtilsUnitTest.java @@ -1,6 +1,6 @@ package com.github.prominence.openweathermap.api.utils; -import com.github.prominence.openweathermap.api.exception.DataNotFoundException; +import com.github.prominence.openweathermap.api.exception.NoDataFoundException; import org.junit.Test; public class RequestUtilsUnitTest { @@ -10,7 +10,7 @@ public class RequestUtilsUnitTest { RequestUtils.getResponse("wrongUrl"); } - @Test(expected = DataNotFoundException.class) + @Test(expected = NoDataFoundException.class) public void whenPassUrlToNonExistingPage_thenThrowAnException() { RequestUtils.getResponse("https://openweathermap.org/somePage"); }