From 97473c4aae3fdd06f778f705bbe7a2cd5025be1a Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Fri, 31 May 2024 13:02:25 +0200 Subject: [PATCH] reorganize tests --- .../wtapi/{ => object}/CoordinateTest.java | 2 +- .../LightningMapsTest.java} | 7 +- .../wtapi/thunder/MockThunderProvider.java | 73 +++++++++++++++++++ .../wtapi/thunder/ThunderProviderTest.java | 36 +++++++++ .../MockWeatherProvider.java | 2 +- .../OpenWeatherMapTest.java} | 4 +- .../wtapi/{ => weather}/ProviderTest.java | 3 +- 7 files changed, 118 insertions(+), 9 deletions(-) rename src/test/java/eu/m724/wtapi/{ => object}/CoordinateTest.java (93%) rename src/test/java/eu/m724/wtapi/{TestLM.java => thunder/LightningMapsTest.java} (75%) create mode 100644 src/test/java/eu/m724/wtapi/thunder/MockThunderProvider.java create mode 100644 src/test/java/eu/m724/wtapi/thunder/ThunderProviderTest.java rename src/test/java/eu/m724/wtapi/{impl => weather}/MockWeatherProvider.java (98%) rename src/test/java/eu/m724/wtapi/{TestOWM.java => weather/OpenWeatherMapTest.java} (95%) rename src/test/java/eu/m724/wtapi/{ => weather}/ProviderTest.java (97%) diff --git a/src/test/java/eu/m724/wtapi/CoordinateTest.java b/src/test/java/eu/m724/wtapi/object/CoordinateTest.java similarity index 93% rename from src/test/java/eu/m724/wtapi/CoordinateTest.java rename to src/test/java/eu/m724/wtapi/object/CoordinateTest.java index 5869512..748c34d 100644 --- a/src/test/java/eu/m724/wtapi/CoordinateTest.java +++ b/src/test/java/eu/m724/wtapi/object/CoordinateTest.java @@ -1,4 +1,4 @@ -package eu.m724.wtapi; +package eu.m724.wtapi.object; import org.junit.Test; diff --git a/src/test/java/eu/m724/wtapi/TestLM.java b/src/test/java/eu/m724/wtapi/thunder/LightningMapsTest.java similarity index 75% rename from src/test/java/eu/m724/wtapi/TestLM.java rename to src/test/java/eu/m724/wtapi/thunder/LightningMapsTest.java index 050f23c..ba02ae9 100644 --- a/src/test/java/eu/m724/wtapi/TestLM.java +++ b/src/test/java/eu/m724/wtapi/thunder/LightningMapsTest.java @@ -1,4 +1,4 @@ -package eu.m724.wtapi; +package eu.m724.wtapi.thunder; import java.util.ArrayList; @@ -8,7 +8,7 @@ import eu.m724.wtapi.object.Coordinates; import eu.m724.wtapi.thunder.ThunderProvider; import eu.m724.wtapi.thunder.impl.lightningmaps.LightningMapsProvider; -public class TestLM { +public class LightningMapsTest { @Test public void lightningMapsTest() throws InterruptedException { ArrayList coordinatesList = new ArrayList<>(); @@ -28,7 +28,8 @@ public class TestLM { } System.out.printf("Strikes in the last 10s: %d\n", coordinatesList.size()); - System.out.printf("%f %f", coordinatesList.get(0).latitude, coordinatesList.get(0).longitude); + if (coordinatesList.size() > 0) // lightningmaps can be unreliable + System.out.printf("%f %f", coordinatesList.get(0).latitude, coordinatesList.get(0).longitude); } } diff --git a/src/test/java/eu/m724/wtapi/thunder/MockThunderProvider.java b/src/test/java/eu/m724/wtapi/thunder/MockThunderProvider.java new file mode 100644 index 0000000..e661c70 --- /dev/null +++ b/src/test/java/eu/m724/wtapi/thunder/MockThunderProvider.java @@ -0,0 +1,73 @@ +package eu.m724.wtapi.thunder; + +import java.util.ArrayList; +import java.util.Random; +import java.util.function.Consumer; + +import eu.m724.wtapi.object.Coordinates; +import eu.m724.wtapi.provider.exception.ProviderException; +import eu.m724.wtapi.thunder.ThunderProvider; +import eu.m724.wtapi.thunder.impl.lightningmaps.TimedStrike; + +public class MockThunderProvider extends ThunderProvider { + ArrayList> strikeHandlers = new ArrayList<>(); + ArrayList strikes = new ArrayList<>(); + Random rnd = new Random(); + + @Override + public void init() throws ProviderException { + System.out.println("mock thunder init"); + + } + + @Override + public void start() throws ProviderException { + System.out.println("mock thunder start"); + + long now = System.currentTimeMillis(); + + for (int i=0; i<40; i++) { + strikes.add(new TimedStrike(now + i * 100, + new Coordinates( + rnd.nextDouble(-90, 90), + rnd.nextDouble(-180, 180) + ))); + } + + } + + @Override + public void stop() { + System.out.println("mock thunder stop"); + + } + + @Override + public void tick() { + long now = System.currentTimeMillis(); + + while (strikes.size() > 0) { + TimedStrike str = strikes.get(0); + if (now > str.timestamp) { + System.out.printf("mock thunder given: %d\n", str.timestamp); + strikeHandlers.forEach(con -> con.accept(str.coordinates)); + strikes.remove(0); + } else break; + } + System.out.println("mock thunder tick"); + + } + + @Override + public void registerStrikeHandler(Consumer runnable) { + strikeHandlers.add(runnable); + System.out.println("mock thunder strike handler added"); + } + + @Override + public int getDelay() { + // TODO Auto-generated method stub + return 10000; + } + +} diff --git a/src/test/java/eu/m724/wtapi/thunder/ThunderProviderTest.java b/src/test/java/eu/m724/wtapi/thunder/ThunderProviderTest.java new file mode 100644 index 0000000..a02a45a --- /dev/null +++ b/src/test/java/eu/m724/wtapi/thunder/ThunderProviderTest.java @@ -0,0 +1,36 @@ +package eu.m724.wtapi.thunder; + +import java.util.ArrayList; + +import org.junit.Test; + +import eu.m724.wtapi.object.Coordinates; +import eu.m724.wtapi.thunder.ThunderProvider; + +public class ThunderProviderTest { + @Test + public void testThunderProvider() throws InterruptedException { + ArrayList coordinatesList = new ArrayList<>(); + + ThunderProvider provider = new MockThunderProvider(); + + provider.registerStrikeHandler(coordinates -> + coordinatesList.add(coordinates)); + + provider.init(); + + provider.start(); + + for (int i=0; i < 50; i++) { + provider.tick(); + Thread.sleep(50); + } + + provider.stop(); + + System.out.printf("Strikes in the last 5s: %d\n", coordinatesList.size()); + System.out.printf("%f %f", coordinatesList.get(0).latitude, coordinatesList.get(0).longitude); + + assert coordinatesList.size() == 25; + } +} diff --git a/src/test/java/eu/m724/wtapi/impl/MockWeatherProvider.java b/src/test/java/eu/m724/wtapi/weather/MockWeatherProvider.java similarity index 98% rename from src/test/java/eu/m724/wtapi/impl/MockWeatherProvider.java rename to src/test/java/eu/m724/wtapi/weather/MockWeatherProvider.java index d554ce1..a9bfa67 100644 --- a/src/test/java/eu/m724/wtapi/impl/MockWeatherProvider.java +++ b/src/test/java/eu/m724/wtapi/weather/MockWeatherProvider.java @@ -1,4 +1,4 @@ -package eu.m724.wtapi.impl; +package eu.m724.wtapi.weather; import java.util.concurrent.CompletableFuture; import eu.m724.wtapi.object.Coordinates; diff --git a/src/test/java/eu/m724/wtapi/TestOWM.java b/src/test/java/eu/m724/wtapi/weather/OpenWeatherMapTest.java similarity index 95% rename from src/test/java/eu/m724/wtapi/TestOWM.java rename to src/test/java/eu/m724/wtapi/weather/OpenWeatherMapTest.java index 2bffb8f..2098c9f 100644 --- a/src/test/java/eu/m724/wtapi/TestOWM.java +++ b/src/test/java/eu/m724/wtapi/weather/OpenWeatherMapTest.java @@ -1,4 +1,4 @@ -package eu.m724.wtapi; +package eu.m724.wtapi.weather; import static org.junit.Assert.assertNotNull; @@ -12,7 +12,7 @@ import eu.m724.wtapi.object.Weather; import eu.m724.wtapi.provider.WeatherProvider; import eu.m724.wtapi.provider.impl.openweathermap.OpenWeatherMapProvider; -public class TestOWM { +public class OpenWeatherMapTest { @Test public void openWeatherMapTest() throws InterruptedException, ExecutionException { String apiKey = "f3198d2a4ae3076e0aa47c821a253447"; diff --git a/src/test/java/eu/m724/wtapi/ProviderTest.java b/src/test/java/eu/m724/wtapi/weather/ProviderTest.java similarity index 97% rename from src/test/java/eu/m724/wtapi/ProviderTest.java rename to src/test/java/eu/m724/wtapi/weather/ProviderTest.java index bfe5cdb..912be8f 100644 --- a/src/test/java/eu/m724/wtapi/ProviderTest.java +++ b/src/test/java/eu/m724/wtapi/weather/ProviderTest.java @@ -1,4 +1,4 @@ -package eu.m724.wtapi; +package eu.m724.wtapi.weather; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -10,7 +10,6 @@ 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;