Search in sources :

Example 1 with ResourceCompatibilityChecker

use of com.linkedin.restli.tools.compatibility.ResourceCompatibilityChecker in project rest.li by linkedin.

the class RestLiSnapshotCompatibilityChecker method checkCompatibility.

private CompatibilityInfoMap checkCompatibility(String prevRestModelPath, String currRestModelPath, CompatibilityLevel compatLevel, boolean isAgainstRestSpec) {
    final CompatibilityInfoMap infoMap = _infoMap;
    if (compatLevel == CompatibilityLevel.OFF) {
        // skip check entirely.
        return infoMap;
    }
    final Stack<Object> path = new Stack<>();
    path.push("");
    FileInputStream prevSnapshotFile = null;
    FileInputStream currSnapshotFile = null;
    try {
        prevSnapshotFile = new FileInputStream(prevRestModelPath);
    } catch (FileNotFoundException e) {
        infoMap.addRestSpecInfo(CompatibilityInfo.Type.RESOURCE_NEW, path, currRestModelPath);
    }
    try {
        currSnapshotFile = new FileInputStream(currRestModelPath);
    } catch (FileNotFoundException e) {
        infoMap.addRestSpecInfo(CompatibilityInfo.Type.RESOURCE_MISSING, path, prevRestModelPath);
    }
    if (prevSnapshotFile == null || currSnapshotFile == null) {
        return infoMap;
    }
    AbstractSnapshot prevSnapshot = null;
    AbstractSnapshot currSnapshot = null;
    try {
        if (isAgainstRestSpec) {
            prevSnapshot = new RestSpec(prevSnapshotFile);
        } else {
            prevSnapshot = new Snapshot(prevSnapshotFile);
        }
        currSnapshot = new Snapshot(currSnapshotFile);
    } catch (IOException e) {
        infoMap.addRestSpecInfo(CompatibilityInfo.Type.OTHER_ERROR, path, e.getMessage());
    }
    if (prevSnapshot == null || currSnapshot == null) {
        return infoMap;
    }
    final DataSchemaResolver currResolver = createResolverFromSnapshot(currSnapshot, _resolverPath);
    final DataSchemaResolver prevResolver;
    if (isAgainstRestSpec) {
        prevResolver = currResolver;
    } else {
        prevResolver = createResolverFromSnapshot(prevSnapshot, _resolverPath);
    }
    final ResourceCompatibilityChecker checker = new ResourceCompatibilityChecker(prevSnapshot.getResourceSchema(), prevResolver, currSnapshot.getResourceSchema(), currResolver);
    checker.check(compatLevel);
    infoMap.addAll(checker.getInfoMap());
    return infoMap;
}
Also used : DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver) FileNotFoundException(java.io.FileNotFoundException) ResourceCompatibilityChecker(com.linkedin.restli.tools.compatibility.ResourceCompatibilityChecker) IOException(java.io.IOException) CompatibilityInfoMap(com.linkedin.restli.tools.compatibility.CompatibilityInfoMap) FileInputStream(java.io.FileInputStream) Stack(java.util.Stack)

Example 2 with ResourceCompatibilityChecker

use of com.linkedin.restli.tools.compatibility.ResourceCompatibilityChecker 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<>();
    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) DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver) MultiFormatDataSchemaResolver(com.linkedin.data.schema.resolver.MultiFormatDataSchemaResolver) 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)

Aggregations

DataSchemaResolver (com.linkedin.data.schema.DataSchemaResolver)2 ResourceCompatibilityChecker (com.linkedin.restli.tools.compatibility.ResourceCompatibilityChecker)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 Stack (java.util.Stack)2 DefaultDataSchemaResolver (com.linkedin.data.schema.resolver.DefaultDataSchemaResolver)1 MultiFormatDataSchemaResolver (com.linkedin.data.schema.resolver.MultiFormatDataSchemaResolver)1 ResourceSchema (com.linkedin.restli.restspec.ResourceSchema)1 CompatibilityInfoMap (com.linkedin.restli.tools.compatibility.CompatibilityInfoMap)1 ParseException (org.apache.commons.cli.ParseException)1