Search in sources :

Example 1 with AwsRequestFailureException

use of org.springframework.integration.aws.support.AwsRequestFailureException in project spring-integration-aws by spring-projects.

the class KinesisProducingMessageHandlerTests method testKinesisMessageHandler.

@Test
@SuppressWarnings("unchecked")
public void testKinesisMessageHandler() {
    Message<?> message = MessageBuilder.withPayload("message").build();
    try {
        this.kinesisSendChannel.send(message);
    } catch (Exception e) {
        assertThat(e).isInstanceOf(MessageHandlingException.class);
        assertThat(e.getCause()).isInstanceOf(IllegalStateException.class);
        assertThat(e.getMessage()).contains("'stream' must not be null for sending a Kinesis record");
    }
    this.kinesisMessageHandler.setStream("foo");
    try {
        this.kinesisSendChannel.send(message);
    } catch (Exception e) {
        assertThat(e).isInstanceOf(MessageHandlingException.class);
        assertThat(e.getCause()).isInstanceOf(IllegalStateException.class);
        assertThat(e.getMessage()).contains("'partitionKey' must not be null for sending a Kinesis record");
    }
    message = MessageBuilder.fromMessage(message).setHeader(AwsHeaders.PARTITION_KEY, "fooKey").setHeader(AwsHeaders.SEQUENCE_NUMBER, "10").build();
    this.kinesisSendChannel.send(message);
    Message<?> success = this.successChannel.receive(10000);
    assertThat(success.getHeaders().get(AwsHeaders.PARTITION_KEY)).isEqualTo("fooKey");
    assertThat(success.getHeaders().get(AwsHeaders.SEQUENCE_NUMBER)).isEqualTo("10");
    assertThat(success.getPayload()).isEqualTo("message");
    message = MessageBuilder.fromMessage(message).setHeader(AwsHeaders.PARTITION_KEY, "fooKey").setHeader(AwsHeaders.SEQUENCE_NUMBER, "10").build();
    this.kinesisSendChannel.send(message);
    Message<?> failed = this.errorChannel.receive(10000);
    AwsRequestFailureException putRecordFailure = (AwsRequestFailureException) failed.getPayload();
    assertThat(putRecordFailure.getCause().getMessage()).isEqualTo("putRecordRequestEx");
    assertThat(((PutRecordRequest) putRecordFailure.getRequest()).getStreamName()).isEqualTo("foo");
    assertThat(((PutRecordRequest) putRecordFailure.getRequest()).getPartitionKey()).isEqualTo("fooKey");
    assertThat(((PutRecordRequest) putRecordFailure.getRequest()).getSequenceNumberForOrdering()).isEqualTo("10");
    assertThat(((PutRecordRequest) putRecordFailure.getRequest()).getExplicitHashKey()).isNull();
    assertThat(((PutRecordRequest) putRecordFailure.getRequest()).getData()).isEqualTo(ByteBuffer.wrap("message".getBytes()));
    message = new GenericMessage<>(new PutRecordsRequest().withStreamName("myStream").withRecords(new PutRecordsRequestEntry().withData(ByteBuffer.wrap("test".getBytes())).withPartitionKey("testKey")));
    this.kinesisSendChannel.send(message);
    success = this.successChannel.receive(10000);
    assertThat(((PutRecordsRequest) success.getPayload()).getRecords()).containsExactlyInAnyOrder(new PutRecordsRequestEntry().withData(ByteBuffer.wrap("test".getBytes())).withPartitionKey("testKey"));
    message = new GenericMessage<>(new PutRecordsRequest().withStreamName("myStream").withRecords(new PutRecordsRequestEntry().withData(ByteBuffer.wrap("test".getBytes())).withPartitionKey("testKey")));
    this.kinesisSendChannel.send(message);
    failed = this.errorChannel.receive(10000);
    AwsRequestFailureException putRecordsFailure = (AwsRequestFailureException) failed.getPayload();
    assertThat(putRecordsFailure.getCause().getMessage()).isEqualTo("putRecordsRequestEx");
    assertThat(((PutRecordsRequest) putRecordsFailure.getRequest()).getStreamName()).isEqualTo("myStream");
    assertThat(((PutRecordsRequest) putRecordsFailure.getRequest()).getRecords()).containsExactlyInAnyOrder(new PutRecordsRequestEntry().withData(ByteBuffer.wrap("test".getBytes())).withPartitionKey("testKey"));
}
Also used : AwsRequestFailureException(org.springframework.integration.aws.support.AwsRequestFailureException) PutRecordsRequestEntry(com.amazonaws.services.kinesis.model.PutRecordsRequestEntry) PutRecordRequest(com.amazonaws.services.kinesis.model.PutRecordRequest) MessageHandlingException(org.springframework.messaging.MessageHandlingException) AwsRequestFailureException(org.springframework.integration.aws.support.AwsRequestFailureException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) PutRecordsRequest(com.amazonaws.services.kinesis.model.PutRecordsRequest) Test(org.junit.Test)

Aggregations

PutRecordRequest (com.amazonaws.services.kinesis.model.PutRecordRequest)1 PutRecordsRequest (com.amazonaws.services.kinesis.model.PutRecordsRequest)1 PutRecordsRequestEntry (com.amazonaws.services.kinesis.model.PutRecordsRequestEntry)1 Test (org.junit.Test)1 AwsRequestFailureException (org.springframework.integration.aws.support.AwsRequestFailureException)1 MessageHandlingException (org.springframework.messaging.MessageHandlingException)1