2525import com .amazonaws .services .dynamodbv2 .model .ProvisionedThroughput ;
2626import com .amazonaws .services .dynamodbv2 .model .ScalarAttributeType ;
2727import org .junit .rules .ExternalResource ;
28+ import org .socialsignin .spring .data .dynamodb .repository .support .DynamoDBEntityInformation ;
2829import org .socialsignin .spring .data .dynamodb .repository .support .DynamoDBEntityMetadataSupport ;
30+ import org .socialsignin .spring .data .dynamodb .repository .support .DynamoDBIdIsHashAndRangeKeyEntityInformation ;
2931import org .springframework .context .annotation .Bean ;
3032import org .springframework .context .annotation .Configuration ;
3133
34+ import javax .swing .text .html .Option ;
3235import java .util .ArrayList ;
3336import java .util .List ;
37+ import java .util .Optional ;
3438
3539@ Configuration
3640public class DynamoDBLocalResource extends ExternalResource {
@@ -43,27 +47,31 @@ public AmazonDynamoDB amazonDynamoDB() {
4347 return ddb ;
4448 }
4549
46- public CreateTableResult createTable (Class <?> domainType ) {
50+ public static CreateTableResult createTable (AmazonDynamoDB ddb , Class <?> domainType ) {
4751 DynamoDBEntityMetadataSupport support = new DynamoDBEntityMetadataSupport (domainType );
52+ DynamoDBEntityInformation entityInfo = support .getEntityInformation ();
4853
49- String tableName = support .getDynamoDBTableName ();
50- String hashKey = support .getHashKeyPropertyName ();
51- String rangeKey = support .getHashKeyPropertyName ();
54+ String tableName = entityInfo .getDynamoDBTableName ();
55+ String hashKey = entityInfo .getHashKeyPropertyName ();
56+ Optional <String > rangeKey = Optional .empty ();
57+ if (entityInfo instanceof DynamoDBIdIsHashAndRangeKeyEntityInformation ) {
58+ rangeKey = Optional .of (((DynamoDBIdIsHashAndRangeKeyEntityInformation )entityInfo ).getRangeKeyPropertyName ());
59+ }
5260
53- return createTable (tableName , hashKey , rangeKey );
61+ return createTable (ddb , tableName , hashKey , rangeKey );
5462 }
5563
56- private CreateTableResult createTable (String tableName , String hashKeyName , String rangeKeyName ) {
64+ private static CreateTableResult createTable (AmazonDynamoDB ddb , String tableName , String hashKeyName , Optional < String > rangeKeyName ) {
5765 List <AttributeDefinition > attributeDefinitions = new ArrayList <>();
5866 attributeDefinitions .add (new AttributeDefinition (hashKeyName , ScalarAttributeType .S ));
5967
6068 List <KeySchemaElement > ks = new ArrayList <>();
6169 ks .add (new KeySchemaElement (hashKeyName , KeyType .HASH ));
6270
63- if (rangeKeyName != null ) {
64- attributeDefinitions .add (new AttributeDefinition (rangeKeyName , ScalarAttributeType .S ));
71+ if (rangeKeyName . isPresent () ) {
72+ attributeDefinitions .add (new AttributeDefinition (rangeKeyName . get () , ScalarAttributeType .S ));
6573
66- ks .add (new KeySchemaElement (rangeKeyName , KeyType .RANGE ));
74+ ks .add (new KeySchemaElement (rangeKeyName . get () , KeyType .RANGE ));
6775 }
6876
6977 ProvisionedThroughput provisionedthroughput = new ProvisionedThroughput (10L , 10L );
0 commit comments