use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class TestCompatibilityChecker method testCompatibility.
private void testCompatibility(String olderSchemaText, String newerSchemaText, List<CompatibilityOptions> compatibilityOptions, boolean hasError, Object[] expected, int expectedIndex) throws IOException {
if (_debug)
out.println(olderSchemaText + "\n" + newerSchemaText);
DataSchema olderSchema = TestUtil.dataSchemaFromString(olderSchemaText);
DataSchema newerSchema = TestUtil.dataSchemaFromString(newerSchemaText);
assertNotNull(olderSchema, olderSchemaText);
assertNotNull(newerSchema, newerSchemaText);
for (CompatibilityOptions option : compatibilityOptions) {
CompatibilityResult result = CompatibilityChecker.checkCompatibility(olderSchema, newerSchema, option);
String messageText = result.getMessages().toString();
if (_debug)
out.println(result);
assertEquals(result.isError(), hasError, olderSchemaText + "\n" + newerSchemaText + "\n" + messageText);
for (int i = expectedIndex; i < expected.length; i++) {
String expectedText = ".*" + expected[i] + "\n.*";
if (_debug)
out.println(expectedText);
Pattern pattern = Pattern.compile(expectedText, Pattern.DOTALL);
Matcher matcher = pattern.matcher(messageText);
boolean matches = matcher.matches();
assertTrue(matches, messageText + "\n" + expectedText);
}
}
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class TestDataSchemaResolver method lookup.
public void lookup(DataSchemaResolver resolver, String[][] lookups, char separator, boolean debug) {
PegasusSchemaParser parser = new SchemaParser(resolver);
for (String[] entry : lookups) {
String name = entry[0];
String expectFound = entry[1];
String expected = entry[2];
DataSchema schema = parser.lookupName(name);
if (debug) {
out.println("----" + name + "-----");
}
String errorMessage = parser.errorMessage();
if (debug && errorMessage.isEmpty() == false) {
out.println(errorMessage);
}
if (expectFound == ERROR) {
assertTrue(parser.hasError());
assertTrue(expected == null || errorMessage.contains(expected));
} else if (expectFound == FOUND) {
assertTrue(schema != null);
String schemaText = schema.toString();
if (debug) {
out.println(schemaText);
}
assertFalse(parser.hasError());
assertTrue(schema instanceof NamedDataSchema);
NamedDataSchema namedSchema = (NamedDataSchema) schema;
assertEquals(namedSchema.getFullName(), name);
assertTrue(schemaText.contains(expected));
assertTrue(resolver.bindings().containsKey(name));
assertSame(resolver.bindings().get(name), namedSchema);
String location = entry[3];
DataSchemaLocation namedSchemalocation = resolver.nameToDataSchemaLocations().get(name);
String locationNorm;
if (namedSchemalocation.toString().contains(".jar")) {
locationNorm = location.replace(separator, '/');
} else {
locationNorm = location.replace('/', separator);
}
assertNotNull(namedSchemalocation);
assertEquals(namedSchemalocation.toString().indexOf(locationNorm), namedSchemalocation.toString().length() - locationNorm.length());
assertTrue(resolver.locationResolved(namedSchemalocation));
} else if (expectFound == NOT_FOUND) {
assertTrue(schema == null);
assertFalse(parser.hasError());
assertTrue(expected == null || errorMessage.contains(expected));
assertFalse(resolver.bindings().containsKey(name));
} else {
assertTrue(false);
}
}
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class TestConversions method testConvertDataMapToDataSchema.
@Test
public void testConvertDataMapToDataSchema() throws IOException {
for (String good : goodInputs) {
NamedDataSchema dataSchema = (NamedDataSchema) TestUtil.dataSchemaFromString(good);
DataMap mapFromString = TestUtil.dataMapFromString(good);
PegasusSchemaParser parser = new SchemaParser();
DataSchema schemaFromMap = Conversions.dataMapToDataSchema(mapFromString, parser);
assertEquals(schemaFromMap, dataSchema);
}
for (String bad : badInputs) {
DataMap mapFromString = TestUtil.dataMapFromString(bad);
PegasusSchemaParser parser = new SchemaParser();
DataSchema schemaFromMap = Conversions.dataMapToDataSchema(mapFromString, parser);
assertNull(schemaFromMap);
assertTrue(parser.hasError());
}
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class TestValidation method testJsonValidation.
@Test
public void testJsonValidation() throws IOException {
Object[][] input = { { "{ \"type\" : \"record\", \"name\" : \"Foo\", \"fields\" : [ { \"name\" : \"intField\", \"type\" : \"int\" } ] }", new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.NORMAL), "{ \"intField\" : " + Integer.MAX_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.OFF), "{ \"intField\" : " + Integer.MAX_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.NORMAL), "{ \"intField\" : " + Integer.MIN_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.OFF), "{ \"intField\" : " + Integer.MIN_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.OFF), "{ \"intField\" : " + ((long) Integer.MAX_VALUE + 1) + " }", "ERROR :: /intField :: 2147483648 is not backed by a Integer" } }, { "{ \"type\" : \"record\", \"name\" : \"Foo\", \"fields\" : [ { \"name\" : \"longField\", \"type\" : \"long\" } ] }", new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.NORMAL), "{ \"longField\" : " + Long.MAX_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.OFF), "{ \"longField\" : " + Long.MAX_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.NORMAL), "{ \"longField\" : " + Long.MIN_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.OFF), "{ \"longField\" : " + Long.MIN_VALUE + " }" } } };
for (Object[] row : input) {
String schemaText = (String) row[0];
for (int i = 1; i < row.length; i++) {
Object[] test = (Object[]) row[i];
ValidationOptions options = (ValidationOptions) test[0];
String dataText = (String) test[1];
String expectedResult = test.length > 2 ? (String) test[2] : null;
DataSchema schema = dataSchemaFromString(schemaText);
DataMap dataMap = dataMapFromString(dataText);
ValidationResult result = ValidateDataAgainstSchema.validate(dataMap, schema, options);
if (expectedResult == null) {
Assert.assertTrue(result.isValid());
Assert.assertEquals(result.getMessages().size(), 0);
} else {
Assert.assertTrue(result.toString().contains(expectedResult));
}
}
}
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class TestValidation method testUnrecognizedFieldTrimming.
@Test
public void testUnrecognizedFieldTrimming() throws IOException {
ValidationOptions options = new ValidationOptions(RequiredMode.CAN_BE_ABSENT_IF_HAS_DEFAULT, CoercionMode.NORMAL, UnrecognizedFieldMode.TRIM);
String schemaText = "{\n" + " \"name\" : \"Foo\",\n" + " \"type\" : \"record\",\n" + " \"fields\" : [\n" + " { \"name\" : \"primitive\", \"type\" : \"int\", \"optional\" : true },\n" + " { \"name\" : \"arrayField\", \"type\" : { \"type\" : \"array\", \"items\" : \"Foo\" }, \"optional\" : true },\n" + " { \"name\" : \"mapField\", \"type\" : { \"type\" : \"map\", \"values\" : \"Foo\" }, \"optional\" : true },\n" + " { \"name\" : \"unionField\", \"type\" : [ \"int\", \"string\", \"Foo\" ], \"optional\" : true },\n" + " { \"name\" : \"recordField\", \"type\" : \"Foo\", \"optional\" : true }\n" + " ]\n" + "}\n";
DataSchema schema = dataSchemaFromString(schemaText);
String dataString = "{\n" + " \"primitive\" : 1,\n" + " \"unrecognizedPrimitive\": -1,\n" + " \"arrayField\" : [ { \"primitive\": 2, \"unrecognizedInArray\": -2 } ],\n" + " \"mapField\" : { \"key\": { \"primitive\": 3, \"unrecognizedInMap\": -3 } },\n" + " \"unionField\" : { \"Foo\": { \"primitive\": 4, \"unrecognizedInMap\": -4 } },\n" + " \"recordField\" : {\n" + " \"primitive\" : 5,\n" + " \"unrecognizedPrimitive\": -5\n" + " },\n" + " \"unrecognizedMap\": { \"key\": -100},\n" + " \"unrecognizedArray\": [ -101 ]\n" + "}";
String trimmedDataString = "{\n" + " \"primitive\" : 1,\n" + " \"arrayField\" : [ { \"primitive\": 2 } ],\n" + " \"mapField\" : { \"key\": { \"primitive\": 3 } },\n" + " \"unionField\" : { \"Foo\": { \"primitive\": 4 } },\n" + " \"recordField\" : {\n" + " \"primitive\" : 5\n" + " }\n" + "}";
// mutable
DataMap toValidate = dataMapFromString(dataString);
ValidationResult result = validate(toValidate, schema, options);
Assert.assertTrue(result.isValid(), MessageUtil.messagesToString(result.getMessages()));
Assert.assertEquals(result.getFixed(), dataMapFromString(trimmedDataString));
Assert.assertSame(toValidate, result.getFixed());
// read-only
DataMap readOnlyToValidate = dataMapFromString(dataString);
readOnlyToValidate.makeReadOnly();
ValidationResult readOnlyResult = validate(readOnlyToValidate, schema, options);
Assert.assertTrue(readOnlyResult.hasFixupReadOnlyError());
String message = readOnlyResult.getMessages().toString();
Assert.assertEquals(readOnlyResult.getMessages().size(), 7);
String[] expectedStrings = new String[] { "/unrecognizedMap", "/unrecognizedArray", "/recordField/unrecognizedPrimitive", "/unionField/Foo/unrecognizedInMap", "/unrecognizedPrimitive", "/arrayField/0/unrecognizedInArray", "/mapField/key/unrecognizedInMap" };
for (String expected : expectedStrings) {
Assert.assertTrue(message.contains(expected), message + " does not contain " + expected);
}
}
Aggregations