Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Merge a2f70c5 into 828e1f0
Browse files Browse the repository at this point in the history
  • Loading branch information
msgroi committed Sep 19, 2018
2 parents 828e1f0 + a2f70c5 commit d8b4982
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@
* hash tables, only hash and range tables"). Therefore, the only table that has HK only does not have an LSI.
* - Virtual tables with only a HK may not be mapped to a table that has both a HK and RK per
* https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html, "When you add an item, the primary
* key attribute(s) are the only required attributes."
* key attribute(s) are the only required attributes. Attribute values cannot be null." See
* {@link SharedTableRangeKeyTest} for a simple test that demonstrates this.
* - All virtual types could have been mapped into a set of tables that have only byte array types, and convert
* all virtual types down to byte arrays and back. This would necessitate a smaller set of tables, possibly as few
* as 3. However, the mapping layer would also need to be responsible for maintaining consistency with respect to
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.salesforce.dynamodbv2.mt.mappers.sharedtable;

import static com.amazonaws.services.dynamodbv2.model.ScalarAttributeType.S;
import static com.salesforce.dynamodbv2.testsupport.ItemBuilder.HASH_KEY_FIELD;
import static com.salesforce.dynamodbv2.testsupport.ItemBuilder.RANGE_KEY_FIELD;
import static com.salesforce.dynamodbv2.testsupport.TestSupport.getPollInterval;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import com.amazonaws.services.dynamodbv2.model.KeyType;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import com.amazonaws.services.dynamodbv2.model.PutItemRequest;
import com.salesforce.dynamodbv2.dynamodblocal.AmazonDynamoDbLocal;
import com.salesforce.dynamodbv2.testsupport.ItemBuilder;
import com.salesforce.dynamodbv2.testsupport.TestAmazonDynamoDbAdminUtils;
import org.junit.jupiter.api.Test;

/**
* Tests whether it's possible to use a table with a KeySchema that has both a HK and RK for storing data
* for virtual tables that have only a HK by not passing in a RK attribute or a null RK attribute value.
*
* @author msgroi
*/
class SharedTableRangeKeyTest {

private static final String TABLE = "Table";

@Test
void test() {
AmazonDynamoDB amazonDynamoDb = AmazonDynamoDbLocal.getAmazonDynamoDbLocal();

// create table with hashkey and rangekey
new TestAmazonDynamoDbAdminUtils(amazonDynamoDb)
.createTableIfNotExists(new CreateTableRequest()
.withTableName(TABLE)
.withProvisionedThroughput(new ProvisionedThroughput(1L, 1L))
.withAttributeDefinitions(
new AttributeDefinition(HASH_KEY_FIELD, S),
new AttributeDefinition(RANGE_KEY_FIELD, S))
.withKeySchema(
new KeySchemaElement(HASH_KEY_FIELD, KeyType.HASH),
new KeySchemaElement(RANGE_KEY_FIELD, KeyType.RANGE)), getPollInterval());

// insert an item that has no range key attribute
try {
amazonDynamoDb.putItem(new PutItemRequest().withTableName(TABLE)
.withItem(ItemBuilder.builder(S, "hk").build()));
fail("expected exception not encountered");
} catch (AmazonServiceException e) {
assertEquals("One of the required keys was not given a value (Service: null; Status Code: 400; Error "
+ "Code: ValidationException; Request ID: null)", e.getMessage());
}

// insert an item that has a range key attribute with a null value
try {
amazonDynamoDb.putItem(new PutItemRequest().withTableName(TABLE)
.withItem(ItemBuilder.builder(S, "hk").rangeKey(S, null).build()));
fail("expected exception not encountered");
} catch (AmazonServiceException e) {
assertEquals("Supplied AttributeValue is empty, must contain exactly one of the supported datatypes "
+ "(Service: null; Status Code: 400; Error Code: ValidationException; Request ID: null)",
e.getMessage());
}


// insert an item that has a range key attribute with a null value
try {
amazonDynamoDb.putItem(new PutItemRequest().withTableName(TABLE)
.withItem(ItemBuilder.builder(S, "hk").rangeKey(S, "").build()));
fail("expected exception not encountered");
} catch (AmazonServiceException e) {
assertEquals("One or more parameter values were invalid: An AttributeValue may not contain an "
+ "empty string (Service: null; Status Code: 400; Error Code: ValidationException; Request ID: null)",
e.getMessage());
}
}

}

0 comments on commit d8b4982

Please sign in to comment.