test: fixed EventDeserializerTest and implemented the GameEvent test part of it

This commit is contained in:
Yannik Bretschneider 2021-05-21 14:16:19 +02:00
parent 035bf2eb7c
commit a4c510279a

View File

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