Search in sources :

Example 11 with S3AsyncClient

use of software.amazon.awssdk.services.s3.S3AsyncClient in project aws-sdk-java-v2 by aws.

the class GetObjectWithChecksumTest method async_getObject_with_customized_multipart_checksum.

@Test
public void async_getObject_with_customized_multipart_checksum(WireMockRuntimeInfo wm) {
    stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200).withHeader("x-amz-checksum-crc32", "abcdef==-12").withBody(EXAMPLE_RESPONSE_BODY)));
    S3AsyncClient s3Client = getAsyncClientBuilder(wm).overrideConfiguration(o -> o.addExecutionInterceptor(captureChecksumValidationInterceptor)).build();
    String response = s3Client.getObject(r -> r.bucket(EXAMPLE_BUCKET).key("key").checksumMode(ChecksumMode.ENABLED), AsyncResponseTransformer.toBytes()).join().asUtf8String();
    assertThat(response).isEqualTo("Hello world");
    assertThat(captureChecksumValidationInterceptor.responseValidation()).isEqualTo(ChecksumValidation.FORCE_SKIP);
    assertThat(captureChecksumValidationInterceptor.validationAlgorithm()).isNull();
}
Also used : ChecksumMode(software.amazon.awssdk.services.s3.model.ChecksumMode) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) WireMock.any(com.github.tomakehurst.wiremock.client.WireMock.any) StaticCredentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider) Function(java.util.function.Function) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) ResponseInputStream(software.amazon.awssdk.core.ResponseInputStream) AsyncResponseTransformer(software.amazon.awssdk.core.async.AsyncResponseTransformer) URI(java.net.URI) Region(software.amazon.awssdk.regions.Region) CaptureChecksumValidationInterceptor(software.amazon.awssdk.services.s3.utils.CaptureChecksumValidationInterceptor) ChecksumValidation(software.amazon.awssdk.core.checksums.ChecksumValidation) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) S3Client(software.amazon.awssdk.services.s3.S3Client) WireMockRuntimeInfo(com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) S3AsyncClientBuilder(software.amazon.awssdk.services.s3.S3AsyncClientBuilder) Algorithm(software.amazon.awssdk.core.checksums.Algorithm) S3ClientBuilder(software.amazon.awssdk.services.s3.S3ClientBuilder) WireMockTest(com.github.tomakehurst.wiremock.junit5.WireMockTest) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) WireMock.anyUrl(com.github.tomakehurst.wiremock.client.WireMock.anyUrl) BufferedReader(java.io.BufferedReader) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) InputStream(java.io.InputStream) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) Test(org.junit.jupiter.api.Test) WireMockTest(com.github.tomakehurst.wiremock.junit5.WireMockTest)

Example 12 with S3AsyncClient

use of software.amazon.awssdk.services.s3.S3AsyncClient in project aws-sdk-java-v2 by aws.

the class GetObjectWithChecksumTest method async_getObject_with_correct_http_checksum.

@Test
public void async_getObject_with_correct_http_checksum(WireMockRuntimeInfo wm) {
    stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200).withHeader("x-amz-checksum-crc32", "i9aeUg==").withBody(EXAMPLE_RESPONSE_BODY)));
    S3AsyncClient s3Client = getAsyncClientBuilder(wm).overrideConfiguration(o -> o.addExecutionInterceptor(captureChecksumValidationInterceptor)).build();
    String response = s3Client.getObject(r -> r.bucket(EXAMPLE_BUCKET).key("key").checksumMode(ChecksumMode.ENABLED), AsyncResponseTransformer.toBytes()).join().asUtf8String();
    assertThat(response).isEqualTo("Hello world");
    assertThat(captureChecksumValidationInterceptor.responseValidation()).isEqualTo(ChecksumValidation.VALIDATED);
    assertThat(captureChecksumValidationInterceptor.validationAlgorithm()).isEqualTo(Algorithm.CRC32);
}
Also used : ChecksumMode(software.amazon.awssdk.services.s3.model.ChecksumMode) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) WireMock.any(com.github.tomakehurst.wiremock.client.WireMock.any) StaticCredentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider) Function(java.util.function.Function) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) ResponseInputStream(software.amazon.awssdk.core.ResponseInputStream) AsyncResponseTransformer(software.amazon.awssdk.core.async.AsyncResponseTransformer) URI(java.net.URI) Region(software.amazon.awssdk.regions.Region) CaptureChecksumValidationInterceptor(software.amazon.awssdk.services.s3.utils.CaptureChecksumValidationInterceptor) ChecksumValidation(software.amazon.awssdk.core.checksums.ChecksumValidation) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) S3Client(software.amazon.awssdk.services.s3.S3Client) WireMockRuntimeInfo(com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) S3AsyncClientBuilder(software.amazon.awssdk.services.s3.S3AsyncClientBuilder) Algorithm(software.amazon.awssdk.core.checksums.Algorithm) S3ClientBuilder(software.amazon.awssdk.services.s3.S3ClientBuilder) WireMockTest(com.github.tomakehurst.wiremock.junit5.WireMockTest) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) WireMock.anyUrl(com.github.tomakehurst.wiremock.client.WireMock.anyUrl) BufferedReader(java.io.BufferedReader) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) InputStream(java.io.InputStream) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) Test(org.junit.jupiter.api.Test) WireMockTest(com.github.tomakehurst.wiremock.junit5.WireMockTest)

Example 13 with S3AsyncClient

use of software.amazon.awssdk.services.s3.S3AsyncClient in project aws-sdk-java-v2 by aws.

the class AsyncResponseTransformerTest method AsyncResponseTransformerPrepareCalled_BeforeCredentialsResolution.

@Test
public void AsyncResponseTransformerPrepareCalled_BeforeCredentialsResolution() {
    S3AsyncClient client = S3AsyncClient.builder().credentialsProvider(AwsCredentialsProviderChain.of(ProfileCredentialsProvider.create("dummyprofile"))).build();
    try {
        client.getObject(b -> b.bucket("dummy").key("key"), asyncResponseTransformer).join();
        fail("Expected an exception during credential resolution");
    } catch (CompletionException e) {
    }
    verify(asyncResponseTransformer, times(1)).prepare();
    verify(asyncResponseTransformer, times(1)).exceptionOccurred(any());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AwsCredentialsProviderChain(software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Mockito.times(org.mockito.Mockito.times) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test) ProfileCredentialsProvider(software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider) Mockito.verify(org.mockito.Mockito.verify) Assertions.fail(org.assertj.core.api.Assertions.fail) AsyncResponseTransformer(software.amazon.awssdk.core.async.AsyncResponseTransformer) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) CompletionException(java.util.concurrent.CompletionException) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) Test(org.junit.Test)

Example 14 with S3AsyncClient

use of software.amazon.awssdk.services.s3.S3AsyncClient in project aws-sdk-java-v2 by aws.

the class ContentLengthMismatchTest method contentShorterThanRequestBodyLengthFails.

@Test
public void contentShorterThanRequestBodyLengthFails() {
    String bucket = "Example-Bucket";
    String key = "Example-Object";
    S3AsyncClient s3Client = getAsyncClientBuilder().build();
    AsyncRequestBody requestBody = new AsyncRequestBody() {

        @Override
        public Optional<Long> contentLength() {
            return Optional.of(2L);
        }

        @Override
        public void subscribe(Subscriber<? super ByteBuffer> subscriber) {
            AsyncRequestBody.fromString("A").subscribe(subscriber);
        }
    };
    assertThatThrownBy(() -> s3Client.putObject(r -> r.bucket(bucket).key(key), requestBody).get(10, TimeUnit.SECONDS)).isInstanceOf(ExecutionException.class).hasMessageContaining("content-length");
}
Also used : Subscriber(org.reactivestreams.Subscriber) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) ExecutionException(java.util.concurrent.ExecutionException) ByteBuffer(java.nio.ByteBuffer) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) Test(org.junit.Test)

Example 15 with S3AsyncClient

use of software.amazon.awssdk.services.s3.S3AsyncClient in project aws-sdk-java-v2 by aws.

the class ContentLengthMismatchTest method checksumDoesNotExceedAsyncRequestBodyLengthForPuts.

@Test
public void checksumDoesNotExceedAsyncRequestBodyLengthForPuts() {
    String bucket = "Example-Bucket";
    String key = "Example-Object";
    String content = "Hello, World!";
    String eTag = "65A8E27D8879283831B664BD8B7F0AD4";
    stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200).withHeader("ETag", eTag)));
    S3AsyncClient s3Client = getAsyncClientBuilder().build();
    PutObjectResponse response = s3Client.putObject(r -> r.bucket(bucket).key(key), new AsyncRequestBody() {

        @Override
        public Optional<Long> contentLength() {
            return Optional.of((long) content.length());
        }

        @Override
        public void subscribe(Subscriber<? super ByteBuffer> subscriber) {
            AsyncRequestBody.fromString(content + " Extra stuff!").subscribe(subscriber);
        }
    }).join();
    verify(putRequestedFor(anyUrl()).withRequestBody(equalTo(content)));
    assertThat(response.eTag()).isEqualTo(eTag);
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) WireMock.any(com.github.tomakehurst.wiremock.client.WireMock.any) StaticCredentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider) ByteBuffer(java.nio.ByteBuffer) WireMockRule(com.github.tomakehurst.wiremock.junit.WireMockRule) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) URI(java.net.URI) Region(software.amazon.awssdk.regions.Region) Subscriber(org.reactivestreams.Subscriber) PutObjectResponse(software.amazon.awssdk.services.s3.model.PutObjectResponse) WireMock.equalTo(com.github.tomakehurst.wiremock.client.WireMock.equalTo) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) Test(org.junit.Test) WireMock.verify(com.github.tomakehurst.wiremock.client.WireMock.verify) WireMock.putRequestedFor(com.github.tomakehurst.wiremock.client.WireMock.putRequestedFor) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) S3AsyncClientBuilder(software.amazon.awssdk.services.s3.S3AsyncClientBuilder) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) Optional(java.util.Optional) WireMock.anyUrl(com.github.tomakehurst.wiremock.client.WireMock.anyUrl) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) Subscriber(org.reactivestreams.Subscriber) PutObjectResponse(software.amazon.awssdk.services.s3.model.PutObjectResponse) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) ByteBuffer(java.nio.ByteBuffer) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) Test(org.junit.Test)

Aggregations

S3AsyncClient (software.amazon.awssdk.services.s3.S3AsyncClient)25 Test (org.junit.Test)18 Region (software.amazon.awssdk.regions.Region)14 S3AsyncClientBuilder (software.amazon.awssdk.services.s3.S3AsyncClientBuilder)14 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)11 WireMock.aResponse (com.github.tomakehurst.wiremock.client.WireMock.aResponse)10 WireMock.any (com.github.tomakehurst.wiremock.client.WireMock.any)10 WireMock.anyUrl (com.github.tomakehurst.wiremock.client.WireMock.anyUrl)10 WireMock.stubFor (com.github.tomakehurst.wiremock.client.WireMock.stubFor)10 URI (java.net.URI)10 AwsBasicCredentials (software.amazon.awssdk.auth.credentials.AwsBasicCredentials)10 StaticCredentialsProvider (software.amazon.awssdk.auth.credentials.StaticCredentialsProvider)10 S3Client (software.amazon.awssdk.services.s3.S3Client)8 S3ClientBuilder (software.amazon.awssdk.services.s3.S3ClientBuilder)8 Collectors (java.util.stream.Collectors)7 GetObjectResponse (software.amazon.awssdk.services.s3.model.GetObjectResponse)7 WireMockRule (com.github.tomakehurst.wiremock.junit.WireMockRule)6 AsyncResponseTransformer (software.amazon.awssdk.core.async.AsyncResponseTransformer)6 InputStream (java.io.InputStream)5 ByteBuffer (java.nio.ByteBuffer)5