-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidatorTest.java
More file actions
45 lines (37 loc) · 1.41 KB
/
ValidatorTest.java
File metadata and controls
45 lines (37 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package org.burningwave.json;
import java.util.Collection;
import org.burningwave.json.bean.Root;
import org.junit.jupiter.api.Test;
class ValidatorTest extends BaseTest {
static final Facade facade = Facade.create();
@Test
void validateTestOne() {
testThrow(() -> {
facade.validator().registerCheck(
//Checking whether a value in any field marked as required (e.g.: @JsonProperty(value = "answer", required = true)) is null
Check.forAll().checkMandatory(),
//Checking whether a string value in any field is empty
Check.forAllStringValues().execute(pathValidationContext -> {
if (pathValidationContext.getValue() != null && pathValidationContext.getValue().trim().equals("")) {
pathValidationContext.rejectValue("IS_EMPTY", "is empty");
}
})
);
//Loading the JSON object
ObjectHandler objectHandler = facade.newObjectHandler(
ObjectHandlerTest.class.getClassLoader().getResourceAsStream("quiz.json"),
Root.class
);
Collection<Throwable> exceptions = facade.validator().validate(
Validation.Config.forJsonObject(objectHandler.getValue())
//By calling this method the validation will be performed on the entire document,
//otherwise the validation will stop at the first exception thrown
.withCompleteValidation()
);
for (Throwable exc : exceptions) {
System.err.println(exc.getMessage());
};
throw exceptions.iterator().next();
});
}
}