Skip to content

fix: Account for indeterminate Map ordering in unit tests #274

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 16 commits into from
Feb 10, 2022
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import javax.crypto.KeyGenerator;
Expand Down Expand Up @@ -198,15 +199,16 @@ public void ensureEncryptedAttributesUnmodified() throws GeneralSecurityExceptio
Map<String, AttributeValue> encryptedAttributes =
encryptor.encryptAllFieldsExcept(
Collections.unmodifiableMap(attribs), context, "hashKey", "rangeKey", "version");
String encryptedString = encryptedAttributes.toString();
// Using TreeMap before casting to string to avoid nondeterministic key orders.
String encryptedString = new TreeMap<>(encryptedAttributes).toString();
encryptor.decryptAllFieldsExcept(
Collections.unmodifiableMap(encryptedAttributes),
context,
"hashKey",
"rangeKey",
"version");

assertEquals(encryptedString, encryptedAttributes.toString());
assertEquals(encryptedString, new TreeMap<>(encryptedAttributes).toString());
}

@Test(expectedExceptions = SignatureException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,13 @@ public void testSimpleMapWithNull() {
marshall(av);
Assert.fail("Unexpected success");
} catch (final NullPointerException npe) {
Assert.assertEquals(
"Encountered null map value for key NullKeyValue while marshalling attribute value {M: {KeyValue={S: ValueValue,}, NullKeyValue=null},}",
npe.getMessage());
// Map entries may permute under nondeterministic Java API
String npeMessage = npe.getMessage();
String common =
"Encountered null map value for key NullKeyValue while marshalling attribute value ";
String case1 = common + "{M: {KeyValue={S: ValueValue,}, NullKeyValue=null},}";
String case2 = common + "{M: {NullKeyValue=null, KeyValue={S: ValueValue,}},}";
Assert.assertTrue(case1.equals(npeMessage) || case2.equals(npeMessage));
}
}

Expand Down