-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrong length validation of event title field (#7841)
* add DecodedSize.java annotation * formatter + checkstyle * add parameterized test for validator * fix sonar issue * add test case with null * small fix of null test case in test sources
- Loading branch information
Showing
6 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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
24 changes: 24 additions & 0 deletions
24
service-api/src/main/java/greencity/annotations/DecodedSize.java
This file contains 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,24 @@ | ||
package greencity.annotations; | ||
|
||
import greencity.validator.DecodedSizeValidator; | ||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Constraint(validatedBy = DecodedSizeValidator.class) | ||
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.PARAMETER}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface DecodedSize { | ||
String message() default "Size must be between {min} and {max} after decoding"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
|
||
int min() default 0; | ||
|
||
int max() default Integer.MAX_VALUE; | ||
} |
This file contains 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
29 changes: 29 additions & 0 deletions
29
service-api/src/main/java/greencity/validator/DecodedSizeValidator.java
This file contains 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,29 @@ | ||
package greencity.validator; | ||
|
||
import greencity.annotations.DecodedSize; | ||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import org.jsoup.parser.Parser; | ||
|
||
public class DecodedSizeValidator implements ConstraintValidator<DecodedSize, String> { | ||
private int max; | ||
private int min; | ||
|
||
@Override | ||
public void initialize(DecodedSize constraintAnnotation) { | ||
this.max = constraintAnnotation.max(); | ||
this.min = constraintAnnotation.min(); | ||
} | ||
|
||
@Override | ||
public boolean isValid(String value, ConstraintValidatorContext context) { | ||
if (value == null) { | ||
return true; | ||
} | ||
|
||
String decodedString = Parser.unescapeEntities(value, true); | ||
int length = decodedString.length(); | ||
|
||
return length >= min && length <= max; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
service-api/src/test/java/greencity/validator/DecodedSizeValidatorTest.java
This file contains 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,42 @@ | ||
package greencity.validator; | ||
|
||
import greencity.annotations.DecodedSize; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.mockito.Mockito; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class DecodedSizeValidatorTest { | ||
|
||
private final DecodedSizeValidator validator = new DecodedSizeValidator(); | ||
|
||
@ParameterizedTest | ||
@CsvSource({ | ||
"'', 0, 10, true", | ||
"'abc', 1, 3, true", | ||
"'abc', 0, 3, true", | ||
"'abc', 1, 2, false", | ||
"'abc', 2, 4, true", | ||
"'a&b&c', 3, 5, true", | ||
"'a&b&c', 0, 2, false", | ||
"'a<>&', 0, 9, true", | ||
"'a<>&', 0, 3, false", | ||
"'Hello <world>', 0, 20, true", | ||
"'Hello <world>', 0, 12, false", | ||
", 0, 0, true", | ||
}) | ||
void testDecodedSizeValidator(String input, int min, int max, boolean expectedValidity) { | ||
DecodedSize annotation = Mockito.mock(DecodedSize.class); | ||
Mockito.when(annotation.min()).thenReturn(min); | ||
Mockito.when(annotation.max()).thenReturn(max); | ||
|
||
validator.initialize(annotation); | ||
|
||
boolean isValid = validator.isValid(input, Mockito.mock(ConstraintValidatorContext.class)); | ||
boolean condition = expectedValidity == isValid; | ||
assertTrue(condition, | ||
String.format("For input '%s' with min %d and max %d, expected %b but got %b", input, min, max, | ||
expectedValidity, isValid)); | ||
} | ||
} |