X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fdatabase%2Fmemory%2FMemoryDatabaseTest.kt;h=caacb68d7526914938f8daf89fe1764802a85360;hb=2a9ef0e07136d8c56f4eea32ed49e43bfd5e576f;hp=cc4778fa635aab0f5eeccbd58af4f63291f059a3;hpb=438378deab1514f0f608d975ef65f5b7aea44ccb;p=Sone.git 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 cc4778f..caacb68 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.* @@ -57,8 +58,8 @@ class MemoryDatabaseTest { assertThat(memoryDatabase.getPostReply("reply2"), isPostReply("reply2", "post2", 4000L, "reply2")) assertThat(memoryDatabase.getPostReply("reply3"), isPostReply("reply3", "post1", 5000L, "reply3")) assertThat(memoryDatabase.getPostReply("reply4"), nullValue()) - assertThat(memoryDatabase.getAlbum("album1"), isAlbum("album1", null, "album1", "album-description1")) - assertThat(memoryDatabase.getAlbum("album2"), isAlbum("album2", null, "album2", "album-description2")) + assertThat(memoryDatabase.getAlbum("album1"), isAlbum("album1", "root", "album1", "album-description1")) + assertThat(memoryDatabase.getAlbum("album2"), isAlbum("album2", "root", "album2", "album-description2")) assertThat(memoryDatabase.getAlbum("album3"), isAlbum("album3", "album1", "album3", "album-description3")) assertThat(memoryDatabase.getAlbum("album4"), nullValue()) assertThat(memoryDatabase.getImage("image1"), isImage("image1", 1000L, "KSK@image1", "image1", "image-description1", 16, 9)) @@ -123,9 +124,10 @@ class MemoryDatabaseTest { .setDescription("album-description3") .update() firstAlbum.addAlbum(thirdAlbum) - val rootAlbum = mock() - whenever(rootAlbum.id).thenReturn("root") - whenever(rootAlbum.albums).thenReturn(listOf(firstAlbum, secondAlbum)) + val rootAlbum = AlbumImpl(sone, "root").also { + it.addAlbum(firstAlbum) + it.addAlbum(secondAlbum) + } whenever(sone.rootAlbum).thenReturn(rootAlbum) val firstImage = TestImageBuilder().withId("image1") .build() @@ -175,45 +177,27 @@ class MemoryDatabaseTest { @Test fun `post recipients are detected correctly`() { - val postWithRecipient = createPost(of(RECIPIENT_ID)) + val postWithRecipient = createPost(id = "p1", recipient = createRemoteSone(RECIPIENT_ID)) memoryDatabase.storePost(postWithRecipient) - val postWithoutRecipient = createPost(absent()) + val postWithoutRecipient = createPost(id = "p2", recipient = null) memoryDatabase.storePost(postWithoutRecipient) - assertThat(memoryDatabase.getDirectedPosts(RECIPIENT_ID), contains(postWithRecipient)) - } - - private fun createPost(recipient: Optional): Post { - val postWithRecipient = mock() - whenever(postWithRecipient.id).thenReturn(randomUUID().toString()) - whenever(postWithRecipient.sone).thenReturn(sone) - whenever(postWithRecipient.recipientId).thenReturn(recipient) - return postWithRecipient + assertThat(memoryDatabase.getDirectedPosts(RECIPIENT_ID), contains(isPost(isRecipientId = equalTo(RECIPIENT_ID)))) } @Test fun `post replies are managed correctly`() { - val firstPost = createPost(absent()) - val firstPostFirstReply = createPostReply(firstPost, 1000L) - val secondPost = createPost(absent()) - val secondPostFirstReply = createPostReply(secondPost, 1000L) - val secondPostSecondReply = createPostReply(secondPost, 2000L) + val firstPost = createPost() + val firstPostFirstReply = createPostReply(id = "p1r1", post = firstPost, time = 1000L) + val secondPost = createPost() + 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 @@ -404,7 +388,7 @@ class MemoryDatabaseTest { prepareConfigurationValues() val postReply = mock() whenever(postReply.id).thenReturn("post-reply-id") - memoryDatabase.setPostReplyKnown(postReply, true) + memoryDatabase.setPostReplyKnown(postReply) assertThat(configuration.getStringValue("KnownReplies/0/ID").value, equalTo("post-reply-id")) assertThat(configuration.getStringValue("KnownReplies/1/ID").value, equalTo(null)) } @@ -434,8 +418,8 @@ class MemoryDatabaseTest { prepareConfigurationValues() val postReply = mock() whenever(postReply.id).thenReturn("post-reply-id") - memoryDatabase.setPostReplyKnown(postReply, true) - memoryDatabase.setPostReplyKnown(postReply, true) + memoryDatabase.setPostReplyKnown(postReply) + memoryDatabase.setPostReplyKnown(postReply) verify(configuration, times(1)).getStringValue("KnownReplies/1/ID") }