|
| 1 | +package com.exceptionless.exceptionlessclient.storage; |
| 2 | + |
| 3 | +import com.exceptionless.exceptionlessclient.models.storage.StorageItem; |
| 4 | +import org.junit.jupiter.api.BeforeEach; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | +import java.util.stream.Collectors; |
| 9 | + |
| 10 | +import static org.assertj.core.api.Assertions.assertThat; |
| 11 | + |
| 12 | +public class InMemoryStorageTest { |
| 13 | + private InMemoryStorage<Integer> storage; |
| 14 | + |
| 15 | + @BeforeEach |
| 16 | + public void setup() { |
| 17 | + storage = InMemoryStorage.<Integer>builder().maxItems(2).build(); |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + public void itCanSave() { |
| 22 | + storage.save(123); |
| 23 | + |
| 24 | + assertThat(storage.peek().getValue()).isEqualTo(123); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void itCanSaveAndRemoveIfExceedTheMaxItemsSize() { |
| 29 | + storage.save(123); |
| 30 | + storage.save(456); |
| 31 | + storage.save(789); |
| 32 | + |
| 33 | + assertThat(storage.peek().getValue()).isEqualTo(456); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void itCanGetItemsForALimitLessThanCurrentItems() { |
| 38 | + storage.save(123); |
| 39 | + storage.save(456); |
| 40 | + |
| 41 | + List<StorageItem<Integer>> items = storage.get(1); |
| 42 | + assertThat(items.stream().map(StorageItem::getValue).collect(Collectors.toList())) |
| 43 | + .isEqualTo(List.of(123)); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void itCanGetItemsForALimitEqualToCurrentItems() { |
| 48 | + storage.save(123); |
| 49 | + storage.save(456); |
| 50 | + |
| 51 | + List<StorageItem<Integer>> items = storage.get(2); |
| 52 | + assertThat(items.stream().map(StorageItem::getValue).collect(Collectors.toList())) |
| 53 | + .isEqualTo(List.of(123, 456)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void itCanGetItemsForALimitMoreThanCurrentItems() { |
| 58 | + storage.save(123); |
| 59 | + storage.save(456); |
| 60 | + |
| 61 | + List<StorageItem<Integer>> items = storage.get(3); |
| 62 | + assertThat(items.stream().map(StorageItem::getValue).collect(Collectors.toList())) |
| 63 | + .isEqualTo(List.of(123, 456)); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void itCanRemoveByTimestamp() { |
| 68 | + storage.save(123); |
| 69 | + StorageItem<Integer> item = storage.peek(); |
| 70 | + storage.remove(item.getTimestamp()); |
| 71 | + |
| 72 | + assertThat(storage.peek()).isNull(); |
| 73 | + } |
| 74 | +} |
0 commit comments