Added tests for forecast functionality.

This commit is contained in:
Alexey Zinchenko 2021-03-24 02:15:01 +03:00
parent 48d0c9479d
commit cdc5a4b380
5 changed files with 429 additions and 13 deletions

View File

@ -31,7 +31,6 @@ import com.github.prominence.openweathermap.api.request.weather.CurrentWeatherRe
import static com.github.prominence.openweathermap.api.enums.SubscriptionPlan.*;
public class OpenWeatherMapClient {
private final String apiKey;
public OpenWeatherMapClient(String apiKey) {
@ -47,10 +46,4 @@ public class OpenWeatherMapClient {
public FiveDayThreeHourStepForecastRequester forecast5Day3HourStep() {
return new FiveDayThreeHourStepForecastRequesterImpl(apiKey);
}
// TODO:
// * Forecast: hourly, daily. Probably better to cover all free-plan functionality?
// * Air Pollution
// * Ultraviolet index
// DOCS
}

View File

@ -24,6 +24,9 @@ package com.github.prominence.openweathermap.api.request.forecast.free;
import com.github.prominence.openweathermap.api.model.Coordinate;
/**
* An interface for <a href="https://openweathermap.org/forecast5">API</a> methods.
*/
public interface FiveDayThreeHourStepForecastRequester {
FiveDayThreeHourStepForecastRequestCustomizer byCityName(String cityName);
@ -37,4 +40,6 @@ public interface FiveDayThreeHourStepForecastRequester {
FiveDayThreeHourStepForecastRequestCustomizer byCoordinate(Coordinate coordinate);
FiveDayThreeHourStepForecastRequestCustomizer byZipCodeAndCountry(String zipCode, String countryCode);
FiveDayThreeHourStepForecastRequestCustomizer byZipCodeInUSA(String zipCode);
}

View File

@ -70,4 +70,10 @@ public class FiveDayThreeHourStepForecastRequesterImpl implements FiveDayThreeHo
urlBuilder.addRequestParameter("zip", zipCode + "," + countryCode);
return new FiveDayThreeHourStepForecastRequestCustomizerImpl(urlBuilder);
}
@Override
public FiveDayThreeHourStepForecastRequestCustomizer byZipCodeInUSA(String zipCode) {
urlBuilder.addRequestParameter("zip", zipCode);
return new FiveDayThreeHourStepForecastRequestCustomizerImpl(urlBuilder);
}
}

View File

@ -25,6 +25,9 @@ package com.github.prominence.openweathermap.api.request.weather;
import com.github.prominence.openweathermap.api.request.weather.multiple.MultipleLocationsCurrentWeatherRequester;
import com.github.prominence.openweathermap.api.request.weather.single.SingleLocationCurrentWeatherRequester;
/**
* An interface for <a href="https://openweathermap.org/current">API</a> methods.
*/
public interface CurrentWeatherRequester {
SingleLocationCurrentWeatherRequester single();

View File

@ -23,16 +23,23 @@
package com.github.prominence.openweathermap.api.request.forecast.free;
import com.github.prominence.openweathermap.api.ApiTest;
import com.github.prominence.openweathermap.api.OpenWeatherMapClient;
import com.github.prominence.openweathermap.api.enums.Language;
import com.github.prominence.openweathermap.api.enums.UnitSystem;
import com.github.prominence.openweathermap.api.exception.InvalidAuthTokenException;
import com.github.prominence.openweathermap.api.exception.NoDataFoundException;
import com.github.prominence.openweathermap.api.model.Coordinate;
import com.github.prominence.openweathermap.api.model.forecast.Forecast;
import com.github.prominence.openweathermap.api.model.forecast.WeatherForecast;
import org.junit.Assert;
import org.junit.Test;
public class FiveDayThreeHourStepForecastIntegrationTest extends ApiTest {
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class FiveDayThreeHourStepForecastIntegrationTest extends ApiTest {
@Test
public void whenGetJavaObject_thenReturnNotNull() {
public void whenGetForecastByCityNameRequestAsJava_thenReturnNotNull() {
final Forecast forecast = getClient()
.forecast5Day3HourStep()
.byCityName("Minsk")
@ -43,19 +50,421 @@ public class FiveDayThreeHourStepForecastIntegrationTest extends ApiTest {
.asJava();
Assert.assertNotNull(forecast);
System.out.println(forecast);
Assert.assertNotNull(forecast.getLocation());
Assert.assertNotNull(forecast.getWeatherForecasts());
for (WeatherForecast weatherForecast : forecast.getWeatherForecasts()) {
Assert.assertNotNull(weatherForecast.getState());
Assert.assertNotNull(weatherForecast.getDescription());
Assert.assertNotNull(weatherForecast.getForecastTime());
Assert.assertNotNull(weatherForecast.getTemperature());
Assert.assertNotNull(weatherForecast.getAtmosphericPressure());
Assert.assertNotNull(weatherForecast.getHumidity());
Assert.assertNotNull(weatherForecast.getWind());
}
}
@Test
public void whenGetJavaObject_thenFieldsAreFilled() {
public void whenGetForecastByCityNameRequestAsJSON_thenReturnNotNull() {
final String forecastJson = getClient()
.forecast5Day3HourStep()
.byCityName("Minsk")
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.count(15)
.retrieve()
.asJSON();
Assert.assertTrue(forecastJson.startsWith("{"));
}
@Test
public void whenGetForecastByCityNameRequestAsXML_thenReturnNotNull() {
final String forecastXml = getClient()
.forecast5Day3HourStep()
.byCityName("Minsk")
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.count(15)
.retrieve()
.asXML();
Assert.assertTrue(forecastXml.startsWith("<"));
}
@Test
public void whenGetForecastByCityNameAndCountryCodeRequestAsJava_thenReturnNotNull() {
final Forecast forecast = getClient()
.forecast5Day3HourStep()
.byCityName("London")
.byCityName("Minsk", "BY")
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.count(15)
.retrieve()
.asJava();
Assert.assertNotNull(forecast);
System.out.println(forecast);
Assert.assertNotNull(forecast.getLocation());
Assert.assertNotNull(forecast.getWeatherForecasts());
for (WeatherForecast weatherForecast : forecast.getWeatherForecasts()) {
Assert.assertNotNull(weatherForecast.getState());
Assert.assertNotNull(weatherForecast.getDescription());
Assert.assertNotNull(weatherForecast.getForecastTime());
Assert.assertNotNull(weatherForecast.getTemperature());
Assert.assertNotNull(weatherForecast.getAtmosphericPressure());
Assert.assertNotNull(weatherForecast.getHumidity());
Assert.assertNotNull(weatherForecast.getWind());
}
}
@Test
public void whenGetForecastByCityNameAndCountryCodeRequestAsJSON_thenReturnNotNull() {
final String forecastJson = getClient()
.forecast5Day3HourStep()
.byCityName("Minsk", "by")
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.count(15)
.retrieve()
.asJSON();
Assert.assertTrue(forecastJson.startsWith("{"));
}
@Test
public void whenGetForecastByCityNameAndCountryCodeRequestAsXML_thenReturnNotNull() {
final String forecastXml = getClient()
.forecast5Day3HourStep()
.byCityName("Minsk", "by")
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.count(15)
.retrieve()
.asXML();
Assert.assertTrue(forecastXml.startsWith("<"));
}
@Test
public void whenGetForecastByCityNameAndStateCodeAndCountryCodeRequestAsJava_thenReturnNotNull() {
final Forecast forecast = getClient()
.forecast5Day3HourStep()
.byCityName("New York", "NY", "US")
.language(Language.CHINESE_TRADITIONAL)
.unitSystem(UnitSystem.STANDARD)
.count(15)
.retrieve()
.asJava();
Assert.assertNotNull(forecast);
Assert.assertNotNull(forecast.getLocation());
Assert.assertNotNull(forecast.getWeatherForecasts());
for (WeatherForecast weatherForecast : forecast.getWeatherForecasts()) {
Assert.assertNotNull(weatherForecast.getState());
Assert.assertNotNull(weatherForecast.getDescription());
Assert.assertNotNull(weatherForecast.getForecastTime());
Assert.assertNotNull(weatherForecast.getTemperature());
Assert.assertNotNull(weatherForecast.getAtmosphericPressure());
Assert.assertNotNull(weatherForecast.getHumidity());
Assert.assertNotNull(weatherForecast.getWind());
}
}
@Test
public void whenGetForecastByCityNameAndStateCodeAndCountryCodeRequestAsJSON_thenReturnNotNull() {
final String forecastJson = getClient()
.forecast5Day3HourStep()
.byCityName("New York", "NY", "US")
.language(Language.SPANISH)
.unitSystem(UnitSystem.IMPERIAL)
.count(15)
.retrieve()
.asJSON();
Assert.assertTrue(forecastJson.startsWith("{"));
}
@Test
public void whenGetForecastByCityNameAndStateCodeAndCountryCodeRequestAsXML_thenReturnNotNull() {
final String forecastXml = getClient()
.forecast5Day3HourStep()
.byCityName("New York", "NY", "US")
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.retrieve()
.asXML();
Assert.assertTrue(forecastXml.startsWith("<"));
}
@Test
public void whenGetForecastByCityIdRequestAsJava_thenReturnNotNull() {
final Forecast forecast = getClient()
.forecast5Day3HourStep()
.byCityId(350001514)
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.count(15)
.retrieve()
.asJava();
Assert.assertNotNull(forecast);
Assert.assertNotNull(forecast.getLocation());
Assert.assertNotNull(forecast.getWeatherForecasts());
for (WeatherForecast weatherForecast : forecast.getWeatherForecasts()) {
Assert.assertNotNull(weatherForecast.getState());
Assert.assertNotNull(weatherForecast.getDescription());
Assert.assertNotNull(weatherForecast.getForecastTime());
Assert.assertNotNull(weatherForecast.getTemperature());
Assert.assertNotNull(weatherForecast.getAtmosphericPressure());
Assert.assertNotNull(weatherForecast.getHumidity());
Assert.assertNotNull(weatherForecast.getWind());
}
}
@Test
public void whenGetForecastByCityIdRequestAsJSON_thenReturnNotNull() {
final String forecastJson = getClient()
.forecast5Day3HourStep()
.byCityId(350001514)
.language(Language.SPANISH)
.unitSystem(UnitSystem.IMPERIAL)
.count(15)
.retrieve()
.asJSON();
Assert.assertTrue(forecastJson.startsWith("{"));
}
@Test
public void whenGetForecastByCityIdRequestAsXML_thenReturnNotNull() {
final String forecastXml = getClient()
.forecast5Day3HourStep()
.byCityId(350001514)
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.retrieve()
.asXML();
Assert.assertTrue(forecastXml.startsWith("<"));
}
@Test
public void whenGetForecastByCoordinatesRequestAsJava_thenReturnNotNull() {
final Forecast forecast = getClient()
.forecast5Day3HourStep()
.byCoordinate(Coordinate.forValues(5, 5))
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.count(15)
.retrieve()
.asJava();
Assert.assertNotNull(forecast);
Assert.assertNotNull(forecast.getLocation());
Assert.assertNotNull(forecast.getWeatherForecasts());
for (WeatherForecast weatherForecast : forecast.getWeatherForecasts()) {
Assert.assertNotNull(weatherForecast.getState());
Assert.assertNotNull(weatherForecast.getDescription());
Assert.assertNotNull(weatherForecast.getForecastTime());
Assert.assertNotNull(weatherForecast.getTemperature());
Assert.assertNotNull(weatherForecast.getAtmosphericPressure());
Assert.assertNotNull(weatherForecast.getHumidity());
Assert.assertNotNull(weatherForecast.getWind());
}
}
@Test
public void whenGetForecastByCoordinatesRequestAsJSON_thenReturnNotNull() {
final String forecastJson = getClient()
.forecast5Day3HourStep()
.byCoordinate(Coordinate.forValues(5, 5))
.language(Language.SPANISH)
.unitSystem(UnitSystem.IMPERIAL)
.count(15)
.retrieve()
.asJSON();
Assert.assertTrue(forecastJson.startsWith("{"));
}
@Test
public void whenGetForecastByCoordinatesRequestAsXML_thenReturnNotNull() {
final String forecastXml = getClient()
.forecast5Day3HourStep()
.byCoordinate(Coordinate.forValues(5, 5))
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.retrieve()
.asXML();
Assert.assertTrue(forecastXml.startsWith("<"));
}
@Test
public void whenGetForecastByZipCodeInUSARequestAsJava_thenReturnNotNull() {
final Forecast forecast = getClient()
.forecast5Day3HourStep()
.byZipCodeInUSA("10005")
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.count(15)
.retrieve()
.asJava();
Assert.assertNotNull(forecast);
Assert.assertNotNull(forecast.getLocation());
Assert.assertNotNull(forecast.getWeatherForecasts());
for (WeatherForecast weatherForecast : forecast.getWeatherForecasts()) {
Assert.assertNotNull(weatherForecast.getState());
Assert.assertNotNull(weatherForecast.getDescription());
Assert.assertNotNull(weatherForecast.getForecastTime());
Assert.assertNotNull(weatherForecast.getTemperature());
Assert.assertNotNull(weatherForecast.getAtmosphericPressure());
Assert.assertNotNull(weatherForecast.getHumidity());
Assert.assertNotNull(weatherForecast.getWind());
}
}
@Test
public void whenGetForecastByZipCodeInUSARequestAsJSON_thenReturnNotNull() {
final String forecastJson = getClient()
.forecast5Day3HourStep()
.byZipCodeInUSA("10005")
.language(Language.SPANISH)
.unitSystem(UnitSystem.IMPERIAL)
.count(15)
.retrieve()
.asJSON();
Assert.assertTrue(forecastJson.startsWith("{"));
}
@Test
public void whenGetForecastByZipCodeInUSARequestAsXML_thenReturnNotNull() {
final String forecastXml = getClient()
.forecast5Day3HourStep()
.byZipCodeInUSA("10005")
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.retrieve()
.asXML();
Assert.assertTrue(forecastXml.startsWith("<"));
}
@Test
public void whenGetForecastByZipCodeAndCountryCodeRequestAsJava_thenReturnNotNull() {
final Forecast forecast = getClient()
.forecast5Day3HourStep()
.byZipCodeAndCountry("220015", "by")
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.count(15)
.retrieve()
.asJava();
Assert.assertNotNull(forecast);
Assert.assertNotNull(forecast.getLocation());
Assert.assertNotNull(forecast.getWeatherForecasts());
for (WeatherForecast weatherForecast : forecast.getWeatherForecasts()) {
Assert.assertNotNull(weatherForecast.getState());
Assert.assertNotNull(weatherForecast.getDescription());
Assert.assertNotNull(weatherForecast.getForecastTime());
Assert.assertNotNull(weatherForecast.getTemperature());
Assert.assertNotNull(weatherForecast.getAtmosphericPressure());
Assert.assertNotNull(weatherForecast.getHumidity());
Assert.assertNotNull(weatherForecast.getWind());
}
}
@Test
public void whenGetForecastByZipCodeAndCountryCodeRequestAsJSON_thenReturnNotNull() {
final String forecastJson = getClient()
.forecast5Day3HourStep()
.byZipCodeAndCountry("220015", "by")
.language(Language.SPANISH)
.unitSystem(UnitSystem.IMPERIAL)
.count(15)
.retrieve()
.asJSON();
Assert.assertTrue(forecastJson.startsWith("{"));
}
@Test
public void whenGetForecastByZipCodeAndCountryCodeRequestAsXML_thenReturnNotNull() {
final String forecastXml = getClient()
.forecast5Day3HourStep()
.byZipCodeAndCountry("220015", "by")
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.retrieve()
.asXML();
Assert.assertTrue(forecastXml.startsWith("<"));
}
@Test
public void whenGetForecastByCityNameAsyncRequestAsJava_thenReturnNotNull() throws ExecutionException, InterruptedException {
final CompletableFuture<Forecast> forecastFuture = getClient()
.forecast5Day3HourStep()
.byCityName("Minsk")
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.count(15)
.retrieveAsync()
.asJava();
Assert.assertNotNull(forecastFuture);
System.out.println(forecastFuture.get());
}
@Test
public void whenGetForecastByCityNameAsyncRequestAsJSON_thenReturnNotNull() throws ExecutionException, InterruptedException {
final CompletableFuture<String> forecastFuture = getClient()
.forecast5Day3HourStep()
.byCityId(350001514)
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.count(15)
.retrieveAsync()
.asJSON();
Assert.assertNotNull(forecastFuture);
System.out.println(forecastFuture.get());
}
@Test
public void whenGetForecastByCityNameAsyncRequestAsXML_thenReturnNotNull() throws ExecutionException, InterruptedException {
final CompletableFuture<String> forecastFuture = getClient()
.forecast5Day3HourStep()
.byCityId(350001514)
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.count(15)
.retrieveAsync()
.asXML();
Assert.assertNotNull(forecastFuture);
System.out.println(forecastFuture.get());
}
@Test(expected = InvalidAuthTokenException.class)
public void whenRequestCurrentWeatherWithInvalidApiKey_thenThrowAnException() {
OpenWeatherMapClient client = new OpenWeatherMapClient("invalidKey");
client
.forecast5Day3HourStep()
.byCityId(350001514)
.retrieve()
.asJSON();
}
@Test(expected = NoDataFoundException.class)
public void whenRequestCurrentWeatherForInvalidLocation_thenThrowAnException() {
getClient()
.forecast5Day3HourStep()
.byCityName("invalidCity")
.retrieve()
.asJava();
}
}