Skip to content

Commit 1fa0892

Browse files
committed
Make mock matcher take a record
This record makes matching more extensible, and also no provides the ApiOperation being invoked, allowing for more operation-specific mocking.
1 parent e4be78c commit 1fa0892

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

client-core/src/test/java/software/amazon/smithy/java/client/core/plugins/InjectIdempotencyTokenPluginTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void injectsToken() {
8181
private String callAndGetToken(String operation, Document input) {
8282
var mock = MockPlugin.builder()
8383
.addMatcher(
84-
(ctx, i) -> new MockedResult.Response(
84+
request -> new MockedResult.Response(
8585
HttpResponse.builder()
8686
.statusCode(200)
8787
.headers(HttpHeaders.of(Map.of("content-type", List.of("application/json"))))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.java.client.http.mock;
7+
8+
import software.amazon.smithy.java.context.Context;
9+
import software.amazon.smithy.java.core.schema.ApiOperation;
10+
import software.amazon.smithy.java.core.schema.SerializableStruct;
11+
12+
/**
13+
* The input to a {@link MockPlugin.Builder#addMatcher} function.
14+
*
15+
* @param context Context of the call.
16+
* @param operation Operation being called.
17+
* @param input Input of the call.
18+
*/
19+
public record MatcherRequest(Context context, ApiOperation<?, ?> operation, SerializableStruct input) {}

mock-client-plugin/src/main/java/software/amazon/smithy/java/client/http/mock/MockPlugin.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.util.NoSuchElementException;
1414
import java.util.ServiceLoader;
1515
import java.util.concurrent.CompletableFuture;
16-
import java.util.function.BiFunction;
1716
import java.util.function.Function;
1817
import java.util.stream.Collectors;
1918
import software.amazon.smithy.java.client.core.ClientConfig;
@@ -64,7 +63,7 @@ public final class MockPlugin implements ClientPlugin {
6463
.map(ServiceLoader.Provider::get)
6564
.collect(Collectors.toMap(ServerProtocolProvider::getProtocolId, Function.identity()));
6665

67-
private final List<BiFunction<Context, SerializableStruct, MockedResult>> matchers = new ArrayList<>();
66+
private final List<Function<MatcherRequest, MockedResult>> matchers = new ArrayList<>();
6867
private final List<MockedRequest> requests = Collections.synchronizedList(new ArrayList<>());
6968
private final Service mockService;
7069

@@ -109,7 +108,7 @@ public void clearRequests() {
109108
* Creates a MockPlugin.
110109
*/
111110
public static final class Builder {
112-
private final List<BiFunction<Context, SerializableStruct, MockedResult>> matchers = new ArrayList<>();
111+
private final List<Function<MatcherRequest, MockedResult>> matchers = new ArrayList<>();
113112

114113
private Builder() {}
115114

@@ -128,10 +127,10 @@ public MockPlugin build() {
128127
* <p>If null is returned, then subsequent matchers will attempt to match and intercept the request. If no
129128
* matcher intercepts the request, {@link NoSuchElementException} is thrown.
130129
*
131-
* @param matcher Matcher that receives the context and input shape, and returns a result or null.
130+
* @param matcher Matcher that returns a result or null based on the request.
132131
* @return the builder.
133132
*/
134-
public Builder addMatcher(BiFunction<Context, SerializableStruct, MockedResult> matcher) {
133+
public Builder addMatcher(Function<MatcherRequest, MockedResult> matcher) {
135134
matchers.add(matcher);
136135
return this;
137136
}
@@ -147,7 +146,7 @@ public Builder addMatcher(BiFunction<Context, SerializableStruct, MockedResult>
147146
* @return the builder.
148147
*/
149148
public Builder addQueue(MockQueue resultQueue) {
150-
matchers.add((context, input) -> resultQueue.poll());
149+
matchers.add(request -> resultQueue.poll());
151150
return this;
152151
}
153152
}
@@ -225,8 +224,13 @@ public CompletableFuture<HttpResponse> send(Context context, HttpRequest request
225224
requests.add(currentRequest.request());
226225

227226
MockedResult result = null;
227+
var matcherRequest = new MatcherRequest(
228+
context,
229+
currentRequest.operation().getApiOperation(),
230+
currentRequest.request().input()
231+
);
228232
for (var matcher : matchers) {
229-
result = matcher.apply(context, currentRequest.request().input());
233+
result = matcher.apply(matcherRequest);
230234
if (result != null) {
231235
break;
232236
}

mock-client-plugin/src/test/java/software/amazon/smithy/java/client/http/mock/MockPluginTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,16 @@ public void returnsResultsBasedOnPredicates() throws URISyntaxException {
185185
var bQueue = new MockQueue();
186186

187187
var mock = MockPlugin.builder()
188-
.addMatcher((ctx, i) -> {
189-
if (i instanceof Document d) {
188+
.addMatcher(request -> {
189+
if (request.input() instanceof Document d) {
190190
if (d.getMember("id").asString().equals("a")) {
191191
return aQueue.poll();
192192
}
193193
}
194194
return null;
195195
})
196-
.addMatcher((ctx, i) -> {
197-
if (i instanceof Document d) {
196+
.addMatcher(request -> {
197+
if (request.input() instanceof Document d) {
198198
if (d.getMember("id").asString().equals("b")) {
199199
return bQueue.poll();
200200
}

0 commit comments

Comments
 (0)