use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.
the class ExampleRequestResponseGenerator method addPathKeys.
private void addPathKeys(AbstractRequestBuilder<?, ?, ?> builder) {
for (Map.Entry<ResourceSchema, ResourceSpec> entry : _parentResources.entrySet()) {
ResourceSchema resourceSchema = entry.getKey();
ResourceSpec resourceSpec = entry.getValue();
if (resourceSpec.getKeyType() != null) {
switch(toResourceKeyType(resourceSpec.getKeyType().getType())) {
case PRIMITIVE:
case COMPLEX:
String keyName = resourceSchema.getCollection().getIdentifier().getName();
builder.pathKey(keyName, generateKey(resourceSpec, resourceSchema, null));
break;
case COMPOUND:
// old assocKey version
Map<String, CompoundKey.TypeInfo> keyParts = resourceSpec.getKeyParts();
for (Map.Entry<String, CompoundKey.TypeInfo> infoEntry : keyParts.entrySet()) {
String key = infoEntry.getKey();
CompoundKey.TypeInfo typeInfo = infoEntry.getValue();
builder.pathKey(key, _dataGenerator.buildData(key, typeInfo.getBinding().getSchema()));
}
// new key version
String assocKeyName = resourceSchema.getAssociation().getIdentifier();
builder.pathKey(assocKeyName, generateKey(resourceSpec, resourceSchema, null));
break;
case NONE:
break;
default:
throw new IllegalStateException("Unrecognized key type: " + resourceSpec.getKeyType().getType());
}
}
}
}
use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.
the class ResourceSchemaCollection method createFromIdls.
/**
* Create {@link ResourceSchemaCollection} from idl files.
*
* @param restspecSearchPaths file system paths to search for idl files
* @return constructed ResourceSchemaCollection
*/
public static ResourceSchemaCollection createFromIdls(String[] restspecSearchPaths) {
final RestSpecCodec codec = new RestSpecCodec();
final Map<String, ResourceSchema> resourceSchemaMap = new HashMap<String, ResourceSchema>();
for (String path : restspecSearchPaths) {
final File dir = new File(path);
if (!dir.isDirectory()) {
throw new IllegalArgumentException(String.format("path '%s' is not a directory", dir.getAbsolutePath()));
}
final File[] idlFiles = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(RestConstants.RESOURCE_MODEL_FILENAME_EXTENSION);
}
});
for (File idlFile : idlFiles) {
try {
final FileInputStream is = new FileInputStream(idlFile);
final ResourceSchema resourceSchema = codec.readResourceSchema(is);
resourceSchemaMap.put(resourceSchema.getName(), resourceSchema);
} catch (IOException e) {
throw new RestLiInternalException(String.format("Error loading restspec IDL file '%s'", idlFile.getName()), e);
}
}
}
return new ResourceSchemaCollection(resourceSchemaMap);
}
use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.
the class ResourceSchemaCollection method getAllSubResourcesRecursive.
private List<ResourceSchema> getAllSubResourcesRecursive(ResourceSchema parentSchema, List<ResourceSchema> accumulator) {
final List<ResourceSchema> subResources = getSubResources(parentSchema);
if (subResources == null) {
return null;
}
accumulator.addAll(subResources);
for (ResourceSchema sub : subResources) {
getAllSubResourcesRecursive(sub, accumulator);
}
return accumulator;
}
use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.
the class ResourceSchemaCollection method loadOrCreateResourceSchema.
/**
* For each given {@link ResourceModel}, the classpath is checked for a .restspec.json
* matching the name of the {@link ResourceModel}, if found it is loaded. If a .restspec.json file
* is not found, one is created {@link ResourceSchemaCollection} from specified root {@link ResourceModel}.
* All resources will be recursively traversed to discover subresources.
* Root resources not specified are excluded.
*
* @param rootResources root resources in ResourceModel type
* @return constructed ResourceSchemaCollection
*/
public static ResourceSchemaCollection loadOrCreateResourceSchema(Map<String, ResourceModel> rootResources) {
final ResourceModelEncoder encoder = new ResourceModelEncoder(new NullDocsProvider());
final Map<String, ResourceSchema> schemaMap = new TreeMap<String, ResourceSchema>();
for (ResourceModel resource : rootResources.values()) {
schemaMap.put(resource.getName(), encoder.loadOrBuildResourceSchema(resource));
}
return new ResourceSchemaCollection(schemaMap);
}
use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.
the class TestExamplesGenerator method testExamples.
@Test
public void testExamples() throws IOException {
final Map<String, ResourceModel> resources = buildResourceModels(ActionsResource.class, GreetingsResource.class, GroupsResource2.class, GroupContactsResource2.class, GroupMembershipsResource2.class, RootSimpleResource.class, CollectionUnderSimpleResource.class, SimpleResourceUnderCollectionResource.class, CustomTypesResource.class);
final ResourceSchemaCollection resourceSchemas = ResourceSchemaCollection.loadOrCreateResourceSchema(resources);
final DataSchemaResolver schemaResolver = new ClasspathResourceDataSchemaResolver(SchemaParserFactory.instance());
final ValidationOptions valOptions = new ValidationOptions(RequiredMode.MUST_BE_PRESENT);
ExampleRequestResponse capture;
ValidationResult valRet;
final ResourceSchema greetings = resourceSchemas.getResource("greetings");
ExampleRequestResponseGenerator greetingsGenerator = new ExampleRequestResponseGenerator(greetings, schemaResolver);
final ResourceSchema groups = resourceSchemas.getResource("groups");
ExampleRequestResponseGenerator groupsGenerator = new ExampleRequestResponseGenerator(groups, schemaResolver);
final ResourceSchema groupsContacts = resourceSchemas.getResource("groups.contacts");
ExampleRequestResponseGenerator groupsContactsGenerator = new ExampleRequestResponseGenerator(Collections.singletonList(groups), groupsContacts, schemaResolver);
final ResourceSchema greeting = resourceSchemas.getResource("greeting");
ExampleRequestResponseGenerator greetingGenerator = new ExampleRequestResponseGenerator(greeting, schemaResolver);
final ResourceSchema actions = resourceSchemas.getResource("actions");
ExampleRequestResponseGenerator actionsGenerator = new ExampleRequestResponseGenerator(actions, schemaResolver);
final ResourceSchema customTypes = resourceSchemas.getResource("customTypes");
ExampleRequestResponseGenerator customTypesGenerator = new ExampleRequestResponseGenerator(customTypes, schemaResolver);
List<ResourceSchema> subResources = resourceSchemas.getSubResources(greeting);
final ResourceSchema subgreetings = subResources.get(0);
ExampleRequestResponseGenerator subgreetingsGenerator = new ExampleRequestResponseGenerator(Collections.singletonList(greeting), subgreetings, schemaResolver);
subResources = resourceSchemas.getSubResources(subgreetings);
final ResourceSchema subsubgreeting = subResources.get(0);
ExampleRequestResponseGenerator subsubgreetingGenerator = new ExampleRequestResponseGenerator(Arrays.asList(greeting, subgreetings), subsubgreeting, schemaResolver);
capture = greetingsGenerator.method(ResourceMethod.GET);
valRet = validateSingleResponse(capture.getResponse(), Greeting.class, valOptions);
Assert.assertTrue(valRet.isValid(), valRet.getMessages().toString());
capture = greetingsGenerator.method(ResourceMethod.CREATE);
Assert.assertSame(capture.getResponse().getEntity().length(), 0);
capture = greetingsGenerator.finder("search");
valRet = validateCollectionResponse(capture.getResponse(), Greeting.class, valOptions);
Assert.assertNull(valRet, (valRet == null ? null : valRet.getMessages().toString()));
capture = groupsContactsGenerator.method(ResourceMethod.GET);
valRet = validateSingleResponse(capture.getResponse(), GroupContact.class, valOptions);
Assert.assertTrue(valRet.isValid(), valRet.getMessages().toString());
capture = groupsGenerator.finder("search");
String queryString = capture.getRequest().getURI().getQuery();
Assert.assertTrue(queryString.contains("q=search"));
Assert.assertTrue(queryString.contains("keywords="));
Assert.assertTrue(queryString.contains("nameKeywords="));
Assert.assertTrue(queryString.contains("groupID="));
valRet = validateCollectionResponse(capture.getResponse(), Group.class, valOptions);
Assert.assertNull(valRet, (valRet == null ? null : valRet.getMessages().toString()));
capture = greetingsGenerator.action("purge", ResourceLevel.COLLECTION);
final DataMap purgeResponse = DataMapUtils.readMap(capture.getResponse());
Assert.assertTrue(purgeResponse.containsKey("value"));
capture = greetingsGenerator.action("updateTone", ResourceLevel.ENTITY);
valRet = validateCollectionResponse(capture.getResponse(), Greeting.class, valOptions);
Assert.assertNull(valRet, (valRet == null ? null : valRet.getMessages().toString()));
capture = groupsGenerator.action("sendTestAnnouncement", ResourceLevel.ENTITY);
Assert.assertSame(capture.getResponse().getEntity().length(), 0);
capture = greetingGenerator.method(ResourceMethod.GET);
valRet = validateSingleResponse(capture.getResponse(), Greeting.class, valOptions);
Assert.assertTrue(valRet.isValid(), valRet.getMessages().toString());
RestRequest request = capture.getRequest();
Assert.assertEquals(request.getURI(), URI.create("/greeting"));
capture = greetingGenerator.method(ResourceMethod.UPDATE);
Assert.assertSame(capture.getResponse().getEntity().length(), 0);
request = capture.getRequest();
Assert.assertEquals(request.getURI(), URI.create("/greeting"));
valRet = validateSingleRequest(capture.getRequest(), Greeting.class, valOptions);
Assert.assertTrue(valRet.isValid(), valRet.getMessages().toString());
capture = greetingGenerator.method(ResourceMethod.PARTIAL_UPDATE);
Assert.assertSame(capture.getResponse().getEntity().length(), 0);
request = capture.getRequest();
Assert.assertEquals(request.getURI(), URI.create("/greeting"));
DataMap patchMap = _codec.bytesToMap(capture.getRequest().getEntity().copyBytes());
checkPatchMap(patchMap);
capture = greetingGenerator.method(ResourceMethod.DELETE);
Assert.assertSame(capture.getResponse().getEntity().length(), 0);
request = capture.getRequest();
Assert.assertEquals(request.getURI(), URI.create("/greeting"));
capture = greetingGenerator.action("exampleAction", ResourceLevel.ENTITY);
DataMap exampleActionResponse = DataMapUtils.readMap(capture.getResponse());
Assert.assertTrue(exampleActionResponse.containsKey("value"));
request = capture.getRequest();
Assert.assertTrue(validateUrlPath(request.getURI(), new String[] { "greeting" }));
capture = subgreetingsGenerator.method(ResourceMethod.CREATE);
Assert.assertSame(capture.getResponse().getEntity().length(), 0);
request = capture.getRequest();
Assert.assertEquals(request.getURI(), URI.create("/greeting/subgreetings"));
valRet = validateSingleRequest(capture.getRequest(), Greeting.class, valOptions);
Assert.assertTrue(valRet.isValid(), valRet.getMessages().toString());
capture = subsubgreetingGenerator.method(ResourceMethod.GET);
valRet = validateSingleResponse(capture.getResponse(), Greeting.class, valOptions);
Assert.assertTrue(valRet.isValid(), valRet.getMessages().toString());
request = capture.getRequest();
Assert.assertTrue(validateUrlPath(request.getURI(), new String[] { "greeting", "subgreetings", null, "subsubgreeting" }));
capture = subsubgreetingGenerator.method(ResourceMethod.UPDATE);
Assert.assertSame(capture.getResponse().getEntity().length(), 0);
request = capture.getRequest();
Assert.assertTrue(validateUrlPath(request.getURI(), new String[] { "greeting", "subgreetings", null, "subsubgreeting" }));
valRet = validateSingleRequest(capture.getRequest(), Greeting.class, valOptions);
Assert.assertTrue(valRet.isValid(), valRet.getMessages().toString());
capture = subsubgreetingGenerator.method(ResourceMethod.PARTIAL_UPDATE);
Assert.assertSame(capture.getResponse().getEntity().length(), 0);
request = capture.getRequest();
Assert.assertTrue(validateUrlPath(request.getURI(), new String[] { "greeting", "subgreetings", null, "subsubgreeting" }));
patchMap = _codec.bytesToMap(capture.getRequest().getEntity().copyBytes());
checkPatchMap(patchMap);
capture = subsubgreetingGenerator.method(ResourceMethod.DELETE);
Assert.assertSame(capture.getResponse().getEntity().length(), 0);
request = capture.getRequest();
Assert.assertTrue(validateUrlPath(request.getURI(), new String[] { "greeting", "subgreetings", null, "subsubgreeting" }));
capture = subsubgreetingGenerator.action("exampleAction", ResourceLevel.ENTITY);
exampleActionResponse = DataMapUtils.readMap(capture.getResponse());
Assert.assertTrue(exampleActionResponse.containsKey("value"));
request = capture.getRequest();
Assert.assertTrue(validateUrlPath(request.getURI(), new String[] { "greeting", "subgreetings", null, "subsubgreeting" }));
capture = subgreetingsGenerator.finder("search");
queryString = capture.getRequest().getURI().getQuery();
Assert.assertTrue(queryString.contains("q=search"), queryString);
Assert.assertTrue(queryString.contains("id:"), queryString);
Assert.assertTrue(queryString.contains("message:"), queryString);
Assert.assertTrue(queryString.contains("tone:"), queryString);
capture = actionsGenerator.action("echoMessageArray", ResourceLevel.COLLECTION);
final DataMap echoMessageArrayResponse = DataMapUtils.readMap(capture.getResponse());
Assert.assertTrue(echoMessageArrayResponse.containsKey("value"));
capture = actionsGenerator.action("echoToneArray", ResourceLevel.COLLECTION);
final DataMap echoToneArrayResponse = DataMapUtils.readMap(capture.getResponse());
Assert.assertTrue(echoToneArrayResponse.containsKey("value"));
capture = actionsGenerator.action("echoStringArray", ResourceLevel.COLLECTION);
final DataMap echoStringArrayResponse = DataMapUtils.readMap(capture.getResponse());
Assert.assertTrue(echoStringArrayResponse.containsKey("value"));
capture = customTypesGenerator.action("action", ResourceLevel.COLLECTION);
DataMap requestMap = _codec.bytesToMap(capture.getRequest().getEntity().copyBytes());
Assert.assertTrue(requestMap.containsKey("l"));
Assert.assertEquals(requestMap.size(), 1);
final DataMap customTypesActionResponse = DataMapUtils.readMap(capture.getResponse());
Assert.assertTrue(customTypesActionResponse.containsKey("value"));
Assert.assertEquals(customTypesActionResponse.size(), 1);
}
Aggregations