Package uk.co.o2.json.schema.jaxrs

Examples of uk.co.o2.json.schema.jaxrs.ClasspathSchemaLookup


    @Test
    public void generateErrorMessage_shouldCreateATextErrorDocument() throws Exception {
        JsonSchemaProvider provider = new JsonSchemaProvider(mock(SchemaLookup.class));

        ErrorMessage errorA = new ErrorMessage("foo.bar", "errorAMessage");
        ErrorMessage errorB = new ErrorMessage("foo.baz", "errorBMessage");
        List<ErrorMessage> validationErrors = Arrays.asList(errorA, errorB);

        Response response = provider.generateErrorMessage(validationErrors);

        assertEquals(400, response.getStatus());
        assertEquals(MediaType.TEXT_PLAIN_TYPE, response.getMetadata().getFirst("Content-Type"));

        String entity = (String) response.getEntity();
        assertTrue(entity.contains(errorA.getLocation() + ": "));
        assertTrue(entity.contains(errorA.getMessage()));

        assertTrue(entity.contains(errorB.getLocation() + ": "));
        assertTrue(entity.contains(errorB.getMessage()));
    }
View Full Code Here


    private JsonFactory jsonFactory = new JsonFactory(new ObjectMapper());
    private SchemaPassThroughCache schemaFactory = new SchemaPassThroughCache(jsonFactory);

    @Test
    public void validateSchema_shouldWorkFromADifferentPackage() throws Exception {
        JsonSchema schema = schemaFactory.getSchema(getClass().getClassLoader().getResource("sample-json-schema.json"));
        JsonNode json = jsonFactory.createJsonParser(getClass().getClassLoader().getResource("valid-json-document.json")).readValueAsTree();

        List<ErrorMessage> errors = schema.validate(json);

        assertTrue(errors.isEmpty());
    }
View Full Code Here

        if (schemaAnnotation != null) {
            ObjectMapper mapper = locateMapper(type, mediaType);
            JsonParser jp = mapper.getFactory().createJsonParser(entityStream);
            jp.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);
            URL schemaLocation = schemaLookup.getSchemaURL(schemaAnnotation.value());
            JsonSchema jsonSchema = cache.getSchema(schemaLocation);
            JsonNode jsonNode = mapper.readTree(jp);
            List<ErrorMessage> validationErrors = jsonSchema.validate(jsonNode);
            if (validationErrors.isEmpty()) {
                return mapper.reader().withType(mapper.constructType(genericType)).readValue(jsonNode);
            }

            throw new WebApplicationException(generateErrorMessage(validationErrors));
View Full Code Here

        }

        for (Iterator<String> iterator = rawProperties.fieldNames(); iterator.hasNext();) {
            String fieldName = iterator.next();

            Property property = new Property();
            property.setName(fieldName);
           
            JsonNode nestedSchema = rawProperties.get(fieldName);
            property.setNestedSchema(parse(nestedSchema, schemaLocation));

            JsonNode required = nestedSchema.get("required");
            if (required != null) {
                property.setRequired(required.booleanValue());
            }
           
            schema.getProperties().add(property);
        }
    }
View Full Code Here

    private final SchemaPassThroughCache cache;
    private SchemaLookup schemaLookup;

    public JsonSchemaProvider(SchemaLookup schemaLookup) {
        cache = new SchemaPassThroughCache(new JsonFactory(new ObjectMapper()));
        this.schemaLookup = schemaLookup;
        this.configure(SerializationFeature.INDENT_OUTPUT, true);
    }
View Full Code Here

                cache.registerSchema((URL) invocation.getArguments()[0], expectedSchema);
                return expectedSchema;
            }
        });

        SchemaCompilerFactory schemaCompilerFactoryMock = mock(SchemaCompilerFactory.class);
        when(schemaCompilerFactoryMock.create()).thenReturn(schemaCompilerMock);
        cache.setSchemaCompilerFactory(schemaCompilerFactoryMock);

        JsonSchema result = cache.getSchema(expectedLocation);

        assertSame(expectedSchema, result);
View Full Code Here

TOP

Related Classes of uk.co.o2.json.schema.jaxrs.ClasspathSchemaLookup

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.