Skip to content

Commit

Permalink
Test for a constraint validator injecting config properties
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta committed Sep 23, 2024
1 parent 4452e0f commit f0fffb2
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import io.quarkus.it.hibernate.validator.groups.MyBeanWithGroups;
import io.quarkus.it.hibernate.validator.groups.ValidationGroups;
import io.quarkus.it.hibernate.validator.injection.InjectedConstraintValidatorConstraint;
import io.quarkus.it.hibernate.validator.injection.InjectedPropertyConstraintValidatorConstraint;
import io.quarkus.it.hibernate.validator.injection.MyService;
import io.quarkus.it.hibernate.validator.orm.TestEntity;
import io.quarkus.runtime.StartupEvent;
Expand Down Expand Up @@ -199,6 +200,11 @@ public String testInjection() {

result.append(formatViolations(validator.validate(new BeanWithInjectedConstraintValidatorConstraint("Invalid value"))));

result.append(formatViolations(validator.validate(new BeanWithInjectedPropertyConstraintValidatorConstraint("1234"))));

result.append(formatViolations(
validator.validate(new BeanWithInjectedPropertyConstraintValidatorConstraint("Invalid value"))));

return result.build();
}

Expand Down Expand Up @@ -432,6 +438,20 @@ public String getValue() {
}
}

public static class BeanWithInjectedPropertyConstraintValidatorConstraint {

@InjectedPropertyConstraintValidatorConstraint
private String value;

public BeanWithInjectedPropertyConstraintValidatorConstraint(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@InterceptorBinding
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.quarkus.it.hibernate.validator.injection;

import java.util.regex.Pattern;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

@ApplicationScoped
public class InjectedPropertyConstraintValidator
implements ConstraintValidator<InjectedPropertyConstraintValidatorConstraint, String> {

@Inject
ValidationConfigProperties config;
private Pattern pattern;

@Override
public void initialize(InjectedPropertyConstraintValidatorConstraint constraintAnnotation) {
pattern = Pattern.compile(config.pattern());
}

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (value == null) {
return true;
}

return pattern.matcher(value).matches();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.it.hibernate.validator.injection;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;

@Target({ ElementType.TYPE_USE, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = { InjectedPropertyConstraintValidator.class })
public @interface InjectedPropertyConstraintValidatorConstraint {

String message() default "{InjectedPropertyConstraintValidatorConstraint.message}";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.quarkus.it.hibernate.validator.injection;

import io.smallrye.config.ConfigMapping;

@ConfigMapping(prefix = "some.app.config.validation")
public interface ValidationConfigProperties {
String pattern();
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
MyCustomConstraint.message = invalid MyOtherBean
InjectedConstraintValidatorConstraint.message = InjectedConstraintValidatorConstraint violation
pattern.message=Value is not in line with the pattern
InjectedPropertyConstraintValidatorConstraint.message = InjectedPropertyConstraintValidatorConstraint violation
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ quarkus.datasource.db-kind=h2
quarkus.datasource.jdbc.url=jdbc:h2:tcp://localhost/mem:test
quarkus.hibernate-orm.database.generation = drop-and-create

# Pattern used in a custom constraint validator
some.app.config.validation.pattern=\\d+
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ public void testInjection() throws Exception {
StringBuilder expected = new StringBuilder();
expected.append("passed").append("\n");
expected.append("failed: value (InjectedConstraintValidatorConstraint violation)");
expected.append("passed").append("\n");
expected.append("failed: value (InjectedPropertyConstraintValidatorConstraint violation)");

RestAssured.when()
.get("/hibernate-validator/test/injection")
Expand Down

0 comments on commit f0fffb2

Please sign in to comment.