Gamelib/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/serialize/EntitySerializerTest.java

269 lines
7.7 KiB
Java

package uulm.teamname.marvelous.gamelibrary.json.ingame.serialize;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.entities.*;
import uulm.teamname.marvelous.gamelibrary.entities.Character;
import java.util.ArrayList;
import static org.assertj.core.api.Assertions.*;
class EntitySerializerTest {
@Test
void serializeCharacter() throws JsonProcessingException {
//testing, 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 chara = new Character(
new EntityID(EntityType.P2, 3),
new IntVector2(12, 24),
"CoolHeroThing",
200,
300,
400,
500,
600,
700);
chara.inventory.addStone(StoneType.SoulStone);
chara.inventory.addStone(StoneType.SpaceStone);
chara.inventory.addStone(StoneType.MindStone);
var jsonRepresentingCharacter = """
{
"entityType":"Character",
"name":"CoolHeroThing",
"PID":2,
"ID":3,
"HP":200,
"MP":300,
"AP":400,
"stones":[0,1,5],
"position":[12,24]
}""".replace("\n", "");
assertThat(mapper.writeValueAsString(chara))
.isEqualTo(jsonRepresentingCharacter);
}
@Test
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();
//________________________________________________
ArrayList<StoneType> al = new ArrayList<>();
al.add(StoneType.MindStone);
al.add(StoneType.PowerStone);
var npc = new NPC(
new EntityID(EntityType.NPC, 3),
new IntVector2(12, 24),
200,
al);
var jsonRepresentingNPC = """
{
"entityType":"NPC",
"ID":3,
"MP":200,
"stones":[1,3],
"position":[12,24]
}""".replace("\n", "");
assertThat(mapper.writeValueAsString(npc))
.isEqualTo(jsonRepresentingNPC);
//________________________________________________
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(
new EntityID(EntityType.NPC, 3),
new IntVector2(12, 24));
//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 = """
{
"entityType":"NPC",
"ID":3,
"MP":0,
"stones":[],
"position":[12,24]
}""".replace("\n", "");
assertThat(mapper.writeValueAsString(npc2))
.isEqualTo(jsonRepresentingNPC);
npc2.inventory.addStone(StoneType.MindStone);
jsonRepresentingNPC = """
{
"entityType":"NPC",
"ID":3,
"MP":0,
"stones":[1],
"position":[12,24]
}""".replace("\n", "");
assertThat(mapper.writeValueAsString(npc2))
.isEqualTo(jsonRepresentingNPC);
//________________________________________________
var npc3 = new NPC(
new EntityID(EntityType.NPC, 3),
new IntVector2(12, 24),
200);
jsonRepresentingNPC = """
{
"entityType":"NPC",
"ID":3,
"MP":200,
"stones":[],
"position":[12,24]
}""".replace("\n", "");
assertThat(mapper.writeValueAsString(npc3))
.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);
}
@Test
void serializePortal() throws JsonProcessingException {
var mapper = new ObjectMapper();
Portal portal = new Portal(
new EntityID(EntityType.Portals, 15),
new IntVector2(25, 35)
);
String jsonRepresentingPortal = """
{
"entityType":"Portal",
"ID":15,
"position":[25,35]
}
""".replace("\n", "");
assertThat(mapper.writeValueAsString(portal))
.isEqualTo(jsonRepresentingPortal);
}
}