This commit is contained in:
Minecon724 2024-05-28 19:27:25 +02:00
parent fb563c1bf2
commit 21a3ed1563
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
4 changed files with 146 additions and 18 deletions

View file

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

View file

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

View file

@ -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<Weather> weatherFuture =
provider.getWeather(new Coordinates(0, 0));
Weather weather = weatherFuture.get();
assertNotNull(weather);
assert provider.getLastRequestQuota() == 1;
CompletableFuture<Weather[]> 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<Weather> 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<Weather[]> 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 }));
}
}

View file

@ -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<Weather> getWeather(Coordinates coordinates) {
req++;
if (coordinates == null)
throw new NullPointerException("no coordinates passed");
@ -29,6 +39,9 @@ public class MockWeatherAPIWrapper extends WeatherAPIWrapper {
CompletableFuture<Weather> completableFuture =
new CompletableFuture<>();
if (faulty)
completableFuture.completeExceptionally(new ServerProviderException("server is on vacation rn"));
else
completableFuture.complete(new Weather(WeatherState.CLEAR, 0));
return completableFuture;
@ -36,7 +49,10 @@ public class MockWeatherAPIWrapper extends WeatherAPIWrapper {
@Override
public CompletableFuture<Weather[]> 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,14 +60,19 @@ public class MockWeatherAPIWrapper extends WeatherAPIWrapper {
CompletableFuture<Weather[]> completableFuture =
new CompletableFuture<>();
int pairs = coordinateses.length / 2;
Weather[] weathers = new Weather[pairs];
Weather[] weathers = new Weather[len];
for (int i=0; i<len; i++) {
if (coordinateses[i] == null)
throw new IllegalArgumentException("a coordinate is null");
for (int i=0; i<pairs; i++) {
weathers[i] = new Weather(WeatherState.CLEAR, 1);
}
if (faulty)
completableFuture.completeExceptionally(new QuotaExceededException("im too lazy lmao", 60));
else
completableFuture.complete(weathers);
return completableFuture;
@ -59,12 +80,12 @@ public class MockWeatherAPIWrapper extends WeatherAPIWrapper {
@Override
public int getQuotaHourly() {
return 69;
return 5;
}
@Override
public int getLastRequestQuota() {
return 1;
return this.req; // ONCE AGAIN TJHIS IS NOT HOW IT SHOULD BE DONE
}
}