Renamed exception. Documentation enhancements. Small refactoring for unit systems.

This commit is contained in:
Alexey Zinchenko 2019-06-26 20:35:56 +03:00
parent babc48bf6c
commit 3845e41bcd
9 changed files with 53 additions and 53 deletions

View File

@ -59,7 +59,7 @@ final Weather weather = openWeatherClient
.byCoordinate(new Coordinate(5, 5)) .byCoordinate(new Coordinate(5, 5))
.accuracy(Accuracy.ACCURATE) .accuracy(Accuracy.ACCURATE)
.language(Language.ROMANIAN) .language(Language.ROMANIAN)
.unitSystem(Unit.METRIC_SYSTEM) .unitSystem(UnitSystem.METRIC)
.retrieve() .retrieve()
.asJava(); .asJava();
@ -68,7 +68,7 @@ final CompletableFuture<String> weatherXmlFuture = openWeatherClient
.single() .single()
.byZipCodeAndCountry("220015", "by") .byZipCodeAndCountry("220015", "by")
.language(Language.RUSSIAN) .language(Language.RUSSIAN)
.unitSystem(Unit.METRIC_SYSTEM) .unitSystem(UnitSystem.METRIC)
.retrieveAsync() .retrieveAsync()
.asXML(); .asXML();
``` ```
@ -81,7 +81,7 @@ final String weatherListJson = openWeatherClient
.byRectangle(new CoordinateRectangle(12, 32, 15, 37), 10, true) .byRectangle(new CoordinateRectangle(12, 32, 15, 37), 10, true)
.accuracy(Accuracy.ACCURATE) .accuracy(Accuracy.ACCURATE)
.language(Language.ROMANIAN) .language(Language.ROMANIAN)
.unitSystem(Unit.METRIC_SYSTEM) .unitSystem(UnitSystem.METRIC)
.retrieve() .retrieve()
.asJSON(); .asJSON();
@ -89,7 +89,7 @@ final CompletableFuture<List<Weather>> weatherListFuture = openWeatherClient
.currentWeather() .currentWeather()
.multiple() .multiple()
.byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true) .byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true)
.unitSystem(Unit.IMPERIAL_SYSTEM) .unitSystem(UnitSystem.IMPERIAL)
.retrieveAsync() .retrieveAsync()
.asJava(); .asJava();
``` ```
@ -164,9 +164,9 @@ Location: Minsk(BY), Weather: слегка облачно, 20.0 ℃, 1019.0 hPa,
#### Unit #### Unit
| Constant | Description | | Constant | Description |
|----------------------|------------------------------------------------| |----------------------|------------------------------------------------|
| Unit.METRIC_SYSTEM | Celsius, meter/sec, hPa, mm(rain, snow). | | UnitSystem.METRIC | Celsius, meter/sec, hPa, mm(rain, snow). |
| Unit.IMPERIAL_SYSTEM | Fahrenheit, miles/hour, hPa, mm(rain, snow). | | UnitSystem.IMPERIAL | Fahrenheit, miles/hour, hPa, mm(rain, snow). |
| Unit.STANDARD_SYSTEM | Kelvin, meter/sec, hPa, mm(rain, snow) | | UnitSystem.STANDARD | Kelvin, meter/sec, hPa, mm(rain, snow) |
### Dependencies ### Dependencies
* com.fasterxml.jackson.core:jackson-databind:2.9.9 * com.fasterxml.jackson.core:jackson-databind:2.9.9

View File

@ -24,9 +24,9 @@ package com.github.prominence.openweathermap.api.enums;
public enum UnitSystem { public enum UnitSystem {
METRIC_SYSTEM("metric"), METRIC("metric"),
IMPERIAL_SYSTEM("imperial"), IMPERIAL("imperial"),
STANDARD_SYSTEM("standard"); STANDARD("standard");
private final String value; private final String value;
@ -36,10 +36,10 @@ public enum UnitSystem {
public static String getWindUnit(UnitSystem type) { public static String getWindUnit(UnitSystem type) {
switch (type) { switch (type) {
case IMPERIAL_SYSTEM: case IMPERIAL:
return "miles/hour"; return "miles/hour";
case STANDARD_SYSTEM: case STANDARD:
case METRIC_SYSTEM: case METRIC:
default: default:
return "meter/sec"; return "meter/sec";
} }
@ -47,11 +47,11 @@ public enum UnitSystem {
public static String getTemperatureUnit(UnitSystem type) { public static String getTemperatureUnit(UnitSystem type) {
switch (type) { switch (type) {
case METRIC_SYSTEM: case METRIC:
return ""; return "";
case IMPERIAL_SYSTEM: case IMPERIAL:
return ""; return "";
case STANDARD_SYSTEM: case STANDARD:
default: default:
return ""; return "";
} }

View File

@ -22,13 +22,13 @@
package com.github.prominence.openweathermap.api.exception; 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."); 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()); super(throwable.getMessage(), throwable.getCause());
} }

View File

@ -84,7 +84,7 @@ public class CurrentWeatherResponseMapper implements ResponseMapper<Weather> {
private UnitSystem unitSystem; private UnitSystem unitSystem;
CurrentWeatherResponseMapper(UnitSystem unitSystem) { CurrentWeatherResponseMapper(UnitSystem unitSystem) {
this.unitSystem = unitSystem != null ? unitSystem : UnitSystem.STANDARD_SYSTEM; this.unitSystem = unitSystem != null ? unitSystem : UnitSystem.STANDARD;
} }
@Override @Override

View File

@ -78,7 +78,7 @@ public class MultipleResultCurrentWeatherRequestCustomizerImpl implements Multip
if (language != null) { if (language != null) {
urlBuilder.addRequestParameter("lang", language.getValue()); urlBuilder.addRequestParameter("lang", language.getValue());
} }
if (unitSystem != null && unitSystem != UnitSystem.STANDARD_SYSTEM) { if (unitSystem != null && unitSystem != UnitSystem.STANDARD) {
urlBuilder.addRequestParameter("units", unitSystem.getValue()); urlBuilder.addRequestParameter("units", unitSystem.getValue());
} }
} }

View File

@ -78,7 +78,7 @@ public class SingleResultCurrentWeatherRequestCustomizerImpl implements SingleRe
if (language != null) { if (language != null) {
urlBuilder.addRequestParameter("lang", language.getValue()); urlBuilder.addRequestParameter("lang", language.getValue());
} }
if (unitSystem != null && unitSystem != UnitSystem.STANDARD_SYSTEM) { if (unitSystem != null && unitSystem != UnitSystem.STANDARD) {
urlBuilder.addRequestParameter("units", unitSystem.getValue()); urlBuilder.addRequestParameter("units", unitSystem.getValue());
} }
} }

View File

@ -22,7 +22,7 @@
package com.github.prominence.openweathermap.api.utils; 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 com.github.prominence.openweathermap.api.exception.InvalidAuthTokenException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -52,8 +52,8 @@ public final class RequestUtils {
* @param requestUrl url for API call execution. * @param requestUrl url for API call execution.
* @return <code>InputStream</code> instance containing http response body. * @return <code>InputStream</code> 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 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 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. * @throws IllegalStateException in case of unexpected response or error.
*/ */
private static InputStream executeRequest(URL requestUrl) { private static InputStream executeRequest(URL requestUrl) {
InputStream resultStream; InputStream resultStream;
@ -70,13 +70,13 @@ public final class RequestUtils {
throw new InvalidAuthTokenException(); throw new InvalidAuthTokenException();
case HttpURLConnection.HTTP_NOT_FOUND: case HttpURLConnection.HTTP_NOT_FOUND:
case HttpURLConnection.HTTP_BAD_REQUEST: case HttpURLConnection.HTTP_BAD_REQUEST:
throw new DataNotFoundException(); throw new NoDataFoundException();
default: default:
throw new IllegalStateException("Unexpected value: " + connection.getResponseCode()); throw new IllegalStateException("Unexpected value: " + connection.getResponseCode());
} }
} catch (IllegalStateException | IOException ex) { } catch (IllegalStateException | IOException ex) {
logger.error("An error occurred during OpenWeatherMap API response parsing: ", ex); logger.error("An error occurred during OpenWeatherMap API response parsing: ", ex);
throw new DataNotFoundException(ex); throw new NoDataFoundException(ex);
} }
return resultStream; return resultStream;

View File

@ -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.Accuracy;
import com.github.prominence.openweathermap.api.enums.Language; import com.github.prominence.openweathermap.api.enums.Language;
import com.github.prominence.openweathermap.api.enums.UnitSystem; 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.exception.InvalidAuthTokenException;
import com.github.prominence.openweathermap.api.impl.OpenWeatherMapClient; import com.github.prominence.openweathermap.api.impl.OpenWeatherMapClient;
import com.github.prominence.openweathermap.api.model.Coordinate; import com.github.prominence.openweathermap.api.model.Coordinate;
@ -46,7 +46,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.single() .single()
.byCoordinate(new Coordinate(5, 5)) .byCoordinate(new Coordinate(5, 5))
.accuracy(Accuracy.ACCURATE) .accuracy(Accuracy.ACCURATE)
.unitSystem(UnitSystem.METRIC_SYSTEM) .unitSystem(UnitSystem.METRIC)
.retrieve() .retrieve()
.asJava(); .asJava();
@ -75,7 +75,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.single() .single()
.byCityName("Minsk") .byCityName("Minsk")
.language(Language.RUSSIAN) .language(Language.RUSSIAN)
.unitSystem(UnitSystem.METRIC_SYSTEM) .unitSystem(UnitSystem.METRIC)
.retrieve() .retrieve()
.asJava(); .asJava();
@ -90,7 +90,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.single() .single()
.byCityName("Moscow", "ru") .byCityName("Moscow", "ru")
.language(Language.RUSSIAN) .language(Language.RUSSIAN)
.unitSystem(UnitSystem.METRIC_SYSTEM) .unitSystem(UnitSystem.METRIC)
.retrieve() .retrieve()
.asJava(); .asJava();
@ -105,7 +105,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.single() .single()
.byZipCodeAndCountry("220015", "by") .byZipCodeAndCountry("220015", "by")
.language(Language.RUSSIAN) .language(Language.RUSSIAN)
.unitSystem(UnitSystem.METRIC_SYSTEM) .unitSystem(UnitSystem.METRIC)
.retrieve() .retrieve()
.asJava(); .asJava();
@ -120,7 +120,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.single() .single()
.byZipCodeAndCountry("220015", "by") .byZipCodeAndCountry("220015", "by")
.language(Language.RUSSIAN) .language(Language.RUSSIAN)
.unitSystem(UnitSystem.METRIC_SYSTEM) .unitSystem(UnitSystem.METRIC)
.retrieve() .retrieve()
.asJSON(); .asJSON();
@ -135,7 +135,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.single() .single()
.byZipCodeAndCountry("220015", "by") .byZipCodeAndCountry("220015", "by")
.language(Language.RUSSIAN) .language(Language.RUSSIAN)
.unitSystem(UnitSystem.METRIC_SYSTEM) .unitSystem(UnitSystem.METRIC)
.retrieve() .retrieve()
.asXML(); .asXML();
@ -150,7 +150,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.single() .single()
.byZipCodeAndCountry("220015", "by") .byZipCodeAndCountry("220015", "by")
.language(Language.RUSSIAN) .language(Language.RUSSIAN)
.unitSystem(UnitSystem.METRIC_SYSTEM) .unitSystem(UnitSystem.METRIC)
.retrieve() .retrieve()
.asHTML(); .asHTML();
@ -165,7 +165,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.single() .single()
.byZipCodeAndCountry("220015", "by") .byZipCodeAndCountry("220015", "by")
.language(Language.RUSSIAN) .language(Language.RUSSIAN)
.unitSystem(UnitSystem.METRIC_SYSTEM) .unitSystem(UnitSystem.METRIC)
.retrieveAsync() .retrieveAsync()
.asXML(); .asXML();
@ -180,7 +180,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.single() .single()
.byZipCodeAndCountry("220015", "by") .byZipCodeAndCountry("220015", "by")
.language(Language.RUSSIAN) .language(Language.RUSSIAN)
.unitSystem(UnitSystem.METRIC_SYSTEM) .unitSystem(UnitSystem.METRIC)
.retrieveAsync() .retrieveAsync()
.asJava(); .asJava();
@ -195,7 +195,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.single() .single()
.byZipCodeAndCountry("220015", "by") .byZipCodeAndCountry("220015", "by")
.language(Language.RUSSIAN) .language(Language.RUSSIAN)
.unitSystem(UnitSystem.METRIC_SYSTEM) .unitSystem(UnitSystem.METRIC)
.retrieveAsync() .retrieveAsync()
.asJSON(); .asJSON();
@ -210,7 +210,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.single() .single()
.byZipCodeAndCountry("220015", "by") .byZipCodeAndCountry("220015", "by")
.language(Language.RUSSIAN) .language(Language.RUSSIAN)
.unitSystem(UnitSystem.METRIC_SYSTEM) .unitSystem(UnitSystem.METRIC)
.retrieveAsync() .retrieveAsync()
.asHTML(); .asHTML();
@ -226,7 +226,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.byRectangle(new CoordinateRectangle(12, 32, 15, 37), 10) .byRectangle(new CoordinateRectangle(12, 32, 15, 37), 10)
.accuracy(Accuracy.ACCURATE) .accuracy(Accuracy.ACCURATE)
.language(Language.ROMANIAN) .language(Language.ROMANIAN)
.unitSystem(UnitSystem.METRIC_SYSTEM) .unitSystem(UnitSystem.METRIC)
.retrieve() .retrieve()
.asJava(); .asJava();
@ -243,7 +243,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.byRectangle(new CoordinateRectangle(12, 32, 15, 37), 10, true) .byRectangle(new CoordinateRectangle(12, 32, 15, 37), 10, true)
.accuracy(Accuracy.ACCURATE) .accuracy(Accuracy.ACCURATE)
.language(Language.ROMANIAN) .language(Language.ROMANIAN)
.unitSystem(UnitSystem.METRIC_SYSTEM) .unitSystem(UnitSystem.METRIC)
.retrieve() .retrieve()
.asJava(); .asJava();
@ -259,7 +259,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.multiple() .multiple()
.byCitiesInCycle(new Coordinate(55.5, 37.5), 10) .byCitiesInCycle(new Coordinate(55.5, 37.5), 10)
.language(Language.GERMAN) .language(Language.GERMAN)
.unitSystem(UnitSystem.IMPERIAL_SYSTEM) .unitSystem(UnitSystem.IMPERIAL)
.retrieve() .retrieve()
.asJava(); .asJava();
@ -275,7 +275,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.multiple() .multiple()
.byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true) .byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true)
.language(Language.GERMAN) .language(Language.GERMAN)
.unitSystem(UnitSystem.IMPERIAL_SYSTEM) .unitSystem(UnitSystem.IMPERIAL)
.retrieve() .retrieve()
.asJava(); .asJava();
@ -291,7 +291,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.multiple() .multiple()
.byCitiesInCycle(new Coordinate(55.5, 37.5), 10) .byCitiesInCycle(new Coordinate(55.5, 37.5), 10)
.language(Language.GERMAN) .language(Language.GERMAN)
.unitSystem(UnitSystem.IMPERIAL_SYSTEM) .unitSystem(UnitSystem.IMPERIAL)
.retrieve() .retrieve()
.asJSON(); .asJSON();
@ -306,7 +306,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.multiple() .multiple()
.byCitiesInCycle(new Coordinate(55.5, 37.5), 10) .byCitiesInCycle(new Coordinate(55.5, 37.5), 10)
.language(Language.GERMAN) .language(Language.GERMAN)
.unitSystem(UnitSystem.IMPERIAL_SYSTEM) .unitSystem(UnitSystem.IMPERIAL)
.retrieve() .retrieve()
.asXML(); .asXML();
@ -321,7 +321,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.multiple() .multiple()
.byCitiesInCycle(new Coordinate(55.5, 37.5), 10) .byCitiesInCycle(new Coordinate(55.5, 37.5), 10)
.language(Language.GERMAN) .language(Language.GERMAN)
.unitSystem(UnitSystem.IMPERIAL_SYSTEM) .unitSystem(UnitSystem.IMPERIAL)
.retrieve() .retrieve()
.asHTML(); .asHTML();
@ -336,7 +336,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.multiple() .multiple()
.byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true) .byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true)
.language(Language.GERMAN) .language(Language.GERMAN)
.unitSystem(UnitSystem.IMPERIAL_SYSTEM) .unitSystem(UnitSystem.IMPERIAL)
.retrieveAsync() .retrieveAsync()
.asJava(); .asJava();
@ -353,7 +353,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.multiple() .multiple()
.byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true) .byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true)
.language(Language.GERMAN) .language(Language.GERMAN)
.unitSystem(UnitSystem.IMPERIAL_SYSTEM) .unitSystem(UnitSystem.IMPERIAL)
.retrieveAsync() .retrieveAsync()
.asXML(); .asXML();
@ -368,7 +368,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.multiple() .multiple()
.byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true) .byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true)
.language(Language.GERMAN) .language(Language.GERMAN)
.unitSystem(UnitSystem.IMPERIAL_SYSTEM) .unitSystem(UnitSystem.IMPERIAL)
.retrieveAsync() .retrieveAsync()
.asJSON(); .asJSON();
@ -383,7 +383,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.multiple() .multiple()
.byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true) .byCitiesInCycle(new Coordinate(55.5, 37.5), 10, true)
.language(Language.GERMAN) .language(Language.GERMAN)
.unitSystem(UnitSystem.IMPERIAL_SYSTEM) .unitSystem(UnitSystem.IMPERIAL)
.retrieveAsync() .retrieveAsync()
.asHTML(); .asHTML();
@ -402,7 +402,7 @@ public class CurrentWeatherIntegrationTest extends ApiTest {
.asJSON(); .asJSON();
} }
@Test(expected = DataNotFoundException.class) @Test(expected = NoDataFoundException.class)
public void whenRequestCurrentWeatherForInvalidLocation_thenThrowAnException() { public void whenRequestCurrentWeatherForInvalidLocation_thenThrowAnException() {
getClient() getClient()
.currentWeather() .currentWeather()

View File

@ -1,6 +1,6 @@
package com.github.prominence.openweathermap.api.utils; 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; import org.junit.Test;
public class RequestUtilsUnitTest { public class RequestUtilsUnitTest {
@ -10,7 +10,7 @@ public class RequestUtilsUnitTest {
RequestUtils.getResponse("wrongUrl"); RequestUtils.getResponse("wrongUrl");
} }
@Test(expected = DataNotFoundException.class) @Test(expected = NoDataFoundException.class)
public void whenPassUrlToNonExistingPage_thenThrowAnException() { public void whenPassUrlToNonExistingPage_thenThrowAnException() {
RequestUtils.getResponse("https://openweathermap.org/somePage"); RequestUtils.getResponse("https://openweathermap.org/somePage");
} }