Search in sources :

Example 21 with ResourceSchema

use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.

the class RestLiResourceModelCompatibilityChecker method check.

/**
   * Check backwards compatibility between two idl (.restspec.json) files.
   *
   * @param prevRestspecPath previously existing idl file
   * @param currRestspecPath current idl file
   * @param compatLevel compatibility level which affects the return value
   * @return true if the check result conforms the compatibility level requirement
   *         e.g. false if backwards compatible changes are found but the level is equivalent
   */
public boolean check(String prevRestspecPath, String currRestspecPath, CompatibilityLevel compatLevel) {
    _prevRestspecPath = prevRestspecPath;
    _currRestspecPath = currRestspecPath;
    Stack<Object> path = new Stack<Object>();
    path.push("");
    ResourceSchema prevRec = null;
    ResourceSchema currRec = null;
    try {
        prevRec = _codec.readResourceSchema(new FileInputStream(prevRestspecPath));
    } catch (FileNotFoundException e) {
        _infoMap.addRestSpecInfo(CompatibilityInfo.Type.RESOURCE_NEW, path, currRestspecPath);
    } catch (IOException e) {
        _infoMap.addRestSpecInfo(CompatibilityInfo.Type.OTHER_ERROR, path, e.getMessage());
    }
    try {
        currRec = _codec.readResourceSchema(new FileInputStream(currRestspecPath));
    } catch (FileNotFoundException e) {
        _infoMap.addRestSpecInfo(CompatibilityInfo.Type.RESOURCE_MISSING, path, prevRestspecPath);
    } catch (Exception e) {
        _infoMap.addRestSpecInfo(CompatibilityInfo.Type.OTHER_ERROR, path, e.getMessage());
    }
    if (prevRec == null || currRec == null) {
        return _infoMap.isCompatible(compatLevel);
    }
    final DataSchemaResolver resolver;
    if (_resolverPath == null) {
        resolver = new DefaultDataSchemaResolver();
    } else {
        resolver = MultiFormatDataSchemaResolver.withBuiltinFormats(_resolverPath);
    }
    ResourceCompatibilityChecker checker = new ResourceCompatibilityChecker(prevRec, resolver, currRec, resolver);
    boolean check = checker.check(compatLevel);
    _infoMap.addAll(checker.getInfoMap());
    return check;
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) MultiFormatDataSchemaResolver(com.linkedin.data.schema.resolver.MultiFormatDataSchemaResolver) DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver) DefaultDataSchemaResolver(com.linkedin.data.schema.resolver.DefaultDataSchemaResolver) FileNotFoundException(java.io.FileNotFoundException) DefaultDataSchemaResolver(com.linkedin.data.schema.resolver.DefaultDataSchemaResolver) ResourceCompatibilityChecker(com.linkedin.restli.tools.compatibility.ResourceCompatibilityChecker) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.apache.commons.cli.ParseException) Stack(java.util.Stack)

Example 22 with ResourceSchema

use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.

the class TestResourceCompatibilityChecker method testFailSimpleFile.

@Test
public void testFailSimpleFile() throws IOException {
    final Collection<CompatibilityInfo> resourceTestErrors = new HashSet<CompatibilityInfo>();
    final Collection<CompatibilityInfo> modelTestErrors = new HashSet<CompatibilityInfo>();
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "simple", "supports"), CompatibilityInfo.Type.ARRAY_NOT_CONTAIN, new StringArray(Arrays.asList("delete", "get"))));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "simple", "methods"), CompatibilityInfo.Type.ARRAY_MISSING_ELEMENT, "delete"));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "simple", "methods", "get", "parameters", "param1", "type"), CompatibilityInfo.Type.TYPE_ERROR, "schema type changed from string to int"));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "simple", "actions", "oneAction", "parameters", "bitfield", "items"), CompatibilityInfo.Type.TYPE_ERROR, "schema type changed from boolean to int"));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "simple", "actions", "oneAction", "parameters"), CompatibilityInfo.Type.ARRAY_MISSING_ELEMENT, "someString"));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "simple", "actions", "oneAction", "parameters"), CompatibilityInfo.Type.PARAMETER_NEW_REQUIRED, "someStringNew"));
    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_SIMPLE_FILE);
    final ResourceSchema currResource = idlToResource(IDLS_SUFFIX + CURR_SIMPLE_FAIL_FILE);
    ResourceCompatibilityChecker checker = new ResourceCompatibilityChecker(prevResource, prevSchemaResolver, currResource, incompatSchemaResolver);
    Assert.assertFalse(checker.check(CompatibilityLevel.BACKWARDS));
    final Collection<CompatibilityInfo> resourceIncompatible = new HashSet<CompatibilityInfo>(checker.getInfoMap().getRestSpecIncompatibles());
    for (CompatibilityInfo te : resourceTestErrors) {
        Assert.assertTrue(resourceIncompatible.contains(te), "Reported resource incompatibles should contain: " + te.toString());
        resourceIncompatible.remove(te);
    }
    Assert.assertTrue(resourceIncompatible.isEmpty());
    final Collection<CompatibilityInfo> modelIncompatible = new HashSet<CompatibilityInfo>(checker.getInfoMap().getModelIncompatibles());
    for (CompatibilityInfo te : modelTestErrors) {
        Assert.assertTrue(modelIncompatible.contains(te), "Reported model incompatibles should contain: " + te.toString());
        modelIncompatible.remove(te);
    }
    Assert.assertTrue(modelIncompatible.isEmpty());
// ignore compatibles
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) StringArray(com.linkedin.data.template.StringArray) CompatibilityInfo(com.linkedin.restli.tools.idlcheck.CompatibilityInfo) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 23 with ResourceSchema

use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.

the class TestResourceCompatibilityChecker method testFailAssociationFile.

@Test
public void testFailAssociationFile() throws IOException {
    final AssocKeySchema prevAssocKey = new AssocKeySchema();
    prevAssocKey.setName("key1");
    prevAssocKey.setType("string");
    final Collection<CompatibilityInfo> testErrors = new HashSet<CompatibilityInfo>();
    testErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "association", "assocKeys"), CompatibilityInfo.Type.ARRAY_NOT_EQUAL, new AssocKeySchemaArray(Arrays.asList(prevAssocKey))));
    testErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "association", "supports"), CompatibilityInfo.Type.ARRAY_NOT_CONTAIN, new StringArray(Arrays.asList("create", "get"))));
    testErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "association", "methods", "create", "parameters"), CompatibilityInfo.Type.PARAMETER_NEW_REQUIRED, "data"));
    testErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "association", "methods"), CompatibilityInfo.Type.ARRAY_MISSING_ELEMENT, "get"));
    testErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "association", "entity", "path"), CompatibilityInfo.Type.VALUE_NOT_EQUAL, "/greetings/assoc/{greetingsId}", "/greetings/association/{greetingsId}"));
    final ResourceSchema prevResource = idlToResource(IDLS_SUFFIX + PREV_ASSOC_FILE);
    final ResourceSchema currResource = idlToResource(IDLS_SUFFIX + CURR_ASSOC_FAIL_FILE);
    ResourceCompatibilityChecker checker = new ResourceCompatibilityChecker(prevResource, prevSchemaResolver, currResource, prevSchemaResolver);
    Assert.assertFalse(checker.check(CompatibilityLevel.BACKWARDS));
    final Collection<CompatibilityInfo> incompatibles = new HashSet<CompatibilityInfo>(checker.getInfoMap().getIncompatibles());
    for (CompatibilityInfo te : testErrors) {
        Assert.assertTrue(incompatibles.contains(te), "Reported incompatibles should contain: " + te.toString());
        incompatibles.remove(te);
    }
    Assert.assertTrue(incompatibles.isEmpty());
}
Also used : AssocKeySchemaArray(com.linkedin.restli.restspec.AssocKeySchemaArray) ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) StringArray(com.linkedin.data.template.StringArray) CompatibilityInfo(com.linkedin.restli.tools.idlcheck.CompatibilityInfo) AssocKeySchema(com.linkedin.restli.restspec.AssocKeySchema) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 24 with ResourceSchema

use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.

the class TestResourceCompatibilityChecker method testPassAssociationFile.

@Test
public void testPassAssociationFile() throws IOException {
    final Collection<CompatibilityInfo> testDiffs = new HashSet<CompatibilityInfo>();
    testDiffs.add(new CompatibilityInfo(Arrays.<Object>asList("", "association", "methods", "create", "parameters"), CompatibilityInfo.Type.PARAMETER_NEW_OPTIONAL, "type"));
    final ResourceSchema prevResource = idlToResource(IDLS_SUFFIX + PREV_ASSOC_FILE);
    final ResourceSchema currResource = idlToResource(IDLS_SUFFIX + CURR_ASSOC_PASS_FILE);
    ResourceCompatibilityChecker checker = new ResourceCompatibilityChecker(prevResource, prevSchemaResolver, currResource, prevSchemaResolver);
    Assert.assertTrue(checker.check(CompatibilityLevel.BACKWARDS));
    final Collection<CompatibilityInfo> incompatibles = checker.getInfoMap().getIncompatibles();
    final Collection<CompatibilityInfo> compatibles = new HashSet<CompatibilityInfo>(checker.getInfoMap().getCompatibles());
    for (CompatibilityInfo di : testDiffs) {
        Assert.assertTrue(compatibles.contains(di), "Reported compatibles should contain: " + di.toString());
        compatibles.remove(di);
    }
    Assert.assertTrue(incompatibles.isEmpty());
    Assert.assertTrue(compatibles.isEmpty());
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) CompatibilityInfo(com.linkedin.restli.tools.idlcheck.CompatibilityInfo) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 25 with ResourceSchema

use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.

the class TestResourceSchemaCollection method testSubresource.

@Test
public void testSubresource() {
    final ResourceSchema groupsResource = _schemas.getResource("groups");
    final List<ResourceSchema> groupsSubresources = _schemas.getSubResources(groupsResource);
    Assert.assertEquals(groupsSubresources.size(), 1);
    final ResourceSchema groupsContactsResource = groupsSubresources.get(0);
    Assert.assertEquals(groupsContactsResource.getName(), "contacts");
    Assert.assertEquals(groupsContactsResource.getNamespace(), groupsResource.getNamespace());
    final ResourceSchema noNamespaceResource = _schemas.getResource("noNamespace");
    final List<ResourceSchema> actualNoNamespaceSubresources = _schemas.getSubResources(noNamespaceResource);
    Assert.assertEquals(actualNoNamespaceSubresources.size(), 2);
    final Set<String> expectedNoNamespaceSubresources = new HashSet<String>();
    expectedNoNamespaceSubresources.add("noNamespaceSub");
    expectedNoNamespaceSubresources.add("com.linkedin.restli.examples.noNamespace");
    for (ResourceSchema sub : actualNoNamespaceSubresources) {
        final String schemaFullName = getResourceSchemaFullName(sub, sub.getName());
        Assert.assertTrue(expectedNoNamespaceSubresources.contains(schemaFullName));
    }
    final ResourceSchema greetingResource = _schemas.getResource("greeting");
    final List<ResourceSchema> greetingSubResources = _schemas.getSubResources(greetingResource);
    Assert.assertEquals(greetingSubResources.size(), 1);
    final ResourceSchema subgreetingsResource = greetingSubResources.get(0);
    Assert.assertEquals(subgreetingsResource.getName(), "subgreetings");
    Assert.assertEquals(subgreetingsResource.getNamespace(), greetingResource.getNamespace());
    final List<ResourceSchema> subgreetingsSubResources = _schemas.getSubResources(subgreetingsResource);
    Assert.assertEquals(subgreetingsSubResources.size(), 1);
    final ResourceSchema subsubgreetingResource = subgreetingsSubResources.get(0);
    Assert.assertEquals(subsubgreetingResource.getName(), "subsubgreeting");
    Assert.assertEquals(subsubgreetingResource.getNamespace(), greetingResource.getNamespace());
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

ResourceSchema (com.linkedin.restli.restspec.ResourceSchema)35 Test (org.testng.annotations.Test)13 DataMap (com.linkedin.data.DataMap)9 HashSet (java.util.HashSet)9 CompatibilityInfo (com.linkedin.restli.tools.idlcheck.CompatibilityInfo)8 IOException (java.io.IOException)7 HashMap (java.util.HashMap)6 File (java.io.File)5 StringArray (com.linkedin.data.template.StringArray)4 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 CodeUtil (com.linkedin.pegasus.generator.CodeUtil)3 RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)3 ResourceModelEncoder (com.linkedin.restli.internal.server.model.ResourceModelEncoder)3 FileInputStream (java.io.FileInputStream)3 DataSchema (com.linkedin.data.schema.DataSchema)2 DataSchemaResolver (com.linkedin.data.schema.DataSchemaResolver)2 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)2 RecordTemplate (com.linkedin.data.template.RecordTemplate)2