SchemaLoader { public static Schema load(final JSONObject schemaJson) { return SchemaLoader.load(schemaJson, new DefaultSchemaClient()); } SchemaLoader(SchemaLoaderBuilder builder);   SchemaLoader(LoadingState ls); static SchemaLoaderBuilder builder(); static Schema load(final JSONObject schemaJson); static Schema load(final JSONObject schemaJson, final SchemaClient schemaClient); Schema.Builder<?> load();  }
@Test public void notSchema() { NotSchema actual = (NotSchema) SchemaLoader.load(get("notSchema")); assertNotNull(actual); }
@Test public void nullSchema() { NullSchema actual = (NullSchema) SchemaLoader.load(get("nullSchema")); assertNotNull(actual); }
@Test public void pointerResolution() { ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("pointerResolution")); ObjectSchema rectangleSchema = (ObjectSchema) ((ReferenceSchema) actual.getPropertySchemas() .get("rectangle")) .getReferredSchema(); assertNotNull(rectangleSchema); ReferenceSchema aRef = (ReferenceSchema) rectangleSchema.getPropertySchemas().get("a"); assertTrue(aRef.getReferredSchema() instanceof NumberSchema); }
@Test public void pointerResolvedToBoolean() { ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("pointerResolution")); TrueSchema boolSchema = (TrueSchema) ((ReferenceSchema) actual.getPropertySchemas() .get("boolRef")) .getReferredSchema(); assertNotNull(boolSchema); }
@Test public void v6InternalRefResolution() { SchemaLoader loader = v6Loader().schemaJson(get("v6Ref")).build(); Schema actual = loader.load().build(); }
@Test public void sameDocumentReferenceResolution() { v6Loader().schemaJson(get("v6SameDocumentRef")).build().load().build(); }
@Test(expected = SchemaException.class) public void pointerResolutionFailure() { SchemaLoader.load(get("pointerResolutionFailure")); }
@Test(expected = SchemaException.class) public void pointerResolutionQueryFailure() { SchemaLoader.load(get("pointerResolutionQueryFailure")); }
@Test @Ignore public void propsAroundRefExtendTheReferredSchema() { ObjectSchema actual = (ObjectSchema) SchemaLoader .load(get("propsAroundRefExtendTheReferredSchema")); ReferenceSchema propRef = (ReferenceSchema) actual.getPropertySchemas().get("prop"); ObjectSchema prop = (ObjectSchema) propRef .getReferredSchema(); assertTrue(prop.requiresObject()); assertEquals(1, prop.getMinProperties().intValue()); }
@Test public void recursiveSchema() { SchemaLoader.load(get("recursiveSchema")); }
@Test public void refWithType() { ObjectSchema actualRoot = (ObjectSchema) SchemaLoader.load(get("refWithType")); ReferenceSchema actual = (ReferenceSchema) actualRoot.getPropertySchemas().get("prop"); ObjectSchema propSchema = (ObjectSchema) actual.getReferredSchema(); assertEquals(propSchema.getRequiredProperties(), asList("a", "b")); }
@Test public void remotePointerResulion() { SchemaClient schemaClient = mock(SchemaClient.class); when(schemaClient.get("http: when(schemaClient.get("http: when(schemaClient.get("http: asStream("{}")); SchemaLoader.load(get("remotePointerResolution"), schemaClient); }
@Test public void resolutionScopeTest() { SchemaLoader.load(get("resolutionScopeTest"), new SchemaClient() { @Override public InputStream get(final String url) { System.out.println("GET " + url); return new DefaultSchemaClient().get(url); } }); }
@Test public void selfRecursiveSchema() { SchemaLoader.load(get("selfRecursiveSchema")); }
@Test public void sniffByContainsDoesNotAffectV4() { JSONObject schema = new JSONObject(); schema.put("contains", new JSONObject()); Schema actual = SchemaLoader.load(schema); assertFalse(actual instanceof ArraySchema); }
@Test public void stringSchema() { StringSchema actual = (StringSchema) SchemaLoader.load(get("stringSchema")); assertEquals(2, actual.getMinLength().intValue()); assertEquals(3, actual.getMaxLength().intValue()); }
@Test public void stringSchemaWithFormat() { StringSchema subject = (StringSchema) SchemaLoader.load(get("stringSchemaWithFormat")); TestSupport.expectFailure(subject, "asd"); }
@Test public void tupleSchema() { ArraySchema actual = (ArraySchema) SchemaLoader.load(get("tupleSchema")); assertFalse(actual.permitsAdditionalItems()); assertNull(actual.getAllItemSchema()); assertEquals(2, actual.getItemSchemas().size()); assertEquals(BooleanSchema.INSTANCE, actual.getItemSchemas().get(0)); assertEquals(NullSchema.INSTANCE, actual.getItemSchemas().get(1)); }
@Test(expected = SchemaException.class) public void unknownSchema() { SchemaLoader.load(get("unknown")); }
@Test public void v6SchemaJsonIdIsRecognized() { SchemaClient client = mock(SchemaClient.class); ByteArrayInputStream retval = new ByteArrayInputStream("{}".getBytes()); when(client.get("http: v6Loader() .schemaJson(get("schemaWithIdV6")) .schemaClient(client) .build().load(); }
@Test public void schemaPointerIsPopulated() { JSONObject rawSchema = ResourceLoader.DEFAULT.readObj("objecttestschemas.json") .getJSONObject("objectWithSchemaDep"); ObjectSchema schema = (ObjectSchema) SchemaLoader.load(rawSchema); String actualSchemaPointer = schema.getSchemaDependencies().get("a").getSchemaLocation(); String expectedSchemaPointer = new JSONPointer(asList("dependencies", "a")).toURIFragment(); assertEquals(expectedSchemaPointer, actualSchemaPointer); }
@Test public void constLoadingV4() { Schema actual = SchemaLoader.load(get("constSchema")); assertFalse(actual instanceof ConstSchema); }
@Test public void folderNameResolution() { SchemaClient client = mock(SchemaClient.class); when(client.get("http: v6Loader().schemaJson(get("folderNameResolution")) .schemaClient(client).build().load().build(); }
@Test public void otherFolderNameResolution() { v6Loader().schemaJson(get("otherFolderNameResolution")).build().load().build(); }
@Test public void nullableBooleansAre_NotLoaded_withoutNullableSupport() { ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("nullableSupport")); Schema nullableSchema = actual.getPropertySchemas().get("isNullable"); Schema nonNullableSchema = actual.getPropertySchemas().get("nonNullable"); Schema implicitNonNullable = actual.getPropertySchemas().get("implicitNonNullable"); assertNull(nullableSchema.isNullable()); assertNull(nonNullableSchema.isNullable()); assertNull(implicitNonNullable.isNullable()); }
@Test public void unknownTypeException() { try { SchemaLoader.load(get("unknownType")); fail("did not throw exception for unknown type"); } catch (SchemaException actual) { SchemaException expected = new SchemaException("#/properties/prop", "unknown type: [integgggger]"); assertEquals(expected, actual); } }
@Test public void unknownMetaSchemaException() { try { SchemaLoader.load(get("unknownMetaSchema")); fail("did not throw exception"); } catch (SchemaException e) { assertEquals("#", e.getSchemaLocation()); } }
@Test public void syntheticAllOf() { String actual = SchemaLoader.load(get("boolAndNot")).toString(); assertThat(new JSONObject(actual), sameJsonAs(get("boolAndNot"))); }
@Test public void syntheticAllOfTrue() { JSONObject o = get("trueAndNot"); String actual = SchemaLoader.load(o).toString(); assertThat(new JSONObject(actual), sameJsonAs(o)); }
@Test public void commonPropsGoIntoWrappingAllOf() { CombinedSchema actual = (CombinedSchema) SchemaLoader.load(get("syntheticAllOfWithCommonProps")); assertEquals(CombinedSchema.ALL_CRITERION, actual.getCriterion()); assertEquals("http: assertEquals("my title", actual.getTitle()); assertEquals("my description", actual.getDescription()); assertNull(actual.getSubschemas().iterator().next().getId()); }
@Test public void commonPropsAreNotUnprocessedProps() { JSONObject schemaJson = get("schemaWithUnprocessedProperties"); ObjectSchema subject = (ObjectSchema) SchemaLoader.load(schemaJson); assertThat(new JSONObject(subject.toString()), sameJsonAs(schemaJson)); }
@Test public void httpsSchemaURI() { JSONObject schemaJson = ResourceLoader.DEFAULT.readObj("https-schema-uri.json"); Schema schema = SchemaLoader.load(schemaJson); assertNotNull(schema.getSchemaLocation()); }
@Test public void booleanSchema() { BooleanSchema actual = (BooleanSchema) SchemaLoader.load(get("booleanSchema")); assertNotNull(actual); }
@Test public void emptyPatternProperties() { ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("emptyPatternProperties")); assertNotNull(actual); assertEquals(0, actual.getPatternProperties().size()); }
@Test public void emptySchema() { assertTrue(SchemaLoader.load(get("emptySchema")) instanceof EmptySchema); }
@Test public void emptySchemaWithDefault() { EmptySchema actual = (EmptySchema) SchemaLoader.load(get("emptySchemaWithDefault")); assertNotNull(actual); }
@Test public void enumSchema() { EnumSchema actual = (EnumSchema) SchemaLoader.load(get("enumSchema")); Map<String, Object> expectedObject = new HashMap<>(); expectedObject.put("a", "b"); assertEquals(new HashSet<>(asList(1, 2, "a", expectedObject, null)), new HashSet<>(actual.getPossibleValues())); }
@Test public void genericProperties() { Schema actual = SchemaLoader.load(get("genericProperties")); assertEquals("myId", actual.getId()); assertEquals("my title", actual.getTitle()); assertEquals("my description", actual.getDescription()); }
@Test public void integerSchema() { NumberSchema actual = (NumberSchema) SchemaLoader.load(get("integerSchema")); assertEquals(10, actual.getMinimum().intValue()); assertEquals(20, actual.getMaximum().intValue()); assertEquals(5, actual.getMultipleOf().intValue()); assertTrue(actual.isExclusiveMinimum()); assertTrue(actual.isExclusiveMaximum()); assertTrue(actual.requiresInteger()); }
@Test public void conditionalSchemaLoadingV4() { Schema actual = SchemaLoader.load(get("conditionalSchemaIf")); assertFalse(actual instanceof ConditionalSchema); }
@Test(expected = SchemaException.class) public void invalidExclusiveMinimum() { SchemaLoader.load(get("invalidExclusiveMinimum")); }
@Test(expected = SchemaException.class) public void invalidIntegerSchema() { JSONObject input = get("invalidIntegerSchema"); SchemaLoader.load(input); }
@Test(expected = SchemaException.class) public void invalidStringSchema() { SchemaLoader.load(get("invalidStringSchema")); }
@Test(expected = SchemaException.class) public void invalidType() { SchemaLoader.load(get("invalidType")); }
@Test public void jsonPointerInArray() { assertTrue(SchemaLoader.load(get("jsonPointerInArray")) instanceof ArraySchema); }
@Test public void multipleTypes() { assertTrue(SchemaLoader.load(get("multipleTypes")) instanceof CombinedSchema); }
@Test public void implicitAnyOfLoadsTypeProps() { CombinedSchema schema = (CombinedSchema) SchemaLoader.load(get("multipleTypesWithProps")); StringSchema stringSchema = schema.getSubschemas().stream() .filter(sub -> sub instanceof StringSchema) .map(sub -> (StringSchema) sub) .findFirst().orElseThrow(() -> new AssertionError("no StringSchema")); NumberSchema numSchema = schema.getSubschemas().stream() .filter(sub -> sub instanceof NumberSchema) .map(sub -> (NumberSchema) sub) .findFirst() .orElseThrow(() -> new AssertionError("no NumberSchema")); assertEquals(3, stringSchema.getMinLength().intValue()); assertEquals(5, numSchema.getMinimum().intValue()); }
@Test public void neverMatchingAnyOf() { assertTrue(SchemaLoader.load(get("anyOfNeverMatches")) instanceof CombinedSchema); }
@Test public void noExplicitObject() { ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("noExplicitObject")); assertFalse(actual.requiresObject()); }