|
| 1 | +/* |
| 2 | + * Copyright 2019-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.aws.outbound; |
| 18 | + |
| 19 | +import com.amazonaws.services.kinesis.producer.KinesisProducer; |
| 20 | +import com.amazonaws.services.kinesis.producer.UserRecord; |
| 21 | +import org.junit.jupiter.api.AfterEach; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.mockito.ArgumentCaptor; |
| 24 | +import org.mockito.Mockito; |
| 25 | + |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | +import org.springframework.context.annotation.Bean; |
| 28 | +import org.springframework.context.annotation.Configuration; |
| 29 | +import org.springframework.integration.annotation.ServiceActivator; |
| 30 | +import org.springframework.integration.aws.support.AwsHeaders; |
| 31 | +import org.springframework.integration.aws.support.KplBackpressureException; |
| 32 | +import org.springframework.integration.config.EnableIntegration; |
| 33 | +import org.springframework.integration.handler.advice.RequestHandlerRetryAdvice; |
| 34 | +import org.springframework.messaging.Message; |
| 35 | +import org.springframework.messaging.MessageChannel; |
| 36 | +import org.springframework.messaging.MessageHandler; |
| 37 | +import org.springframework.messaging.MessageHandlingException; |
| 38 | +import org.springframework.messaging.support.MessageBuilder; |
| 39 | +import org.springframework.retry.support.RetryTemplate; |
| 40 | +import org.springframework.test.annotation.DirtiesContext; |
| 41 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 42 | + |
| 43 | +import static org.assertj.core.api.Assertions.assertThat; |
| 44 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 45 | +import static org.mockito.ArgumentMatchers.any; |
| 46 | +import static org.mockito.BDDMockito.given; |
| 47 | +import static org.mockito.Mockito.clearInvocations; |
| 48 | +import static org.mockito.Mockito.mock; |
| 49 | +import static org.mockito.Mockito.verify; |
| 50 | + |
| 51 | +/** The class contains test cases for KplMessageHandler. |
| 52 | + * |
| 53 | + * @author Siddharth Jain |
| 54 | + * |
| 55 | + * @since 3.0.9 |
| 56 | + */ |
| 57 | +@SpringJUnitConfig |
| 58 | +@DirtiesContext |
| 59 | +public class KplMessageHandlerTests { |
| 60 | + |
| 61 | + @Autowired |
| 62 | + protected KinesisProducer kinesisProducer; |
| 63 | + |
| 64 | + @Autowired |
| 65 | + protected MessageChannel kinesisSendChannel; |
| 66 | + |
| 67 | + @Autowired |
| 68 | + protected KplMessageHandler kplMessageHandler; |
| 69 | + |
| 70 | + @Test |
| 71 | + @SuppressWarnings("unchecked") |
| 72 | + void kplMessageHandlerWithRawPayloadBackpressureDisabledSuccess() { |
| 73 | + given(this.kinesisProducer.addUserRecord(any(UserRecord.class))) |
| 74 | + .willReturn(mock()); |
| 75 | + final Message<?> message = MessageBuilder |
| 76 | + .withPayload("someMessage") |
| 77 | + .setHeader(AwsHeaders.PARTITION_KEY, "somePartitionKey") |
| 78 | + .setHeader(AwsHeaders.SEQUENCE_NUMBER, "10") |
| 79 | + .setHeader("someHeaderKey", "someHeaderValue") |
| 80 | + .build(); |
| 81 | + |
| 82 | + ArgumentCaptor<UserRecord> userRecordRequestArgumentCaptor = ArgumentCaptor |
| 83 | + .forClass(UserRecord.class); |
| 84 | + this.kplMessageHandler.setBackPressureThreshold(0); |
| 85 | + this.kinesisSendChannel.send(message); |
| 86 | + verify(this.kinesisProducer).addUserRecord(userRecordRequestArgumentCaptor.capture()); |
| 87 | + verify(this.kinesisProducer, Mockito.never()).getOutstandingRecordsCount(); |
| 88 | + UserRecord userRecord = userRecordRequestArgumentCaptor.getValue(); |
| 89 | + assertThat(userRecord.getStreamName()).isEqualTo("someStream"); |
| 90 | + assertThat(userRecord.getPartitionKey()).isEqualTo("somePartitionKey"); |
| 91 | + assertThat(userRecord.getExplicitHashKey()).isNull(); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + @SuppressWarnings("unchecked") |
| 96 | + void kplMessageHandlerWithRawPayloadBackpressureEnabledCapacityAvailable() { |
| 97 | + given(this.kinesisProducer.addUserRecord(any(UserRecord.class))) |
| 98 | + .willReturn(mock()); |
| 99 | + this.kplMessageHandler.setBackPressureThreshold(2); |
| 100 | + given(this.kinesisProducer.getOutstandingRecordsCount()) |
| 101 | + .willReturn(1); |
| 102 | + final Message<?> message = MessageBuilder |
| 103 | + .withPayload("someMessage") |
| 104 | + .setHeader(AwsHeaders.PARTITION_KEY, "somePartitionKey") |
| 105 | + .setHeader(AwsHeaders.SEQUENCE_NUMBER, "10") |
| 106 | + .setHeader("someHeaderKey", "someHeaderValue") |
| 107 | + .build(); |
| 108 | + |
| 109 | + ArgumentCaptor<UserRecord> userRecordRequestArgumentCaptor = ArgumentCaptor |
| 110 | + .forClass(UserRecord.class); |
| 111 | + |
| 112 | + this.kinesisSendChannel.send(message); |
| 113 | + verify(this.kinesisProducer).addUserRecord(userRecordRequestArgumentCaptor.capture()); |
| 114 | + verify(this.kinesisProducer).getOutstandingRecordsCount(); |
| 115 | + UserRecord userRecord = userRecordRequestArgumentCaptor.getValue(); |
| 116 | + assertThat(userRecord.getStreamName()).isEqualTo("someStream"); |
| 117 | + assertThat(userRecord.getPartitionKey()).isEqualTo("somePartitionKey"); |
| 118 | + assertThat(userRecord.getExplicitHashKey()).isNull(); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + @SuppressWarnings("unchecked") |
| 123 | + void kplMessageHandlerWithRawPayloadBackpressureEnabledCapacityInsufficient() { |
| 124 | + given(this.kinesisProducer.addUserRecord(any(UserRecord.class))) |
| 125 | + .willReturn(mock()); |
| 126 | + this.kplMessageHandler.setBackPressureThreshold(2); |
| 127 | + given(this.kinesisProducer.getOutstandingRecordsCount()) |
| 128 | + .willReturn(5); |
| 129 | + final Message<?> message = MessageBuilder |
| 130 | + .withPayload("someMessage") |
| 131 | + .setHeader(AwsHeaders.PARTITION_KEY, "somePartitionKey") |
| 132 | + .setHeader(AwsHeaders.SEQUENCE_NUMBER, "10") |
| 133 | + .setHeader("someHeaderKey", "someHeaderValue") |
| 134 | + .build(); |
| 135 | + |
| 136 | + assertThatExceptionOfType(RuntimeException.class) |
| 137 | + .isThrownBy(() -> this.kinesisSendChannel.send(message)) |
| 138 | + .withCauseInstanceOf(MessageHandlingException.class) |
| 139 | + .withRootCauseExactlyInstanceOf(KplBackpressureException.class) |
| 140 | + .withStackTraceContaining("Cannot send record to Kinesis since buffer is at max capacity."); |
| 141 | + |
| 142 | + verify(this.kinesisProducer, Mockito.never()).addUserRecord(any(UserRecord.class)); |
| 143 | + verify(this.kinesisProducer).getOutstandingRecordsCount(); |
| 144 | + } |
| 145 | + |
| 146 | + @AfterEach |
| 147 | + public void tearDown() { |
| 148 | + clearInvocations(this.kinesisProducer); |
| 149 | + } |
| 150 | + |
| 151 | + @Configuration |
| 152 | + @EnableIntegration |
| 153 | + public static class ContextConfiguration { |
| 154 | + |
| 155 | + @Bean |
| 156 | + public KinesisProducer kinesisProducer() { |
| 157 | + return mock(); |
| 158 | + } |
| 159 | + |
| 160 | + @Bean |
| 161 | + public RequestHandlerRetryAdvice retryAdvice() { |
| 162 | + RequestHandlerRetryAdvice requestHandlerRetryAdvice = new RequestHandlerRetryAdvice(); |
| 163 | + requestHandlerRetryAdvice.setRetryTemplate(RetryTemplate.builder() |
| 164 | + .retryOn(KplBackpressureException.class) |
| 165 | + .exponentialBackoff(100, 2.0, 1000) |
| 166 | + .maxAttempts(3) |
| 167 | + .build()); |
| 168 | + return requestHandlerRetryAdvice; |
| 169 | + } |
| 170 | + |
| 171 | + @Bean |
| 172 | + @ServiceActivator(inputChannel = "kinesisSendChannel", adviceChain = "retryAdvice") |
| 173 | + public MessageHandler kplMessageHandler(KinesisProducer kinesisProducer) { |
| 174 | + KplMessageHandler kplMessageHandler = new KplMessageHandler(kinesisProducer); |
| 175 | + kplMessageHandler.setAsync(true); |
| 176 | + kplMessageHandler.setStream("someStream"); |
| 177 | + return kplMessageHandler; |
| 178 | + } |
| 179 | + |
| 180 | + } |
| 181 | + |
| 182 | +} |
0 commit comments