From 6153c7b97d58f36383f05d4357194c9c3dcb7720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Thu, 1 Sep 2022 14:55:00 +0200 Subject: [PATCH] Add support for match mailboxes with more different names (#1961) --- .../data/repositories/MailboxRepository.kt | 34 +++++++++- .../repositories/MailboxRepositoryTest.kt | 62 ++++++++++++++++++- 2 files changed, 91 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/MailboxRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/MailboxRepository.kt index 8c1097bd..ad4f669e 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/MailboxRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/MailboxRepository.kt @@ -47,7 +47,37 @@ class MailboxRepository @Inject constructor( } else mailbox } - private fun List.filterByStudent(student: Student): Mailbox? = find { - it.studentName.trim() == student.studentName.trim() + private fun List.filterByStudent(student: Student): Mailbox? { + val normalizedStudentName = student.studentName.normalizeStudentName() + + return find { + it.studentName.normalizeStudentName() == normalizedStudentName + } ?: singleOrNull { + it.studentName.getFirstAndLastPart() == normalizedStudentName.getFirstAndLastPart() + } ?: singleOrNull { + it.studentName.getUnauthorizedVersion() == normalizedStudentName + } + } + + private fun String.normalizeStudentName(): String { + return trim().split(" ").joinToString(" ") { part -> + part.lowercase().replaceFirstChar { it.uppercase() } + } + } + + private fun String.getFirstAndLastPart(): String { + val parts = normalizeStudentName().split(" ") + + val endParts = parts.filterIndexed { i, _ -> + i == 0 || parts.size == i - 1 + } + return endParts.joinToString(" ") + } + + private fun String.getUnauthorizedVersion(): String { + return normalizeStudentName().split(" ") + .joinToString(" ") { + it.first() + "*".repeat(it.length - 1) + } } } diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/MailboxRepositoryTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/MailboxRepositoryTest.kt index 56e31563..300662f0 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/MailboxRepositoryTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/MailboxRepositoryTest.kt @@ -77,20 +77,76 @@ class MailboxRepositoryTest { assertEquals(expectedMailbox, selectedMailbox) } - @Test(expected = IllegalArgumentException::class) - fun `get mailbox for student with strange name`() = runTest { + @Test + fun `get mailbox for unique non-authorized student`() = runTest { val student = getStudentEntity( userName = "Stanisław Kowalski", - studentName = "J**** K*****", + studentName = "J** K*******", ) val expectedMailbox = getMailboxEntity("Jan Kowalski") coEvery { mailboxDao.loadAll(any()) } returns listOf( expectedMailbox, ) + assertEquals(expectedMailbox, systemUnderTest.getMailbox(student)) + } + + @Test(expected = IllegalArgumentException::class) + fun `get mailbox for not-unique non-authorized student`() = runTest { + val student = getStudentEntity( + userName = "Stanisław Kowalski", + studentName = "J** K*******", + ) + coEvery { mailboxDao.loadAll(any()) } returns listOf( + getMailboxEntity("Jan Kowalski"), + getMailboxEntity("Jan Kurowski"), + ) + systemUnderTest.getMailbox(student) } + @Test + fun `get mailbox for student with uppercase name`() = runTest { + val student = getStudentEntity( + userName = "Mochoń Julia", + studentName = "KLAUDIA MOCHOŃ", + ) + val expectedMailbox = getMailboxEntity("Klaudia Mochoń") + coEvery { mailboxDao.loadAll(any()) } returns listOf( + expectedMailbox, + ) + + assertEquals(expectedMailbox, systemUnderTest.getMailbox(student)) + } + + @Test + fun `get mailbox for student with second name`() = runTest { + val student = getStudentEntity( + userName = "Fistaszek Karolina", + studentName = "Julia Fistaszek", + ) + val expectedMailbox = getMailboxEntity("Julia Maria Fistaszek") + coEvery { mailboxDao.loadAll(any()) } returns listOf( + expectedMailbox, + ) + + assertEquals(expectedMailbox, systemUnderTest.getMailbox(student)) + } + + @Test + fun `get mailbox for student with second name and uppercase`() = runTest { + val student = getStudentEntity( + userName = "BEDNAREK KAMIL", + studentName = "ALEKSANDRA BEDNAREK", + ) + val expectedMailbox = getMailboxEntity("Aleksandra Anna Bednarek") + coEvery { mailboxDao.loadAll(any()) } returns listOf( + expectedMailbox, + ) + + assertEquals(expectedMailbox, systemUnderTest.getMailbox(student)) + } + private fun getMailboxEntity( studentName: String, ) = Mailbox(