|
12 | 12 | import com.fasterxml.jackson.databind.ObjectMapper;
|
13 | 13 | import org.junit.jupiter.api.BeforeEach;
|
14 | 14 | import org.junit.jupiter.api.Test;
|
| 15 | +import org.mockito.ArgumentCaptor; |
15 | 16 | import software.amazon.awssdk.services.sqs.SqsClient;
|
16 | 17 | import software.amazon.awssdk.services.sqs.model.DeleteMessageBatchRequest;
|
17 | 18 | import software.amazon.awssdk.services.sqs.model.GetQueueAttributesRequest;
|
|
23 | 24 | import software.amazon.lambda.powertools.sqs.handlers.PartialBatchPartialFailureHandler;
|
24 | 25 | import software.amazon.lambda.powertools.sqs.handlers.PartialBatchSuccessHandler;
|
25 | 26 | import software.amazon.lambda.powertools.sqs.handlers.SqsMessageHandlerWithNonRetryableHandler;
|
| 27 | +import software.amazon.lambda.powertools.sqs.handlers.SqsMessageHandlerWithNonRetryableHandlerWithDelete; |
26 | 28 |
|
27 | 29 | import static org.assertj.core.api.Assertions.assertThat;
|
28 | 30 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
29 | 31 | import static org.mockito.ArgumentMatchers.any;
|
30 | 32 | import static org.mockito.Mockito.mock;
|
| 33 | +import static org.mockito.Mockito.never; |
31 | 34 | import static org.mockito.Mockito.reset;
|
32 | 35 | import static org.mockito.Mockito.times;
|
33 | 36 | import static org.mockito.Mockito.verify;
|
@@ -131,6 +134,137 @@ void shouldBatchProcessAndMoveNonRetryableExceptionToDlq() {
|
131 | 134 | verify(sqsClient).sendMessageBatch(any(Consumer.class));
|
132 | 135 | }
|
133 | 136 |
|
| 137 | + @Test |
| 138 | + void shouldBatchProcessAndDeleteNonRetryableExceptionMessage() { |
| 139 | + requestHandler = new SqsMessageHandlerWithNonRetryableHandlerWithDelete(); |
| 140 | + event.getRecords().get(0).setMessageId(""); |
| 141 | + |
| 142 | + requestHandler.handleRequest(event, context); |
| 143 | + |
| 144 | + verify(mockedRandom).nextInt(); |
| 145 | + ArgumentCaptor<DeleteMessageBatchRequest> captor = ArgumentCaptor.forClass(DeleteMessageBatchRequest.class); |
| 146 | + verify(sqsClient).deleteMessageBatch(captor.capture()); |
| 147 | + verify(sqsClient, never()).sendMessageBatch(any(Consumer.class)); |
| 148 | + verify(sqsClient, never()).getQueueAttributes(any(GetQueueAttributesRequest.class)); |
| 149 | + |
| 150 | + assertThat(captor.getValue()) |
| 151 | + .satisfies(deleteMessageBatchRequest -> assertThat(deleteMessageBatchRequest.entries()) |
| 152 | + .hasSize(2) |
| 153 | + .extracting("id") |
| 154 | + .contains("", "2e1424d4-f796-459a-8184-9c92662be6da")); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + void shouldBatchProcessAndFailWithExceptionForNonRetryableExceptionAndNoDlq() { |
| 159 | + requestHandler = new SqsMessageHandlerWithNonRetryableHandler(); |
| 160 | + |
| 161 | + event.getRecords().get(0).setMessageId(""); |
| 162 | + event.getRecords().forEach(sqsMessage -> sqsMessage.setEventSourceArn(sqsMessage.getEventSourceArn() + "-temp")); |
| 163 | + |
| 164 | + when(sqsClient.getQueueAttributes(any(GetQueueAttributesRequest.class))).thenReturn(GetQueueAttributesResponse.builder() |
| 165 | + .build()); |
| 166 | + |
| 167 | + assertThatExceptionOfType(SQSBatchProcessingException.class) |
| 168 | + .isThrownBy(() -> requestHandler.handleRequest(event, context)) |
| 169 | + .satisfies(e -> { |
| 170 | + assertThat(e.getExceptions()) |
| 171 | + .hasSize(1) |
| 172 | + .extracting("detailMessage") |
| 173 | + .containsExactly("Invalid message and was moved to DLQ"); |
| 174 | + |
| 175 | + assertThat(e.getFailures()) |
| 176 | + .hasSize(1) |
| 177 | + .extracting("messageId") |
| 178 | + .containsExactly(""); |
| 179 | + |
| 180 | + assertThat(e.successMessageReturnValues()) |
| 181 | + .hasSize(1) |
| 182 | + .contains("Success"); |
| 183 | + }); |
| 184 | + |
| 185 | + verify(mockedRandom).nextInt(); |
| 186 | + verify(sqsClient).deleteMessageBatch(any(DeleteMessageBatchRequest.class)); |
| 187 | + verify(sqsClient, never()).sendMessageBatch(any(Consumer.class)); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + void shouldBatchProcessAndFailWithExceptionForNonRetryableExceptionWhenFailedParsingPolicy() { |
| 192 | + requestHandler = new SqsMessageHandlerWithNonRetryableHandler(); |
| 193 | + event.getRecords().get(0).setMessageId(""); |
| 194 | + event.getRecords().forEach(sqsMessage -> sqsMessage.setEventSourceArn(sqsMessage.getEventSourceArn() + "-temp-queue")); |
| 195 | + |
| 196 | + HashMap<QueueAttributeName, String> attributes = new HashMap<>(); |
| 197 | + |
| 198 | + attributes.put(QueueAttributeName.REDRIVE_POLICY, "MalFormedRedrivePolicy"); |
| 199 | + |
| 200 | + when(sqsClient.getQueueAttributes(any(GetQueueAttributesRequest.class))).thenReturn(GetQueueAttributesResponse.builder() |
| 201 | + .attributes(attributes) |
| 202 | + .build()); |
| 203 | + |
| 204 | + assertThatExceptionOfType(SQSBatchProcessingException.class) |
| 205 | + .isThrownBy(() -> requestHandler.handleRequest(event, context)) |
| 206 | + .satisfies(e -> { |
| 207 | + assertThat(e.getExceptions()) |
| 208 | + .hasSize(1) |
| 209 | + .extracting("detailMessage") |
| 210 | + .containsExactly("Invalid message and was moved to DLQ"); |
| 211 | + |
| 212 | + assertThat(e.getFailures()) |
| 213 | + .hasSize(1) |
| 214 | + .extracting("messageId") |
| 215 | + .containsExactly(""); |
| 216 | + |
| 217 | + assertThat(e.successMessageReturnValues()) |
| 218 | + .hasSize(1) |
| 219 | + .contains("Success"); |
| 220 | + }); |
| 221 | + |
| 222 | + verify(mockedRandom).nextInt(); |
| 223 | + verify(sqsClient).deleteMessageBatch(any(DeleteMessageBatchRequest.class)); |
| 224 | + verify(sqsClient, never()).sendMessageBatch(any(Consumer.class)); |
| 225 | + } |
| 226 | + |
| 227 | + @Test |
| 228 | + void shouldBatchProcessAndMoveNonRetryableExceptionToDlqAndThrowException() throws IOException { |
| 229 | + requestHandler = new SqsMessageHandlerWithNonRetryableHandler(); |
| 230 | + event = MAPPER.readValue(this.getClass().getResource("/threeMessageSqsBatchEvent.json"), SQSEvent.class); |
| 231 | + |
| 232 | + event.getRecords().get(1).setMessageId(""); |
| 233 | + |
| 234 | + HashMap<QueueAttributeName, String> attributes = new HashMap<>(); |
| 235 | + |
| 236 | + attributes.put(QueueAttributeName.REDRIVE_POLICY, "{\n" + |
| 237 | + " \"deadLetterTargetArn\": \"arn:aws:sqs:us-east-2:123456789012:retry-queue\",\n" + |
| 238 | + " \"maxReceiveCount\": 2\n" + |
| 239 | + "}"); |
| 240 | + |
| 241 | + when(sqsClient.getQueueAttributes(any(GetQueueAttributesRequest.class))).thenReturn(GetQueueAttributesResponse.builder() |
| 242 | + .attributes(attributes) |
| 243 | + .build()); |
| 244 | + |
| 245 | + assertThatExceptionOfType(SQSBatchProcessingException.class) |
| 246 | + .isThrownBy(() -> requestHandler.handleRequest(event, context)) |
| 247 | + .satisfies(e -> { |
| 248 | + assertThat(e.getExceptions()) |
| 249 | + .hasSize(1) |
| 250 | + .extracting("detailMessage") |
| 251 | + .containsExactly("Invalid message and should be reprocessed"); |
| 252 | + |
| 253 | + assertThat(e.getFailures()) |
| 254 | + .hasSize(1) |
| 255 | + .extracting("messageId") |
| 256 | + .containsExactly("2e1424d4-f796-459a-9696-9c92662ba5da"); |
| 257 | + |
| 258 | + assertThat(e.successMessageReturnValues()) |
| 259 | + .hasSize(1) |
| 260 | + .contains("Success"); |
| 261 | + }); |
| 262 | + |
| 263 | + verify(mockedRandom).nextInt(); |
| 264 | + verify(sqsClient).deleteMessageBatch(any(DeleteMessageBatchRequest.class)); |
| 265 | + verify(sqsClient).sendMessageBatch(any(Consumer.class)); |
| 266 | + } |
| 267 | + |
134 | 268 | private void setupContext() {
|
135 | 269 | when(context.getFunctionName()).thenReturn("testFunction");
|
136 | 270 | when(context.getInvokedFunctionArn()).thenReturn("testArn");
|
|
0 commit comments