|
| 1 | +/** |
| 2 | + * Copyright © 2013 spring-data-dynamodb (https://github.com/derjust/spring-data-dynamodb) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.socialsignin.spring.data.dynamodb.utils; |
| 17 | + |
| 18 | +import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; |
| 19 | +import com.amazonaws.services.dynamodbv2.local.embedded.DynamoDBEmbedded; |
| 20 | +import com.amazonaws.services.dynamodbv2.model.AttributeDefinition; |
| 21 | +import com.amazonaws.services.dynamodbv2.model.CreateTableRequest; |
| 22 | +import com.amazonaws.services.dynamodbv2.model.CreateTableResult; |
| 23 | +import com.amazonaws.services.dynamodbv2.model.KeySchemaElement; |
| 24 | +import com.amazonaws.services.dynamodbv2.model.KeyType; |
| 25 | +import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; |
| 26 | +import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType; |
| 27 | +import org.junit.rules.ExternalResource; |
| 28 | +import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBEntityMetadataSupport; |
| 29 | +import org.springframework.context.annotation.Bean; |
| 30 | +import org.springframework.context.annotation.Configuration; |
| 31 | + |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +@Configuration |
| 36 | +public class DynamoDBLocalResource extends ExternalResource { |
| 37 | + |
| 38 | + private AmazonDynamoDB ddb; |
| 39 | + |
| 40 | + @Bean |
| 41 | + public AmazonDynamoDB amazonDynamoDB() { |
| 42 | + ddb = DynamoDBEmbedded.create().amazonDynamoDB(); |
| 43 | + return ddb; |
| 44 | + } |
| 45 | + |
| 46 | + public CreateTableResult createTable(Class<?> domainType) { |
| 47 | + DynamoDBEntityMetadataSupport support = new DynamoDBEntityMetadataSupport(domainType); |
| 48 | + |
| 49 | + String tableName = support.getDynamoDBTableName(); |
| 50 | + String hashKey = support.getHashKeyPropertyName(); |
| 51 | + String rangeKey = support.getHashKeyPropertyName(); |
| 52 | + |
| 53 | + return createTable(tableName, hashKey, rangeKey); |
| 54 | + } |
| 55 | + |
| 56 | + private CreateTableResult createTable(String tableName, String hashKeyName, String rangeKeyName) { |
| 57 | + List<AttributeDefinition> attributeDefinitions = new ArrayList<>(); |
| 58 | + attributeDefinitions.add(new AttributeDefinition(hashKeyName, ScalarAttributeType.S)); |
| 59 | + |
| 60 | + List<KeySchemaElement> ks = new ArrayList<>(); |
| 61 | + ks.add(new KeySchemaElement(hashKeyName, KeyType.HASH)); |
| 62 | + |
| 63 | + if (rangeKeyName != null) { |
| 64 | + attributeDefinitions.add(new AttributeDefinition(rangeKeyName, ScalarAttributeType.S)); |
| 65 | + |
| 66 | + ks.add(new KeySchemaElement(rangeKeyName, KeyType.RANGE)); |
| 67 | + } |
| 68 | + |
| 69 | + ProvisionedThroughput provisionedthroughput = new ProvisionedThroughput(10L, 10L); |
| 70 | + |
| 71 | + CreateTableRequest request = |
| 72 | + new CreateTableRequest() |
| 73 | + .withTableName(tableName) |
| 74 | + .withAttributeDefinitions(attributeDefinitions) |
| 75 | + .withKeySchema(ks) |
| 76 | + .withProvisionedThroughput(provisionedthroughput); |
| 77 | + |
| 78 | + return ddb.createTable(request); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + protected void after() { |
| 83 | + ddb.shutdown(); |
| 84 | + }; |
| 85 | +} |
0 commit comments