Search in sources :

Example 1 with RestResponseDecoder

use of com.linkedin.restli.internal.client.RestResponseDecoder in project rest.li by linkedin.

the class TestRestClientRequestBuilder method clientGeneratedStreamRequest.

//This is similar to clientGeneratedRestRequest above except that it will generate a StreamRequest instead
//of a RestRequest. Note that this will ONLY happen if either acceptResponseAttachments below is 'true' OR
//streamingAttachmentDataSources below is non-null with a size greater then 0. If both of these do not hold,
//then a StreamRequest will not be generated by the RestClient.
@SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
private <T extends Request> StreamRequest clientGeneratedStreamRequest(Class<T> requestClass, ResourceMethod method, DataMap entityBody, RestClient.ContentType contentType, List<RestClient.AcceptType> acceptTypes, boolean acceptContentTypePerClient, List<Object> streamingAttachmentDataSources, boolean acceptResponseAttachments) throws URISyntaxException {
    // massive setup...
    Client mockClient = EasyMock.createMock(Client.class);
    @SuppressWarnings({ "rawtypes" }) Request<?> mockRequest = EasyMock.createMock(requestClass);
    RecordTemplate mockRecordTemplate = EasyMock.createMock(RecordTemplate.class);
    @SuppressWarnings({ "rawtypes" }) RestResponseDecoder mockResponseDecoder = EasyMock.createMock(RestResponseDecoder.class);
    RestliRequestOptions requestOptions = RestliRequestOptions.DEFAULT_OPTIONS;
    //If there is a desire to receive response attachments, then we must use request options.
    if (!acceptContentTypePerClient || acceptResponseAttachments) {
        requestOptions = new RestliRequestOptions(ProtocolVersionOption.USE_LATEST_IF_AVAILABLE, null, null, contentType, acceptTypes, acceptResponseAttachments);
    }
    setCommonExpectations(mockRequest, method, mockResponseDecoder, requestOptions);
    if (streamingAttachmentDataSources != null && streamingAttachmentDataSources.size() > 0) {
        EasyMock.expect(mockRequest.getStreamingAttachments()).andReturn(streamingAttachmentDataSources).times(2);
    } else {
        EasyMock.expect(mockRequest.getStreamingAttachments()).andReturn(null).times(2);
    }
    setResourceMethodExpectations(method, mockRequest, mockRecordTemplate, entityBody);
    Capture<StreamRequest> streamRequestCapture = new Capture<StreamRequest>();
    EasyMock.expect(mockClient.getMetadata(new URI(HOST + SERVICE_NAME))).andReturn(Collections.<String, Object>emptyMap()).once();
    mockClient.streamRequest(EasyMock.capture(streamRequestCapture), (RequestContext) EasyMock.anyObject(), (Callback<StreamResponse>) EasyMock.anyObject());
    EasyMock.expectLastCall().once();
    EasyMock.replay(mockClient, mockRequest, mockRecordTemplate);
    // do work!
    RestClient restClient;
    if (acceptContentTypePerClient) {
        // configuration per client
        restClient = new RestClient(mockClient, HOST, contentType, acceptTypes);
    } else {
        // configuration per request
        restClient = new RestClient(mockClient, HOST);
    }
    restClient.sendRequest(mockRequest);
    return streamRequestCapture.getValue();
}
Also used : StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteString(com.linkedin.data.ByteString) URI(java.net.URI) Capture(org.easymock.Capture) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) RestResponseDecoder(com.linkedin.restli.internal.client.RestResponseDecoder) RecordTemplate(com.linkedin.data.template.RecordTemplate) Client(com.linkedin.r2.transport.common.Client)

Example 2 with RestResponseDecoder

use of com.linkedin.restli.internal.client.RestResponseDecoder in project rest.li by linkedin.

the class TestRestClientRequestBuilder method clientGeneratedRestRequest.

@SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
private <T extends Request> RestRequest clientGeneratedRestRequest(Class<T> requestClass, ResourceMethod method, DataMap entityBody, RestClient.ContentType contentType, List<RestClient.AcceptType> acceptTypes, boolean acceptContentTypePerClient) throws URISyntaxException {
    // massive setup...
    Client mockClient = EasyMock.createMock(Client.class);
    @SuppressWarnings({ "rawtypes" }) Request<?> mockRequest = EasyMock.createMock(requestClass);
    RecordTemplate mockRecordTemplate = EasyMock.createMock(RecordTemplate.class);
    @SuppressWarnings({ "rawtypes" }) RestResponseDecoder mockResponseDecoder = EasyMock.createMock(RestResponseDecoder.class);
    RestliRequestOptions requestOptions = RestliRequestOptions.DEFAULT_OPTIONS;
    if (!acceptContentTypePerClient) {
        requestOptions = new RestliRequestOptions(ProtocolVersionOption.USE_LATEST_IF_AVAILABLE, null, null, contentType, acceptTypes, false);
    }
    setCommonExpectations(mockRequest, method, mockResponseDecoder, requestOptions);
    EasyMock.expect(mockRequest.getStreamingAttachments()).andReturn(null).times(2);
    setResourceMethodExpectations(method, mockRequest, mockRecordTemplate, entityBody);
    Capture<RestRequest> restRequestCapture = new Capture<RestRequest>();
    EasyMock.expect(mockClient.getMetadata(new URI(HOST + SERVICE_NAME))).andReturn(Collections.<String, Object>emptyMap()).once();
    mockClient.restRequest(EasyMock.capture(restRequestCapture), (RequestContext) EasyMock.anyObject(), (Callback<RestResponse>) EasyMock.anyObject());
    EasyMock.expectLastCall().once();
    EasyMock.replay(mockClient, mockRequest, mockRecordTemplate);
    // do work!
    RestClient restClient;
    if (acceptContentTypePerClient) {
        // configuration per client
        restClient = new RestClient(mockClient, HOST, contentType, acceptTypes);
    } else {
        // configuration per request
        restClient = new RestClient(mockClient, HOST);
    }
    restClient.sendRequest(mockRequest);
    return restRequestCapture.getValue();
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) URI(java.net.URI) Capture(org.easymock.Capture) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponseDecoder(com.linkedin.restli.internal.client.RestResponseDecoder) RecordTemplate(com.linkedin.data.template.RecordTemplate) Client(com.linkedin.r2.transport.common.Client)

Aggregations

ByteString (com.linkedin.data.ByteString)2 RecordTemplate (com.linkedin.data.template.RecordTemplate)2 Client (com.linkedin.r2.transport.common.Client)2 RestResponseDecoder (com.linkedin.restli.internal.client.RestResponseDecoder)2 URI (java.net.URI)2 Capture (org.easymock.Capture)2 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 RestResponse (com.linkedin.r2.message.rest.RestResponse)1 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)1 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)1