⚡️ Use shell to store reply data
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 5 Aug 2020 20:22:51 +0000 (22:22 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 6 Aug 2020 09:25:56 +0000 (11:25 +0200)
src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabase.kt
src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryPostReply.kt
src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt
src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt

index 3b5a6a9..d48ef1e 100644 (file)
@@ -63,7 +63,7 @@ class MemoryDatabase @Inject constructor(private val configuration: Configuratio
        private val allPosts = mutableMapOf<String, Post>()
        private val sonePosts: Multimap<String, Post> = HashMultimap.create<String, Post>()
        private val knownPosts = mutableSetOf<String>()
-       private val allPostReplies = mutableMapOf<String, PostReply>()
+       private val allPostReplies = mutableMapOf<String, MemoryPostReply.Shell>()
        private val sonePostReplies: Multimap<String, PostReply> = TreeMultimap.create<String, PostReply>(Comparator { leftString, rightString -> leftString.compareTo(rightString) }, newestReplyFirst)
        private val knownPostReplies = mutableSetOf<String>()
        private val allAlbums = mutableMapOf<String, Album>()
@@ -123,7 +123,7 @@ class MemoryDatabase @Inject constructor(private val configuration: Configuratio
                        }
                        sonePostReplies.putAll(sone.id, sone.replies)
                        for (postReply in sone.replies) {
-                               allPostReplies[postReply.id] = postReply
+                               allPostReplies[postReply.id] = postReply.toShell()
                        }
                        sone.allAlbums.let { albums ->
                                soneAlbums.putAll(sone.id, albums)
@@ -223,12 +223,15 @@ class MemoryDatabase @Inject constructor(private val configuration: Configuratio
                }
        }
 
-       override fun getPostReply(id: String) = readLock.withLock { allPostReplies[id] }
+       override fun getPostReply(id: String) = readLock.withLock {
+               allPostReplies[id]?.build(newPostReplyBuilder())
+       }
 
        override fun getReplies(postId: String) =
                        readLock.withLock {
                                allPostReplies.values
                                                .filter { it.postId == postId }
+                                               .map { it.build(newPostReplyBuilder()) }
                                                .sortedWith(newestReplyFirst.reversed())
                        }
 
@@ -237,7 +240,7 @@ class MemoryDatabase @Inject constructor(private val configuration: Configuratio
 
        override fun storePostReply(postReply: PostReply) =
                        writeLock.withLock {
-                               allPostReplies[postReply.id] = postReply
+                               allPostReplies[postReply.id] = postReply.toShell()
                        }
 
        override fun removePostReply(postReply: PostReply) =
@@ -297,7 +300,7 @@ class MemoryDatabase @Inject constructor(private val configuration: Configuratio
                                saveKnownPosts()
                        }
 
-       protected fun isPostReplyKnown(postReply: PostReply) = readLock.withLock { postReply.id in knownPostReplies }
+       internal fun isPostReplyKnown(postReply: PostReply) = readLock.withLock { postReply.id in knownPostReplies }
 
        override fun setPostReplyKnown(postReply: PostReply): Unit =
                        writeLock.withLock {
index 0f2969a..67b44b7 100644 (file)
@@ -19,6 +19,7 @@ package net.pterodactylus.sone.database.memory
 import com.google.common.base.Optional
 import net.pterodactylus.sone.data.Post
 import net.pterodactylus.sone.data.PostReply
+import net.pterodactylus.sone.database.PostReplyBuilder
 import net.pterodactylus.sone.database.SoneProvider
 import net.pterodactylus.sone.utils.asOptional
 
@@ -67,4 +68,14 @@ class MemoryPostReply(
                                '}'
        }
 
+       class Shell(val id: String, val soneId: String, val postId: String, val time: Long, val text: String) {
+
+               fun build(postReplyBuilder: PostReplyBuilder): PostReply {
+                       return postReplyBuilder.withId(id).from(soneId).to(postId).withTime(time).withText(text).build()
+               }
+
+       }
+
 }
+
+fun PostReply.toShell() = MemoryPostReply.Shell(id, sone.id, postId, time, text)
index 973047f..6e9b31a 100644 (file)
@@ -25,6 +25,7 @@ import net.pterodactylus.sone.test.*
 import net.pterodactylus.sone.test.Matchers.*
 import net.pterodactylus.util.config.*
 import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers
 import org.hamcrest.Matchers.*
 import org.mockito.ArgumentMatchers.anyString
 import org.mockito.Mockito.*
@@ -194,27 +195,17 @@ class MemoryDatabaseTest {
        @Test
        fun `post replies are managed correctly`() {
                val firstPost = createPost(absent())
-               val firstPostFirstReply = createPostReply(firstPost, 1000L)
+               val firstPostFirstReply = createPostReply(id = "p1r1", post = firstPost, time = 1000L)
                val secondPost = createPost(absent())
-               val secondPostFirstReply = createPostReply(secondPost, 1000L)
-               val secondPostSecondReply = createPostReply(secondPost, 2000L)
+               val secondPostFirstReply = createPostReply(id = "p2r1", post = secondPost, time = 1000L)
+               val secondPostSecondReply = createPostReply(id = "p2r2", post = secondPost, time = 2000L)
                memoryDatabase.storePost(firstPost)
                memoryDatabase.storePost(secondPost)
                memoryDatabase.storePostReply(firstPostFirstReply)
                memoryDatabase.storePostReply(secondPostFirstReply)
                memoryDatabase.storePostReply(secondPostSecondReply)
-               assertThat(memoryDatabase.getReplies(firstPost.id), contains(firstPostFirstReply))
-               assertThat(memoryDatabase.getReplies(secondPost.id), contains(secondPostFirstReply, secondPostSecondReply))
-       }
-
-       private fun createPostReply(post: Post, time: Long): PostReply {
-               val postReply = mock<PostReply>()
-               whenever(postReply.id).thenReturn(randomUUID().toString())
-               whenever(postReply.time).thenReturn(time)
-               whenever(postReply.post).thenReturn(of(post))
-               val postId = post.id
-               whenever(postReply.postId).thenReturn(postId)
-               return postReply
+               assertThat(memoryDatabase.getReplies(firstPost.id).map(PostReply::id), Matchers.contains("p1r1"))
+               assertThat(memoryDatabase.getReplies(secondPost.id).map(PostReply::id), contains("p2r1", "p2r2"))
        }
 
        @Test
index 1077524..e90c080 100644 (file)
@@ -83,8 +83,8 @@ fun createPost(text: String = "", sone: Sone? = remoteSone1, known: Boolean = fa
        }
 }
 
-fun createPostReply(text: String = "", post: Post? = createPost(), sone: Sone = remoteSone1, known: Boolean = false, time: Long = 1) = object : PostReply {
-       override val id = "reply-id"
+fun createPostReply(text: String = "text", post: Post? = createPost(), sone: Sone = remoteSone1, known: Boolean = false, time: Long = 1, id: String = "reply-id") = object : PostReply {
+       override val id = id
        override fun getSone() = sone
        override fun getPostId() = post!!.id
        override fun getPost(): Optional<Post> = Optional.fromNullable(post)