Skip to content

Commit

Permalink
Migrate tests to ValidationAssertions
Browse files Browse the repository at this point in the history
  • Loading branch information
wendigo authored and findepi committed Jul 4, 2023
1 parent 5bf7af1 commit 660b260
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 157 deletions.
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<air.check.skip-pmd>true</air.check.skip-pmd>
<air.javadoc.lint>-missing</air.javadoc.lint>
<project.build.targetJdk>17</project.build.targetJdk>
<dep.airlift.version>234</dep.airlift.version>
</properties>

<organization>
Expand Down Expand Up @@ -71,14 +72,14 @@
<dependency>
<groupId>io.airlift</groupId>
<artifactId>testing</artifactId>
<version>218</version>
<version>${dep.airlift.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>json</artifactId>
<version>233</version>
<version>${dep.airlift.version}</version>
<scope>test</scope>
</dependency>

Expand Down
102 changes: 24 additions & 78 deletions src/test/java/io/airlift/units/TestDataSizeValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,21 @@
*/
package io.airlift.units;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.ValidationException;
import jakarta.validation.Validator;
import org.hibernate.validator.HibernateValidator;
import org.hibernate.validator.messageinterpolation.ParameterMessageInterpolator;
import org.testng.annotations.Test;

import java.util.Optional;
import java.util.Set;

import static io.airlift.testing.ValidationAssertions.assertFailsValidation;
import static io.airlift.testing.ValidationAssertions.assertValidates;
import static io.airlift.units.ConstraintValidatorAssert.assertThat;
import static io.airlift.units.DataSize.Unit.GIGABYTE;
import static io.airlift.units.DataSize.Unit.KILOBYTE;
import static io.airlift.units.DataSize.Unit.MEGABYTE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.testng.Assert.assertTrue;

public class TestDataSizeValidator
{
private static final Validator VALIDATOR = Validation.byProvider(HibernateValidator.class)
.configure()
// Disable classpath scanning for configuration for security reasons
.ignoreXmlConfiguration()
// Disable default message interpolation which scans classpath and required runtime dependency
.messageInterpolator(new ParameterMessageInterpolator())
.buildValidatorFactory()
.getValidator();

@Test
public void testMaxDataSizeValidator()
{
Expand Down Expand Up @@ -75,29 +60,29 @@ public void testMinDataSizeValidator()
@Test
public void testAllowsNullMinAnnotation()
{
VALIDATOR.validate(new NullMinAnnotation());
assertValidates(new NullMinAnnotation());
}

@Test
public void testAllowsNullMaxAnnotation()
{
VALIDATOR.validate(new NullMaxAnnotation());
assertValidates(new NullMaxAnnotation());
}

@Test
public void testDetectsBrokenMinAnnotation()
{
assertThatThrownBy(() -> VALIDATOR.validate(new BrokenMinAnnotation()))
assertThatThrownBy(() -> assertValidates(new BrokenMinAnnotation()))
.isInstanceOf(ValidationException.class)
.hasMessage("HV000032: Unable to initialize io.airlift.units.MinDataSizeValidator.")
.hasRootCauseInstanceOf(IllegalArgumentException.class)
.hasRootCauseMessage("size is not a valid data size string: broken");

assertThatThrownBy(() -> VALIDATOR.validate(new MinAnnotationOnOptional()))
assertThatThrownBy(() -> assertValidates(new MinAnnotationOnOptional()))
.isInstanceOf(ValidationException.class)
.hasMessageContaining("No validator could be found for constraint 'io.airlift.units.MinDataSize' validating type 'java.util.Optional<io.airlift.units.DataSize>'. Check configuration for 'constrainedByMin'");

assertThatThrownBy(() -> VALIDATOR.validate(new BrokenOptionalMinAnnotation()))
assertThatThrownBy(() -> assertValidates(new BrokenOptionalMinAnnotation()))
.isInstanceOf(ValidationException.class)
.hasMessage("HV000032: Unable to initialize io.airlift.units.MinDataSizeValidator.")
.hasRootCauseInstanceOf(IllegalArgumentException.class)
Expand All @@ -107,17 +92,17 @@ public void testDetectsBrokenMinAnnotation()
@Test
public void testDetectsBrokenMaxAnnotation()
{
assertThatThrownBy(() -> VALIDATOR.validate(new BrokenMaxAnnotation()))
assertThatThrownBy(() -> assertValidates(new BrokenMaxAnnotation()))
.isInstanceOf(ValidationException.class)
.hasMessage("HV000032: Unable to initialize io.airlift.units.MaxDataSizeValidator.")
.hasRootCauseInstanceOf(IllegalArgumentException.class)
.hasRootCauseMessage("size is not a valid data size string: broken");

assertThatThrownBy(() -> VALIDATOR.validate(new MaxAnnotationOnOptional()))
assertThatThrownBy(() -> assertValidates(new MaxAnnotationOnOptional()))
.isInstanceOf(ValidationException.class)
.hasMessageContaining("No validator could be found for constraint 'io.airlift.units.MaxDataSize' validating type 'java.util.Optional<io.airlift.units.DataSize>'. Check configuration for 'constrainedByMin'");

assertThatThrownBy(() -> VALIDATOR.validate(new BrokenOptionalMaxAnnotation()))
assertThatThrownBy(() -> assertValidates(new BrokenOptionalMaxAnnotation()))
.isInstanceOf(ValidationException.class)
.hasMessage("HV000032: Unable to initialize io.airlift.units.MaxDataSizeValidator.")
.hasRootCauseInstanceOf(IllegalArgumentException.class)
Expand All @@ -127,69 +112,30 @@ public void testDetectsBrokenMaxAnnotation()
@Test
public void testPassesValidation()
{
assertTrue(VALIDATOR.validate(new ConstrainedDataSize(new DataSize(7, MEGABYTE))).isEmpty());

assertThat(VALIDATOR.validate(new ConstrainedOptionalDataSize(Optional.of(new DataSize(7, MEGABYTE))))).isEmpty();

assertThat(VALIDATOR.validate(new ConstrainedOptionalDataSize(Optional.empty()))).isEmpty();

assertThat(VALIDATOR.validate(new ConstrainedOptionalDataSize(null))).isEmpty();
assertValidates(new ConstrainedDataSize(new DataSize(7, MEGABYTE)));
assertValidates(new ConstrainedOptionalDataSize(Optional.of(new DataSize(7, MEGABYTE))));
assertValidates(new ConstrainedOptionalDataSize(Optional.empty()));
assertValidates(new ConstrainedOptionalDataSize(null));
}

@Test
public void testFailsMaxDataSizeConstraint()
{
Set<? extends ConstraintViolation<?>> violations = VALIDATOR.validate(new ConstrainedDataSize(new DataSize(11, MEGABYTE)));
assertThat(violations).hasSize(2);
assertThat(violations)
.extracting(violation -> violation.getConstraintDescriptor().getAnnotation())
.allMatch(MaxDataSize.class::isInstance);
assertThat(violations)
.extracting(violation -> violation.getPropertyPath().toString())
.containsOnly("constrainedByMax", "constrainedByMinAndMax");
assertThat(violations)
.extracting(ConstraintViolation::getMessage)
.containsOnly("must be less than or equal to 10000kB", "must be less than or equal to 10MB");

violations = VALIDATOR.validate(new ConstrainedOptionalDataSize(Optional.of(new DataSize(11, MEGABYTE))));
assertThat(violations).hasSize(2);
assertThat(violations)
.extracting(violation -> violation.getConstraintDescriptor().getAnnotation())
.allMatch(MaxDataSize.class::isInstance);
assertThat(violations)
.extracting(violation -> violation.getPropertyPath().toString())
.containsOnly("constrainedByMax", "constrainedByMinAndMax");
assertThat(violations)
.extracting(ConstraintViolation::getMessage)
.containsOnly("must be less than or equal to 10000kB", "must be less than or equal to 10MB");
assertFailsValidation(new ConstrainedDataSize(new DataSize(11, MEGABYTE)), "constrainedByMinAndMax", "must be less than or equal to 10000kB", MaxDataSize.class);
assertFailsValidation(new ConstrainedDataSize(new DataSize(11, MEGABYTE)), "constrainedByMax", "must be less than or equal to 10MB", MaxDataSize.class);

assertFailsValidation(new ConstrainedOptionalDataSize(Optional.of(new DataSize(11, MEGABYTE))), "constrainedByMinAndMax", "must be less than or equal to 10000kB", MaxDataSize.class);
assertFailsValidation(new ConstrainedOptionalDataSize(Optional.of(new DataSize(11, MEGABYTE))), "constrainedByMax", "must be less than or equal to 10MB", MaxDataSize.class);
}

@Test
public void testFailsMinDataSizeConstraint()
{
Set<? extends ConstraintViolation<?>> violations = VALIDATOR.validate(new ConstrainedDataSize(new DataSize(1, MEGABYTE)));
assertThat(violations).hasSize(2);
assertThat(violations)
.extracting(violation -> violation.getConstraintDescriptor().getAnnotation())
.allMatch(MinDataSize.class::isInstance);
assertThat(violations)
.extracting(violation -> violation.getPropertyPath().toString())
.containsOnly("constrainedByMin", "constrainedByMinAndMax");
assertThat(violations)
.extracting(ConstraintViolation::getMessage)
.containsOnly("must be greater than or equal to 5MB", "must be greater than or equal to 5000kB");

violations = VALIDATOR.validate(new ConstrainedOptionalDataSize(Optional.of(new DataSize(1, MEGABYTE))));
assertThat(violations).hasSize(2);
assertThat(violations)
.extracting(violation -> violation.getConstraintDescriptor().getAnnotation())
.allMatch(MinDataSize.class::isInstance);
assertThat(violations)
.extracting(violation -> violation.getPropertyPath().toString())
.containsOnly("constrainedByMin", "constrainedByMinAndMax");
assertThat(violations)
.extracting(ConstraintViolation::getMessage)
.containsOnly("must be greater than or equal to 5MB", "must be greater than or equal to 5000kB");
assertFailsValidation(new ConstrainedDataSize(new DataSize(1, MEGABYTE)), "constrainedByMin", "must be greater than or equal to 5MB", MinDataSize.class);
assertFailsValidation(new ConstrainedDataSize(new DataSize(1, MEGABYTE)), "constrainedByMinAndMax", "must be greater than or equal to 5000kB", MinDataSize.class);

assertFailsValidation(new ConstrainedOptionalDataSize(Optional.of(new DataSize(1, MEGABYTE))), "constrainedByMin", "must be greater than or equal to 5MB", MinDataSize.class);
assertFailsValidation(new ConstrainedOptionalDataSize(Optional.of(new DataSize(1, MEGABYTE))), "constrainedByMinAndMax", "must be greater than or equal to 5000kB", MinDataSize.class);
}

@SuppressWarnings("UnusedDeclaration")
Expand Down
Loading

0 comments on commit 660b260

Please sign in to comment.