Search in sources :

Example 16 with ResourceSpecImpl

use of com.linkedin.restli.common.ResourceSpecImpl in project rest.li by linkedin.

the class ResourceSchemaToResourceSpecTranslator method buildResourceSpec.

private ResourceSpec buildResourceSpec(StringArray supports, TypeSpec<?> key, ComplexKeySpec<?, ?> complexKeyType, Map<String, ?> keyParts, String entitySchemaString, ActionSchemaArray actions, ActionSchemaArray entityActions) {
    Set<ResourceMethod> supportedMethods = toResourceMethods(supports);
    ActionCollectionMetadata actionCollectionMetadata = toDynamicRecordMetadata(actions, entityActions);
    Map<String, DynamicRecordMetadata> actionRequestMetadata = actionCollectionMetadata._request;
    Map<String, DynamicRecordMetadata> actionResponseMetadata = actionCollectionMetadata._response;
    DataSchema entitySchema = entitySchemaString == null ? null : RestSpecCodec.textToSchema(entitySchemaString, _schemaResolver);
    TypeSpec<? extends RecordTemplate> value = toValueType(entitySchema);
    return new ResourceSpecImpl(supportedMethods, actionRequestMetadata, actionResponseMetadata, key, complexKeyType, value, keyParts);
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) DynamicRecordMetadata(com.linkedin.data.template.DynamicRecordMetadata) ResourceSpecImpl(com.linkedin.restli.common.ResourceSpecImpl) ResourceMethod(com.linkedin.restli.common.ResourceMethod)

Example 17 with ResourceSpecImpl

use of com.linkedin.restli.common.ResourceSpecImpl in project rest.li by linkedin.

the class TestCharacterEncoding method testQueryParamValueEncoding.

@Test(dataProvider = RESTLI_PROTOCOL_1_2_PREFIX + "protocolVersionsAndAcceptTypes")
public void testQueryParamValueEncoding(ProtocolVersion protocolVersion, String acceptType, DataCodec codec) {
    RestLiConfig config = new RestLiConfig();
    config.setResourcePackageNames(QueryParamMockCollection.class.getPackage().getName());
    if (acceptType != null) {
        config.addCustomContentType(acceptType, codec);
    }
    RestLiServer server = new RestLiServer(config, new PrototypeResourceFactory(), null);
    for (char c = 0; c < 256; ++c) {
        final String testValue = String.valueOf(c);
        GetRequest<EmptyRecord> req = new GetRequestBuilder<String, EmptyRecord>(QueryParamMockCollection.RESOURCE_NAME, EmptyRecord.class, new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), Collections.<String, DynamicRecordMetadata>emptyMap(), Collections.<String, DynamicRecordMetadata>emptyMap(), String.class, null, null, EmptyRecord.class, Collections.<String, CompoundKey.TypeInfo>emptyMap()), RestliRequestOptions.DEFAULT_OPTIONS).id("dummy").setParam(QueryParamMockCollection.VALUE_KEY, testValue).build();
        RestRequest restRequest = new RestRequestBuilder(RestliUriBuilderUtil.createUriBuilder(req, protocolVersion).build()).setMethod(req.getMethod().getHttpMethod().toString()).addHeaderValue(RestConstants.HEADER_ACCEPT, acceptType).build();
        // N.B. since QueryParamMockCollection is implemented using the synchronous rest.li interface,
        // RestLiServer.handleRequest() will invoke the application resource *and* the callback
        // *synchronously*, ensuring that the all instances of the callback are invoked before the
        // loop terminates.
        server.handleRequest(restRequest, new RequestContext(), new Callback<RestResponse>() {

            @Override
            public void onError(Throwable e) {
                Assert.fail();
            }

            @Override
            public void onSuccess(RestResponse result) {
                try {
                    DataMap data = codec.readMap(result.getEntity().asInputStream());
                    Assert.assertEquals(data.get(QueryParamMockCollection.VALUE_KEY), testValue);
                    Assert.assertEquals(QueryParamMockCollection._lastQueryParamValue, testValue);
                    if (acceptType != null) {
                        Assert.assertEquals(result.getHeader(RestConstants.HEADER_CONTENT_TYPE), acceptType);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    Assert.fail();
                }
            }
        });
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) RestLiServer(com.linkedin.restli.server.RestLiServer) CompoundKey(com.linkedin.restli.common.CompoundKey) RestResponse(com.linkedin.r2.message.rest.RestResponse) IOException(java.io.IOException) DataMap(com.linkedin.data.DataMap) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) PrototypeResourceFactory(com.linkedin.restli.server.resources.PrototypeResourceFactory) ResourceSpecImpl(com.linkedin.restli.common.ResourceSpecImpl) RestLiConfig(com.linkedin.restli.server.RestLiConfig) Test(org.testng.annotations.Test)

Example 18 with ResourceSpecImpl

use of com.linkedin.restli.common.ResourceSpecImpl in project rest.li by linkedin.

the class TestClientBuilders method testActionRequestInputIsReadOnly.

@Test
@SuppressWarnings("unchecked")
public void testActionRequestInputIsReadOnly() {
    FieldDef<TestRecord> pParam = new FieldDef<>("p", TestRecord.class, DataTemplateUtil.getSchema(TestRecord.class));
    Map<String, DynamicRecordMetadata> requestMetadataMap = new HashMap<>();
    DynamicRecordMetadata requestMetadata = new DynamicRecordMetadata("action", Collections.<FieldDef<?>>singleton(pParam));
    requestMetadataMap.put("action", requestMetadata);
    DynamicRecordMetadata responseMetadata = new DynamicRecordMetadata("action", Collections.<FieldDef<?>>emptyList());
    Map<String, DynamicRecordMetadata> responseMetadataMap = new HashMap<>();
    responseMetadataMap.put("action", responseMetadata);
    ResourceSpec resourceSpec = new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), requestMetadataMap, responseMetadataMap, ComplexResourceKey.class, TestRecord.class, TestRecord.class, TestRecord.class, Collections.<String, CompoundKey.TypeInfo>emptyMap());
    ActionRequestBuilder<ComplexResourceKey<TestRecord, TestRecord>, TestRecord> builder = new ActionRequestBuilder<>(TEST_URI, TestRecord.class, resourceSpec, RestliRequestOptions.DEFAULT_OPTIONS);
    TestRecord testRecord1 = new TestRecord();
    TestRecord testRecord2 = new TestRecord();
    ComplexResourceKey<TestRecord, TestRecord> key = new ComplexResourceKey<>(testRecord1, testRecord2);
    ActionRequest<TestRecord> request = builder.name("action").setParam(pParam, testRecord1).id(key).build();
    DynamicRecordTemplate inputParams = (DynamicRecordTemplate) request.getInputRecord();
    Assert.assertNotSame(inputParams.getValue(pParam).data(), testRecord1.data());
    Assert.assertTrue(inputParams.data().isReadOnly());
    Assert.assertTrue(inputParams.getValue(pParam).data().isMadeReadOnly());
    Assert.assertNotSame(request.getId(), key);
    Assert.assertTrue(((ComplexResourceKey<TestRecord, TestRecord>) request.getId()).isReadOnly());
    testRecord1.data().makeReadOnly();
    testRecord2.data().makeReadOnly();
    request = builder.build();
    inputParams = (DynamicRecordTemplate) request.getInputRecord();
    Assert.assertSame(inputParams.getValue(pParam).data(), testRecord1.data());
    Assert.assertTrue(inputParams.data().isReadOnly());
    Assert.assertSame(request.getId(), key);
}
Also used : HashMap(java.util.HashMap) CompoundKey(com.linkedin.restli.common.CompoundKey) ResourceSpec(com.linkedin.restli.common.ResourceSpec) ByteString(com.linkedin.data.ByteString) FieldDef(com.linkedin.data.template.FieldDef) DynamicRecordMetadata(com.linkedin.data.template.DynamicRecordMetadata) DynamicRecordTemplate(com.linkedin.data.template.DynamicRecordTemplate) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) TestRecord(com.linkedin.restli.client.test.TestRecord) ResourceSpecImpl(com.linkedin.restli.common.ResourceSpecImpl) Test(org.testng.annotations.Test)

Example 19 with ResourceSpecImpl

use of com.linkedin.restli.common.ResourceSpecImpl in project rest.li by linkedin.

the class TestRequest method testResourceProperties.

@Test
public void testResourceProperties() {
    Set<ResourceMethod> expectedSupportedMethods = new HashSet<>();
    expectedSupportedMethods.add(ResourceMethod.GET);
    expectedSupportedMethods.add(ResourceMethod.BATCH_PARTIAL_UPDATE);
    ResourceSpec expectedResourceSpec = new ResourceSpecImpl(expectedSupportedMethods, null, null, ComplexResourceKey.class, TestRecord.class, TestRecord.class, TestRecord.class, Collections.<String, Object>emptyMap());
    Map<String, Object> pathKeys = new HashMap<>();
    pathKeys.put("id", new ComplexResourceKey<>(new TestRecord(), new TestRecord()));
    Request<TestRecord> request = new Request<>(ResourceMethod.GET, null, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList(), new EntityResponseDecoder<>(TestRecord.class), expectedResourceSpec, Collections.<String, Object>emptyMap(), Collections.<String, Class<?>>emptyMap(), null, "testRecord", pathKeys, RestliRequestOptions.DEFAULT_OPTIONS, null);
    ResourceProperties expectedResourceProperties = new ResourcePropertiesImpl(expectedResourceSpec.getSupportedMethods(), expectedResourceSpec.getKeyType(), expectedResourceSpec.getComplexKeyType(), expectedResourceSpec.getValueType(), expectedResourceSpec.getKeyParts());
    Assert.assertEquals(request.getResourceProperties(), expectedResourceProperties);
}
Also used : HashMap(java.util.HashMap) ResourcePropertiesImpl(com.linkedin.restli.internal.common.ResourcePropertiesImpl) ResourceSpec(com.linkedin.restli.common.ResourceSpec) ResourceProperties(com.linkedin.restli.common.ResourceProperties) ResourceSpecImpl(com.linkedin.restli.common.ResourceSpecImpl) TestRecord(com.linkedin.restli.client.test.TestRecord) ResourceMethod(com.linkedin.restli.common.ResourceMethod) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 20 with ResourceSpecImpl

use of com.linkedin.restli.common.ResourceSpecImpl in project rest.li by linkedin.

the class TestClientBuilders method getResourceSpecForBuilderPathKeys.

private ResourceSpec getResourceSpecForBuilderPathKeys() {
    List<FieldDef<?>> fieldDefs = new ArrayList<>();
    fieldDefs.add(new FieldDef<>("key1", Integer.class, DataTemplateUtil.getSchema(Integer.class)));
    fieldDefs.add(new FieldDef<>("key2", Integer.class, DataTemplateUtil.getSchema(Integer.class)));
    DynamicRecordMetadata requestMetadata = new DynamicRecordMetadata("action", fieldDefs);
    Map<String, DynamicRecordMetadata> requestMetadataMap = new HashMap<>();
    requestMetadataMap.put("action", requestMetadata);
    DynamicRecordMetadata responseMetadata = new DynamicRecordMetadata("action", Collections.<FieldDef<?>>emptyList());
    Map<String, DynamicRecordMetadata> responseMetadataMap = new HashMap<>();
    responseMetadataMap.put("action", responseMetadata);
    return new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), requestMetadataMap, responseMetadataMap);
}
Also used : FieldDef(com.linkedin.data.template.FieldDef) DynamicRecordMetadata(com.linkedin.data.template.DynamicRecordMetadata) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ByteString(com.linkedin.data.ByteString) ResourceSpecImpl(com.linkedin.restli.common.ResourceSpecImpl)

Aggregations

ResourceSpecImpl (com.linkedin.restli.common.ResourceSpecImpl)25 Test (org.testng.annotations.Test)22 TestRecord (com.linkedin.restli.client.test.TestRecord)19 ResourceMethod (com.linkedin.restli.common.ResourceMethod)17 DynamicRecordMetadata (com.linkedin.data.template.DynamicRecordMetadata)14 ResourceSpec (com.linkedin.restli.common.ResourceSpec)9 CompoundKey (com.linkedin.restli.common.CompoundKey)7 HashMap (java.util.HashMap)6 ByteString (com.linkedin.data.ByteString)4 FieldDef (com.linkedin.data.template.FieldDef)4 DataMap (com.linkedin.data.DataMap)3 EmptyRecord (com.linkedin.restli.common.EmptyRecord)3 DynamicRecordTemplate (com.linkedin.data.template.DynamicRecordTemplate)2 RequestContext (com.linkedin.r2.message.RequestContext)2 RestRequest (com.linkedin.r2.message.rest.RestRequest)2 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)2 RestResponse (com.linkedin.r2.message.rest.RestResponse)2 ResourcePropertiesImpl (com.linkedin.restli.internal.common.ResourcePropertiesImpl)2 RestLiConfig (com.linkedin.restli.server.RestLiConfig)2 RestLiServer (com.linkedin.restli.server.RestLiServer)2