From daa854b7b9cfa88591371dacf53ebd2d56ee18d2 Mon Sep 17 00:00:00 2001 From: John Walker Date: Fri, 7 Dec 2018 13:00:23 -0800 Subject: [PATCH 1/2] Refactor tests to use hamcrest matchers Hamcrest matchers are a more generic alternative to assertNotEquals. They are a little easier to maintain, since hamcrest just provides matchers. --- .../encryption/DynamoDBEncryptorTest.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/test/java/com/amazonaws/services/dynamodbv2/datamodeling/encryption/DynamoDBEncryptorTest.java b/src/test/java/com/amazonaws/services/dynamodbv2/datamodeling/encryption/DynamoDBEncryptorTest.java index 7d193304..4a33d36a 100644 --- a/src/test/java/com/amazonaws/services/dynamodbv2/datamodeling/encryption/DynamoDBEncryptorTest.java +++ b/src/test/java/com/amazonaws/services/dynamodbv2/datamodeling/encryption/DynamoDBEncryptorTest.java @@ -15,9 +15,10 @@ package com.amazonaws.services.dynamodbv2.datamodeling.encryption; import static com.amazonaws.services.dynamodbv2.datamodeling.encryption.utils.EncryptionContextOperators.overrideEncryptionContextTableName; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; @@ -69,7 +70,8 @@ public class DynamoDBEncryptorTest { private DynamoDBEncryptor encryptor; private Map attribs; private EncryptionContext context; - + private static final String OVERRIDDEN_TABLE_NAME = "TheBestTableName"; + @BeforeClass public static void setUpClass() throws Exception { KeyGenerator aesGen = KeyGenerator.getInstance("AES"); @@ -316,9 +318,9 @@ public void testNullEncryptionContextOperator() throws GeneralSecurityException @Test public void testTableNameOverriddenEncryptionContextOperator() throws GeneralSecurityException { // Ensure that the table name is different from what we override the table to. - assertNotEquals(context.getTableName(), "TheBestTableName"); + assertThat(context.getTableName(), not(equalTo(OVERRIDDEN_TABLE_NAME))); DynamoDBEncryptor encryptor = DynamoDBEncryptor.getInstance(prov); - encryptor.setEncryptionContextOverrideOperator(overrideEncryptionContextTableName(context.getTableName(), "TheBestTableName")); + encryptor.setEncryptionContextOverrideOperator(overrideEncryptionContextTableName(context.getTableName(), OVERRIDDEN_TABLE_NAME)); Map encryptedItems = encryptor.encryptAllFieldsExcept(attribs, context, Collections.emptyList()); Map decryptedItems = encryptor.decryptAllFieldsExcept(encryptedItems, context, Collections.emptyList()); assertThat(decryptedItems, AttrMatcher.match(attribs)); @@ -332,10 +334,10 @@ public void testTableNameOverriddenEncryptionContextOperator() throws GeneralSec @Test public void testTableNameOverriddenEncryptionContextOperatorWithSecondEncryptor() throws GeneralSecurityException { // Ensure that the table name is different from what we override the table to. - assertNotEquals(context.getTableName(), "TheBestTableName"); + assertThat(context.getTableName(), not(equalTo(OVERRIDDEN_TABLE_NAME))); DynamoDBEncryptor encryptor = DynamoDBEncryptor.getInstance(prov); DynamoDBEncryptor encryptorWithoutOverride = DynamoDBEncryptor.getInstance(prov); - encryptor.setEncryptionContextOverrideOperator(overrideEncryptionContextTableName(context.getTableName(), "TheBestTableName")); + encryptor.setEncryptionContextOverrideOperator(overrideEncryptionContextTableName(context.getTableName(), OVERRIDDEN_TABLE_NAME)); Map encryptedItems = encryptor.encryptAllFieldsExcept(attribs, context, Collections.emptyList()); EncryptionContext expectedOverriddenContext = new EncryptionContext.Builder(context).withTableName("TheBestTableName").build(); @@ -351,10 +353,10 @@ public void testTableNameOverriddenEncryptionContextOperatorWithSecondEncryptor( @Test(expected = SignatureException.class) public void testTableNameOverriddenEncryptionContextOperatorWithSecondEncryptorButTheOriginalEncryptionContext() throws GeneralSecurityException { // Ensure that the table name is different from what we override the table to. - assertNotEquals(context.getTableName(), "TheBestTableName"); + assertThat(context.getTableName(), not(equalTo(OVERRIDDEN_TABLE_NAME))); DynamoDBEncryptor encryptor = DynamoDBEncryptor.getInstance(prov); DynamoDBEncryptor encryptorWithoutOverride = DynamoDBEncryptor.getInstance(prov); - encryptor.setEncryptionContextOverrideOperator(overrideEncryptionContextTableName(context.getTableName(), "TheBestTableName")); + encryptor.setEncryptionContextOverrideOperator(overrideEncryptionContextTableName(context.getTableName(), OVERRIDDEN_TABLE_NAME)); Map encryptedItems = encryptor.encryptAllFieldsExcept(attribs, context, Collections.emptyList()); // Use the original encryption context, and expect a signature failure From 244c417ca4d0055c2e0b8ceac633410eb1bf7e9d Mon Sep 17 00:00:00 2001 From: John Walker Date: Fri, 7 Dec 2018 13:11:32 -0800 Subject: [PATCH 2/2] Add hamcrest-all to test dependencies Add a dependency on hamcrest-all to make unit testing easier. --- pom.xml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pom.xml b/pom.xml index 34947992..23a98c57 100644 --- a/pom.xml +++ b/pom.xml @@ -68,6 +68,13 @@ test + + org.hamcrest + hamcrest-all + 1.3 + test + + org.bouncycastle bcprov-ext-jdk15on