Search in sources :

Example 31 with UpdateResponse

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

the class TestRestLiMethodInvocation method testAsyncPut.

@SuppressWarnings("unchecked")
@Test
public void testAsyncPut() throws Exception {
    Map<String, ResourceModel> resourceModelMap = buildResourceModels(AsyncStatusCollectionResource.class, AsyncLocationResource.class, AsyncDiscoveredItemsResource.class);
    ResourceModel statusResourceModel = resourceModelMap.get("/asyncstatuses");
    ResourceModel locationResourceModel = statusResourceModel.getSubResource("asynclocation");
    ResourceModel followsAssociationResourceModel = buildResourceModel(AsyncFollowsAssociativeResource.class);
    ResourceModel discoveredItemsResourceModel = resourceModelMap.get("/asyncdiscovereditems");
    RestLiCallback callback = getCallback();
    ResourceMethodDescriptor methodDescriptor;
    AsyncStatusCollectionResource statusResource;
    AsyncFollowsAssociativeResource followsResource;
    AsyncLocationResource locationResource;
    AsyncDiscoveredItemsResource discoveredItemsResource;
    // #1 Update on collection resource
    methodDescriptor = statusResourceModel.findMethod(ResourceMethod.UPDATE);
    statusResource = getMockResource(AsyncStatusCollectionResource.class);
    long id = eq(1L);
    Status status = EasyMock.anyObject();
    statusResource.update(id, status, EasyMock.anyObject());
    EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

        @Override
        public Object answer() throws Throwable {
            @SuppressWarnings("unchecked") Callback<UpdateResponse> callback = (Callback<UpdateResponse>) EasyMock.getCurrentArguments()[2];
            callback.onSuccess(null);
            return null;
        }
    });
    EasyMock.replay(statusResource);
    checkAsyncInvocation(statusResource, callback, methodDescriptor, "PUT", version, "/asyncstatuses/1", "{}", buildPathKeys("statusID", 1L));
    // #2 Update on association resource
    methodDescriptor = followsAssociationResourceModel.findMethod(ResourceMethod.UPDATE);
    followsResource = getMockResource(AsyncFollowsAssociativeResource.class);
    CompoundKey rawKey = new CompoundKey();
    rawKey.append("followerID", 1L);
    rawKey.append("followeeID", 2L);
    CompoundKey key = eq(rawKey);
    Followed followed = EasyMock.anyObject();
    followsResource.update(key, followed, (Callback<UpdateResponse>) EasyMock.anyObject());
    EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

        @Override
        public Object answer() throws Throwable {
            Callback<UpdateResponse> callback = (Callback<UpdateResponse>) EasyMock.getCurrentArguments()[2];
            callback.onSuccess(null);
            return null;
        }
    });
    EasyMock.replay(followsResource);
    checkAsyncInvocation(followsResource, callback, methodDescriptor, "PUT", version, "/asyncfollows/(followerID:1,followeeID:2)", "{}", buildPathKeys("followerID", 1L, "followeeID", 2L, followsAssociationResourceModel.getKeyName(), rawKey));
    // #3 Update on simple resource
    methodDescriptor = locationResourceModel.findMethod(ResourceMethod.UPDATE);
    locationResource = getMockResource(AsyncLocationResource.class);
    Location location = EasyMock.anyObject();
    locationResource.update(location, EasyMock.anyObject());
    EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

        @Override
        public Object answer() throws Throwable {
            @SuppressWarnings("unchecked") Callback<UpdateResponse> callback = (Callback<UpdateResponse>) EasyMock.getCurrentArguments()[1];
            callback.onSuccess(null);
            return null;
        }
    });
    EasyMock.replay(locationResource);
    checkAsyncInvocation(locationResource, callback, methodDescriptor, "PUT", version, "/asyncstatuses/1/asynclocation", "{}", buildPathKeys("statusID", 1L));
    // #4 Update on complex-key resource
    methodDescriptor = discoveredItemsResourceModel.findMethod(ResourceMethod.UPDATE);
    discoveredItemsResource = getMockResource(AsyncDiscoveredItemsResource.class);
    ComplexResourceKey<DiscoveredItemKey, DiscoveredItemKeyParams> complexKey = getDiscoveredItemComplexKey(1L, 2, 3L);
    discoveredItemsResource.update(eq(complexKey), EasyMock.anyObject(DiscoveredItem.class), EasyMock.anyObject());
    EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

        @Override
        public Object answer() throws Throwable {
            @SuppressWarnings("unchecked") Callback<UpdateResponse> callback = (Callback<UpdateResponse>) EasyMock.getCurrentArguments()[2];
            callback.onSuccess(null);
            return null;
        }
    });
    EasyMock.replay(discoveredItemsResource);
    checkAsyncInvocation(discoveredItemsResource, callback, methodDescriptor, "PUT", version, "/asyncdiscovereditems/(itemId:1,type:2,userId:3)", "{}", buildPathKeys("asyncDiscoveredItemId", complexKey));
}
Also used : AsyncLocationResource(com.linkedin.restli.server.twitter.AsyncLocationResource) Status(com.linkedin.restli.server.twitter.TwitterTestDataModels.Status) HttpStatus(com.linkedin.restli.common.HttpStatus) AsyncDiscoveredItemsResource(com.linkedin.restli.server.twitter.AsyncDiscoveredItemsResource) CompoundKey(com.linkedin.restli.common.CompoundKey) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) Followed(com.linkedin.restli.server.twitter.TwitterTestDataModels.Followed) ByteString(com.linkedin.data.ByteString) CustomString(com.linkedin.restli.server.custom.types.CustomString) AsyncFollowsAssociativeResource(com.linkedin.restli.server.twitter.AsyncFollowsAssociativeResource) AsyncStatusCollectionResource(com.linkedin.restli.server.twitter.AsyncStatusCollectionResource) UpdateResponse(com.linkedin.restli.server.UpdateResponse) Callback(com.linkedin.common.callback.Callback) RestLiCallback(com.linkedin.restli.internal.server.RestLiCallback) FilterChainCallback(com.linkedin.restli.internal.server.filter.FilterChainCallback) DiscoveredItemKeyParams(com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItemKeyParams) RestLiCallback(com.linkedin.restli.internal.server.RestLiCallback) DiscoveredItem(com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItem) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) DiscoveredItemKey(com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItemKey) Location(com.linkedin.restli.server.twitter.TwitterTestDataModels.Location) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 32 with UpdateResponse

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

the class TestRestLiResponseHandler method testBatchResponses.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
public void testBatchResponses(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
    RestLiResponse response;
    // #4 batch
    Map<Long, Status> map = new HashMap<>();
    map.put(1L, buildStatusRecord());
    map.put(2L, buildStatusRecord());
    map.put(3L, buildStatusRecord());
    response = invokeResponseHandler("/test", map, ResourceMethod.BATCH_GET, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 200, 1, true, errorResponseHeaderName);
    Map<Long, UpdateResponse> updateStatusMap = new HashMap<>();
    updateStatusMap.put(1L, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
    updateStatusMap.put(2L, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
    updateStatusMap.put(3L, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
    BatchUpdateResult<Long, Status> batchUpdateResult = new BatchUpdateResult<>(updateStatusMap);
    response = invokeResponseHandler("/test", batchUpdateResult, ResourceMethod.BATCH_UPDATE, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 200, 1, true, errorResponseHeaderName);
    response = invokeResponseHandler("/test", batchUpdateResult, ResourceMethod.BATCH_PARTIAL_UPDATE, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 200, 1, true, errorResponseHeaderName);
    response = invokeResponseHandler("/test", batchUpdateResult, ResourceMethod.BATCH_DELETE, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 200, 1, true, errorResponseHeaderName);
    List<CreateResponse> createResponses = new ArrayList<>();
    createResponses.add(new CreateResponse("42", HttpStatus.S_204_NO_CONTENT));
    createResponses.add(new CreateResponse(HttpStatus.S_400_BAD_REQUEST));
    createResponses.add(new CreateResponse(HttpStatus.S_500_INTERNAL_SERVER_ERROR));
    BatchCreateResult<Long, Status> batchCreateResult = new BatchCreateResult<>(createResponses);
    // here
    response = invokeResponseHandler("/test", batchCreateResult, ResourceMethod.BATCH_CREATE, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 200, 1, true, errorResponseHeaderName);
}
Also used : Status(com.linkedin.restli.server.twitter.TwitterTestDataModels.Status) HttpStatus(com.linkedin.restli.common.HttpStatus) HashMap(java.util.HashMap) CreateResponse(com.linkedin.restli.server.CreateResponse) ArrayList(java.util.ArrayList) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) UpdateResponse(com.linkedin.restli.server.UpdateResponse) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) BatchCreateResult(com.linkedin.restli.server.BatchCreateResult) Test(org.testng.annotations.Test)

Example 33 with UpdateResponse

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

the class TestUpdateResponseBuilder method testBuilder.

@Test
public void testBuilder() {
    HttpStatus status = HttpStatus.S_200_OK;
    UpdateResponse updateResponse = new UpdateResponse(status);
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor();
    RoutingResult routingResult = new RoutingResult(null, mockDescriptor);
    UpdateResponseBuilder updateResponseBuilder = new UpdateResponseBuilder();
    RestLiResponseData responseData = updateResponseBuilder.buildRestLiResponseData(null, routingResult, updateResponse, headers, Collections.<HttpCookie>emptyList());
    PartialRestResponse partialRestResponse = updateResponseBuilder.buildResponse(routingResult, responseData);
    EasyMock.verify(mockDescriptor);
    ResponseBuilderUtil.validateHeaders(partialRestResponse, headers);
    Assert.assertEquals(partialRestResponse.getStatus(), status);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) UpdateResponse(com.linkedin.restli.server.UpdateResponse) HttpStatus(com.linkedin.restli.common.HttpStatus) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) Test(org.testng.annotations.Test)

Example 34 with UpdateResponse

use of com.linkedin.restli.server.UpdateResponse in project incubator-gobblin by apache.

the class FlowConfigsResource method delete.

/**
 * Delete a configured flow. Running flows are not affected. The schedule will be removed for scheduled flows.
 * @param key composite key containing flow group and flow name that identifies the flow to remove from the
 * {@link FlowCatalog}
 * @return {@link UpdateResponse}
 */
@Override
public UpdateResponse delete(ComplexResourceKey<FlowId, EmptyRecord> key) {
    String flowGroup = key.getKey().getFlowGroup();
    String flowName = key.getKey().getFlowName();
    URI flowUri = null;
    LOG.info("Delete called with flowGroup " + flowGroup + " flowName " + flowName);
    try {
        URI flowCatalogURI = new URI("gobblin-flow", null, "/", null, null);
        flowUri = new URI(flowCatalogURI.getScheme(), flowCatalogURI.getAuthority(), "/" + flowGroup + "/" + flowName, null, null);
        getFlowCatalog().remove(flowUri);
        return new UpdateResponse(HttpStatus.S_200_OK);
    } catch (URISyntaxException e) {
        logAndThrowRestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "bad URI " + flowUri, e);
    }
    return null;
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 35 with UpdateResponse

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

the class GroupsResource2 method update.

@Override
public UpdateResponse update(Integer id, PatchRequest<Group> patch) {
    Group group = get(id);
    try {
        PatchApplier.applyPatch(group, patch);
    } catch (DataProcessingException e) {
        return new UpdateResponse(HttpStatus.S_400_BAD_REQUEST);
    }
    boolean wasUpdated = getGroupMgr().update(id, group);
    return new UpdateResponse(wasUpdated ? HttpStatus.S_204_NO_CONTENT : HttpStatus.S_404_NOT_FOUND);
}
Also used : Group(com.linkedin.restli.examples.groups.api.Group) UpdateResponse(com.linkedin.restli.server.UpdateResponse) DataProcessingException(com.linkedin.data.transform.DataProcessingException)

Aggregations

UpdateResponse (com.linkedin.restli.server.UpdateResponse)55 BatchUpdateResult (com.linkedin.restli.server.BatchUpdateResult)21 HashMap (java.util.HashMap)21 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)18 DataProcessingException (com.linkedin.data.transform.DataProcessingException)12 Map (java.util.Map)11 Test (org.testng.annotations.Test)11 CompoundKey (com.linkedin.restli.common.CompoundKey)9 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)6 HttpStatus (com.linkedin.restli.common.HttpStatus)5 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)5 ByteString (com.linkedin.data.ByteString)4 PatchRequest (com.linkedin.restli.common.PatchRequest)4 UpdateStatus (com.linkedin.restli.common.UpdateStatus)4 Photo (com.linkedin.restli.example.Photo)4 ValidationDemo (com.linkedin.restli.examples.greetings.api.ValidationDemo)4 BatchPatchRequest (com.linkedin.restli.server.BatchPatchRequest)4 Callback (com.linkedin.common.callback.Callback)3 DataMap (com.linkedin.data.DataMap)3 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)3