feat: created proper JSON deserializer and created test for JSON, which is still used for TDD
This commit is contained in:
parent
e3635851d2
commit
6947105077
@ -1,2 +1,68 @@
|
||||
package uulm.teamname.marvelous.gamelibrary.json.ingame;public class EventDeserializer {
|
||||
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.entities.Entity;
|
||||
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
|
||||
import uulm.teamname.marvelous.gamelibrary.events.Event;
|
||||
import uulm.teamname.marvelous.gamelibrary.events.EventType;
|
||||
import uulm.teamname.marvelous.gamelibrary.events.GamestateEvent;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class EventDeserializer extends JsonDeserializer<Event> {
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public Event deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
||||
ObjectCodec codec = p.getCodec();
|
||||
JsonNode node = codec.readTree(p);
|
||||
|
||||
JsonNode currentNode = null;
|
||||
|
||||
Event result = null;
|
||||
EventType eventType = null;
|
||||
|
||||
if (!node.has("eventType")) {
|
||||
throw new IOException("Event had wrong format: no EventType found");
|
||||
} else {
|
||||
eventType = EventType.valueOf(node.get("eventType").asText());
|
||||
|
||||
switch (eventType) {
|
||||
case Ack:
|
||||
case Nack:
|
||||
case GamestateEvent:
|
||||
result = new GamestateEvent();
|
||||
result.type = eventType;
|
||||
|
||||
if ((currentNode = node.get("activeCharacter")) != null) {
|
||||
((GamestateEvent) result).activeCharacter = mapper
|
||||
.readValue(currentNode.toString(), EntityID.class);
|
||||
}
|
||||
|
||||
if ((currentNode = node.get("turnOrder")) != null) {
|
||||
((GamestateEvent) result).turnOrder = mapper
|
||||
.readValue(currentNode.toString(), EntityID[].class);
|
||||
}
|
||||
|
||||
|
||||
// currentNode = node.get("entities");
|
||||
// ((GameStateEvent) result).entities = currentNode == null
|
||||
// ? null
|
||||
// : mapper.readValue(currentNode.asText(), Entity[].class);
|
||||
//
|
||||
// currentNode = node.get("turnOrder");
|
||||
// ((GameStateEvent) result).turnOrder = currentNode == null
|
||||
// ? null
|
||||
// : mapper.readValue(currentNode.asText(), EntityID[].class);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,125 @@
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
package uulm.teamname.marvelous.gamelibrary.json;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import uulm.teamname.marvelous.gamelibrary.entities.Entity;
|
||||
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
|
||||
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.json.ingame.MessageStructure;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
class JSONTest {
|
||||
|
||||
|
||||
String messageStructureStart;
|
||||
|
||||
/** 0 is REQUESTS, 1 is EVENTS */
|
||||
String[] messageStructureEnd;
|
||||
|
||||
/** Still need to add: messages, messageType */
|
||||
MessageStructure target;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
messageStructureStart = """
|
||||
{
|
||||
"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
|
||||
}
|
||||
}
|
||||
""",
|
||||
};
|
||||
target = new MessageStructure();
|
||||
target.customContentType = "TestCustomContent";
|
||||
target.customContent = new HashMap<String, Object>();
|
||||
target.customContent.put("customKey", "customResult");
|
||||
target.customContent.put("customNumber", 15);
|
||||
target.customContent.put("customProperty", true);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
}
|
||||
|
||||
@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
|
||||
void somewhatComplicatedInDevelopmentTest() {
|
||||
String eventRepresentation = """
|
||||
{
|
||||
"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}
|
||||
]
|
||||
}
|
||||
""";
|
||||
String completeMessageStructure = messageStructureStart + eventRepresentation + messageStructureEnd[1];
|
||||
|
||||
System.out.println(JSON.parse(completeMessageStructure));
|
||||
}
|
||||
|
||||
@Test
|
||||
void simpleInDevTest() throws JsonProcessingException {
|
||||
String simpleEntity = """
|
||||
{
|
||||
"entityID": "P2",
|
||||
"ID": 4
|
||||
}
|
||||
""";
|
||||
System.out.println(new ObjectMapper().readValue(simpleEntity, EntityID.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void stringify() {
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user