forked from github/wulkanowy-mirror
Add support for messages plus API (#1945)
This commit is contained in:
parent
08a3bd77bd
commit
9d47127921
42 changed files with 3065 additions and 575 deletions
|
@ -1,5 +1,7 @@
|
|||
package io.github.wulkanowy
|
||||
|
||||
import io.github.wulkanowy.data.db.entities.Mailbox
|
||||
import io.github.wulkanowy.data.db.entities.MailboxType
|
||||
import io.github.wulkanowy.data.db.entities.Semester
|
||||
import io.github.wulkanowy.data.db.entities.Student
|
||||
import io.github.wulkanowy.sdk.Sdk
|
||||
|
@ -21,6 +23,16 @@ fun getSemesterEntity(diaryId: Int = 1, semesterId: Int = 1, start: LocalDate =
|
|||
end = end
|
||||
)
|
||||
|
||||
fun getMailboxEntity() = Mailbox(
|
||||
globalKey = "v4",
|
||||
fullName = "",
|
||||
userName = "",
|
||||
userLoginId = 0,
|
||||
studentName = "",
|
||||
schoolNameShort = "",
|
||||
type = MailboxType.UNKNOWN,
|
||||
)
|
||||
|
||||
fun getSemesterPojo(diaryId: Int, semesterId: Int, start: LocalDate, end: LocalDate, semesterName: Int = 1) = SdkSemester(
|
||||
diaryId = diaryId,
|
||||
kindergartenDiaryId = 0,
|
||||
|
|
|
@ -10,12 +10,10 @@ import io.github.wulkanowy.data.db.entities.MessageWithAttachment
|
|||
import io.github.wulkanowy.data.enums.MessageFolder
|
||||
import io.github.wulkanowy.data.errorOrNull
|
||||
import io.github.wulkanowy.data.toFirstResult
|
||||
import io.github.wulkanowy.getSemesterEntity
|
||||
import io.github.wulkanowy.getMailboxEntity
|
||||
import io.github.wulkanowy.getStudentEntity
|
||||
import io.github.wulkanowy.sdk.Sdk
|
||||
import io.github.wulkanowy.sdk.pojo.Folder
|
||||
import io.github.wulkanowy.sdk.pojo.MessageDetails
|
||||
import io.github.wulkanowy.sdk.pojo.Sender
|
||||
import io.github.wulkanowy.utils.AutoRefreshHelper
|
||||
import io.github.wulkanowy.utils.Status
|
||||
import io.github.wulkanowy.utils.status
|
||||
|
@ -23,7 +21,6 @@ import io.mockk.*
|
|||
import io.mockk.impl.annotations.MockK
|
||||
import io.mockk.impl.annotations.SpyK
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.flow.toList
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
@ -60,7 +57,7 @@ class MessageRepositoryTest {
|
|||
|
||||
private val student = getStudentEntity()
|
||||
|
||||
private val semester = getSemesterEntity()
|
||||
private val mailbox = getMailboxEntity()
|
||||
|
||||
private lateinit var repository: MessageRepository
|
||||
|
||||
|
@ -80,59 +77,18 @@ class MessageRepositoryTest {
|
|||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get messages when read by values was changed on already read message`() = runTest {
|
||||
every { messageDb.loadAll(any(), any()) } returns flow {
|
||||
val dbMessage = getMessageEntity(3, "", false).apply {
|
||||
unreadBy = 10
|
||||
readBy = 5
|
||||
isNotified = true
|
||||
}
|
||||
emit(listOf(dbMessage))
|
||||
}
|
||||
coEvery { sdk.getMessages(Folder.RECEIVED, any(), any()) } returns listOf(
|
||||
getMessageDto(messageId = 3, content = "", unread = false).copy(
|
||||
unreadBy = 5,
|
||||
readBy = 10,
|
||||
)
|
||||
)
|
||||
coEvery { messageDb.deleteAll(any()) } just Runs
|
||||
coEvery { messageDb.insertAll(any()) } returns listOf()
|
||||
|
||||
repository.getMessages(
|
||||
student = student,
|
||||
semester = semester,
|
||||
folder = MessageFolder.RECEIVED,
|
||||
forceRefresh = true,
|
||||
notify = true, // all new messages will be marked as not notified
|
||||
).toFirstResult().dataOrNull.orEmpty()
|
||||
|
||||
coVerify(exactly = 1) { messageDb.deleteAll(emptyList()) }
|
||||
coVerify(exactly = 1) { messageDb.insertAll(emptyList()) }
|
||||
coVerify(exactly = 1) {
|
||||
messageDb.updateAll(withArg {
|
||||
assertEquals(1, it.size)
|
||||
assertEquals(5, it.single().unreadBy)
|
||||
assertEquals(10, it.single().readBy)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get messages when fetched completely new message without notify`() = runBlocking {
|
||||
every { messageDb.loadAll(any(), any()) } returns flowOf(emptyList())
|
||||
coEvery { sdk.getMessages(Folder.RECEIVED, any(), any()) } returns listOf(
|
||||
getMessageDto(messageId = 4, content = "Test", unread = true).copy(
|
||||
unreadBy = 5,
|
||||
readBy = 10,
|
||||
)
|
||||
coEvery { sdk.getMessages(Folder.RECEIVED, any()) } returns listOf(
|
||||
getMessageDto()
|
||||
)
|
||||
coEvery { messageDb.deleteAll(any()) } just Runs
|
||||
coEvery { messageDb.insertAll(any()) } returns listOf()
|
||||
|
||||
repository.getMessages(
|
||||
student = student,
|
||||
semester = semester,
|
||||
mailbox = mailbox,
|
||||
folder = MessageFolder.RECEIVED,
|
||||
forceRefresh = true,
|
||||
notify = false,
|
||||
|
@ -151,7 +107,7 @@ class MessageRepositoryTest {
|
|||
fun `throw error when message is not in the db`() {
|
||||
val testMessage = getMessageEntity(1, "", false)
|
||||
coEvery {
|
||||
messageDb.loadMessageWithAttachment(1, 1)
|
||||
messageDb.loadMessageWithAttachment("v4")
|
||||
} throws NoSuchElementException("No message in database")
|
||||
|
||||
runBlocking { repository.getMessage(student, testMessage).toFirstResult() }
|
||||
|
@ -162,7 +118,7 @@ class MessageRepositoryTest {
|
|||
val testMessage = getMessageEntity(123, "Test", false)
|
||||
val messageWithAttachment = MessageWithAttachment(testMessage, emptyList())
|
||||
|
||||
coEvery { messageDb.loadMessageWithAttachment(1, testMessage.messageId) } returns flowOf(
|
||||
coEvery { messageDb.loadMessageWithAttachment("v4") } returns flowOf(
|
||||
messageWithAttachment
|
||||
)
|
||||
|
||||
|
@ -174,7 +130,7 @@ class MessageRepositoryTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `get message when content in db is empty`() {
|
||||
fun `get message when content in db is empty`() = runTest {
|
||||
val testMessage = getMessageEntity(123, "", true)
|
||||
val testMessageWithContent = testMessage.copy().apply { content = "Test" }
|
||||
|
||||
|
@ -182,23 +138,19 @@ class MessageRepositoryTest {
|
|||
val mWaWithContent = MessageWithAttachment(testMessageWithContent, emptyList())
|
||||
|
||||
coEvery {
|
||||
messageDb.loadMessageWithAttachment(
|
||||
1,
|
||||
testMessage.messageId
|
||||
)
|
||||
messageDb.loadMessageWithAttachment("v4")
|
||||
} returnsMany listOf(flowOf(mWa), flowOf(mWaWithContent))
|
||||
coEvery {
|
||||
sdk.getMessageDetails(
|
||||
messageId = testMessage.messageId,
|
||||
folderId = 1,
|
||||
read = false,
|
||||
id = testMessage.realId
|
||||
)
|
||||
} returns MessageDetails("Test", emptyList())
|
||||
sdk.getMessageDetails("v4")
|
||||
} returns mockk {
|
||||
every { sender } returns ""
|
||||
every { recipients } returns listOf("")
|
||||
every { attachments } returns listOf()
|
||||
}
|
||||
coEvery { messageDb.updateAll(any()) } just Runs
|
||||
coEvery { messageAttachmentDao.insertAttachments(any()) } returns listOf(1)
|
||||
|
||||
val res = runBlocking { repository.getMessage(student, testMessage).toFirstResult() }
|
||||
val res = repository.getMessage(student, testMessage).toFirstResult()
|
||||
|
||||
assertEquals(null, res.errorOrNull)
|
||||
assertEquals(Status.SUCCESS, res.status)
|
||||
|
@ -211,7 +163,7 @@ class MessageRepositoryTest {
|
|||
val testMessage = getMessageEntity(123, "", false)
|
||||
|
||||
coEvery {
|
||||
messageDb.loadMessageWithAttachment(1, testMessage.messageId)
|
||||
messageDb.loadMessageWithAttachment("v4")
|
||||
} throws UnknownHostException()
|
||||
|
||||
runBlocking { repository.getMessage(student, testMessage).toFirstResult() }
|
||||
|
@ -222,7 +174,7 @@ class MessageRepositoryTest {
|
|||
val testMessage = getMessageEntity(123, "", true)
|
||||
|
||||
coEvery {
|
||||
messageDb.loadMessageWithAttachment(1, testMessage.messageId)
|
||||
messageDb.loadMessageWithAttachment("v4")
|
||||
} throws UnknownHostException()
|
||||
|
||||
runBlocking { repository.getMessage(student, testMessage).toList()[1] }
|
||||
|
@ -233,42 +185,30 @@ class MessageRepositoryTest {
|
|||
content: String,
|
||||
unread: Boolean
|
||||
) = Message(
|
||||
studentId = 1,
|
||||
realId = 1,
|
||||
messageGlobalKey = "v4",
|
||||
mailboxKey = "",
|
||||
correspondents = "",
|
||||
messageId = messageId,
|
||||
sender = "",
|
||||
senderId = 0,
|
||||
recipient = "Wielu adresatów",
|
||||
subject = "",
|
||||
date = Instant.EPOCH,
|
||||
folderId = 1,
|
||||
unread = unread,
|
||||
removed = false,
|
||||
hasAttachments = false
|
||||
).apply {
|
||||
this.content = content
|
||||
unreadBy = 1
|
||||
readBy = 1
|
||||
}
|
||||
|
||||
private fun getMessageDto(
|
||||
messageId: Int,
|
||||
content: String,
|
||||
unread: Boolean,
|
||||
) = io.github.wulkanowy.sdk.pojo.Message(
|
||||
id = 1,
|
||||
messageId = messageId,
|
||||
sender = Sender("", "", 0, 0, 0, ""),
|
||||
private fun getMessageDto() = io.github.wulkanowy.sdk.pojo.Message(
|
||||
globalKey = "v4",
|
||||
mailbox = "",
|
||||
correspondents = "",
|
||||
id = 4,
|
||||
recipients = listOf(),
|
||||
subject = "",
|
||||
content = content,
|
||||
date = Instant.EPOCH.atZone(ZoneOffset.UTC).toLocalDateTime(),
|
||||
content = "Test",
|
||||
dateZoned = Instant.EPOCH.atZone(ZoneOffset.UTC),
|
||||
folderId = 1,
|
||||
unread = unread,
|
||||
unreadBy = 0,
|
||||
readBy = 0,
|
||||
removed = false,
|
||||
unread = true,
|
||||
hasAttachments = false,
|
||||
)
|
||||
}
|
||||
|
|
|
@ -1,19 +1,15 @@
|
|||
package io.github.wulkanowy.data.repositories
|
||||
|
||||
import io.github.wulkanowy.data.db.dao.RecipientDao
|
||||
import io.github.wulkanowy.data.db.entities.ReportingUnit
|
||||
import io.github.wulkanowy.data.mappers.mapToEntities
|
||||
import io.github.wulkanowy.getMailboxEntity
|
||||
import io.github.wulkanowy.getStudentEntity
|
||||
import io.github.wulkanowy.sdk.Sdk
|
||||
import io.github.wulkanowy.sdk.pojo.MailboxType
|
||||
import io.github.wulkanowy.utils.AutoRefreshHelper
|
||||
import io.mockk.MockKAnnotations
|
||||
import io.mockk.Runs
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.coVerify
|
||||
import io.mockk.every
|
||||
import io.mockk.*
|
||||
import io.mockk.impl.annotations.MockK
|
||||
import io.mockk.impl.annotations.SpyK
|
||||
import io.mockk.just
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Before
|
||||
|
@ -36,9 +32,30 @@ class RecipientLocalTest {
|
|||
private lateinit var recipientRepository: RecipientRepository
|
||||
|
||||
private val remoteList = listOf(
|
||||
SdkRecipient("2rPracownik", "Kowalski Jan", 3, 4, 2, "hash", "Kowalski Jan [KJ] - Pracownik (Fake123456)"),
|
||||
SdkRecipient("3rPracownik", "Kowalska Karolina", 4, 4, 2, "hash", "Kowalska Karolina [KK] - Pracownik (Fake123456)"),
|
||||
SdkRecipient("4rPracownik", "Krupa Stanisław", 5, 4, 1, "hash", "Krupa Stanisław [KS] - Uczeń (Fake123456)")
|
||||
SdkRecipient(
|
||||
mailboxGlobalKey = "2rPracownik",
|
||||
userName = "Kowalski Jan",
|
||||
fullName = "Kowalski Jan [KJ] - Pracownik (Fake123456)",
|
||||
studentName = "",
|
||||
schoolNameShort = "",
|
||||
type = MailboxType.UNKNOWN,
|
||||
),
|
||||
SdkRecipient(
|
||||
mailboxGlobalKey = "3rPracownik",
|
||||
userName = "Kowalska Karolina",
|
||||
fullName = "Kowalska Karolina [KK] - Pracownik (Fake123456)",
|
||||
studentName = "",
|
||||
schoolNameShort = "",
|
||||
type = MailboxType.UNKNOWN,
|
||||
),
|
||||
SdkRecipient(
|
||||
mailboxGlobalKey = "4rPracownik",
|
||||
userName = "Krupa Stanisław",
|
||||
fullName = "Krupa Stanisław [KS] - Uczeń (Fake123456)",
|
||||
studentName = "",
|
||||
schoolNameShort = "",
|
||||
type = MailboxType.UNKNOWN,
|
||||
)
|
||||
)
|
||||
|
||||
@Before
|
||||
|
@ -52,39 +69,61 @@ class RecipientLocalTest {
|
|||
@Test
|
||||
fun `load recipients when items already in database`() {
|
||||
// prepare
|
||||
coEvery { recipientDb.loadAll(4, 123, 7) } returnsMany listOf(
|
||||
remoteList.mapToEntities(4),
|
||||
remoteList.mapToEntities(4)
|
||||
coEvery { recipientDb.loadAll(io.github.wulkanowy.data.db.entities.MailboxType.UNKNOWN, "v4") } returnsMany listOf(
|
||||
remoteList.mapToEntities("v4"),
|
||||
remoteList.mapToEntities("v4")
|
||||
)
|
||||
coEvery { recipientDb.insertAll(any()) } returns listOf(1, 2, 3)
|
||||
coEvery { recipientDb.deleteAll(any()) } just Runs
|
||||
|
||||
// execute
|
||||
val res = runBlocking { recipientRepository.getRecipients(student, ReportingUnit(4, 123, "", 4, "", listOf()), 7) }
|
||||
val res = runBlocking {
|
||||
recipientRepository.getRecipients(
|
||||
student = student,
|
||||
mailbox = getMailboxEntity(),
|
||||
type = io.github.wulkanowy.data.db.entities.MailboxType.UNKNOWN,
|
||||
)
|
||||
}
|
||||
|
||||
// verify
|
||||
assertEquals(3, res.size)
|
||||
coVerify { recipientDb.loadAll(4, 123, 7) }
|
||||
coVerify {
|
||||
recipientDb.loadAll(
|
||||
type = io.github.wulkanowy.data.db.entities.MailboxType.UNKNOWN,
|
||||
studentMailboxGlobalKey = "v4"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `load recipients when database is empty`() {
|
||||
// prepare
|
||||
coEvery { sdk.getRecipients(123, 7) } returns remoteList
|
||||
coEvery { recipientDb.loadAll(4, 123, 7) } returnsMany listOf(
|
||||
coEvery { sdk.getRecipients("v4") } returns remoteList
|
||||
coEvery {
|
||||
recipientDb.loadAll(
|
||||
io.github.wulkanowy.data.db.entities.MailboxType.UNKNOWN,
|
||||
"v4"
|
||||
)
|
||||
} returnsMany listOf(
|
||||
emptyList(),
|
||||
remoteList.mapToEntities(4)
|
||||
remoteList.mapToEntities("v4")
|
||||
)
|
||||
coEvery { recipientDb.insertAll(any()) } returns listOf(1, 2, 3)
|
||||
coEvery { recipientDb.deleteAll(any()) } just Runs
|
||||
|
||||
// execute
|
||||
val res = runBlocking { recipientRepository.getRecipients(student, ReportingUnit(4, 123, "", 4, "", listOf()), 7) }
|
||||
val res = runBlocking {
|
||||
recipientRepository.getRecipients(
|
||||
student = student,
|
||||
mailbox = getMailboxEntity(),
|
||||
type = io.github.wulkanowy.data.db.entities.MailboxType.UNKNOWN,
|
||||
)
|
||||
}
|
||||
|
||||
// verify
|
||||
assertEquals(3, res.size)
|
||||
coVerify { sdk.getRecipients(123, 7) }
|
||||
coVerify { recipientDb.loadAll(4, 123, 7) }
|
||||
coVerify { sdk.getRecipients("v4") }
|
||||
coVerify { recipientDb.loadAll(io.github.wulkanowy.data.db.entities.MailboxType.UNKNOWN, "v4") }
|
||||
coVerify { recipientDb.insertAll(match { it.isEmpty() }) }
|
||||
coVerify { recipientDb.deleteAll(match { it.isEmpty() }) }
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue