Search in sources :

Example 1 with ResourceSchema

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());
            }
        }
    }
}
Also used : RichResourceSchema(com.linkedin.restli.common.util.RichResourceSchema) ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) CompoundKey(com.linkedin.restli.common.CompoundKey) ResourceSpec(com.linkedin.restli.common.ResourceSpec) Map(java.util.Map) DataMap(com.linkedin.data.DataMap) HashMap(java.util.HashMap)

Example 2 with ResourceSchema

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);
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) RestSpecCodec(com.linkedin.restli.restspec.RestSpecCodec) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) FileFilter(java.io.FileFilter) File(java.io.File)

Example 3 with ResourceSchema

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;
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema)

Example 4 with ResourceSchema

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);
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) ResourceModelEncoder(com.linkedin.restli.internal.server.model.ResourceModelEncoder) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) TreeMap(java.util.TreeMap) NullDocsProvider(com.linkedin.restli.internal.server.model.ResourceModelEncoder.NullDocsProvider)

Example 5 with ResourceSchema

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);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) Group(com.linkedin.restli.examples.groups.api.Group) ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) ExampleRequestResponse(com.linkedin.restli.docgen.examplegen.ExampleRequestResponse) ByteString(com.linkedin.data.ByteString) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) GroupContact(com.linkedin.restli.examples.groups.api.GroupContact) DataMap(com.linkedin.data.DataMap) ExampleRequestResponseGenerator(com.linkedin.restli.docgen.examplegen.ExampleRequestResponseGenerator) RestRequest(com.linkedin.r2.message.rest.RestRequest) ClasspathResourceDataSchemaResolver(com.linkedin.data.schema.resolver.ClasspathResourceDataSchemaResolver) DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) ClasspathResourceDataSchemaResolver(com.linkedin.data.schema.resolver.ClasspathResourceDataSchemaResolver) Test(org.testng.annotations.Test)

Aggregations

ResourceSchema (com.linkedin.restli.restspec.ResourceSchema)35 Test (org.testng.annotations.Test)13 DataMap (com.linkedin.data.DataMap)9 HashSet (java.util.HashSet)9 CompatibilityInfo (com.linkedin.restli.tools.idlcheck.CompatibilityInfo)8 IOException (java.io.IOException)7 HashMap (java.util.HashMap)6 File (java.io.File)5 StringArray (com.linkedin.data.template.StringArray)4 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 CodeUtil (com.linkedin.pegasus.generator.CodeUtil)3 RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)3 ResourceModelEncoder (com.linkedin.restli.internal.server.model.ResourceModelEncoder)3 FileInputStream (java.io.FileInputStream)3 DataSchema (com.linkedin.data.schema.DataSchema)2 DataSchemaResolver (com.linkedin.data.schema.DataSchemaResolver)2 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)2 RecordTemplate (com.linkedin.data.template.RecordTemplate)2