Skip to content

Commit

Permalink
Merge pull request #10 from wonwoo/mapping
Browse files Browse the repository at this point in the history
#6 createTable id not mapping
  • Loading branch information
wonwoo committed Mar 2, 2018
2 parents e47217c + bd8eec6 commit 4470ee0
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.socialsignin.spring.data.dynamodb.mapping.DynamoDBMappingContext;
import org.socialsignin.spring.data.dynamodb.mapping.DynamoDBPersistentEntity;
import org.socialsignin.spring.data.dynamodb.mapping.DynamoDBPersistentEntityImpl;
import org.socialsignin.spring.data.dynamodb.mapping.DynamoDBPersistentProperty;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.StringUtils;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import com.amazonaws.services.dynamodbv2.model.*;
import com.amazonaws.services.dynamodbv2.model.CreateTableResult;

/**
* @author wonwoo
Expand Down Expand Up @@ -84,8 +84,8 @@ public List<CreateTableResult> createTable() {
}
DynamoDBTable table = findMergedAnnotation(entity.getTypeInformation().getType(), DynamoDBTable.class);
if (!createTable.isTable(table.tableName())) {
CreateTableResult createTableTable = createTable.createTable(table.tableName(), idProperty.getName());
if(!ACTIVE.equals(createTableTable.getTableDescription().getTableStatus()) ) {
CreateTableResult createTableTable = createTable.createTable(table.tableName(), getIdProperty(idProperty));
if(!ACTIVE.equals(createTableTable.getTableDescription().getTableStatus())) {
createTable.waitTableExists(table.tableName(), this.secondsBetweenPolls, this.timeoutSeconds);
}
results.add(createTableTable);
Expand All @@ -94,6 +94,16 @@ public List<CreateTableResult> createTable() {
return results;
}

private String getIdProperty(DynamoDBPersistentProperty idProperty) {
DynamoDBHashKey dynamoDBHashKey = idProperty.findAnnotation(DynamoDBHashKey.class);
String attributeName = dynamoDBHashKey.attributeName();
if(StringUtils.hasText(attributeName)) {
return attributeName;
}
return idProperty.getName();
}


private <A extends Annotation> A findMergedAnnotation(AnnotatedElement element, Class<A> annotationType) {
return AnnotatedElementUtils.findMergedAnnotation(element, annotationType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@

package com.github.wonwoo.dynamodb;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import com.amazonaws.services.dynamodbv2.model.CreateTableResult;
import com.amazonaws.services.dynamodbv2.model.ListTablesResult;
import com.amazonaws.services.dynamodbv2.model.TableDescription;
import com.amazonaws.services.dynamodbv2.model.TableStatus;
import com.github.wonwoo.dynamodb.autoconfigure.CreateTable;
import com.github.wonwoo.dynamodb.autoconfigure.DynamoDbMapping;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -35,10 +31,14 @@
import org.socialsignin.spring.data.dynamodb.mapping.DynamoDBPersistentProperty;
import org.springframework.data.util.TypeInformation;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import com.amazonaws.services.dynamodbv2.model.CreateTableResult;
import com.amazonaws.services.dynamodbv2.model.ListTablesResult;
import com.amazonaws.services.dynamodbv2.model.TableDescription;
import com.amazonaws.services.dynamodbv2.model.TableStatus;
import com.github.wonwoo.dynamodb.autoconfigure.CreateTable;
import com.github.wonwoo.dynamodb.autoconfigure.DynamoDbMapping;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -65,6 +65,7 @@ public void setup() {
context = new DynamoDBMappingContext();
HashSet<Class<?>> initialEntitySet = new HashSet<>();
initialEntitySet.add(Person.class);
initialEntitySet.add(Foo.class);
context.setInitialEntitySet(initialEntitySet);
context.afterPropertiesSet();
this.dynamoDbMapping = new DynamoDbMapping(createTable, context);
Expand All @@ -75,28 +76,57 @@ public void setup() {
@Test
public void getPersistentEntities() {
Collection<DynamoDBPersistentEntityImpl<?>> entities = this.dynamoDbMapping.getPersistentEntities();
assertThat(entities).hasSize(1);
assertThat(entities).hasSize(2);
}

@Test
public void getPersistentEntity() {
public void getPersistentEntityPerson() {
DynamoDBPersistentEntityImpl<?> entity = this.dynamoDbMapping.getPersistentEntity(Person.class);
DynamoDBPersistentProperty idProperty = entity.getIdProperty();
DynamoDBHashKey dynamoDBHashKey = idProperty.getRequiredAnnotation(DynamoDBHashKey.class);
assertThat(dynamoDBHashKey.attributeName()).isEmpty();
assertThat(idProperty.getActualType()).isEqualTo(String.class);
}

@Test
public void getIdProperty() {
public void getPersistentEntityFoo() {
DynamoDBPersistentEntityImpl<?> entity = this.dynamoDbMapping.getPersistentEntity(Foo.class);
DynamoDBPersistentProperty idProperty = entity.getIdProperty();
DynamoDBHashKey dynamoDBHashKey = idProperty.getRequiredAnnotation(DynamoDBHashKey.class);
assertThat(dynamoDBHashKey.attributeName()).isNotBlank();
assertThat(dynamoDBHashKey.attributeName()).isEqualTo("uuid");
assertThat(idProperty.getActualType()).isEqualTo(String.class);
}

@Test
public void getIdPropertyPerson() {
DynamoDBPersistentProperty idProperty = this.dynamoDbMapping.getIdProperty(Person.class);
DynamoDBHashKey dynamoDBHashKey = idProperty.getRequiredAnnotation(DynamoDBHashKey.class);
assertThat(dynamoDBHashKey.attributeName()).isEmpty();
assertThat(idProperty.getActualType()).isEqualTo(String.class);
}

@Test
public void getIdPropertyFoo() {
DynamoDBPersistentProperty idProperty = this.dynamoDbMapping.getIdProperty(Foo.class);
DynamoDBHashKey dynamoDBHashKey = idProperty.getRequiredAnnotation(DynamoDBHashKey.class);
assertThat(dynamoDBHashKey.attributeName()).isNotBlank();
assertThat(dynamoDBHashKey.attributeName()).isEqualTo("uuid");
assertThat(idProperty.getActualType()).isEqualTo(String.class);
}

@Test
public void getTypeInformation() {
public void getTypeInformationPerson() {
TypeInformation<?> typeInformation = this.dynamoDbMapping.getTypeInformation(Person.class);
assertThat(typeInformation.getType()).isEqualTo(Person.class);
}

@Test
public void getTypeInformationFoo() {
TypeInformation<?> typeInformation = this.dynamoDbMapping.getTypeInformation(Foo.class);
assertThat(typeInformation.getType()).isEqualTo(Foo.class);
}

@Test
public void createTable() {
CreateTableResult value = new CreateTableResult();
Expand All @@ -111,9 +141,25 @@ public void createTable() {
given(createTable.createTable(any(), any())).willReturn(value);
given(createTable.waitTableExists(any(), anyLong(), anyLong())).willReturn(true);
List<CreateTableResult> table = this.dynamoDbMapping.createTable();
assertThat(table).hasSize(1);
assertThat(table).hasSize(2);
assertThat(table.iterator().next().getTableDescription()).isEqualTo(tableDescription);
assertThat(table.iterator().next().getTableDescription()).isEqualTo(tableDescription);
verify(createTable).waitTableExists("persons", 10, 30);
verify(createTable).waitTableExists("foo", 10, 30);
}

@DynamoDBTable(tableName = "foo")
private static class Foo {
@DynamoDBHashKey(attributeName = "uuid")
private String id;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}
}

@DynamoDBTable(tableName = "persons")
Expand Down

0 comments on commit 4470ee0

Please sign in to comment.