Search in sources :

Example 1 with StreamBusyException

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

the class TestMIMEReaderStateTransitions method testSinglePartMIMEReaderVerifyState.

@Test
public void testSinglePartMIMEReaderVerifyState() {
    //This will cover drainPart() and most of requestPartData().
    //The caveat is that requestPartData() requires a callback to be registered. This
    //will be covered in the next test.
    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);
    final MultiPartMIMEReader.SinglePartMIMEReader singlePartMIMEReader = reader.new SinglePartMIMEReader(Collections.<String, String>emptyMap());
    singlePartMIMEReader.setState(MultiPartMIMEReader.SingleReaderState.FINISHED);
    try {
        singlePartMIMEReader.verifyUsableState();
        Assert.fail();
    } catch (SinglePartFinishedException singlePartFinishedException) {
    }
    singlePartMIMEReader.setState(MultiPartMIMEReader.SingleReaderState.REQUESTED_DATA);
    try {
        singlePartMIMEReader.verifyUsableState();
        Assert.fail();
    } catch (StreamBusyException streamBusyException) {
    }
    singlePartMIMEReader.setState(MultiPartMIMEReader.SingleReaderState.REQUESTED_DRAIN);
    try {
        singlePartMIMEReader.verifyUsableState();
        Assert.fail();
    } catch (StreamBusyException streamBusyException) {
    }
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) SinglePartFinishedException(com.linkedin.multipart.exceptions.SinglePartFinishedException) StreamBusyException(com.linkedin.multipart.exceptions.StreamBusyException) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Example 2 with StreamBusyException

use of com.linkedin.multipart.exceptions.StreamBusyException 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) {
    }
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) MultiPartReaderFinishedException(com.linkedin.multipart.exceptions.MultiPartReaderFinishedException) StreamBusyException(com.linkedin.multipart.exceptions.StreamBusyException) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Example 3 with StreamBusyException

use of com.linkedin.multipart.exceptions.StreamBusyException 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) {
    }
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) MultiPartReaderFinishedException(com.linkedin.multipart.exceptions.MultiPartReaderFinishedException) StreamBusyException(com.linkedin.multipart.exceptions.StreamBusyException) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Aggregations

StreamBusyException (com.linkedin.multipart.exceptions.StreamBusyException)3 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)3 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)3 Test (org.testng.annotations.Test)3 MultiPartReaderFinishedException (com.linkedin.multipart.exceptions.MultiPartReaderFinishedException)2 SinglePartFinishedException (com.linkedin.multipart.exceptions.SinglePartFinishedException)1