|
| 1 | +package io.loom.eventsourcing.amazon; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 4 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 5 | +import org.assertj.core.api.ThrowableAssert; |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | +import java.util.Random; |
| 10 | +import java.util.UUID; |
| 11 | +import java.util.concurrent.ExecutionException; |
| 12 | + |
| 13 | +import static io.loom.eventsourcing.amazon.LocalDynamoDB.getMapper; |
| 14 | +import static java.util.Arrays.asList; |
| 15 | +import static java.util.Collections.singletonList; |
| 16 | +import static java.util.UUID.randomUUID; |
| 17 | +import static java.util.stream.Collectors.toList; |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 20 | + |
| 21 | +public class DynamoDBEventStore_specs { |
| 22 | + private static final Random random = new Random(); |
| 23 | + |
| 24 | + public static class Event1 { |
| 25 | + private final int value; |
| 26 | + |
| 27 | + @JsonCreator |
| 28 | + Event1(@JsonProperty("value") int value) { |
| 29 | + this.value = value; |
| 30 | + } |
| 31 | + |
| 32 | + public int getValue() { |
| 33 | + return value; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public static class Event2 { |
| 38 | + private final double value; |
| 39 | + |
| 40 | + @JsonCreator |
| 41 | + Event2(@JsonProperty("value") double value) { |
| 42 | + this.value = value; |
| 43 | + } |
| 44 | + |
| 45 | + public double getValue() { |
| 46 | + return value; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public static class Event3 { |
| 51 | + private final String value; |
| 52 | + |
| 53 | + @JsonCreator |
| 54 | + Event3(@JsonProperty("value") String value) { |
| 55 | + this.value = value; |
| 56 | + } |
| 57 | + |
| 58 | + public String getValue() { |
| 59 | + return value; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void queryEvents_restores_events_correctly() |
| 65 | + throws InterruptedException, ExecutionException { |
| 66 | + // Arrange |
| 67 | + final DynamoDBEventStore sut = new DynamoDBEventStore(getMapper()); |
| 68 | + |
| 69 | + UUID streamId = randomUUID(); |
| 70 | + long firstVersion = 1; |
| 71 | + final List<Object> events = asList( |
| 72 | + new Event3(randomUUID().toString()), |
| 73 | + new Event1(random.nextInt()), |
| 74 | + new Event2(random.nextDouble())); |
| 75 | + |
| 76 | + sut.collectEvents(streamId, firstVersion, events).get(); |
| 77 | + |
| 78 | + // Act |
| 79 | + final Iterable<Object> actual = sut.queryEvents(streamId).get(); |
| 80 | + |
| 81 | + // Assert |
| 82 | + assertThat(actual) |
| 83 | + .usingFieldByFieldElementComparator() |
| 84 | + .containsExactlyElementsOf(events); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void queryEvents_filters_events_by_stream_id() |
| 89 | + throws ExecutionException, InterruptedException { |
| 90 | + // Arrange |
| 91 | + final DynamoDBEventStore sut = new DynamoDBEventStore(getMapper()); |
| 92 | + |
| 93 | + UUID streamId = randomUUID(); |
| 94 | + long firstVersion = 1; |
| 95 | + final List<Object> events = asList( |
| 96 | + new Event3(randomUUID().toString()), |
| 97 | + new Event1(random.nextInt()), |
| 98 | + new Event2(random.nextDouble())); |
| 99 | + |
| 100 | + sut.collectEvents(streamId, firstVersion, events).get(); |
| 101 | + |
| 102 | + sut.collectEvents(randomUUID(), firstVersion, events).get(); |
| 103 | + |
| 104 | + // Act |
| 105 | + final Iterable<Object> actual = sut.queryEvents(streamId).get(); |
| 106 | + |
| 107 | + // Assert |
| 108 | + assertThat(actual) |
| 109 | + .usingFieldByFieldElementComparator() |
| 110 | + .containsExactlyElementsOf(events); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void queryEvents_filters_events_by_version() |
| 115 | + throws ExecutionException, InterruptedException { |
| 116 | + // Arrange |
| 117 | + final DynamoDBEventStore sut = new DynamoDBEventStore(getMapper()); |
| 118 | + |
| 119 | + UUID streamId = randomUUID(); |
| 120 | + long firstVersion = 1; |
| 121 | + final List<Object> events = asList( |
| 122 | + new Event3(randomUUID().toString()), |
| 123 | + new Event1(random.nextInt()), |
| 124 | + new Event2(random.nextDouble())); |
| 125 | + |
| 126 | + sut.collectEvents(streamId, firstVersion, events).get(); |
| 127 | + |
| 128 | + long fromVersion = 2; |
| 129 | + |
| 130 | + // Act |
| 131 | + final Iterable<Object> actual = sut.queryEvents(streamId, fromVersion).get(); |
| 132 | + |
| 133 | + // Assert |
| 134 | + assertThat(actual) |
| 135 | + .usingFieldByFieldElementComparator() |
| 136 | + .containsExactlyElementsOf(events.stream().skip(1).collect(toList())); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void collectEvents_controls_concurrency() |
| 141 | + throws ExecutionException, InterruptedException { |
| 142 | + // Arrange |
| 143 | + final DynamoDBEventStore sut = new DynamoDBEventStore(getMapper()); |
| 144 | + |
| 145 | + final UUID streamId = randomUUID(); |
| 146 | + final long firstVersion = 1; |
| 147 | + final List<Object> events = singletonList(new Event1(random.nextInt())); |
| 148 | + |
| 149 | + sut.collectEvents(streamId, firstVersion, events).get(); |
| 150 | + |
| 151 | + // Act |
| 152 | + final ThrowableAssert.ThrowingCallable action = |
| 153 | + () -> sut.collectEvents(streamId, firstVersion, events).get(); |
| 154 | + |
| 155 | + // Assert |
| 156 | + assertThatThrownBy(action); |
| 157 | + } |
| 158 | +} |
0 commit comments