|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.services.eventstreams; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.mockito.Matchers.any; |
| 20 | +import static org.mockito.Mockito.when; |
| 21 | +import io.reactivex.Flowable; |
| 22 | +import java.nio.ByteBuffer; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.util.concurrent.CompletableFuture; |
| 26 | +import java.util.stream.Collectors; |
| 27 | +import java.util.stream.Stream; |
| 28 | +import org.junit.Before; |
| 29 | +import org.junit.Test; |
| 30 | +import org.junit.runner.RunWith; |
| 31 | +import org.mockito.Mock; |
| 32 | +import org.mockito.invocation.InvocationOnMock; |
| 33 | +import org.mockito.runners.MockitoJUnitRunner; |
| 34 | +import org.reactivestreams.Subscriber; |
| 35 | +import org.reactivestreams.Subscription; |
| 36 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 37 | +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 38 | +import software.amazon.awssdk.http.SdkHttpResponse; |
| 39 | +import software.amazon.awssdk.http.async.AsyncExecuteRequest; |
| 40 | +import software.amazon.awssdk.http.async.SdkAsyncHttpClient; |
| 41 | +import software.amazon.awssdk.http.async.SdkHttpContentPublisher; |
| 42 | +import software.amazon.awssdk.services.eventstreamrestjson.EventStreamRestJsonAsyncClient; |
| 43 | +import software.amazon.awssdk.services.eventstreamrestjson.model.EventStream; |
| 44 | +import software.amazon.awssdk.services.eventstreamrestjson.model.EventStreamOperationRequest; |
| 45 | +import software.amazon.awssdk.services.eventstreamrestjson.model.EventStreamOperationResponseHandler; |
| 46 | +import software.amazon.awssdk.services.eventstreamrestjson.model.InputEventStream; |
| 47 | +import software.amazon.eventstream.Message; |
| 48 | +import software.amazon.eventstream.MessageDecoder; |
| 49 | + |
| 50 | +@RunWith(MockitoJUnitRunner.class) |
| 51 | +public class EventMarshallingTest { |
| 52 | + @Mock |
| 53 | + public SdkAsyncHttpClient mockHttpClient; |
| 54 | + |
| 55 | + private EventStreamRestJsonAsyncClient client; |
| 56 | + |
| 57 | + private List<Message> marshalledEvents; |
| 58 | + |
| 59 | + private MessageDecoder chunkDecoder; |
| 60 | + private MessageDecoder eventDecoder; |
| 61 | + |
| 62 | + @Before |
| 63 | + public void setup() { |
| 64 | + when(mockHttpClient.execute(any(AsyncExecuteRequest.class))).thenAnswer(this::mockExecute); |
| 65 | + client = EventStreamRestJsonAsyncClient.builder() |
| 66 | + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) |
| 67 | + .httpClient(mockHttpClient) |
| 68 | + .build(); |
| 69 | + |
| 70 | + marshalledEvents = new ArrayList<>(); |
| 71 | + |
| 72 | + chunkDecoder = new MessageDecoder(); |
| 73 | + eventDecoder = new MessageDecoder(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testMarshalling_setsCorrectEventType() { |
| 78 | + List<InputEventStream> inputEvents = Stream.of( |
| 79 | + InputEventStream.inputEventBuilder().build(), |
| 80 | + InputEventStream.inputEventBBuilder().build(), |
| 81 | + InputEventStream.inputEventTwoBuilder().build() |
| 82 | + ).collect(Collectors.toList()); |
| 83 | + |
| 84 | + Flowable<InputEventStream> inputStream = Flowable.fromIterable(inputEvents); |
| 85 | + |
| 86 | + client.eventStreamOperation(EventStreamOperationRequest.builder().build(), inputStream, EventStreamOperationResponseHandler.builder() |
| 87 | + .subscriber(() -> new Subscriber<EventStream>() { |
| 88 | + @Override |
| 89 | + public void onSubscribe(Subscription subscription) { |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void onNext(EventStream eventStream) { |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void onError(Throwable throwable) { |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void onComplete() { |
| 105 | + |
| 106 | + } |
| 107 | + }) |
| 108 | + .build()).join(); |
| 109 | + |
| 110 | + List<String> expectedTypes = Stream.of( |
| 111 | + "InputEvent", |
| 112 | + "InputEventB", |
| 113 | + "InputEventTwo" |
| 114 | + ).collect(Collectors.toList());; |
| 115 | + |
| 116 | + assertThat(marshalledEvents).hasSize(inputEvents.size()); |
| 117 | + |
| 118 | + for (int i = 0; i < marshalledEvents.size(); ++i) { |
| 119 | + Message marshalledEvent = marshalledEvents.get(i); |
| 120 | + String expectedType = expectedTypes.get(i); |
| 121 | + assertThat(marshalledEvent.getHeaders().get(":event-type").getString()) |
| 122 | + .isEqualTo(expectedType); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private CompletableFuture<Void> mockExecute(InvocationOnMock invocation) { |
| 127 | + AsyncExecuteRequest request = invocation.getArgumentAt(0, AsyncExecuteRequest.class); |
| 128 | + SdkHttpContentPublisher content = request.requestContentPublisher(); |
| 129 | + List<ByteBuffer> chunks = Flowable.fromPublisher(content).toList().blockingGet(); |
| 130 | + |
| 131 | + for (ByteBuffer c : chunks) { |
| 132 | + chunkDecoder.feed(c); |
| 133 | + } |
| 134 | + |
| 135 | + for (Message m : chunkDecoder.getDecodedMessages()) { |
| 136 | + eventDecoder.feed(m.getPayload()); |
| 137 | + } |
| 138 | + |
| 139 | + marshalledEvents.addAll(eventDecoder.getDecodedMessages()); |
| 140 | + |
| 141 | + request.responseHandler().onHeaders(SdkHttpResponse.builder().statusCode(200).build()); |
| 142 | + request.responseHandler().onStream(Flowable.empty()); |
| 143 | + |
| 144 | + return CompletableFuture.completedFuture(null); |
| 145 | + } |
| 146 | +} |
0 commit comments