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);
}
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());
}
}
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);
}
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);
}
Aggregations