Search in sources :

Example 1 with MultiPartReaderFinishedException

use of com.linkedin.multipart.exceptions.MultiPartReaderFinishedException in project rest.li by linkedin.

the class TestMIMEReader method executeRequestAndAssert.

///////////////////////////////////////////////////////////////////////////////////////
private void executeRequestAndAssert(final ByteString payload, final int chunkSize, final MimeMultipart mimeMultipart) throws Exception {
    mockR2AndWrite(payload, chunkSize, mimeMultipart.getContentType());
    final CountDownLatch latch = new CountDownLatch(1);
    //We simulate _client.streamRequest(request, callback);
    _reader = MultiPartMIMEReader.createAndAcquireStream(_streamRequest);
    MultiPartMIMEReaderCallbackImpl testMultiPartMIMEReaderCallback = new MultiPartMIMEReaderCallbackImpl(latch);
    _reader.registerReaderCallback(testMultiPartMIMEReaderCallback);
    latch.await(_testTimeout, TimeUnit.MILLISECONDS);
    //Verify this is unusable.
    try {
        _reader.drainAllParts();
        Assert.fail();
    } catch (MultiPartReaderFinishedException multiPartReaderFinishedException) {
    //pass
    }
    List<SinglePartMIMEReaderCallbackImpl> singlePartMIMEReaderCallbacks = testMultiPartMIMEReaderCallback._singlePartMIMEReaderCallbacks;
    Assert.assertEquals(singlePartMIMEReaderCallbacks.size(), mimeMultipart.getCount());
    for (int i = 0; i < singlePartMIMEReaderCallbacks.size(); i++) {
        //Actual
        final SinglePartMIMEReaderCallbackImpl currentCallback = singlePartMIMEReaderCallbacks.get(i);
        //Expected
        final BodyPart currentExpectedPart = mimeMultipart.getBodyPart(i);
        //Construct expected headers and verify they match
        final Map<String, String> expectedHeaders = new HashMap<String, String>();
        @SuppressWarnings("unchecked") final Enumeration<Header> allHeaders = currentExpectedPart.getAllHeaders();
        while (allHeaders.hasMoreElements()) {
            final Header header = allHeaders.nextElement();
            expectedHeaders.put(header.getName(), header.getValue());
        }
        Assert.assertEquals(currentCallback._headers, expectedHeaders);
        Assert.assertNotNull(currentCallback._finishedData);
        //Verify the body matches
        if (currentExpectedPart.getContent() instanceof byte[]) {
            Assert.assertEquals(currentCallback._finishedData.copyBytes(), currentExpectedPart.getContent());
        } else {
            //Default is String
            Assert.assertEquals(new String(currentCallback._finishedData.copyBytes()), currentExpectedPart.getContent());
        }
    }
    Assert.assertTrue(_reader.haveAllPartsFinished());
    //Mock verifies
    verify(_streamRequest, times(1)).getEntityStream();
    verify(_streamRequest, times(1)).getHeader(HEADER_CONTENT_TYPE);
    verify(_entityStream, times(1)).setReader(isA(MultiPartMIMEReader.R2MultiPartMIMEReader.class));
    final int expectedRequests = (int) Math.ceil((double) payload.length() / chunkSize);
    //One more expected request because we have to make the last call to get called onDone().
    verify(_readHandle, times(expectedRequests + 1)).request(1);
    verifyNoMoreInteractions(_streamRequest);
    verifyNoMoreInteractions(_entityStream);
    verifyNoMoreInteractions(_readHandle);
}
Also used : MultiPartReaderFinishedException(com.linkedin.multipart.exceptions.MultiPartReaderFinishedException) MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) HashMap(java.util.HashMap) ByteString(com.linkedin.data.ByteString) CountDownLatch(java.util.concurrent.CountDownLatch) Header(javax.mail.Header)

Example 2 with MultiPartReaderFinishedException

use of com.linkedin.multipart.exceptions.MultiPartReaderFinishedException in project rest.li by linkedin.

the class TestMIMEReaderClientCallbackExceptions method testMultiPartMIMEReaderCallbackExceptionOnDrainComplete.

@Test(dataProvider = "allTypesOfBodiesDataSource")
public void testMultiPartMIMEReaderCallbackExceptionOnDrainComplete(final int chunkSize, final List<MimeBodyPart> bodyPartList) throws Exception {
    MimeMultipart multiPartMimeBody = new MimeMultipart();
    //Add your body parts
    for (final MimeBodyPart bodyPart : bodyPartList) {
        multiPartMimeBody.addBodyPart(bodyPart);
    }
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    multiPartMimeBody.writeTo(byteArrayOutputStream);
    final ByteString requestPayload = ByteString.copy(byteArrayOutputStream.toByteArray());
    final CountDownLatch countDownLatch = executeRequestPartialReadWithException(requestPayload, chunkSize, multiPartMimeBody.getContentType(), MultiPartMIMEThrowOnFlag.THROW_ON_DRAIN_COMPLETE, SinglePartMIMEThrowOnFlag.NO_THROW);
    countDownLatch.await(_testTimeout, TimeUnit.MILLISECONDS);
    Assert.assertTrue(_currentMultiPartMIMEReaderCallback.getStreamError() instanceof IllegalMonitorStateException);
    Assert.assertEquals(_currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks().size(), 0);
    //Verify this is unusable.
    try {
        _currentMultiPartMIMEReaderCallback.getReader().drainAllParts();
        Assert.fail();
    } catch (MultiPartReaderFinishedException multiPartReaderFinishedException) {
    //pass
    }
}
Also used : MultiPartReaderFinishedException(com.linkedin.multipart.exceptions.MultiPartReaderFinishedException) MimeMultipart(javax.mail.internet.MimeMultipart) ByteString(com.linkedin.data.ByteString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 3 with MultiPartReaderFinishedException

use of com.linkedin.multipart.exceptions.MultiPartReaderFinishedException in project rest.li by linkedin.

the class TestMIMEReaderClientCallbackExceptions method testSinglePartMIMEReaderCallbackExceptionOnDrainComplete.

@Test(dataProvider = "allTypesOfBodiesDataSource")
public void testSinglePartMIMEReaderCallbackExceptionOnDrainComplete(final int chunkSize, final List<MimeBodyPart> bodyPartList) throws Exception {
    MimeMultipart multiPartMimeBody = new MimeMultipart();
    //Add your body parts
    for (final MimeBodyPart bodyPart : bodyPartList) {
        multiPartMimeBody.addBodyPart(bodyPart);
    }
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    multiPartMimeBody.writeTo(byteArrayOutputStream);
    final ByteString requestPayload = ByteString.copy(byteArrayOutputStream.toByteArray());
    final CountDownLatch countDownLatch = executeRequestPartialReadWithException(requestPayload, chunkSize, multiPartMimeBody.getContentType(), MultiPartMIMEThrowOnFlag.NO_THROW, SinglePartMIMEThrowOnFlag.THROW_ON_DRAIN_COMPLETE);
    countDownLatch.await(_testTimeout, TimeUnit.MILLISECONDS);
    Assert.assertTrue(_currentMultiPartMIMEReaderCallback.getStreamError() instanceof IllegalMonitorStateException);
    //Verify these are unusable.
    try {
        _currentMultiPartMIMEReaderCallback.getReader().drainAllParts();
        Assert.fail();
    } catch (MultiPartReaderFinishedException multiPartReaderFinishedException) {
    //pass
    }
    Assert.assertEquals(_currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks().size(), 1);
    Assert.assertTrue(_currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks().get(0).getStreamError() instanceof IllegalMonitorStateException);
    try {
        _currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks().get(0).getSinglePartMIMEReader().requestPartData();
        Assert.fail();
    } catch (SinglePartFinishedException singlePartFinishedException) {
    //pass
    }
}
Also used : MultiPartReaderFinishedException(com.linkedin.multipart.exceptions.MultiPartReaderFinishedException) MimeMultipart(javax.mail.internet.MimeMultipart) ByteString(com.linkedin.data.ByteString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) CountDownLatch(java.util.concurrent.CountDownLatch) SinglePartFinishedException(com.linkedin.multipart.exceptions.SinglePartFinishedException) Test(org.testng.annotations.Test)

Example 4 with MultiPartReaderFinishedException

use of com.linkedin.multipart.exceptions.MultiPartReaderFinishedException in project rest.li by linkedin.

the class TestMIMEReaderClientCallbackExceptions method testSinglePartMIMEReaderCallbackExceptionOnPartDataAvailable.

///////////////////////////////////////////////////////////////////////////////////////
//SinglePartMIMEReader callback invocations throwing exceptions:
//These tests all verify the resilience of the single part mime reader when single part mime reader client callbacks throw runtime exceptions
@Test(dataProvider = "allTypesOfBodiesDataSource")
public void testSinglePartMIMEReaderCallbackExceptionOnPartDataAvailable(final int chunkSize, final List<MimeBodyPart> bodyPartList) throws Exception {
    MimeMultipart multiPartMimeBody = new MimeMultipart();
    //Add your body parts
    for (final MimeBodyPart bodyPart : bodyPartList) {
        multiPartMimeBody.addBodyPart(bodyPart);
    }
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    multiPartMimeBody.writeTo(byteArrayOutputStream);
    final ByteString requestPayload = ByteString.copy(byteArrayOutputStream.toByteArray());
    final CountDownLatch countDownLatch = executeRequestPartialReadWithException(requestPayload, chunkSize, multiPartMimeBody.getContentType(), MultiPartMIMEThrowOnFlag.NO_THROW, SinglePartMIMEThrowOnFlag.THROW_ON_PART_DATA_AVAILABLE);
    countDownLatch.await(_testTimeout, TimeUnit.MILLISECONDS);
    Assert.assertTrue(_currentMultiPartMIMEReaderCallback.getStreamError() instanceof IllegalMonitorStateException);
    //Verify this is unusable.
    try {
        _currentMultiPartMIMEReaderCallback.getReader().drainAllParts();
        Assert.fail();
    } catch (MultiPartReaderFinishedException multiPartReaderFinishedException) {
    //pass
    }
    Assert.assertEquals(_currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks().size(), 1);
    Assert.assertTrue(_currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks().get(0).getStreamError() instanceof IllegalMonitorStateException);
    try {
        _currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks().get(0).getSinglePartMIMEReader().requestPartData();
        Assert.fail();
    } catch (SinglePartFinishedException singlePartFinishedException) {
    //pass
    }
}
Also used : MultiPartReaderFinishedException(com.linkedin.multipart.exceptions.MultiPartReaderFinishedException) MimeMultipart(javax.mail.internet.MimeMultipart) ByteString(com.linkedin.data.ByteString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) CountDownLatch(java.util.concurrent.CountDownLatch) SinglePartFinishedException(com.linkedin.multipart.exceptions.SinglePartFinishedException) Test(org.testng.annotations.Test)

Example 5 with MultiPartReaderFinishedException

use of com.linkedin.multipart.exceptions.MultiPartReaderFinishedException in project rest.li by linkedin.

the class TestMIMEReaderClientCallbackExceptions method testSinglePartMIMEReaderCallbackExceptionOnFinished.

@Test(dataProvider = "allTypesOfBodiesDataSource")
public void testSinglePartMIMEReaderCallbackExceptionOnFinished(final int chunkSize, final List<MimeBodyPart> bodyPartList) throws Exception {
    MimeMultipart multiPartMimeBody = new MimeMultipart();
    //Add your body parts
    for (final MimeBodyPart bodyPart : bodyPartList) {
        multiPartMimeBody.addBodyPart(bodyPart);
    }
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    multiPartMimeBody.writeTo(byteArrayOutputStream);
    final ByteString requestPayload = ByteString.copy(byteArrayOutputStream.toByteArray());
    final CountDownLatch countDownLatch = executeRequestPartialReadWithException(requestPayload, chunkSize, multiPartMimeBody.getContentType(), MultiPartMIMEThrowOnFlag.NO_THROW, SinglePartMIMEThrowOnFlag.THROW_ON_FINISHED);
    countDownLatch.await(_testTimeout, TimeUnit.MILLISECONDS);
    Assert.assertTrue(_currentMultiPartMIMEReaderCallback.getStreamError() instanceof IllegalMonitorStateException);
    //Verify this is unusable.
    try {
        _currentMultiPartMIMEReaderCallback.getReader().drainAllParts();
        Assert.fail();
    } catch (MultiPartReaderFinishedException multiPartReaderFinishedException) {
    //pass
    }
    Assert.assertEquals(_currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks().size(), 1);
    Assert.assertTrue(_currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks().get(0).getStreamError() instanceof IllegalMonitorStateException);
    try {
        _currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks().get(0).getSinglePartMIMEReader().requestPartData();
        Assert.fail();
    } catch (SinglePartFinishedException singlePartFinishedException) {
    //pass
    }
}
Also used : MultiPartReaderFinishedException(com.linkedin.multipart.exceptions.MultiPartReaderFinishedException) MimeMultipart(javax.mail.internet.MimeMultipart) ByteString(com.linkedin.data.ByteString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) CountDownLatch(java.util.concurrent.CountDownLatch) SinglePartFinishedException(com.linkedin.multipart.exceptions.SinglePartFinishedException) Test(org.testng.annotations.Test)

Aggregations

MultiPartReaderFinishedException (com.linkedin.multipart.exceptions.MultiPartReaderFinishedException)11 CountDownLatch (java.util.concurrent.CountDownLatch)9 ByteString (com.linkedin.data.ByteString)8 MimeBodyPart (javax.mail.internet.MimeBodyPart)8 Test (org.testng.annotations.Test)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 MimeMultipart (javax.mail.internet.MimeMultipart)7 SinglePartFinishedException (com.linkedin.multipart.exceptions.SinglePartFinishedException)4 StreamBusyException (com.linkedin.multipart.exceptions.StreamBusyException)2 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)2 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)2 MultiPartIllegalFormatException (com.linkedin.multipart.exceptions.MultiPartIllegalFormatException)1 HashMap (java.util.HashMap)1 BodyPart (javax.mail.BodyPart)1 Header (javax.mail.Header)1