From 9fab35abd7dc86afe186ed69a86758a3a0ac426c Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 5 Aug 2020 22:22:51 +0200 Subject: [PATCH] =?utf8?q?=E2=9A=A1=EF=B8=8F=20Use=20shell=20to=20store=20?= =?utf8?q?reply=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/database/memory/MemoryDatabase.kt | 13 ++++++++----- .../sone/database/memory/MemoryPostReply.kt | 11 +++++++++++ .../sone/database/memory/MemoryDatabaseTest.kt | 21 ++++++--------------- .../kotlin/net/pterodactylus/sone/test/Mocks.kt | 4 ++-- 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabase.kt b/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabase.kt index 3b5a6a9..d48ef1e 100644 --- a/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabase.kt +++ b/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabase.kt @@ -63,7 +63,7 @@ class MemoryDatabase @Inject constructor(private val configuration: Configuratio private val allPosts = mutableMapOf() private val sonePosts: Multimap = HashMultimap.create() private val knownPosts = mutableSetOf() - private val allPostReplies = mutableMapOf() + private val allPostReplies = mutableMapOf() private val sonePostReplies: Multimap = TreeMultimap.create(Comparator { leftString, rightString -> leftString.compareTo(rightString) }, newestReplyFirst) private val knownPostReplies = mutableSetOf() private val allAlbums = mutableMapOf() @@ -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 { diff --git a/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryPostReply.kt b/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryPostReply.kt index 0f2969a..67b44b7 100644 --- a/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryPostReply.kt +++ b/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryPostReply.kt @@ -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) diff --git a/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt b/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt index 973047f..6e9b31a 100644 --- a/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt @@ -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() - 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 diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt b/src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt index 1077524..e90c080 100644 --- a/src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt +++ b/src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt @@ -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 = Optional.fromNullable(post) -- 2.7.4