Ver código fonte

Add tests for all Tester instances that provide self-level validate() (as opposed to compound level e.g. NotTester, AllOfTester).

Sam Jaffe 6 anos atrás
pai
commit
29bb5b9b6b

+ 46 - 3
src/test/java/org/leumasjaffe/json/schema/tester/PropertyNameTesterTest.java

@@ -2,28 +2,71 @@ package org.leumasjaffe.json.schema.tester;
 
 import static org.junit.Assert.assertThat;
 import static org.leumasjaffe.json.schema.matcher.Accepts.accepts;
+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.ValidationException;
 
 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
 import com.fasterxml.jackson.databind.node.NullNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 
 public class PropertyNameTesterTest {
+    @Rule public ExpectedException thrown = ExpectedException.none();
+    
+	private PropertyNameTester singleChar;
+	
+	@Before
+	public void setUp() {
+		this.singleChar = new PropertyNameTester(j -> j.asText().length() == 1);
+	}
 
 	@Test
 	public void testPassesEmptyObject() {
-		PropertyNameTester test = new PropertyNameTester(FixedTester.REJECT);
 		final ObjectNode node = new ObjectNode(JsonNodeFactory.instance);
-		assertThat(test, accepts(node));
+		assertThat(singleChar, accepts(node));
 	}
 	
 	@Test
 	public void testRejectsIfNameFails() {
+		final ObjectNode node = new ObjectNode(JsonNodeFactory.instance);
+		node.set("AB", NullNode.getInstance());
+		assertThat(singleChar, not(accepts(node)));
+	}
+	
+	@Test
+	public void testPassesIfNameIsGood() {
+		final ObjectNode node = new ObjectNode(JsonNodeFactory.instance);
+		node.set("A", NullNode.getInstance());
+		assertThat(singleChar, accepts(node));
+	}
+	
+	@Test
+	public void testValidateDoesNothingOnSuccess() {
+		final ObjectNode node = new ObjectNode(JsonNodeFactory.instance);
+		node.set("A", NullNode.getInstance());
+		singleChar.validate(node);
+	}
+	
+	@Test(expected=ValidationException.class)
+	public void testThrowsOnValidationFailure() {
+		final ObjectNode node = new ObjectNode(JsonNodeFactory.instance);
+		node.set("AB", NullNode.getInstance());
+		singleChar.validate(node);
+	}
+	
+	@Test
+	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());
-		assertThat(test, not(accepts(node)));
+		test.validate(node);
 	}
+
 }

+ 66 - 0
src/test/java/org/leumasjaffe/json/schema/tester/SizeTesterTest.java

@@ -2,9 +2,13 @@ package org.leumasjaffe.json.schema.tester;
 
 import static org.junit.Assert.assertThat;
 import static org.leumasjaffe.json.schema.matcher.Accepts.accepts;
+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.ValidationException;
 import org.leumasjaffe.json.schema.tester.SizeTester;
 
 import com.fasterxml.jackson.databind.JsonNode;
@@ -15,6 +19,8 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.fasterxml.jackson.databind.node.TextNode;
 
 public class SizeTesterTest {
+    @Rule public ExpectedException thrown = ExpectedException.none();
+
 	@Test
 	public void arrayMatcherRejectsObject() {
 		final SizeTester notEmptyArray = SizeTester.minItems(0);
@@ -78,4 +84,64 @@ public class SizeTesterTest {
 		assertThat(notEmptyArray, accepts(new TextNode("_")));
 	}
 	
+	@Test
+	public void testValidateDoesNothingOnSuccess() {
+		SizeTester.minLength(1).validate(new TextNode("_"));
+	}
+	
+	@Test(expected=ValidationException.class)
+	public void testThrowsOnValidationFailure() {
+		SizeTester.minLength(1).validate(new TextNode(""));
+	}
+	
+	@Test
+	public void testValidationPathIsMinProperties() {
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/minProperties"));
+		final ObjectNode node = new ObjectNode(JsonNodeFactory.instance);
+		SizeTester.minProperties(1).validate(node);
+	}
+	
+	@Test
+	public void testValidationPathIsMaxProperties() {
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/maxProperties"));
+		final ObjectNode node = new ObjectNode(JsonNodeFactory.instance);
+		node.set("A", NullNode.getInstance());
+		node.set("B", NullNode.getInstance());
+		SizeTester.maxProperties(1).validate(node);
+	}
+	
+	@Test
+	public void testValidationPathIsMinItems() {
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/minItems"));
+		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
+		SizeTester.minItems(1).validate(node);
+	}
+	
+	@Test
+	public void testValidationPathIsMaxItems() {
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/maxItems"));
+		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
+		node.add(NullNode.getInstance());
+		node.add(NullNode.getInstance());
+		SizeTester.maxItems(1).validate(node);
+	}
+	
+	@Test
+	public void testValidationPathIsMinLength() {
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/minLength"));
+		SizeTester.minLength(1).validate(new TextNode(""));
+	}
+	
+	@Test
+	public void testValidationPathIsMaxLength() {
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/maxLength"));
+		SizeTester.maxLength(1).validate(new TextNode("Hello World"));
+	}
+	
 }

+ 45 - 3
src/test/java/org/leumasjaffe/json/schema/tester/UniqueItemTesterTest.java

@@ -2,33 +2,75 @@ package org.leumasjaffe.json.schema.tester;
 
 import static org.junit.Assert.assertThat;
 import static org.leumasjaffe.json.schema.matcher.Accepts.accepts;
+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.ValidationException;
 
 import com.fasterxml.jackson.databind.node.ArrayNode;
 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 
 public class UniqueItemTesterTest {
+    @Rule public ExpectedException thrown = ExpectedException.none();
+	private UniqueItemTester unique;
+	private ArrayNode arrayRepeated, arrayUnique;
+	
+	@Before
+	public void setUp() {
+		this.unique = new UniqueItemTester();
+		this.arrayRepeated = new ArrayNode(JsonNodeFactory.instance);
+		this.arrayRepeated.add(1.5);
+		this.arrayRepeated.add(1.5);
+		this.arrayUnique = new ArrayNode(JsonNodeFactory.instance);
+		this.arrayUnique.add(1.5);
+		this.arrayUnique.add(1.0);
+	}
 
 	@Test
 	public void testAcceptsEmptyArray() {
 		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
-		assertThat(new UniqueItemTester(), accepts(node));
+		assertThat(unique, accepts(node));
 	}
 
 	@Test
 	public void testRejectsNonArray() {
 		final ObjectNode node = new ObjectNode(JsonNodeFactory.instance);
-		assertThat(new UniqueItemTester(), not(accepts(node)));
+		assertThat(unique, not(accepts(node)));
 	}
 	
 	@Test
 	public void testRejectsArrayWithDuplicates() {
+		assertThat(unique, not(accepts(arrayRepeated)));
+	}
+	
+	@Test
+	public void testPassesArrayWithoutDuplicates() {
+		assertThat(unique, accepts(arrayUnique));
+	}
+	
+	@Test
+	public void testValidateDoesNothingOnSuccess() {
+		unique.validate(arrayUnique);
+	}
+	
+	@Test(expected=ValidationException.class)
+	public void testThrowsOnValidationFailure() {
 		final ArrayNode node = new ArrayNode(JsonNodeFactory.instance);
 		node.add(1.5);
 		node.add(1.5);
-		assertThat(new UniqueItemTester(), not(accepts(node)));
+		unique.validate(arrayRepeated);
 	}
+	
+	@Test
+	public void testValidationPathIsUniqueItems() {
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/uniqueItems"));
+		unique.validate(arrayRepeated);
+	}
+	
 }