use of com.linkedin.data.schema.validation.ValidationResult in project rest.li by linkedin.
the class TestValidator method testValidator.
public void testValidator(String schemaText, Object[][] input, Map<String, Class<? extends Validator>> validatorClassMap, String[] validatorCheckStrings, int tests) throws IOException, InstantiationException {
DataSchema schema = dataSchemaFromString(schemaText);
DataSchemaAnnotationValidator annotationValidator = new DataSchemaAnnotationValidator();
annotationValidator.init(schema, validatorClassMap);
if (debug)
annotationValidator.setDebugMode(true);
String annotationValidatorString = annotationValidator.toString();
if (debug)
out.println(annotationValidator);
for (String checkString : validatorCheckStrings) {
assertTrue(annotationValidatorString.contains(checkString));
}
for (Object[] row : input) {
DataMap value = (DataMap) row[0];
try {
if ((tests & SCHEMA_VALIDATOR) != 0) {
VisitedTrackingValidator visitedValidator = new VisitedTrackingValidator(annotationValidator);
ValidationOptions validationOptions = new ValidationOptions(RequiredMode.CAN_BE_ABSENT_IF_HAS_DEFAULT, CoercionMode.NORMAL);
ValidationResult result = ValidateDataAgainstSchema.validate(value.copy(), schema, validationOptions, visitedValidator);
checkValidationResult(value, result, row, visitedValidator);
}
if ((tests & OBJECT_VALIDATOR) != 0) {
VisitedTrackingValidator visitedValidator = new VisitedTrackingValidator(annotationValidator);
ValidationOptions validationOptions = new ValidationOptions();
ValidationResult result = ValidateDataAgainstSchema.validate(value.copy(), schema, validationOptions, visitedValidator);
checkValidationResult(value, result, row, visitedValidator);
}
} catch (CloneNotSupportedException e) {
throw new IllegalStateException("unexpected exception", e);
}
}
}
use of com.linkedin.data.schema.validation.ValidationResult in project rest.li by linkedin.
the class TestAnyRecordValidator method testAnyRecordValidation.
@Test
public void testAnyRecordValidation() throws IOException {
Object[][] inputs = { { // DataMap is empty
ANYRECORD_SCHEMA, "{" + "}", new AnyRecordValidator.Parameter(false, null), ResultFlag.NOT_VALID, new String[] { "expects data to be a DataMap with one entry" } }, { // DataMap has more than one entry
ANYRECORD_SCHEMA, "{" + " \"a\" : 1," + " \"b\" : 2" + "}", new AnyRecordValidator.Parameter(false, null), ResultFlag.NOT_VALID, new String[] { "expects data to be a DataMap with one entry" } }, { // no resolver, any type schema need not be valid
ANYRECORD_SCHEMA, "{" + " \"abc\" : { }\n" + "}", new AnyRecordValidator.Parameter(false, null), ResultFlag.VALID, new String[] { "INFO", "cannot obtain schema for \"abc\", no resolver" } }, { // no resolver, any type schema must be valid
ANYRECORD_SCHEMA, "{" + " \"abc\" : { }\n" + "}", new AnyRecordValidator.Parameter(true, null), ResultFlag.NOT_VALID, new String[] { "ERROR", "cannot obtain schema for \"abc\", no resolver" } }, { // no resolver but schema exists, any type schema must be valid
ANYRECORD_SCHEMA, "{" + " \"com.linkedin.Foo\" : { }\n" + "}", new AnyRecordValidator.Parameter(true, null), ResultFlag.NOT_VALID, new String[] { "ERROR", "cannot obtain schema for \"com.linkedin.Foo\", no resolver" } }, { // schema exists, any type schema must be valid
ANYRECORD_SCHEMA, "{" + " \"com.linkedin.Foo\" : { }\n" + "}", new AnyRecordValidator.Parameter(true, _resolver), ResultFlag.VALID, new String[] {} }, { // resolver cannot resolve name to schema, any type schema must be valid
ANYRECORD_SCHEMA, "{" + " \"com.linkedin.DoesNotExist\" : { }\n" + "}", new AnyRecordValidator.Parameter(true, _resolver), ResultFlag.NOT_VALID, new String[] { "ERROR", "cannot obtain schema for \"com.linkedin.DoesNotExist\" (" + RESOLVER_ERROR_MESSAGE + ")" } }, { // resolver cannot resolve name to schema, any type schema need not be valid
ANYRECORD_SCHEMA, "{" + " \"com.linkedin.DoesNotExist\" : { }\n" + "}", new AnyRecordValidator.Parameter(false, _resolver), ResultFlag.VALID, new String[] { "INFO", "cannot obtain schema for \"com.linkedin.DoesNotExist\" (" + RESOLVER_ERROR_MESSAGE + ")" } }, { // type schema is valid and any data is valid, any type schema must be valid
ANYRECORD_SCHEMA, "{" + " \"com.linkedin.Bar\" : { \"b\" : \"hello\" }\n" + "}", new AnyRecordValidator.Parameter(true, _resolver), ResultFlag.VALID, new String[] {} }, { // type schema is valid and any data is not valid, any type schema must be valid
ANYRECORD_SCHEMA, "{" + " \"com.linkedin.Bar\" : { \"b\" : 1 }\n" + "}", new AnyRecordValidator.Parameter(true, _resolver), ResultFlag.NOT_VALID, new String[] { "ERROR", "1 cannot be coerced to String" } }, { // type schema is valid and any data is not valid, any type schema must be valid
ANYRECORD_SCHEMA, "{" + " \"com.linkedin.Bar\" : { \"b\" : 1 }\n" + "}", new AnyRecordValidator.Parameter(false, _resolver), ResultFlag.NOT_VALID, new String[] { "ERROR", "1 cannot be coerced to String" } }, { // AnyRecord is field, must sure that the field is being validated
ANYRECORDCLIENT_SCHEMA, "{" + " \"required\" : {\n" + " \"com.linkedin.Bar\" : { \"b\" : 1 }\n" + " }\n" + "}", new AnyRecordValidator.Parameter(true, _resolver), ResultFlag.NOT_VALID, new String[] { "ERROR", "/required/com.linkedin.Bar/b", "1 cannot be coerced to String" } }, { // AnyRecord within AnyRecord, make sure nested AnyRecord is validated
ANYRECORDCLIENT_SCHEMA, "{" + " \"required\" : {\n" + " \"com.linkedin.data.schema.validator.AnyRecord\" : {\n" + " \"com.linkedin.Bar\" : { \"b\" : 1 }\n" + " }\n" + " }\n" + "}", new AnyRecordValidator.Parameter(true, _resolver), ResultFlag.NOT_VALID, new String[] { "ERROR", "/required/com.linkedin.data.schema.validator.AnyRecord/com.linkedin.Bar/b", "1 cannot be coerced to String" } } };
final boolean debug = false;
ValidationOptions options = new ValidationOptions();
for (Object[] row : inputs) {
int i = 0;
DataSchema schema = (DataSchema) row[i++];
Object object = TestUtil.dataMapFromString((String) row[i++]);
AnyRecordValidator.Parameter anyRecordValidatorParameter = (AnyRecordValidator.Parameter) row[i++];
AnyRecordValidator.setParameter(options, anyRecordValidatorParameter);
DataSchemaAnnotationValidator validator = new DataSchemaAnnotationValidator(schema);
if (debug)
TestUtil.out.println(validator);
ValidationResult result = ValidateDataAgainstSchema.validate(object, schema, options, validator);
checkValidationResult(result, row, i, debug);
}
}
use of com.linkedin.data.schema.validation.ValidationResult in project rest.li by linkedin.
the class TestEmptyRecord method testNonEmpty.
@Test
public void testNonEmpty() {
final EmptyRecord record = new EmptyRecord();
record.data().put("non", "empty");
final DataSchemaAnnotationValidator validator = new DataSchemaAnnotationValidator(record.schema());
final ValidationResult result = ValidateDataAgainstSchema.validate(record.data(), record.schema(), new ValidationOptions(), validator);
Assert.assertFalse(result.isValid());
}
use of com.linkedin.data.schema.validation.ValidationResult in project rest.li by linkedin.
the class TestDataTranslator method testDataTranslation.
private void testDataTranslation(String schemaText, String[][] row) throws IOException {
boolean debug = false;
if (debug)
out.print(schemaText);
RecordDataSchema recordDataSchema = (RecordDataSchema) TestUtil.dataSchemaFromString(schemaText);
Schema avroSchema = SchemaTranslator.dataToAvroSchema(recordDataSchema);
if (debug)
out.println(avroSchema);
// translate data
for (int col = 1; col < row.length; col++) {
String result;
GenericRecord avroRecord = null;
Exception exc = null;
if (debug)
out.println(col + " DataMap: " + row[col][0]);
DataMap dataMap = TestUtil.dataMapFromString(row[col][0]);
// translate from Pegasus to Avro
try {
avroRecord = DataTranslator.dataMapToGenericRecord(dataMap, recordDataSchema, avroSchema);
String avroJson = AvroUtil.jsonFromGenericRecord(avroRecord);
if (debug)
out.println(col + " GenericRecord: " + avroJson);
result = avroJson;
} catch (Exception e) {
exc = e;
result = TestUtil.stringFromException(e);
if (debug)
out.println(col + " Exception: " + result);
}
int start = 1;
boolean oneWay = false;
if (start < row[col].length && row[col][start] == ONE_WAY) {
oneWay = true;
start++;
}
// verify
for (int i = start; i < row[col].length; i++) {
if (debug)
out.println(col + " Test:" + row[col][i]);
if (debug && exc != null && result.contains(row[col][i]) == false)
exc.printStackTrace(out);
String expectedBeforeNamespaceProcessor = row[col][i];
String expected = TestAvroUtil.namespaceProcessor(expectedBeforeNamespaceProcessor);
if (debug && expected != expectedBeforeNamespaceProcessor)
out.println(" Expected:" + expected);
assertTrue(result.contains(expected));
}
if (avroRecord != null) {
// translate from Avro back to Pegasus
DataMap dataMapResult = DataTranslator.genericRecordToDataMap(avroRecord, recordDataSchema, avroSchema);
ValidationResult vr = ValidateDataAgainstSchema.validate(dataMap, recordDataSchema, new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.NORMAL));
DataMap fixedInputDataMap = (DataMap) vr.getFixed();
assertTrue(vr.isValid());
if (oneWay == false) {
assertEquals(dataMapResult, fixedInputDataMap);
}
// serialize avroRecord to binary and back
byte[] avroBytes = AvroUtil.bytesFromGenericRecord(avroRecord);
GenericRecord avroRecordFromBytes = AvroUtil.genericRecordFromBytes(avroBytes, avroRecord.getSchema());
byte[] avroBytesAgain = AvroUtil.bytesFromGenericRecord(avroRecordFromBytes);
assertEquals(avroBytes, avroBytesAgain);
// check result of roundtrip binary serialization
DataMap dataMapFromBinaryResult = DataTranslator.genericRecordToDataMap(avroRecordFromBytes, recordDataSchema, avroSchema);
vr = ValidateDataAgainstSchema.validate(dataMapFromBinaryResult, recordDataSchema, new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.NORMAL));
fixedInputDataMap = (DataMap) vr.getFixed();
assertTrue(vr.isValid());
if (oneWay == false) {
assertEquals(dataMapResult, fixedInputDataMap);
}
}
}
}
use of com.linkedin.data.schema.validation.ValidationResult in project rest.li by linkedin.
the class TestSchemaSampleDataGenerator method testRecordSchema.
@Test
public void testRecordSchema() {
final RecordDataSchema schema = (RecordDataSchema) DataTemplateUtil.getSchema(Certification.class);
final DataMap value = SchemaSampleDataGenerator.buildRecordData(schema, _spec);
final ValidationResult result = ValidateDataAgainstSchema.validate(value, schema, new ValidationOptions());
Assert.assertTrue(result.isValid(), Arrays.toString(result.getMessages().toArray()));
}
Aggregations