Search in sources :

Example 1 with RestLiTestAttachmentDataSourceIterator

use of com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSourceIterator 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 2 with RestLiTestAttachmentDataSourceIterator

use of com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSourceIterator 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 3 with RestLiTestAttachmentDataSourceIterator

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

the class TestAttachmentUtils method testMixtureOfAttachments.

@Test
public void testMixtureOfAttachments() {
    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);
    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();
    }
    final List<RestLiTestAttachmentDataSource> allAttachmentDataSources = new ArrayList<>();
    allAttachmentDataSources.add(dataSourceBeginning);
    allAttachmentDataSources.addAll(iteratorDataSources);
    allAttachmentDataSources.add(dataSourceEnd);
    verifyAttachments(streamRequestReaderCallback.getSinglePartMIMEReaderCallbacks(), allAttachmentDataSources);
}
Also used : RestLiTestAttachmentDataSourceIterator(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSourceIterator) MultiPartMIMEReader(com.linkedin.multipart.MultiPartMIMEReader) ArrayList(java.util.ArrayList) 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 RestLiTestAttachmentDataSourceIterator

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

the class TestRestLiResponseAttachments method testRestLiResponseAttachments.

@Test
public void testRestLiResponseAttachments() {
    // In this test we simply add a few attachments and verify the size of the resulting MultiPartMIMEWriter.
    // More detailed tests can be found in TestAttachmentUtils.
    final RestLiResponseAttachments emptyAttachments = new RestLiResponseAttachments.Builder().build();
    Assert.assertEquals(emptyAttachments.getMultiPartMimeWriterBuilder().getCurrentSize(), 0);
    // For multiple data attachments
    final RestLiTestAttachmentDataSource dataSourceA = new RestLiTestAttachmentDataSource("A", ByteString.copyString("partA", Charset.defaultCharset()));
    final RestLiTestAttachmentDataSource dataSourceB = new RestLiTestAttachmentDataSource("B", ByteString.copyString("partB", Charset.defaultCharset()));
    final RestLiTestAttachmentDataSource dataSourceC = new RestLiTestAttachmentDataSource("C", ByteString.copyString("partC", Charset.defaultCharset()));
    final RestLiResponseAttachments.Builder multipleAttachmentsBuilder = new RestLiResponseAttachments.Builder();
    multipleAttachmentsBuilder.appendSingleAttachment(dataSourceA);
    final RestLiTestAttachmentDataSourceIterator dataSourceIterator = new RestLiTestAttachmentDataSourceIterator(Arrays.asList(dataSourceB, dataSourceC), new IllegalArgumentException());
    multipleAttachmentsBuilder.appendMultipleAttachments(dataSourceIterator);
    RestLiResponseAttachments attachments = multipleAttachmentsBuilder.build();
    Assert.assertEquals(attachments.getMultiPartMimeWriterBuilder().getCurrentSize(), 2);
}
Also used : RestLiTestAttachmentDataSourceIterator(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSourceIterator) RestLiTestAttachmentDataSource(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource) Test(org.testng.annotations.Test)

Aggregations

RestLiTestAttachmentDataSource (com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource)4 RestLiTestAttachmentDataSourceIterator (com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSourceIterator)4 Test (org.testng.annotations.Test)4 MultiPartMIMEWriter (com.linkedin.multipart.MultiPartMIMEWriter)3 MultiPartMIMEReader (com.linkedin.multipart.MultiPartMIMEReader)2 MIMETestUtils (com.linkedin.multipart.utils.MIMETestUtils)2 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ArrayList (java.util.ArrayList)1