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))
.accuracy(Accuracy.ACCURATE)
.language(Language.ROMANIAN)
.unitSystem(Unit.METRIC_SYSTEM)
.unitSystem(UnitSystem.METRIC)
.retrieve()
.asJava();
@ -68,7 +68,7 @@ final CompletableFuture<String> 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<List<Weather>> 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

View File

@ -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 "";
}

View File

@ -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());
}

View File

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

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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 <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 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;

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.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()

View File

@ -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");
}