Search in sources :

Example 6 with SchemaParser

use of com.linkedin.data.schema.SchemaParser in project rest.li by linkedin.

the class TestDataSchemaResolver method testClassNameDataSchemaResolver.

@Test
public void testClassNameDataSchemaResolver() {
    @SuppressWarnings("deprecation") final ClassNameDataSchemaResolver resolver = new ClassNameDataSchemaResolver();
    final PegasusSchemaParser parser = new SchemaParser(resolver);
    final Class<? extends RecordTemplate> testClass = ClassNameFooRecord.class;
    final String nonExistSchemaName = "Non-Existing Schema";
    final DataSchema existSchema = parser.lookupName(testClass.getName());
    assertNotNull(existSchema);
    assertTrue(existSchema instanceof RecordDataSchema);
    assertEquals(((RecordDataSchema) existSchema).getFullName(), testClass.getCanonicalName());
    assertFalse(resolver.isBadLocation(new ClassNameDataSchemaLocation(testClass.getName())));
    final DataSchema nonExistSchema = parser.lookupName(nonExistSchemaName);
    assertNull(nonExistSchema);
    assertTrue(parser.errorMessage().contains(nonExistSchemaName));
    assertTrue(resolver.isBadLocation(new ClassNameDataSchemaLocation(nonExistSchemaName)));
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) SchemaParser(com.linkedin.data.schema.SchemaParser) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 7 with SchemaParser

use of com.linkedin.data.schema.SchemaParser in project rest.li by linkedin.

the class TestSchemaFilter method testInputs.

private static void testInputs(Object[][] inputs, boolean isAvroUnionMode) throws IOException {
    for (Object[] row : inputs) {
        String schemaText = (String) row[0];
        Predicate predicate = (Predicate) row[1];
        String expected = (String) row[2];
        NamedDataSchema schema = dataSchemaFromString(schemaText, isAvroUnionMode);
        DataSchema filteredSchema = null;
        SchemaParser parser = new SchemaParser();
        parser.getValidationOptions().setAvroUnionMode(isAvroUnionMode);
        filteredSchema = Filters.removeByPredicate(schema, predicate, parser);
        if (filteredSchema != null) {
            // Schema string match
            String expectedSchemaText = expected;
            DataSchema expectedSchema = dataSchemaFromString(expectedSchemaText, isAvroUnionMode);
            assertEquals(filteredSchema.toString(), expectedSchema.toString());
            assertEquals(filteredSchema, expectedSchema);
        } else {
            String parserMessage = parser.errorMessage();
            assertTrue(parserMessage.contains(expected), "\nContains :" + expected + "\nActual   :" + parserMessage);
        }
    }
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) SchemaParser(com.linkedin.data.schema.SchemaParser) Predicate(com.linkedin.data.it.Predicate)

Example 8 with SchemaParser

use of com.linkedin.data.schema.SchemaParser in project rest.li by linkedin.

the class TestUtil method schemaParserFromString.

public static PegasusSchemaParser schemaParserFromString(String s) throws UnsupportedEncodingException, IOException {
    PegasusSchemaParser parser = new SchemaParser();
    parser.parse(inputStreamFromString(s));
    return parser;
}
Also used : PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) SchemaParser(com.linkedin.data.schema.SchemaParser) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser)

Example 9 with SchemaParser

use of com.linkedin.data.schema.SchemaParser in project rest.li by linkedin.

the class TestUtil method schemaParserFromObjects.

public static PegasusSchemaParser schemaParserFromObjects(List<Object> objects) throws IOException {
    SchemaParser parser = new SchemaParser();
    parser.parse(objects);
    return parser;
}
Also used : SchemaParser(com.linkedin.data.schema.SchemaParser) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser)

Example 10 with SchemaParser

use of com.linkedin.data.schema.SchemaParser in project rest.li by linkedin.

the class TestResourceCompatibilityChecker method testFailCollectionFile.

@Test
public void testFailCollectionFile() throws IOException {
    final SchemaParser sp = new SchemaParser();
    sp.parse("\"StringRef\"");
    final Collection<CompatibilityInfo> resourceTestErrors = new HashSet<CompatibilityInfo>();
    final Collection<CompatibilityInfo> modelTestErrors = new HashSet<CompatibilityInfo>();
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "identifier", "params"), CompatibilityInfo.Type.TYPE_ERROR, "schema type changed from string to long"));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "supports"), CompatibilityInfo.Type.ARRAY_NOT_CONTAIN, new StringArray(Arrays.asList("batch_get", "create", "delete", "get", "get_all"))));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "methods"), CompatibilityInfo.Type.ARRAY_MISSING_ELEMENT, "batch_get"));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "finders", "search", "metadata", "type"), CompatibilityInfo.Type.TYPE_ERROR, "schema type changed from array to int"));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "finders", "search", "assocKeys"), CompatibilityInfo.Type.VALUE_NOT_EQUAL, new StringArray(Arrays.asList("q", "s")), new StringArray(Arrays.asList("q", "changed_key"))));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "finders", "find_assocKey_downgrade", "assocKeys"), CompatibilityInfo.Type.FINDER_ASSOCKEYS_DOWNGRADE));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "actions", "oneAction", "parameters", "bitfield", "items"), CompatibilityInfo.Type.TYPE_ERROR, "schema type changed from boolean to int"));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "actions", "oneAction", "parameters", "someString", "type"), CompatibilityInfo.Type.TYPE_UNKNOWN, sp.errorMessage()));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "actions", "oneAction", "parameters", "stringMap", "type"), CompatibilityInfo.Type.TYPE_ERROR, "schema type changed from string to int"));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "entity", "actions", "anotherAction", "parameters"), CompatibilityInfo.Type.ARRAY_MISSING_ELEMENT, "subMap"));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "entity", "actions", "exceptionTest", "throws"), CompatibilityInfo.Type.ARRAY_NOT_CONTAIN, new StringArray(Arrays.asList("com.linkedin.groups.api.GroupOwnerException", "java.io.FileNotFoundException"))));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "entity", "actions", "someAction", "parameters", "a", "optional"), CompatibilityInfo.Type.PARAMETER_WRONG_OPTIONALITY));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "entity", "actions", "someAction", "parameters", "b", "type"), CompatibilityInfo.Type.TYPE_ERROR, "schema type changed from string to int"));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "entity", "actions", "someAction", "parameters"), CompatibilityInfo.Type.ARRAY_MISSING_ELEMENT, "e"));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "entity", "actions", "someAction", "parameters"), CompatibilityInfo.Type.PARAMETER_NEW_REQUIRED, "f"));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "entity", "actions", "someAction", "returns"), CompatibilityInfo.Type.TYPE_MISSING));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.asList("", "collection", "finders", "oneFinder"), CompatibilityInfo.Type.PAGING_REMOVED));
    modelTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("com.linkedin.greetings.api.Greeting"), CompatibilityInfo.Type.TYPE_BREAKS_NEW_READER, "new record added required fields newField"));
    modelTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("com.linkedin.greetings.api.Greeting"), CompatibilityInfo.Type.TYPE_BREAKS_OLD_READER, "new record removed required fields message"));
    modelTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("com.linkedin.greetings.api.Greeting", "id", "string"), CompatibilityInfo.Type.TYPE_BREAKS_NEW_AND_OLD_READERS, "schema type changed from long to string"));
    final ResourceSchema prevResource = idlToResource(IDLS_SUFFIX + PREV_COLL_FILE);
    final ResourceSchema currResource = idlToResource(IDLS_SUFFIX + CURR_COLL_FAIL_FILE);
    ResourceCompatibilityChecker checker = new ResourceCompatibilityChecker(prevResource, prevSchemaResolver, currResource, incompatSchemaResolver);
    Assert.assertFalse(checker.check(CompatibilityLevel.BACKWARDS));
    final Collection<CompatibilityInfo> resourceIncompatibles = new HashSet<CompatibilityInfo>(checker.getInfoMap().getRestSpecIncompatibles());
    for (CompatibilityInfo te : resourceTestErrors) {
        Assert.assertTrue(resourceIncompatibles.contains(te), "Reported resource incompatibles should contain: " + te.toString());
        resourceIncompatibles.remove(te);
    }
    Assert.assertTrue(resourceIncompatibles.isEmpty());
    final Collection<CompatibilityInfo> modelIncompatibles = new HashSet<CompatibilityInfo>(checker.getInfoMap().getModelIncompatibles());
    for (CompatibilityInfo te : modelTestErrors) {
        Assert.assertTrue(modelIncompatibles.contains(te), "Reported model incompatibles should contain: " + te.toString());
        modelIncompatibles.remove(te);
    }
    Assert.assertTrue(modelIncompatibles.isEmpty());
// ignore compatibles
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) StringArray(com.linkedin.data.template.StringArray) CompatibilityInfo(com.linkedin.restli.tools.idlcheck.CompatibilityInfo) SchemaParser(com.linkedin.data.schema.SchemaParser) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

SchemaParser (com.linkedin.data.schema.SchemaParser)16 DataSchema (com.linkedin.data.schema.DataSchema)11 PegasusSchemaParser (com.linkedin.data.schema.PegasusSchemaParser)10 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)9 Test (org.testng.annotations.Test)7 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)6 DataMap (com.linkedin.data.DataMap)5 Schema (org.apache.avro.Schema)5 Predicate (com.linkedin.data.it.Predicate)4 GenericRecord (org.apache.avro.generic.GenericRecord)4 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 BeforeTest (org.testng.annotations.BeforeTest)2 ByteString (com.linkedin.data.ByteString)1 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)1 DataSchemaLocation (com.linkedin.data.schema.DataSchemaLocation)1 DataSchemaResolver (com.linkedin.data.schema.DataSchemaResolver)1 DataSchemaTraverse (com.linkedin.data.schema.DataSchemaTraverse)1 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)1