Search in sources :

Example 1 with VertxAsyncResponseTransformer

use of io.reactiverse.awssdk.converters.VertxAsyncResponseTransformer in project aws-sdk by reactiverse.

the class VertxS3ClientSpec method downloadImageFromBucketToPump.

@Test
@Order(6)
void downloadImageFromBucketToPump(Vertx vertx, VertxTestContext ctx) throws Exception {
    final Context originalContext = vertx.getOrCreateContext();
    final S3AsyncClient s3 = s3(originalContext);
    Buffer received = Buffer.buffer();
    AtomicBoolean handlerCalled = new AtomicBoolean(false);
    VertxAsyncResponseTransformer<GetObjectResponse> transformer = new VertxAsyncResponseTransformer<>(new WriteStream<Buffer>() {

        @Override
        public WriteStream<Buffer> exceptionHandler(Handler<Throwable> handler) {
            return null;
        }

        @Override
        public Future<Void> write(Buffer data) {
            received.appendBuffer(data);
            return Future.succeededFuture();
        }

        @Override
        public void write(Buffer data, Handler<AsyncResult<Void>> handler) {
            received.appendBuffer(data);
            handler.handle(null);
        }

        @Override
        public void end(Handler<AsyncResult<Void>> handler) {
            assertTrue(handlerCalled.get(), "Response handler should have been called before first bytes are received");
            if (received.length() == fileSize)
                ctx.completeNow();
            handler.handle(null);
        }

        @Override
        public WriteStream<Buffer> setWriteQueueMaxSize(int maxSize) {
            return null;
        }

        @Override
        public boolean writeQueueFull() {
            return false;
        }

        @Override
        public WriteStream<Buffer> drainHandler(@Nullable Handler<Void> handler) {
            return null;
        }
    });
    transformer.setResponseHandler(resp -> {
        handlerCalled.set(true);
    });
    single(s3.getObject(VertxS3ClientSpec::downloadImgReq, transformer)).subscribe(getRes -> {
    }, ctx::failNow);
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) Context(io.vertx.core.Context) Buffer(io.vertx.core.buffer.Buffer) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) WriteStream(io.vertx.core.streams.WriteStream) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VertxAsyncResponseTransformer(io.reactiverse.awssdk.converters.VertxAsyncResponseTransformer) Future(io.vertx.core.Future) AsyncResult(io.vertx.core.AsyncResult) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 2 with VertxAsyncResponseTransformer

use of io.reactiverse.awssdk.converters.VertxAsyncResponseTransformer in project aws-sdk by reactiverse.

the class VertxS3ClientSpec method downloadImageFromBucketWithoutSettingResponseHandler.

@Test
@Order(7)
void downloadImageFromBucketWithoutSettingResponseHandler(Vertx vertx, VertxTestContext ctx) throws Exception {
    final Context originalContext = vertx.getOrCreateContext();
    final S3AsyncClient s3 = s3(originalContext);
    final Buffer received = Buffer.buffer();
    AtomicBoolean handlerCalled = new AtomicBoolean(false);
    VertxAsyncResponseTransformer<GetObjectResponse> transformer = new VertxAsyncResponseTransformer<>(new WriteStream<Buffer>() {

        @Override
        public WriteStream<Buffer> exceptionHandler(Handler<Throwable> handler) {
            return null;
        }

        @Override
        public Future<Void> write(Buffer data) {
            received.appendBuffer(data);
            return Future.succeededFuture();
        }

        @Override
        public void write(Buffer data, Handler<AsyncResult<Void>> handler) {
            received.appendBuffer(data);
            handler.handle(null);
        }

        @Override
        public void end(Handler<AsyncResult<Void>> handler) {
            assertTrue(handlerCalled.get(), "Response handler should have been called before first bytes are received");
            if (received.length() == fileSize)
                ctx.completeNow();
            handler.handle(null);
        }

        @Override
        public WriteStream<Buffer> setWriteQueueMaxSize(int maxSize) {
            return null;
        }

        @Override
        public boolean writeQueueFull() {
            return false;
        }

        @Override
        public WriteStream<Buffer> drainHandler(@Nullable Handler<Void> handler) {
            return null;
        }
    });
    transformer.setResponseHandler(resp -> {
        handlerCalled.set(true);
    });
    single(s3.getObject(VertxS3ClientSpec::downloadImgReq, transformer)).subscribe(getRes -> ctx.completeNow(), ctx::failNow);
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) Context(io.vertx.core.Context) Buffer(io.vertx.core.buffer.Buffer) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) WriteStream(io.vertx.core.streams.WriteStream) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VertxAsyncResponseTransformer(io.reactiverse.awssdk.converters.VertxAsyncResponseTransformer) Future(io.vertx.core.Future) AsyncResult(io.vertx.core.AsyncResult) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Aggregations

VertxAsyncResponseTransformer (io.reactiverse.awssdk.converters.VertxAsyncResponseTransformer)2 AsyncResult (io.vertx.core.AsyncResult)2 Context (io.vertx.core.Context)2 Future (io.vertx.core.Future)2 Buffer (io.vertx.core.buffer.Buffer)2 WriteStream (io.vertx.core.streams.WriteStream)2 VertxTestContext (io.vertx.junit5.VertxTestContext)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Order (org.junit.jupiter.api.Order)2 Test (org.junit.jupiter.api.Test)2 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)2 S3AsyncClient (software.amazon.awssdk.services.s3.S3AsyncClient)2 GetObjectResponse (software.amazon.awssdk.services.s3.model.GetObjectResponse)2