From 1bd4e8a9c5a34bde3233eca12b92fb108d127590 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Mon, 17 May 2021 20:10:42 +0200 Subject: [PATCH] feat: partially created a test for EventDeserialzer, still needs some work --- .../json/ingame/EventDeserializerTest.java | 110 +++++++++++++++++- 1 file changed, 105 insertions(+), 5 deletions(-) diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EventDeserializerTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EventDeserializerTest.java index 856acc3..430d3a7 100644 --- a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EventDeserializerTest.java +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EventDeserializerTest.java @@ -9,10 +9,11 @@ import static org.assertj.core.api.Assertions.*; import uulm.teamname.marvelous.gamelibrary.IntVector2; import uulm.teamname.marvelous.gamelibrary.entities.*; import uulm.teamname.marvelous.gamelibrary.entities.Character; -import uulm.teamname.marvelous.gamelibrary.events.Event; -import uulm.teamname.marvelous.gamelibrary.events.EventType; -import uulm.teamname.marvelous.gamelibrary.events.GamestateEvent; -import uulm.teamname.marvelous.gamelibrary.events.NotificationEvent; +import uulm.teamname.marvelous.gamelibrary.events.*; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.concurrent.ThreadLocalRandom; import static org.mockito.Mockito.*; import static org.junit.jupiter.api.Assertions.*; @@ -174,7 +175,8 @@ class EventDeserializerTest { {"entityID": "P2", "ID": 1}, {"entityID": "P2", "ID": 2}, {"entityID": "P2", "ID": 3}, - {"entityID": "P2", "ID": 4} + {"entityID": "P2", "ID": 4}, + {"entityID": "P2", "ID": 5} ], "mapSize": [15, 300], "activeCharacter": {"entityID": "P2", "ID": 3}, @@ -192,4 +194,102 @@ class EventDeserializerTest { .isEqualTo(gamestate); } + @Test + void GameEventsTest() { + Iterator randomIntegers = ThreadLocalRandom.current().ints().iterator(); + + int repetitions = 3; + + // Keys in a Game Event are: + // message, roundCount, turnCount, playerWon, timeLeft, characterOrder, nextCharacter + + EventType type; + + final StringBuilder message = new StringBuilder(); + final int messageLength = 200; + final int minMessageLength = 5; + + int roundCount; + + int turnCount; + + int playerWon; + + int timeLeft; + + EntityID[] characterOrder; + final HashSet usedCharacters = new HashSet<>(); + + EntityID nextCharacter; + + for (int i = 0; i < repetitions; i++) { + // Get a random type of GameEvent + switch (Math.abs(randomIntegers.next()) % 8) { + case 0 -> type = EventType.TurnEvent; + case 1 -> type = EventType.WinEvent; + case 2 -> type = EventType.TurnTimeoutEvent; + case 3 -> type = EventType.TimeoutWarningEvent; + case 4 -> type = EventType.TimeoutEvent; + case 5 -> type = EventType.PauseStartEvent; + case 6 -> type = EventType.PauseStopEvent; + case 7 -> type = EventType.DisconnectEvent; + default -> throw new IllegalStateException("Unexpected value: " + Math.abs(randomIntegers.next()) % 8); + } + + // Get a random message string + int nameLength = Math.abs(randomIntegers.next() % 12); + for (int j = 0; j < (nameLength) + 3; j++) { + // append char between A and Z, and make lowercase 50% of the time + message.append((char) ( + (65 + Math.abs(randomIntegers.next() % 26)) + + 32 * Math.abs(randomIntegers.next() % 2) + )); + } + + // Get (realistic-seeming) random values for the int variables + roundCount = Math.abs(randomIntegers.next()) % 1000; + turnCount = Math.abs(randomIntegers.next()) % 6000; + playerWon = Math.abs(randomIntegers.next()) % 2 + 1; + timeLeft = Math.abs(randomIntegers.next()) % 600 + 1; // zero time left wouldn't make sense + + // Get character order + characterOrder = new EntityID[12]; + for (int j = 0; j < 12; j++) { + EntityID entityID; + do { // generate new ids until id was not added before. Inefficient, but whatever. + entityID = new EntityID( + Math.abs(randomIntegers.next()) % 2 == 1 ? EntityType.P1 : EntityType.P2, + Math.abs(randomIntegers.next()) % 6); + } while (!usedCharacters.add(entityID)); + characterOrder[i] = entityID; + } + + nextCharacter = characterOrder[Math.abs(randomIntegers.next()) % 12]; + + var gameEvent = new GameEvent(); + gameEvent.type = type; + gameEvent.message = message.toString(); + gameEvent.nextCharacter = nextCharacter; + gameEvent.timeLeft = timeLeft; + gameEvent.playerWon = playerWon; + gameEvent.characterOrder = characterOrder; + gameEvent.turnCount = turnCount; + gameEvent.roundCount = roundCount; + + var jsonRepresentingGameEvent = """ + { + "eventType": "%s", + + } + """; + + message.setLength(0); + usedCharacters.clear(); + } + } + + // TODO: Write tests for the other event types + // NOTE: it is not required to check all, only the categories (like GamestateEvent or CharacterEvent) + // because of unchecked event building in the deserializer + } \ No newline at end of file