use of com.linkedin.multipart.exceptions.MultiPartReaderFinishedException in project rest.li by linkedin.
the class TestMIMEReaderDrain method executeRequestWithDrainStrategy.
///////////////////////////////////////////////////////////////////////////////////////
private void executeRequestWithDrainStrategy(final int chunkSize, final List<MimeBodyPart> bodyPartList, final String drainStrategy, final String serverHeaderPrefix) 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());
_currentMimeMultipartBody = multiPartMimeBody;
mockR2AndWrite(requestPayload, chunkSize, multiPartMimeBody.getContentType());
final CountDownLatch latch = new CountDownLatch(1);
MultiPartMIMEReader reader = MultiPartMIMEReader.createAndAcquireStream(_streamRequest);
_currentMultiPartMIMEReaderCallback = new MultiPartMIMEDrainReaderCallbackImpl(latch, drainStrategy, reader);
reader.registerReaderCallback(_currentMultiPartMIMEReaderCallback);
latch.await(_testTimeout, TimeUnit.MILLISECONDS);
Assert.assertEquals(_currentMultiPartMIMEReaderCallback.getResponseHeaders().get(DRAIN_HEADER), serverHeaderPrefix + drainStrategy);
try {
reader.drainAllParts();
Assert.fail();
} catch (MultiPartReaderFinishedException multiPartReaderFinishedException) {
}
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) requestPayload.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);
}
use of com.linkedin.multipart.exceptions.MultiPartReaderFinishedException in project rest.li by linkedin.
the class TestMIMEReaderExceptions method executeRequestWithDesiredException.
///////////////////////////////////////////////////////////////////////////////////////
private void executeRequestWithDesiredException(final ByteString requestPayload, final int chunkSize, final String contentTypeHeader, final String desiredExceptionMessage) throws Exception {
mockR2AndWrite(requestPayload, chunkSize, contentTypeHeader);
final CountDownLatch latch = new CountDownLatch(1);
MultiPartMIMEReader reader = MultiPartMIMEReader.createAndAcquireStream(_streamRequest);
_currentMultiPartMIMEReaderCallback = new MultiPartMIMEExceptionReaderCallbackImpl(latch, reader);
reader.registerReaderCallback(_currentMultiPartMIMEReaderCallback);
latch.await(_testTimeout, TimeUnit.MILLISECONDS);
//Verify the correct exception was sent to the reader callback. The test itself will then verify
//if the correct error (if applicable) was sent to the single part reader callback.
Assert.assertTrue(_currentMultiPartMIMEReaderCallback.getStreamError() instanceof MultiPartIllegalFormatException);
Assert.assertEquals(_currentMultiPartMIMEReaderCallback.getStreamError().getMessage(), desiredExceptionMessage);
//Verify these are unusable.
try {
reader.drainAllParts();
Assert.fail();
} catch (MultiPartReaderFinishedException multiPartReaderFinishedException) {
//pass
}
//Unnecessary to verify how many times requestData on the read handle was called.
verify(_readHandle, atLeastOnce()).request(isA(Integer.class));
verify(_readHandle, times(1)).cancel();
verify(_streamRequest, times(1)).getEntityStream();
verify(_streamRequest, times(1)).getHeader(MIMETestUtils.HEADER_CONTENT_TYPE);
verify(_entityStream, times(1)).setReader(isA(MultiPartMIMEReader.R2MultiPartMIMEReader.class));
verifyNoMoreInteractions(_streamRequest);
verifyNoMoreInteractions(_entityStream);
}
use of com.linkedin.multipart.exceptions.MultiPartReaderFinishedException in project rest.li by linkedin.
the class TestMIMEReaderStateTransitions method testDrainAllPartsMultiPartMIMEReader.
@Test
public void testDrainAllPartsMultiPartMIMEReader() {
final EntityStream entityStream = mock(EntityStream.class);
final StreamRequest streamRequest = mock(StreamRequest.class);
when(streamRequest.getEntityStream()).thenReturn(entityStream);
when(streamRequest.getHeader(MultiPartMIMEUtils.CONTENT_TYPE_HEADER)).thenReturn("multipart/mixed; boundary=\"--123\"");
MultiPartMIMEReader reader = MultiPartMIMEReader.createAndAcquireStream(streamRequest);
//Test each possible exception:
reader.setState(MultiPartMIMEReader.MultiPartReaderState.FINISHED);
try {
reader.drainAllParts();
Assert.fail();
} catch (MultiPartReaderFinishedException multiPartReaderFinishedException) {
}
reader.setState(MultiPartMIMEReader.MultiPartReaderState.READING_EPILOGUE);
try {
reader.drainAllParts();
Assert.fail();
} catch (MultiPartReaderFinishedException multiPartReaderFinishedException) {
}
reader.setState(MultiPartMIMEReader.MultiPartReaderState.CALLBACK_BOUND_AND_READING_PREAMBLE);
try {
reader.drainAllParts();
Assert.fail();
} catch (StreamBusyException streamBusyException) {
}
reader.setState(MultiPartMIMEReader.MultiPartReaderState.DRAINING);
try {
reader.drainAllParts();
Assert.fail();
} catch (StreamBusyException streamBusyException) {
}
//This is the desired top level reader state
reader.setState(MultiPartMIMEReader.MultiPartReaderState.READING_PARTS);
final MultiPartMIMEReader.SinglePartMIMEReader singlePartMIMEReader = reader.new SinglePartMIMEReader(Collections.<String, String>emptyMap());
//This is a undesired single part state
singlePartMIMEReader.setState(MultiPartMIMEReader.SingleReaderState.REQUESTED_DATA);
reader.setCurrentSinglePartMIMEReader(singlePartMIMEReader);
try {
reader.drainAllParts();
Assert.fail();
} catch (StreamBusyException streamBusyException) {
}
}
use of com.linkedin.multipart.exceptions.MultiPartReaderFinishedException in project rest.li by linkedin.
the class TestMIMEReaderStateTransitions method testRegisterCallbackMultiPartMIMEReader.
//MultiPartMIMEReader exceptions:
@Test
public void testRegisterCallbackMultiPartMIMEReader() {
final EntityStream entityStream = mock(EntityStream.class);
final StreamRequest streamRequest = mock(StreamRequest.class);
when(streamRequest.getEntityStream()).thenReturn(entityStream);
when(streamRequest.getHeader(MultiPartMIMEUtils.CONTENT_TYPE_HEADER)).thenReturn("multipart/mixed; boundary=\"--123\"");
MultiPartMIMEReader reader = MultiPartMIMEReader.createAndAcquireStream(streamRequest);
//Test each possible exception:
reader.setState(MultiPartMIMEReader.MultiPartReaderState.FINISHED);
try {
reader.registerReaderCallback(EMPTY_MULTI_PART_MIME_READER_CALLBACK);
Assert.fail();
} catch (MultiPartReaderFinishedException multiPartReaderFinishedException) {
}
reader.setState(MultiPartMIMEReader.MultiPartReaderState.READING_EPILOGUE);
try {
reader.registerReaderCallback(EMPTY_MULTI_PART_MIME_READER_CALLBACK);
Assert.fail();
} catch (MultiPartReaderFinishedException multiPartReaderFinishedException) {
}
reader.setState(MultiPartMIMEReader.MultiPartReaderState.CALLBACK_BOUND_AND_READING_PREAMBLE);
try {
reader.registerReaderCallback(EMPTY_MULTI_PART_MIME_READER_CALLBACK);
Assert.fail();
} catch (StreamBusyException streamBusyException) {
}
reader.setState(MultiPartMIMEReader.MultiPartReaderState.DRAINING);
try {
reader.registerReaderCallback(EMPTY_MULTI_PART_MIME_READER_CALLBACK);
Assert.fail();
} catch (StreamBusyException streamBusyException) {
}
//This is a desired top level reader state
reader.setState(MultiPartMIMEReader.MultiPartReaderState.READING_PARTS);
final MultiPartMIMEReader.SinglePartMIMEReader singlePartMIMEReader = reader.new SinglePartMIMEReader(Collections.<String, String>emptyMap());
//This is a undesired single part state
singlePartMIMEReader.setState(MultiPartMIMEReader.SingleReaderState.REQUESTED_DATA);
reader.setCurrentSinglePartMIMEReader(singlePartMIMEReader);
try {
reader.registerReaderCallback(EMPTY_MULTI_PART_MIME_READER_CALLBACK);
Assert.fail();
} catch (StreamBusyException streamBusyException) {
}
}
use of com.linkedin.multipart.exceptions.MultiPartReaderFinishedException in project rest.li by linkedin.
the class TestMIMEReaderClientCallbackExceptions method testMultiPartMIMEReaderCallbackExceptionOnFinished.
@Test(dataProvider = "allTypesOfBodiesDataSource")
public void testMultiPartMIMEReaderCallbackExceptionOnFinished(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_FINISHED, SinglePartMIMEThrowOnFlag.NO_THROW);
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(), 6);
//callback threw
for (int i = 0; i < _currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks().size(); i++) {
Assert.assertNull(_currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks().get(i).getStreamError());
//Verify this is unusable.
try {
_currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks().get(i).getSinglePartMIMEReader().requestPartData();
Assert.fail();
} catch (SinglePartFinishedException singlePartFinishedException) {
//pass
}
}
}
Aggregations