Browse Source

Fixing typo with schema. Adding tests for schema mapping.

Sam Jaffe 6 years ago
parent
commit
a87cd6eedf

+ 5 - 2
src/main/lombok/org/leumasjaffe/json/schema/factory/SchemaFactory.java

@@ -2,6 +2,7 @@ package org.leumasjaffe.json.schema.factory;
 
 import java.util.List;
 import java.util.function.Predicate;
+import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import org.leumasjaffe.json.JsonHelper;
@@ -36,7 +37,7 @@ public class SchemaFactory {
 		case BOOLEAN:
 			return new Schema(object.asBoolean() ? FixedTester.ACCEPT : FixedTester.REJECT);
 		case OBJECT:
-			final SchemaFactory versioned = getVersionFactory(object.path("$ref").asText());
+			final SchemaFactory versioned = getVersionFactory(object.path("$schema").asText());
 			return new Schema(JsonHelper.fields(object, versioned::createMapping));
 		default:
 			throw new IllegalStateException("Expected OBJECT or BOOLEAN, got " + object.getNodeType());
@@ -61,7 +62,9 @@ public class SchemaFactory {
 
 	private static int getVersionInt(final String version) {
 		final Pattern pat = Pattern.compile("http://json-schema.org/draft-(\\d+)/schema#");
-		return Integer.parseInt(pat.matcher(version).group(1), 10);
+		final Matcher m = pat.matcher(version);
+		m.matches();
+		return Integer.parseInt(m.group(1), 10);
 	}
 
 	protected final List<Tester> createArray(final JsonNode array) {

+ 2 - 0
src/test/java/org/leumasjaffe/json/schema/JsonFactorySuite.java

@@ -2,10 +2,12 @@ package org.leumasjaffe.json.schema;
 
 import org.junit.runner.RunWith;
 import org.junit.runners.Suite;
+import org.leumasjaffe.json.schema.factory.SchemaFactoryTest;
 import org.leumasjaffe.json.schema.factory.SchemaV6FactoryTest;
 
 @RunWith(Suite.class)
 @Suite.SuiteClasses({
+	SchemaFactoryTest.class,
 	SchemaV6FactoryTest.class
 })
 public class JsonFactorySuite {

+ 50 - 0
src/test/java/org/leumasjaffe/json/schema/factory/SchemaFactoryTest.java

@@ -0,0 +1,50 @@
+package org.leumasjaffe.json.schema.factory;
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public class SchemaFactoryTest {
+	SchemaFactory factory;
+	ObjectMapper mapper;
+	
+	@Before
+	public void setUp() {
+		factory = new SchemaFactory();
+		mapper = new ObjectMapper();
+	}
+	
+	private JsonNode readTree(String data) {
+		try {
+			return mapper.readTree(data);
+		} catch (IOException e) {
+			throw new RuntimeException(e);
+		}
+	}
+	
+	@Test(expected=UnsupportedOperationException.class)
+	public void testFailsIfNoSchemaMember() {
+		factory.create(readTree("{ \"$id\": \"unit-test-schema\" }"));
+	}
+
+	@Test(expected=IllegalArgumentException.class)
+	public void testFailsIfIllegalSchemaVersionMember() {
+		factory.create(readTree("{ \"$id\": \"unit-test-schema\", \"$schema\":\"http://json-schema.org/draft-00/schema#\" }"));
+	}
+	
+	@Test
+	public void testCanRouteV6Schemas() {
+		try {
+			factory.create(readTree("{ \"$id\": \"unit-test-schema\", \"$schema\":\"http://json-schema.org/draft-06/schema#\" }"));
+		} catch (Throwable t) {
+			t.printStackTrace();
+			fail(t.getMessage());
+		}
+	}
+}