Skip to content

Commit d099beb

Browse files
committed
tests
1 parent eba2414 commit d099beb

22 files changed

Lines changed: 415 additions & 432 deletions

File tree

docs/asciidoc/modules/avaje-inject.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<path>
2222
<groupId>io.avaje</groupId>
2323
<artifactId>avaje-inject-generator</artifactId>
24-
<version>10.0</version>
24+
<version>10.3</version>
2525
</path>
2626
</annotationProcessorPaths>
2727
</configuration>

modules/jooby-avaje-inject/pom.xml

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111

1212
<modelVersion>4.0.0</modelVersion>
1313
<artifactId>jooby-avaje-inject</artifactId>
14-
14+
15+
<properties>
16+
<maven.compiler.proc>full</maven.compiler.proc>
17+
</properties>
18+
1519
<dependencies>
1620
<dependency>
1721
<groupId>com.github.spotbugs</groupId>
@@ -21,7 +25,6 @@
2125
<dependency>
2226
<groupId>io.jooby</groupId>
2327
<artifactId>jooby</artifactId>
24-
<version>${jooby.version}</version>
2528
</dependency>
2629

2730
<!-- Avaje Inject -->
@@ -33,7 +36,7 @@
3336
<dependency>
3437
<groupId>io.avaje</groupId>
3538
<artifactId>avaje-inject-generator</artifactId>
36-
<scope>provided</scope>
39+
<scope>test</scope>
3740
</dependency>
3841

3942
<!-- Test dependencies -->
@@ -74,30 +77,4 @@
7477
<scope>test</scope>
7578
</dependency>
7679
</dependencies>
77-
78-
<build>
79-
<plugins>
80-
<plugin>
81-
<groupId>org.apache.maven.plugins</groupId>
82-
<artifactId>maven-compiler-plugin</artifactId>
83-
<executions>
84-
<execution>
85-
<id>test</id>
86-
<phase>test-compile</phase>
87-
</execution>
88-
</executions>
89-
<configuration>
90-
<compilerArgs>
91-
<arg>-parameters</arg>
92-
</compilerArgs>
93-
<annotationProcessorPaths>
94-
<path>
95-
<groupId>io.avaje</groupId>
96-
<artifactId>avaje-inject-generator</artifactId>
97-
</path>
98-
</annotationProcessorPaths>
99-
</configuration>
100-
</plugin>
101-
</plugins>
102-
</build>
10380
</project>

modules/jooby-avaje-validator/pom.xml

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111

1212
<modelVersion>4.0.0</modelVersion>
1313
<artifactId>jooby-avaje-validator</artifactId>
14-
14+
15+
<properties>
16+
<maven.compiler.proc>full</maven.compiler.proc>
17+
</properties>
18+
1519
<dependencies>
1620
<dependency>
1721
<groupId>io.jooby</groupId>
@@ -41,7 +45,18 @@
4145
<artifactId>jooby-netty</artifactId>
4246
<scope>test</scope>
4347
</dependency>
44-
48+
49+
<dependency>
50+
<groupId>io.jooby</groupId>
51+
<artifactId>jooby-apt</artifactId>
52+
<scope>test</scope>
53+
</dependency>
54+
<dependency>
55+
<groupId>io.avaje</groupId>
56+
<artifactId>avaje-validator-generator</artifactId>
57+
<scope>test</scope>
58+
</dependency>
59+
4560
<dependency>
4661
<groupId>io.jooby</groupId>
4762
<artifactId>jooby-jackson</artifactId>
@@ -95,16 +110,6 @@
95110
<compilerArgs>
96111
<arg>-parameters</arg>
97112
</compilerArgs>
98-
<annotationProcessorPaths>
99-
<path>
100-
<groupId>io.jooby</groupId>
101-
<artifactId>jooby-apt</artifactId>
102-
</path>
103-
<path>
104-
<groupId>io.avaje</groupId>
105-
<artifactId>avaje-validator-generator</artifactId>
106-
</path>
107-
</annotationProcessorPaths>
108113
</configuration>
109114
</plugin>
110115
</plugins>

modules/jooby-avaje-validator/src/main/java/io/jooby/avaje/validator/AvajeValidatorModule.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import edu.umd.cs.findbugs.annotations.NonNull;
1717
import io.avaje.validation.ConstraintViolationException;
1818
import io.avaje.validation.Validator;
19+
import io.jooby.Context;
1920
import io.jooby.Extension;
2021
import io.jooby.Jooby;
2122
import io.jooby.StatusCode;
@@ -169,8 +170,8 @@ static class MvcValidatorImpl implements MvcValidator {
169170
}
170171

171172
@Override
172-
public void validate(Object bean) throws ConstraintViolationException {
173-
validator.validate(bean);
173+
public void validate(Context ctx, Object bean) throws ConstraintViolationException {
174+
validator.validate(bean, ctx.locale());
174175
}
175176
}
176177
}
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
package io.jooby.avaje.validator;
2+
3+
import io.jooby.avaje.validator.app.App;
4+
import io.jooby.avaje.validator.app.NewAccountRequest;
5+
import io.jooby.avaje.validator.app.Person;
6+
import io.jooby.test.JoobyTest;
7+
import io.jooby.validation.ValidationResult;
8+
import io.restassured.RestAssured;
9+
import io.restassured.builder.RequestSpecBuilder;
10+
import io.restassured.http.ContentType;
11+
import io.restassured.specification.RequestSpecification;
12+
import org.assertj.core.api.Assertions;
13+
import org.junit.jupiter.api.Test;
14+
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
import java.util.Map;
18+
19+
import static io.jooby.StatusCode.UNPROCESSABLE_ENTITY_CODE;
20+
import static io.jooby.avaje.validator.app.App.DEFAULT_TITLE;
21+
import static io.restassured.RestAssured.given;
22+
23+
@JoobyTest(value = App.class, port = 8099)
24+
public class AvajeValidatorModuleTest {
25+
26+
protected static RequestSpecification SPEC =
27+
new RequestSpecBuilder()
28+
.setPort(8099)
29+
.setContentType(ContentType.JSON)
30+
.setAccept(ContentType.JSON)
31+
.build();
32+
33+
static {
34+
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
35+
}
36+
37+
@Test
38+
public void validate_personBean_shouldDetect2Violations() {
39+
Person person = new Person(null, "Last Name");
40+
41+
ValidationResult actualResult =
42+
given()
43+
.spec(SPEC)
44+
.with()
45+
.body(person)
46+
.post("/create-person")
47+
.then()
48+
.assertThat()
49+
.statusCode(UNPROCESSABLE_ENTITY_CODE)
50+
.extract()
51+
.as(ValidationResult.class);
52+
53+
var fieldError =
54+
new ValidationResult.Error(
55+
"firstName", List.of("must not be empty"), ValidationResult.ErrorType.FIELD);
56+
ValidationResult expectedResult = buildResult(List.of(fieldError));
57+
58+
Assertions.assertThat(expectedResult)
59+
.usingRecursiveComparison()
60+
.ignoringCollectionOrderInFieldsMatchingRegexes("errors\\.messages")
61+
.isEqualTo(actualResult);
62+
}
63+
64+
@Test
65+
public void validate_arrayOfPerson_shouldDetect2Violations() {
66+
Person person1 = new Person("First Name", "Last Name");
67+
Person person2 = new Person(null, "Last Name 2");
68+
69+
ValidationResult actualResult =
70+
given()
71+
.spec(SPEC)
72+
.with()
73+
.body(new Person[] {person1, person2})
74+
.post("/create-array-of-persons")
75+
.then()
76+
.assertThat()
77+
.statusCode(UNPROCESSABLE_ENTITY_CODE)
78+
.extract()
79+
.as(ValidationResult.class);
80+
81+
var fieldError =
82+
new ValidationResult.Error(
83+
"firstName", List.of("must not be empty"), ValidationResult.ErrorType.FIELD);
84+
ValidationResult expectedResult = buildResult(List.of(fieldError));
85+
86+
Assertions.assertThat(expectedResult)
87+
.usingRecursiveComparison()
88+
.ignoringCollectionOrderInFieldsMatchingRegexes("errors\\.messages")
89+
.isEqualTo(actualResult);
90+
}
91+
92+
@Test
93+
public void validate_listOfPerson_shouldDetect2Violations() {
94+
Person person1 = new Person("First Name", "Last Name");
95+
Person person2 = new Person(null, "Last Name 2");
96+
97+
ValidationResult actualResult =
98+
given()
99+
.spec(SPEC)
100+
.with()
101+
.body(List.of(person1, person2))
102+
.post("/create-list-of-persons")
103+
.then()
104+
.assertThat()
105+
.statusCode(UNPROCESSABLE_ENTITY_CODE)
106+
.extract()
107+
.as(ValidationResult.class);
108+
109+
var fieldError =
110+
new ValidationResult.Error(
111+
"firstName", List.of("must not be empty"), ValidationResult.ErrorType.FIELD);
112+
ValidationResult expectedResult = buildResult(List.of(fieldError));
113+
114+
Assertions.assertThat(expectedResult)
115+
.usingRecursiveComparison()
116+
.ignoringCollectionOrderInFieldsMatchingRegexes("errors\\.messages")
117+
.isEqualTo(actualResult);
118+
}
119+
120+
@Test
121+
public void validate_mapOfPerson_shouldDetect2Violations() {
122+
Person person1 = new Person("First Name", "Last Name");
123+
Person person2 = new Person(null, "Last Name 2");
124+
125+
ValidationResult actualResult =
126+
given()
127+
.spec(SPEC)
128+
.with()
129+
.body(Map.of("1", person1, "2", person2))
130+
.post("/create-map-of-persons")
131+
.then()
132+
.assertThat()
133+
.statusCode(UNPROCESSABLE_ENTITY_CODE)
134+
.extract()
135+
.as(ValidationResult.class);
136+
137+
var fieldError =
138+
new ValidationResult.Error(
139+
"firstName", List.of("must not be empty"), ValidationResult.ErrorType.FIELD);
140+
ValidationResult expectedResult = buildResult(List.of(fieldError));
141+
142+
Assertions.assertThat(expectedResult)
143+
.usingRecursiveComparison()
144+
.ignoringCollectionOrderInFieldsMatchingRegexes("errors\\.messages")
145+
.isEqualTo(actualResult);
146+
}
147+
148+
@Test
149+
public void validate_newAccountBean_shouldDetect6Violations() {
150+
NewAccountRequest request = new NewAccountRequest();
151+
request.setLogin("jk");
152+
request.setPassword("123");
153+
request.setConfirmPassword("1234");
154+
request.setPerson(new Person(null, "Last Name"));
155+
156+
ValidationResult actualResult =
157+
given()
158+
.spec(SPEC)
159+
.with()
160+
.body(request)
161+
.post("/create-new-account")
162+
.then()
163+
.assertThat()
164+
.statusCode(UNPROCESSABLE_ENTITY_CODE)
165+
.extract()
166+
.as(ValidationResult.class);
167+
168+
List<ValidationResult.Error> errors =
169+
List.of(
170+
new ValidationResult.Error(
171+
"password",
172+
List.of("length must be between 8 and 24"),
173+
ValidationResult.ErrorType.FIELD),
174+
new ValidationResult.Error(
175+
"person.firstName", List.of("must not be empty"), ValidationResult.ErrorType.FIELD),
176+
new ValidationResult.Error(
177+
"confirmPassword",
178+
List.of("length must be between 8 and 24"),
179+
ValidationResult.ErrorType.FIELD),
180+
new ValidationResult.Error(
181+
"login",
182+
List.of("length must be between 3 and 16"),
183+
ValidationResult.ErrorType.FIELD));
184+
185+
ValidationResult expectedResult = buildResult(errors);
186+
187+
Assertions.assertThat(expectedResult)
188+
.usingRecursiveComparison()
189+
.ignoringCollectionOrderInFieldsMatchingRegexes("errors")
190+
.ignoringCollectionOrderInFieldsMatchingRegexes("errors\\.messages")
191+
.isEqualTo(actualResult);
192+
}
193+
194+
private ValidationResult buildResult(List<ValidationResult.Error> errors) {
195+
return new ValidationResult(DEFAULT_TITLE, UNPROCESSABLE_ENTITY_CODE, errors);
196+
}
197+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.jooby.avaje.validator.app;
2+
3+
import io.jooby.Jooby;
4+
import io.jooby.StatusCode;
5+
import io.jooby.avaje.validator.AvajeValidatorModule;
6+
import io.jooby.avaje.validator.ConstraintViolationHandler;
7+
import io.jooby.jackson.JacksonModule;
8+
import jakarta.validation.ConstraintViolationException;
9+
10+
public class App extends Jooby {
11+
12+
private static final StatusCode STATUS_CODE = StatusCode.UNPROCESSABLE_ENTITY;
13+
public static final String DEFAULT_TITLE = "Validation failed";
14+
15+
{
16+
install(new JacksonModule());
17+
install(new AvajeValidatorModule());
18+
19+
mvc(new Controller());
20+
21+
error(
22+
ConstraintViolationException.class,
23+
new ConstraintViolationHandler(STATUS_CODE, DEFAULT_TITLE));
24+
}
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.jooby.avaje.validator.app;
2+
3+
import io.jooby.annotation.POST;
4+
import io.jooby.annotation.Path;
5+
import jakarta.validation.Valid;
6+
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
@Path("")
11+
public class Controller {
12+
13+
@POST("/create-person")
14+
public void createPerson(@Valid Person person) {}
15+
16+
@POST("/create-array-of-persons")
17+
public void createArrayOfPersons(@Valid Person[] persons) {}
18+
19+
@POST("/create-list-of-persons")
20+
public void createListOfPersons(@Valid List<Person> persons) {}
21+
22+
@POST("/create-map-of-persons")
23+
public void createMapOfPersons(@Valid Map<String, Person> persons) {}
24+
25+
@POST("/create-new-account")
26+
public void createNewAccount(@Valid NewAccountRequest request) {}
27+
}

0 commit comments

Comments
 (0)