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 static org.hamcrest.core.Is.is;
019import static org.hamcrest.core.IsNot.not;
020import static org.hamcrest.core.IsSame.sameInstance;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertThat;
023import java.io.InputStream;
024import org.junit.Before;
025import org.junit.BeforeClass;
026import org.junit.Test;
027import org.modeshape.schematic.SchemaLibrary;
028import org.modeshape.schematic.Schematic;
029import org.modeshape.schematic.document.Document;
030import org.modeshape.schematic.document.Json;
031import org.modeshape.schematic.document.ParsingException;
032
033public class DocumentTransformerTest {
034
035    protected static final String PARTS_SCHEMA_URI = "json/schema/spec-example-doc.json";
036
037    private static SchemaLibrary schemas;
038    private static boolean print;
039
040    @BeforeClass
041    public static void beforeAll() throws Exception {
042        schemas = Schematic.createSchemaLibrary();
043        schemas.put("http://json-schema.org/draft-03/schema#", Json.read(resource("json/schema/draft-03/schema.json")));
044        schemas.put(PARTS_SCHEMA_URI, Json.read(resource("json/schema/spec-example.json")));
045        // schemas.put("json/schema/repository-config-schema.json",
046        // Json.read(resource("json/schema/repository-config-schema.json")));
047    }
048
049    @Before
050    public void beforeEach() {
051        print = false;
052    }
053
054    @Test
055    public void shouldNotTransformDocumentWithNoMismatchedValues() throws Exception {
056        Document doc = doc("{ 'name' : 'Acme Bottle Opener', 'id' : 123 , 'price' : 2.99, 'tags' : [ 'easy', 'under-10-dollars' ] }");
057        transform(doc, PARTS_SCHEMA_URI, 0);
058    }
059
060    @Test
061    public void shouldTransformDocumentWithStringValueWhereIntegerExpected() throws Exception {
062        // print = true;
063        Document doc = doc("{ 'name' : 'Acme Bottle Opener', 'id' : '123' , 'price' : 2.99, 'tags' : [ 'easy', 'under-10-dollars' ] }");
064        transform(doc, PARTS_SCHEMA_URI, 1);
065    }
066
067    protected static InputStream resource( String resourcePath ) {
068        InputStream result = SchemaValidationTest.class.getClassLoader().getResourceAsStream(resourcePath);
069        assertNotNull("Could not find resource \"" + resourcePath + "\"", result);
070        return result;
071    }
072
073    protected static Document doc( String content ) throws ParsingException {
074        Document doc = Json.read(content);
075        if (print) System.out.println(doc);
076        return doc;
077    }
078
079    protected static Document transform( Document doc,
080                                         String schemaUri,
081                                         int numExpectedMismatchedValues ) {
082        SchemaLibrary.Results results = schemas.validate(doc, schemaUri);
083        if (print) System.out.println(results);
084        if (numExpectedMismatchedValues > 0) {
085            assertThat("expected mismatch errors, but found none", results.hasOnlyTypeMismatchErrors(), is(true));
086            assertThat("expected different number of mismatches", results.errorCount(), is(numExpectedMismatchedValues));
087        } else {
088            assertThat("expected no mismatch errors", results.hasOnlyTypeMismatchErrors(), is(false));
089            assertThat("expected to find problems", results.hasProblems(), is(false));
090        }
091        Document output = schemas.convertValues(doc, results);
092        if (numExpectedMismatchedValues > 0) {
093            assertThat(output, is(not(sameInstance(doc))));
094            // Now double check that the output is valid ...
095            SchemaLibrary.Results newResults = schemas.validate(output, schemaUri);
096            assertThat(newResults.hasErrors(), is(false));
097            if (print) {
098                System.out.println("After converting: " + output);
099                System.out.println(newResults);
100            }
101        } else {
102            assertThat(output, is(sameInstance(doc)));
103        }
104        return output;
105    }
106
107}