Gamelib/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java

135 lines
4.1 KiB
Java
Raw Normal View History

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;
2021-05-13 14:10:51 +00:00
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
2021-05-30 16:07:05 +00:00
import static org.assertj.core.api.Assertions.*;
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.messages.EventMessage;
2021-05-30 16:07:05 +00:00
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;
// TODO: add proper CharacterConfig, otherwise Character events won't be deserializable
@BeforeEach
void setUp() {
messageStructureStart = """
2021-05-30 16:07:05 +00:00
{
"messages":[
""";
messageStructureEnd = new String[]{
"""
],
"messageType": "REQUESTS",
"customContentType": "TestCustomContent",
"customContent": {
"customKey" = "customResult",
"customNumber" = 15,
"customProperty" = true
}
""",
2021-05-30 16:07:05 +00:00
"""
],
"messageType": "EVENTS",
"customContentType": "TestCustomContent",
"customContent": {
"customKey": "customResult",
"customNumber": 15,
"customProperty": 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);
this.json = new JSON(null);
// FIXME: Temporary hotfix, nothing valid yet
}
@AfterEach
void tearDown() {
}
@Test
void parseNotificationEvents() {
Event[] targetEvents = {
new EventBuilder(EventType.Ack).buildGameStateEvent(),
new EventBuilder(EventType.Nack).buildGameStateEvent(),
};
String eventRepresentation = """
2021-05-30 16:07:05 +00:00
{"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
2021-05-17 18:11:21 +00:00
void parseMoreComplicatedEvents() {
String eventRepresentation = """
2021-05-30 16:07:05 +00:00
{
"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
2021-05-13 14:10:51 +00:00
@Disabled
void simpleInDevTest() throws JsonProcessingException {
String simpleEntity = """
2021-05-30 16:07:05 +00:00
{
"entityID": "P2",
"ID": 4
}
""";
System.out.println(new ObjectMapper().readValue(simpleEntity, EntityID.class));
}
@Test
void stringify() {
}
}