use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.
the class TestResourceSchemaCollection method testRootWithResourceModel.
@Test
public void testRootWithResourceModel() {
final Map<String, ResourceType> expectedTypes = new HashMap<String, ResourceType>();
expectedTypes.put("com.linkedin.restli.examples.greetings.client.actions", ResourceType.ACTIONS);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.annotatedComplexKeys", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.autoValidationDemos", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.autoValidationWithProjection", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.compression", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.customTypes", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.customTypes2", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.customTypes3", ResourceType.ASSOCIATION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.chainedTyperefs", ResourceType.ASSOCIATION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.createGreeting", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.projectMessage", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.validationCreateAndGet", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.customMetadataProjections", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.customTypes2.customTypes4", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.typerefKeys", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.cookie", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.withContext", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.exceptions", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.exceptions2", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.exceptions3", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.greetings", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.nullGreeting", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.greetingsAuth", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.greetingsCallback", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.greetingsPromise", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.greetingsPromiseCtx", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.greetingsTask", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.greetingsold", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.streamingGreetings", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.mixed", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.pagingMetadataProjections", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.stringKeys", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.stringKeys.stringKeysSub", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.complexArray", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.complexKeys", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.complexKeys.complexKeysSub", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.complexByteKeys", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.associations", ResourceType.ASSOCIATION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.associations.associationsSub", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.finders", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.greeting", ResourceType.SIMPLE);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.greeting.subgreetings", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.typerefPrimitiveLongAssociationKeyResource", ResourceType.ASSOCIATION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.typerefCustomDoubleAssociationKeyResource", ResourceType.ASSOCIATION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.greeting.subgreetings.subsubgreeting", ResourceType.SIMPLE);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.validationDemos", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.groups.client.groupMemberships", ResourceType.ASSOCIATION);
expectedTypes.put("com.linkedin.restli.examples.groups.client.groupMembershipsComplex", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.groups.client.groups", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.groups.client.groups.contacts", ResourceType.COLLECTION);
expectedTypes.put("noNamespace", ResourceType.COLLECTION);
expectedTypes.put("noNamespace.noNamespaceSub", ResourceType.COLLECTION);
expectedTypes.put("noNamespace.noNamespaceSub.subSub", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.noNamespace.noNamespace", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.typeref.client.typeref", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.manualProjections", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.scala.client.scalaGreetings", ResourceType.COLLECTION);
expectedTypes.put("com.linkedin.restli.examples.greetings.client.asyncErrors", ResourceType.ACTIONS);
for (Map.Entry<String, ResourceSchema> entry : _schemas.getResources().entrySet()) {
final ResourceSchema schema = entry.getValue();
final ResourceType actualType;
if (schema.hasCollection()) {
actualType = ResourceType.COLLECTION;
} else if (schema.hasAssociation()) {
actualType = ResourceType.ASSOCIATION;
} else if (schema.hasSimple()) {
actualType = ResourceType.SIMPLE;
} else {
Assert.assertTrue(schema.hasActionsSet());
actualType = ResourceType.ACTIONS;
}
final String schemaFullName = getResourceSchemaFullName(schema, entry.getKey());
final ResourceType expectedType = expectedTypes.get(schemaFullName);
Assert.assertNotNull(expectedType, "Resource type for " + schemaFullName);
Assert.assertSame(actualType, expectedType, schemaFullName);
}
}
use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.
the class RestLiSnapshotExporter method generateSnapshotFiles.
private GeneratorResult generateSnapshotFiles(String apiName, String outdir, Map<String, ResourceModel> rootResourceMap, DocsProvider docsProvider) throws IOException {
SnapshotResult result = new SnapshotResult();
final File outdirFile = new File(outdir);
if (!outdirFile.exists()) {
if (!outdirFile.mkdirs()) {
throw new RuntimeException("Output directory '" + outdir + "' could not be created!");
}
}
if (!outdirFile.isDirectory()) {
throw new RuntimeException("Output directory '" + outdir + "' is not a directory");
}
if (!outdirFile.canRead() || !outdirFile.canWrite()) {
throw new RuntimeException("Output directory '" + outdir + "' must be writeable");
}
final ResourceModelEncoder encoder = new ResourceModelEncoder(docsProvider);
final List<ResourceSchema> rootResourceNodes = new ArrayList<ResourceSchema>();
for (Map.Entry<String, ResourceModel> entry : rootResourceMap.entrySet()) {
final ResourceSchema rootResourceNode = encoder.buildResourceSchema(entry.getValue());
rootResourceNodes.add(rootResourceNode);
}
for (ResourceSchema rootResourceNode : rootResourceNodes) {
String fileName = rootResourceNode.getName();
if (rootResourceNode.hasNamespace()) {
final String namespace = rootResourceNode.getNamespace();
fileName = namespace + "." + fileName;
}
if (apiName != null && !apiName.isEmpty()) {
fileName = apiName + "-" + fileName;
}
File writtenFile = writeSnapshotFile(outdirFile, fileName, rootResourceNode);
result.addModifiedFile(writtenFile);
result.addTargetFile(writtenFile);
}
return result;
}
use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.
the class RestSpecParser method parseSources.
/**
* @param sourcePaths can be either a directory (all idl files from it) or individual idl files
*/
public ParseResult parseSources(String[] sourcePaths) {
final ParseResult result = new ParseResult();
for (String sourcePath : sourcePaths) {
final File source = new File(sourcePath);
if (!source.exists()) {
result._message.append("IDL file or directory doesn't exist: ").append(source.getAbsolutePath());
} else {
final File[] sources;
if (source.isDirectory()) {
final FileUtil.FileExtensionFilter filter = new FileUtil.FileExtensionFilter(RestConstants.RESOURCE_MODEL_FILENAME_EXTENSION);
final List<File> sourceFilesInDirectory = FileUtil.listFiles(source, filter);
sources = sourceFilesInDirectory.toArray(new File[0]);
} else {
sources = new File[] { source };
}
for (File sourceFile : sources) {
try {
final ResourceSchema resource = _codec.readResourceSchema(new FileInputStream(sourceFile));
result._schemaAndFiles.add(new CodeUtil.Pair<ResourceSchema, File>(resource, sourceFile));
result._sourceFiles.add(sourceFile);
} catch (IOException e) {
result._message.append("Error processing file [").append(sourceFile.getAbsolutePath()).append(']').append(e.getMessage());
}
}
}
}
return result;
}
use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.
the class TestResourceCompatibilityChecker method testPassCollectionFile.
@Test
public void testPassCollectionFile() throws IOException {
final Collection<CompatibilityInfo> resourceTestDiffs = new HashSet<CompatibilityInfo>();
final Collection<CompatibilityInfo> modelTestDiffs = new HashSet<CompatibilityInfo>();
resourceTestDiffs.add(new CompatibilityInfo(Arrays.<Object>asList(""), CompatibilityInfo.Type.OPTIONAL_VALUE, "namespace"));
resourceTestDiffs.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "supports"), CompatibilityInfo.Type.SUPERSET, new HashSet<String>(Arrays.asList("update"))));
resourceTestDiffs.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "methods"), CompatibilityInfo.Type.SUPERSET, new HashSet<String>(Arrays.asList("update"))));
resourceTestDiffs.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "finders", "search", "parameters", "tone"), CompatibilityInfo.Type.OPTIONAL_PARAMETER));
resourceTestDiffs.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "finders", "search", "parameters"), CompatibilityInfo.Type.PARAMETER_NEW_OPTIONAL, "newParam"));
resourceTestDiffs.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "finders", "search", "parameters", "tone"), CompatibilityInfo.Type.DEPRECATED, "The \"items\" field"));
resourceTestDiffs.add(new CompatibilityInfo(Arrays.asList("", "collection", "finders", "search"), CompatibilityInfo.Type.PAGING_ADDED));
resourceTestDiffs.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "actions", "oneAction", "parameters"), CompatibilityInfo.Type.PARAMETER_NEW_OPTIONAL, "newParam"));
resourceTestDiffs.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "actions", "oneAction", "parameters", "bitfield"), CompatibilityInfo.Type.DEPRECATED, "The \"items\" field"));
resourceTestDiffs.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "actions", "oneAction", "parameters", "someString"), CompatibilityInfo.Type.OPTIONAL_PARAMETER));
resourceTestDiffs.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "actions", "exceptionTest", "throws"), CompatibilityInfo.Type.SUPERSET, new HashSet<String>(Arrays.asList("java.lang.NullPointerException"))));
resourceTestDiffs.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "entity", "actions", "someAction", "parameters", "b", "default"), CompatibilityInfo.Type.VALUE_DIFFERENT, "default", "changed"));
resourceTestDiffs.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "finders", "oneFinder", "annotations", "deprecated"), CompatibilityInfo.Type.ANNOTATIONS_CHANGED, "added"));
resourceTestDiffs.add(new CompatibilityInfo(Arrays.<Object>asList("", "collection", "finders", "oneFinder", "parameters", "param1", "annotations", "deprecated"), CompatibilityInfo.Type.ANNOTATIONS_CHANGED, "added"));
modelTestDiffs.add(new CompatibilityInfo(Arrays.<Object>asList("com.linkedin.greetings.api.Greeting"), CompatibilityInfo.Type.TYPE_INFO, "new record added optional fields newField"));
final ResourceSchema prevResource = idlToResource(IDLS_SUFFIX + PREV_COLL_FILE);
final ResourceSchema currResource = idlToResource(IDLS_SUFFIX + CURR_COLL_PASS_FILE);
ResourceCompatibilityChecker checker = new ResourceCompatibilityChecker(prevResource, prevSchemaResolver, currResource, compatSchemaResolver);
boolean check = checker.check(CompatibilityLevel.BACKWARDS);
Assert.assertTrue(check);
final Collection<CompatibilityInfo> resourceIncompatibles = checker.getInfoMap().getRestSpecIncompatibles();
final Collection<CompatibilityInfo> resourceCompatibles = new HashSet<CompatibilityInfo>(checker.getInfoMap().getRestSpecCompatibles());
for (CompatibilityInfo di : resourceTestDiffs) {
Assert.assertTrue(resourceCompatibles.contains(di), "Reported resource compatibles should contain: " + di.toString());
resourceCompatibles.remove(di);
}
Assert.assertTrue(resourceIncompatibles.isEmpty());
Assert.assertTrue(resourceCompatibles.isEmpty());
final Collection<CompatibilityInfo> modelIncompatibles = checker.getInfoMap().getModelIncompatibles();
final Collection<CompatibilityInfo> modelCompatibles = new HashSet<CompatibilityInfo>(checker.getInfoMap().getModelCompatibles());
for (CompatibilityInfo di : modelTestDiffs) {
Assert.assertTrue(modelCompatibles.contains(di), "Reported model compatibles should contain: " + di.toString());
modelCompatibles.remove(di);
}
Assert.assertTrue(modelIncompatibles.isEmpty());
Assert.assertTrue(modelCompatibles.isEmpty());
}
use of com.linkedin.restli.restspec.ResourceSchema 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
}
Aggregations