Search in sources :

Example 16 with CreateResponse

use of com.linkedin.restli.server.CreateResponse in project rest.li by linkedin.

the class ComplexKeysResource method batchCreate.

@Override
public BatchCreateResult<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchCreate(final BatchCreateRequest<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> entities) {
    List<CreateResponse> createResponses = new ArrayList<CreateResponse>(entities.getInput().size());
    for (Message message : entities.getInput()) {
        ComplexResourceKey<TwoPartKey, TwoPartKey> key = _dataProvider.create(message);
        CreateResponse createResponse = new CreateResponse(key);
        createResponses.add(createResponse);
    }
    return new BatchCreateResult<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message>(createResponses);
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) Message(com.linkedin.restli.examples.greetings.api.Message) CreateResponse(com.linkedin.restli.server.CreateResponse) ArrayList(java.util.ArrayList) BatchCreateResult(com.linkedin.restli.server.BatchCreateResult)

Example 17 with CreateResponse

use of com.linkedin.restli.server.CreateResponse in project rest.li by linkedin.

the class ValidationDemoResource method batchCreate.

@RestMethod.BatchCreate
public BatchCreateResult<Integer, ValidationDemo> batchCreate(final BatchCreateRequest<Integer, ValidationDemo> entities, @ValidatorParam RestLiDataValidator validator) {
    List<CreateResponse> results = new ArrayList<CreateResponse>();
    int id = 0;
    for (ValidationDemo entity : entities.getInput()) {
        ValidationResult result = validator.validateInput(entity);
        if (result.isValid()) {
            results.add(new CreateResponse(id));
            id++;
        } else {
            results.add(new CreateResponse(new RestLiServiceException(HttpStatus.S_422_UNPROCESSABLE_ENTITY, result.getMessages().toString())));
        }
    }
    return new BatchCreateResult<Integer, ValidationDemo>(results);
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) CreateResponse(com.linkedin.restli.server.CreateResponse) ArrayList(java.util.ArrayList) BatchCreateResult(com.linkedin.restli.server.BatchCreateResult) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo)

Example 18 with CreateResponse

use of com.linkedin.restli.server.CreateResponse in project rest.li by linkedin.

the class TestRestLiResponseHandler method testBasicResponses.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "statusData")
public void testBasicResponses(AcceptTypeData acceptTypeData, String expectedStatus, ProtocolVersion protocolVersion, String errorResponseHeaderName, String idHeaderName) throws Exception {
    RestResponse response;
    // #1 simple record template
    response = invokeResponseHandler("/test", buildStatusRecord(), ResourceMethod.GET, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 200, 2, acceptTypeData.responseContentType, Status.class.getName(), null, true, errorResponseHeaderName);
    assertEquals(response.getEntity().asAvroString(), expectedStatus);
    // #2 create (with id)
    response = invokeResponseHandler("/test", new CreateResponse(1), ResourceMethod.CREATE, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 201, 3, null, null, null, false, errorResponseHeaderName);
    assertEquals(response.getHeader(RestConstants.HEADER_LOCATION), "/test/1");
    assertEquals(response.getHeader(idHeaderName), "1");
    // #2.1 create (without id)
    response = invokeResponseHandler("/test", new CreateResponse(HttpStatus.S_201_CREATED), ResourceMethod.CREATE, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 201, 1, null, null, null, false, errorResponseHeaderName);
    // #2.2 create (with id and slash at the end of uri)
    response = invokeResponseHandler("/test/", new CreateResponse(1), ResourceMethod.CREATE, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 201, 3, null, null, null, false, errorResponseHeaderName);
    assertEquals(response.getHeader(RestConstants.HEADER_LOCATION), "/test/1");
    assertEquals(response.getHeader(idHeaderName), "1");
    // #2.3 create (without id and slash at the end of uri)
    response = invokeResponseHandler("/test/", new CreateResponse(HttpStatus.S_201_CREATED), ResourceMethod.CREATE, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 201, 1, null, null, null, false, errorResponseHeaderName);
    // #3 update
    response = invokeResponseHandler("/test", new UpdateResponse(HttpStatus.S_204_NO_CONTENT), ResourceMethod.UPDATE, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 204, 1, null, null, null, false, errorResponseHeaderName);
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) CreateStatus(com.linkedin.restli.common.CreateStatus) Status(com.linkedin.restli.server.twitter.TwitterTestDataModels.Status) HttpStatus(com.linkedin.restli.common.HttpStatus) UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestResponse(com.linkedin.r2.message.rest.RestResponse) PartialRestResponse(com.linkedin.restli.internal.server.response.PartialRestResponse) CreateResponse(com.linkedin.restli.server.CreateResponse) Test(org.testng.annotations.Test)

Example 19 with CreateResponse

use of com.linkedin.restli.server.CreateResponse in project rest.li by linkedin.

the class TestRestLiResponseHandler method testInvalidAcceptHeaders.

@Test
private void testInvalidAcceptHeaders() throws Exception {
    Map<String, String> badAcceptHeaders = Collections.singletonMap("Accept", "foo/bar");
    // check response with body (expect 406 error)
    try {
        invokeResponseHandler("/test", buildStatusRecord(), ResourceMethod.GET, badAcceptHeaders, AllProtocolVersions.LATEST_PROTOCOL_VERSION);
        Assert.fail();
    } catch (RestLiServiceException e) {
        Assert.assertEquals(e.getStatus().getCode(), 406);
    }
    // check response without body (expect 406 error)
    try {
        invokeResponseHandler("/test", new CreateResponse(HttpStatus.S_201_CREATED), ResourceMethod.CREATE, badAcceptHeaders, AllProtocolVersions.LATEST_PROTOCOL_VERSION);
        Assert.fail();
    } catch (RestLiServiceException e) {
        Assert.assertEquals(e.getStatus().getCode(), 406);
    }
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) CreateResponse(com.linkedin.restli.server.CreateResponse) ByteString(com.linkedin.data.ByteString) Test(org.testng.annotations.Test)

Aggregations

CreateResponse (com.linkedin.restli.server.CreateResponse)19 Test (org.testng.annotations.Test)9 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)8 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)5 BatchCreateResult (com.linkedin.restli.server.BatchCreateResult)5 ResourceContext (com.linkedin.restli.server.ResourceContext)5 ArrayList (java.util.ArrayList)5 HttpStatus (com.linkedin.restli.common.HttpStatus)4 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)4 UpdateResponse (com.linkedin.restli.server.UpdateResponse)4 Status (com.linkedin.restli.server.twitter.TwitterTestDataModels.Status)4 ByteString (com.linkedin.data.ByteString)3 DataMap (com.linkedin.data.DataMap)3 RestRequest (com.linkedin.r2.message.rest.RestRequest)3 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)3 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)3 RestLiResponseData (com.linkedin.restli.server.RestLiResponseData)3 URI (java.net.URI)3 PathSpec (com.linkedin.data.schema.PathSpec)2 PatchTree (com.linkedin.data.transform.patch.request.PatchTree)2