feat: implemented deserializer for IntVector2 and created a test for it

This commit is contained in:
Yannik Bretschneider 2021-05-11 20:26:57 +02:00
parent f09b9e2388
commit f4123481bc
3 changed files with 81 additions and 0 deletions

View File

@ -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;

View File

@ -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<IntVector2> {
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;
}
}

View File

@ -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<Integer> 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);
}
}
}