Search in sources :

Example 1 with SimpleSubscriber

use of software.amazon.awssdk.http.async.SimpleSubscriber in project aws-sdk by reactiverse.

the class AsyncHttpClientTest method testPut.

@Test
@Timeout(value = 15, timeUnit = TimeUnit.SECONDS)
public void testPut(VertxTestContext ctx) {
    final byte[] payload = "the-body".getBytes();
    server.requestHandler(req -> {
        req.bodyHandler(buff -> {
            req.response().end(buff);
        });
    });
    server.listen(PORT, HOST, res -> {
        assertTrue(res.succeeded());
        SdkHttpFullRequest request = SdkHttpFullRequest.builder().protocol(SCHEME).host(HOST).port(PORT).method(SdkHttpMethod.PUT).putHeader("Content-Length", String.valueOf(payload.length)).contentStreamProvider(() -> new ByteArrayInputStream(payload)).build();
        client.execute(AsyncExecuteRequest.builder().request(request).requestContentPublisher(new SimpleHttpContentPublisher(request)).responseHandler(new SdkAsyncHttpResponseHandler() {

            @Override
            public void onHeaders(SdkHttpResponse headers) {
                assertEquals(200, headers.statusCode());
            }

            @Override
            public void onStream(Publisher<ByteBuffer> stream) {
                stream.subscribe(new SimpleSubscriber(body -> {
                    assertEquals("the-body", Unpooled.wrappedBuffer(body).toString(StandardCharsets.UTF_8));
                    ctx.completeNow();
                }));
            }

            @Override
            public void onError(Throwable error) {
                throw new RuntimeException(error);
            }
        }).build());
    });
}
Also used : SimpleHttpContentPublisher(software.amazon.awssdk.core.internal.http.async.SimpleHttpContentPublisher) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) SdkAsyncHttpResponseHandler(software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) SdkHttpResponse(software.amazon.awssdk.http.SdkHttpResponse) ByteBuffer(java.nio.ByteBuffer) SimpleSubscriber(software.amazon.awssdk.http.async.SimpleSubscriber) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Example 2 with SimpleSubscriber

use of software.amazon.awssdk.http.async.SimpleSubscriber in project aws-sdk-java-v2 by aws.

the class AsyncRequestBodyTest method hasCorrectContent.

@Test
public void hasCorrectContent() throws InterruptedException {
    StringBuilder sb = new StringBuilder();
    CountDownLatch done = new CountDownLatch(1);
    Subscriber<ByteBuffer> subscriber = new SimpleSubscriber(buffer -> {
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);
        sb.append(new String(bytes, StandardCharsets.UTF_8));
    }) {

        @Override
        public void onError(Throwable t) {
            super.onError(t);
            done.countDown();
        }

        @Override
        public void onComplete() {
            super.onComplete();
            done.countDown();
        }
    };
    provider.subscribe(subscriber);
    done.await();
    assertThat(sb.toString()).isEqualTo(testString);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) SimpleSubscriber(software.amazon.awssdk.http.async.SimpleSubscriber) Test(org.junit.Test)

Example 3 with SimpleSubscriber

use of software.amazon.awssdk.http.async.SimpleSubscriber in project aws-sdk-java-v2 by aws.

the class ChecksumCalculatingAsyncRequestBodyTest method hasCorrectContent.

@Test
public void hasCorrectContent() throws InterruptedException {
    StringBuilder sb = new StringBuilder();
    CountDownLatch done = new CountDownLatch(1);
    Subscriber<ByteBuffer> subscriber = new SimpleSubscriber(buffer -> {
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);
        sb.append(new String(bytes, StandardCharsets.UTF_8));
    }) {

        @Override
        public void onError(Throwable t) {
            super.onError(t);
            done.countDown();
        }

        @Override
        public void onComplete() {
            super.onComplete();
            done.countDown();
        }
    };
    provider.subscribe(subscriber);
    done.await(10, TimeUnit.SECONDS);
    assertThat(sb).hasToString(expectedTestString);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) SimpleSubscriber(software.amazon.awssdk.http.async.SimpleSubscriber) Test(org.junit.Test)

Example 4 with SimpleSubscriber

use of software.amazon.awssdk.http.async.SimpleSubscriber in project aws-sdk by reactiverse.

the class AsyncHttpClientTest method testGet.

@Test
@Timeout(value = 15, timeUnit = TimeUnit.SECONDS)
public void testGet(VertxTestContext ctx) {
    server.requestHandler(req -> {
        req.response().end("foo");
    });
    server.listen(PORT, HOST, res -> {
        assertTrue(res.succeeded());
        client.execute(AsyncExecuteRequest.builder().request(SdkHttpRequest.builder().protocol(SCHEME).host(HOST).port(PORT).method(SdkHttpMethod.GET).build()).responseHandler(new SdkAsyncHttpResponseHandler() {

            @Override
            public void onHeaders(SdkHttpResponse headers) {
                assertEquals(200, headers.statusCode());
            }

            @Override
            public void onStream(Publisher<ByteBuffer> stream) {
                stream.subscribe(new SimpleSubscriber(body -> {
                    assertEquals("foo", Unpooled.wrappedBuffer(body).toString(StandardCharsets.UTF_8));
                    ctx.completeNow();
                }));
            }

            @Override
            public void onError(Throwable error) {
                throw new RuntimeException(error);
            }
        }).build());
    });
}
Also used : SdkAsyncHttpResponseHandler(software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler) SdkHttpResponse(software.amazon.awssdk.http.SdkHttpResponse) ByteBuffer(java.nio.ByteBuffer) SimpleSubscriber(software.amazon.awssdk.http.async.SimpleSubscriber) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Example 5 with SimpleSubscriber

use of software.amazon.awssdk.http.async.SimpleSubscriber in project aws-sdk-java-v2 by aws.

the class GetObjectAsyncIntegrationTest method customResponseHandler_InterceptorRecievesResponsePojo.

@Test
public void customResponseHandler_InterceptorRecievesResponsePojo() throws Exception {
    final CompletableFuture<String> cf = new CompletableFuture<>();
    try (S3AsyncClient asyncWithInterceptor = createClientWithInterceptor(new AssertingExecutionInterceptor())) {
        String result = asyncWithInterceptor.getObject(getObjectRequest, new AsyncResponseTransformer<GetObjectResponse, String>() {

            @Override
            public CompletableFuture<String> prepare() {
                return cf;
            }

            @Override
            public void onResponse(GetObjectResponse response) {
                // POJO returned by modifyResponse should be delivered to the AsyncResponseTransformer
                assertThat(response.metadata()).hasEntrySatisfying("x-amz-assert", s -> assertThat(s).isEqualTo("injected-value"));
            }

            @Override
            public void onStream(SdkPublisher<ByteBuffer> publisher) {
                publisher.subscribe(new SimpleSubscriber(b -> {
                }) {

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        cf.complete("result");
                    }
                });
            }

            @Override
            public void exceptionOccurred(Throwable throwable) {
                cf.completeExceptionally(throwable);
            }
        }).join();
        assertThat(result).isEqualTo("result");
    }
}
Also used : BeforeClass(org.junit.BeforeClass) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ResponsePublisher(software.amazon.awssdk.core.async.ResponsePublisher) S3BucketUtils.temporaryBucketName(software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName) CompletableFuture(java.util.concurrent.CompletableFuture) ByteBuffer(java.nio.ByteBuffer) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) Context(software.amazon.awssdk.core.interceptor.Context) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) AsyncResponseTransformer(software.amazon.awssdk.core.async.AsyncResponseTransformer) Path(java.nio.file.Path) AfterClass(org.junit.AfterClass) ImmutableMap(software.amazon.awssdk.utils.ImmutableMap) Files(java.nio.file.Files) IOException(java.io.IOException) Test(org.junit.Test) SdkPublisher(software.amazon.awssdk.core.async.SdkPublisher) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) SimpleSubscriber(software.amazon.awssdk.http.async.SimpleSubscriber) SdkResponse(software.amazon.awssdk.core.SdkResponse) RandomTempFile(software.amazon.awssdk.testutils.RandomTempFile) Assert.assertEquals(org.junit.Assert.assertEquals) ClientOverrideConfiguration(software.amazon.awssdk.core.client.config.ClientOverrideConfiguration) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncResponseTransformer(software.amazon.awssdk.core.async.AsyncResponseTransformer) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) SdkPublisher(software.amazon.awssdk.core.async.SdkPublisher) SimpleSubscriber(software.amazon.awssdk.http.async.SimpleSubscriber) Test(org.junit.Test)

Aggregations

ByteBuffer (java.nio.ByteBuffer)5 SimpleSubscriber (software.amazon.awssdk.http.async.SimpleSubscriber)5 Test (org.junit.Test)3 Timeout (io.vertx.junit5.Timeout)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.jupiter.api.Test)2 SdkHttpResponse (software.amazon.awssdk.http.SdkHttpResponse)2 SdkAsyncHttpResponseHandler (software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 AfterClass (org.junit.AfterClass)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 BeforeClass (org.junit.BeforeClass)1 SdkResponse (software.amazon.awssdk.core.SdkResponse)1