feat: added static methods to JSON for configuration deserialization
This commit is contained in:
parent
672e5e7727
commit
d74cd114cc
@ -4,9 +4,13 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
|||||||
import com.fasterxml.jackson.databind.InjectableValues;
|
import com.fasterxml.jackson.databind.InjectableValues;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import uulm.teamname.marvelous.gamelibrary.config.CharacterConfig;
|
import uulm.teamname.marvelous.gamelibrary.config.CharacterConfig;
|
||||||
|
import uulm.teamname.marvelous.gamelibrary.config.PartyConfig;
|
||||||
|
import uulm.teamname.marvelous.gamelibrary.config.ScenarioConfig;
|
||||||
import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage;
|
import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage;
|
||||||
import uulm.teamname.marvelous.gamelibrary.messages.server.EventMessage;
|
import uulm.teamname.marvelous.gamelibrary.messages.server.EventMessage;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -14,6 +18,68 @@ import java.util.Optional;
|
|||||||
*/
|
*/
|
||||||
public class JSON {
|
public class JSON {
|
||||||
|
|
||||||
|
private static final ObjectMapper staticMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
public static Optional<CharacterConfig> parseCharacterConfig (String jsonRepresentingConfig) {
|
||||||
|
Optional<CharacterConfig> result;
|
||||||
|
try {
|
||||||
|
result = Optional.of(staticMapper.readValue(jsonRepresentingConfig, CharacterConfig.class));
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
result = Optional.empty();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Optional<CharacterConfig> parseCharacterConfig (File jsonRepresentingConfig) {
|
||||||
|
Optional<CharacterConfig> result;
|
||||||
|
try {
|
||||||
|
result = Optional.of(staticMapper.readValue(jsonRepresentingConfig, CharacterConfig.class));
|
||||||
|
} catch (IOException e) {
|
||||||
|
result = Optional.empty();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Optional<PartyConfig> parsePartyConfig (String jsonRepresentingConfig) {
|
||||||
|
Optional<PartyConfig> result;
|
||||||
|
try {
|
||||||
|
result = Optional.of(staticMapper.readValue(jsonRepresentingConfig, PartyConfig.class));
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
result = Optional.empty();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Optional<PartyConfig> parsePartyConfig (File jsonRepresentingConfig) {
|
||||||
|
Optional<PartyConfig> result;
|
||||||
|
try {
|
||||||
|
result = Optional.of(staticMapper.readValue(jsonRepresentingConfig, PartyConfig.class));
|
||||||
|
} catch (IOException e) {
|
||||||
|
result = Optional.empty();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Optional<ScenarioConfig> parseScenarioConfig (String jsonRepresentingConfig) {
|
||||||
|
Optional<ScenarioConfig> result;
|
||||||
|
try {
|
||||||
|
result = Optional.of(staticMapper.readValue(jsonRepresentingConfig, ScenarioConfig.class));
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
result = Optional.empty();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Optional<ScenarioConfig> parseScenarioConfig (File jsonRepresentingConfig) {
|
||||||
|
Optional<ScenarioConfig> result;
|
||||||
|
try {
|
||||||
|
result = Optional.of(staticMapper.readValue(jsonRepresentingConfig, ScenarioConfig.class));
|
||||||
|
} catch (IOException e) {
|
||||||
|
result = Optional.empty();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
private final ObjectMapper mapper = new ObjectMapper();
|
private final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
public JSON (CharacterConfig config) {
|
public JSON (CharacterConfig config) {
|
||||||
|
@ -56,6 +56,65 @@ class JSONTest {
|
|||||||
// >>>>>>> Deserialization tests <<<<<<<
|
// >>>>>>> Deserialization tests <<<<<<<
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void scenarioConfigDeserializationTest() {
|
||||||
|
var jsonRepresentingConfig = """
|
||||||
|
{
|
||||||
|
"scenario":[
|
||||||
|
["GRASS", "GRASS", "GRASS", "GRASS", "GRASS"],
|
||||||
|
["GRASS", "GRASS", "GRASS", "GRASS", "GRASS"],
|
||||||
|
["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"],
|
||||||
|
["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"],
|
||||||
|
["GRASS", "GRASS", "ROCK", "ROCK", "GRASS"],
|
||||||
|
["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"],
|
||||||
|
["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"],
|
||||||
|
["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"],
|
||||||
|
["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"],
|
||||||
|
["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"],
|
||||||
|
["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"],
|
||||||
|
["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"],
|
||||||
|
["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"],
|
||||||
|
["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"],
|
||||||
|
["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"],
|
||||||
|
["ROCK", "GRASS", "ROCK", "GRASS", "GRASS"],
|
||||||
|
["ROCK", "GRASS", "ROCK", "ROCK", "GRASS"],
|
||||||
|
["ROCK", "ROCK", "ROCK", "ROCK", "GRASS"]
|
||||||
|
],
|
||||||
|
"author": "jakobmh",
|
||||||
|
"name": "avengerstower"
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
|
var result = JSON.parseScenarioConfig(jsonRepresentingConfig).get();
|
||||||
|
|
||||||
|
var actual = new ScenarioConfig();
|
||||||
|
actual.scenario = new FieldType[][] {
|
||||||
|
{FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS},
|
||||||
|
{FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS},
|
||||||
|
{FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS},
|
||||||
|
{FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS},
|
||||||
|
{FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.ROCK, FieldType.GRASS},
|
||||||
|
{FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS},
|
||||||
|
{FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS},
|
||||||
|
{FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS},
|
||||||
|
{FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS},
|
||||||
|
{FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS},
|
||||||
|
{FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS},
|
||||||
|
{FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS},
|
||||||
|
{FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS},
|
||||||
|
{FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS},
|
||||||
|
{FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS},
|
||||||
|
{FieldType.ROCK, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS},
|
||||||
|
{FieldType.ROCK, FieldType.GRASS, FieldType.ROCK, FieldType.ROCK, FieldType.GRASS},
|
||||||
|
{FieldType.ROCK, FieldType.ROCK, FieldType.ROCK, FieldType.ROCK, FieldType.GRASS}
|
||||||
|
};
|
||||||
|
|
||||||
|
actual.author = "jakobmh";
|
||||||
|
actual.name = "avengerstower";
|
||||||
|
|
||||||
|
assertThat(result).isEqualTo(actual);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void characterSelectionDeserializationParseTest() throws JsonProcessingException {
|
void characterSelectionDeserializationParseTest() throws JsonProcessingException {
|
||||||
var jsonRepresentingMessage = """
|
var jsonRepresentingMessage = """
|
||||||
|
Loading…
Reference in New Issue
Block a user