package de.zalando.toga.validator; import com.github.fge.jsonschema.core.report.ProcessingReport; import org.junit.Test; import java.io.File; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; public class ValidatorTest { @Test public void emptyObjectTest() { File schema = new File("src/test/resources/validator/empty_object_schema.json"); validateSchema(schema, "{}"); } @Test public void simpleFailure() { File schema = new File("src/test/resources/validator/single_string_schema.json"); validateSchema(schema, "{}", false); } @Test public void simpleStringTest() { File schema = new File("src/test/resources/validator/single_string_schema.json"); validateSchema(schema, "{\"someString\":\"\"}"); validateSchema(schema, "{\"someString\":\"short\"}"); validateSchema(schema, "{\"someString\":\"a little longer with some spaces\"}"); } @Test(expected = RuntimeException.class) public void loadNonExistingSchemaShouldThrowException() { new SchemaValidator().loadSchema(new File("this/does/not/exist")); } @Test(expected = IllegalStateException.class) public void validateWithoutSchemaShouldThrowException() { new SchemaValidator().validate(""); } @Test(expected = RuntimeException.class) public void validateNullInputShouldThrowException() { File schema = new File("src/test/resources/validator/single_string_schema.json"); validateSchema(schema, null); } @Test(expected = RuntimeException.class) public void validateInvalidInputShouldThrowException() { File schema = new File("src/test/resources/validator/single_string_schema.json"); validateSchema(schema, "{"); } private void validateSchema(File schema, String input, boolean expectedResult) { SchemaValidator validator = new SchemaValidator(); validator.loadSchema(schema); ProcessingReport report = validator.validate(input); assertThat(report.isSuccess(), is(expectedResult)); } private void validateSchema(File schema, String input) { validateSchema(schema, input, true); } }