Search in sources :

Example 1 with S3Response

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");
    });
}
Also used : GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) SQSEvent(com.amazonaws.services.lambda.runtime.events.SQSEvent) ResponseInputStream(software.amazon.awssdk.core.ResponseInputStream) LoggingOrderMessageHandler(software.amazon.lambda.powertools.testsuite.handler.LoggingOrderMessageHandler) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) Test(org.junit.jupiter.api.Test)

Example 2 with S3Response

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));
    }
}
Also used : DeleteObjectRequest(software.amazon.awssdk.services.s3.model.DeleteObjectRequest) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockitoAnnotations.openMocks(org.mockito.MockitoAnnotations.openMocks) BeforeEach(org.junit.jupiter.api.BeforeEach) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) SQSEvent(com.amazonaws.services.lambda.runtime.events.SQSEvent) Collections.singletonList(java.util.Collections.singletonList) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) ArgumentCaptor(org.mockito.ArgumentCaptor) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Assertions(org.assertj.core.api.Assertions) ResponseInputStream(software.amazon.awssdk.core.ResponseInputStream) SQSMessage(com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage) MethodSource(org.junit.jupiter.params.provider.MethodSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) S3Client(software.amazon.awssdk.services.s3.S3Client) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Arguments(org.junit.jupiter.params.provider.Arguments) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) AbortableInputStream(software.amazon.awssdk.http.AbortableInputStream) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) DeleteObjectRequest(software.amazon.awssdk.services.s3.model.DeleteObjectRequest) StringInputStream(software.amazon.awssdk.utils.StringInputStream) Mockito.never(org.mockito.Mockito.never) Stream(java.util.stream.Stream) SqsLargeMessageAspect(software.amazon.lambda.powertools.sqs.internal.SqsLargeMessageAspect) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) SQSEvent(com.amazonaws.services.lambda.runtime.events.SQSEvent) HashMap(java.util.HashMap) ResponseInputStream(software.amazon.awssdk.core.ResponseInputStream) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with S3Response

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);
    });
}
Also used : DeleteObjectRequest(software.amazon.awssdk.services.s3.model.DeleteObjectRequest) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockitoAnnotations.openMocks(org.mockito.MockitoAnnotations.openMocks) BeforeEach(org.junit.jupiter.api.BeforeEach) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) SQSEvent(com.amazonaws.services.lambda.runtime.events.SQSEvent) Collections.singletonList(java.util.Collections.singletonList) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) ArgumentCaptor(org.mockito.ArgumentCaptor) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Assertions(org.assertj.core.api.Assertions) ResponseInputStream(software.amazon.awssdk.core.ResponseInputStream) SQSMessage(com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage) MethodSource(org.junit.jupiter.params.provider.MethodSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) S3Client(software.amazon.awssdk.services.s3.S3Client) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Arguments(org.junit.jupiter.params.provider.Arguments) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) AbortableInputStream(software.amazon.awssdk.http.AbortableInputStream) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) DeleteObjectRequest(software.amazon.awssdk.services.s3.model.DeleteObjectRequest) StringInputStream(software.amazon.awssdk.utils.StringInputStream) Mockito.never(org.mockito.Mockito.never) Stream(java.util.stream.Stream) SqsLargeMessageAspect(software.amazon.lambda.powertools.sqs.internal.SqsLargeMessageAspect) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) SQSEvent(com.amazonaws.services.lambda.runtime.events.SQSEvent) HashMap(java.util.HashMap) ResponseInputStream(software.amazon.awssdk.core.ResponseInputStream) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with S3Response

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);
}
Also used : GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) SQSEvent(com.amazonaws.services.lambda.runtime.events.SQSEvent) HashMap(java.util.HashMap) ResponseInputStream(software.amazon.awssdk.core.ResponseInputStream) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with S3Response

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));
}
Also used : DeleteObjectRequest(software.amazon.awssdk.services.s3.model.DeleteObjectRequest) StringInputStream(software.amazon.awssdk.utils.StringInputStream) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) SQSEvent(com.amazonaws.services.lambda.runtime.events.SQSEvent) ResponseInputStream(software.amazon.awssdk.core.ResponseInputStream) IOException(java.io.IOException) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

SQSEvent (com.amazonaws.services.lambda.runtime.events.SQSEvent)6 Test (org.junit.jupiter.api.Test)6 ResponseInputStream (software.amazon.awssdk.core.ResponseInputStream)6 GetObjectRequest (software.amazon.awssdk.services.s3.model.GetObjectRequest)6 GetObjectResponse (software.amazon.awssdk.services.s3.model.GetObjectResponse)6 IOException (java.io.IOException)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 DeleteObjectRequest (software.amazon.awssdk.services.s3.model.DeleteObjectRequest)4 StringInputStream (software.amazon.awssdk.utils.StringInputStream)4 HashMap (java.util.HashMap)3 SqsLargeMessageAspect (software.amazon.lambda.powertools.sqs.internal.SqsLargeMessageAspect)3 SQSMessage (com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage)2 Collections.singletonList (java.util.Collections.singletonList)2 Map (java.util.Map)2 Consumer (java.util.function.Consumer)2 Stream (java.util.stream.Stream)2 Assertions (org.assertj.core.api.Assertions)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)2