Search in sources :

Example 1 with DynamicRecordTemplate

use of com.linkedin.data.template.DynamicRecordTemplate in project rest.li by linkedin.

the class ActionArgumentBuilder method extractRequestData.

@Override
public RestLiRequestData extractRequestData(RoutingResult routingResult, RestRequest request) {
    ResourceMethodDescriptor resourceMethodDescriptor = routingResult.getResourceMethod();
    final DataMap data;
    if (request.getEntity() == null || request.getEntity().length() == 0) {
        data = new DataMap();
    } else {
        data = DataMapUtils.readMap(request);
    }
    DynamicRecordTemplate template = new DynamicRecordTemplate(data, resourceMethodDescriptor.getRequestDataSchema());
    ValidationResult result = ValidateDataAgainstSchema.validate(data, template.schema(), new ValidationOptions(RequiredMode.IGNORE, CoercionMode.NORMAL));
    if (!result.isValid()) {
        throw new RoutingException("Parameters of method '" + resourceMethodDescriptor.getActionName() + "' failed validation with error '" + result.getMessages() + "'", HttpStatus.S_400_BAD_REQUEST.getCode());
    }
    return new RestLiRequestDataImpl.Builder().entity(template).build();
}
Also used : DynamicRecordTemplate(com.linkedin.data.template.DynamicRecordTemplate) RoutingException(com.linkedin.restli.server.RoutingException) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) DataMap(com.linkedin.data.DataMap)

Example 2 with DynamicRecordTemplate

use of com.linkedin.data.template.DynamicRecordTemplate in project rest.li by linkedin.

the class TestArgumentBuilder method testBuildOptionalArg.

// utility method for reuse
private Object[] testBuildOptionalArg(Parameter<?> param) {
    String paramKey = param.getName();
    Class<?> dataType = param.getType();
    Parameter.ParamType paramType = param.getParamType();
    // mock resource context
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    DynamicRecordTemplate template = null;
    if (paramType == Parameter.ParamType.POST) {
        template = new DynamicRecordTemplate(new DataMap(), null);
    } else {
        MutablePathKeys mockPathKeys = EasyMock.createMock(MutablePathKeys.class);
        EasyMock.expect(mockPathKeys.get(paramKey)).andReturn(null);
        EasyMock.expect(mockResourceContext.getPathKeys()).andReturn(mockPathKeys);
        EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null);
        EasyMock.expect(mockResourceContext.hasParameter(paramKey)).andReturn(false);
        if (DataTemplate.class.isAssignableFrom(dataType)) {
            EasyMock.expect(mockResourceContext.getStructuredParameter(paramKey)).andReturn(null);
        } else {
            EasyMock.expect(mockResourceContext.getParameter(paramKey)).andReturn(null);
        }
        EasyMock.replay(mockResourceContext);
    }
    // invoke buildArgs
    List<Parameter<?>> parameters = Collections.singletonList(param);
    return ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, template, getMockResourceMethodConfig(false));
}
Also used : DynamicRecordTemplate(com.linkedin.data.template.DynamicRecordTemplate) MutablePathKeys(com.linkedin.restli.internal.server.MutablePathKeys) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) Parameter(com.linkedin.restli.internal.server.model.Parameter) DataMap(com.linkedin.data.DataMap)

Example 3 with DynamicRecordTemplate

use of com.linkedin.data.template.DynamicRecordTemplate in project rest.li by linkedin.

the class TestClientBuilders method testActionRequestBuilder.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "action")
public void testActionRequestBuilder(URIDetails expectedURIDetails) {
    FieldDef<String> pParam = new FieldDef<>("p", String.class, DataTemplateUtil.getSchema(String.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, Long.class, TestRecord.class, Collections.<String, CompoundKey.TypeInfo>emptyMap());
    ActionRequestBuilder<Long, TestRecord> builder = new ActionRequestBuilder<>(TEST_URI, TestRecord.class, resourceSpec, RestliRequestOptions.DEFAULT_OPTIONS);
    ActionRequest<TestRecord> request = builder.name("action").setParam(pParam, "42").id(1L).appendSingleAttachment(_dataSourceWriterA).appendMultipleAttachments(_dataSourceIterator).appendSingleAttachment(_dataSourceWriterB).build();
    DataMap d = new DataMap();
    d.put("p", "42");
    @SuppressWarnings("unchecked") DynamicRecordTemplate expectedRecordTemplate = new DynamicRecordTemplate(d, DynamicRecordMetadata.buildSchema("action", Arrays.asList(new FieldDef<>("p", String.class, DataTemplateUtil.getSchema(String.class)))));
    URIDetails.testUriGeneration(request, expectedURIDetails);
    Assert.assertEquals(request.getMethod(), ResourceMethod.ACTION);
    Assert.assertEquals(request.getHeaders(), Collections.<String, String>emptyMap());
    Assert.assertEquals(request.getInputRecord(), expectedRecordTemplate);
    Assert.assertEquals(request.isSafe(), false);
    Assert.assertEquals(request.isIdempotent(), false);
    Assert.assertEquals(request.getStreamingAttachments(), _streamingDataSources);
    try {
        request.getStreamingAttachments().add(new RestLiTestAttachmentDataSource("1", ByteString.empty()));
        Assert.fail("Should not be able to add to an immutable list");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof UnsupportedOperationException);
    }
    Assert.assertEquals(request.getResponseDecoder().getEntityClass(), Void.class);
}
Also used : HashMap(java.util.HashMap) CompoundKey(com.linkedin.restli.common.CompoundKey) ResourceSpec(com.linkedin.restli.common.ResourceSpec) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) FieldDef(com.linkedin.data.template.FieldDef) DynamicRecordMetadata(com.linkedin.data.template.DynamicRecordMetadata) DynamicRecordTemplate(com.linkedin.data.template.DynamicRecordTemplate) RestLiTestAttachmentDataSource(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource) ResourceSpecImpl(com.linkedin.restli.common.ResourceSpecImpl) TestRecord(com.linkedin.restli.client.test.TestRecord) Test(org.testng.annotations.Test)

Example 4 with DynamicRecordTemplate

use of com.linkedin.data.template.DynamicRecordTemplate 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 5 with DynamicRecordTemplate

use of com.linkedin.data.template.DynamicRecordTemplate in project rest.li by linkedin.

the class TestArgumentBuilder method testPostParameterType.

@Test
public void testPostParameterType() {
    String testParamKey = "testParam";
    String expectedTestParamValue = "testParamValue";
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    DataMap entityBody = new DataMap();
    entityBody.put(testParamKey, expectedTestParamValue);
    DynamicRecordTemplate template = new DynamicRecordTemplate(entityBody, null);
    Parameter<String> param = new Parameter<>(testParamKey, String.class, DataSchemaConstants.STRING_DATA_SCHEMA, false, null, Parameter.ParamType.POST, false, AnnotationSet.EMPTY);
    List<Parameter<?>> parameters = Collections.singletonList(param);
    EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null);
    Object[] results = ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, template, getMockResourceMethodConfig(false));
    Assert.assertEquals(results[0], expectedTestParamValue);
}
Also used : DynamicRecordTemplate(com.linkedin.data.template.DynamicRecordTemplate) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) Parameter(com.linkedin.restli.internal.server.model.Parameter) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Aggregations

DynamicRecordTemplate (com.linkedin.data.template.DynamicRecordTemplate)9 DataMap (com.linkedin.data.DataMap)5 FieldDef (com.linkedin.data.template.FieldDef)5 Test (org.testng.annotations.Test)5 HashMap (java.util.HashMap)4 ByteString (com.linkedin.data.ByteString)2 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)2 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)2 DynamicRecordMetadata (com.linkedin.data.template.DynamicRecordMetadata)2 TestRecord (com.linkedin.restli.client.test.TestRecord)2 CompoundKey (com.linkedin.restli.common.CompoundKey)2 ResourceSpec (com.linkedin.restli.common.ResourceSpec)2 ResourceSpecImpl (com.linkedin.restli.common.ResourceSpecImpl)2 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)2 Parameter (com.linkedin.restli.internal.server.model.Parameter)2 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)2 RoutingException (com.linkedin.restli.server.RoutingException)2 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)1 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)1 TransferOwnershipRequest (com.linkedin.restli.examples.groups.api.TransferOwnershipRequest)1