diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java index 6fc0fd3..22b5a1a 100644 --- a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java @@ -8,127 +8,1298 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; -import uulm.teamname.marvelous.gamelibrary.entities.EntityID; +import uulm.teamname.marvelous.gamelibrary.IntVector2; +import uulm.teamname.marvelous.gamelibrary.config.*; +import uulm.teamname.marvelous.gamelibrary.entities.*; +import uulm.teamname.marvelous.gamelibrary.entities.Character; 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.server.EventMessage; +import uulm.teamname.marvelous.gamelibrary.events.GamestateEvent; +import uulm.teamname.marvelous.gamelibrary.messages.AssignmentEnum; +import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage; +import uulm.teamname.marvelous.gamelibrary.messages.ErrorMessage; +import uulm.teamname.marvelous.gamelibrary.messages.RoleEnum; +import uulm.teamname.marvelous.gamelibrary.messages.client.*; +import uulm.teamname.marvelous.gamelibrary.messages.server.*; +import uulm.teamname.marvelous.gamelibrary.requests.Request; +import uulm.teamname.marvelous.gamelibrary.requests.RequestBuilder; +import uulm.teamname.marvelous.gamelibrary.requests.RequestType; import java.util.*; class JSONTest { - String messageStructureStart; - /** 0 is REQUESTS, 1 is EVENTS */ - String[] messageStructureEnd; - - /** Still need to add: messages, messageType */ - EventMessage target; JSON json; + CharacterConfig characterConfig; + HashMap propertiesMap; + // TODO: add proper CharacterConfig, otherwise Character events won't be deserializable @BeforeEach void setUp() { - messageStructureStart = """ + characterConfig = mock(CharacterConfig.class); + propertiesMap = new HashMap<>(); + + when(characterConfig.getMap()).thenReturn(propertiesMap); + + json = new JSON(characterConfig); + + } + + // What follows is code mostly copied from the Message(De)SerializationTests. This is technically not DRY, + // but I claim that under AHA this is fine. + + + // ============================================================================= + // >>>>>>> Deserialization tests <<<<<<< + // ============================================================================= + + @Test + void characterSelectionDeserializationParseTest() throws JsonProcessingException { + var jsonRepresentingMessage = """ { - "messages":[ - """; - messageStructureEnd = new String[]{ - """ - ], - "messageType": "REQUESTS", - "customContentType": "TestCustomContent", - "customContent": { - "customKey" = "customResult", - "customNumber" = 15, - "customProperty" = true - } - """, - """ - ], - "messageType": "EVENTS", - "customContentType": "TestCustomContent", - "customContent": { - "customKey": "customResult", - "customNumber": 15, - "customProperty": true + "messageType":"CHARACTER_SELECTION", + "characters":[false,true,false,true,false,true,false,true,false,true,false,true] } - } - """, - }; - target = new EventMessage(); - target.customContentType = "TestCustomContent"; - target.customContent = new HashMap<>(); - target.customContent.put("customKey", "customResult"); - target.customContent.put("customNumber", 15); - target.customContent.put("customProperty", true); + """.replace("\n", ""); - this.json = new JSON(null); - // FIXME: Temporary hotfix, nothing valid yet - } + 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}; - @AfterEach - void tearDown() { + assertThat((CharacterSelectionMessage) json.parse(jsonRepresentingMessage).get()) + .isEqualTo(message); } @Test - void parseNotificationEvents() { - Event[] targetEvents = { - new EventBuilder(EventType.Ack).buildGameStateEvent(), - new EventBuilder(EventType.Nack).buildGameStateEvent(), - }; - - String eventRepresentation = """ - {"eventType": "Ack"}, - {"eventType": "Nack"} - """; - String completeMessageStructure = messageStructureStart + eventRepresentation + messageStructureEnd[1]; - - // target.messageType = MessageType.EVENTS; - target.messages = targetEvents; - - assertThat(json.parse(completeMessageStructure)).isEqualTo(target); - - } - - @Test - @Disabled - void parseMoreComplicatedEvents() { - String eventRepresentation = """ + void confirmSelectionParseParseTest() throws JsonProcessingException { + var jsonRepresentingMessage = """ { - "eventType": "GamestateEvent", - "activeCharacter": {"entityID": "P2", "ID": 4}, - "turnOrder": [ - {"entityID": "P2", "ID": 4}, - {"entityID": "P2", "ID": 2}, - {"entityID": "P1", "ID": 3}, - {"entityID": "NPC", "ID": 1}, - {"entityID": "InfinityStones", "ID": 5}, - {"entityID": "Rocks", "ID": 0} + "messageType":"CONFIRM_SELECTION", + "selectionComplete":true + } + """; + + var message = new ConfirmSelectionMessage(); +// message.messageType = MessageType.CONFIRM_SELECTION; + message.selectionComplete = true; + + assertThat((ConfirmSelectionMessage) json.parse(jsonRepresentingMessage).get()) + .isEqualTo(message); + } + + @Test + void errorParseTest() throws JsonProcessingException { + var jsonRepresentingMessage = """ + { + "messageType":"ERROR", + "message": "error error", + "type":0 + } + """; + var message = new ErrorMessage(); +// message.messageType = MessageType.ERROR; + message.message = "error error"; + message.type = 0; + + assertThat((ErrorMessage) json.parse(jsonRepresentingMessage).get()) + .isEqualTo(message); + } + + @Test + void gameAssignmentParseTest() 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 + } ] } """; - String completeMessageStructure = messageStructureStart + eventRepresentation + messageStructureEnd[1]; - System.out.println(json.parse(completeMessageStructure)); + 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((GameAssignmentMessage) json.parse(jsonRepresentingMessage).get()) + .isEqualTo(message); } @Test - @Disabled - void simpleInDevTest() throws JsonProcessingException { - String simpleEntity = """ + void gameStructureParseTest() throws JsonProcessingException { + var jsonRepresentingMessage = """ { - "entityID": "P2", - "ID": 4 + "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 + } } """; - System.out.println(new ObjectMapper().readValue(simpleEntity, EntityID.class)); + + 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 = AssignmentEnum.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((GameStructureMessage) json.parse(jsonRepresentingMessage).get()) + .isEqualTo(message); + + } + + + @Test + void generalAssignmentParseTest() throws JsonProcessingException { + var jsonRepresentingMessage = """ + { + "messageType":"GENERAL_ASSIGNMENT", + "gameID":"6a39c3cf-26d8-409e-a309-45590f38ec4f" + } + """; + + var message = new GeneralAssignmentMessage(); +// message.messageType = MessageType.GENERAL_ASSIGNMENT; + message.gameID = "6a39c3cf-26d8-409e-a309-45590f38ec4f"; + + assertThat((GeneralAssignmentMessage) json.parse(jsonRepresentingMessage).get()) + .isEqualTo(message); } @Test - void stringify() { + void goodbyeClientParseTest() throws JsonProcessingException { + var jsonRepresentingMessage = """ + { + "messageType":"GOODBYE_CLIENT", + "message":"und schüss" + } + """; + + var message = new GoodbyeClientMessage(); + message.message = "und schüss"; + + assertThat((GoodbyeClientMessage) json.parse(jsonRepresentingMessage).get()) + .isEqualTo(message); + } + + @Test + void helloClientParseTest() throws JsonProcessingException { + var jsonRepresentingMessage = """ + { + "messageType":"HELLO_CLIENT", + "runningGame":false + } + """; + + var message = new HelloClientMessage(); + message.runningGame = false; + + + assertThat((HelloClientMessage) json.parse(jsonRepresentingMessage).get()) + .isEqualTo(message); + } + + + @Test + void helloServerParseTest() throws JsonProcessingException { + var jsonRepresentingMessage = """ + { + "messageType": "HELLO_SERVER", + "name":"mynamehere", + "deviceID":"8f966f5a-9b62-456c-b4a3-50c90c75789c" + } + """; + + var message = new HelloServerMessage(); + message.name = "mynamehere"; + message.deviceID = "8f966f5a-9b62-456c-b4a3-50c90c75789c"; + + assertThat((HelloServerMessage) json.parse(jsonRepresentingMessage).get()) + .isEqualTo(message); + } + + @Test + void playerReadyParseTest() throws JsonProcessingException { + var jsonRepresentingMessage = """ + { + "messageType":"PLAYER_READY", + "startGame":false, + "role":"PLAYER" + } + """; + + var message = new PlayerReadyMessage(); + message.startGame = false; + message.role = RoleEnum.PLAYER; + + assertThat((PlayerReadyMessage) json.parse(jsonRepresentingMessage).get()) + .isEqualTo(message); + } + + @Test + void reconnectParseTest() throws JsonProcessingException { + var jsonRepresentingMessage = """ + { + "messageType":"RECONNECT", + "reconnect":false + } + """; + + var message = new ReconnectMessage(); + message.reconnect = false; + + assertThat((ReconnectMessage) json.parse(jsonRepresentingMessage).get()) + .isEqualTo(message); + } + + @Test + void eventParseTest() 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) json.parse(jsonRepresentingMessage).get()) + .isEqualTo(message); + } + + @Test + void eventWithCharactersThatNeedConfigParseTest() throws JsonProcessingException { + var alphaConfig = new CharacterProperties(); + alphaConfig.attackRange = 45; + alphaConfig.rangedDamage = 65; + alphaConfig.meleeDamage = 12; + propertiesMap.put("Alpha", alphaConfig); + + var characterAlpha = new Character( + new EntityID(EntityType.P1, 4), + new IntVector2(5, 3), + "Alpha", + 210, + 9, + 3, + 45, // still -1 because no gameConfig object available + 65, + 12); + + characterAlpha.inventory.addStone(StoneType.valueOf(1)); + characterAlpha.inventory.addStone(StoneType.valueOf(4)); + + characterAlpha.setPosition(new IntVector2(4, 2)); + + var steelWallConfig = new CharacterProperties(); + steelWallConfig.attackRange = 23; + steelWallConfig.rangedDamage = 34; + steelWallConfig.meleeDamage = 45; + propertiesMap.put("Steel Wall", steelWallConfig); + + var characterSteelWall = new Character( + new EntityID(EntityType.P1, 4), + new IntVector2(255, 0), + "Steel Wall", + 123, + 2, + 21, + 23, + 34, + 45); + + characterSteelWall.setPosition(new IntVector2(6, 7)); + + + var infinityStone = new InfinityStone( + new EntityID(EntityType.InfinityStones, 3), + new IntVector2(52, 13), + StoneType.valueOf(3)); + + infinityStone.setPosition(new IntVector2(7, 22)); + + + var rock = new Rock( + new EntityID(EntityType.Rocks, 15), + new IntVector2(51, 31), + 195); + + rock.setPosition(new IntVector2(4, 285)); + + var gamestate = new GamestateEvent(); + gamestate.type = EventType.GamestateEvent; + gamestate.entities = new Entity[] { + characterAlpha, + characterSteelWall, + infinityStone, + rock + }; + gamestate.mapSize = new IntVector2(15, 300); + gamestate.activeCharacter = new EntityID(EntityType.P2, 3); + gamestate.stoneCooldowns = new Integer[] {1, 1, 3, 1, 4, 2}; + gamestate.turnOrder = new EntityID[] { + new EntityID(EntityType.P1, 5), + new EntityID(EntityType.P1, 4), + new EntityID(EntityType.P1, 3), + new EntityID(EntityType.P1, 2), + new EntityID(EntityType.P1, 1), + new EntityID(EntityType.P1, 0), + new EntityID(EntityType.P2, 0), + new EntityID(EntityType.P2, 1), + new EntityID(EntityType.P2, 2), + new EntityID(EntityType.P2, 3), + new EntityID(EntityType.P2, 4), + new EntityID(EntityType.P2, 5), + }; + gamestate.winCondition = true; + + + + var jsonRepresentingMessage = """ + { + "messageType":"EVENTS", + "messages":[ + { + "eventType":"Ack" + }, + { + "eventType": "GamestateEvent", + "entities": [ + { + "entityType": "Character", + "name": "Alpha", + "PID": 1, + "ID": 4, + "HP": 210, + "MP": 9, + "AP": 3, + "stones": [1, 4], + "position": [4, 2] + }, + { + "entityType": "Character", + "name": "Steel Wall", + "PID": 1, + "ID": 4, + "HP": 123, + "MP": 2, + "AP": 21, + "stones": [], + "position": [6, 7] + }, + { + "entityType": "InfinityStone", + "ID": 3, + "position": [7, 22] + }, + { + "entityType": "Rock", + "ID": 15, + "HP": 195, + "position": [4, 285] + } + ], + "turnOrder": [ + {"entityID": "P1", "ID": 5}, + {"entityID": "P1", "ID": 4}, + {"entityID": "P1", "ID": 3}, + {"entityID": "P1", "ID": 2}, + {"entityID": "P1", "ID": 1}, + {"entityID": "P1", "ID": 0}, + {"entityID": "P2", "ID": 0}, + {"entityID": "P2", "ID": 1}, + {"entityID": "P2", "ID": 2}, + {"entityID": "P2", "ID": 3}, + {"entityID": "P2", "ID": 4}, + {"entityID": "P2", "ID": 5} + ], + "mapSize": [15, 300], + "activeCharacter": {"entityID": "P2", "ID": 3}, + "stoneCooldowns": [1, 1, 3, 1, 4, 2], + "winCondition": true + } + ] + } + """; + + var message = new EventMessage(); + + message.messages = new Event[] {new EventBuilder(EventType.Ack).buildGameStateEvent(), gamestate}; + + assertThat(json.parse(jsonRepresentingMessage).get()) + .isEqualTo(message); + } + + + @Test + void requestParseTest() 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) json.parse(jsonRepresentingMessage).get()) + .isEqualTo(message); + } + + + // ============================================================================= + // >>>>>>> Serialization tests <<<<<<< + // ============================================================================= + + @Test + void characterSelectionStringifyTest() 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(json.stringify(message).get()) + .isEqualTo(jsonRepresentingMessage); + } + + @Test + void confirmSelectionStringifyTest() throws JsonProcessingException { + var jsonRepresentingMessage = """ + { + "messageType":"CONFIRM_SELECTION", + "selectionComplete":true + } + """.replace("\n", ""); + + var message = new ConfirmSelectionMessage(); +// message.messageType = MessageType.CONFIRM_SELECTION; + message.selectionComplete = true; + + assertThat(json.stringify(message).get()) + .isEqualTo(jsonRepresentingMessage); + } + + @Test + void errorStringifyTest() 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(json.stringify(message).get()) + .isEqualTo(jsonRepresentingMessage); + } + + @Test + void gameAssignmentStringifyTest() 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(json.stringify(message).get()) + .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 = AssignmentEnum.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(json.stringify(message).get()) + .isEqualTo(jsonRepresentingMessage); + + } + + + @Test + void generalAssignmentStringifyTest() 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(json.stringify(message).get()) + .isEqualTo(jsonRepresentingMessage); + } + + @Test + void goodbyeClientStringifyTest() throws JsonProcessingException { + var jsonRepresentingMessage = """ + { + "messageType":"GOODBYE_CLIENT", + "message":"und schüss" + } + """.replace("\n", ""); + + var message = new GoodbyeClientMessage(); + message.message = "und schüss"; + + assertThat(json.stringify(message).get()) + .isEqualTo(jsonRepresentingMessage); + } + + @Test + void helloClientStringifyTest() throws JsonProcessingException { + var jsonRepresentingMessage = """ + { + "messageType":"HELLO_CLIENT", + "runningGame":false + } + """.replace("\n", ""); + + var message = new HelloClientMessage(); + message.runningGame = false; + + + assertThat(json.stringify(message).get()) + .isEqualTo(jsonRepresentingMessage); + } + + + @Test + void helloServerStringifyTest() 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(json.stringify(message).get()) + .isEqualTo(jsonRepresentingMessage); + } + + @Test + void playerReadyStringifyTest() 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(json.stringify(message).get()) + .isEqualTo(jsonRepresentingMessage); + } + + @Test + void reconnectStringifyTest() throws JsonProcessingException { + var jsonRepresentingMessage = """ + { + "messageType":"RECONNECT", + "reconnect":false + } + """.replace("\n", ""); + + var message = new ReconnectMessage(); + message.reconnect = false; + + assertThat(json.stringify(message).get()) + .isEqualTo(jsonRepresentingMessage); + } + + @Test + void eventStringifyTest() 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(json.stringify(message).get()) + .isEqualTo(jsonRepresentingMessage); + } + + + @Test + void requestStringifyTest() 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(json.stringify(message).get()) + .isEqualTo(jsonRepresentingMessage); } }