fix: enabled validation for all characters in CharacterConfig

This commit is contained in:
Yannik Bretschneider 2021-06-05 16:21:46 +02:00
parent ebf9e3e393
commit bf2ef23733
2 changed files with 77 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package uulm.teamname.marvelous.gamelibrary.config; package uulm.teamname.marvelous.gamelibrary.config;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotEmpty;
import java.util.*; import java.util.*;
@ -11,6 +12,7 @@ import java.util.*;
public class CharacterConfig { public class CharacterConfig {
@NotEmpty @NotEmpty
@Valid
public CharacterProperties[] characters; public CharacterProperties[] characters;
@JsonIgnore private Map<String, CharacterProperties> propertyMap; @JsonIgnore private Map<String, CharacterProperties> propertyMap;

View File

@ -4,6 +4,9 @@ import jakarta.validation.ConstraintViolation;
import jakarta.validation.ValidatorFactory; import jakarta.validation.ValidatorFactory;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import uulm.teamname.marvelous.gamelibrary.config.CharacterConfig;
import uulm.teamname.marvelous.gamelibrary.config.CharacterProperties;
import uulm.teamname.marvelous.gamelibrary.config.ScenarioConfig;
import uulm.teamname.marvelous.gamelibrary.json.ValidationUtility; import uulm.teamname.marvelous.gamelibrary.json.ValidationUtility;
import uulm.teamname.marvelous.gamelibrary.messages.client.CharacterSelectionMessage; import uulm.teamname.marvelous.gamelibrary.messages.client.CharacterSelectionMessage;
@ -55,4 +58,76 @@ class MessageValidationUtilityTest {
.isEqualTo("characters: doesn't have twelve elements"); .isEqualTo("characters: doesn't have twelve elements");
} }
@Test
void validateCharacterConfigTest() {
var racoon = new CharacterProperties();
racoon.characterID = 1;
racoon.name = "Rocket Raccoon";
racoon.HP = 100;
racoon.MP = 2;
racoon.AP = 2;
racoon.meleeDamage = 10;
racoon.rangedDamage = 30;
racoon.attackRange = 5;
var quicksilver = new CharacterProperties();
quicksilver.characterID = 2;
quicksilver.name = "Quicksilver";
quicksilver.HP = 100;
quicksilver.MP = 6;
quicksilver.AP = 1;
quicksilver.meleeDamage = 10;
quicksilver.rangedDamage = 10;
quicksilver.attackRange = 3;
var hulk = new CharacterProperties();
hulk.characterID = 3;
hulk.name = "Hulk";
hulk.HP = 100;
hulk.MP = 2;
hulk.AP = 2;
hulk.meleeDamage = 10;
hulk.rangedDamage = 30;
hulk.attackRange = 5;
var loki = new CharacterProperties();
loki.characterID = 19;
loki.name = "Loki";
loki.HP = 100;
loki.MP = 6;
loki.AP = 1;
loki.meleeDamage = 10;
loki.rangedDamage = 10;
loki.attackRange = 3;
var silversurfer = new CharacterProperties();
silversurfer.characterID = 20;
silversurfer.name = "";
silversurfer.HP = 100;
silversurfer.MP = 6;
silversurfer.AP = 1;
silversurfer.meleeDamage = -1;
// silversurfer.rangedDamage = 10;
silversurfer.attackRange = 3;
var characterConfig = new CharacterConfig();
assertThat(ValidationUtility.validate(characterConfig).get())
.isEqualTo("characters must not be empty");
characterConfig.characters = new CharacterProperties[] {racoon, quicksilver, hulk, loki, silversurfer};
assertThat(ValidationUtility.validate(characterConfig).get())
.contains("characters[4].rangedDamage must be greater than 0",
"characters[4].name must not be empty",
"characters[4].meleeDamage must be greater than 0");
}
} }