diff --git a/src/main/java/eu/m724/wtapi/provider/api/WeatherAPIWrapper.java b/src/main/java/eu/m724/wtapi/provider/WeatherProvider.java similarity index 89% rename from src/main/java/eu/m724/wtapi/provider/api/WeatherAPIWrapper.java rename to src/main/java/eu/m724/wtapi/provider/WeatherProvider.java index 25c979c..ceda810 100644 --- a/src/main/java/eu/m724/wtapi/provider/api/WeatherAPIWrapper.java +++ b/src/main/java/eu/m724/wtapi/provider/WeatherProvider.java @@ -1,4 +1,4 @@ -package eu.m724.wtapi.provider.api; +package eu.m724.wtapi.provider; import java.time.Duration; import java.util.concurrent.CompletableFuture; @@ -7,7 +7,7 @@ import eu.m724.wtapi.object.Coordinates; import eu.m724.wtapi.object.Weather; import eu.m724.wtapi.provider.exception.ProviderException; -public abstract class WeatherAPIWrapper { +public abstract class WeatherProvider { /** * initialize the api @@ -48,7 +48,7 @@ public abstract class WeatherAPIWrapper { * estimates minimum delay between calls given last request * @return milliseconds */ - public long estimateDelay() { - return (long) Math.ceil(this.getQuotaHourly() / 2.0 / 60 / 60 / 1000); + public int estimateDelay() { + return (int) Math.ceil(this.getQuotaHourly() / 2.0 / 60 / 60 / 1000); } } diff --git a/src/main/java/eu/m724/wtapi/provider/exception/QuotaExceededException.java b/src/main/java/eu/m724/wtapi/provider/exception/QuotaExceededException.java index 90f6e42..9f26b0f 100644 --- a/src/main/java/eu/m724/wtapi/provider/exception/QuotaExceededException.java +++ b/src/main/java/eu/m724/wtapi/provider/exception/QuotaExceededException.java @@ -7,14 +7,14 @@ public class QuotaExceededException extends ProviderException { private static final long serialVersionUID = 7550042052176034614L; - private Long retryMs; + private int retryMs; /** * * @param message * @param retryMs SUGGESTION after how many ms to retry. it can be null */ - public QuotaExceededException(String message, Long retryMs) { + public QuotaExceededException(String message, int retryMs) { super(message); this.retryMs = retryMs; } @@ -23,7 +23,7 @@ public class QuotaExceededException extends ProviderException { * * @return SUGGESTION after how many ms to retry. it can be null */ - public Long getRetryIn() { + public int getRetryIn() { return this.retryMs; } diff --git a/src/test/java/eu/m724/wtapi/ProviderTest.java b/src/test/java/eu/m724/wtapi/ProviderTest.java new file mode 100644 index 0000000..301b52b --- /dev/null +++ b/src/test/java/eu/m724/wtapi/ProviderTest.java @@ -0,0 +1,107 @@ +package eu.m724.wtapi; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.fail; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +import org.junit.Test; + +import eu.m724.wtapi.impl.MockWeatherProvider; +import eu.m724.wtapi.object.Coordinates; +import eu.m724.wtapi.object.Weather; +import eu.m724.wtapi.provider.WeatherProvider; +import eu.m724.wtapi.provider.exception.ProviderException; +import eu.m724.wtapi.provider.exception.QuotaExceededException; + +public class ProviderTest { + @Test + public void testProvider() throws InterruptedException, ExecutionException { + WeatherProvider provider = new MockWeatherProvider(false); + provider.init(); + assert provider.getQuotaHourly() == 5; + assert provider.getLastRequestQuota() == 0; + + CompletableFuture weatherFuture = + provider.getWeather(new Coordinates(0, 0)); + + Weather weather = weatherFuture.get(); + assertNotNull(weather); + + assert provider.getLastRequestQuota() == 1; + + + CompletableFuture weathersFuture = + provider.getWeatherBulk(new Coordinates[] { + new Coordinates(0, 0), + new Coordinates(0, 100) + }); + + Weather[] weathers = weathersFuture.get(); + assertNotNull(weathers); + assert weathers.length == 2; + assertNotNull(weathers[0]); + assertNotNull(weathers[1]); + + assert provider.getLastRequestQuota() == 2; + } + + @Test + public void testFaultyProvider() throws InterruptedException { + WeatherProvider provider = new MockWeatherProvider(true); + assert provider.getLastRequestQuota() == 0; + + CompletableFuture weatherFuture = + provider.getWeather(new Coordinates(0, 0)); + + Weather weather = null; + + try { + weather = weatherFuture.get(); + fail("Shouldn't pass"); + } catch (ExecutionException e) { + assert e.getCause() instanceof ProviderException; + } + + assertNull(weather); + + assert provider.getLastRequestQuota() == 1; + + + Weather[] weathers = null; + + CompletableFuture weathersFuture = + provider.getWeatherBulk(new Coordinates[] { + new Coordinates(0, 0), + new Coordinates(0, 100) + }); + + try { + weathers = weathersFuture.get(); + fail("Shouldn't pass"); + } catch (ExecutionException e) { + QuotaExceededException qee = (QuotaExceededException) e.getCause(); + assert qee.getRetryIn() == 60; + } + + assert provider.getLastRequestQuota() == 2; + assertNull(weathers); + + } + + @Test + public void testInputErrors() { + WeatherProvider provider = new MockWeatherProvider(false); + + assertThrows(NullPointerException.class, () -> provider.getWeather(null)); + assertThrows(NullPointerException.class, () -> provider.getWeatherBulk(null)); + + assertThrows(IllegalArgumentException.class, () -> provider.getWeatherBulk(new Coordinates[0])); + + assertThrows(IllegalArgumentException.class, () -> + provider.getWeatherBulk(new Coordinates[] { new Coordinates(0, 0), null })); + } +} diff --git a/src/main/java/eu/m724/wtapi/provider/api/MockWeatherAPIWrapper.java b/src/test/java/eu/m724/wtapi/impl/MockWeatherProvider.java similarity index 55% rename from src/main/java/eu/m724/wtapi/provider/api/MockWeatherAPIWrapper.java rename to src/test/java/eu/m724/wtapi/impl/MockWeatherProvider.java index 01148c6..97a7872 100644 --- a/src/main/java/eu/m724/wtapi/provider/api/MockWeatherAPIWrapper.java +++ b/src/test/java/eu/m724/wtapi/impl/MockWeatherProvider.java @@ -1,4 +1,4 @@ -package eu.m724.wtapi.provider.api; +package eu.m724.wtapi.impl; import java.time.Duration; import java.time.temporal.TemporalUnit; @@ -8,18 +8,28 @@ import java.util.function.Supplier; import eu.m724.wtapi.object.Coordinates; import eu.m724.wtapi.object.Weather; import eu.m724.wtapi.object.WeatherState; +import eu.m724.wtapi.provider.WeatherProvider; import eu.m724.wtapi.provider.exception.ProviderException; +import eu.m724.wtapi.provider.exception.QuotaExceededException; +import eu.m724.wtapi.provider.exception.ServerProviderException; -public class MockWeatherAPIWrapper extends WeatherAPIWrapper { +public class MockWeatherProvider extends WeatherProvider { + private int req; // THIS IS NOT HOW IT SHOULD BE DONE + private boolean faulty; + + public MockWeatherProvider(boolean faulty) { + this.faulty = faulty; + } @Override public void init() throws ProviderException { System.out.println("hello from mock provider"); - } @Override public CompletableFuture getWeather(Coordinates coordinates) { + req++; + if (coordinates == null) throw new NullPointerException("no coordinates passed"); @@ -29,14 +39,20 @@ public class MockWeatherAPIWrapper extends WeatherAPIWrapper { CompletableFuture completableFuture = new CompletableFuture<>(); - completableFuture.complete(new Weather(WeatherState.CLEAR, 0)); + if (faulty) + completableFuture.completeExceptionally(new ServerProviderException("server is on vacation rn")); + else + completableFuture.complete(new Weather(WeatherState.CLEAR, 0)); return completableFuture; } @Override public CompletableFuture getWeatherBulk(Coordinates[] coordinateses) { - if (coordinateses.length == 0) + int len = coordinateses.length; + req++; + + if (len == 0) throw new IllegalArgumentException("no coordinates passed"); System.out.printf("mock getting weather for multiple coords"); @@ -44,27 +60,32 @@ public class MockWeatherAPIWrapper extends WeatherAPIWrapper { CompletableFuture completableFuture = new CompletableFuture<>(); - int pairs = coordinateses.length / 2; - Weather[] weathers = new Weather[pairs]; + Weather[] weathers = new Weather[len]; - for (int i=0; i