Search in sources :

Example 31 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class ChecksumCalculatingAsyncRequestBodyTest method bytesArrayConstructorHasCorrectContentType.

@Test
public void bytesArrayConstructorHasCorrectContentType() {
    AsyncRequestBody requestBody = ChecksumCalculatingAsyncRequestBody.builder().asyncRequestBody(AsyncRequestBody.fromBytes("hello world".getBytes())).algorithm(Algorithm.CRC32).trailerHeader("x-amz-checksum-crc32").build();
    assertThat(requestBody.contentType()).isEqualTo(Mimetype.MIMETYPE_OCTET_STREAM);
}
Also used : AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Test(org.junit.Test)

Example 32 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class ChecksumCalculatingAsyncRequestBodyTest method fromBytes_byteArrayNotNullChecksumSupplied.

@Test
public void fromBytes_byteArrayNotNullChecksumSupplied() {
    byte[] original = { 1, 2, 3, 4 };
    // Checksum data in byte format.
    byte[] expected = { 52, 13, 10, 1, 2, 3, 4, 13, 10, 48, 13, 10, 120, 45, 97, 109, 122, 110, 45, 99, 104, 101, 99, 107, 115, 117, 109, 45, 99, 114, 99, 51, 50, 58, 116, 106, 122, 55, 122, 81, 61, 61, 13, 10, 13, 10 };
    byte[] toModify = new byte[original.length];
    System.arraycopy(original, 0, toModify, 0, original.length);
    AsyncRequestBody body = ChecksumCalculatingAsyncRequestBody.builder().asyncRequestBody(AsyncRequestBody.fromBytes(toModify)).algorithm(Algorithm.CRC32).trailerHeader("x-amzn-checksum-crc32").build();
    for (int i = 0; i < toModify.length; ++i) {
        toModify[i]++;
    }
    ByteBuffer publishedBb = Flowable.fromPublisher(body).toList().blockingGet().get(0);
    assertThat(BinaryUtils.copyAllBytesFrom(publishedBb)).isEqualTo(expected);
}
Also used : AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 33 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class ChecksumCalculatingAsyncRequestBodyTest method emptyBytesConstructorHasCorrectContentType.

@Test
public void emptyBytesConstructorHasCorrectContentType() {
    AsyncRequestBody requestBody = ChecksumCalculatingAsyncRequestBody.builder().asyncRequestBody(AsyncRequestBody.empty()).algorithm(Algorithm.CRC32).trailerHeader("x-amz-checksum-crc32").build();
    assertThat(requestBody.contentType()).isEqualTo(Mimetype.MIMETYPE_OCTET_STREAM);
}
Also used : AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Test(org.junit.Test)

Example 34 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class FileAsyncRequestBodyTest method changingFile_fileGetsShorterThanExistingLength_failsBecauseTooShort.

@Test
public void changingFile_fileGetsShorterThanExistingLength_failsBecauseTooShort() throws Exception {
    AsyncRequestBody asyncRequestBody = FileAsyncRequestBody.builder().path(testFile).build();
    ControllableSubscriber subscriber = new ControllableSubscriber();
    // Start reading file
    asyncRequestBody.subscribe(subscriber);
    subscriber.sub.request(1);
    assertTrue(subscriber.onNextSemaphore.tryAcquire(5, TimeUnit.SECONDS));
    // Change the file to be 1 byte shorter than when we started
    int currentSize = Math.toIntExact(Files.size(testFile));
    byte[] slightlyShorterFileContent = new byte[currentSize - 1];
    ThreadLocalRandom.current().nextBytes(slightlyShorterFileContent);
    Files.write(testFile, slightlyShorterFileContent);
    // Finishing reading the file
    subscriber.sub.request(Long.MAX_VALUE);
    assertThatThrownBy(() -> subscriber.completed.get(5, TimeUnit.SECONDS)).hasCauseInstanceOf(IOException.class);
}
Also used : AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Test(org.junit.jupiter.api.Test)

Example 35 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class FileAsyncRequestBodyTest method changingFile_fileGetsShorterThanAlreadyRead_failsBecauseTooShort.

@Test
public void changingFile_fileGetsShorterThanAlreadyRead_failsBecauseTooShort() throws Exception {
    AsyncRequestBody asyncRequestBody = FileAsyncRequestBody.builder().path(testFile).build();
    ControllableSubscriber subscriber = new ControllableSubscriber();
    // Start reading file
    asyncRequestBody.subscribe(subscriber);
    subscriber.sub.request(1);
    assertTrue(subscriber.onNextSemaphore.tryAcquire(5, TimeUnit.SECONDS));
    // Change the file to be shorter than the amount read so far
    Files.write(testFile, "Hello".getBytes(StandardCharsets.UTF_8));
    // Finishing reading the file
    subscriber.sub.request(Long.MAX_VALUE);
    assertThatThrownBy(() -> subscriber.completed.get(5, TimeUnit.SECONDS)).hasCauseInstanceOf(IOException.class);
}
Also used : AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Test(org.junit.jupiter.api.Test)

Aggregations

AsyncRequestBody (software.amazon.awssdk.core.async.AsyncRequestBody)51 ByteBuffer (java.nio.ByteBuffer)25 Test (org.junit.jupiter.api.Test)24 List (java.util.List)17 Test (org.junit.Test)13 Subscriber (org.reactivestreams.Subscriber)12 CompletableFuture (java.util.concurrent.CompletableFuture)11 SdkHttpFullRequest (software.amazon.awssdk.http.SdkHttpFullRequest)11 Publisher (org.reactivestreams.Publisher)9 Flowable (io.reactivex.Flowable)8 Consumer (java.util.function.Consumer)8 AsyncAws4Signer (software.amazon.awssdk.auth.signer.AsyncAws4Signer)8 AsyncResponseTransformer (software.amazon.awssdk.core.async.AsyncResponseTransformer)8 Signer (software.amazon.awssdk.core.signer.Signer)8 MetricCollector (software.amazon.awssdk.metrics.MetricCollector)8 Collections (java.util.Collections)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 Generated (software.amazon.awssdk.annotations.Generated)7 SdkInternalApi (software.amazon.awssdk.annotations.SdkInternalApi)7