Skip to content

Commit

Permalink
Add a test for a bean where constraints are defined only in an XML
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta committed Sep 20, 2024
1 parent 0242fd5 commit 19d8764
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import io.quarkus.it.hibernate.validator.injection.InjectedConstraintValidatorConstraint;
import io.quarkus.it.hibernate.validator.injection.MyService;
import io.quarkus.it.hibernate.validator.orm.TestEntity;
import io.quarkus.it.hibernate.validator.xml.ValidationServiceBasedOnXmlConstraints;
import io.quarkus.runtime.StartupEvent;

@Path("/hibernate-validator/test")
Expand All @@ -63,6 +64,9 @@ public class HibernateValidatorTestResource
@Inject
EnhancedGreetingService enhancedGreetingService;

@Inject
ValidationServiceBasedOnXmlConstraints validationServiceBasedOnXmlConstraints;

@Inject
ZipCodeService zipCodeResource;

Expand Down Expand Up @@ -334,6 +338,17 @@ public String testClockBasedConstraints() {
return result.build();
}

@GET
@Path("/constraints-defined-in-xml")
@Produces(MediaType.TEXT_PLAIN)
public String testConstraintsDefinedInXml() {
ResultBuilder result = new ResultBuilder();

result.append(formatViolations(validationServiceBasedOnXmlConstraints.validateSomeMyXmlBean()));

return result.build();
}

private String formatViolations(Set<? extends ConstraintViolation<?>> violations) {
if (violations.isEmpty()) {
return "passed";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.quarkus.it.hibernate.validator.xml;

public class MyXmlBean {
int id = 0;
String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.quarkus.it.hibernate.validator.xml;

import java.util.Set;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validator;

@ApplicationScoped
public class ValidationServiceBasedOnXmlConstraints {

@Inject
Validator validator;

public Set<ConstraintViolation<MyXmlBean>> validateSomeMyXmlBean() {
return validator.validate(new MyXmlBean());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<validation-config
xmlns="https://jakarta.ee/xml/ns/validation/configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/validation/configuration https://jakarta.ee/xml/ns/validation/validation-configuration-3.0.xsd"
version="3.0">
<constraint-mapping>META-INF/validation/constraints-my-xml-bean.xml</constraint-mapping>
</validation-config>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<constraint-mappings
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/validation/mapping https://jakarta.ee/xml/ns/validation/validation-mapping-3.0.xsd"
xmlns="https://jakarta.ee/xml/ns/validation/mapping" version="3.0">

<bean class="io.quarkus.it.hibernate.validator.xml.MyXmlBean">
<field name="id">
<constraint annotation="jakarta.validation.constraints.Positive"/>
</field>
<field name="name">
<constraint annotation="jakarta.validation.constraints.NotNull"/>
</field>
</bean>
</constraint-mappings>
Original file line number Diff line number Diff line change
Expand Up @@ -540,4 +540,16 @@ void testClockBasedConstraints() {
.then()
.body(is("passed"));
}

@Test
void testConstraintsDefinedInXml() {
RestAssured.given()
.when()
.get("/hibernate-validator/test/constraints-defined-in-xml")
.then()
.statusCode(200)
.body(containsString("failed"),
containsString("id (must be greater than 0)"),
containsString("name (must not be null)"));
}
}

0 comments on commit 19d8764

Please sign in to comment.