-
-
Notifications
You must be signed in to change notification settings - Fork 0
chore(validation): rework dto validation layer #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
43271be
chore(attribute): add improved value validation
theEvilReaper 10e7415
chore(font): rework parameter validation
theEvilReaper f7b0f0d
chore(soundsoruce): add validation layer
theEvilReaper 09617cc
chore(notification): add new validation layer
theEvilReaper 18923b4
chore(sound): add validation to the SoundEventDTO file
theEvilReaper d0a6022
chore(item): add validation layer
theEvilReaper 68cee6d
chore(deps): add new dependencies for the validation testing
theEvilReaper 94c5803
chore(attribute): add missing validation annotation to the uiName field
theEvilReaper e8f5544
chore(tests): add test base class for validation tess
theEvilReaper 9a11584
fix(tests): improve assertNoViolation method
theEvilReaper 77e04e7
chore(tests): add different test implementation for each dto class
theEvilReaper 1d08644
chore(validation): improve annotation usage
theEvilReaper 300361e
fix(deps): improve version naming and usage
theEvilReaper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/test/java/net/onelitefeather/vulpes/backend/domain/ValidationTestBase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package net.onelitefeather.vulpes.backend.domain; | ||
|
|
||
| import jakarta.validation.ConstraintViolation; | ||
| import jakarta.validation.Validation; | ||
| import jakarta.validation.Validator; | ||
| import jakarta.validation.ValidatorFactory; | ||
| import org.hibernate.validator.messageinterpolation.ParameterMessageInterpolator; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| public abstract class ValidationTestBase<T> { | ||
|
|
||
| protected static Validator validator; | ||
|
|
||
| @BeforeAll | ||
| static void setupValidator() { | ||
| try (ValidatorFactory factory = Validation.byDefaultProvider() | ||
| .configure() | ||
| .messageInterpolator(new ParameterMessageInterpolator()) | ||
| .buildValidatorFactory()) { | ||
| validator = factory.getValidator(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Helper method to validate a DTO and assert that it has a violation on a specific property | ||
| * | ||
| * @param dto the DTO to validate | ||
| * @param propertyName the name of the property to validate | ||
| */ | ||
| protected void assertViolation(T dto, String propertyName) { | ||
| Set<ConstraintViolation<T>> violations = validator.validate(dto); | ||
| assertFalse(violations.isEmpty(), "Expected violations for property: " + propertyName); | ||
| boolean found = violations.stream().anyMatch(v -> v.getPropertyPath().toString().equals(propertyName)); | ||
| if (!found) { | ||
| throw new AssertionError("No violation found for property: " + propertyName); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Helper method to validate a DTO and assert that it has no violations on a specific property | ||
| * | ||
| * @param dto the DTO to validate | ||
| * @param propertyName the name of the property to validate | ||
| */ | ||
| protected void assertNoViolation(T dto, String propertyName) { | ||
| Set<ConstraintViolation<T>> violations = validator.validate(dto); | ||
| assertTrue(violations.isEmpty(), "Expected no violations for property: " + propertyName); | ||
| } | ||
| } | ||
62 changes: 62 additions & 0 deletions
62
...tefeather/vulpes/backend/domain/attribute/validation/AttributeModelDTOValidationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package net.onelitefeather.vulpes.backend.domain.attribute.validation; | ||
|
|
||
| import net.onelitefeather.vulpes.backend.domain.ValidationTestBase; | ||
| import net.onelitefeather.vulpes.backend.domain.attribute.AttributeModelDTO; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| class AttributeModelDTOValidationTest extends ValidationTestBase<AttributeModelDTO> { | ||
|
|
||
| @Test | ||
| void testEmptyUiNameValidationFail() { | ||
| AttributeModelDTO dto = new AttributeModelDTO( | ||
| UUID.randomUUID(), | ||
| "", | ||
| "empty_value", // invalid | ||
| 1.0, | ||
| 5.0 | ||
| ); | ||
|
|
||
| assertViolation(dto, "uiName"); | ||
| } | ||
|
|
||
| @Test | ||
| void testDefaultValueValidationFail() { | ||
| AttributeModelDTO dto = new AttributeModelDTO( | ||
| UUID.randomUUID(), | ||
| "Speed", | ||
| "playerSpeed", | ||
| -1.0, | ||
| 5.0 | ||
| ); | ||
|
|
||
| assertViolation(dto, "defaultValue"); | ||
| } | ||
|
|
||
| @Test | ||
| void testMaxValueValidationFail() { | ||
| AttributeModelDTO dto = new AttributeModelDTO( | ||
| UUID.randomUUID(), | ||
| "Speed", | ||
| "playerSpeed", | ||
| 0.0, | ||
| 0.0 // must be strictly positive | ||
| ); | ||
|
|
||
| assertViolation(dto, "maximumValue"); | ||
| } | ||
|
|
||
| @Test | ||
| void testEmptyVariableNameValidationFail() { | ||
| AttributeModelDTO dto = new AttributeModelDTO( | ||
| UUID.randomUUID(), | ||
| "Speed", | ||
| "", | ||
| 0.0, | ||
| 5.0 | ||
| ); | ||
|
|
||
| assertViolation(dto, "variableName"); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.