Search in sources :

Example 1 with S3ClientBuilder

use of software.amazon.awssdk.services.s3.S3ClientBuilder in project fluency by komamitsu.

the class AwsS3SenderTest method close.

@Test
void close() {
    S3Client s3Client = mock(S3Client.class);
    S3ClientBuilder s3ClientBuilder = mock(S3ClientBuilder.class);
    doReturn(s3Client).when(s3ClientBuilder).build();
    AwsS3Sender sender = new AwsS3Sender(s3ClientBuilder);
    sender.close();
    verify(s3Client, times(1)).close();
}
Also used : S3ClientBuilder(software.amazon.awssdk.services.s3.S3ClientBuilder) S3Client(software.amazon.awssdk.services.s3.S3Client) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with S3ClientBuilder

use of software.amazon.awssdk.services.s3.S3ClientBuilder in project fluency by komamitsu.

the class AwsS3SenderTest method testSend.

private void testSend(AwsS3Sender.Config config, boolean gzipCompressed, int failures) throws IOException {
    S3Client s3Client = mock(S3Client.class);
    S3ClientBuilder s3ClientBuilder = mock(S3ClientBuilder.class);
    doReturn(s3Client).when(s3ClientBuilder).build();
    AtomicInteger retryCount = new AtomicInteger();
    doAnswer(invocation -> {
        PutObjectRequest request = invocation.getArgument(0);
        assertEquals("hello.world", request.bucket());
        assertEquals("2345/01/31/23/59-59-99999.data", request.key());
        RequestBody body = invocation.getArgument(1);
        try (InputStream s3In = body.contentStreamProvider().newStream();
            InputStream in = gzipCompressed ? new GZIPInputStream(s3In) : s3In) {
            byte[] content = ByteStreams.toByteArray(in);
            assertEquals("0123456789", new String(content, StandardCharsets.UTF_8));
        }
        if (retryCount.getAndIncrement() < failures) {
            throw new RuntimeException("Something happened");
        }
        return null;
    }).when(s3Client).putObject(any(PutObjectRequest.class), any(RequestBody.class));
    AwsS3Sender sender = new AwsS3Sender(s3ClientBuilder, config);
    sender.send("hello.world", "2345/01/31/23/59-59-99999.data", ByteBuffer.wrap("0123456789".getBytes(StandardCharsets.UTF_8)));
    verify(s3Client, times(failures + 1)).putObject(any(PutObjectRequest.class), any(RequestBody.class));
    sender.close();
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) S3ClientBuilder(software.amazon.awssdk.services.s3.S3ClientBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) S3Client(software.amazon.awssdk.services.s3.S3Client) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) RequestBody(software.amazon.awssdk.core.sync.RequestBody)

Example 3 with S3ClientBuilder

use of software.amazon.awssdk.services.s3.S3ClientBuilder in project fluency by komamitsu.

the class AwsS3SenderTest method buildClientWithCustomizedConfig.

@Test
void buildClientWithCustomizedConfig() {
    AwsS3Sender.Config config = new AwsS3Sender.Config();
    config.setEndpoint("https://another.s3endpoi.nt");
    config.setRegion("ap-northeast-1");
    config.setAwsAccessKeyId("foo");
    config.setAwsSecretAccessKey("bar");
    S3Client s3Client = mock(S3Client.class);
    S3ClientBuilder s3ClientBuilder = mock(S3ClientBuilder.class);
    doReturn(s3Client).when(s3ClientBuilder).build();
    doAnswer(invocation -> {
        AwsCredentialsProvider provider = invocation.getArgument(0);
        AwsCredentials awsCredentials = provider.resolveCredentials();
        assertEquals("foo", awsCredentials.accessKeyId());
        assertEquals("bar", awsCredentials.secretAccessKey());
        return null;
    }).when(s3ClientBuilder).credentialsProvider(any());
    new AwsS3Sender(s3ClientBuilder, config);
    verify(s3ClientBuilder, times(1)).build();
    verify(s3ClientBuilder, times(1)).endpointOverride(eq(URI.create("https://another.s3endpoi.nt")));
    verify(s3ClientBuilder, times(1)).region(eq(Region.AP_NORTHEAST_1));
    verify(s3ClientBuilder, times(1)).credentialsProvider(any());
}
Also used : S3ClientBuilder(software.amazon.awssdk.services.s3.S3ClientBuilder) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) S3Client(software.amazon.awssdk.services.s3.S3Client) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with S3ClientBuilder

use of software.amazon.awssdk.services.s3.S3ClientBuilder in project fluency by komamitsu.

the class AwsS3SenderTest method buildClientWithDefaults.

@Test
void buildClientWithDefaults() {
    AwsS3Sender.Config config = new AwsS3Sender.Config();
    S3Client s3Client = mock(S3Client.class);
    S3ClientBuilder s3ClientBuilder = mock(S3ClientBuilder.class);
    doReturn(s3Client).when(s3ClientBuilder).build();
    new AwsS3Sender(s3ClientBuilder, config);
    verify(s3ClientBuilder, times(1)).build();
    verify(s3ClientBuilder, times(0)).endpointOverride(any());
    verify(s3ClientBuilder, times(0)).region(any());
    verify(s3ClientBuilder, times(0)).credentialsProvider(any());
}
Also used : S3ClientBuilder(software.amazon.awssdk.services.s3.S3ClientBuilder) S3Client(software.amazon.awssdk.services.s3.S3Client) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

S3Client (software.amazon.awssdk.services.s3.S3Client)4 S3ClientBuilder (software.amazon.awssdk.services.s3.S3ClientBuilder)4 Test (org.junit.jupiter.api.Test)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 InputStream (java.io.InputStream)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 AwsCredentials (software.amazon.awssdk.auth.credentials.AwsCredentials)1 AwsCredentialsProvider (software.amazon.awssdk.auth.credentials.AwsCredentialsProvider)1 RequestBody (software.amazon.awssdk.core.sync.RequestBody)1 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)1