Search in sources :

Example 11 with ResourceSpec

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

the class TestRequest method testHeadersCaseInsensitiveGet.

@Test
public void testHeadersCaseInsensitiveGet() {
    final long id = 42l;
    final ResourceSpec spec = new ResourceSpecImpl(EnumSet.allOf(ResourceMethod.class), Collections.<String, DynamicRecordMetadata>emptyMap(), Collections.<String, DynamicRecordMetadata>emptyMap(), Long.class, null, null, TestRecord.class, Collections.<String, Class<?>>emptyMap());
    GetRequestBuilder<Long, TestRecord> builder = new GetRequestBuilder<Long, TestRecord>("abc", TestRecord.class, spec, RestliRequestOptions.DEFAULT_OPTIONS);
    Request<TestRecord> request = builder.id(id).addHeader("header", "value").build();
    Assert.assertEquals(request.getHeaders().get("HEADER"), "value");
}
Also used : ResourceSpec(com.linkedin.restli.common.ResourceSpec) ResourceSpecImpl(com.linkedin.restli.common.ResourceSpecImpl) TestRecord(com.linkedin.restli.client.test.TestRecord) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Test(org.testng.annotations.Test)

Example 12 with ResourceSpec

use of com.linkedin.restli.common.ResourceSpec 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 13 with ResourceSpec

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

the class TestClientBuilders method testActionRequestInputIsReadOnly.

@Test
@SuppressWarnings("unchecked")
public void testActionRequestInputIsReadOnly() {
    FieldDef<TestRecord> pParam = new FieldDef<TestRecord>("p", TestRecord.class, DataTemplateUtil.getSchema(TestRecord.class));
    Map<String, DynamicRecordMetadata> requestMetadataMap = new HashMap<String, DynamicRecordMetadata>();
    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<String, DynamicRecordMetadata>();
    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<ComplexResourceKey<TestRecord, TestRecord>, TestRecord>(TEST_URI, TestRecord.class, resourceSpec, RestliRequestOptions.DEFAULT_OPTIONS);
    TestRecord testRecord1 = new TestRecord();
    TestRecord testRecord2 = new TestRecord();
    ComplexResourceKey<TestRecord, TestRecord> key = new ComplexResourceKey<TestRecord, TestRecord>(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 14 with ResourceSpec

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

the class TestClientBuilders method testBuilderPathKeys19.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "simpleSubResourceAction1")
public void testBuilderPathKeys19(URIDetails expectedURIDetails) {
    ResourceSpec resourceSpec = getResourceSpecForBuilderPathKeys();
    Request<TestRecord> request = new ActionRequestBuilder<Void, TestRecord>(SUBRESOURCE_SIMPLE_SUB_URI, TestRecord.class, resourceSpec, RestliRequestOptions.DEFAULT_OPTIONS).name("action").pathKey("key1", 1).build();
    URIDetails.testUriGeneration(request, expectedURIDetails);
    testPathKeys(request, SUBRESOURCE_SIMPLE_SUB_URI, Collections.singletonMap("key1", 1));
}
Also used : ResourceSpec(com.linkedin.restli.common.ResourceSpec) TestRecord(com.linkedin.restli.client.test.TestRecord) Test(org.testng.annotations.Test)

Example 15 with ResourceSpec

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

the class TestClientBuilders method testBuilderPathKeys2.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "subSubResourceAction2")
public void testBuilderPathKeys2(URIDetails expectedURIDetails) {
    ResourceSpec resourceSpec = getResourceSpecForBuilderPathKeys();
    // test with keys containing URL escaped chars
    Request<TestRecord> request = new ActionRequestBuilder<Void, TestRecord>(SUBRESOURCE_URI, TestRecord.class, resourceSpec, RestliRequestOptions.DEFAULT_OPTIONS).name("action").pathKey("key1", "http://example.com/images/1.png").pathKey("key2", "http://example.com/images/2.png").build();
    URIDetails.testUriGeneration(request, expectedURIDetails);
    Map<String, String> pathKeys1 = new HashMap<String, String>();
    pathKeys1.put("key1", "http://example.com/images/1.png");
    pathKeys1.put("key2", "http://example.com/images/2.png");
    testPathKeys(request, SUBRESOURCE_URI, pathKeys1);
}
Also used : HashMap(java.util.HashMap) ResourceSpec(com.linkedin.restli.common.ResourceSpec) ByteString(com.linkedin.data.ByteString) TestRecord(com.linkedin.restli.client.test.TestRecord) Test(org.testng.annotations.Test)

Aggregations

ResourceSpec (com.linkedin.restli.common.ResourceSpec)21 Test (org.testng.annotations.Test)15 TestRecord (com.linkedin.restli.client.test.TestRecord)14 ResourceSpecImpl (com.linkedin.restli.common.ResourceSpecImpl)9 HashMap (java.util.HashMap)7 ByteString (com.linkedin.data.ByteString)5 CompoundKey (com.linkedin.restli.common.CompoundKey)5 ResourceMethod (com.linkedin.restli.common.ResourceMethod)5 DynamicRecordMetadata (com.linkedin.data.template.DynamicRecordMetadata)4 FieldDef (com.linkedin.data.template.FieldDef)4 DataMap (com.linkedin.data.DataMap)2 DynamicRecordTemplate (com.linkedin.data.template.DynamicRecordTemplate)2 ResourcePropertiesImpl (com.linkedin.restli.internal.common.ResourcePropertiesImpl)2 CollectionRequest (com.linkedin.restli.common.CollectionRequest)1 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)1 EmptyRecord (com.linkedin.restli.common.EmptyRecord)1 ResourceProperties (com.linkedin.restli.common.ResourceProperties)1 RichResourceSchema (com.linkedin.restli.common.util.RichResourceSchema)1 RestLiTestAttachmentDataSource (com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource)1 ResourceSchema (com.linkedin.restli.restspec.ResourceSchema)1