-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
133 additions
and
7 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
24 changes: 24 additions & 0 deletions
24
backend/src/main/java/com/bakdata/conquery/util/validation/DataSizeMaxValidator.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 com.bakdata.conquery.util.validation; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import jakarta.validation.constraints.Max; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.dropwizard.util.DataSize; | ||
|
||
@AutoService(ConstraintValidator.class) | ||
public class DataSizeMaxValidator implements ConstraintValidator<Max, DataSize> { | ||
|
||
private long maxBytes; | ||
|
||
@Override | ||
public void initialize(Max constraintAnnotation) { | ||
maxBytes = constraintAnnotation.value(); | ||
} | ||
|
||
@Override | ||
public boolean isValid(DataSize value, ConstraintValidatorContext context) { | ||
return value.toBytes() <= maxBytes; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
backend/src/main/java/com/bakdata/conquery/util/validation/DataSizeMinValidator.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 com.bakdata.conquery.util.validation; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import jakarta.validation.constraints.Min; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.dropwizard.util.DataSize; | ||
|
||
@AutoService(ConstraintValidator.class) | ||
public class DataSizeMinValidator implements ConstraintValidator<Min, DataSize> { | ||
|
||
private long minBytes; | ||
|
||
@Override | ||
public void initialize(Min constraintAnnotation) { | ||
minBytes = constraintAnnotation.value(); | ||
} | ||
|
||
@Override | ||
public boolean isValid(DataSize value, ConstraintValidatorContext context) { | ||
return value.toBytes() >= minBytes; | ||
} | ||
} |
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
77 changes: 77 additions & 0 deletions
77
backend/src/test/java/com/bakdata/conquery/util/validation/DataSizeValidationTest.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,77 @@ | ||
package com.bakdata.conquery.util.validation; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Set; | ||
import jakarta.validation.ConstraintViolation; | ||
import jakarta.validation.Validator; | ||
import jakarta.validation.constraints.Max; | ||
import jakarta.validation.constraints.Min; | ||
|
||
import io.dropwizard.jersey.validation.Validators; | ||
import io.dropwizard.util.DataSize; | ||
import lombok.Data; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class DataSizeValidationTest { | ||
|
||
Validator VALIDATOR = Validators.newValidator(); | ||
|
||
|
||
@Test | ||
void inBounds() { | ||
Container container = new Container(DataSize.bytes(5)); | ||
|
||
Set<ConstraintViolation<Container>> validate = VALIDATOR.validate(container); | ||
|
||
assertThat(validate).isEmpty(); | ||
|
||
} | ||
|
||
@Test | ||
void onMaxBound() { | ||
Container container = new Container(DataSize.bytes(6)); | ||
|
||
Set<ConstraintViolation<Container>> validate = VALIDATOR.validate(container); | ||
|
||
assertThat(validate).isEmpty(); | ||
|
||
} | ||
|
||
@Test | ||
void onMinBound() { | ||
Container container = new Container(DataSize.bytes(3)); | ||
|
||
Set<ConstraintViolation<Container>> validate = VALIDATOR.validate(container); | ||
|
||
assertThat(validate).isEmpty(); | ||
|
||
} | ||
|
||
@Test | ||
void maxedOut() { | ||
Container container = new Container(DataSize.bytes(7)); | ||
|
||
Set<ConstraintViolation<Container>> validate = VALIDATOR.validate(container); | ||
|
||
assertThat(validate).hasSize(1); | ||
|
||
} | ||
|
||
@Test | ||
void minedOut() { | ||
Container container = new Container(DataSize.bytes(2)); | ||
|
||
Set<ConstraintViolation<Container>> validate = VALIDATOR.validate(container); | ||
|
||
assertThat(validate).hasSize(1); | ||
|
||
} | ||
|
||
@Data | ||
private static class Container { | ||
@Min(3) | ||
@Max(6) | ||
private final DataSize size; | ||
} | ||
} |