From 34dcc75400edd87809d25baf9afbc46ff8869de6 Mon Sep 17 00:00:00 2001 From: C H Date: Thu, 3 Jun 2021 15:15:55 +0200 Subject: [PATCH] Update EventSerializerTest.java --- .../ingame/serialize/EventSerializerTest.java | 101 +++++++++--------- 1 file changed, 52 insertions(+), 49 deletions(-) diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/serialize/EventSerializerTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/serialize/EventSerializerTest.java index be25cfe..c541660 100644 --- a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/serialize/EventSerializerTest.java +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/serialize/EventSerializerTest.java @@ -10,6 +10,8 @@ import uulm.teamname.marvelous.gamelibrary.entities.*; import uulm.teamname.marvelous.gamelibrary.entities.Character; import uulm.teamname.marvelous.gamelibrary.events.*; +import java.util.HashMap; + import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -72,43 +74,11 @@ public class EventSerializerTest { 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); - * */ + // ERROR!!! "Can not write a field name, expecting a value" bei active Character + // assertThat(mapper.writeValueAsString(gse2)) + // .isEqualTo(jsonRepresentingGSE); } @@ -127,7 +97,7 @@ public class EventSerializerTest { "entity":{"entityType":"NPC","ID":1,"MP":0,"stones":[],"position":[1,1]} }""".replace("\n", ""); - System.out.println(mapper.writeValueAsString(ee)); + // System.out.println(mapper.writeValueAsString(ee)); assertThat(mapper.writeValueAsString(ee)) .isEqualTo(jsonRepresentingEE); @@ -138,8 +108,7 @@ public class EventSerializerTest { 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)); + // System.out.println(mapper.writeValueAsString(ee2)); jsonRepresentingEE = """ { @@ -149,7 +118,7 @@ public class EventSerializerTest { "amount":10 }""".replace("\n", ""); - System.out.println(mapper.writeValueAsString(ee2)); + // System.out.println(mapper.writeValueAsString(ee2)); assertThat(mapper.writeValueAsString(ee2)) .isEqualTo(jsonRepresentingEE); @@ -166,7 +135,7 @@ public class EventSerializerTest { "targetField":[1,1] }""".replace("\n", ""); - System.out.println(mapper.writeValueAsString(ee3)); + //System.out.println(mapper.writeValueAsString(ee3)); assertThat(mapper.writeValueAsString(ee3)) .isEqualTo(jsonRepresentingEE); } @@ -174,13 +143,6 @@ public class EventSerializerTest { @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(); @@ -198,7 +160,7 @@ public class EventSerializerTest { "originField":null,"targetField":[2,2] }""".replace("\n", ""); - System.out.println(mapper.writeValueAsString(ce)); + // System.out.println(mapper.writeValueAsString(ce)); assertThat(mapper.writeValueAsString(ce)) .isEqualTo(jsonRepresentingEE); @@ -219,6 +181,8 @@ public class EventSerializerTest { }""".replace("\n", ""); System.out.println(mapper.writeValueAsString(ce2)); + + // ERROR !!! "Can not start an object, expecting field name (context: Object)" bei stoneType //assertThat(mapper.writeValueAsString(ce2)) // .isEqualTo(jsonRepresentingEE); @@ -247,7 +211,10 @@ public class EventSerializerTest { "originField":null,"targetField":[2,2] }""".replace("\n", ""); - // System.out.println(mapper.writeValueAsString(ge)); + System.out.println(mapper.writeValueAsString(ge)); + + // ERROR !!! "Current context not Array but Object" bei characterorder + // assertThat(mapper.writeValueAsString(ge)) // .isEqualTo(jsonRepresentingEE); @@ -296,8 +263,44 @@ public class EventSerializerTest { //System.out.println(mapper.writeValueAsString(ge4)); assertThat(mapper.writeValueAsString(ge4)) .isEqualTo(jsonRepresentingEE); + + GameEvent ge5 = new GameEvent(); + ge5.type = EventType.TimeoutEvent; + ge5.message = "timeout"; + + + jsonRepresentingEE = """ + { + "eventType":"TimeoutEvent", + "message":"timeout" + }""".replace("\n", ""); + + // System.out.println(mapper.writeValueAsString(ge5)); + assertThat(mapper.writeValueAsString(ge5)) + .isEqualTo(jsonRepresentingEE); } + @Test + void serializeCustomEvent() throws JsonProcessingException { + var mapper = new ObjectMapper(); + + CustomEvent ce = new CustomEvent(); + ce.type = EventType.CustomEvent; + ce.teamIdentifier ="identity"; + ce.customContent = new HashMap(1,1); + + + var jsonRepresentingEE = """ + { + "eventType":"CustomEvent", + "teamIdentifier":"identity", + "customContent":{} + }""".replace("\n", ""); + + // System.out.println(mapper.writeValueAsString(ce)); + assertThat(mapper.writeValueAsString(ce)) + .isEqualTo(jsonRepresentingEE); + } /* ObjectMapper mapper;