• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.modulepath.tests.mocktests;
17 
18 import static software.amazon.awssdk.utils.FunctionalUtils.invokeSafely;
19 
20 import java.nio.ByteBuffer;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Optional;
24 import java.util.concurrent.CompletableFuture;
25 import org.reactivestreams.Subscriber;
26 import org.reactivestreams.Subscription;
27 import software.amazon.awssdk.http.HttpExecuteResponse;
28 import software.amazon.awssdk.http.SdkHttpRequest;
29 import software.amazon.awssdk.http.async.AsyncExecuteRequest;
30 import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
31 import software.amazon.awssdk.http.async.SdkHttpContentPublisher;
32 import software.amazon.awssdk.utils.IoUtils;
33 
34 /**
35  * Mock implementation of {@link SdkAsyncHttpClient}.
36  */
37 public final class MockAyncHttpClient implements SdkAsyncHttpClient {
38 
39     private final List<SdkHttpRequest> capturedRequests = new ArrayList<>();
40     private HttpExecuteResponse nextResponse;
41 
42 
43     @Override
execute(AsyncExecuteRequest request)44     public CompletableFuture<Void> execute(AsyncExecuteRequest request) {
45         capturedRequests.add(request.request());
46 
47         request.responseHandler().onHeaders(nextResponse.httpResponse());
48         request.responseHandler().onStream(new SdkHttpContentPublisher() {
49             byte[] content = nextResponse.responseBody().map(p -> invokeSafely(() -> IoUtils.toByteArray(p)))
50                                          .orElseGet(() -> new byte[0]);
51 
52             @Override
53             public Optional<Long> contentLength() {
54                 return Optional.of((long) content.length);
55             }
56 
57             @Override
58             public void subscribe(Subscriber<? super ByteBuffer> s) {
59                 s.onSubscribe(new Subscription() {
60                     private boolean running = true;
61 
62                     @Override
63                     public void request(long n) {
64                         if (n <= 0) {
65                             running = false;
66                             s.onError(new IllegalArgumentException("Demand must be positive"));
67                         } else if (running) {
68                             running = false;
69                             s.onNext(ByteBuffer.wrap(content));
70                             s.onComplete();
71                         }
72                     }
73 
74                     @Override
75                     public void cancel() {
76                         running = false;
77                     }
78                 });
79             }
80         });
81         return CompletableFuture.completedFuture(null);
82     }
83 
84     @Override
close()85     public void close() {
86     }
87 
88     /**
89      * Resets this mock by clearing any captured requests and wiping any stubbed responses.
90      */
reset()91     public void reset() {
92         this.capturedRequests.clear();
93         this.nextResponse = null;
94     }
95 
stubNextResponse(HttpExecuteResponse nextResponse)96     public void stubNextResponse(HttpExecuteResponse nextResponse) {
97         this.nextResponse = nextResponse;
98     }
99 
100 }
101