001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.schematic.internal.schema; 017 018import java.io.InputStream; 019import org.junit.Before; 020import org.junit.BeforeClass; 021import org.junit.Test; 022import org.modeshape.schematic.DocumentLibrary; 023import org.modeshape.schematic.SchemaLibrary; 024import org.modeshape.schematic.document.Document; 025import org.modeshape.schematic.document.Json; 026import org.modeshape.schematic.internal.InMemoryDocumentLibrary; 027import org.modeshape.schematic.internal.document.Paths; 028 029public class SchemaValidationTest { 030 031 private static DocumentLibrary docs; 032 private SchemaDocumentCache schemaDocs; 033 private SchemaLibrary.Results results; 034 035 @BeforeClass 036 public static void beforeAll() throws Exception { 037 docs = new InMemoryDocumentLibrary("tests"); 038 docs.put("http://json-schema.org/draft-03/schema#", Json.read(resource("json/schema/draft-03/schema.json"))); 039 docs.put("json/spec-example-doc.json", Json.read(resource("json/spec-example-doc.json"))); 040 docs.put("json/schema/spec-example.json", Json.read(resource("json/schema/spec-example.json"))); 041 docs.put("json/sample-repo-config.json", Json.read(resource("json/sample-repo-config.json"))); 042 docs.put("json/empty.json", Json.read(resource("json/empty.json"))); 043 docs.put("json/schema/repository-config-schema.json", Json.read(resource("json/schema/repository-config-schema.json"))); 044 docs.put("json/schema/enum-example.json", Json.read(resource("json/schema/enum-example.json"))); 045 docs.put("json/enum-example-doc.json", Json.read(resource("json/enum-example-doc.json"))); 046 } 047 048 protected static InputStream resource( String resourcePath ) { 049 InputStream result = SchemaValidationTest.class.getClassLoader().getResourceAsStream(resourcePath); 050 assert result != null : "Could not find resource \"" + resourcePath + "\""; 051 return result; 052 } 053 054 @Before 055 public void beforeEach() { 056 schemaDocs = new SchemaDocumentCache(docs, null); 057 } 058 059 protected void assertNoProblems( SchemaLibrary.Results results ) { 060 if (results.hasProblems()) { 061 System.out.println(results); 062 assert false; 063 } 064 } 065 066 protected SchemaLibrary.Results validate( String documentUri, 067 String schemaUri ) { 068 Document doc = docs.get(documentUri); 069 ValidationResult problems = new ValidationResult(); 070 SchemaDocument schema = schemaDocs.get(schemaUri, problems); 071 if (schema != null) { 072 schema.getValidator().validate(null, null, doc, Paths.rootPath(), problems, schemaDocs); 073 } 074 return problems; 075 } 076 077 @Test 078 public void shouldBeAbleToValidateDocumentUsingSimpleSchemaDocument() throws Exception { 079 results = validate("json/spec-example-doc.json", "json/schema/spec-example.json"); 080 assertNoProblems(results); 081 } 082 083 @Test 084 public void shouldBeAbleToValidateSampleDocumentUsingSchemaDocument() throws Exception { 085 results = validate("json/sample-repo-config.json", "json/schema/repository-config-schema.json"); 086 assertNoProblems(results); 087 } 088 089 @Test 090 public void shouldBeAbleToValidateDocumentUsingCaseInsensitiveEnums() throws Exception { 091 results = validate("json/enum-example-doc.json", "json/schema/enum-example.json"); 092 assertNoProblems(results); 093 } 094 095 @Test 096 public void shouldNotBeAbleToValidateEmptyDocumentUsingSchemaDocument() throws Exception { 097 results = validate("json/empty.json", "json/schema/repository-config-schema.json"); 098 assert results.errorCount() == 1; 099 } 100}