Replace test for ConfigurationLoader with Kotlin version
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / database / memory / ConfigurationLoaderTest.kt
1 package net.pterodactylus.sone.database.memory
2
3 import net.pterodactylus.sone.test.TestValue.from
4 import net.pterodactylus.sone.test.mock
5 import net.pterodactylus.sone.test.whenever
6 import net.pterodactylus.util.config.Configuration
7 import net.pterodactylus.util.config.Value
8 import org.hamcrest.MatcherAssert.assertThat
9 import org.hamcrest.Matchers.containsInAnyOrder
10 import org.hamcrest.Matchers.nullValue
11 import org.junit.Test
12
13 /**
14  * Unit test for [ConfigurationLoader].
15  *
16  * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net)
17  */
18 class ConfigurationLoaderTest {
19
20         private val configuration = mock<Configuration>()
21         private val configurationLoader = ConfigurationLoader(configuration)
22
23         private fun setupStringValue(attribute: String, value: String? = null): Value<String?> =
24                         from(value).apply {
25                                 whenever(configuration.getStringValue(attribute)).thenReturn(this)
26                         }
27
28         @Test
29         fun `loader can load known posts`() {
30                 setupStringValue("KnownPosts/0/ID", "Post2")
31                 setupStringValue("KnownPosts/1/ID", "Post1")
32                 setupStringValue("KnownPosts/2/ID")
33                 val knownPosts = configurationLoader.loadKnownPosts()
34                 assertThat(knownPosts, containsInAnyOrder("Post1", "Post2"))
35         }
36
37         @Test
38         fun `loader can load known post replies`() {
39                 setupStringValue("KnownReplies/0/ID", "PostReply2")
40                 setupStringValue("KnownReplies/1/ID", "PostReply1")
41                 setupStringValue("KnownReplies/2/ID")
42                 val knownPosts = configurationLoader.loadKnownPostReplies()
43                 assertThat(knownPosts, containsInAnyOrder("PostReply1", "PostReply2"))
44         }
45
46         @Test
47         fun `loader can load bookmarked posts`() {
48                 setupStringValue("Bookmarks/Post/0/ID", "Post2")
49                 setupStringValue("Bookmarks/Post/1/ID", "Post1")
50                 setupStringValue("Bookmarks/Post/2/ID")
51                 val knownPosts = configurationLoader.loadBookmarkedPosts()
52                 assertThat(knownPosts, containsInAnyOrder("Post1", "Post2"))
53         }
54
55         @Test
56         fun `loader can save bookmarked posts`() {
57                 val post1 = setupStringValue("Bookmarks/Post/0/ID")
58                 val post2 = setupStringValue("Bookmarks/Post/1/ID")
59                 val post3 = setupStringValue("Bookmarks/Post/2/ID")
60                 val originalPosts = setOf("Post1", "Post2")
61                 configurationLoader.saveBookmarkedPosts(originalPosts)
62                 val extractedPosts = setOf(post1.value, post2.value)
63                 assertThat(extractedPosts, containsInAnyOrder("Post1", "Post2"))
64                 assertThat(post3.value, nullValue())
65         }
66
67 }