use of software.amazon.awssdk.services.s3.model.S3Response in project aws-lambda-powertools-java by awslabs.
the class LoggingOrderTest method testThatLoggingAnnotationActsLast.
/**
* The SQSEvent payload will be altered by the @SqsLargeMessage annotation. Logging of the event should happen
* after the event has been altered
*/
@Test
public void testThatLoggingAnnotationActsLast() throws IOException {
ResponseInputStream<GetObjectResponse> s3Response = new ResponseInputStream<>(GetObjectResponse.builder().build(), AbortableInputStream.create(new ByteArrayInputStream("A big message".getBytes())));
when(s3Client.getObject(any(GetObjectRequest.class))).thenReturn(s3Response);
SQSEvent sqsEvent = messageWithBody("[\"software.amazon.payloadoffloading.PayloadS3Pointer\",{\"s3BucketName\":\"" + BUCKET_NAME + "\",\"s3Key\":\"" + BUCKET_KEY + "\"}]");
LoggingOrderMessageHandler requestHandler = new LoggingOrderMessageHandler();
requestHandler.handleRequest(sqsEvent, context);
assertThat(Files.lines(Paths.get("target/logfile.json"))).hasSize(2).satisfies(line -> {
Map<String, Object> actual = parseToMap(line.get(0));
String message = actual.get("message").toString();
assertThat(message).contains("A big message");
});
}
use of software.amazon.awssdk.services.s3.model.S3Response in project aws-lambda-powertools-java by awslabs.
the class SqsUtilsLargeMessageTest method testLargeMessageDeleteFromS3Toggle.
@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testLargeMessageDeleteFromS3Toggle(boolean deleteS3Payload) {
ResponseInputStream<GetObjectResponse> s3Response = new ResponseInputStream<>(GetObjectResponse.builder().build(), AbortableInputStream.create(new ByteArrayInputStream("A big message".getBytes())));
when(s3Client.getObject(any(GetObjectRequest.class))).thenReturn(s3Response);
SQSEvent sqsEvent = messageWithBody("[\"software.amazon.payloadoffloading.PayloadS3Pointer\",{\"s3BucketName\":\"" + BUCKET_NAME + "\",\"s3Key\":\"" + BUCKET_KEY + "\"}]");
Map<String, String> sqsMessage = SqsUtils.enrichedMessageFromS3(sqsEvent, deleteS3Payload, sqsMessages -> {
Map<String, String> someBusinessLogic = new HashMap<>();
someBusinessLogic.put("Message", sqsMessages.get(0).getBody());
return someBusinessLogic;
});
assertThat(sqsMessage).hasSize(1).containsEntry("Message", "A big message");
if (deleteS3Payload) {
ArgumentCaptor<DeleteObjectRequest> delete = ArgumentCaptor.forClass(DeleteObjectRequest.class);
verify(s3Client).deleteObject(delete.capture());
Assertions.assertThat(delete.getValue()).satisfies((Consumer<DeleteObjectRequest>) deleteObjectRequest -> {
assertThat(deleteObjectRequest.bucket()).isEqualTo(BUCKET_NAME);
assertThat(deleteObjectRequest.key()).isEqualTo(BUCKET_KEY);
});
} else {
verify(s3Client, never()).deleteObject(any(DeleteObjectRequest.class));
}
}
use of software.amazon.awssdk.services.s3.model.S3Response in project aws-lambda-powertools-java by awslabs.
the class SqsUtilsLargeMessageTest method testLargeMessage.
@Test
public void testLargeMessage() {
ResponseInputStream<GetObjectResponse> s3Response = new ResponseInputStream<>(GetObjectResponse.builder().build(), AbortableInputStream.create(new ByteArrayInputStream("A big message".getBytes())));
when(s3Client.getObject(any(GetObjectRequest.class))).thenReturn(s3Response);
SQSEvent sqsEvent = messageWithBody("[\"software.amazon.payloadoffloading.PayloadS3Pointer\",{\"s3BucketName\":\"" + BUCKET_NAME + "\",\"s3Key\":\"" + BUCKET_KEY + "\"}]");
Map<String, String> sqsMessage = SqsUtils.enrichedMessageFromS3(sqsEvent, sqsMessages -> {
Map<String, String> someBusinessLogic = new HashMap<>();
someBusinessLogic.put("Message", sqsMessages.get(0).getBody());
return someBusinessLogic;
});
assertThat(sqsMessage).hasSize(1).containsEntry("Message", "A big message");
ArgumentCaptor<DeleteObjectRequest> delete = ArgumentCaptor.forClass(DeleteObjectRequest.class);
verify(s3Client).deleteObject(delete.capture());
Assertions.assertThat(delete.getValue()).satisfies((Consumer<DeleteObjectRequest>) deleteObjectRequest -> {
assertThat(deleteObjectRequest.bucket()).isEqualTo(BUCKET_NAME);
assertThat(deleteObjectRequest.key()).isEqualTo(BUCKET_KEY);
});
}
use of software.amazon.awssdk.services.s3.model.S3Response in project aws-lambda-powertools-java by awslabs.
the class SqsUtilsLargeMessageTest method shouldNotProcessSmallMessageBody.
@Test
public void shouldNotProcessSmallMessageBody() {
ResponseInputStream<GetObjectResponse> s3Response = new ResponseInputStream<>(GetObjectResponse.builder().build(), AbortableInputStream.create(new ByteArrayInputStream("A big message".getBytes())));
when(s3Client.getObject(any(GetObjectRequest.class))).thenReturn(s3Response);
SQSEvent sqsEvent = messageWithBody("This is small message");
Map<String, String> sqsMessage = SqsUtils.enrichedMessageFromS3(sqsEvent, sqsMessages -> {
Map<String, String> someBusinessLogic = new HashMap<>();
someBusinessLogic.put("Message", sqsMessages.get(0).getBody());
return someBusinessLogic;
});
assertThat(sqsMessage).containsEntry("Message", "This is small message");
verifyNoInteractions(s3Client);
}
use of software.amazon.awssdk.services.s3.model.S3Response in project aws-lambda-powertools-java by awslabs.
the class SqsLargeMessageAspectTest method shouldFailEntireBatchIfFailedProcessingDownloadMessageFromS3.
@Test
public void shouldFailEntireBatchIfFailedProcessingDownloadMessageFromS3() {
ResponseInputStream<GetObjectResponse> s3Response = new ResponseInputStream<>(GetObjectResponse.builder().build(), AbortableInputStream.create(new StringInputStream("test") {
@Override
public void close() throws IOException {
throw new IOException("Failed");
}
}));
when(s3Client.getObject(any(GetObjectRequest.class))).thenReturn(s3Response);
String messageBody = "[\"software.amazon.payloadoffloading.PayloadS3Pointer\",{\"s3BucketName\":\"" + BUCKET_NAME + "\",\"s3Key\":\"" + BUCKET_KEY + "\"}]";
SQSEvent sqsEvent = messageWithBody(messageBody);
assertThatExceptionOfType(FailedProcessingLargePayloadException.class).isThrownBy(() -> requestHandler.handleRequest(sqsEvent, context)).withCauseInstanceOf(IOException.class);
verify(s3Client, never()).deleteObject(any(DeleteObjectRequest.class));
}
Aggregations