Search in sources :

Example 1 with MIMEDataPart

use of com.linkedin.multipart.utils.MIMEDataPart in project rest.li by linkedin.

the class TestMIMEWriter method testPrependDataSources.

@Test(dataProvider = "prependDataSources")
public void testPrependDataSources(final MultiPartMIMEWriter.Builder builder, final List<MultiPartMIMEDataSourceWriter> prependDataSources, final List<MIMEDataPart> expectedParts, final String expectedPreamble, final int expectedSize) throws Exception {
    for (final MultiPartMIMEDataSourceWriter dataSourceWriter : prependDataSources) {
        builder.prependDataSource(dataSourceWriter);
    }
    Assert.assertEquals(builder.getCurrentSize(), expectedSize);
    final MultiPartMIMEWriter writer = builder.build();
    final FutureCallback<ByteString> futureCallback = new FutureCallback<>();
    final FullEntityReader fullEntityReader = new FullEntityReader(futureCallback);
    writer.getEntityStream().setReader(fullEntityReader);
    futureCallback.get(_testTimeout, TimeUnit.MILLISECONDS);
    final StreamRequest multiPartMIMEStreamRequest = MultiPartMIMEStreamRequestFactory.generateMultiPartMIMEStreamRequest(URI.create("localhost"), "mixed", writer, Collections.<String, String>emptyMap());
    final JavaxMailMultiPartMIMEReader javaxMailMultiPartMIMEReader = new JavaxMailMultiPartMIMEReader(multiPartMIMEStreamRequest.getHeader(MultiPartMIMEUtils.CONTENT_TYPE_HEADER), futureCallback.get());
    javaxMailMultiPartMIMEReader.parseRequestIntoParts();
    List<MIMEDataPart> dataSourceList = javaxMailMultiPartMIMEReader._dataSourceList;
    Assert.assertEquals(dataSourceList.size(), 3);
    for (int i = 0; i < dataSourceList.size(); i++) {
        Assert.assertEquals(dataSourceList.get(i), expectedParts.get(i));
    }
    // Javax mail incorrectly adds the CRLF for the first boundary to the end of the preamble, so we trim
    Assert.assertEquals(javaxMailMultiPartMIMEReader._preamble != null ? javaxMailMultiPartMIMEReader._preamble.trim() : null, expectedPreamble);
}
Also used : FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) ByteString(com.linkedin.data.ByteString) MIMEDataPart(com.linkedin.multipart.utils.MIMEDataPart) FutureCallback(com.linkedin.common.callback.FutureCallback) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 2 with MIMEDataPart

use of com.linkedin.multipart.utils.MIMEDataPart in project rest.li by linkedin.

the class TestMIMEWriter method testSingleDataSource.

@Test(dataProvider = "singleDataSources")
public void testSingleDataSource(final ByteString body, final Map<String, String> headers) throws Exception {
    final MIMEDataPart expectedMultiPartMIMEDataPart = new MIMEDataPart(body, headers);
    final MultiPartMIMEInputStream singleDataSource = new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(body.copyBytes()), _scheduledExecutorService, headers).build();
    final MultiPartMIMEWriter multiPartMIMEWriter = new MultiPartMIMEWriter.Builder("preamble", "epilogue").appendDataSource(singleDataSource).build();
    final FutureCallback<ByteString> futureCallback = new FutureCallback<>();
    final FullEntityReader fullEntityReader = new FullEntityReader(futureCallback);
    multiPartMIMEWriter.getEntityStream().setReader(fullEntityReader);
    futureCallback.get(_testTimeout, TimeUnit.MILLISECONDS);
    final StreamRequest multiPartMIMEStreamRequest = MultiPartMIMEStreamRequestFactory.generateMultiPartMIMEStreamRequest(URI.create("localhost"), "mixed", multiPartMIMEWriter, Collections.<String, String>emptyMap());
    JavaxMailMultiPartMIMEReader javaxMailMultiPartMIMEReader = new JavaxMailMultiPartMIMEReader(multiPartMIMEStreamRequest.getHeader(MultiPartMIMEUtils.CONTENT_TYPE_HEADER), futureCallback.get());
    javaxMailMultiPartMIMEReader.parseRequestIntoParts();
    List<MIMEDataPart> dataSourceList = javaxMailMultiPartMIMEReader._dataSourceList;
    Assert.assertEquals(dataSourceList.size(), 1);
    Assert.assertEquals(dataSourceList.get(0), expectedMultiPartMIMEDataPart);
    // Javax mail incorrectly adds the CRLF for the first boundary to the end of the preamble, so we trim
    Assert.assertEquals(javaxMailMultiPartMIMEReader._preamble.trim(), "preamble");
}
Also used : ByteString(com.linkedin.data.ByteString) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) ByteArrayInputStream(java.io.ByteArrayInputStream) MIMEDataPart(com.linkedin.multipart.utils.MIMEDataPart) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 3 with MIMEDataPart

use of com.linkedin.multipart.utils.MIMEDataPart in project rest.li by linkedin.

the class TestMIMEWriter method testMultipleDataSources.

@Test
public void testMultipleDataSources() throws Exception {
    final List<MIMEDataPart> expectedParts = new ArrayList<>();
    expectedParts.add(_normalBody);
    expectedParts.add(_normalBody);
    expectedParts.add(_headerLessBody);
    expectedParts.add(_normalBody);
    expectedParts.add(_bodyLessBody);
    expectedParts.add(_purelyEmptyBody);
    expectedParts.add(_purelyEmptyBody);
    expectedParts.add(_headerLessBody);
    expectedParts.add(_headerLessBody);
    expectedParts.add(_headerLessBody);
    expectedParts.add(_normalBody);
    expectedParts.add(_bodyLessBody);
    final List<MultiPartMIMEDataSourceWriter> inputStreamDataSources = new ArrayList<>();
    inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(_normalBodyData), _scheduledExecutorService, _normalBodyHeaders).build());
    inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(_normalBodyData), _scheduledExecutorService, _normalBodyHeaders).build());
    inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(_headerLessBodyData), _scheduledExecutorService, Collections.<String, String>emptyMap()).build());
    inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(_normalBodyData), _scheduledExecutorService, _normalBodyHeaders).build());
    inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(new byte[0]), _scheduledExecutorService, _bodyLessHeaders).build());
    inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(new byte[0]), _scheduledExecutorService, Collections.<String, String>emptyMap()).build());
    inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(new byte[0]), _scheduledExecutorService, Collections.<String, String>emptyMap()).build());
    inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(_headerLessBodyData), _scheduledExecutorService, Collections.<String, String>emptyMap()).build());
    inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(_headerLessBodyData), _scheduledExecutorService, Collections.<String, String>emptyMap()).build());
    inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(_headerLessBodyData), _scheduledExecutorService, Collections.<String, String>emptyMap()).build());
    inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(_normalBodyData), _scheduledExecutorService, _normalBodyHeaders).build());
    inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(new byte[0]), _scheduledExecutorService, _bodyLessHeaders).build());
    final MultiPartMIMEWriter.Builder multiPartMIMEWriterBuilder = new MultiPartMIMEWriter.Builder("preamble", "epilogue").appendDataSources(inputStreamDataSources);
    Assert.assertEquals(multiPartMIMEWriterBuilder.getCurrentSize(), inputStreamDataSources.size());
    final MultiPartMIMEWriter multiPartMIMEWriter = multiPartMIMEWriterBuilder.build();
    final FutureCallback<ByteString> futureCallback = new FutureCallback<>();
    final FullEntityReader fullEntityReader = new FullEntityReader(futureCallback);
    multiPartMIMEWriter.getEntityStream().setReader(fullEntityReader);
    futureCallback.get(_testTimeout, TimeUnit.MILLISECONDS);
    final StreamRequest multiPartMIMEStreamRequest = MultiPartMIMEStreamRequestFactory.generateMultiPartMIMEStreamRequest(URI.create("localhost"), "mixed", multiPartMIMEWriter, Collections.<String, String>emptyMap());
    JavaxMailMultiPartMIMEReader javaxMailMultiPartMIMEReader = new JavaxMailMultiPartMIMEReader(multiPartMIMEStreamRequest.getHeader(MultiPartMIMEUtils.CONTENT_TYPE_HEADER), futureCallback.get());
    javaxMailMultiPartMIMEReader.parseRequestIntoParts();
    List<MIMEDataPart> dataSourceList = javaxMailMultiPartMIMEReader._dataSourceList;
    Assert.assertEquals(dataSourceList.size(), 12);
    for (int i = 0; i < dataSourceList.size(); i++) {
        Assert.assertEquals(dataSourceList.get(i), expectedParts.get(i));
    }
    // Javax mail incorrectly adds the CRLF for the first boundary to the end of the preamble, so we trim
    Assert.assertEquals(javaxMailMultiPartMIMEReader._preamble.trim(), "preamble");
}
Also used : ByteString(com.linkedin.data.ByteString) ArrayList(java.util.ArrayList) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) ByteArrayInputStream(java.io.ByteArrayInputStream) MIMEDataPart(com.linkedin.multipart.utils.MIMEDataPart) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 4 with MIMEDataPart

use of com.linkedin.multipart.utils.MIMEDataPart in project rest.li by linkedin.

the class TestMIMEWriter method setup.

@BeforeTest
public void setup() {
    _normalBodyData = "abc".getBytes();
    _normalBodyHeaders = new HashMap<>();
    _normalBodyHeaders.put("simpleheader", "simplevalue");
    // Second body has no headers
    _headerLessBodyData = "def".getBytes();
    // Third body has only headers
    _bodyLessHeaders = new HashMap<>();
    _normalBodyHeaders.put("header1", "value1");
    _normalBodyHeaders.put("header2", "value2");
    _normalBodyHeaders.put("header3", "value3");
    _normalBody = new MIMEDataPart(ByteString.copy(_normalBodyData), _normalBodyHeaders);
    _headerLessBody = new MIMEDataPart(ByteString.copy(_headerLessBodyData), Collections.<String, String>emptyMap());
    _bodyLessBody = new MIMEDataPart(ByteString.empty(), _bodyLessHeaders);
    _purelyEmptyBody = new MIMEDataPart(ByteString.empty(), Collections.<String, String>emptyMap());
}
Also used : MIMEDataPart(com.linkedin.multipart.utils.MIMEDataPart) ByteString(com.linkedin.data.ByteString) BeforeTest(org.testng.annotations.BeforeTest)

Example 5 with MIMEDataPart

use of com.linkedin.multipart.utils.MIMEDataPart in project rest.li by linkedin.

the class TestMIMEIntegrationReaderWriter method dataSourceSetup.

@BeforeClass
public void dataSourceSetup() {
    scheduledExecutorService = Executors.newScheduledThreadPool(10);
    _normalBodyData = "some normal body that is relatively small".getBytes();
    _normalBodyHeaders = new HashMap<>();
    _normalBodyHeaders.put("simpleheader", "simplevalue");
    // Second body has no headers
    _headerLessBodyData = "a body without headers".getBytes();
    // Third body has only headers
    _bodyLessHeaders = new HashMap<>();
    _normalBodyHeaders.put("header1", "value1");
    _normalBodyHeaders.put("header2", "value2");
    _normalBodyHeaders.put("header3", "value3");
    _normalBody = new MIMEDataPart(ByteString.copy(_normalBodyData), _normalBodyHeaders);
    _headerLessBody = new MIMEDataPart(ByteString.copy(_headerLessBodyData), Collections.<String, String>emptyMap());
    _bodyLessBody = new MIMEDataPart(ByteString.empty(), _bodyLessHeaders);
    _purelyEmptyBody = new MIMEDataPart(ByteString.empty(), Collections.<String, String>emptyMap());
}
Also used : MIMEDataPart(com.linkedin.multipart.utils.MIMEDataPart) ByteString(com.linkedin.data.ByteString) BeforeClass(org.testng.annotations.BeforeClass)

Aggregations

MIMEDataPart (com.linkedin.multipart.utils.MIMEDataPart)6 ByteString (com.linkedin.data.ByteString)5 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)4 BeforeTest (org.testng.annotations.BeforeTest)4 FutureCallback (com.linkedin.common.callback.FutureCallback)3 FullEntityReader (com.linkedin.r2.message.stream.entitystream.FullEntityReader)3 Test (org.testng.annotations.Test)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 BeforeClass (org.testng.annotations.BeforeClass)1