|
| 1 | +package org.jnosql.diana.dynamodb.key; |
| 2 | + |
| 3 | +import static java.util.Arrays.asList; |
| 4 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 5 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 10 | + |
| 11 | +import java.util.List; |
| 12 | +import java.util.Optional; |
| 13 | +import java.util.concurrent.ExecutorService; |
| 14 | +import java.util.concurrent.Executors; |
| 15 | +import java.util.concurrent.ThreadLocalRandom; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | +import java.util.stream.Collectors; |
| 18 | +import java.util.stream.StreamSupport; |
| 19 | + |
| 20 | +import org.jnosql.diana.api.Value; |
| 21 | +import org.jnosql.diana.api.key.BucketManager; |
| 22 | +import org.jnosql.diana.api.key.BucketManagerFactory; |
| 23 | +import org.jnosql.diana.api.key.KeyValueEntity; |
| 24 | +import org.jnosql.diana.dynamodb.DynamoDBTestUtils; |
| 25 | +import org.junit.AfterClass; |
| 26 | +import org.junit.jupiter.api.AfterAll; |
| 27 | +import org.junit.jupiter.api.BeforeAll; |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +public class DynamoDBKeyValueEntityManagerTest { |
| 32 | + |
| 33 | + private BucketManager keyValueEntityManager; |
| 34 | + |
| 35 | + private BucketManagerFactory keyValueEntityManagerFactory; |
| 36 | + |
| 37 | + private DynamoDBKeyValueConfiguration configuration; |
| 38 | + |
| 39 | + private User userOtavio = new User("otavio"); |
| 40 | + private KeyValueEntity keyValueOtavio = KeyValueEntity.of("otavio", Value.of(userOtavio)); |
| 41 | + |
| 42 | + private User userSoro = new User("soro"); |
| 43 | + private KeyValueEntity keyValueSoro = KeyValueEntity.of("soro", Value.of(userSoro)); |
| 44 | + |
| 45 | + @BeforeEach |
| 46 | + public void init() { |
| 47 | + keyValueEntityManagerFactory = DynamoDBTestUtils.get(); |
| 48 | + keyValueEntityManager = keyValueEntityManagerFactory.getBucketManager("users-entity"); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + @Test |
| 53 | + public void shouldPutValue() { |
| 54 | + keyValueEntityManager.put("otavio", userOtavio); |
| 55 | + Optional<Value> otavio = keyValueEntityManager.get("otavio"); |
| 56 | + assertTrue(otavio.isPresent()); |
| 57 | + assertEquals(userOtavio, otavio.get().get(User.class)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void shouldPutKeyValue() { |
| 62 | + keyValueEntityManager.put(keyValueOtavio); |
| 63 | + Optional<Value> otavio = keyValueEntityManager.get("otavio"); |
| 64 | + assertTrue(otavio.isPresent()); |
| 65 | + assertEquals(userOtavio, otavio.get().get(User.class)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void shouldPutIterableKeyValue() { |
| 70 | + |
| 71 | + |
| 72 | + keyValueEntityManager.put(asList(keyValueSoro, keyValueOtavio)); |
| 73 | + Optional<Value> otavio = keyValueEntityManager.get("otavio"); |
| 74 | + assertTrue(otavio.isPresent()); |
| 75 | + assertEquals(userOtavio, otavio.get().get(User.class)); |
| 76 | + |
| 77 | + Optional<Value> soro = keyValueEntityManager.get("soro"); |
| 78 | + assertTrue(soro.isPresent()); |
| 79 | + assertEquals(userSoro, soro.get().get(User.class)); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void shouldMultiGet() { |
| 84 | + User user = new User("otavio"); |
| 85 | + KeyValueEntity keyValue = KeyValueEntity.of("otavio", Value.of(user)); |
| 86 | + keyValueEntityManager.put(keyValue); |
| 87 | + assertNotNull(keyValueEntityManager.get("otavio")); |
| 88 | + |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void shouldRemoveKey() { |
| 94 | + |
| 95 | + keyValueEntityManager.put(keyValueOtavio); |
| 96 | + assertTrue(keyValueEntityManager.get("otavio").isPresent()); |
| 97 | + keyValueEntityManager.remove("otavio"); |
| 98 | + assertFalse(keyValueEntityManager.get("otavio").isPresent()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void shouldRemoveMultiKey() { |
| 103 | + |
| 104 | + keyValueEntityManager.put(asList(keyValueSoro, keyValueOtavio)); |
| 105 | + List<String> keys = asList("otavio", "soro"); |
| 106 | + Iterable<Value> values = keyValueEntityManager.get(keys); |
| 107 | + assertThat(StreamSupport.stream(values.spliterator(), false).map(value -> value.get(User.class)).collect(Collectors.toList()), containsInAnyOrder(userOtavio, userSoro)); |
| 108 | + keyValueEntityManager.remove(keys); |
| 109 | + Iterable<Value> users = values; |
| 110 | + assertEquals(0L, StreamSupport.stream(keyValueEntityManager.get(keys).spliterator(), false).count()); |
| 111 | + } |
| 112 | + |
| 113 | + @AfterAll |
| 114 | + public static void shutDown() { |
| 115 | + DynamoDBTestUtils.shutDown(); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments