test: complete tests for message (de)serialization

This commit is contained in:
Yannik Bretschneider 2021-06-03 22:16:45 +02:00
parent 0770c134a1
commit c67968896b
2 changed files with 704 additions and 1 deletions

View File

@ -4,8 +4,19 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.config.*;
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
import uulm.teamname.marvelous.gamelibrary.entities.EntityType;
import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.events.EventBuilder;
import uulm.teamname.marvelous.gamelibrary.events.EventType;
import uulm.teamname.marvelous.gamelibrary.messages.*;
import uulm.teamname.marvelous.gamelibrary.requests.Request;
import uulm.teamname.marvelous.gamelibrary.requests.RequestBuilder;
import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
import java.util.HashMap;
import static org.assertj.core.api.Assertions.*;
@ -355,7 +366,7 @@ public class MessageDeserializationTest {
void goodbyeClientTest() throws JsonProcessingException {
var jsonRepresentingMessage = """
{
"messageType":"GOODBYE_ClIENT",
"messageType":"GOODBYE_CLIENT",
"message":"und schüss"
}
""";
@ -436,4 +447,119 @@ public class MessageDeserializationTest {
assertThat((ReconnectMessage) mapper.readValue(jsonRepresentingMessage, BasicMessage.class))
.isEqualTo(message);
}
@Test
void eventTest() throws JsonProcessingException {
var jsonRepresentingMessage = """
{
"messageType":"EVENTS",
"messages":[
{
"eventType": "MeleeAttackEvent",
"originEntity": {"entityID": "P1", "ID": 3},
"targetEntity": {"entityID": "P2", "ID": 4},
"originField": [3, 7],
"targetField": [3, 6]
},
{
"eventType": "MoveEvent",
"originEntity": {"entityID": "P1", "ID": 3},
"originField": [3, 7],
"targetField": [3, 6]
}
],
"customContentType":"SomeAwesomeCustomContent",
"customContent":{
"customKey":"someFancyValue",
"customInteger":5,
"customTruth":false
}
}
""";
var meleeAttackEvent = new EventBuilder(EventType.MeleeAttackEvent)
.withOriginEntity(new EntityID(EntityType.P1, 3))
.withTargetEntity(new EntityID(EntityType.P2, 4))
.withOriginField(new IntVector2(3, 7))
.withTargetField(new IntVector2(3, 6))
.buildCharacterEvent();
var moveEvent = new EventBuilder(EventType.MoveEvent)
.withOriginEntity(new EntityID(EntityType.P1, 3))
.withOriginField(new IntVector2(3, 7))
.withTargetField(new IntVector2(3, 6))
.buildCharacterEvent();
var message = new EventMessage();
message.messages = new Event[] {meleeAttackEvent, moveEvent};
message.customContentType = "SomeAwesomeCustomContent";
message.customContent = new HashMap<>();
message.customContent.put("customKey", "someFancyValue");
message.customContent.put("customInteger", 5);
message.customContent.put("customTruth", false);
assertThat((EventMessage) mapper.readValue(jsonRepresentingMessage, BasicMessage.class))
.isEqualTo(message);
}
@Test
void requestTest() throws JsonProcessingException {
var jsonRepresentingMessage = """
{
"messageType":"REQUESTS",
"messages":[
{
"requestType": "MeleeAttackRequest",
"originEntity": {"entityID": "P1", "ID": 3},
"targetEntity": {"entityID": "P2", "ID": 4},
"originField": [3, 7],
"targetField": [3, 6],
"value":14
},
{
"requestType": "MoveRequest",
"originEntity": {"entityID": "P1", "ID": 3},
"originField": [3, 7],
"targetField": [3, 6]
}
],
"customContentType":"SomeAwesomeCustomContent",
"customContent":{
"customKey":"someFancyValue",
"customInteger":5,
"customTruth":false
}
}
""";
var meleeAttackRequest = new RequestBuilder(RequestType.MeleeAttackRequest)
.withOriginEntity(new EntityID(EntityType.P1, 3))
.withTargetEntity(new EntityID(EntityType.P2, 4))
.withOriginField(new IntVector2(3, 7))
.withTargetField(new IntVector2(3, 6))
.withValue(14)
.buildCharacterRequest();
var moveRequest = new RequestBuilder(RequestType.MoveRequest)
.withOriginEntity(new EntityID(EntityType.P1, 3))
.withOriginField(new IntVector2(3, 7))
.withTargetField(new IntVector2(3, 6))
.buildCharacterRequest();
var message = new RequestMessage();
message.messages = new Request[] {meleeAttackRequest, moveRequest};
message.customContentType = "SomeAwesomeCustomContent";
message.customContent = new HashMap<>();
message.customContent.put("customKey", "someFancyValue");
message.customContent.put("customInteger", 5);
message.customContent.put("customTruth", false);
assertThat((RequestMessage) mapper.readValue(jsonRepresentingMessage, BasicMessage.class))
.isEqualTo(message);
}
}

View File

@ -0,0 +1,577 @@
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.BeforeEach;
import org.junit.jupiter.api.Test;
import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.config.CharacterProperties;
import uulm.teamname.marvelous.gamelibrary.config.FieldType;
import uulm.teamname.marvelous.gamelibrary.config.PartyConfig;
import uulm.teamname.marvelous.gamelibrary.config.ScenarioConfig;
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
import uulm.teamname.marvelous.gamelibrary.entities.EntityType;
import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.events.EventBuilder;
import uulm.teamname.marvelous.gamelibrary.events.EventType;
import uulm.teamname.marvelous.gamelibrary.messages.*;
import uulm.teamname.marvelous.gamelibrary.requests.Request;
import uulm.teamname.marvelous.gamelibrary.requests.RequestBuilder;
import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
import java.util.HashMap;
import static org.assertj.core.api.Assertions.assertThat;
public class MessageSerializationTest {
ObjectMapper mapper;
@BeforeEach
void beforeEach() {
mapper = new ObjectMapper();
}
@Test
void CharacterSelectionDeserializationTest() throws JsonProcessingException {
var jsonRepresentingMessage = """
{
"messageType":"CHARACTER_SELECTION",
"characters":[false,true,false,true,false,true,false,true,false,true,false,true]
}
""".replace("\n", "");
var message = new CharacterSelectionMessage();
// message.messageType = MessageType.CHARACTER_SELECTION;
message.characters = new Boolean[] {false,true,false,true,false,true,false,true,false,true,false,true};
assertThat(
(mapper.writeValueAsString(message)))
.isEqualTo(jsonRepresentingMessage);
}
@Test
void confirmSelectionTest() throws JsonProcessingException {
var jsonRepresentingMessage = """
{
"messageType":"CONFIRM_SELECTION",
"selectionComplete":true
}
""".replace("\n", "");
var message = new ConfirmSelectionMessage();
// message.messageType = MessageType.CONFIRM_SELECTION;
message.selectionComplete = true;
assertThat(mapper.writeValueAsString(message))
.isEqualTo(jsonRepresentingMessage);
}
@Test
void errorTest() throws JsonProcessingException {
var jsonRepresentingMessage = """
{
"messageType":"ERROR",
"message":"error error",
"type":0
}
""".replace("\n", "");
var message = new ErrorMessage();
// message.messageType = MessageType.ERROR;
message.message = "error error";
message.type = 0;
assertThat(mapper.writeValueAsString(message))
.isEqualTo(jsonRepresentingMessage);
}
@Test
void gameAssignmentTest() throws JsonProcessingException {
var jsonRepresentingMessage = """
{
"messageType":"GAME_ASSIGNMENT",
"gameID":"6a39c3cf-26d8-409e-a309-45590f38ec4f",
"characterSelection":[
{
"characterID":1,
"name":"Rocket Raccoon",
"HP":100,
"MP":2,
"AP":2,
"meleeDamage":10,
"rangeCombatDamage":30,
"rangeCombatReach":5
},
{
"characterID":2,
"name":"Quicksilver",
"HP":100,
"MP":6,
"AP":1,
"meleeDamage":10,
"rangeCombatDamage":10,
"rangeCombatReach":3
},
{
"characterID":3,
"name":"Hulk",
"HP":100,
"MP":2,
"AP":2,
"meleeDamage":10,
"rangeCombatDamage":30,
"rangeCombatReach":5
}
]
}
""".replace("\n", "");
var racoon = new CharacterProperties();
racoon.characterID = 1;
racoon.name = "Rocket Raccoon";
racoon.HP = 100;
racoon.MP = 2;
racoon.AP = 2;
racoon.meleeDamage = 10;
racoon.rangedDamage = 30;
racoon.attackRange = 5;
var quicksilver = new CharacterProperties();
quicksilver.characterID = 2;
quicksilver.name = "Quicksilver";
quicksilver.HP = 100;
quicksilver.MP = 6;
quicksilver.AP = 1;
quicksilver.meleeDamage = 10;
quicksilver.rangedDamage = 10;
quicksilver.attackRange = 3;
var hulk = new CharacterProperties();
hulk.characterID = 3;
hulk.name = "Hulk";
hulk.HP = 100;
hulk.MP = 2;
hulk.AP = 2;
hulk.meleeDamage = 10;
hulk.rangedDamage = 30;
hulk.attackRange = 5;
var message = new GameAssignmentMessage();
// message.messageType = MessageType.GAME_ASSIGNMENT;
message.gameID = "6a39c3cf-26d8-409e-a309-45590f38ec4f";
message.characterSelection = new CharacterProperties[] {racoon, quicksilver, hulk};
assertThat(mapper.writeValueAsString(message))
.isEqualTo(jsonRepresentingMessage);
}
@Test
void testGameStructure() throws JsonProcessingException {
var jsonRepresentingMessage = """
{
"messageType":"GAME_STRUCTURE",
"assignment":"PlayerOne",
"playerOneName":"Gandalf",
"playerTwoName":"Bilbo",
"playerOneCharacters":[
{
"characterID":1,
"name":"Rocket Raccoon",
"HP":100,
"MP":2,
"AP":2,
"meleeDamage":10,
"rangeCombatDamage":30,
"rangeCombatReach":5
},
{
"characterID":2,
"name":"Quicksilver",
"HP":100,
"MP":6,
"AP":1,
"meleeDamage":10,
"rangeCombatDamage":10,
"rangeCombatReach":3
},
{
"characterID":3,
"name":"Hulk",
"HP":100,
"MP":2,
"AP":2,
"meleeDamage":10,
"rangeCombatDamage":30,
"rangeCombatReach":5
}
],
"playerTwoCharacters":[
{
"characterID":19,
"name":"Loki",
"HP":100,
"MP":6,
"AP":1,
"meleeDamage":10,
"rangeCombatDamage":10,
"rangeCombatReach":3
},
{
"characterID":20,
"name":"Silver Surfer",
"HP":100,
"MP":6,
"AP":1,
"meleeDamage":10,
"rangeCombatDamage":10,
"rangeCombatReach":3
}
],
"scenarioconfig":{
"scenario":[
["GRASS","GRASS","GRASS","GRASS","GRASS","GRASS","GRASS"],
["GRASS","GRASS","GRASS","ROCK","GRASS","GRASS","GRASS"],
["GRASS","GRASS","GRASS","ROCK","GRASS","GRASS","GRASS"],
["GRASS","GRASS","GRASS","ROCK","GRASS","GRASS","GRASS"],
["GRASS","GRASS","ROCK","ROCK","ROCK","GRASS","GRASS"],
["GRASS","ROCK","ROCK","ROCK","ROCK","ROCK","GRASS"]
],
"author":"jakobmh",
"name":"asgard"
},
"matchconfig":{
"maxRounds":30,
"maxRoundTime":300,
"maxGameTime":1800,
"maxAnimationTime":50,
"spaceStoneCD":0,
"mindStoneCD":0,
"realityStoneCD":0,
"powerStoneCD":0,
"timeStoneCD":0,
"soulStoneCD":0,
"mindStoneDMG":0,
"maxPauseTime":0,
"maxResponseTime":0
}
}
""".replace("\n", "");
var racoon = new CharacterProperties();
racoon.characterID = 1;
racoon.name = "Rocket Raccoon";
racoon.HP = 100;
racoon.MP = 2;
racoon.AP = 2;
racoon.meleeDamage = 10;
racoon.rangedDamage = 30;
racoon.attackRange = 5;
var quicksilver = new CharacterProperties();
quicksilver.characterID = 2;
quicksilver.name = "Quicksilver";
quicksilver.HP = 100;
quicksilver.MP = 6;
quicksilver.AP = 1;
quicksilver.meleeDamage = 10;
quicksilver.rangedDamage = 10;
quicksilver.attackRange = 3;
var hulk = new CharacterProperties();
hulk.characterID = 3;
hulk.name = "Hulk";
hulk.HP = 100;
hulk.MP = 2;
hulk.AP = 2;
hulk.meleeDamage = 10;
hulk.rangedDamage = 30;
hulk.attackRange = 5;
var loki = new CharacterProperties();
loki.characterID = 19;
loki.name = "Loki";
loki.HP = 100;
loki.MP = 6;
loki.AP = 1;
loki.meleeDamage = 10;
loki.rangedDamage = 10;
loki.attackRange = 3;
var silversurfer = new CharacterProperties();
silversurfer.characterID = 20;
silversurfer.name = "Silver Surfer";
silversurfer.HP = 100;
silversurfer.MP = 6;
silversurfer.AP = 1;
silversurfer.meleeDamage = 10;
silversurfer.rangedDamage = 10;
silversurfer.attackRange = 3;
var scenarioConfig = new ScenarioConfig();
scenarioConfig.scenario = new FieldType[][] {
{FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS} ,
{FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS} ,
{FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS} ,
{FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS} ,
{FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.ROCK, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS} ,
{FieldType.GRASS, FieldType.ROCK, FieldType.ROCK, FieldType.ROCK, FieldType.ROCK, FieldType.ROCK, FieldType.GRASS}
};
scenarioConfig.author = "jakobmh";
scenarioConfig.name = "asgard";
var matchconfig = new PartyConfig();
matchconfig.maxRounds = 30;
matchconfig.maxRoundTime = 300;
matchconfig.maxGameTime = 1800;
matchconfig.maxAnimationTime = 50;
var message = new GameStructureMessage();
// message.messageType = MessageType.GAME_STRUCTURE;
message.assignment = Assignment.PlayerOne;
message.playerOneName = "Gandalf";
message.playerTwoName = "Bilbo";
message.playerOneCharacters = new CharacterProperties[] {racoon, quicksilver, hulk};
message.playerTwoCharacters = new CharacterProperties[] {loki, silversurfer};
message.scenarioconfig = scenarioConfig;
message.matchconfig = matchconfig;
assertThat(mapper.writeValueAsString(message))
.isEqualTo(jsonRepresentingMessage);
}
@Test
void generalAssignmentTest() throws JsonProcessingException {
var jsonRepresentingMessage = """
{
"messageType":"GENERAL_ASSIGNMENT",
"gameID":"6a39c3cf-26d8-409e-a309-45590f38ec4f"
}
""".replace("\n", "");
var message = new GeneralAssignmentMessage();
// message.messageType = MessageType.GENERAL_ASSIGNMENT;
message.gameID = "6a39c3cf-26d8-409e-a309-45590f38ec4f";
assertThat(mapper.writeValueAsString(message))
.isEqualTo(jsonRepresentingMessage);
}
@Test
void goodbyeClientTest() throws JsonProcessingException {
var jsonRepresentingMessage = """
{
"messageType":"GOODBYE_CLIENT",
"message":"und schüss"
}
""".replace("\n", "");
var message = new GoodbyeClientMessage();
message.message = "und schüss";
assertThat(mapper.writeValueAsString(message))
.isEqualTo(jsonRepresentingMessage);
}
@Test
void helloClientTest() throws JsonProcessingException {
var jsonRepresentingMessage = """
{
"messageType":"HELLO_CLIENT",
"runningGame":false
}
""".replace("\n", "");
var message = new HelloClientMessage();
message.runningGame = false;
assertThat(mapper.writeValueAsString(message))
.isEqualTo(jsonRepresentingMessage);
}
@Test
void helloServerTest() throws JsonProcessingException {
var jsonRepresentingMessage = """
{
"messageType":"HELLO_SERVER",
"name":"mynamehere",
"deviceID":"8f966f5a-9b62-456c-b4a3-50c90c75789c"
}
""".replace("\n", "");
var message = new HelloServerMessage();
message.name = "mynamehere";
message.deviceID = "8f966f5a-9b62-456c-b4a3-50c90c75789c";
assertThat(mapper.writeValueAsString(message))
.isEqualTo(jsonRepresentingMessage);
}
@Test
void playerReadyTest() throws JsonProcessingException {
var jsonRepresentingMessage = """
{
"messageType":"PLAYER_READY",
"startGame":false,
"role":"PLAYER"
}
""".replace("\n", "");
var message = new PlayerReadyMessage();
message.startGame = false;
message.role = RoleEnum.PLAYER;
assertThat(mapper.writeValueAsString(message))
.isEqualTo(jsonRepresentingMessage);
}
@Test
void reconnectTest() throws JsonProcessingException {
var jsonRepresentingMessage = """
{
"messageType":"RECONNECT",
"reconnect":false
}
""".replace("\n", "");
var message = new ReconnectMessage();
message.reconnect = false;
assertThat(mapper.writeValueAsString(message))
.isEqualTo(jsonRepresentingMessage);
}
@Test
void eventTest() throws JsonProcessingException {
var jsonRepresentingMessage = """
{
"messageType":"EVENTS",
"messages":[
{
"eventType":"MeleeAttackEvent",
"originEntity":{"entityID":"P1","ID":3},
"targetEntity":{"entityID":"P2","ID":4},
"originField":[3,7],
"targetField":[3,6]
},
{
"eventType":"MoveEvent",
"originEntity":{"entityID":"P1","ID":3},
"originField":[3,7],
"targetField":[3,6]
}
],
"customContentType":"SomeAwesomeCustomContent",
"customContent":{
"customKey":"someFancyValue",
"customInteger":5,
"customTruth":false
}
}
""".replace("\n", "");
var meleeAttackEvent = new EventBuilder(EventType.MeleeAttackEvent)
.withOriginEntity(new EntityID(EntityType.P1, 3))
.withTargetEntity(new EntityID(EntityType.P2, 4))
.withOriginField(new IntVector2(3, 7))
.withTargetField(new IntVector2(3, 6))
.buildCharacterEvent();
var moveEvent = new EventBuilder(EventType.MoveEvent)
.withOriginEntity(new EntityID(EntityType.P1, 3))
.withOriginField(new IntVector2(3, 7))
.withTargetField(new IntVector2(3, 6))
.buildCharacterEvent();
var message = new EventMessage();
message.messages = new Event[] {meleeAttackEvent, moveEvent};
message.customContentType = "SomeAwesomeCustomContent";
message.customContent = new HashMap<>();
message.customContent.put("customKey", "someFancyValue");
message.customContent.put("customInteger", 5);
message.customContent.put("customTruth", false);
assertThat(mapper.writeValueAsString(message))
.isEqualTo(jsonRepresentingMessage);
}
@Test
void requestTest() throws JsonProcessingException {
var jsonRepresentingMessage = """
{
"messageType":"REQUESTS",
"messages":[
{
"requestType":"MeleeAttackRequest",
"originEntity":{"entityID":"P1","ID":3},
"targetEntity":{"entityID":"P2","ID":4},
"originField":[3,7],
"targetField":[3,6],
"value":14
},
{
"requestType":"MoveRequest",
"originEntity":{"entityID":"P1","ID":3},
"originField":[3,7],
"targetField":[3,6]
}
],
"customContentType":"SomeAwesomeCustomContent",
"customContent":{
"customKey":"someFancyValue",
"customInteger":5,
"customTruth":false
}
}
""".replace("\n", "");
var meleeAttackRequest = new RequestBuilder(RequestType.MeleeAttackRequest)
.withOriginEntity(new EntityID(EntityType.P1, 3))
.withTargetEntity(new EntityID(EntityType.P2, 4))
.withOriginField(new IntVector2(3, 7))
.withTargetField(new IntVector2(3, 6))
.withValue(14)
.buildCharacterRequest();
var moveRequest = new RequestBuilder(RequestType.MoveRequest)
.withOriginEntity(new EntityID(EntityType.P1, 3))
.withOriginField(new IntVector2(3, 7))
.withTargetField(new IntVector2(3, 6))
.buildCharacterRequest();
var message = new RequestMessage();
message.messages = new Request[] {meleeAttackRequest, moveRequest};
message.customContentType = "SomeAwesomeCustomContent";
message.customContent = new HashMap<>();
message.customContent.put("customKey", "someFancyValue");
message.customContent.put("customInteger", 5);
message.customContent.put("customTruth", false);
assertThat(mapper.writeValueAsString(message))
.isEqualTo(jsonRepresentingMessage);
}
}