From a4c510279a35bfd10da62b3026eb2ed4c3c369ad Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Fri, 21 May 2021 14:16:19 +0200 Subject: [PATCH] test: fixed EventDeserializerTest and implemented the GameEvent test part of it --- .../json/ingame/EventDeserializerTest.java | 51 ++++++++++++++++--- 1 file changed, 43 insertions(+), 8 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 430d3a7..e741e67 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 @@ -11,6 +11,7 @@ import uulm.teamname.marvelous.gamelibrary.entities.*; import uulm.teamname.marvelous.gamelibrary.entities.Character; import uulm.teamname.marvelous.gamelibrary.events.*; +import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; import java.util.concurrent.ThreadLocalRandom; @@ -185,9 +186,9 @@ class EventDeserializerTest { } """; - System.out.println("=== DEBUG OUTPUT ===\n" - + mapper.readValue(jsonRepresentingGamestate, Event.class) - + "\n===================="); + //System.out.println("=== DEBUG OUTPUT ===\n" + // + mapper.readValue(jsonRepresentingGamestate, Event.class) + // + "\n===================="); assertThat((GamestateEvent) mapper.readValue(jsonRepresentingGamestate, Event.class)) .withFailMessage("GamestateEvent was not properly deserialized") @@ -195,10 +196,10 @@ class EventDeserializerTest { } @Test - void GameEventsTest() { + void GameEventsTest() throws JsonProcessingException { Iterator randomIntegers = ThreadLocalRandom.current().ints().iterator(); - int repetitions = 3; + int repetitions = 1000; // Keys in a Game Event are: // message, roundCount, turnCount, playerWon, timeLeft, characterOrder, nextCharacter @@ -219,6 +220,7 @@ class EventDeserializerTest { EntityID[] characterOrder; final HashSet usedCharacters = new HashSet<>(); + StringBuilder jsonRepresentingCharacterOrder = new StringBuilder(); EntityID nextCharacter; @@ -261,7 +263,7 @@ class EventDeserializerTest { Math.abs(randomIntegers.next()) % 2 == 1 ? EntityType.P1 : EntityType.P2, Math.abs(randomIntegers.next()) % 6); } while (!usedCharacters.add(entityID)); - characterOrder[i] = entityID; + characterOrder[j] = entityID; } nextCharacter = characterOrder[Math.abs(randomIntegers.next()) % 12]; @@ -276,14 +278,47 @@ class EventDeserializerTest { gameEvent.turnCount = turnCount; gameEvent.roundCount = roundCount; + // create JSON representation of CharacterOrder + + jsonRepresentingCharacterOrder.append('[').append('\n'); + for (EntityID id: characterOrder) { + jsonRepresentingCharacterOrder.append('\t') + .append(String.format( + "{ \"entityID\": \"%s\", \"ID\": %d}" + , id.type, id.id)) + .append(',') + .append('\n'); + } + jsonRepresentingCharacterOrder.setLength(jsonRepresentingCharacterOrder.length() - 2); // remove trailing , + jsonRepresentingCharacterOrder.append('\n').append(']'); + var jsonRepresentingGameEvent = """ { "eventType": "%s", - + "message": "%s", + "roundCount": %d, + "turnCount": %d, + "playerWon": %d, + "timeLeft": %d, + "characterOrder": %s, + "nextCharacter": {"entityID": "%s", "ID": %d} } - """; + """.formatted( + gameEvent.type, + gameEvent.message, + gameEvent.roundCount, + gameEvent.turnCount, + gameEvent.playerWon, + gameEvent.timeLeft, + jsonRepresentingCharacterOrder.toString(), + gameEvent.nextCharacter.type, gameEvent.nextCharacter.id + ); + + assertThat((GameEvent) mapper.readValue(jsonRepresentingGameEvent, Event.class)) + .isEqualTo(gameEvent); message.setLength(0); + jsonRepresentingCharacterOrder.setLength(0); usedCharacters.clear(); } }