Sfoglia il codice sorgente

Make tests passing for allOf and anyOf tester validation.

Sam Jaffe 6 anni fa
parent
commit
e9a72b78d6

+ 14 - 0
src/main/lombok/org/leumasjaffe/json/schema/tester/AllOfTester.java

@@ -2,8 +2,11 @@ package org.leumasjaffe.json.schema.tester;
 
 import java.util.Arrays;
 import java.util.List;
+import java.util.stream.Collectors;
 
+import org.leumasjaffe.container.EitherStream;
 import org.leumasjaffe.json.schema.Tester;
+import org.leumasjaffe.json.schema.ValidationException;
 
 import com.fasterxml.jackson.databind.JsonNode;
 
@@ -19,6 +22,17 @@ public class AllOfTester implements Tester {
 	public AllOfTester(Tester...testers) {
 		this(Arrays.asList(testers));
 	}
+	
+	@Override
+	public void validate(JsonNode node) throws ValidationException {
+		final List<ValidationException> exceptions = EitherStream.from(children)
+				.flatMap(t -> t.validate(node), ValidationException.class)
+				.collect(Collectors.toList());
+		if (!exceptions.isEmpty()) {
+			throw new ValidationException("allOf", "JSON node failed some sub-schemas",
+					exceptions);
+		}
+	}
 
 	@Override
 	public boolean accepts(JsonNode node) {

+ 14 - 0
src/main/lombok/org/leumasjaffe/json/schema/tester/AnyOfTester.java

@@ -2,8 +2,11 @@ package org.leumasjaffe.json.schema.tester;
 
 import java.util.Arrays;
 import java.util.List;
+import java.util.stream.Collectors;
 
+import org.leumasjaffe.container.EitherStream;
 import org.leumasjaffe.json.schema.Tester;
+import org.leumasjaffe.json.schema.ValidationException;
 
 import com.fasterxml.jackson.databind.JsonNode;
 
@@ -19,6 +22,17 @@ public class AnyOfTester implements Tester {
 	public AnyOfTester(Tester...testers) {
 		this(Arrays.asList(testers));
 	}
+	
+	@Override
+	public void validate(JsonNode node) throws ValidationException {
+		final List<ValidationException> exceptions = EitherStream.from(children)
+				.flatMap(t -> t.validate(node), ValidationException.class)
+				.collect(Collectors.toList());
+		if (exceptions.size() == children.size()) {
+			throw new ValidationException("anyOf", "JSON node failed every sub-schema",
+					exceptions);
+		}
+	}
 
 	@Override
 	public boolean accepts(JsonNode node) {

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

@@ -69,16 +69,14 @@ public class AnyOfTesterTest {
 	
 	@Test(expected=ValidationException.class)
 	public void testThrowsOnValidationFailure() {
-		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
-		new AnyOfTester(isArray, notEmpty).validate(node);
+		new AnyOfTester(isArray, notEmpty).validate(NullNode.getInstance());
 	}
 	
 	@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);
+		new AnyOfTester(isArray, isObject, notEmpty).validate(NullNode.getInstance());
 	}
 	
 	@Test