|
|
@@ -2,9 +2,13 @@ package org.leumasjaffe.json.schema.tester;
|
|
|
|
|
|
import static org.junit.Assert.assertThat;
|
|
|
import static org.leumasjaffe.json.schema.matcher.Accepts.accepts;
|
|
|
+import static org.leumasjaffe.json.schema.matcher.JsonPath.jsonPath;
|
|
|
import static org.leumasjaffe.json.schema.matcher.Not.not;
|
|
|
|
|
|
+import org.junit.Rule;
|
|
|
import org.junit.Test;
|
|
|
+import org.junit.rules.ExpectedException;
|
|
|
+import org.leumasjaffe.json.schema.ValidationException;
|
|
|
import org.leumasjaffe.json.schema.tester.SizeTester;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
@@ -15,6 +19,8 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
import com.fasterxml.jackson.databind.node.TextNode;
|
|
|
|
|
|
public class SizeTesterTest {
|
|
|
+ @Rule public ExpectedException thrown = ExpectedException.none();
|
|
|
+
|
|
|
@Test
|
|
|
public void arrayMatcherRejectsObject() {
|
|
|
final SizeTester notEmptyArray = SizeTester.minItems(0);
|
|
|
@@ -78,4 +84,64 @@ public class SizeTesterTest {
|
|
|
assertThat(notEmptyArray, accepts(new TextNode("_")));
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void testValidateDoesNothingOnSuccess() {
|
|
|
+ SizeTester.minLength(1).validate(new TextNode("_"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test(expected=ValidationException.class)
|
|
|
+ public void testThrowsOnValidationFailure() {
|
|
|
+ SizeTester.minLength(1).validate(new TextNode(""));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testValidationPathIsMinProperties() {
|
|
|
+ thrown.expect(ValidationException.class);
|
|
|
+ thrown.expect(jsonPath("#/minProperties"));
|
|
|
+ final ObjectNode node = new ObjectNode(JsonNodeFactory.instance);
|
|
|
+ SizeTester.minProperties(1).validate(node);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testValidationPathIsMaxProperties() {
|
|
|
+ thrown.expect(ValidationException.class);
|
|
|
+ thrown.expect(jsonPath("#/maxProperties"));
|
|
|
+ final ObjectNode node = new ObjectNode(JsonNodeFactory.instance);
|
|
|
+ node.set("A", NullNode.getInstance());
|
|
|
+ node.set("B", NullNode.getInstance());
|
|
|
+ SizeTester.maxProperties(1).validate(node);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testValidationPathIsMinItems() {
|
|
|
+ thrown.expect(ValidationException.class);
|
|
|
+ thrown.expect(jsonPath("#/minItems"));
|
|
|
+ final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
|
|
|
+ SizeTester.minItems(1).validate(node);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testValidationPathIsMaxItems() {
|
|
|
+ thrown.expect(ValidationException.class);
|
|
|
+ thrown.expect(jsonPath("#/maxItems"));
|
|
|
+ final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
|
|
|
+ node.add(NullNode.getInstance());
|
|
|
+ node.add(NullNode.getInstance());
|
|
|
+ SizeTester.maxItems(1).validate(node);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testValidationPathIsMinLength() {
|
|
|
+ thrown.expect(ValidationException.class);
|
|
|
+ thrown.expect(jsonPath("#/minLength"));
|
|
|
+ SizeTester.minLength(1).validate(new TextNode(""));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testValidationPathIsMaxLength() {
|
|
|
+ thrown.expect(ValidationException.class);
|
|
|
+ thrown.expect(jsonPath("#/maxLength"));
|
|
|
+ SizeTester.maxLength(1).validate(new TextNode("Hello World"));
|
|
|
+ }
|
|
|
+
|
|
|
}
|