-
Notifications
You must be signed in to change notification settings - Fork 920
Expand protocol test coverage for json1.0, query and cbor #6245
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
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6534599
Add support for new smithy translated protocol test format
alextwoods 497eff3
Merge branch 'master' into alexwoo/protocol-test-updates
alextwoods f7ebd56
Various fixes + start adding new query tests
alextwoods cd347c6
Merge branch 'master' into alexwoo/protocol-test-updates
alextwoods 5ee139f
WIP - adding more new test features
alextwoods be08f95
All query tests passing!
alextwoods 461a79e
Add json1.0 tests + fix test with custom host
alextwoods bf89f54
Add cbor tests from smithy + add new cborEquals body assertion
alextwoods 397dad6
Remove cbor test that java cannot change behavior for
alextwoods 0452595
Add changelog + fix checkstyle
alextwoods c164890
Fix "/" on query paths
alextwoods e8fa838
Fix request nil check
alextwoods 3b67aec
Revert behavior change in query when request path is set - remove fai…
alextwoods a7df9a3
Minor cleanups
alextwoods 6f86b08
Add comment to explain content-type in header contains
alextwoods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
.../src/main/java/software/amazon/awssdk/protocol/asserts/marshalling/CborBodyAssertion.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.protocol.asserts.marshalling; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.fasterxml.jackson.databind.node.DoubleNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.fasterxml.jackson.dataformat.cbor.CBORFactory; | ||
import com.github.tomakehurst.wiremock.verification.LoggedRequest; | ||
import java.util.Base64; | ||
|
||
/** | ||
* Asserts on the body (expected to be CBOR) of the marshalled request. | ||
*/ | ||
public class CborBodyAssertion extends MarshallingAssertion { | ||
private static final ObjectMapper MAPPER = new ObjectMapper(new CBORFactory()); | ||
|
||
private final String cborEquals; | ||
|
||
public CborBodyAssertion(String cborEquals) { | ||
this.cborEquals = cborEquals; | ||
} | ||
|
||
@Override | ||
protected void doAssert(LoggedRequest actual) throws Exception { | ||
JsonNode expected = normalizeToDoubles(MAPPER.readTree(Base64.getDecoder().decode(cborEquals))); | ||
JsonNode actualJson = normalizeToDoubles(MAPPER.readTree(actual.getBody())); | ||
assertEquals(expected, actualJson); | ||
} | ||
|
||
/** | ||
* CBOR allows serializing floating point numbers to different sizes (eg, float16/32/64). | ||
* However, in assertEquals float and double nodes will never be equal so we convert them | ||
* all to doubles. | ||
*/ | ||
private JsonNode normalizeToDoubles(JsonNode node) { | ||
if (node.isFloat() || node.isDouble()) { | ||
return DoubleNode.valueOf(node.doubleValue()); | ||
} | ||
if (node.isArray()) { | ||
ArrayNode array = MAPPER.createArrayNode(); | ||
for (JsonNode item : node) { | ||
array.add(normalizeToDoubles(item)); | ||
} | ||
return array; | ||
} | ||
if (node.isObject()) { | ||
ObjectNode obj = MAPPER.createObjectNode(); | ||
node.fields().forEachRemaining(entry -> obj.set(entry.getKey(), normalizeToDoubles(entry.getValue()))); | ||
return obj; | ||
} | ||
return node; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...src/main/java/software/amazon/awssdk/protocol/asserts/marshalling/QueryBodyAssertion.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.protocol.asserts.marshalling; | ||
|
||
import static org.hamcrest.Matchers.containsInAnyOrder; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.Assert.assertThat; | ||
import static software.amazon.awssdk.protocol.asserts.marshalling.QueryUtils.parseQueryParams; | ||
import static software.amazon.awssdk.protocol.asserts.marshalling.QueryUtils.parseQueryParamsFromBody; | ||
|
||
import com.github.tomakehurst.wiremock.verification.LoggedRequest; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class QueryBodyAssertion extends MarshallingAssertion { | ||
private final String queryEquals; | ||
|
||
public QueryBodyAssertion(String queryEquals) { | ||
this.queryEquals = queryEquals; | ||
} | ||
|
||
@Override | ||
protected void doAssert(LoggedRequest actual) throws Exception { | ||
Map<String, List<String>> expectedParams = parseQueryParamsFromBody(queryEquals); | ||
try { | ||
Map<String, List<String>> actualParams = parseQueryParams(actual); | ||
doAssert(expectedParams, actualParams); | ||
} catch (AssertionError error) { | ||
// We may send the query params in the body if there is no other content. Try | ||
// decoding body as params and rerun the assertions. | ||
Map<String, List<String>> actualParams = parseQueryParamsFromBody( | ||
actual.getBodyAsString()); | ||
doAssert(expectedParams, actualParams); | ||
} | ||
} | ||
|
||
private void doAssert(Map<String, List<String>> expectedParams, Map<String, List<String>> actualParams) { | ||
assertThat(actualParams.keySet(), equalTo(expectedParams.keySet())); | ||
expectedParams.forEach((key, value) -> assertParamsEqual(actualParams.get(key), value)); | ||
} | ||
|
||
private void assertParamsEqual(List<String> actual, List<String> expected) { | ||
if (expected.stream().allMatch(QueryBodyAssertion::isNumeric)) { | ||
assertThat( | ||
actual.stream().map(Double::parseDouble).collect(Collectors.toList()), | ||
containsInAnyOrder(expected.stream().map(Double::parseDouble).toArray())); | ||
} else { | ||
assertThat(actual, containsInAnyOrder(expected.toArray())); | ||
} | ||
} | ||
|
||
public static boolean isNumeric(String str) { | ||
if (str == null || str.isEmpty()) { | ||
return false; | ||
} | ||
try { | ||
Double.parseDouble(str); | ||
return true; | ||
} catch (NumberFormatException e) { | ||
return false; | ||
} | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand the java limitation of not being able to have a number as an enum value, but can you explain how this substitution works in practice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the value has leading digits (eg:
0
or123_some_value
) the regex will match, and then we just prefix it with "VALUE", so we end up withVALUE_0
andVALUE_123_SOME_VALUE
for those cases.