From 156677f290b652117b638fad27964d3051bd8fe0 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Thu, 13 May 2021 16:11:13 +0200 Subject: [PATCH] test: wrote partial EventDeserializer test --- .../json/ingame/EventDeserializerTest.java | 190 ++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EventDeserializerTest.java diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EventDeserializerTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EventDeserializerTest.java new file mode 100644 index 0000000..b48d71a --- /dev/null +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EventDeserializerTest.java @@ -0,0 +1,190 @@ +package uulm.teamname.marvelous.gamelibrary.json.ingame; + +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 static org.assertj.core.api.Assertions.*; + +import uulm.teamname.marvelous.gamelibrary.IntVector2; +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.EventType; +import uulm.teamname.marvelous.gamelibrary.events.GamestateEvent; +import uulm.teamname.marvelous.gamelibrary.events.NotificationEvent; + +import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.*; + +class EventDeserializerTest { + + ObjectMapper mapper; + + @BeforeEach + void beforeEach() { + mapper = new ObjectMapper(); + } + + @Test + void NotificationEventTest() throws JsonProcessingException { + var ack = new GamestateEvent(); + ack.type = EventType.Ack; + + var jsonRepresentingAck = "{\"eventType\": \"Ack\"}"; + + assertThat((GamestateEvent) mapper.readValue(jsonRepresentingAck, Event.class)) + .withFailMessage("Ack was not properly deserialized") + .isEqualTo(ack); + + var nack = new GamestateEvent(); + nack.type = EventType.Nack; + + var jsonRepresentingNack = "{\"eventType\": \"Nack\"}"; + + assertThat((GamestateEvent) mapper.readValue(jsonRepresentingNack, Event.class)) + .withFailMessage("Nack was not properly deserialized") + .isEqualTo(nack); + } + + @Test + void GamestateEventTest() throws JsonProcessingException { + + var characterAlpha = new Character( + new EntityID(EntityType.P1, 4), + new IntVector2(5, 3), + "Alpha", + 210, + 9, + 3, + -1, // still -1 because no gameConfig object available + -1, + -1); + + characterAlpha.inventory.addStone(StoneType.valueOf(1)); + characterAlpha.inventory.addStone(StoneType.valueOf(4)); + + characterAlpha.setPosition(new IntVector2(4, 2)); + + + var characterSteelWall = new Character( + new EntityID(EntityType.P1, 4), + new IntVector2(255, 0), + "Steel Wall", + 123, + 2, + 21, + -1, // still -1 because no gameConfig object available + -1, + -1); + + 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 jsonRepresentingGamestate = """ + { + "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} + ], + "mapSize": [15, 300], + "activeCharacter": {"entityID": "P2", "ID": 3}, + "stoneCooldowns": [1, 1, 3, 1, 4, 2], + "winCondition": true + } + """; + + assertThat((GamestateEvent) mapper.readValue(jsonRepresentingGamestate, GamestateEvent.class)) + .withFailMessage("GamestateEvent was not properly deserialized") + .isEqualTo(gamestate); + } + +} \ No newline at end of file