Skip to content

Commit bf89f54

Browse files
committed
Add cbor tests from smithy + add new cborEquals body assertion
1 parent 461a79e commit bf89f54

File tree

5 files changed

+629
-446
lines changed

5 files changed

+629
-446
lines changed

test/protocol-tests-core/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@
137137
<groupId>com.fasterxml.jackson.core</groupId>
138138
<artifactId>jackson-annotations</artifactId>
139139
</dependency>
140+
<dependency>
141+
<groupId>com.fasterxml.jackson.dataformat</groupId>
142+
<artifactId>jackson-dataformat-cbor</artifactId>
143+
</dependency>
140144
</dependencies>
141145

142146
<!-- Disable spotbugs for this test module to speed up the build. -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.protocol.asserts.marshalling;
17+
18+
import static org.junit.Assert.assertEquals;
19+
20+
import com.fasterxml.jackson.databind.JsonNode;
21+
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import com.fasterxml.jackson.databind.node.ArrayNode;
23+
import com.fasterxml.jackson.databind.node.DoubleNode;
24+
import com.fasterxml.jackson.databind.node.ObjectNode;
25+
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
26+
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
27+
import java.util.Base64;
28+
29+
/**
30+
* Asserts on the body (expected to be CBOR) of the marshalled request.
31+
*/
32+
public class CborBodyAssertion extends MarshallingAssertion {
33+
private static final ObjectMapper MAPPER = new ObjectMapper(new CBORFactory());
34+
35+
private final String cborEquals;
36+
37+
public CborBodyAssertion(String cborEquals) {
38+
this.cborEquals = cborEquals;
39+
}
40+
41+
@Override
42+
protected void doAssert(LoggedRequest actual) throws Exception {
43+
JsonNode expected = normalizeToDoubles(MAPPER.readTree(Base64.getDecoder().decode(cborEquals)));
44+
JsonNode actualJson = normalizeToDoubles(MAPPER.readTree(actual.getBody()));
45+
assertEquals(expected, actualJson);
46+
}
47+
48+
private JsonNode normalizeToDoubles(JsonNode node) {
49+
if (node.isFloat() || node.isDouble()) {
50+
return DoubleNode.valueOf(node.doubleValue());
51+
} else if (node.isInt() || node.isLong() || node.isShort() || node.isBigInteger() || node.isBigDecimal()) {
52+
return DoubleNode.valueOf(node.doubleValue());
53+
} else if (node.isArray()) {
54+
ArrayNode array = MAPPER.createArrayNode();
55+
for (JsonNode item : node) {
56+
array.add(normalizeToDoubles(item));
57+
}
58+
return array;
59+
} else if (node.isObject()) {
60+
ObjectNode obj = MAPPER.createObjectNode();
61+
node.fields().forEachRemaining(entry -> obj.set(entry.getKey(), normalizeToDoubles(entry.getValue())));
62+
return obj;
63+
}
64+
return node;
65+
}
66+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public void setQueryEquals(String queryEquals) {
3737
addAssertion(new QueryBodyAssertion(queryEquals));
3838
}
3939

40+
public void setCborEquals(String cborEquals) {
41+
addAssertion(new CborBodyAssertion(cborEquals));
42+
}
43+
4044
public void setEquals(String equals) {
4145
addAssertion(new RawBodyAssertion(equals));
4246
}

0 commit comments

Comments
 (0)