|
|
@@ -1,21 +1,34 @@
|
|
|
package org.leumasjaffe.json.schema.tester;
|
|
|
|
|
|
+import static org.hamcrest.collection.IsIterableWithSize.iterableWithSize;
|
|
|
import static org.junit.Assert.assertThat;
|
|
|
import static org.leumasjaffe.json.schema.matcher.Accepts.accepts;
|
|
|
+import static org.leumasjaffe.json.schema.matcher.CausingExceptions.causedBy;
|
|
|
+import static org.leumasjaffe.json.schema.matcher.JsonPath.jsonPath;
|
|
|
import static org.leumasjaffe.json.schema.matcher.Not.not;
|
|
|
|
|
|
+import org.junit.Before;
|
|
|
+import org.junit.Rule;
|
|
|
import org.junit.Test;
|
|
|
+import org.junit.rules.ExpectedException;
|
|
|
import org.leumasjaffe.json.schema.Tester;
|
|
|
+import org.leumasjaffe.json.schema.ValidationException;
|
|
|
|
|
|
-import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.node.ArrayNode;
|
|
|
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
|
|
import com.fasterxml.jackson.databind.node.NullNode;
|
|
|
|
|
|
public class OneOfTesterTest {
|
|
|
- Tester isArray = JsonNode::isArray;
|
|
|
- Tester isObject = JsonNode::isObject;
|
|
|
- Tester notEmpty = j -> j.size() != 0;
|
|
|
+ @Rule public ExpectedException thrown = ExpectedException.none();
|
|
|
+
|
|
|
+ Tester isArray, isObject, notEmpty;
|
|
|
+
|
|
|
+ @Before
|
|
|
+ public void setUp() {
|
|
|
+ this.isArray = TypeTester.fromType("array");
|
|
|
+ this.isObject = TypeTester.fromType("object");
|
|
|
+ this.notEmpty = SizeTester.minItems(1);
|
|
|
+ }
|
|
|
|
|
|
@Test
|
|
|
public void testFailsIfAllFail() {
|
|
|
@@ -45,4 +58,58 @@ public class OneOfTesterTest {
|
|
|
final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
|
|
|
assertThat(fails, accepts(node));
|
|
|
}
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testValidateDoesNothingOnSuccess() {
|
|
|
+ final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
|
|
|
+ new OneOfTester(isArray, notEmpty).validate(node);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test(expected=ValidationException.class)
|
|
|
+ public void testThrowsOnValidationFailureWithNoMatches() {
|
|
|
+ final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
|
|
|
+ new OneOfTester(isObject, notEmpty).validate(node);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test(expected=ValidationException.class)
|
|
|
+ public void testThrowsOnValidationFailureWithManyMatches() {
|
|
|
+ final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
|
|
|
+ node.addNull();
|
|
|
+ new OneOfTester(isArray, notEmpty).validate(node);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testValidationPathIsAllOfWithNoMatches() {
|
|
|
+ thrown.expect(ValidationException.class);
|
|
|
+ thrown.expect(jsonPath("#/oneOf"));
|
|
|
+ final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
|
|
|
+ new OneOfTester(isObject, notEmpty).validate(node);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testValidationPathIsOneOfWithManyMatches() {
|
|
|
+ thrown.expect(ValidationException.class);
|
|
|
+ thrown.expect(jsonPath("#/oneOf"));
|
|
|
+ final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
|
|
|
+ node.addNull();
|
|
|
+ new OneOfTester(isArray, notEmpty).validate(node);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testContainsOneSubExceptionForEachFailureWithNoMatchese() {
|
|
|
+ thrown.expect(ValidationException.class);
|
|
|
+ thrown.expect(causedBy(iterableWithSize(2)));
|
|
|
+ final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
|
|
|
+ new OneOfTester(isObject, notEmpty).validate(node);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testContainsNoExceptionsForEachFailureWithManyMatches() {
|
|
|
+ thrown.expect(ValidationException.class);
|
|
|
+ thrown.expect(causedBy(iterableWithSize(0)));
|
|
|
+ final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
|
|
|
+ node.addNull();
|
|
|
+ new OneOfTester(isArray, isObject, notEmpty).validate(node);
|
|
|
+ }
|
|
|
+
|
|
|
}
|