Search in sources :

Example 1 with MessageDecoder

use of software.amazon.eventstream.MessageDecoder in project aws-sdk-java-v2 by aws.

the class Aws4EventStreamSignerTest method testEventStreamSigning.

@Test
public void testEventStreamSigning() {
    TestVector testVector = generateTestVector();
    SdkHttpFullRequest.Builder request = testVector.httpFullRequest();
    AwsBasicCredentials credentials = AwsBasicCredentials.create("access", "secret");
    SdkHttpFullRequest signedRequest = SignerTestUtils.signRequest(signer, request.build(), credentials, "demo", signingClock(), "us-east-1");
    AsyncRequestBody transformedPublisher = SignerTestUtils.signAsyncRequest(signer, signedRequest, testVector.requestBodyPublisher(), credentials, "demo", signingClock(), "us-east-1");
    TestSubscriber testSubscriber = TestSubscriber.create();
    Flowable.fromPublisher(transformedPublisher).flatMap(new Function<ByteBuffer, Publisher<?>>() {

        Queue<Message> messages = new LinkedList<>();

        MessageDecoder decoder = new MessageDecoder(message -> messages.offer(message));

        @Override
        public Publisher<?> apply(ByteBuffer byteBuffer) throws Exception {
            decoder.feed(byteBuffer.array());
            List<Message> messageList = new ArrayList<>();
            while (!messages.isEmpty()) {
                messageList.add(messages.poll());
            }
            return Flowable.fromIterable(messageList);
        }
    }).subscribe(testSubscriber);
    testSubscriber.assertNoErrors();
    testSubscriber.assertComplete();
    testSubscriber.assertValueSequence(testVector.expectedMessagePublisher().blockingIterable());
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BiFunction(io.reactivex.functions.BiFunction) SignerTestUtils(software.amazon.awssdk.auth.signer.internal.SignerTestUtils) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) Message(software.amazon.eventstream.Message) EVENT_STREAM_DATE(software.amazon.awssdk.auth.signer.internal.BaseEventStreamAsyncAws4Signer.EVENT_STREAM_DATE) ArrayList(java.util.ArrayList) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) BinaryUtils(software.amazon.awssdk.utils.BinaryUtils) TestSubscriber(io.reactivex.subscribers.TestSubscriber) SdkHttpMethod(software.amazon.awssdk.http.SdkHttpMethod) ZoneOffset(java.time.ZoneOffset) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) LinkedList(java.util.LinkedList) Subscriber(org.reactivestreams.Subscriber) Publisher(org.reactivestreams.Publisher) Mockito.times(org.mockito.Mockito.times) EVENT_STREAM_SIGNATURE(software.amazon.awssdk.auth.signer.internal.BaseEventStreamAsyncAws4Signer.EVENT_STREAM_SIGNATURE) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) ZoneId(java.time.ZoneId) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) MessageDecoder(software.amazon.eventstream.MessageDecoder) Mockito.never(org.mockito.Mockito.never) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) Stream(java.util.stream.Stream) Function(io.reactivex.functions.Function) Lists(org.assertj.core.util.Lists) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Subscription(org.reactivestreams.Subscription) Clock(java.time.Clock) Queue(java.util.Queue) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) HeaderValue(software.amazon.eventstream.HeaderValue) Message(software.amazon.eventstream.Message) ArrayList(java.util.ArrayList) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) ByteBuffer(java.nio.ByteBuffer) BiFunction(io.reactivex.functions.BiFunction) Function(io.reactivex.functions.Function) MessageDecoder(software.amazon.eventstream.MessageDecoder) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Queue(java.util.Queue) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) Test(org.junit.jupiter.api.Test)

Example 2 with MessageDecoder

use of software.amazon.eventstream.MessageDecoder in project aws-sdk-java-v2 by aws.

the class Aws4EventStreamSignerTest method testBackPressure.

/**
 * Test that without demand from subscriber, trailing empty frame is not delivered
 */
@Test
public void testBackPressure() {
    TestVector testVector = generateTestVector();
    SdkHttpFullRequest.Builder request = testVector.httpFullRequest();
    AwsBasicCredentials credentials = AwsBasicCredentials.create("access", "secret");
    SdkHttpFullRequest signedRequest = SignerTestUtils.signRequest(signer, request.build(), credentials, "demo", signingClock(), "us-east-1");
    AsyncRequestBody transformedPublisher = SignerTestUtils.signAsyncRequest(signer, signedRequest, testVector.requestBodyPublisher(), credentials, "demo", signingClock(), "us-east-1");
    Subscriber<Object> subscriber = Mockito.spy(new Subscriber<Object>() {

        @Override
        public void onSubscribe(Subscription s) {
            // Only request the number of request body (excluding trailing empty frame)
            s.request(testVector.requestBody().size());
        }

        @Override
        public void onNext(Object o) {
        }

        @Override
        public void onError(Throwable t) {
            fail("onError should never been called");
        }

        @Override
        public void onComplete() {
            fail("onComplete should never been called");
        }
    });
    Flowable.fromPublisher(transformedPublisher).flatMap(new Function<ByteBuffer, Publisher<?>>() {

        Queue<Message> messages = new LinkedList<>();

        MessageDecoder decoder = new MessageDecoder(message -> messages.offer(message));

        @Override
        public Publisher<?> apply(ByteBuffer byteBuffer) throws Exception {
            decoder.feed(byteBuffer.array());
            List<Message> messageList = new ArrayList<>();
            while (!messages.isEmpty()) {
                messageList.add(messages.poll());
            }
            return Flowable.fromIterable(messageList);
        }
    }).subscribe(subscriber);
    // The number of events equal to the size of request body (excluding trailing empty frame)
    verify(subscriber, times(testVector.requestBody().size())).onNext(any());
    // subscriber is not terminated (no onError/onComplete) since trailing empty frame is not delivered yet
    verify(subscriber, never()).onError(any());
    verify(subscriber, never()).onComplete();
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BiFunction(io.reactivex.functions.BiFunction) SignerTestUtils(software.amazon.awssdk.auth.signer.internal.SignerTestUtils) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) Message(software.amazon.eventstream.Message) EVENT_STREAM_DATE(software.amazon.awssdk.auth.signer.internal.BaseEventStreamAsyncAws4Signer.EVENT_STREAM_DATE) ArrayList(java.util.ArrayList) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) BinaryUtils(software.amazon.awssdk.utils.BinaryUtils) TestSubscriber(io.reactivex.subscribers.TestSubscriber) SdkHttpMethod(software.amazon.awssdk.http.SdkHttpMethod) ZoneOffset(java.time.ZoneOffset) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) LinkedList(java.util.LinkedList) Subscriber(org.reactivestreams.Subscriber) Publisher(org.reactivestreams.Publisher) Mockito.times(org.mockito.Mockito.times) EVENT_STREAM_SIGNATURE(software.amazon.awssdk.auth.signer.internal.BaseEventStreamAsyncAws4Signer.EVENT_STREAM_SIGNATURE) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) ZoneId(java.time.ZoneId) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) MessageDecoder(software.amazon.eventstream.MessageDecoder) Mockito.never(org.mockito.Mockito.never) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) Stream(java.util.stream.Stream) Function(io.reactivex.functions.Function) Lists(org.assertj.core.util.Lists) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Subscription(org.reactivestreams.Subscription) Clock(java.time.Clock) Queue(java.util.Queue) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) HeaderValue(software.amazon.eventstream.HeaderValue) Message(software.amazon.eventstream.Message) ArrayList(java.util.ArrayList) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) ByteBuffer(java.nio.ByteBuffer) BiFunction(io.reactivex.functions.BiFunction) Function(io.reactivex.functions.Function) MessageDecoder(software.amazon.eventstream.MessageDecoder) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) Subscription(org.reactivestreams.Subscription) Queue(java.util.Queue) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) Test(org.junit.jupiter.api.Test)

Example 3 with MessageDecoder

use of software.amazon.eventstream.MessageDecoder in project aws-sdk-java-v2 by aws.

the class EventStreamAws4SignerTest method readMessages.

private List<Message> readMessages(AsyncRequestBody signedBody) {
    MessageDecoder decoder = new MessageDecoder();
    Flowable.fromPublisher(signedBody).blockingForEach(x -> decoder.feed(x.array()));
    return decoder.getDecodedMessages();
}
Also used : MessageDecoder(software.amazon.eventstream.MessageDecoder)

Example 4 with MessageDecoder

use of software.amazon.eventstream.MessageDecoder in project aws-sdk-java-v2 by aws.

the class EventMarshallingTest method setup.

@Before
public void setup() {
    when(mockHttpClient.execute(any(AsyncExecuteRequest.class))).thenAnswer(this::mockExecute);
    client = EventStreamRestJsonAsyncClient.builder().region(Region.US_WEST_2).credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))).httpClient(mockHttpClient).build();
    marshalledEvents = new ArrayList<>();
    chunkDecoder = new MessageDecoder();
    eventDecoder = new MessageDecoder();
}
Also used : AsyncExecuteRequest(software.amazon.awssdk.http.async.AsyncExecuteRequest) MessageDecoder(software.amazon.eventstream.MessageDecoder) Before(org.junit.Before)

Aggregations

MessageDecoder (software.amazon.eventstream.MessageDecoder)3 Flowable (io.reactivex.Flowable)2 BiFunction (io.reactivex.functions.BiFunction)2 Function (io.reactivex.functions.Function)2 TestSubscriber (io.reactivex.subscribers.TestSubscriber)2 ByteBuffer (java.nio.ByteBuffer)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Clock (java.time.Clock)2 Instant (java.time.Instant)2 OffsetDateTime (java.time.OffsetDateTime)2 ZoneId (java.time.ZoneId)2 ZoneOffset (java.time.ZoneOffset)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 Queue (java.util.Queue)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Collectors (java.util.stream.Collectors)2