Skip to content

Commit c786e67

Browse files
authored
fix: Account for indeterminate Map ordering in unit tests (#274)
1 parent 3037b5f commit c786e67

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

sdk1/src/test/java/com/amazonaws/services/dynamodbv2/datamodeling/encryption/DynamoDBEncryptorTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.HashSet;
5050
import java.util.Map;
5151
import java.util.Set;
52+
import java.util.TreeMap;
5253
import java.util.concurrent.ConcurrentHashMap;
5354
import java.util.concurrent.atomic.AtomicInteger;
5455
import javax.crypto.KeyGenerator;
@@ -198,15 +199,16 @@ public void ensureEncryptedAttributesUnmodified() throws GeneralSecurityExceptio
198199
Map<String, AttributeValue> encryptedAttributes =
199200
encryptor.encryptAllFieldsExcept(
200201
Collections.unmodifiableMap(attribs), context, "hashKey", "rangeKey", "version");
201-
String encryptedString = encryptedAttributes.toString();
202+
// Using TreeMap before casting to string to avoid nondeterministic key orders.
203+
String encryptedString = new TreeMap<>(encryptedAttributes).toString();
202204
encryptor.decryptAllFieldsExcept(
203205
Collections.unmodifiableMap(encryptedAttributes),
204206
context,
205207
"hashKey",
206208
"rangeKey",
207209
"version");
208210

209-
assertEquals(encryptedString, encryptedAttributes.toString());
211+
assertEquals(encryptedString, new TreeMap<>(encryptedAttributes).toString());
210212
}
211213

212214
@Test(expectedExceptions = SignatureException.class)

sdk1/src/test/java/com/amazonaws/services/dynamodbv2/datamodeling/internal/AttributeValueMarshallerTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,13 @@ public void testSimpleMapWithNull() {
266266
marshall(av);
267267
Assert.fail("Unexpected success");
268268
} catch (final NullPointerException npe) {
269-
Assert.assertEquals(
270-
"Encountered null map value for key NullKeyValue while marshalling attribute value {M: {KeyValue={S: ValueValue,}, NullKeyValue=null},}",
271-
npe.getMessage());
269+
// Map entries may permute under nondeterministic Java API
270+
String npeMessage = npe.getMessage();
271+
String common =
272+
"Encountered null map value for key NullKeyValue while marshalling attribute value ";
273+
String case1 = common + "{M: {KeyValue={S: ValueValue,}, NullKeyValue=null},}";
274+
String case2 = common + "{M: {NullKeyValue=null, KeyValue={S: ValueValue,}},}";
275+
Assert.assertTrue(case1.equals(npeMessage) || case2.equals(npeMessage));
272276
}
273277
}
274278

0 commit comments

Comments
 (0)