25
25
import com .amazonaws .services .dynamodbv2 .model .ProvisionedThroughput ;
26
26
import com .amazonaws .services .dynamodbv2 .model .ScalarAttributeType ;
27
27
import org .junit .rules .ExternalResource ;
28
+ import org .socialsignin .spring .data .dynamodb .repository .support .DynamoDBEntityInformation ;
28
29
import org .socialsignin .spring .data .dynamodb .repository .support .DynamoDBEntityMetadataSupport ;
30
+ import org .socialsignin .spring .data .dynamodb .repository .support .DynamoDBIdIsHashAndRangeKeyEntityInformation ;
29
31
import org .springframework .context .annotation .Bean ;
30
32
import org .springframework .context .annotation .Configuration ;
31
33
34
+ import javax .swing .text .html .Option ;
32
35
import java .util .ArrayList ;
33
36
import java .util .List ;
37
+ import java .util .Optional ;
34
38
35
39
@ Configuration
36
40
public class DynamoDBLocalResource extends ExternalResource {
@@ -43,27 +47,31 @@ public AmazonDynamoDB amazonDynamoDB() {
43
47
return ddb ;
44
48
}
45
49
46
- public CreateTableResult createTable (Class <?> domainType ) {
50
+ public static CreateTableResult createTable (AmazonDynamoDB ddb , Class <?> domainType ) {
47
51
DynamoDBEntityMetadataSupport support = new DynamoDBEntityMetadataSupport (domainType );
52
+ DynamoDBEntityInformation entityInfo = support .getEntityInformation ();
48
53
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
+ }
52
60
53
- return createTable (tableName , hashKey , rangeKey );
61
+ return createTable (ddb , tableName , hashKey , rangeKey );
54
62
}
55
63
56
- private CreateTableResult createTable (String tableName , String hashKeyName , String rangeKeyName ) {
64
+ private static CreateTableResult createTable (AmazonDynamoDB ddb , String tableName , String hashKeyName , Optional < String > rangeKeyName ) {
57
65
List <AttributeDefinition > attributeDefinitions = new ArrayList <>();
58
66
attributeDefinitions .add (new AttributeDefinition (hashKeyName , ScalarAttributeType .S ));
59
67
60
68
List <KeySchemaElement > ks = new ArrayList <>();
61
69
ks .add (new KeySchemaElement (hashKeyName , KeyType .HASH ));
62
70
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 ));
65
73
66
- ks .add (new KeySchemaElement (rangeKeyName , KeyType .RANGE ));
74
+ ks .add (new KeySchemaElement (rangeKeyName . get () , KeyType .RANGE ));
67
75
}
68
76
69
77
ProvisionedThroughput provisionedthroughput = new ProvisionedThroughput (10L , 10L );
0 commit comments