Skip to content

Commit aa986c8

Browse files
committed
Added unit tests for storage package
1 parent 6db3253 commit aa986c8

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.exceptionless.exceptionlessclient.storage;
2+
3+
import com.exceptionless.exceptionlessclient.models.Event;
4+
import com.exceptionless.exceptionlessclient.models.settings.ServerSettings;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
public class InMemoryStorageProviderTest {
10+
@Test
11+
public void itCanSetStorageCorrectly() {
12+
InMemoryStorageProvider storageProvider =
13+
InMemoryStorageProvider.builder().maxQueueItems(2).build();
14+
15+
storageProvider.getQueue().save(Event.builder().referenceId("123-abcdef").build());
16+
storageProvider.getQueue().save(Event.builder().referenceId("456-abcdef").build());
17+
storageProvider.getQueue().save(Event.builder().referenceId("789-abcdef").build());
18+
19+
storageProvider.getSettings().save(ServerSettings.builder().version(1L).build());
20+
storageProvider.getSettings().save(ServerSettings.builder().version(2L).build());
21+
22+
assertThat(storageProvider.getSettings().peek().getValue().getVersion()).isEqualTo(2);
23+
assertThat(storageProvider.getQueue().peek().getValue().getReferenceId())
24+
.isEqualTo("456-abcdef");
25+
}
26+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)