diff --git a/kotlin-insight-client/kotlin-insight-client-sdk/src/main/kotlin/com/linkedplanet/kotlininsightclient/sdk/SdkInsightAttachmentOperator.kt b/kotlin-insight-client/kotlin-insight-client-sdk/src/main/kotlin/com/linkedplanet/kotlininsightclient/sdk/SdkInsightAttachmentOperator.kt index 3eb6d079..0672ecc8 100644 --- a/kotlin-insight-client/kotlin-insight-client-sdk/src/main/kotlin/com/linkedplanet/kotlininsightclient/sdk/SdkInsightAttachmentOperator.kt +++ b/kotlin-insight-client/kotlin-insight-client-sdk/src/main/kotlin/com/linkedplanet/kotlininsightclient/sdk/SdkInsightAttachmentOperator.kt @@ -22,6 +22,7 @@ package com.linkedplanet.kotlininsightclient.sdk import arrow.core.Either import arrow.core.raise.either import com.linkedplanet.kotlininsightclient.api.error.InsightClientError +import com.linkedplanet.kotlininsightclient.api.error.OtherNotFoundError import com.linkedplanet.kotlininsightclient.api.interfaces.InsightAttachmentOperator import com.linkedplanet.kotlininsightclient.api.model.AttachmentId import com.linkedplanet.kotlininsightclient.api.model.InsightAttachment @@ -66,7 +67,7 @@ object SdkInsightAttachmentOperator : InsightAttachmentOperator { val attachmentId = attachmentUrlResolver.parseAttachmentIdFromPathInformation(url) val attachmentBean = objectFacade.loadAttachmentBeanById(attachmentId) fileManager.getObjectAttachmentContent(attachmentBean.objectId, attachmentBean.nameInFileSystem) - } + }.mapLeft { OtherNotFoundError("Attachment download failed for url:$url") } override suspend fun downloadAttachmentZip(objectId: InsightObjectId): Either = either { diff --git a/kotlin-insight-client/kotlin-insight-client-sdk/src/main/kotlin/com/linkedplanet/kotlininsightclient/sdk/services/ReverseEngineeredAttachmentUrlResolver.kt b/kotlin-insight-client/kotlin-insight-client-sdk/src/main/kotlin/com/linkedplanet/kotlininsightclient/sdk/services/ReverseEngineeredAttachmentUrlResolver.kt index 1a1471a7..04d67309 100644 --- a/kotlin-insight-client/kotlin-insight-client-sdk/src/main/kotlin/com/linkedplanet/kotlininsightclient/sdk/services/ReverseEngineeredAttachmentUrlResolver.kt +++ b/kotlin-insight-client/kotlin-insight-client-sdk/src/main/kotlin/com/linkedplanet/kotlininsightclient/sdk/services/ReverseEngineeredAttachmentUrlResolver.kt @@ -34,7 +34,7 @@ internal class ReverseEngineeredAttachmentUrlResolver { private val applicationProperties: ApplicationProperties by getComponent() - private val pattern = Pattern.compile(".*/(\\d+)/?") + private val pattern = Pattern.compile(".*/(\\d+)(/[^/]*)?$") private val INSIGHT_REST_BASE_URL = "/rest/insight/1.0" private fun baseUrl(): String = applicationProperties.jiraBaseUrl diff --git a/kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/InsightAttachmentOperatorTest.kt b/kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/InsightAttachmentOperatorTest.kt index dd906a0a..be6e2137 100644 --- a/kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/InsightAttachmentOperatorTest.kt +++ b/kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/InsightAttachmentOperatorTest.kt @@ -57,7 +57,7 @@ interface InsightAttachmentOperatorTest { assertThat(firstAttachment.created, endsWith(":06:08.208Z")) // 7 works fine locally and should be correct, assertThat(firstAttachment.created, startsWith("2023-02-21T0")) // but github pipeline insists on 8 o'clock - val downloadContent = insightAttachmentOperator.downloadAttachment(attachments.first().url).orNull()!! + val downloadContent = insightAttachmentOperator.downloadAttachment(attachments.first().url).getOrNull()!! val sha256HashIS = calculateSha256(downloadContent.readBytes()) assertThat(sha256HashIS, equalTo("fd411837a51c43670e8d7367e64f72dbbcda5016f59988547c12d067505ef75b")) } @@ -67,34 +67,39 @@ interface InsightAttachmentOperatorTest { @Test fun attachmentTestAttachmentCRUD() = runBlocking { - insightObjectOperator.makeSureObjectWithNameDoesNotExist(Country.id, "Attachistan") - try { - val disclaimer = "created by Test and should only exist during test run. Deutsches ß und ä." - val country = insightObjectOperator.createObject( - Country.id, - CountryName.attributeId toValue "Attachistan", - CountryShortName.attributeId toValue disclaimer, - toDomain = ::identity - ).orFail() - - val attachment = insightAttachmentOperator.uploadAttachment( - country.id, "attachistan.txt", "content".byteInputStream() - ).orFail() - - assertThat(attachment.filename, equalTo("attachistan.txt")) - - val downloadContent = insightAttachmentOperator.downloadAttachment(attachment.url).orFail() - val downloadContentString = String(downloadContent.readBytes()) - assertThat(downloadContentString, equalTo("content")) - - insightAttachmentOperator.deleteAttachment(attachment.id).orFail() - assertThat(insightAttachmentOperator.downloadAttachment(attachment.url).isLeft(), equalTo(true)) - } finally { - insightObjectOperator.makeSureObjectWithNameDoesNotExist(Country.id, "Attachistan") + mapOf( + "AttachmentWithSimpleName" to "simple.txt", + "AttachmentWithNumber" to "01231515_a_1241.txt", + "AttachmentWithImg" to "cover-L.jpg", + ).forEach { (attachmentName, fileName) -> + insightObjectOperator.makeSureObjectWithNameDoesNotExist(Country.id, attachmentName) + try { + val disclaimer = "created by Test and should only exist during test run. Deutsches ß und ä." + val country = insightObjectOperator.createObject( + Country.id, + CountryName.attributeId toValue attachmentName, + CountryShortName.attributeId toValue disclaimer, + toDomain = ::identity + ).orFail() + + val attachment = insightAttachmentOperator.uploadAttachment( + country.id, fileName, "content".byteInputStream() + ).orFail() + + assertThat(attachment.filename, equalTo(fileName)) + + val downloadContent = insightAttachmentOperator.downloadAttachment(attachment.url).orFail() + val downloadContentString = String(downloadContent.readBytes()) + assertThat(downloadContentString, equalTo("content")) + + insightAttachmentOperator.deleteAttachment(attachment.id).orFail() + assertThat(insightAttachmentOperator.downloadAttachment(attachment.url).isLeft(), equalTo(true)) + } finally { + insightObjectOperator.makeSureObjectWithNameDoesNotExist(Country.id, attachmentName) + } } } - @Test fun attachmentTestGetAttachmentsForNotExistingObject() = runBlocking { val responseError = insightAttachmentOperator.getAttachments(InsightObjectId.notPersistedObjectId).asError()