1 package net.pterodactylus.sone.database.memory;
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.hamcrest.Matchers.nullValue;
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.when;
10 import java.util.HashSet;
13 import net.pterodactylus.sone.test.TestValue;
14 import net.pterodactylus.util.config.Configuration;
15 import net.pterodactylus.util.config.ConfigurationException;
16 import net.pterodactylus.util.config.Value;
18 import org.junit.Test;
21 * Unit test for {@link ConfigurationLoader}.
23 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
25 public class ConfigurationLoaderTest {
27 private final Configuration configuration = mock(Configuration.class);
28 private final ConfigurationLoader configurationLoader =
29 new ConfigurationLoader(configuration);
32 public void loaderCanLoadKnownPosts() {
33 when(configuration.getStringValue("KnownPosts/0/ID"))
34 .thenReturn(TestValue.from("Post2"));
35 when(configuration.getStringValue("KnownPosts/1/ID"))
36 .thenReturn(TestValue.from("Post1"));
37 when(configuration.getStringValue("KnownPosts/2/ID"))
38 .thenReturn(TestValue.<String>from(null));
39 Set<String> knownPosts = configurationLoader.loadKnownPosts();
40 assertThat(knownPosts, containsInAnyOrder("Post1", "Post2"));
44 public void loaderCanLoadKnownPostReplies() {
45 when(configuration.getStringValue("KnownReplies/0/ID"))
46 .thenReturn(TestValue.from("PostReply2"));
47 when(configuration.getStringValue("KnownReplies/1/ID"))
48 .thenReturn(TestValue.from("PostReply1"));
49 when(configuration.getStringValue("KnownReplies/2/ID"))
50 .thenReturn(TestValue.<String>from(null));
51 Set<String> knownPosts = configurationLoader.loadKnownPostReplies();
52 assertThat(knownPosts,
53 containsInAnyOrder("PostReply1", "PostReply2"));
57 public void loaderCanLoadBookmarkedPosts() {
58 when(configuration.getStringValue("Bookmarks/Post/0/ID"))
59 .thenReturn(TestValue.from("Post2"));
60 when(configuration.getStringValue("Bookmarks/Post/1/ID"))
61 .thenReturn(TestValue.from("Post1"));
62 when(configuration.getStringValue("Bookmarks/Post/2/ID"))
63 .thenReturn(TestValue.<String>from(null));
64 Set<String> knownPosts = configurationLoader.loadBookmarkedPosts();
65 assertThat(knownPosts, containsInAnyOrder("Post1", "Post2"));
69 public void loaderCanSaveBookmarkedPosts() throws ConfigurationException {
70 final Value<String> post1 = TestValue.<String>from(null);
71 final Value<String> post2 = TestValue.<String>from(null);
72 final Value<String> post3 = TestValue.<String>from(null);
73 when(configuration.getStringValue("Bookmarks/Post/0/ID")).thenReturn(post1);
74 when(configuration.getStringValue("Bookmarks/Post/1/ID")).thenReturn(post2);
75 when(configuration.getStringValue("Bookmarks/Post/2/ID")).thenReturn(post3);
76 HashSet<String> originalPosts = new HashSet<String>(asList("Post1", "Post2"));
77 configurationLoader.saveBookmarkedPosts(originalPosts);
78 HashSet<String> extractedPosts =
79 new HashSet<String>(asList(post1.getValue(), post2.getValue()));
80 assertThat(extractedPosts, containsInAnyOrder("Post1", "Post2"));
81 assertThat(post3.getValue(), nullValue());