|
| 1 | +/* |
| 2 | + * Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except |
| 5 | + * in compliance with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://aws.amazon.com/apache2.0 |
| 8 | + * |
| 9 | + * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | +package com.amazonaws.services.dynamodbv2.mapper.integration; |
| 14 | + |
| 15 | +import static org.testng.Assert.assertEquals; |
| 16 | +import static org.testng.Assert.assertNull; |
| 17 | + |
| 18 | +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute; |
| 19 | +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey; |
| 20 | +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper; |
| 21 | +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable; |
| 22 | +import com.amazonaws.services.dynamodbv2.datamodeling.encryption.DoNotTouch; |
| 23 | +import com.amazonaws.services.dynamodbv2.mapper.encryption.TestDynamoDBMapperFactory; |
| 24 | +import com.amazonaws.services.dynamodbv2.model.AttributeValue; |
| 25 | +import com.amazonaws.services.dynamodbv2.model.PutItemRequest; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.Map; |
| 28 | +import org.testng.annotations.BeforeClass; |
| 29 | +import org.testng.annotations.Test; |
| 30 | + |
| 31 | +public class PlaintextItemITCase extends DynamoDBMapperCryptoIntegrationTestBase { |
| 32 | + private static final String STRING_ATTRIBUTE = "stringAttribute"; |
| 33 | + private static Map<String, AttributeValue> plaintextItem = new HashMap<>(); |
| 34 | + // Test data |
| 35 | + static { |
| 36 | + plaintextItem.put(KEY_NAME, new AttributeValue().withS("" + startKey++)); |
| 37 | + plaintextItem.put(STRING_ATTRIBUTE, new AttributeValue().withS("" + startKey++)); |
| 38 | + } |
| 39 | + |
| 40 | + @BeforeClass |
| 41 | + public static void setUp() throws Exception { |
| 42 | + DynamoDBMapperCryptoIntegrationTestBase.setUp(); |
| 43 | + // Insert the data |
| 44 | + dynamo.putItem(new PutItemRequest(TABLE_NAME, plaintextItem)); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testLoadWithPlaintextItem() { |
| 49 | + DynamoDBMapper util = TestDynamoDBMapperFactory.createDynamoDBMapper(dynamo); |
| 50 | + UntouchedTable load = util.load(UntouchedTable.class, plaintextItem.get(KEY_NAME).getS()); |
| 51 | + |
| 52 | + assertEquals(load.getKey(), plaintextItem.get(KEY_NAME).getS()); |
| 53 | + assertEquals(load.getStringAttribute(), plaintextItem.get(STRING_ATTRIBUTE).getS()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testLoadWithPlaintextItemWithModelHavingNewEncryptedAttribute() { |
| 58 | + DynamoDBMapper util = TestDynamoDBMapperFactory.createDynamoDBMapper(dynamo); |
| 59 | + UntouchedWithNewEncryptedAttributeTable load = |
| 60 | + util.load( |
| 61 | + UntouchedWithNewEncryptedAttributeTable.class, plaintextItem.get(KEY_NAME).getS()); |
| 62 | + |
| 63 | + assertEquals(load.getKey(), plaintextItem.get(KEY_NAME).getS()); |
| 64 | + assertEquals(load.getStringAttribute(), plaintextItem.get(STRING_ATTRIBUTE).getS()); |
| 65 | + assertNull(load.getNewAttribute()); |
| 66 | + } |
| 67 | + |
| 68 | + @DynamoDBTable(tableName = "aws-java-sdk-util-crypto") |
| 69 | + public static class UntouchedTable { |
| 70 | + |
| 71 | + private String key; |
| 72 | + |
| 73 | + private String stringAttribute; |
| 74 | + |
| 75 | + @DynamoDBHashKey |
| 76 | + @DoNotTouch |
| 77 | + public String getKey() { |
| 78 | + return key; |
| 79 | + } |
| 80 | + |
| 81 | + public void setKey(String key) { |
| 82 | + this.key = key; |
| 83 | + } |
| 84 | + |
| 85 | + @DynamoDBAttribute |
| 86 | + @DoNotTouch |
| 87 | + public String getStringAttribute() { |
| 88 | + return stringAttribute; |
| 89 | + } |
| 90 | + |
| 91 | + public void setStringAttribute(String stringAttribute) { |
| 92 | + this.stringAttribute = stringAttribute; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean equals(Object o) { |
| 97 | + if (this == o) return true; |
| 98 | + if (o == null || getClass() != o.getClass()) return false; |
| 99 | + UntouchedTable that = (UntouchedTable) o; |
| 100 | + return key.equals(that.key) && stringAttribute.equals(that.stringAttribute); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public static final class UntouchedWithNewEncryptedAttributeTable extends UntouchedTable { |
| 105 | + private String newAttribute; |
| 106 | + |
| 107 | + public String getNewAttribute() { |
| 108 | + return newAttribute; |
| 109 | + } |
| 110 | + |
| 111 | + public void setNewAttribute(String newAttribute) { |
| 112 | + this.newAttribute = newAttribute; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments