Skip to content

Commit

Permalink
Merge pull request #286 from MyPureCloud/hotfix/MTSDK-395-Expose-file…
Browse files Browse the repository at this point in the history
…-size

MTSDK-395 - Expose File Size
  • Loading branch information
AfanasievAnton authored May 22, 2024
2 parents 6cfb4e3 + f6b4295 commit 39d89d9
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,25 @@ internal class AttachmentHandlerImplTest {

@Test
fun `when prepare()`() {
val expectedAttachment = Attachment(AttachmentValues.Id, AttachmentValues.FileName, State.Presigning)
val expectedProcessedAttachment = ProcessedAttachment(expectedAttachment, ByteArray(1))
val expectedAttachment = Attachment(
AttachmentValues.Id,
AttachmentValues.FileName,
null,
State.Presigning
)
val expectedProcessedAttachment = ProcessedAttachment(expectedAttachment, ByteArray(AttachmentValues.FileSize))
val expectedOnAttachmentRequest = OnAttachmentRequest(
token = TestValues.Token,
attachmentId = AttachmentValues.Id,
fileName = AttachmentValues.FileName,
fileType = "image/png",
1,
fileSize = AttachmentValues.FileSize,
null,
true,
)

val onAttachmentRequest =
subject.prepare(AttachmentValues.Id, ByteArray(1), AttachmentValues.FileName)
subject.prepare(AttachmentValues.Id, ByteArray(AttachmentValues.FileSize), AttachmentValues.FileName)

verify {
mockLogger.i(capture(logSlot))
Expand All @@ -112,7 +117,7 @@ internal class AttachmentHandlerImplTest {
fun `when upload() processed attachment`() {
val expectedProgress = 25f
val expectedAttachment =
Attachment(AttachmentValues.Id, AttachmentValues.FileName, State.Uploading)
Attachment(AttachmentValues.Id, AttachmentValues.FileName, null, State.Uploading)
val mockUploadProgress: ((Float) -> Unit) = spyk()
val progressSlot = slot<Float>()
givenPrepareCalled(uploadProgress = mockUploadProgress)
Expand Down Expand Up @@ -208,7 +213,7 @@ internal class AttachmentHandlerImplTest {
val expectedDownloadUrl = "http://somedownloadurl.com"
val expectedState = State.Uploaded(expectedDownloadUrl)
val expectedAttachment =
Attachment(AttachmentValues.Id, AttachmentValues.FileName, expectedState)
Attachment(AttachmentValues.Id, AttachmentValues.FileName, null, expectedState)
val expectedProcessedAttachment = ProcessedAttachment(expectedAttachment, ByteArray(1))
givenPrepareCalled()

Expand Down Expand Up @@ -241,7 +246,7 @@ internal class AttachmentHandlerImplTest {
@Test
fun `when detach() on uploaded attachment`() {
val expectedAttachment =
Attachment(AttachmentValues.Id, AttachmentValues.FileName, State.Detaching)
Attachment(AttachmentValues.Id, AttachmentValues.FileName, null, State.Detaching)
val expectedProcessedAttachment = ProcessedAttachment(expectedAttachment, ByteArray(1))
val expectedDeleteAttachmentRequest =
DeleteAttachmentRequest(TestValues.Token, AttachmentValues.Id)
Expand All @@ -263,7 +268,7 @@ internal class AttachmentHandlerImplTest {
@Test
fun `when detach() on not uploaded attachment`() {
val expectedAttachment =
Attachment(AttachmentValues.Id, AttachmentValues.FileName, State.Detached)
Attachment(AttachmentValues.Id, AttachmentValues.FileName, null, State.Detached)

givenPrepareCalled()

Expand Down Expand Up @@ -292,7 +297,7 @@ internal class AttachmentHandlerImplTest {
@Test
fun `when OnDetached()`() {
val expectedAttachment =
Attachment(AttachmentValues.Id, AttachmentValues.FileName, State.Detached)
Attachment(AttachmentValues.Id, AttachmentValues.FileName, null, State.Detached)
givenPrepareCalled()
givenUploadSuccessCalled()

Expand Down Expand Up @@ -451,7 +456,7 @@ internal class AttachmentHandlerImplTest {
val expectedDownloadUrl = "http://somedownloadurl.com"
val expectedState = State.Sent(expectedDownloadUrl)
val expectedAttachment =
Attachment(AttachmentValues.Id, AttachmentValues.FileName, expectedState)
Attachment(AttachmentValues.Id, AttachmentValues.FileName, AttachmentValues.FileSize, expectedState)
givenPrepareCalled()
givenUploadSuccessCalled()
givenOnSendingCalled()
Expand All @@ -461,6 +466,7 @@ internal class AttachmentHandlerImplTest {
AttachmentValues.Id to Attachment(
AttachmentValues.Id,
AttachmentValues.FileName,
AttachmentValues.FileSize,
State.Sent("http://somedownloadurl.com")
)
)
Expand Down Expand Up @@ -492,6 +498,7 @@ internal class AttachmentHandlerImplTest {
AttachmentValues.Id to Attachment(
AttachmentValues.Id,
AttachmentValues.FileName,
AttachmentValues.FileSize,
State.Sent("http://somedownloadurl.com")
)
)
Expand Down Expand Up @@ -524,7 +531,8 @@ internal class AttachmentHandlerImplTest {

val givenByteArray = ByteArray(2000)

val onAttachmentRequest = subject.prepare(AttachmentValues.Id, givenByteArray, AttachmentValues.FileName)
val onAttachmentRequest =
subject.prepare(AttachmentValues.Id, givenByteArray, AttachmentValues.FileName)

assertThat(subject.fileAttachmentProfile).isNull()
assertThat(onAttachmentRequest).isEqualTo(expectedOnAttachmentRequest)
Expand Down Expand Up @@ -556,7 +564,8 @@ internal class AttachmentHandlerImplTest {
true,
)

val onAttachmentRequest = subject.prepare(AttachmentValues.Id, givenByteArray, AttachmentValues.FileName)
val onAttachmentRequest =
subject.prepare(AttachmentValues.Id, givenByteArray, AttachmentValues.FileName)

assertThat(subject.fileAttachmentProfile?.maxFileSizeKB).isEqualTo(expectedFileSizeInKB)
assertThat(onAttachmentRequest).isEqualTo(expectedOnAttachmentRequest)
Expand Down Expand Up @@ -588,7 +597,8 @@ internal class AttachmentHandlerImplTest {
true,
)

val onAttachmentRequest = subject.prepare(AttachmentValues.Id, givenByteArray, AttachmentValues.FileName)
val onAttachmentRequest =
subject.prepare(AttachmentValues.Id, givenByteArray, AttachmentValues.FileName)

assertThat(subject.fileAttachmentProfile?.maxFileSizeKB).isEqualTo(expectedFileSizeInKB)
assertThat(onAttachmentRequest).isEqualTo(expectedOnAttachmentRequest)
Expand Down Expand Up @@ -629,7 +639,8 @@ internal class AttachmentHandlerImplTest {
true,
)

val onAttachmentRequest = subject.prepare(AttachmentValues.Id, givenByteArray, AttachmentValues.FileName)
val onAttachmentRequest =
subject.prepare(AttachmentValues.Id, givenByteArray, AttachmentValues.FileName)

assertThat(onAttachmentRequest).isEqualTo(expectedOnAttachmentRequest)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.genesys.cloud.messenger.transport.util.extensions.mapOriginatingEntit
import com.genesys.cloud.messenger.transport.util.extensions.toFileAttachmentProfile
import com.genesys.cloud.messenger.transport.util.extensions.toMessage
import com.genesys.cloud.messenger.transport.util.extensions.toMessageList
import com.genesys.cloud.messenger.transport.utility.AttachmentValues
import com.genesys.cloud.messenger.transport.utility.MessageValues
import com.genesys.cloud.messenger.transport.utility.QuickReplyTestValues
import com.genesys.cloud.messenger.transport.utility.StructuredMessageValues
Expand Down Expand Up @@ -171,11 +172,13 @@ internal class MessageExtensionTest {
"first test attachment id" to Attachment(
id = "first test attachment id",
fileName = "test.png",
fileSize = AttachmentValues.FileSize,
Attachment.State.Uploaded("http://test.com")
),
"second test attachment id" to Attachment(
id = "second test attachment id",
fileName = "test2.png",
fileSize = null,
Attachment.State.Detached,
)
)
Expand All @@ -185,6 +188,7 @@ internal class MessageExtensionTest {
attachment = Attachment(
id = "first test attachment id",
fileName = "test.png",
fileSize = AttachmentValues.FileSize,
state = Attachment.State.Uploaded("http://test.com")
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.genesys.cloud.messenger.transport.shyrka.send.TextMessage
import com.genesys.cloud.messenger.transport.util.Request
import com.genesys.cloud.messenger.transport.util.logs.Log
import com.genesys.cloud.messenger.transport.util.logs.LogMessages
import com.genesys.cloud.messenger.transport.utility.AttachmentValues
import com.genesys.cloud.messenger.transport.utility.QuickReplyTestValues
import io.mockk.Called
import io.mockk.clearMocks
Expand Down Expand Up @@ -307,7 +308,7 @@ internal class MessageStoreTest {
fun `when onMessageError() happens after message being Sent`() {
val errorMessage = "some test error message"
val testMessage = "test message"
val expectedState = Message.State.Error(
val expectedState = State.Error(
ErrorCode.MessageTooLong,
errorMessage
)
Expand All @@ -325,8 +326,8 @@ internal class MessageStoreTest {
verify { mockMessageListener.invoke(capture(messageSlot)) }
(messageSlot.captured as MessageEvent.MessageUpdated).message.run {
assertThat(this).isEqualTo(expectedMessage)
assertThat((state as Message.State.Error).code).isEqualTo(expectedState.code)
assertThat((state as Message.State.Error).message).isEqualTo(expectedState.message)
assertThat((state as State.Error).code).isEqualTo(expectedState.code)
assertThat((state as State.Error).message).isEqualTo(expectedState.message)
}
}

Expand Down Expand Up @@ -547,7 +548,7 @@ internal class MessageStoreTest {
private fun attachment(
id: String = "given id",
state: Attachment.State = Attachment.State.Presigning,
) = Attachment(id, "file.png", state)
) = Attachment(id, "file.png", AttachmentValues.FileSize, state)

private fun messageList(size: Int = 5): List<Message> {
val messageList = mutableListOf<Message>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class MCAttachmentTests : BaseMessagingClientTest() {
val expectedAttachment = Attachment(
"attachment_id",
"image.png",
AttachmentValues.FileSize,
Attachment.State.Sent("https://downloadurl.com")
)
val expectedMessage = Message(
Expand Down Expand Up @@ -376,6 +377,7 @@ class MCAttachmentTests : BaseMessagingClientTest() {
val expectedAttachment = Attachment(
"attachment_id",
"image.png",
AttachmentValues.FileSize,
Attachment.State.Sent("https://downloadurl.com")
)
val expectedMessage = Message(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.genesys.cloud.messenger.transport.util

import com.genesys.cloud.messenger.transport.core.Message
import com.genesys.cloud.messenger.transport.utility.AttachmentValues
import com.genesys.cloud.messenger.transport.utility.TestValues
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
Expand All @@ -23,7 +24,7 @@ internal object Response {
fun onMessage(direction: Message.Direction = Message.Direction.Inbound) =
"""{"type":"message","class":"StructuredMessage","code":200,"body":{"text":"Hello world!","direction":"${direction.name}","id":"test_id","channel":{"time":"2022-08-22T19:24:26.704Z","messageId":"message_id"},"type":"Text","metadata":{"customMessageId":"some_custom_message_id"}}}"""
fun onMessageWithAttachment(direction: Message.Direction = Message.Direction.Outbound) =
"""{"type":"message","class":"StructuredMessage","code":200,"body":{"direction":"${direction.name}","id":"msg_id","channel":{"time":"some_time","type":"Private"},"type":"Text","text":"Hi","content":[{"attachment":{"id":"attachment_id","filename":"image.png","mediaType":"Image","mime":"image/png","url":"https://downloadurl.com"},"contentType":"Attachment"}],"originatingEntity":"Human"}}"""
"""{"type":"message","class":"StructuredMessage","code":200,"body":{"direction":"${direction.name}","id":"msg_id","channel":{"time":"some_time","type":"Private"},"type":"Text","text":"Hi","content":[{"attachment":{"id":"attachment_id","filename":"image.png","mediaType":"Image","fileSize":${AttachmentValues.FileSize},"mime":"image/png","url":"https://downloadurl.com"},"contentType":"Attachment"}],"originatingEntity":"Human"}}"""
const val onMessageWithQuickReplies =
"""{"type":"message","class":"StructuredMessage","code":200,"body":{"direction":"Outbound","id":"msg_id","channel":{"time":"some_time","type":"Private"},"type":"Structured","text":"Hi","content":[{"contentType":"QuickReply","quickReply":{"text":"text_a","payload":"payload_a","action":"action_a"}},{"contentType":"QuickReply","quickReply":{"text":"text_b","payload":"payload_b","action":"action_b"}}],"originatingEntity":"Bot"}}"""
const val onMessageWithoutQuickReplies =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlinx.serialization.Transient
data class Attachment(
val id: String,
@Transient val fileName: String? = null,
@Transient val fileSize: Int? = null,
@Transient val state: State = State.Presigning,
) {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ private fun List<AttachmentContent>.toAttachments(): Map<String, Attachment> {
attachment.id to Attachment(
id = attachment.id,
fileName = attachment.filename,
fileSize = attachment.fileSize,
state = Attachment.State.Sent(attachment.url),
)
}
Expand Down

0 comments on commit 39d89d9

Please sign in to comment.