Browse Source

Expand testing for error states.

Sam Jaffe 6 years ago
parent
commit
bebc70b83a

+ 3 - 1
src/main/lombok/org/leumasjaffe/json/schema/factory/SchemaFactory.java

@@ -68,7 +68,9 @@ public class SchemaFactory {
 	}
 
 	protected final List<Tester> createArray(final JsonNode array) {
-		assert(array.isArray() && array.size() >= 1);
+		if (!array.isArray() || array.size() == 0) {
+			throw new IllegalArgumentException("Required an array with at least one element");
+		}
 		return JsonHelper.toArray(array, this::create);
 	}
 	

+ 78 - 0
src/test/java/org/leumasjaffe/json/schema/factory/SchemaV6FactoryTest.java

@@ -43,6 +43,11 @@ public class SchemaV6FactoryTest {
 		final Map.Entry<String, JsonNode> pair = schema.fields().next();
 		return factory.createMapping(pair.getKey(), pair.getValue());
 	}
+	
+	@Test(expected=IllegalArgumentException.class)
+	public void testRejectsGarbage() {
+		fromSingleElement("{ \"pants\": true }");
+	}
 
 	@Test
 	public void testIDSchema() {
@@ -337,4 +342,77 @@ public class SchemaV6FactoryTest {
 		assertTrue(sing.accepts(BooleanNode.TRUE));
 		assertFalse(sing.accepts(new DoubleNode(1.5)));
 	}
+	
+	@Test(expected=IllegalArgumentException.class)
+	public void testAllOfSchemaRequiresArray() {
+		fromSingleElement("{ \"allOf\": true }");
+	}
+	
+	@Test(expected=IllegalArgumentException.class)
+	public void testAllOfSchemaRequiresNonEmptyArray() {
+		fromSingleElement("{ \"allOf\": [] }");
+	}
+	
+	@Test
+	public void testAllOfSchemaPassesNonEmptyArray() {
+		try {
+			fromSingleElement("{ \"allOf\": [ false ] }");
+		} catch (IllegalArgumentException ex) {
+			fail("Couldn't parse");
+		}
+	}
+	
+	@Test(expected=IllegalArgumentException.class)
+	public void testAnyOfSchemaRequiresArray() {
+		fromSingleElement("{ \"anyOf\": true }");
+	}
+	
+	@Test(expected=IllegalArgumentException.class)
+	public void testAnyOfSchemaRequiresNonEmptyArray() {
+		fromSingleElement("{ \"anyOf\": [] }");
+	}
+	
+	@Test
+	public void testAnyOfSchemaPassesNonEmptyArray() {
+		try {
+			fromSingleElement("{ \"anyOf\": [ false ] }");
+		} catch (IllegalArgumentException ex) {
+			fail("Couldn't parse");
+		}
+	}
+	
+	@Test(expected=IllegalArgumentException.class)
+	public void testOneOfSchemaRequiresArray() {
+		fromSingleElement("{ \"oneOf\": true }");
+	}
+	
+	@Test(expected=IllegalArgumentException.class)
+	public void testOneOfSchemaRequiresNonEmptyArray() {
+		fromSingleElement("{ \"oneOf\": [] }");
+	}
+	
+	@Test
+	public void testOneOfSchemaPassesNonEmptyArray() {
+		try {
+			fromSingleElement("{ \"oneOf\": [ false ] }");
+		} catch (IllegalArgumentException ex) {
+			fail("Couldn't parse");
+		}
+	}
+	
+	@Test(expected=IllegalStateException.class)
+	public void testNotSchemaRequiresSubSchema() {
+		fromSingleElement("{ \"not\": [] }");
+	}
+	
+	@Test
+	public void testNotSchemaPassesBooleanAndObject() {
+		try {
+			fromSingleElement("{ \"not\": {} }");
+			fromSingleElement("{ \"not\": false }");
+		} catch (IllegalArgumentException ex) {
+			fail("Couldn't parse");
+		}
+	}
+	
 }