|
| 1 | +package org.socialsignin.spring.data.dynamodb.domain.sample; |
| 2 | + |
| 3 | +import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; |
| 4 | +import com.amazonaws.services.dynamodbv2.model.AttributeDefinition; |
| 5 | +import com.amazonaws.services.dynamodbv2.model.CreateTableRequest; |
| 6 | +import com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex; |
| 7 | +import com.amazonaws.services.dynamodbv2.model.KeySchemaElement; |
| 8 | +import com.amazonaws.services.dynamodbv2.model.KeyType; |
| 9 | +import com.amazonaws.services.dynamodbv2.model.Projection; |
| 10 | +import com.amazonaws.services.dynamodbv2.model.ProjectionType; |
| 11 | +import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; |
| 12 | +import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType; |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.Test; |
| 15 | +import org.junit.runner.RunWith; |
| 16 | +import org.socialsignin.spring.data.dynamodb.repository.config.EnableDynamoDBRepositories; |
| 17 | +import org.socialsignin.spring.data.dynamodb.utils.DynamoDBLocalResource; |
| 18 | +import org.springframework.beans.factory.annotation.Autowired; |
| 19 | +import org.springframework.context.annotation.Configuration; |
| 20 | +import org.springframework.test.context.ContextConfiguration; |
| 21 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.util.UUID; |
| 26 | + |
| 27 | +import static org.junit.Assert.assertEquals; |
| 28 | + |
| 29 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 30 | +@ContextConfiguration(classes = {DynamoDBLocalResource.class, DuplicateColumnUsageTest.DuplicateColumnUsageTestConfig.class}) |
| 31 | +public class DuplicateColumnUsageTest { |
| 32 | + |
| 33 | + |
| 34 | + @Configuration |
| 35 | + @EnableDynamoDBRepositories(basePackages = "org.socialsignin.spring.data.dynamodb.domain.sample") |
| 36 | + static class DuplicateColumnUsageTestConfig { |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setUp() { |
| 43 | + // DynamoDBLocalResource.createTable(ddb, Playlist.class); |
| 44 | + List<AttributeDefinition> ad = new ArrayList<>(); |
| 45 | + ad.add(new AttributeDefinition("Genre", ScalarAttributeType.S)); |
| 46 | + ad.add(new AttributeDefinition("PlaylistName", ScalarAttributeType.S)); |
| 47 | + ad.add(new AttributeDefinition("UserName", ScalarAttributeType.S)); |
| 48 | + |
| 49 | + List<KeySchemaElement> ks = new ArrayList<>(); |
| 50 | + ks.add(new KeySchemaElement("UserName", KeyType.HASH)); |
| 51 | + ks.add(new KeySchemaElement("PlaylistName", KeyType.RANGE)); |
| 52 | + |
| 53 | + List<KeySchemaElement> gsiks = new ArrayList<>(); |
| 54 | + gsiks.add(new KeySchemaElement("Genre", KeyType.HASH)); |
| 55 | + gsiks.add(new KeySchemaElement("PlaylistName", KeyType.RANGE)); |
| 56 | + |
| 57 | + List<GlobalSecondaryIndex> gsis = new ArrayList<>(); |
| 58 | + gsis.add(new GlobalSecondaryIndex() |
| 59 | + .withIndexName("Genre-PlaylistName-index") |
| 60 | + .withKeySchema(gsiks) |
| 61 | + .withProjection(new Projection().withProjectionType(ProjectionType.ALL)) |
| 62 | + .withProvisionedThroughput(new ProvisionedThroughput(10L, 10L))); |
| 63 | + |
| 64 | + CreateTableRequest ctr = new CreateTableRequest(); |
| 65 | + ctr.withTableName("playlist"); |
| 66 | + ctr.withAttributeDefinitions(ad); |
| 67 | + ctr.withKeySchema(ks); |
| 68 | + ctr.withGlobalSecondaryIndexes(gsis); |
| 69 | + ctr.withProvisionedThroughput(new ProvisionedThroughput(10L, 10L)); |
| 70 | + |
| 71 | + ddb.createTable(ctr); |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + @Autowired |
| 76 | + private PlaylistRepository playlistRepository; |
| 77 | + @Autowired |
| 78 | + private AmazonDynamoDB ddb; |
| 79 | + |
| 80 | + |
| 81 | + private Playlist generatePlaylist(String genre, String playlistName) { |
| 82 | + final String userName = "userName-" + UUID.randomUUID().toString(); |
| 83 | + final String displayName = "displayName-" + UUID.randomUUID().toString(); |
| 84 | + PlaylistId id = new PlaylistId(userName, playlistName); |
| 85 | + |
| 86 | + Playlist playlist = new Playlist(id); |
| 87 | + playlist.setDisplayName(displayName); |
| 88 | + playlist.setGenre(genre); |
| 89 | + |
| 90 | + return playlist; |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testGsiSharedRange() { |
| 95 | + final String GENRE = "GENRE"; |
| 96 | + playlistRepository.save(generatePlaylist(GENRE, "1")); |
| 97 | + playlistRepository.save(generatePlaylist(GENRE, "2")); |
| 98 | + playlistRepository.save(generatePlaylist(GENRE, "3")); |
| 99 | + try { |
| 100 | + Thread.sleep(2000L); |
| 101 | + } catch (InterruptedException e) { |
| 102 | + e.printStackTrace(); |
| 103 | + } |
| 104 | + |
| 105 | + assertEquals(3, playlistRepository.findByGenre(GENRE).size()); |
| 106 | + |
| 107 | + assertEquals(1, playlistRepository.findByGenreAndPlaylistName(GENRE, "2").size()); |
| 108 | + |
| 109 | + List<Playlist> actual = playlistRepository.findByGenreAndPlaylistNameBetween(GENRE, "2", "3"); |
| 110 | + |
| 111 | + assertEquals(2, actual.size()); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments