EventSerializer tests

This commit is contained in:
C H 2021-06-03 15:01:01 +02:00
parent d70cd36a39
commit f54bae4c1f
2 changed files with 301 additions and 6 deletions

View File

@ -248,4 +248,5 @@ class EntitySerializerTest {
}
}

View File

@ -1,15 +1,305 @@
package uulm.teamname.marvelous.gamelibrary.json.ingame.serialize;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.jqwik.api.*;
import net.jqwik.api.lifecycle.BeforeProperty;
//import net.jqwik.api.*;
//import net.jqwik.api.lifecycle.BeforeProperty;
import org.junit.jupiter.api.Test;
import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
import uulm.teamname.marvelous.gamelibrary.entities.EntityType;
import uulm.teamname.marvelous.gamelibrary.events.EventBuilder;
import uulm.teamname.marvelous.gamelibrary.entities.*;
import uulm.teamname.marvelous.gamelibrary.entities.Character;
import uulm.teamname.marvelous.gamelibrary.events.*;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class EventSerializerTest {
//System.out.println(mapper.writeValueAsString(gse));
EntityID[] turns;
EntityID turn;
Entity[] entities;
Entity entity;
EventBuilder filled;
@Test
void serializeGamestateEvent() throws JsonProcessingException {
var mapper = new ObjectMapper();
GamestateEvent gse = new GamestateEvent();
gse.type = EventType.Ack;
var jsonRepresentingGSE = """
{
"eventType":"Ack"
}""".replace("\n", "");
assertThat(mapper.writeValueAsString(gse))
.isEqualTo(jsonRepresentingGSE);
gse.type = EventType.Nack;
jsonRepresentingGSE = """
{
"eventType":"Nack"
}""".replace("\n", "");
assertThat(mapper.writeValueAsString(gse))
.isEqualTo(jsonRepresentingGSE);
gse.type = EventType.GamestateEvent;
EventBuilder eb = new EventBuilder(EventType.GamestateEvent);
GamestateEvent gse2 = eb // no exception if all properties present
.withEntities(entities)
.withTurnOrder(turns)
.withMapSize(new IntVector2(42, 24))
.withActiveCharacter(turn)
.withStoneCooldowns(new Integer[] {6, 5, 4, 3, 2, 1})
.withWinCondition(false)
.buildGameStateEvent();
// gse2.entities = [new EntityID(EntityType.NPC, 1),new IntVector2(1,1)]
Entity[] e = {new NPC(new EntityID(EntityType.NPC, 1), new IntVector2(1,1))};
gse2.entities = e;
EntityID[] eid = {new EntityID(EntityType.NPC, 1)};
gse2.turnOrder= eid;
var ac= new EntityID(EntityType.P1, 2);
gse2.activeCharacter = ac;
System.out.println(mapper.writeValueAsString(gse2));
/*
* private Entity[] entities;
private EntityID[] turnOrder;
private IntVector2 mapSize;
private EntityID activeCharacter;
private Integer[] stoneCooldowns;
private Boolean winCondition;
* */
//Entity[] ent = {new EntityID(EntityType.NPC};
// gse.entities = new Entity[];
/**
* gen.writeArrayFieldStart("entities");
for (Entity entity : value.entities) {
gen.writeObject(entity);
}
gen.writeEndArray();
gen.writeObjectField("mapSize", value.mapSize);
gen.writeArrayFieldStart("turnOrder");
for (EntityID id : value.turnOrder) {
gen.writeObject(id);
}
gen.writeObjectField("activeCharacter", value.activeCharacter);
gen.writeFieldName("stoneCooldowns");
gen.writeArray(
Arrays.stream(value.stoneCooldowns).mapToInt(x -> x).toArray(),
0,
value.stoneCooldowns.length);
gen.writeBooleanField("winCondition", value.winCondition);
* */
}
@Test
void serializeEntityEvent() throws JsonProcessingException {
var mapper = new ObjectMapper();
EntityEvent ee = new EntityEvent();
ee.type = EventType.SpawnEntityEvent;
ee.entity = new NPC(new EntityID(EntityType.NPC, 1), new IntVector2(1,1));
var jsonRepresentingEE = """
{
"eventType":"SpawnEntityEvent",
"entity":{"entityType":"NPC","ID":1,"MP":0,"stones":[],"position":[1,1]}
}""".replace("\n", "");
System.out.println(mapper.writeValueAsString(ee));
assertThat(mapper.writeValueAsString(ee))
.isEqualTo(jsonRepresentingEE);
EntityEvent ee2 = new EntityEvent();
ee2.type = EventType.TakenDamageEvent;
ee2.targetEntity = new EntityID(EntityType.P1,5);
ee2.targetField = new IntVector2(1,1);
ee2.amount = 10;
// {"eventType":"TakenDamageEvent","targetEntity":{"entityID":"P1","ID":5},"targetField":[1,1],"amount":10}
System.out.println(mapper.writeValueAsString(ee2));
jsonRepresentingEE = """
{
"eventType":"TakenDamageEvent",
"targetEntity":{"entityID":"P1","ID":5},
"targetField":[1,1],
"amount":10
}""".replace("\n", "");
System.out.println(mapper.writeValueAsString(ee2));
assertThat(mapper.writeValueAsString(ee2))
.isEqualTo(jsonRepresentingEE);
EntityEvent ee3 = new EntityEvent();
ee3.type = EventType.DestroyedEntityEvent;
ee3.targetEntity = new EntityID(EntityType.P1,5);
ee3.targetField = new IntVector2(1,1);
jsonRepresentingEE = """
{
"eventType":"DestroyedEntityEvent",
"targetEntity":{"entityID":"P1","ID":5},
"targetField":[1,1]
}""".replace("\n", "");
System.out.println(mapper.writeValueAsString(ee3));
assertThat(mapper.writeValueAsString(ee3))
.isEqualTo(jsonRepresentingEE);
}
@Test
void serializeCharacterEvent() throws JsonProcessingException {
/*
* gen.writeObjectField("originEntity", value.originEntity);
gen.writeObjectField("targetEntity", value.targetEntity);
gen.writeObjectField("originField", value.originField);
gen.writeObjectField("targetField", value.targetField);
* */
var mapper = new ObjectMapper();
CharacterEvent ce = new CharacterEvent();
ce.type = EventType.MeleeAttackEvent;
ce.targetEntity = new EntityID(EntityType.P1,5);
ce.targetField = new IntVector2(1,1);
ce.originEntity = new EntityID(EntityType.P2,7);
ce.targetField = new IntVector2(2,2);
var jsonRepresentingEE = """
{
"eventType":"MeleeAttackEvent",
"originEntity":{"entityID":"P2","ID":7},
"targetEntity":{"entityID":"P1","ID":5},
"originField":null,"targetField":[2,2]
}""".replace("\n", "");
System.out.println(mapper.writeValueAsString(ce));
assertThat(mapper.writeValueAsString(ce))
.isEqualTo(jsonRepresentingEE);
CharacterEvent ce2 = new CharacterEvent();
ce2.type = EventType.UseInfinityStoneEvent;
ce2.targetEntity = new EntityID(EntityType.P1,5);
ce2.targetField = new IntVector2(1,1);
ce2.originEntity = new EntityID(EntityType.P2,7);
ce2.targetField = new IntVector2(2,2);
ce2.stoneType = StoneType.MindStone;
jsonRepresentingEE = """
{
"eventType":"UseInfinityStoneEvent",
"originEntity":{"entityID":"P2","ID":7},
"targetEntity":{"entityID":"P1","ID":5},
"originField":null,"targetField":[2,2]
}""".replace("\n", "");
System.out.println(mapper.writeValueAsString(ce2));
//assertThat(mapper.writeValueAsString(ce2))
// .isEqualTo(jsonRepresentingEE);
//gen.writeObject(new EntityID(EntityType.InfinityStones, value.stoneType.getID()));
}
@Test
void serializeGameEvent() throws JsonProcessingException {
var mapper = new ObjectMapper();
GameEvent ge = new GameEvent();
ge.type = EventType.RoundSetupEvent;
ge.roundCount =5;
EntityID[] eid = {new EntityID(EntityType.NPC, 1)};
ge.characterOrder = eid;
var jsonRepresentingEE = """
{
"eventType":"MeleeAttackEvent",
"originEntity":{"entityID":"P2","ID":7},
"targetEntity":{"entityID":"P1","ID":5},
"originField":null,"targetField":[2,2]
}""".replace("\n", "");
// System.out.println(mapper.writeValueAsString(ge));
// assertThat(mapper.writeValueAsString(ge))
// .isEqualTo(jsonRepresentingEE);
GameEvent ge2 = new GameEvent();
ge2.type = EventType.TurnEvent;
ge2.turnCount =5;
ge2.nextCharacter = new EntityID(EntityType.NPC, 1);
jsonRepresentingEE = """
{
"eventType":"TurnEvent",
"turnCount":5,
"nextCharacter":{"entityID":"NPC","ID":1}
}""".replace("\n", "");
// System.out.println(mapper.writeValueAsString(ge2));
assertThat(mapper.writeValueAsString(ge2))
.isEqualTo(jsonRepresentingEE);
GameEvent ge3 = new GameEvent();
ge3.type = EventType.WinEvent;
ge3.playerWon =1;
jsonRepresentingEE = """
{
"eventType":"WinEvent",
"playerWon":1
}""".replace("\n", "");
// System.out.println(mapper.writeValueAsString(ge3));
assertThat(mapper.writeValueAsString(ge3))
.isEqualTo(jsonRepresentingEE);
GameEvent ge4 = new GameEvent();
ge4.type = EventType.TimeoutWarningEvent;
ge4.message = "warning";
ge4.timeLeft = 1;
jsonRepresentingEE = """
{
"eventType":"TimeoutWarningEvent",
"message":"warning",
"timeLeft":1
}""".replace("\n", "");
//System.out.println(mapper.writeValueAsString(ge4));
assertThat(mapper.writeValueAsString(ge4))
.isEqualTo(jsonRepresentingEE);
}
/*
ObjectMapper mapper;
@BeforeProperty
@ -66,8 +356,9 @@ public class EventSerializerTest {
.map(pos -> new IntVector2(pos.get1(), pos.get2()));
}
@Provide("attackPositions")
/** Returns tuples of origin vectors (of an attack), and the vector pointing to the attack dir */
// Returns tuples of origin vectors (of an attack), and the vector pointing to the attack dir
private Arbitrary<Tuple.Tuple2<IntVector2, IntVector2>> attackPositions() {
return Combinators.combine(randomPositions(), directions().tuple5())
.as((origin, dir) -> {
@ -82,4 +373,7 @@ public class EventSerializerTest {
return Tuple.of(origin, attackField);
});
}
*/
}