Skip to content

Commit 51538dd

Browse files
committed
spring-projectsgh-4901: introduce ValidatingEntityCallback
1 parent c0e7bfb commit 51538dd

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/ValidatingEntityCallback.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@
2828
/**
2929
* JSR-303 dependant entities validator.
3030
* <p>
31-
* When it is registered as Spring component its automatically invoked
32-
* after any {@link AbstractMongoEventListener} and before entities are saved in database.
31+
* When it is registered as Spring component its automatically invoked after any {@link AbstractMongoEventListener} and
32+
* before entities are saved in database.
3333
*
3434
* @author original authors of {@link ValidatingMongoEventListener}
3535
* @author Rene Felgenträger
36-
*
3736
* @see {@link ValidatingMongoEventListener}
3837
*/
3938
public class ValidatingEntityCallback implements BeforeSaveCallback<Object>, Ordered {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ValidatingEntityCallbackUnitTests.java

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,40 +34,42 @@
3434
*/
3535
class ValidatingEntityCallbackUnitTests {
3636

37-
private ValidatingEntityCallback callback;
37+
private ValidatingEntityCallback callback;
3838

39-
@BeforeEach
40-
public void setUp() {
41-
try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) {
42-
callback = new ValidatingEntityCallback(factory.getValidator());
43-
}
44-
}
39+
@BeforeEach
40+
public void setUp() {
41+
try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) {
42+
callback = new ValidatingEntityCallback(factory.getValidator());
43+
}
44+
}
4545

46-
@Test // GH-4910
47-
void invalidModel_throwsException() {
48-
Coordinates coordinates = new Coordinates(-1, -1);
46+
@Test
47+
// GH-4910
48+
void invalidModel_throwsException() {
49+
Coordinates coordinates = new Coordinates(-1, -1);
4950

50-
assertThatExceptionOfType(ConstraintViolationException.class)
51-
.isThrownBy(() -> callback.onBeforeSave(coordinates, coordinates.toDocument(), "coordinates"))
52-
.satisfies(e -> assertThat(e.getConstraintViolations()).hasSize(2));
53-
}
51+
assertThatExceptionOfType(ConstraintViolationException.class).isThrownBy(
52+
() -> callback.onBeforeSave(coordinates, coordinates.toDocument(), "coordinates"))
53+
.satisfies(e -> assertThat(e.getConstraintViolations()).hasSize(2));
54+
}
5455

55-
@Test // GH-4910
56-
void validModel_noExceptionThrown() {
57-
Coordinates coordinates = new Coordinates(0, 0);
58-
Object entity = callback.onBeforeSave(coordinates, coordinates.toDocument(), "coordinates");
59-
assertThat(entity).isEqualTo(coordinates);
60-
}
56+
@Test
57+
// GH-4910
58+
void validModel_noExceptionThrown() {
59+
Coordinates coordinates = new Coordinates(0, 0);
60+
Object entity = callback.onBeforeSave(coordinates, coordinates.toDocument(), "coordinates");
61+
assertThat(entity).isEqualTo(coordinates);
62+
}
6163

62-
record Coordinates(@NotNull @Min(0) Integer x, @NotNull @Min(0) Integer y) {
64+
record Coordinates(@NotNull @Min(0) Integer x, @NotNull @Min(0) Integer y) {
6365

64-
Document toDocument() {
65-
return Document.parse("""
66-
{
67-
"x": %d,
68-
"y": %d
69-
}
70-
""".formatted(x, y));
71-
}
72-
}
66+
Document toDocument() {
67+
return Document.parse("""
68+
{
69+
"x": %d,
70+
"y": %d
71+
}
72+
""".formatted(x, y));
73+
}
74+
}
7375
}

0 commit comments

Comments
 (0)