Search in sources :

Example 1 with RestLiTestAttachmentDataSource

use of com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource in project rest.li by linkedin.

the class TestParseqTraceDebugRequestHandler method testResponseStreamingAttachmentsForbidden.

@Test
public void testResponseStreamingAttachmentsForbidden() {
    //This test verifies that the ParseqTraceDebugRequestHandler aborts any potential outgoing response attachments
    //set by a resource method.
    final URI uri = URI.create("http://host/abc/12/__debug/parseqtrace/raw");
    ParseqTraceDebugRequestHandler requestHandler = new ParseqTraceDebugRequestHandler();
    RestRequestBuilder requestBuilder = new RestRequestBuilder(uri);
    RestRequest request = requestBuilder.build();
    RequestContext requestContext = new RequestContext();
    final RequestExecutionCallback<RestResponse> callback = new RequestExecutionCallback<RestResponse>() {

        @Override
        public void onError(Throwable e, RequestExecutionReport executionReport, RestLiAttachmentReader requestAttachmentReader, RestLiResponseAttachments responseAttachments) {
            Assert.fail("Request execution failed unexpectedly.");
        }

        @Override
        public void onSuccess(RestResponse result, RequestExecutionReport executionReport, RestLiResponseAttachments responseAttachments) {
        }
    };
    final RestLiTestAttachmentDataSource testAttachmentDataSource = RestLiTestAttachmentDataSource.createWithRandomPayload("1");
    final RestLiResponseAttachments responseAttachments = new RestLiResponseAttachments.Builder().appendSingleAttachment(testAttachmentDataSource).build();
    requestHandler.handleRequest(request, requestContext, new RestLiDebugRequestHandler.ResourceDebugRequestHandler() {

        @Override
        public void handleRequest(RestRequest request, RequestContext requestContext, RequestExecutionCallback<RestResponse> callback) {
            RestResponse response = EasyMock.createMock(RestResponse.class);
            //Provide some attachments that should be aborted.
            callback.onSuccess(response, new RequestExecutionReportBuilder().build(), responseAttachments);
        }
    }, null, callback);
    Assert.assertTrue(testAttachmentDataSource.dataSourceAborted());
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) RestLiTestAttachmentDataSource(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource) RestLiAttachmentReader(com.linkedin.restli.common.attachments.RestLiAttachmentReader) Test(org.testng.annotations.Test)

Example 2 with RestLiTestAttachmentDataSource

use of com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource in project rest.li by linkedin.

the class TestRestClientRequestBuilder method generateRequestAttachments.

private List<Object> generateRequestAttachments() {
    final List<Object> requestAttachments = new ArrayList<>();
    requestAttachments.add(new RestLiTestAttachmentDataSource(FIRST_PART_ID, FIRST_PART_PAYLOAD));
    requestAttachments.add(new RestLiTestAttachmentDataSource(SECOND_PART_ID, SECOND_PART_PAYLOAD));
    return requestAttachments;
}
Also used : ArrayList(java.util.ArrayList) RestLiTestAttachmentDataSource(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource)

Example 3 with RestLiTestAttachmentDataSource

use of com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource in project rest.li by linkedin.

the class TestAttachmentUtils method testMultipleAttachments.

@Test
public void testMultipleAttachments() {
    final MultiPartMIMEWriter.Builder builder = new MultiPartMIMEWriter.Builder();
    final List<RestLiTestAttachmentDataSource> testAttachmentDataSources = generateTestDataSources();
    final RestLiTestAttachmentDataSourceIterator dataSourceIterator = new RestLiTestAttachmentDataSourceIterator(testAttachmentDataSources, new IllegalArgumentException());
    // Let each data source know its parent, so that when the data source is done, it can notify it's parent to call onNewDataSourceWriter()
    for (final RestLiTestAttachmentDataSource dataSource : testAttachmentDataSources) {
        dataSource.setParentDataSourceIterator(dataSourceIterator);
    }
    AttachmentUtils.appendMultipleAttachmentsToBuilder(builder, dataSourceIterator);
    final StreamRequest streamRequest = MultiPartMIMEStreamRequestFactory.generateMultiPartMIMEStreamRequest(URI.create("foo"), "related", builder.build());
    final MultiPartMIMEReader streamRequestReader = MultiPartMIMEReader.createAndAcquireStream(streamRequest);
    final CountDownLatch streamRequestReaderLatch = new CountDownLatch(1);
    final MIMETestUtils.MultiPartMIMEFullReaderCallback streamRequestReaderCallback = new MIMETestUtils.MultiPartMIMEFullReaderCallback(streamRequestReaderLatch);
    streamRequestReader.registerReaderCallback(streamRequestReaderCallback);
    try {
        streamRequestReaderLatch.await(3000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException interruptedException) {
        Assert.fail();
    }
    verifyAttachments(streamRequestReaderCallback.getSinglePartMIMEReaderCallbacks(), testAttachmentDataSources);
}
Also used : RestLiTestAttachmentDataSourceIterator(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSourceIterator) MultiPartMIMEReader(com.linkedin.multipart.MultiPartMIMEReader) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) RestLiTestAttachmentDataSource(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource) MultiPartMIMEWriter(com.linkedin.multipart.MultiPartMIMEWriter) MIMETestUtils(com.linkedin.multipart.utils.MIMETestUtils) Test(org.testng.annotations.Test)

Example 4 with RestLiTestAttachmentDataSource

use of com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource in project rest.li by linkedin.

the class TestAttachmentUtils method testMixtureOfAttachmentsAbort.

@Test
public void testMixtureOfAttachmentsAbort() {
    final MultiPartMIMEWriter.Builder builder = new MultiPartMIMEWriter.Builder();
    final List<RestLiTestAttachmentDataSource> iteratorDataSources = generateTestDataSources();
    final RestLiTestAttachmentDataSourceIterator dataSourceIterator = new RestLiTestAttachmentDataSourceIterator(iteratorDataSources, new IllegalArgumentException());
    // Let each data source know its parent, so that when the data source is done, it can notify it's parent to call onNewDataSourceWriter()
    for (final RestLiTestAttachmentDataSource dataSource : iteratorDataSources) {
        dataSource.setParentDataSourceIterator(dataSourceIterator);
    }
    // Now one at the beginning
    final RestLiTestAttachmentDataSource dataSourceBeginning = RestLiTestAttachmentDataSource.createWithRandomPayload("BEG");
    // And one at the end
    final RestLiTestAttachmentDataSource dataSourceEnd = RestLiTestAttachmentDataSource.createWithRandomPayload("END");
    AttachmentUtils.appendSingleAttachmentToBuilder(builder, dataSourceBeginning);
    AttachmentUtils.appendMultipleAttachmentsToBuilder(builder, dataSourceIterator);
    AttachmentUtils.appendSingleAttachmentToBuilder(builder, dataSourceEnd);
    // Abort everything. We want to make sure that our process of wrapping the rest.li data sources into the multipart
    // mime layer correctly propagates the abandon/aborts.
    builder.build().abortAllDataSources(new IllegalStateException());
    Assert.assertTrue(dataSourceIterator.dataSourceIteratorAbandoned());
    Assert.assertTrue(dataSourceBeginning.dataSourceAborted());
    Assert.assertTrue(dataSourceEnd.dataSourceAborted());
    // that an abort has taken place.
    for (final RestLiTestAttachmentDataSource dataSource : iteratorDataSources) {
        Assert.assertTrue(dataSource.dataSourceAborted());
    }
}
Also used : RestLiTestAttachmentDataSourceIterator(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSourceIterator) RestLiTestAttachmentDataSource(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource) MultiPartMIMEWriter(com.linkedin.multipart.MultiPartMIMEWriter) Test(org.testng.annotations.Test)

Example 5 with RestLiTestAttachmentDataSource

use of com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource in project rest.li by linkedin.

the class TestAttachmentUtils method generateTestDataSources.

private List<RestLiTestAttachmentDataSource> generateTestDataSources() {
    final RestLiTestAttachmentDataSource dataSourceA = RestLiTestAttachmentDataSource.createWithRandomPayload("A");
    final RestLiTestAttachmentDataSource dataSourceB = RestLiTestAttachmentDataSource.createWithRandomPayload("B");
    final RestLiTestAttachmentDataSource dataSourceC = RestLiTestAttachmentDataSource.createWithRandomPayload("C");
    final RestLiTestAttachmentDataSource emptyBodySource = new RestLiTestAttachmentDataSource("D", ByteString.empty());
    return new ArrayList<>(Arrays.asList(dataSourceA, dataSourceB, dataSourceC, emptyBodySource));
}
Also used : ArrayList(java.util.ArrayList) RestLiTestAttachmentDataSource(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource)

Aggregations

RestLiTestAttachmentDataSource (com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource)17 Test (org.testng.annotations.Test)14 MultiPartMIMEWriter (com.linkedin.multipart.MultiPartMIMEWriter)7 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)6 MultiPartMIMEReader (com.linkedin.multipart.MultiPartMIMEReader)5 RestLiAttachmentReader (com.linkedin.restli.common.attachments.RestLiAttachmentReader)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 RequestContext (com.linkedin.r2.message.RequestContext)4 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)4 RestLiTestAttachmentDataSourceIterator (com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSourceIterator)4 URI (java.net.URI)4 ByteString (com.linkedin.data.ByteString)3 MIMETestUtils (com.linkedin.multipart.utils.MIMETestUtils)3 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)3 TestRecord (com.linkedin.restli.client.test.TestRecord)3 ArrayList (java.util.ArrayList)3 Callback (com.linkedin.common.callback.Callback)2 DataMap (com.linkedin.data.DataMap)2 MultiPartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback)2 SinglePartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback)2