Skip to content

Commit

Permalink
Add a few validator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andriy-dmytruk committed Nov 3, 2022
1 parent 79c3e9d commit 16874f0
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,6 @@ private boolean doesHaveValidatedTypeParameters(
) {
if (!context.elementRequireCascadeValidation.containsKey(annotatedElement)) {
context.elementRequireCascadeValidation.put(annotatedElement, false);
AnnotationMetadata annotationMetadata = annotatedElement.getAnnotationMetadata();

Argument<Object> annotatedElementAsArgument = null;
if (annotatedElement instanceof Argument) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class ValidatorSpec extends Specification {
void "validate property argument - with iterable constraints"() {
given:
var e = new ValidatorSpecClasses.Email(["[email protected]", "", "[email protected]"])
var violations = validator.validate(e);
var violations = validator.validate(e)
violations = violations.sort{it->it.getPropertyPath().toString()}

expect:
Expand Down Expand Up @@ -777,6 +777,33 @@ class ValidatorSpec extends Specification {
constraintViolations[0].toString() == 'DefaultConstraintViolation{rootBean=class io.micronaut.validation.validator.$BookService$Definition$Intercepted, invalidValue=50, path=saveBook.pages}'
constraintViolations[1].toString() == 'DefaultConstraintViolation{rootBean=class io.micronaut.validation.validator.$BookService$Definition$Intercepted, invalidValue=, path=saveBook.title}'
}
void "test cascade to container"() {
given:
def salad = new ValidatorSpecClasses.Salad([
new ValidatorSpecClasses.Ingredient("carrot"),
new ValidatorSpecClasses.Ingredient("")
])
def violations = validator.validate(salad)
expect:
violations.size() == 1
violations[0].invalidValue == ""
}
void "test cascade to container with setter"() {
given:
def salad = new ValidatorSpecClasses.SaladWithSetter()
salad.ingredients = [
new ValidatorSpecClasses.Ingredient("carrot"),
new ValidatorSpecClasses.Ingredient("")
]
def violations = validator.validate(salad)
expect:
violations.size() == 1
violations[0].invalidValue == ""
}
}
@Introspected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ public Set<Book> getBooks() {
// test validate property argument cascade
@Introspected
public static class Email {
final private @Size(max=2) List<@NotBlank String> recoveryEmails;
private @Size(max=2) List<@NotBlank String> recoveryEmails;

public Email(@Size(max=2) List<String> recoveryEmails) {
public Email(List<String> recoveryEmails) {
this.recoveryEmails = recoveryEmails;
}

Expand Down Expand Up @@ -290,4 +290,36 @@ enum AuthorState {
PUBLISHED,
DRAFT
}

// test cascade to container
@Introspected
public static class Salad {
List<@Valid Ingredient> ingredients;

public Salad(List<Ingredient> ingredients) {
this.ingredients = ingredients;
}

public List<Ingredient> getIngredients() {
return ingredients;
}
}

@Introspected
public static class SaladWithSetter {
List<@Valid Ingredient> ingredients;

public List<Ingredient> getIngredients() {
return ingredients;
}

public void setIngredients(List<Ingredient> ingredients) {
this.ingredients = ingredients;
}
}

@Introspected
public record Ingredient(
@NotBlank String name
) {}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package io.micronaut.validation.validator.pojo

import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.Executable
import io.micronaut.context.exceptions.BeanInstantiationException
import io.micronaut.validation.Pojo
import jakarta.inject.Singleton
import spock.lang.Specification

import javax.validation.ConstraintViolationException
import javax.validation.Valid


class PojoConfigurationPropertiesSpec extends Specification {
Expand All @@ -15,6 +19,20 @@ class PojoConfigurationPropertiesSpec extends Specification {
]
])

void "test @Valid on config props property manual"() {
when:
Pojo pojo = new Pojo()
pojo.name = ""
PojoConfigProps configProps = new PojoConfigProps([pojo])
// configProps.pojos = [pojo]

context.getBean(PojoConfigPropsValidator).validateConfigProps(configProps)

then:
def ex = thrown(ConstraintViolationException)
ex.message.contains("must not be blank")
}

void "test @Valid on config props property"() {
when:
context.getBean(PojoConfigProps)
Expand All @@ -24,3 +42,9 @@ class PojoConfigurationPropertiesSpec extends Specification {
ex.message.contains("List of constraint violations:[\n\tpojos[0]<E Pojo>.name - must not be blank\n]")
}
}

@Singleton
class PojoConfigPropsValidator {
@Executable
void validateConfigProps(@Valid PojoConfigProps configProps) {}
}

0 comments on commit 16874f0

Please sign in to comment.