Sfoglia il codice sorgente

Make propertyNames pass validate() unit tests.

Sam Jaffe 6 anni fa
parent
commit
22a6517229

+ 15 - 5
src/main/lombok/org/leumasjaffe/json/schema/ValidationException.java

@@ -18,28 +18,38 @@ public class ValidationException extends IllegalArgumentException {
 	@Getter List<ValidationException> causingExceptions; 
 	
 	private ValidationException(Either<String, Integer> path, String message,
-			ValidationException... causes) {
+			List<ValidationException> causes) {
 		super(message);
 		this.jsonPath.add(path);
-		this.causingExceptions = Collections.unmodifiableList(Arrays.asList(causes));
+		this.causingExceptions = Collections.unmodifiableList(new ArrayList<>(causes));
 		this.causingExceptions.forEach(ve -> ve.jsonPath.add(0, path));
 	}
 
 	public ValidationException(String key, String message) {
-		this(Either.ofLeft(key), message);
+		this(Either.ofLeft(key), message, Collections.emptyList());
 	}
 	
 	public ValidationException(int index, String message) {
-		this(Either.ofRight(index), message);
+		this(Either.ofRight(index), message, Collections.emptyList());
 	}
 
 	public ValidationException(String key, String message,
 			ValidationException... causes) {
-		this(Either.ofLeft(key), message, causes);
+		this(Either.ofLeft(key), message, Arrays.asList(causes));
 	}
 
 	public ValidationException(int index, String message,
 			ValidationException... causes) {
+		this(Either.ofRight(index), message, Arrays.asList(causes));
+	}
+
+	public ValidationException(String key, String message,
+			List<ValidationException> causes) {
+		this(Either.ofLeft(key), message, causes);
+	}
+
+	public ValidationException(int index, String message,
+			List<ValidationException> causes) {
 		this(Either.ofRight(index), message, causes);
 	}
 

+ 20 - 0
src/main/lombok/org/leumasjaffe/json/schema/tester/PropertyNameTester.java

@@ -1,7 +1,11 @@
 package org.leumasjaffe.json.schema.tester;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import org.leumasjaffe.json.JsonHelper;
 import org.leumasjaffe.json.schema.Tester;
+import org.leumasjaffe.json.schema.ValidationException;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.node.TextNode;
@@ -14,6 +18,22 @@ import lombok.experimental.FieldDefaults;
 @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
 public class PropertyNameTester implements Tester {
 	Tester schema;
+	
+	@Override
+	public void validate(JsonNode node) throws ValidationException {
+		final List<ValidationException> exceptions = new ArrayList<>();
+		node.fieldNames().forEachRemaining(name -> {
+			try {
+				schema.validate(TextNode.valueOf(name));
+			} catch (ValidationException ve) {
+				exceptions.add(ve);
+			}
+		});
+		if (!exceptions.isEmpty()) {
+			throw new ValidationException("propertyNames", "unable to match property names",
+					exceptions);
+		}
+	}
 
 	@Override
 	public boolean accepts(JsonNode node) {

+ 1 - 1
src/main/lombok/org/leumasjaffe/json/schema/tester/UniqueItemTester.java

@@ -23,7 +23,7 @@ public class UniqueItemTester implements Tester {
 		final List<ValidationException> exceptions = new Helper(node).validate();
 		if (!exceptions.isEmpty()) {
 			throw new ValidationException("uniqueItems", "duplicate items in array",
-					exceptions.toArray(new ValidationException[0]));
+					exceptions);
 		}
 	}
 

+ 10 - 4
src/test/java/org/leumasjaffe/json/schema/matcher/JsonPath.java

@@ -1,5 +1,7 @@
 package org.leumasjaffe.json.schema.matcher;
 
+import static org.hamcrest.Matchers.equalTo;
+
 import org.hamcrest.Description;
 import org.hamcrest.Matcher;
 import org.hamcrest.TypeSafeMatcher;
@@ -7,12 +9,16 @@ import org.leumasjaffe.json.schema.ValidationException;
 
 public class JsonPath extends TypeSafeMatcher<ValidationException> {
 	public static Matcher<ValidationException> jsonPath(final String path) {
+		return new JsonPath(equalTo(path));
+	}
+
+	public static Matcher<ValidationException> jsonPath(final Matcher<String> path) {
 		return new JsonPath(path);
 	}
+
+	private final Matcher<String> path;
 	
-	private final String path;
-	
-	private JsonPath(final String path) {
+	private JsonPath(final Matcher<String> path) {
 		this.path = path;
 	}
 
@@ -28,7 +34,7 @@ public class JsonPath extends TypeSafeMatcher<ValidationException> {
 
 	@Override
 	protected boolean matchesSafely(ValidationException arg0) {
-		return arg0.getPath().equals(path);
+		return path.matches(arg0.getPath());
 	}
 
 }

+ 21 - 4
src/test/java/org/leumasjaffe/json/schema/tester/PropertyNameTesterTest.java

@@ -1,7 +1,12 @@
 package org.leumasjaffe.json.schema.tester;
 
+import static org.hamcrest.Matchers.allOf;
+import static org.hamcrest.Matchers.startsWith;
+import static org.hamcrest.collection.IsIterableWithSize.iterableWithSize;
+import static org.hamcrest.core.Every.everyItem;
 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;
 
@@ -22,7 +27,7 @@ public class PropertyNameTesterTest {
 	
 	@Before
 	public void setUp() {
-		this.singleChar = new PropertyNameTester(j -> j.asText().length() == 1);
+		this.singleChar = new PropertyNameTester(SizeTester.maxLength(1));
 	}
 
 	@Test
@@ -63,10 +68,22 @@ public class PropertyNameTesterTest {
 	public void testValidationPathIsPropertyNames() {
 		thrown.expect(ValidationException.class);
 		thrown.expect(jsonPath("#/propertyNames"));
-		PropertyNameTester test = new PropertyNameTester(FixedTester.REJECT);
 		final ObjectNode node = new ObjectNode(JsonNodeFactory.instance);
-		node.set("A", NullNode.getInstance());
-		test.validate(node);
+		node.set("AB", NullNode.getInstance());
+		singleChar.validate(node);
+	}
+	
+	@Test
+	public void testProducesSubExceptionForEachException() {
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/propertyNames"));
+		final ObjectNode node = new ObjectNode(JsonNodeFactory.instance);
+		node.set("AB", NullNode.getInstance());
+		node.set("BC", NullNode.getInstance());
+		thrown.expect(ValidationException.class);
+		thrown.expect(causedBy(allOf(iterableWithSize(2),
+				everyItem(jsonPath(startsWith("#/propertyNames/"))))));
+		singleChar.validate(node);
 	}
 
 }