From f6b4295e75aef7b884ecb5e7ac43dc9c199bfaa6 Mon Sep 17 00:00:00 2001 From: aafanasi Date: Wed, 22 May 2024 13:02:41 -0300 Subject: [PATCH] MTSDK-395 - Expose File Size - Add fileSize field to Attachment object - Add/Update unit tests --- .../core/AttachmentHandlerImplTest.kt | 39 ++++++++++++------- .../transport/core/MessageExtensionTest.kt | 4 ++ .../kotlin/transport/core/MessageStoreTest.kt | 9 +++-- .../core/messagingclient/MCAttachmentTests.kt | 2 + .../kotlin/transport/util/Response.kt | 3 +- .../messenger/transport/core/Attachment.kt | 1 + .../util/extensions/MessageExtension.kt | 1 + 7 files changed, 40 insertions(+), 19 deletions(-) diff --git a/transport/src/androidUnitTest/kotlin/transport/core/AttachmentHandlerImplTest.kt b/transport/src/androidUnitTest/kotlin/transport/core/AttachmentHandlerImplTest.kt index ae0a982f..d7a525ce 100644 --- a/transport/src/androidUnitTest/kotlin/transport/core/AttachmentHandlerImplTest.kt +++ b/transport/src/androidUnitTest/kotlin/transport/core/AttachmentHandlerImplTest.kt @@ -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)) @@ -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() givenPrepareCalled(uploadProgress = mockUploadProgress) @@ -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() @@ -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) @@ -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() @@ -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() @@ -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() @@ -461,6 +466,7 @@ internal class AttachmentHandlerImplTest { AttachmentValues.Id to Attachment( AttachmentValues.Id, AttachmentValues.FileName, + AttachmentValues.FileSize, State.Sent("http://somedownloadurl.com") ) ) @@ -492,6 +498,7 @@ internal class AttachmentHandlerImplTest { AttachmentValues.Id to Attachment( AttachmentValues.Id, AttachmentValues.FileName, + AttachmentValues.FileSize, State.Sent("http://somedownloadurl.com") ) ) @@ -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) @@ -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) @@ -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) @@ -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) } diff --git a/transport/src/androidUnitTest/kotlin/transport/core/MessageExtensionTest.kt b/transport/src/androidUnitTest/kotlin/transport/core/MessageExtensionTest.kt index ce7d17a6..6fb6ae49 100644 --- a/transport/src/androidUnitTest/kotlin/transport/core/MessageExtensionTest.kt +++ b/transport/src/androidUnitTest/kotlin/transport/core/MessageExtensionTest.kt @@ -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 @@ -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, ) ) @@ -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") ) ) diff --git a/transport/src/androidUnitTest/kotlin/transport/core/MessageStoreTest.kt b/transport/src/androidUnitTest/kotlin/transport/core/MessageStoreTest.kt index 9849e89c..2a9d8430 100644 --- a/transport/src/androidUnitTest/kotlin/transport/core/MessageStoreTest.kt +++ b/transport/src/androidUnitTest/kotlin/transport/core/MessageStoreTest.kt @@ -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 @@ -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 ) @@ -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) } } @@ -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 { val messageList = mutableListOf() diff --git a/transport/src/androidUnitTest/kotlin/transport/core/messagingclient/MCAttachmentTests.kt b/transport/src/androidUnitTest/kotlin/transport/core/messagingclient/MCAttachmentTests.kt index 537b3193..4ab50bc9 100644 --- a/transport/src/androidUnitTest/kotlin/transport/core/messagingclient/MCAttachmentTests.kt +++ b/transport/src/androidUnitTest/kotlin/transport/core/messagingclient/MCAttachmentTests.kt @@ -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( @@ -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( diff --git a/transport/src/androidUnitTest/kotlin/transport/util/Response.kt b/transport/src/androidUnitTest/kotlin/transport/util/Response.kt index 5dac843b..a7a0e999 100644 --- a/transport/src/androidUnitTest/kotlin/transport/util/Response.kt +++ b/transport/src/androidUnitTest/kotlin/transport/util/Response.kt @@ -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 @@ -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 = diff --git a/transport/src/commonMain/kotlin/com/genesys/cloud/messenger/transport/core/Attachment.kt b/transport/src/commonMain/kotlin/com/genesys/cloud/messenger/transport/core/Attachment.kt index 1ef940ee..aa69133c 100644 --- a/transport/src/commonMain/kotlin/com/genesys/cloud/messenger/transport/core/Attachment.kt +++ b/transport/src/commonMain/kotlin/com/genesys/cloud/messenger/transport/core/Attachment.kt @@ -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, ) { /** diff --git a/transport/src/commonMain/kotlin/com/genesys/cloud/messenger/transport/util/extensions/MessageExtension.kt b/transport/src/commonMain/kotlin/com/genesys/cloud/messenger/transport/util/extensions/MessageExtension.kt index 3de5170b..5b49e711 100644 --- a/transport/src/commonMain/kotlin/com/genesys/cloud/messenger/transport/util/extensions/MessageExtension.kt +++ b/transport/src/commonMain/kotlin/com/genesys/cloud/messenger/transport/util/extensions/MessageExtension.kt @@ -78,6 +78,7 @@ private fun List.toAttachments(): Map { attachment.id to Attachment( id = attachment.id, fileName = attachment.filename, + fileSize = attachment.fileSize, state = Attachment.State.Sent(attachment.url), ) }