Search in sources :

Example 1 with CompatibilityInfoMap

use of com.linkedin.restli.tools.compatibility.CompatibilityInfoMap 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 CompatibilityInfoMap

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

the class TestRestliSnapshotCompatibilityChecker method testIncompatibleRestLiDataAnnotations.

@Test
public void testIncompatibleRestLiDataAnnotations() {
    final Collection<CompatibilityInfo> errors = new HashSet<>();
    errors.add(new CompatibilityInfo(Arrays.<Object>asList("", "annotations", "intB"), CompatibilityInfo.Type.ANNOTATION_CHANGE_BREAKS_OLD_CLIENT, "Cannot add ReadOnly annotation"));
    errors.add(new CompatibilityInfo(Arrays.<Object>asList("", "annotations", "validationDemoNext/intA"), CompatibilityInfo.Type.ANNOTATION_CHANGE_BREAKS_OLD_CLIENT, "Cannot add CreateOnly annotation"));
    // Removing ReadOnly annotation on a required field
    errors.add(new CompatibilityInfo(Arrays.<Object>asList("", "annotations", "validationDemoNext/stringB"), CompatibilityInfo.Type.ANNOTATION_CHANGE_BREAKS_NEW_SERVER, "Cannot remove ReadOnly annotation"));
    // Removing ReadOnly annotation on an optional field
    errors.add(new CompatibilityInfo(Arrays.<Object>asList("", "annotations", "intA"), CompatibilityInfo.Type.ANNOTATION_CHANGE_MAY_REQUIRE_CLIENT_CODE_CHANGE, "Cannot remove ReadOnly annotation"));
    // Removing CreateOnly annotation on a required field
    errors.add(new CompatibilityInfo(Arrays.<Object>asList("", "annotations", "stringB"), CompatibilityInfo.Type.ANNOTATION_CHANGE_MAY_REQUIRE_CLIENT_CODE_CHANGE, "Cannot remove CreateOnly annotation"));
    // Removing CreateOnly annotation on an optional field
    errors.add(new CompatibilityInfo(Arrays.<Object>asList("", "annotations", "intB"), CompatibilityInfo.Type.ANNOTATION_CHANGE_MAY_REQUIRE_CLIENT_CODE_CHANGE, "Cannot remove CreateOnly annotation"));
    final RestLiSnapshotCompatibilityChecker checker = new RestLiSnapshotCompatibilityChecker();
    final CompatibilityInfoMap infoMap = checker.check(RESOURCES_DIR + FS + "snapshots" + FS + "prev-validationdemos.snapshot.json", RESOURCES_DIR + FS + "snapshots" + FS + "curr-validationdemos-fail.snapshot.json", CompatibilityLevel.BACKWARDS);
    final Collection<CompatibilityInfo> restSpecIncompatibles = infoMap.getRestSpecIncompatibles();
    for (CompatibilityInfo e : errors) {
        Assert.assertTrue(restSpecIncompatibles.contains(e), "Reported restspec incompatibles should contain: " + e.toString());
        restSpecIncompatibles.remove(e);
    }
    Assert.assertTrue(restSpecIncompatibles.isEmpty());
}
Also used : CompatibilityInfo(com.linkedin.restli.tools.idlcheck.CompatibilityInfo) CompatibilityInfoMap(com.linkedin.restli.tools.compatibility.CompatibilityInfoMap) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 3 with CompatibilityInfoMap

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

the class TestRestliSnapshotCompatibilityChecker method testCompatibleRestSpecVsSnapshot.

@Test
public void testCompatibleRestSpecVsSnapshot() {
    final RestLiSnapshotCompatibilityChecker checker = new RestLiSnapshotCompatibilityChecker();
    final CompatibilityInfoMap infoMap = checker.checkRestSpecVsSnapshot(RESOURCES_DIR + FS + "idls" + FS + "twitter-statuses.restspec.json", RESOURCES_DIR + FS + "snapshots" + FS + "twitter-statuses.snapshot.json", CompatibilityLevel.EQUIVALENT);
    Assert.assertTrue(infoMap.isEquivalent());
}
Also used : CompatibilityInfoMap(com.linkedin.restli.tools.compatibility.CompatibilityInfoMap) Test(org.testng.annotations.Test)

Example 4 with CompatibilityInfoMap

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

the class TestRestliSnapshotCompatibilityChecker method testFileNotFound.

@Test
public void testFileNotFound() {
    final String nonExistentFilename1 = "NonExistentFile1";
    final String nonExistentFilename2 = "NonExistentFile2";
    final Collection<CompatibilityInfo> testIncompatibles = new HashSet<>();
    final Collection<CompatibilityInfo> testCompatibles = new HashSet<>();
    testIncompatibles.add(new CompatibilityInfo(Arrays.<Object>asList(""), CompatibilityInfo.Type.RESOURCE_MISSING, nonExistentFilename1));
    testCompatibles.add(new CompatibilityInfo(Arrays.<Object>asList(""), CompatibilityInfo.Type.RESOURCE_NEW, nonExistentFilename2));
    final RestLiSnapshotCompatibilityChecker checker = new RestLiSnapshotCompatibilityChecker();
    CompatibilityInfoMap infoMap = checker.check(nonExistentFilename1, nonExistentFilename2, CompatibilityLevel.BACKWARDS);
    Assert.assertFalse(infoMap.isCompatible(CompatibilityLevel.BACKWARDS));
    final Collection<CompatibilityInfo> incompatibles = new HashSet<>(infoMap.getIncompatibles());
    final Collection<CompatibilityInfo> compatibles = new HashSet<>(infoMap.getCompatibles());
    for (CompatibilityInfo te : incompatibles) {
        Assert.assertTrue(testIncompatibles.contains(te), "Reported incompatibles should contain: " + te.toString());
        incompatibles.remove(te);
    }
    for (CompatibilityInfo di : compatibles) {
        Assert.assertTrue(testCompatibles.contains(di), "Reported compatibles should contain: " + di.toString());
        compatibles.remove(di);
    }
}
Also used : CompatibilityInfo(com.linkedin.restli.tools.idlcheck.CompatibilityInfo) CompatibilityInfoMap(com.linkedin.restli.tools.compatibility.CompatibilityInfoMap) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 5 with CompatibilityInfoMap

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

the class TestPegasusSchemaSnapshotCompatibilityChecker method testIncompatiblePegasusSchemaSnapshot.

@Test(dataProvider = "incompatibleInputFiles")
public void testIncompatiblePegasusSchemaSnapshot(String prevSchema, String currSchema, Collection<CompatibilityInfo> expectedIncompatibilityErrors, Collection<CompatibilityInfo> expectedCompatibilityDiffs) {
    PegasusSchemaSnapshotCompatibilityChecker checker = new PegasusSchemaSnapshotCompatibilityChecker();
    CompatibilityInfoMap infoMap = checker.checkPegasusSchemaCompatibility(snapshotDir + FS + prevSchema, snapshotDir + FS + currSchema, CompatibilityOptions.Mode.DATA);
    Assert.assertFalse(infoMap.isModelCompatible(CompatibilityLevel.BACKWARDS));
    Assert.assertFalse(infoMap.isModelCompatible(CompatibilityLevel.EQUIVALENT));
    Assert.assertTrue(infoMap.isModelCompatible(CompatibilityLevel.IGNORE));
    final Collection<CompatibilityInfo> modelIncompatibles = infoMap.getModelIncompatibles();
    final Collection<CompatibilityInfo> modelCompatibles = infoMap.getModelCompatibles();
    for (CompatibilityInfo error : expectedIncompatibilityErrors) {
        Assert.assertTrue(modelIncompatibles.contains(error), "Reported model incompatibles should contain: " + error.toString());
        modelIncompatibles.remove(error);
    }
    for (CompatibilityInfo diff : expectedCompatibilityDiffs) {
        Assert.assertTrue(modelCompatibles.contains(diff), "Reported model compatibles should contain: " + diff.toString());
        modelCompatibles.remove(diff);
    }
    Assert.assertTrue(modelIncompatibles.isEmpty());
    Assert.assertTrue(modelCompatibles.isEmpty());
}
Also used : CompatibilityInfo(com.linkedin.restli.tools.idlcheck.CompatibilityInfo) CompatibilityInfoMap(com.linkedin.restli.tools.compatibility.CompatibilityInfoMap) Test(org.testng.annotations.Test)

Aggregations

CompatibilityInfoMap (com.linkedin.restli.tools.compatibility.CompatibilityInfoMap)8 Test (org.testng.annotations.Test)7 CompatibilityInfo (com.linkedin.restli.tools.idlcheck.CompatibilityInfo)4 HashSet (java.util.HashSet)3 DataSchemaResolver (com.linkedin.data.schema.DataSchemaResolver)1 ResourceCompatibilityChecker (com.linkedin.restli.tools.compatibility.ResourceCompatibilityChecker)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Stack (java.util.Stack)1