forked from IQSS/dataverse
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(validation): simplify ValidateURL IQSS#8531
- Simplify the code for URLValidator - Make it nullsafe - Make allowed schemes configurable from annotation - Rewrite tests to JUnit5, more examples and test with real subject classes - Move message string to validation bundle
- Loading branch information
1 parent
237d5ba
commit 223d5a2
Showing
5 changed files
with
119 additions
and
70 deletions.
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
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
131 changes: 91 additions & 40 deletions
131
src/test/java/edu/harvard/iq/dataverse/validation/URLValidatorTest.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 |
---|---|---|
@@ -1,56 +1,107 @@ | ||
|
||
package edu.harvard.iq.dataverse.validation; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import javax.validation.ConstraintViolation; | ||
import javax.validation.Validation; | ||
import javax.validation.Validator; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import javax.validation.ConstraintValidatorContext; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
import edu.harvard.iq.dataverse.validation.URLValidator; | ||
import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl; | ||
import org.hibernate.validator.internal.engine.path.PathImpl; | ||
import javax.validation.Validation; | ||
import javax.validation.ValidatorFactory; | ||
import org.junit.Test; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
/** | ||
* | ||
* @author skraffmi | ||
*/ | ||
public class URLValidatorTest { | ||
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); | ||
|
||
|
||
@Test | ||
public void testIsURLValid() { | ||
assertEquals(true, URLValidator.isURLValid(null)); | ||
assertEquals(true, URLValidator.isURLValid("")); | ||
assertEquals(true, URLValidator.isURLValid("https://twitter.com/")); | ||
|
||
assertEquals(false, URLValidator.isURLValid("cnn.com")); | ||
|
||
final Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); | ||
|
||
public static Stream<Arguments> stdUrlExamples() { | ||
return Stream.of( | ||
Arguments.of(true, null), | ||
Arguments.of(false, ""), | ||
Arguments.of(true, "https://twitter.com"), | ||
Arguments.of(true, "http://foobar.com:9101"), | ||
Arguments.of(true, "ftp://user@foobar.com"), | ||
Arguments.of(false, "cnn.com"), | ||
Arguments.of(false, "smb://user@foobar.com") | ||
); | ||
} | ||
|
||
@Test | ||
public void testIsValidWithUnspecifiedContext() { | ||
String value = "https://twitter.com/"; | ||
ConstraintValidatorContext context = null; | ||
assertEquals(true, new URLValidator().isValid(value, context)); | ||
|
||
@ParameterizedTest | ||
@MethodSource("stdUrlExamples") | ||
public void testIsURLValid(boolean expected, String url) { | ||
assertEquals(expected, URLValidator.isURLValid(url)); | ||
} | ||
|
||
@Test | ||
public void testIsValidWithContextAndValidURL() { | ||
String value = "https://twitter.com/"; | ||
ConstraintValidatorContext context = new ConstraintValidatorContextImpl(validatorFactory.getClockProvider(), PathImpl.createPathFromString(""),null, null); | ||
|
||
assertEquals(true, new URLValidator().isValid(value, context)); | ||
|
||
/** | ||
* This is a simple test class, as we defined the annotation for fields only. | ||
*/ | ||
private final class SubjectClass { | ||
@ValidateURL | ||
String url; | ||
|
||
SubjectClass(String url) { | ||
this.url = url; | ||
} | ||
} | ||
|
||
@Test | ||
public void testIsValidWithContextButInvalidURL() { | ||
String value = "cnn.com"; | ||
ConstraintValidatorContext context = new ConstraintValidatorContextImpl(validatorFactory.getClockProvider(), PathImpl.createPathFromString(""),null, null); | ||
|
||
assertEquals(false, new URLValidator().isValid(value, context)); | ||
|
||
@ParameterizedTest | ||
@MethodSource("stdUrlExamples") | ||
public void testConstraint(boolean expected, String url) { | ||
// given | ||
URLValidatorTest.SubjectClass sut = new URLValidatorTest.SubjectClass(url); | ||
|
||
//when | ||
Set<ConstraintViolation<URLValidatorTest.SubjectClass>> violations = validator.validate(sut); | ||
|
||
// then | ||
assertEquals(expected ? 0 : 1, violations.size()); | ||
violations.stream().findFirst().ifPresent( c -> { | ||
assertTrue(c.getMessage().contains(url)); }); | ||
} | ||
|
||
public static Stream<Arguments> fancyUrlExamples() { | ||
return Stream.of( | ||
Arguments.of(true, null), | ||
Arguments.of(false, ""), | ||
Arguments.of(false, "https://twitter.com"), | ||
Arguments.of(true, "http://foobar.com:9101"), | ||
Arguments.of(false, "ftp://user@foobar.com"), | ||
Arguments.of(false, "cnn.com"), | ||
Arguments.of(true, "smb://user@foobar.com") | ||
); | ||
} | ||
|
||
/** | ||
* This is a simple test class like above, but with a scheme given | ||
*/ | ||
private final class SubjectSchemeClass { | ||
@ValidateURL(schemes = {"http", "smb"}) | ||
String url; | ||
|
||
SubjectSchemeClass(String url) { | ||
this.url = url; | ||
} | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("fancyUrlExamples") | ||
public void testConstraintWithSchemes(boolean expected, String url) { | ||
// given | ||
URLValidatorTest.SubjectSchemeClass sut = new URLValidatorTest.SubjectSchemeClass(url); | ||
|
||
//when | ||
Set<ConstraintViolation<URLValidatorTest.SubjectSchemeClass>> violations = validator.validate(sut); | ||
|
||
// then | ||
assertEquals(expected ? 0 : 1, violations.size()); | ||
violations.stream().findFirst().ifPresent( c -> { | ||
assertTrue(c.getMessage().contains(url)); }); | ||
} | ||
|
||
} |