|
| 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 | +} |
0 commit comments