Add JSON Schema validation
This commit is contained in:
parent
9f21a102c4
commit
fd6848dddf
|
@ -11,10 +11,30 @@ import org.junit.Test;
|
|||
public class JSONSchemaTest {
|
||||
|
||||
@Test
|
||||
public void validateJSON() {
|
||||
public void givenInvalidInput_whenValidating_thenInvalid() {
|
||||
|
||||
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json")));
|
||||
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product.json")));
|
||||
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product_invalid.json")));
|
||||
|
||||
Schema schema = SchemaLoader.load(jsonSchema);
|
||||
|
||||
try {
|
||||
|
||||
schema.validate(jsonSubject);
|
||||
}
|
||||
|
||||
catch (ValidationException e) {
|
||||
|
||||
System.out.println(e.getMessage());
|
||||
e.getCausingExceptions().stream().map(ValidationException::getMessage).forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidInput_whenValidating_thenValid() {
|
||||
|
||||
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json")));
|
||||
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product_valid.json")));
|
||||
|
||||
Schema schema = SchemaLoader.load(jsonSchema);
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"id": 1,
|
||||
"name": "Lampshade",
|
||||
"price": 0
|
||||
"price": 10
|
||||
}
|
Loading…
Reference in New Issue