test: fixed EventDeserializerTest and implemented the GameEvent test part of it
This commit is contained in:
parent
035bf2eb7c
commit
a4c510279a
@ -11,6 +11,7 @@ import uulm.teamname.marvelous.gamelibrary.entities.*;
|
|||||||
import uulm.teamname.marvelous.gamelibrary.entities.Character;
|
import uulm.teamname.marvelous.gamelibrary.entities.Character;
|
||||||
import uulm.teamname.marvelous.gamelibrary.events.*;
|
import uulm.teamname.marvelous.gamelibrary.events.*;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
@ -185,9 +186,9 @@ class EventDeserializerTest {
|
|||||||
}
|
}
|
||||||
""";
|
""";
|
||||||
|
|
||||||
System.out.println("=== DEBUG OUTPUT ===\n"
|
//System.out.println("=== DEBUG OUTPUT ===\n"
|
||||||
+ mapper.readValue(jsonRepresentingGamestate, Event.class)
|
// + mapper.readValue(jsonRepresentingGamestate, Event.class)
|
||||||
+ "\n====================");
|
// + "\n====================");
|
||||||
|
|
||||||
assertThat((GamestateEvent) mapper.readValue(jsonRepresentingGamestate, Event.class))
|
assertThat((GamestateEvent) mapper.readValue(jsonRepresentingGamestate, Event.class))
|
||||||
.withFailMessage("GamestateEvent was not properly deserialized")
|
.withFailMessage("GamestateEvent was not properly deserialized")
|
||||||
@ -195,10 +196,10 @@ class EventDeserializerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void GameEventsTest() {
|
void GameEventsTest() throws JsonProcessingException {
|
||||||
Iterator<Integer> randomIntegers = ThreadLocalRandom.current().ints().iterator();
|
Iterator<Integer> randomIntegers = ThreadLocalRandom.current().ints().iterator();
|
||||||
|
|
||||||
int repetitions = 3;
|
int repetitions = 1000;
|
||||||
|
|
||||||
// Keys in a Game Event are:
|
// Keys in a Game Event are:
|
||||||
// message, roundCount, turnCount, playerWon, timeLeft, characterOrder, nextCharacter
|
// message, roundCount, turnCount, playerWon, timeLeft, characterOrder, nextCharacter
|
||||||
@ -219,6 +220,7 @@ class EventDeserializerTest {
|
|||||||
|
|
||||||
EntityID[] characterOrder;
|
EntityID[] characterOrder;
|
||||||
final HashSet<EntityID> usedCharacters = new HashSet<>();
|
final HashSet<EntityID> usedCharacters = new HashSet<>();
|
||||||
|
StringBuilder jsonRepresentingCharacterOrder = new StringBuilder();
|
||||||
|
|
||||||
EntityID nextCharacter;
|
EntityID nextCharacter;
|
||||||
|
|
||||||
@ -261,7 +263,7 @@ class EventDeserializerTest {
|
|||||||
Math.abs(randomIntegers.next()) % 2 == 1 ? EntityType.P1 : EntityType.P2,
|
Math.abs(randomIntegers.next()) % 2 == 1 ? EntityType.P1 : EntityType.P2,
|
||||||
Math.abs(randomIntegers.next()) % 6);
|
Math.abs(randomIntegers.next()) % 6);
|
||||||
} while (!usedCharacters.add(entityID));
|
} while (!usedCharacters.add(entityID));
|
||||||
characterOrder[i] = entityID;
|
characterOrder[j] = entityID;
|
||||||
}
|
}
|
||||||
|
|
||||||
nextCharacter = characterOrder[Math.abs(randomIntegers.next()) % 12];
|
nextCharacter = characterOrder[Math.abs(randomIntegers.next()) % 12];
|
||||||
@ -276,14 +278,47 @@ class EventDeserializerTest {
|
|||||||
gameEvent.turnCount = turnCount;
|
gameEvent.turnCount = turnCount;
|
||||||
gameEvent.roundCount = roundCount;
|
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 = """
|
var jsonRepresentingGameEvent = """
|
||||||
{
|
{
|
||||||
"eventType": "%s",
|
"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);
|
message.setLength(0);
|
||||||
|
jsonRepresentingCharacterOrder.setLength(0);
|
||||||
usedCharacters.clear();
|
usedCharacters.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user