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>()
}
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)
}
}
- 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())
}
override fun storePostReply(postReply: PostReply) =
writeLock.withLock {
- allPostReplies[postReply.id] = postReply
+ allPostReplies[postReply.id] = postReply.toShell()
}
override fun removePostReply(postReply: PostReply) =
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 {
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
'}'
}
+ 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)
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.*
@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
}
}
-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)