Skip to content

Add Query Compatible protocol tests #6230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@

public class TestCase {

private String id;
private String description;
// Given is optional
private Given given = new Given();
private When when;
private Then then;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getDescription() {
return description;
}
Expand Down Expand Up @@ -57,7 +66,7 @@ public void setThen(Then then) {

@Override
public String toString() {
return description;
return id;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ public class Then {
private final MarshallingAssertion serializedAs;
private final UnmarshallingAssertion deserializedAs;
private final UnmarshallingAssertion errorDeserializedAs;
private final String errorCode;

@JsonCreator
public Then(@JsonProperty("serializedAs") SerializedAs serializedAs,
@JsonProperty("deserializedAs") JsonNode deserializedAs) {
@JsonProperty("deserializedAs") JsonNode deserializedAs,
@JsonProperty("errorCode") String errorCode) {
this.serializedAs = serializedAs;
this.deserializedAs = new UnmarshalledResultAssertion(deserializedAs);
this.errorDeserializedAs = new UnmarshalledErrorAssertion(deserializedAs);
this.errorCode = errorCode;
}

/**
Expand All @@ -59,4 +62,12 @@ public UnmarshallingAssertion getUnmarshallingAssertion() {
public UnmarshallingAssertion getErrorUnmarshallingAssertion() {
return errorDeserializedAs;
}

/**
*
* @return The errorCode String to use for error unmarshalling tests
*/
public String getErrorCode() {
return errorCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void runTests(List<TestCase> tests) throws Exception {
}

public void runTest(TestCase testCase) throws Exception {
log.info("Running test: {}", testCase.getDescription());
log.info("Running test: [{}] {}", testCase.getId(), testCase.getDescription());
switch (testCase.getWhen().getAction()) {
case MARSHALL:
marshallingTestRunner.runTest(testCase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@
import com.github.tomakehurst.wiremock.client.WireMock;
import java.lang.reflect.InvocationTargetException;
import java.util.Base64;
import org.junit.Assert;
import software.amazon.awssdk.awscore.exception.AwsErrorDetails;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.model.intermediate.Metadata;
import software.amazon.awssdk.core.sync.ResponseTransformer;
import software.amazon.awssdk.http.AbortableInputStream;
import software.amazon.awssdk.protocol.asserts.unmarshalling.UnmarshallingTestContext;
import software.amazon.awssdk.protocol.model.GivenResponse;
import software.amazon.awssdk.protocol.model.TestCase;
import software.amazon.awssdk.protocol.model.Then;
import software.amazon.awssdk.protocol.reflect.ClientReflector;
import software.amazon.awssdk.protocol.reflect.ShapeModelReflector;
import software.amazon.awssdk.utils.IoUtils;
Expand Down Expand Up @@ -90,9 +94,30 @@ private void runErrorUnmarshallTest(TestCase testCase) throws Exception {
throw new IllegalStateException("Test case expected client to throw error");
} catch (InvocationTargetException t) {
String errorName = testCase.getWhen().getErrorName();
testCase.getThen().getErrorUnmarshallingAssertion().assertMatches(
createErrorContext(operationName, errorName), t.getCause());
Throwable cause = t.getCause();
Then then = testCase.getThen();

then.getErrorUnmarshallingAssertion().assertMatches(
createErrorContext(operationName, errorName), cause);

validateErrorCodeIfPresent(then, cause);
}
}

private void validateErrorCodeIfPresent(Then then, Throwable cause) {
String expectedErrorCode = then.getErrorCode();
if (expectedErrorCode != null) {
String actualErrorCode = extractErrorCode(cause);
Assert.assertEquals(expectedErrorCode, actualErrorCode);
}
}

private String extractErrorCode(Throwable cause) {
if (!(cause instanceof AwsServiceException)) {
return null;
}
AwsErrorDetails awsErrorDetails = ((AwsServiceException) cause).awsErrorDetails();
return awsErrorDetails.errorCode();
}

private UnmarshallingTestContext createErrorContext(String operationName, String errorName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[
{
"id": "QueryCompatibleAwsJson10CborSendsQueryModeHeader",
"description": "Clients for query-compatible services MUST send the x-amzn-query-mode header.",
"given": {
"input": {}
},
"when": {
"action": "marshall",
"operation": "QueryCompatibleOperation"
},
"then": {
"serializedAs": {
"method": "POST",
"headers": {
"contains": {
"X-Amz-Target": "QueryCompatibleJsonRpc10.QueryCompatibleOperation",
"x-amzn-query-mode": "true",
"Content-Type": "application/x-amz-json-1.0"
}
},
"body": {
"jsonEquals": "{}"
},
"uri": "/"
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"id": "QueryCompatibleAwsJson10CborNoCustomCodeError",
"description": "Parses simple errors with no query error code",
"given": {
"response": {
"status_code": 400,
"headers": {
"Content-Type": "application/x-amz-json-1.0"
},
"body": "{\n \"__type\": \"aws.protocoltests.json10#NoCustomCodeError\",\n \"Message\": \"Hi\"\n}"
}
},
"when": {
"action": "errorUnmarshall",
"operation": "QueryCompatibleOperation",
"error": "NoCustomCodeError"
},
"then": {
"deserializedAs": {
"message": "Hi"
}
}
},
{
"id": "QueryCompatibleAwsJson10CustomCodeError",
"description": "Parses simple errors with query error code",
"given": {
"response": {
"status_code": 400,
"headers": {
"x-amzn-query-error": "Customized;Sender",
"Content-Type": "application/x-amz-json-1.0"
},
"body": "{\n \"__type\": \"aws.protocoltests.json10#CustomCodeError\",\n \"Message\": \"Hi\"\n}"
}
},
"when": {
"action": "errorUnmarshall",
"operation": "QueryCompatibleOperation",
"error": "CustomCodeError"
},
"then": {
"deserializedAs": {
"message": "Hi"
},
"errorCode": "Customized"
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[
{
"id": "NonQueryCompatibleRpcV2CborForbidsQueryModeHeader",
"description": "The query mode header MUST NOT be set on non-query-compatible services.",
"given": {
"input": {}
},
"when": {
"action": "marshall",
"operation": "NonQueryCompatibleOperation"
},
"then": {
"serializedAs": {
"method": "POST",
"headers": {
"contains": {
"Accept": "application/cbor",
"smithy-protocol": "rpc-v2-cbor"
},
"doesNotContain": [
"x-amzn-query-mode"
]
},
"body": {
"encodedEquals": ""
},
"uri": "/service/NonQueryCompatibleRpcV2Protocol/operation/NonQueryCompatibleOperation"
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[
{
"id": "QueryCompatibleRpcV2CborSendsQueryModeHeader",
"description": "Clients for query-compatible services MUST send the x-amzn-query-mode header.",
"given": {
"input": {}
},
"when": {
"action": "marshall",
"operation": "QueryCompatibleOperation"
},
"then": {
"serializedAs": {
"method": "POST",
"headers": {
"contains": {
"smithy-protocol": "rpc-v2-cbor",
"Accept": "application/cbor",
"x-amzn-query-mode": "true"
},
"doesNotContain": [
"Content-Type",
"X-Amz-Target"
]
},
"body": {
"encodedEquals": ""
},
"uri": "/service/QueryCompatibleRpcV2Protocol/operation/QueryCompatibleOperation"
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[
{
"id": "QueryCompatibleRpcV2CborNoCustomCodeError",
"description": "Parses simple RpcV2 CBOR errors with no query error code",
"given": {
"response": {
"status_code": 400,
"headers": {
"smithy-protocol": "rpc-v2-cbor",
"Content-Type": "application/cbor"
},
"binaryBody": "uQACZl9fdHlwZXgtYXdzLnByb3RvY29sdGVzdHMucnBjdjJjYm9yI05vQ3VzdG9tQ29kZUVycm9yZ01lc3NhZ2ViSGk="
}
},
"when": {
"action": "errorUnmarshall",
"operation": "QueryCompatibleOperation",
"error": "NoCustomCodeError"
},
"then": {
"deserializedAs": {
"message": "Hi"
}
}
},
{
"id": "QueryCompatibleRpcV2CborCustomCodeError",
"description": "Parses simple RpcV2 CBOR errors with query error code",
"given": {
"response": {
"status_code": 400,
"headers": {
"x-amzn-query-error": "Customized;Sender",
"smithy-protocol": "rpc-v2-cbor",
"Content-Type": "application/cbor"
},
"binaryBody": "uQACZl9fdHlwZXgrYXdzLnByb3RvY29sdGVzdHMucnBjdjJjYm9yI0N1c3RvbUNvZGVFcnJvcmdNZXNzYWdlYkhp"
}
},
"when": {
"action": "errorUnmarshall",
"operation": "QueryCompatibleOperation",
"error": "CustomCodeError"
},
"then": {
"deserializedAs": {
"message": "Hi"
},
"errorCode": "Customized"
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"testCases": [
"cases/json-querycompatible-input.json",
"cases/json-querycompatible-output.json"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"testCases": [
"cases/smithy-rpcv2-nonquerycompatible-input.json",
"cases/smithy-rpcv2-nonquerycompatible-output.json"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"testCases": [
"cases/smithy-rpcv2-querycompatible-input.json",
"cases/smithy-rpcv2-querycompatible-output.json"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"skipEndpointTestGeneration": true
}
Loading