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;
}
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
}
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());
}
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());
}
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());
}
Aggregations