Changed custom exceptions: now they are uncheked. Fixed several API issues. Added tests.

This commit is contained in:
Alexey Zinchenko 2019-05-29 22:50:07 +03:00
parent 340383afc8
commit c6bbf7be90
17 changed files with 464 additions and 57 deletions

10
pom.xml
View File

@ -72,7 +72,7 @@
<version>3.1.0</version>
<configuration>
<excludes>
<exclude>ApplicationTest.class</exclude>
<exclude>**/test/*</exclude>
</excludes>
</configuration>
</plugin>
@ -143,6 +143,12 @@
<version>1.18.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

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

View File

@ -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<T> 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<T> 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<String, String> getAdditionalParameters() {
@ -104,6 +109,7 @@ abstract class BasicRequester<T> extends AuthenticationTokenBasedRequester {
}
protected abstract String getRequestType();
protected abstract T executeRequest(String requestSpecificParameters) throws InvalidAuthTokenException, DataNotFoundException;
protected abstract T executeRequest(String requestSpecificParameters);
}

View File

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

View File

@ -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<HourlyForecast> {
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);

View File

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

View File

@ -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<UltravioletIndex> getUVIndexForecast(int amountOfDays) throws InvalidAuthTokenException, DataNotFoundException {
public List<UltravioletIndex> 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<UltravioletIndex> getUVIndexByPeriod(Date from, Date to) throws InvalidAuthTokenException, DataNotFoundException {
public List<UltravioletIndex> 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<UltravioletIndex> getListOfObjects(String requestParameters) throws InvalidAuthTokenException, DataNotFoundException {
private List<UltravioletIndex> getListOfObjects(String requestParameters) {
List<UltravioletIndex> ultravioletIndex = null;
TypeReference<List<UltravioletIndex>> typeRef = new TypeReference<List<UltravioletIndex>>() {};
TypeReference<List<UltravioletIndex>> typeRef = new TypeReference<List<UltravioletIndex>>() {
};
try (InputStream response = executeRequest("uvi/forecast", requestParameters)) {
ultravioletIndex = (List<UltravioletIndex>) 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;
}
}

View File

@ -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<Weather> {
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));

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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