Update EntitySerializerTest.java

This commit is contained in:
C H 2021-06-03 09:47:34 +02:00
parent d99a2ce3c3
commit d70cd36a39

View File

@ -6,6 +6,7 @@ import org.junit.jupiter.api.Test;
import uulm.teamname.marvelous.gamelibrary.IntVector2; import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.entities.*; 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.entities.Rock;
import uulm.teamname.marvelous.gamelibrary.json.ingame.deserialize.EntityDeserializer; import uulm.teamname.marvelous.gamelibrary.json.ingame.deserialize.EntityDeserializer;
import java.util.ArrayList; import java.util.ArrayList;
@ -19,6 +20,10 @@ class EntitySerializerTest {
@Test @Test
void serializeCharacter() throws JsonProcessingException { void serializeCharacter() throws JsonProcessingException {
//test if all required values are set in json
// and that this is done in the right order as defined in the methods serialize() and serializeCharacter().
var mapper = new ObjectMapper(); var mapper = new ObjectMapper();
var chara = new Character( var chara = new Character(
@ -55,12 +60,19 @@ class EntitySerializerTest {
@Test @Test
void serializeNPC() throws JsonProcessingException { void serializeNPC() throws JsonProcessingException {
//test if all required values are set in json
// and that this is done in the right order as defined in the methods serialize() and serializeNPC().
var mapper = new ObjectMapper(); var mapper = new ObjectMapper();
//________________________________________________
ArrayList<StoneType> al = new ArrayList<>(); ArrayList<StoneType> al = new ArrayList<>();
al.add(StoneType.MindStone); al.add(StoneType.MindStone);
al.add(StoneType.PowerStone); al.add(StoneType.PowerStone);
var npc = new NPC( var npc = new NPC(
new EntityID(EntityType.NPC, 3), new EntityID(EntityType.NPC, 3),
new IntVector2(12, 24), new IntVector2(12, 24),
@ -80,13 +92,51 @@ class EntitySerializerTest {
assertThat(mapper.writeValueAsString(npc)) assertThat(mapper.writeValueAsString(npc))
.isEqualTo(jsonRepresentingNPC); .isEqualTo(jsonRepresentingNPC);
// System.out.println(mapper.writeValueAsString(npc)); //________________________________________________
// al.removeStone(StoneType.MindStone);
//Inventory inv = new Inventory(al);
var npc0= new NPC(
new EntityID(EntityType.NPC, 3),
new IntVector2(12, 24));
npc0.inventory.addStone(StoneType.SoulStone);
npc0.inventory.addStone(StoneType.TimeStone);
jsonRepresentingNPC = """
{
"entityType":"NPC",
"ID":3,
"MP":0,
"stones":[4,5],
"position":[12,24]
}""".replace("\n", "");
// System.out.println(mapper.writeValueAsString(npc0));
assertThat(mapper.writeValueAsString(npc0))
.isEqualTo(jsonRepresentingNPC);
npc0.inventory.removeStone(StoneType.TimeStone);
jsonRepresentingNPC = """
{
"entityType":"NPC",
"ID":3,
"MP":0,
"stones":[5],
"position":[12,24]
}""".replace("\n", "");
//System.out.println(mapper.writeValueAsString(npc0));
assertThat(mapper.writeValueAsString(npc0))
.isEqualTo(jsonRepresentingNPC);
//________________________________________________
var npc2 = new NPC( var npc2 = new NPC(
new EntityID(EntityType.NPC, 3), new EntityID(EntityType.NPC, 3),
new IntVector2(12, 24)); new IntVector2(12, 24));
//test if all values are set in json and that this is done in the right order as defined in the method serializeNPC(). //test if all required values are set in json and that this is done in the right order as defined in the methods serialize() and serializeNPC().
jsonRepresentingNPC = """ jsonRepresentingNPC = """
{ {
"entityType":"NPC", "entityType":"NPC",
@ -96,7 +146,7 @@ class EntitySerializerTest {
"position":[12,24] "position":[12,24]
}""".replace("\n", ""); }""".replace("\n", "");
// System.out.println(mapper.writeValueAsString(npc2));
assertThat(mapper.writeValueAsString(npc2)) assertThat(mapper.writeValueAsString(npc2))
.isEqualTo(jsonRepresentingNPC); .isEqualTo(jsonRepresentingNPC);
@ -111,10 +161,12 @@ class EntitySerializerTest {
"position":[12,24] "position":[12,24]
}""".replace("\n", ""); }""".replace("\n", "");
// System.out.println(mapper.writeValueAsString(npc2));
assertThat(mapper.writeValueAsString(npc2)) assertThat(mapper.writeValueAsString(npc2))
.isEqualTo(jsonRepresentingNPC); .isEqualTo(jsonRepresentingNPC);
//________________________________________________
var npc3 = new NPC( var npc3 = new NPC(
new EntityID(EntityType.NPC, 3), new EntityID(EntityType.NPC, 3),
new IntVector2(12, 24), new IntVector2(12, 24),
@ -129,10 +181,71 @@ class EntitySerializerTest {
"position":[12,24] "position":[12,24]
}""".replace("\n", ""); }""".replace("\n", "");
// System.out.println(mapper.writeValueAsString(npc3));
assertThat(mapper.writeValueAsString(npc3)) assertThat(mapper.writeValueAsString(npc3))
.isEqualTo(jsonRepresentingNPC); .isEqualTo(jsonRepresentingNPC);
} }
@Test
void serializeRock() throws JsonProcessingException {
var mapper = new ObjectMapper();
Rock rock1 = new Rock(
new EntityID(EntityType.Rocks, 3),
new IntVector2(12, 24),
200);
var jsonRepresentingNPC = """
{
"entityType":"Rock",
"ID":3,
"HP":200,
"position":[12,24]
}""".replace("\n", "");
// System.out.println(mapper.writeValueAsString(rock1));
assertThat(mapper.writeValueAsString(rock1))
.isEqualTo(jsonRepresentingNPC);
rock1.decreaseHp(50);
jsonRepresentingNPC = """
{
"entityType":"Rock",
"ID":3,
"HP":150,
"position":[12,24]
}""".replace("\n", "");
//System.out.println(mapper.writeValueAsString(rock1));
assertThat(mapper.writeValueAsString(rock1))
.isEqualTo(jsonRepresentingNPC);
}
@Test
void serializeInfinityStone() throws JsonProcessingException {
var mapper = new ObjectMapper();
InfinityStone inf1 = new InfinityStone(
new EntityID(EntityType.InfinityStones, 3),
new IntVector2(12, 24),
StoneType.PowerStone);
var jsonRepresentingNPC = """
{
"entityType":"InfinityStone",
"ID":3,
"position":[12,24]
}""".replace("\n", "");
System.out.println(mapper.writeValueAsString(inf1));
assertThat(mapper.writeValueAsString(inf1))
.isEqualTo(jsonRepresentingNPC);
}
} }