From f4123481bc28cbcb1b3e65a526991b9eabe733f1 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Tue, 11 May 2021 20:26:57 +0200 Subject: [PATCH] feat: implemented deserializer for IntVector2 and created a test for it --- .../marvelous/gamelibrary/IntVector2.java | 4 ++ .../json/ingame/IntVector2Deserializer.java | 24 +++++++++ .../ingame/IntVector2DeserializerTest.java | 53 +++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/IntVector2Deserializer.java create mode 100644 src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/IntVector2DeserializerTest.java diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/IntVector2.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/IntVector2.java index c6dc01c..1872d3a 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/IntVector2.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/IntVector2.java @@ -1,9 +1,13 @@ package uulm.teamname.marvelous.gamelibrary; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import uulm.teamname.marvelous.gamelibrary.json.ingame.IntVector2Deserializer; + import java.io.Serializable; import java.util.Objects; /** Represents a 2d vector of integers. */ +@JsonDeserialize(using = IntVector2Deserializer.class) public class IntVector2 implements Serializable { private int x; private int y; diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/IntVector2Deserializer.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/IntVector2Deserializer.java new file mode 100644 index 0000000..f154762 --- /dev/null +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/IntVector2Deserializer.java @@ -0,0 +1,24 @@ +package uulm.teamname.marvelous.gamelibrary.json.ingame; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.ObjectCodec; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import uulm.teamname.marvelous.gamelibrary.IntVector2; + +import java.io.IOException; + +public class IntVector2Deserializer extends JsonDeserializer { + + ObjectMapper mapper = new ObjectMapper(); + + @Override + public IntVector2 deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { + var values = mapper.readValue(p, Integer[].class); + IntVector2 result = new IntVector2(values[0], values[1]); + return result; + } +} diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/IntVector2DeserializerTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/IntVector2DeserializerTest.java new file mode 100644 index 0000000..52ce1d4 --- /dev/null +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/IntVector2DeserializerTest.java @@ -0,0 +1,53 @@ +package uulm.teamname.marvelous.gamelibrary.json.ingame; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import uulm.teamname.marvelous.gamelibrary.IntVector2; + +import java.util.Iterator; +import java.util.concurrent.ThreadLocalRandom; +import java.util.stream.IntStream; + +import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.*; +import static org.assertj.core.api.Assertions.*; + +class IntVector2DeserializerTest { + + ObjectMapper mapper; + + @BeforeEach + void beforeAll() { + mapper = new ObjectMapper(); + } + + + @Test + void simpleTest() throws JsonProcessingException { + IntVector2 target = new IntVector2(4, 17); + String jsonDescribingTarget = "[4, 17]"; + String jsonNotDescribingTarget = "[2, 21]"; + assertThat(mapper.readValue(jsonDescribingTarget, IntVector2.class)) + .isEqualTo(target); + assertThat(mapper.readValue(jsonNotDescribingTarget, IntVector2.class)) + .isNotEqualTo(target); + } + + @Test + void randomTests() throws JsonProcessingException { + Iterator randomIntegers = ThreadLocalRandom.current().ints().iterator(); + int repetitions = 50; + for (int i = 0; i < repetitions; i++) { + int x = randomIntegers.next(); + int y = randomIntegers.next(); + IntVector2 target = new IntVector2(x, y); + String jsonDescribingTarget = "[" + x + ", " + y + "]"; + assertThat(mapper.readValue(jsonDescribingTarget, IntVector2.class)) + .isEqualTo(target); + System.out.println(jsonDescribingTarget); + } + } +} \ No newline at end of file