Skip to content

Commit 461a79e

Browse files
committed
Add json1.0 tests + fix test with custom host
1 parent be08f95 commit 461a79e

File tree

14 files changed

+2277
-20
lines changed

14 files changed

+2277
-20
lines changed

core/protocols/aws-query-protocol/src/main/java/software/amazon/awssdk/protocols/query/internal/marshall/QueryProtocolMarshaller.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ private QueryProtocolMarshaller(Builder builder) {
5252

5353
private SdkHttpFullRequest.Builder fillBasicRequestParams(OperationInfo operationInfo) {
5454
return ProtocolUtils.createSdkHttpRequest(operationInfo, endpoint)
55-
.encodedPath("")
5655
.putRawQueryParameter("Action", operationInfo.operationIdentifier())
5756
.putRawQueryParameter("Version", operationInfo.apiVersion());
5857
}

test/protocol-tests-core/src/main/java/software/amazon/awssdk/protocol/asserts/marshalling/JsonBodyAssertion.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
import com.fasterxml.jackson.databind.JsonNode;
2121
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import com.fasterxml.jackson.databind.node.ArrayNode;
23+
import com.fasterxml.jackson.databind.node.IntNode;
24+
import com.fasterxml.jackson.databind.node.LongNode;
25+
import com.fasterxml.jackson.databind.node.ObjectNode;
2226
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
2327

2428
/**
@@ -37,8 +41,35 @@ public JsonBodyAssertion(String jsonEquals) {
3741
@Override
3842
protected void doAssert(LoggedRequest actual) throws Exception {
3943
JsonNode expected = MAPPER.readTree(jsonEquals);
40-
JsonNode actualJson = MAPPER.readTree(actual.getBodyAsString());
44+
JsonNode actualJson = convertWholeNumberDoubleToLong(MAPPER.readTree(actual.getBodyAsString()));
4145
assertEquals(expected, actualJson);
4246
}
4347

48+
public static JsonNode convertWholeNumberDoubleToLong(JsonNode node) {
49+
if (node.isObject()) {
50+
ObjectNode obj = (ObjectNode) node;
51+
ObjectNode result = obj.objectNode();
52+
obj.fieldNames().forEachRemaining(field -> {
53+
result.set(field, convertWholeNumberDoubleToLong(obj.get(field)));
54+
});
55+
return result;
56+
} else if (node.isArray()) {
57+
ArrayNode array = (ArrayNode) node;
58+
ArrayNode result = array.arrayNode();
59+
for (JsonNode item : array) {
60+
result.add(convertWholeNumberDoubleToLong(item));
61+
}
62+
return result;
63+
} else if (node.isDouble()) {
64+
double value = node.doubleValue();
65+
if (value % 1 == 0) {
66+
if (value >= Integer.MIN_VALUE && value <= Integer.MAX_VALUE) {
67+
return new IntNode((int) value);
68+
} else {
69+
return new LongNode((long) value);
70+
}
71+
}
72+
}
73+
return node;
74+
}
4475
}

test/protocol-tests-core/src/main/java/software/amazon/awssdk/protocol/reflect/ClientReflector.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private Class<?> getInterfaceClass() {
6565
public Object invokeMethod(TestCase testCase, Object... params) throws Exception {
6666
String operationName = testCase.getWhen().getOperationName();
6767
Method operationMethod = getOperationMethod(operationName, params);
68-
return operationMethod.invoke(client, params);
68+
return operationMethod.invoke(getRequestClient(testCase), params);
6969
}
7070

7171
/**
@@ -80,7 +80,15 @@ public Object invokeStreamingMethod(TestCase testCase,
8080
ResponseTransformer<?, ?> responseHandler) throws Exception {
8181
String operationName = testCase.getWhen().getOperationName();
8282
Method operationMethod = getOperationMethod(operationName, requestObject.getClass(), ResponseTransformer.class);
83-
return operationMethod.invoke(client, requestObject, responseHandler);
83+
return operationMethod.invoke(getRequestClient(testCase), requestObject, responseHandler);
84+
}
85+
86+
private Object getRequestClient(TestCase testCase) {
87+
Object requestClient = this.client;
88+
if (testCase.getGiven().getHost() != null) {
89+
requestClient = createClientWithEndpoint("https://" + testCase.getGiven().getHost());
90+
}
91+
return requestClient;
8492
}
8593

8694
@Override
@@ -94,13 +102,20 @@ public void close() {
94102
* Create the sync client to use in the tests.
95103
*/
96104
private Object createClient() {
105+
return createClientWithEndpoint(getEndpoint());
106+
}
107+
108+
/**
109+
* Create the sync client to use in the tests.
110+
*/
111+
private Object createClientWithEndpoint(String endpoint) {
97112
try {
98113
// Reflectively create a builder, configure it, and then create the client.
99114
Object untypedBuilder = interfaceClass.getMethod("builder").invoke(null);
100115
AwsClientBuilder<?, ?> builder = (AwsClientBuilder<?, ?>) untypedBuilder;
101116
return builder.credentialsProvider(getMockCredentials())
102117
.region(Region.US_EAST_1)
103-
.endpointOverride(URI.create(getEndpoint()))
118+
.endpointOverride(URI.create(endpoint))
104119
.build();
105120
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
106121
throw new RuntimeException(e);

test/protocol-tests-core/src/main/java/software/amazon/awssdk/protocol/reflect/ShapeModelReflector.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
*/
4242
public class ShapeModelReflector {
4343

44+
public static final long EPOCH_SECONDS_2050 = 2524680000L;
4445
private final IntermediateModel model;
4546
private final String shapeName;
4647
private final JsonNode input;
@@ -250,15 +251,7 @@ private Object getSimpleMemberValue(JsonNode currentNode, MemberModel memberMode
250251
case "Double":
251252
return currentNode.asDouble();
252253
case "Instant":
253-
if (currentNode.isFloatingPointNumber()) {
254-
double fractionalEpoch = currentNode.asDouble();
255-
long seconds = (long) fractionalEpoch;
256-
long nanos = (long) ((fractionalEpoch - seconds) * 1_000_000_000L);
257-
//truncate to ms
258-
return Instant.ofEpochSecond(seconds, (nanos / 1_000_000) * 1_000_000);
259-
} else {
260-
return Instant.ofEpochSecond(currentNode.asLong());
261-
}
254+
return getInstant(currentNode);
262255
case "SdkBytes":
263256
return SdkBytes.fromUtf8String(currentNode.asText());
264257
case "Float":
@@ -273,6 +266,26 @@ private Object getSimpleMemberValue(JsonNode currentNode, MemberModel memberMode
273266
}
274267
}
275268

269+
private static Instant getInstant(JsonNode currentNode) {
270+
if (currentNode.isFloatingPointNumber()) {
271+
double fractionalEpoch = currentNode.asDouble();
272+
long seconds = (long) fractionalEpoch;
273+
long nanos = (long) ((fractionalEpoch - seconds) * 1_000_000_000L);
274+
//truncate to ms
275+
return Instant.ofEpochSecond(seconds, (nanos / 1_000_000) * 1_000_000);
276+
} else {
277+
// attempt to infer if this value should be epoch seconds or milliseconds
278+
long epoch = currentNode.asLong();
279+
// smithy protocol tests occasionally uses small epoch seconds in tests (<10000)
280+
// interpret those as seconds. Additionally, anything that would be past 2050 as seconds,
281+
// consider epoch ms.
282+
if (epoch < 10000L || epoch > EPOCH_SECONDS_2050) {
283+
return Instant.ofEpochMilli(epoch);
284+
}
285+
return Instant.ofEpochSecond(currentNode.asLong());
286+
}
287+
}
288+
276289
private Character asCharacter(JsonNode currentNode) {
277290
String text = currentNode.asText();
278291
if (text != null && text.length() > 1) {

test/protocol-tests-core/src/main/java/software/amazon/awssdk/protocol/runners/MarshallingTestRunner.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class MarshallingTestRunner {
6161
void runTest(TestCase testCase) throws Exception {
6262
resetWireMock();
6363
ShapeModelReflector shapeModelReflector = createShapeModelReflector(testCase);
64-
AwsRequest request = createRequest(testCase, shapeModelReflector);
64+
AwsRequest request = createRequest(shapeModelReflector);
6565

6666
if (!model.getShapes().get(testCase.getWhen().getOperationName() + "Request").isHasStreamingMember()) {
6767
clientReflector.invokeMethod(testCase, request);
@@ -88,16 +88,12 @@ private void resetWireMock() {
8888
stubFor(any(urlMatching(".*")).willReturn(responseDefBuilder));
8989
}
9090

91-
private AwsRequest createRequest(TestCase testCase, ShapeModelReflector shapeModelReflector) {
91+
private AwsRequest createRequest(ShapeModelReflector shapeModelReflector) {
9292
return ((AwsRequest) shapeModelReflector.createShapeObject())
9393
.toBuilder()
9494
.overrideConfiguration(requestConfiguration -> requestConfiguration
9595
.addPlugin(config -> {
9696
config.overrideConfiguration(c -> c.addExecutionInterceptor(localhostOnlyForWiremockInterceptor));
97-
98-
if (StringUtils.isNotBlank(testCase.getGiven().getHost())) {
99-
config.endpointOverride(URI.create("https://" + testCase.getGiven().getHost()));
100-
}
10197
}))
10298
.build();
10399
}

0 commit comments

Comments
 (0)