1212import com .networknt .schema .serialization .JsonMapperFactory ;
1313
1414/**
15- * Sample test.
15+ * Quick start test.
1616 */
17- class SampleTest {
17+ class QuickStartTest {
1818 @ Test
1919 void schemaFromSchemaLocationMapping () {
20- SchemaRegistry factory = SchemaRegistry .withDefaultDialect (SpecificationVersion .DRAFT_2020_12 , builder -> builder .schemaIdResolvers (
21- schemaIdResolvers -> schemaIdResolvers .mapPrefix ("https://www.example.com/schema" , "classpath:schema" )));
20+ SchemaRegistry schemaRegistry = SchemaRegistry .withDefaultDialect (SpecificationVersion .DRAFT_2020_12 ,
21+ builder -> builder .schemaIdResolvers (schemaIdResolvers -> schemaIdResolvers
22+ .mapPrefix ("https://www.example.com/schema" , "classpath:schema" )));
2223 /*
2324 * This should be cached for performance.
2425 */
25- Schema schemaFromSchemaLocation = factory
26+ Schema schemaFromSchemaLocation = schemaRegistry
2627 .getSchema (SchemaLocation .of ("https://www.example.com/schema/example-ref.json" ));
2728 /*
2829 * By default all schemas are preloaded eagerly but ref resolve failures are not
@@ -31,21 +32,22 @@ void schemaFromSchemaLocationMapping() {
3132 */
3233 schemaFromSchemaLocation .initializeValidators ();
3334 List <Error > errors = schemaFromSchemaLocation .validate ("{\" id\" : \" 2\" }" , InputFormat .JSON ,
34- executionContext -> executionContext .executionConfig (executionConfig -> executionConfig .formatAssertionsEnabled (true )));
35+ executionContext -> executionContext
36+ .executionConfig (executionConfig -> executionConfig .formatAssertionsEnabled (true )));
3537 assertEquals (1 , errors .size ());
3638 }
3739
3840 @ Test
3941 void schemaFromSchemaLocationContent () {
4042 String schemaData = "{\" enum\" :[1, 2, 3, 4]}" ;
41-
42- SchemaRegistry factory = SchemaRegistry .withDefaultDialect (SpecificationVersion .DRAFT_2020_12 ,
43- builder -> builder .resourceLoaders ( resourceLoaders -> resourceLoaders . resources (
44- Collections .singletonMap ("https://www.example.com/schema/example-ref.json" , schemaData )))) ;
43+
44+ SchemaRegistry schemaRegistry = SchemaRegistry .withDefaultDialect (SpecificationVersion .DRAFT_2020_12 ,
45+ builder -> builder .schemas (
46+ Collections .singletonMap ("https://www.example.com/schema/example-ref.json" , schemaData )));
4547 /*
4648 * This should be cached for performance.
4749 */
48- Schema schemaFromSchemaLocation = factory
50+ Schema schemaFromSchemaLocation = schemaRegistry
4951 .getSchema (SchemaLocation .of ("https://www.example.com/schema/example-ref.json" ));
5052 /*
5153 * By default all schemas are preloaded eagerly but ref resolve failures are not
@@ -54,50 +56,51 @@ void schemaFromSchemaLocationContent() {
5456 */
5557 schemaFromSchemaLocation .initializeValidators ();
5658 List <Error > errors = schemaFromSchemaLocation .validate ("{\" id\" : \" 2\" }" , InputFormat .JSON ,
57- executionContext -> executionContext .executionConfig (executionConfig -> executionConfig .formatAssertionsEnabled (true )));
59+ executionContext -> executionContext
60+ .executionConfig (executionConfig -> executionConfig .formatAssertionsEnabled (true )));
5861 assertEquals (1 , errors .size ());
5962 }
6063
6164 @ Test
6265 void schemaFromClasspath () {
63- SchemaRegistry factory = SchemaRegistry .withDefaultDialect (SpecificationVersion .DRAFT_2020_12 );
66+ SchemaRegistry schemaRegistry = SchemaRegistry .withDefaultDialect (SpecificationVersion .DRAFT_2020_12 );
6467 /*
6568 * This should be cached for performance.
6669 *
6770 * Loading from using the retrieval IRI is not recommended as it may cause
6871 * confusing when resolving relative $ref when $id is also used.
6972 */
70- Schema schemaFromClasspath = factory .getSchema (SchemaLocation .of ("classpath:schema/example-ref.json" ));
73+ Schema schemaFromClasspath = schemaRegistry .getSchema (SchemaLocation .of ("classpath:schema/example-ref.json" ));
7174 /*
7275 * By default all schemas are preloaded eagerly but ref resolve failures are not
7376 * thrown. You check if there are issues with ref resolving using
7477 * initializeValidators()
7578 */
7679 schemaFromClasspath .initializeValidators ();
7780 List <Error > errors = schemaFromClasspath .validate ("{\" id\" : \" 2\" }" , InputFormat .JSON ,
78- executionContext -> executionContext .executionConfig (executionConfig -> executionConfig .formatAssertionsEnabled (true )));
81+ executionContext -> executionContext
82+ .executionConfig (executionConfig -> executionConfig .formatAssertionsEnabled (true )));
7983 assertEquals (1 , errors .size ());
8084 }
8185
8286 @ Test
8387 void schemaFromString () {
84- SchemaRegistry factory = SchemaRegistry .withDefaultDialect (SpecificationVersion .DRAFT_2020_12 );
88+ SchemaRegistry schemaRegistry = SchemaRegistry .withDefaultDialect (SpecificationVersion .DRAFT_2020_12 );
8589 /*
8690 * This should be cached for performance.
8791 *
8892 * Loading from a String is not recommended as there is no base IRI to use for
8993 * resolving relative $ref.
9094 */
91- Schema schemaFromString = factory
92- .getSchema ("{\" enum\" :[1, 2, 3, 4]}" );
93- List <Error > errors = schemaFromString .validate ("7" , InputFormat .JSON ,
94- executionContext -> executionContext .executionConfig (executionConfig -> executionConfig .formatAssertionsEnabled (true )));
95+ Schema schemaFromString = schemaRegistry .getSchema ("{\" enum\" :[1, 2, 3, 4]}" );
96+ List <Error > errors = schemaFromString .validate ("7" , InputFormat .JSON , executionContext -> executionContext
97+ .executionConfig (executionConfig -> executionConfig .formatAssertionsEnabled (true )));
9598 assertEquals (1 , errors .size ());
9699 }
97100
98101 @ Test
99102 void schemaFromJsonNode () throws JsonProcessingException {
100- SchemaRegistry factory = SchemaRegistry .withDefaultDialect (SpecificationVersion .DRAFT_2020_12 );
103+ SchemaRegistry schemaRegistry = SchemaRegistry .withDefaultDialect (SpecificationVersion .DRAFT_2020_12 );
101104 JsonNode schemaNode = JsonMapperFactory .getInstance ().readTree (
102105 "{\" $schema\" : \" http://json-schema.org/draft-06/schema#\" , \" properties\" : { \" id\" : {\" type\" : \" number\" }}}" );
103106 /*
@@ -106,18 +109,19 @@ void schemaFromJsonNode() throws JsonProcessingException {
106109 * Loading from a JsonNode is not recommended as there is no base IRI to use for
107110 * resolving relative $ref.
108111 *
109- * Note that the V202012 from the factory is the default version if $schema is not
110- * specified. As $schema is specified in the data, V6 is used.
112+ * Note that the V202012 from the schemaRegistry is the default version if $schema is
113+ * not specified. As $schema is specified in the data, V6 is used.
111114 */
112- Schema schemaFromNode = factory .getSchema (schemaNode );
115+ Schema schemaFromNode = schemaRegistry .getSchema (schemaNode );
113116 /*
114117 * By default all schemas are preloaded eagerly but ref resolve failures are not
115118 * thrown. You check if there are issues with ref resolving using
116119 * initializeValidators()
117120 */
118121 schemaFromNode .initializeValidators ();
119122 List <Error > errors = schemaFromNode .validate ("{\" id\" : \" 2\" }" , InputFormat .JSON ,
120- executionContext -> executionContext .executionConfig (executionConfig -> executionConfig .formatAssertionsEnabled (true )));
123+ executionContext -> executionContext
124+ .executionConfig (executionConfig -> executionConfig .formatAssertionsEnabled (true )));
121125 assertEquals (1 , errors .size ());
122126 }
123127}
0 commit comments