Search in sources :

Example 6 with CreateResponse

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

the class MixedResource method create.

@Create
public void create(Greeting entity, @CallbackParam final Callback<CreateResponse> callback) {
    final Runnable requestHandler = new Runnable() {

        public void run() {
            callback.onSuccess(new CreateResponse(HttpStatus.S_200_OK));
        }
    };
    scheduler.schedule(requestHandler, DELAY, TimeUnit.MILLISECONDS);
}
Also used : CreateResponse(com.linkedin.restli.server.CreateResponse) Create(com.linkedin.restli.server.annotations.RestMethod.Create)

Example 7 with CreateResponse

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

the class ExceptionsResource method create.

/**
   * Responds with an error for requests to create insulting greetings, responds
   * with 201 created for all other requests.
   */
@RestMethod.Create
public CreateResponse create(Greeting g) {
    if (g.hasTone() && g.getTone() == Tone.INSULTING) {
        RestLiServiceException notAcceptableException = new RestLiServiceException(HttpStatus.S_406_NOT_ACCEPTABLE, "I will not tolerate your insolence!");
        DataMap details = new DataMap();
        details.put("reason", "insultingGreeting");
        notAcceptableException.setErrorDetails(details);
        notAcceptableException.setServiceErrorCode(999);
        throw notAcceptableException;
    } else {
        return new CreateResponse(g.getId(), HttpStatus.S_201_CREATED);
    }
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) CreateResponse(com.linkedin.restli.server.CreateResponse) DataMap(com.linkedin.data.DataMap)

Example 8 with CreateResponse

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

the class TestBatchCreateResponseBuilder method testCreateResultBuilder.

@Test(dataProvider = "createResultBuilderTestData")
@SuppressWarnings("unchecked")
public void testCreateResultBuilder(String altKeyName, Map<String, AlternativeKey<?, ?>> alternativeKeyMap, List<CreateIdStatus<Object>> expectedStatuses) {
    List<CreateResponse> createResponses = Arrays.asList(new CreateResponse(1L), new CreateResponse(2L));
    BatchCreateResult<Long, Foo> results = new BatchCreateResult<Long, Foo>(createResponses);
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(alternativeKeyMap);
    ResourceContext mockContext = getMockResourceContext(altKeyName);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(null);
    RestLiResponseData responseData = responseBuilder.buildRestLiResponseData(null, routingResult, results, headers, Collections.<HttpCookie>emptyList());
    PartialRestResponse restResponse = responseBuilder.buildResponse(routingResult, responseData);
    EasyMock.verify(mockDescriptor);
    ResponseBuilderUtil.validateHeaders(restResponse, headers);
    Assert.assertFalse(responseData.getBatchCreateResponseEnvelope().isGetAfterCreate());
    List<com.linkedin.restli.common.CreateIdStatus<Object>> items = new ArrayList<CreateIdStatus<Object>>();
    for (BatchCreateResponseEnvelope.CollectionCreateResponseItem item : responseData.getBatchCreateResponseEnvelope().getCreateResponses()) {
        items.add((CreateIdStatus<Object>) item.getRecord());
    }
    Assert.assertEquals(restResponse.getEntity(), new BatchCreateIdResponse<Object>(items));
    Assert.assertEquals(expectedStatuses, items);
    Assert.assertEquals(restResponse.getStatus(), HttpStatus.S_200_OK);
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) CreateResponse(com.linkedin.restli.server.CreateResponse) Foo(com.linkedin.pegasus.generator.examples.Foo) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) ArrayList(java.util.ArrayList) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) BatchCreateResult(com.linkedin.restli.server.BatchCreateResult) Test(org.testng.annotations.Test)

Example 9 with CreateResponse

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

the class TestCreateResponseBuilder method testCreateResponseException.

@Test
public void testCreateResponseException() throws URISyntaxException {
    CreateResponse createResponse = new CreateResponse(new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST));
    RestRequest restRequest = new RestRequestBuilder(new URI("/foo")).build();
    RestLiResponseData envelope = new CreateResponseBuilder().buildRestLiResponseData(restRequest, null, createResponse, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
    Assert.assertTrue(envelope.isErrorResponse());
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) CreateResponse(com.linkedin.restli.server.CreateResponse) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 10 with CreateResponse

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

the class TestCreateResponseBuilder method testBuilder.

@Test(dataProvider = "testData")
public void testBuilder(ProtocolVersion protocolVersion, Object expectedId, String expectedLocation, String expectedHeaderId, String altKeyName, Map<String, AlternativeKey<?, ?>> alternativeKeyMap) throws URISyntaxException {
    CompoundKey compoundKey = new CompoundKey().append("a", "a").append("b", 1);
    CreateResponse createResponse = new CreateResponse(compoundKey);
    IdResponse<?> expectedIdResponse = new IdResponse<Object>(expectedId);
    RestRequest restRequest = new RestRequestBuilder(new URI("/foo")).build();
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
    // the headers passed in are modified
    Map<String, String> expectedHeaders = new HashMap<String, String>(headers);
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(alternativeKeyMap);
    ResourceContext mockContext = getMockResourceContext(protocolVersion, altKeyName);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    CreateResponseBuilder createResponseBuilder = new CreateResponseBuilder();
    RestLiResponseData responseData = createResponseBuilder.buildRestLiResponseData(restRequest, routingResult, createResponse, headers, Collections.<HttpCookie>emptyList());
    Assert.assertFalse(responseData.getCreateResponseEnvelope().isGetAfterCreate());
    PartialRestResponse partialRestResponse = createResponseBuilder.buildResponse(routingResult, responseData);
    expectedHeaders.put(RestConstants.HEADER_LOCATION, expectedLocation);
    if (protocolVersion.equals(AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion())) {
        expectedHeaders.put(RestConstants.HEADER_ID, expectedHeaderId);
    } else {
        expectedHeaders.put(RestConstants.HEADER_RESTLI_ID, expectedHeaderId);
    }
    EasyMock.verify(mockContext, mockDescriptor);
    ResponseBuilderUtil.validateHeaders(partialRestResponse, expectedHeaders);
    Assert.assertEquals(partialRestResponse.getStatus(), HttpStatus.S_201_CREATED);
    Assert.assertEquals(partialRestResponse.getEntity(), expectedIdResponse);
}
Also used : IdResponse(com.linkedin.restli.common.IdResponse) ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) CompoundKey(com.linkedin.restli.common.CompoundKey) HashMap(java.util.HashMap) CreateResponse(com.linkedin.restli.server.CreateResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) URI(java.net.URI) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) 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