Pārlūkot izejas kodu

Add new tests for most of the testers that use sub-testers.
Missing Tests:
- AllItemsTester (used by items, additionalItems, and additionalProperties)
- DependecyTester
- PropertyTester (used by properties, patternProperties)

Sam Jaffe 6 gadi atpakaļ
vecāks
revīzija
6bfb3f687b

+ 47 - 4
src/test/java/org/leumasjaffe/json/schema/tester/AllOfTesterTest.java

@@ -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 AllOfTesterTest {
-	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,34 @@ public class AllOfTesterTest {
 		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
 		assertThat(fails, not(accepts(node)));
 	}
+	
+	@Test
+	public void testValidateDoesNothingOnSuccess() {
+		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
+		node.add(true);
+		new AllOfTester(isArray, notEmpty).validate(node);
+	}
+	
+	@Test(expected=ValidationException.class)
+	public void testThrowsOnValidationFailure() {
+		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
+		new AllOfTester(isArray, notEmpty).validate(node);
+	}
+	
+	@Test
+	public void testValidationPathIsAllOf() {
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/allOf"));
+		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
+		new AllOfTester(isArray, isObject, notEmpty).validate(node);
+	}
+	
+	@Test
+	public void testContainsOneSubExceptionForEachFailure() {
+		thrown.expect(ValidationException.class);
+		thrown.expect(causedBy(iterableWithSize(2)));
+		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
+		new AllOfTester(isArray, isObject, notEmpty).validate(node);
+	}
+	
 }

+ 46 - 4
src/test/java/org/leumasjaffe/json/schema/tester/AnyOfTesterTest.java

@@ -1,21 +1,35 @@
 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;
+import com.fasterxml.jackson.databind.node.TextNode;
 
 public class AnyOfTesterTest {
-	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.minLength(1);
+	}
 
 	@Test
 	public void testFailsIfAllFail() {
@@ -45,4 +59,32 @@ public class AnyOfTesterTest {
 		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
 		assertThat(fails, accepts(node));
 	}
+	
+	@Test
+	public void testValidateDoesNothingOnSuccess() {
+		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
+		node.add(true);
+		new AnyOfTester(isArray, notEmpty).validate(node);
+	}
+	
+	@Test(expected=ValidationException.class)
+	public void testThrowsOnValidationFailure() {
+		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
+		new AnyOfTester(isArray, notEmpty).validate(node);
+	}
+	
+	@Test
+	public void testValidationPathIsAnyOf() {
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/anyOf"));
+		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
+		new AnyOfTester(isArray, isObject, notEmpty).validate(node);
+	}
+	
+	@Test
+	public void testContainsOneSubExceptionForEachSubTest() {
+		thrown.expect(ValidationException.class);
+		thrown.expect(causedBy(iterableWithSize(3)));
+		new AnyOfTester(isArray, isObject, notEmpty).validate(new TextNode(""));
+	}
 }

+ 40 - 0
src/test/java/org/leumasjaffe/json/schema/tester/ContainsTesterTest.java

@@ -1,11 +1,17 @@
 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.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;
@@ -13,6 +19,7 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory;
 import com.fasterxml.jackson.databind.node.NullNode;
 
 public class ContainsTesterTest {
+    @Rule public ExpectedException thrown = ExpectedException.none();
 
 	@Test
 	public void testRejectsNonArray() {
@@ -42,4 +49,37 @@ public class ContainsTesterTest {
 		node.add(true);
 		assertThat(new ContainsTester(test), accepts(node));
 	}
+	
+	@Test
+	public void testValidateDoesNothingOnSuccess() {
+		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
+		node.addNull();
+		new ContainsTester(TypeTester.fromType("null")).validate(node);
+	}
+	
+	@Test(expected=ValidationException.class)
+	public void testThrowsOnValidationFailure() {
+		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
+		node.add(true);
+		new ContainsTester(TypeTester.fromType("null")).validate(node);
+	}
+	
+	@Test
+	public void testValidationPathIsAnyOf() {
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/contains"));
+		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
+		node.add(true);
+		new ContainsTester(TypeTester.fromType("null")).validate(node);
+	}
+	
+	@Test
+	public void testContainsOneSubExceptionForEachArrayElement() {
+		thrown.expect(ValidationException.class);
+		thrown.expect(causedBy(iterableWithSize(2)));
+		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
+		node.add(true);
+		node.add(true);
+		new ContainsTester(TypeTester.fromType("null")).validate(node);
+	}
 }

+ 47 - 2
src/test/java/org/leumasjaffe/json/schema/tester/ItemsTesterTest.java

@@ -1,11 +1,17 @@
 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.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;
@@ -13,10 +19,12 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory;
 import com.fasterxml.jackson.databind.node.NullNode;
 
 public class ItemsTesterTest {
-	
+    @Rule public ExpectedException thrown = ExpectedException.none();
+
 	@Test
 	public void testRejectsNonArray() {
-		assertThat(new ItemsTester(FixedTester.ACCEPT), not(accepts(NullNode.getInstance())));
+		assertThat(new ItemsTester(FixedTester.ACCEPT),
+				not(accepts(NullNode.getInstance())));
 	}
 
 	@Test
@@ -51,4 +59,41 @@ public class ItemsTesterTest {
 		assertThat(new ItemsTester(test, FixedTester.ACCEPT, FixedTester.REJECT),
 				accepts(node));
 	}
+	
+	@Test
+	public void testValidateDoesNothingOnSuccess() {
+		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
+		node.addNull();
+		new ItemsTester(TypeTester.fromType("null")).validate(node);
+	}
+	
+	@Test(expected=ValidationException.class)
+	public void testThrowsOnValidationFailure() {
+		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
+		node.add(true);
+		new ItemsTester(TypeTester.fromType("null")).validate(node);
+	}
+	
+	@Test
+	public void testValidationPathIsItems() {
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/items"));
+		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
+		node.add(true);
+		new ItemsTester(TypeTester.fromType("null")).validate(node);
+	}
+	
+	@Test
+	public void testContainsOneSubExceptionForEachFailure() {
+		thrown.expect(ValidationException.class);
+		thrown.expect(causedBy(iterableWithSize(2)));
+		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
+		node.addNull();
+		node.add(true);
+		node.add(true);
+		node.add(true); // Since we don't test after the count limit
+		Tester isNull = TypeTester.fromType("null");
+		new ItemsTester(isNull, isNull, isNull).validate(node);
+	}
+	
 }

+ 45 - 7
src/test/java/org/leumasjaffe/json/schema/tester/NotTesterTest.java

@@ -1,30 +1,68 @@
 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 org.leumasjaffe.json.schema.tester.NotTester;
 
-import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.node.BooleanNode;
 import com.fasterxml.jackson.databind.node.NullNode;
 
 public class NotTesterTest {
+    @Rule public ExpectedException thrown = ExpectedException.none();
+    
+    Tester nullTester;
+    Tester notNullTester;
+    
+    @Before
+    public void setUp() {
+    	nullTester = TypeTester.fromType("null");
+    	notNullTester = new NotTester(nullTester);
+    }
 
 	@Test
 	public void testNotInvertsOutputTrue() {
-		Tester impl = JsonNode::isNull;
-		assertThat(impl, accepts(NullNode.getInstance()));
-		assertThat(new NotTester(impl), not(accepts(NullNode.getInstance())));
+		assertThat(nullTester, accepts(NullNode.getInstance()));
+		assertThat(notNullTester, not(accepts(NullNode.getInstance())));
 	}
 
 	@Test
 	public void testNotInvertsOutputFalse() {
-		Tester impl = JsonNode::isNull;
-		assertThat(impl, not(accepts(BooleanNode.TRUE)));
-		assertThat(new NotTester(impl), accepts(BooleanNode.TRUE));
+		assertThat(nullTester, not(accepts(BooleanNode.TRUE)));
+		assertThat(notNullTester, accepts(BooleanNode.TRUE));
+	}
+	
+	@Test
+	public void testValidateDoesNothingOnSuccess() {
+		notNullTester.validate(BooleanNode.TRUE);
+	}
+	
+	@Test(expected=ValidationException.class)
+	public void testThrowsOnValidationFailure() {
+		notNullTester.validate(NullNode.getInstance());
+	}
+	
+	@Test
+	public void testValidationPathIsNot() {
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/not"));
+		notNullTester.validate(NullNode.getInstance());
+	}
+	
+	@Test
+	public void testDoesNotContainsSubException() {
+		thrown.expect(ValidationException.class);
+		thrown.expect(causedBy(iterableWithSize(0)));
+		notNullTester.validate(NullNode.getInstance());
 	}
 }

+ 71 - 4
src/test/java/org/leumasjaffe/json/schema/tester/OneOfTesterTest.java

@@ -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);
+	}
+	
 }