Skip to content

Commit b52cba7

Browse files
authored
Fix/attachment url parser (#32)
* add test for attachments with numbers at the beginning * fix attachment url pattern
1 parent 58cce6e commit b52cba7

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

kotlin-insight-client/kotlin-insight-client-sdk/src/main/kotlin/com/linkedplanet/kotlininsightclient/sdk/SdkInsightAttachmentOperator.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package com.linkedplanet.kotlininsightclient.sdk
2222
import arrow.core.Either
2323
import arrow.core.raise.either
2424
import com.linkedplanet.kotlininsightclient.api.error.InsightClientError
25+
import com.linkedplanet.kotlininsightclient.api.error.OtherNotFoundError
2526
import com.linkedplanet.kotlininsightclient.api.interfaces.InsightAttachmentOperator
2627
import com.linkedplanet.kotlininsightclient.api.model.AttachmentId
2728
import com.linkedplanet.kotlininsightclient.api.model.InsightAttachment
@@ -66,7 +67,7 @@ object SdkInsightAttachmentOperator : InsightAttachmentOperator {
6667
val attachmentId = attachmentUrlResolver.parseAttachmentIdFromPathInformation(url)
6768
val attachmentBean = objectFacade.loadAttachmentBeanById(attachmentId)
6869
fileManager.getObjectAttachmentContent(attachmentBean.objectId, attachmentBean.nameInFileSystem)
69-
}
70+
}.mapLeft { OtherNotFoundError("Attachment download failed for url:$url") }
7071

7172
override suspend fun downloadAttachmentZip(objectId: InsightObjectId): Either<InsightClientError, InputStream> =
7273
either {

kotlin-insight-client/kotlin-insight-client-sdk/src/main/kotlin/com/linkedplanet/kotlininsightclient/sdk/services/ReverseEngineeredAttachmentUrlResolver.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal class ReverseEngineeredAttachmentUrlResolver {
3434

3535
private val applicationProperties: ApplicationProperties by getComponent()
3636

37-
private val pattern = Pattern.compile(".*/(\\d+)/?")
37+
private val pattern = Pattern.compile(".*/(\\d+)(/[^/]*)?$")
3838
private val INSIGHT_REST_BASE_URL = "/rest/insight/1.0"
3939

4040
private fun baseUrl(): String = applicationProperties.jiraBaseUrl

kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/InsightAttachmentOperatorTest.kt

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ interface InsightAttachmentOperatorTest {
5757
assertThat(firstAttachment.created, endsWith(":06:08.208Z")) // 7 works fine locally and should be correct,
5858
assertThat(firstAttachment.created, startsWith("2023-02-21T0")) // but github pipeline insists on 8 o'clock
5959

60-
val downloadContent = insightAttachmentOperator.downloadAttachment(attachments.first().url).orNull()!!
60+
val downloadContent = insightAttachmentOperator.downloadAttachment(attachments.first().url).getOrNull()!!
6161
val sha256HashIS = calculateSha256(downloadContent.readBytes())
6262
assertThat(sha256HashIS, equalTo("fd411837a51c43670e8d7367e64f72dbbcda5016f59988547c12d067505ef75b"))
6363
}
@@ -67,34 +67,39 @@ interface InsightAttachmentOperatorTest {
6767

6868
@Test
6969
fun attachmentTestAttachmentCRUD() = runBlocking {
70-
insightObjectOperator.makeSureObjectWithNameDoesNotExist(Country.id, "Attachistan")
71-
try {
72-
val disclaimer = "created by Test and should only exist during test run. Deutsches ß und ä."
73-
val country = insightObjectOperator.createObject(
74-
Country.id,
75-
CountryName.attributeId toValue "Attachistan",
76-
CountryShortName.attributeId toValue disclaimer,
77-
toDomain = ::identity
78-
).orFail()
79-
80-
val attachment = insightAttachmentOperator.uploadAttachment(
81-
country.id, "attachistan.txt", "content".byteInputStream()
82-
).orFail()
83-
84-
assertThat(attachment.filename, equalTo("attachistan.txt"))
85-
86-
val downloadContent = insightAttachmentOperator.downloadAttachment(attachment.url).orFail()
87-
val downloadContentString = String(downloadContent.readBytes())
88-
assertThat(downloadContentString, equalTo("content"))
89-
90-
insightAttachmentOperator.deleteAttachment(attachment.id).orFail()
91-
assertThat(insightAttachmentOperator.downloadAttachment(attachment.url).isLeft(), equalTo(true))
92-
} finally {
93-
insightObjectOperator.makeSureObjectWithNameDoesNotExist(Country.id, "Attachistan")
70+
mapOf(
71+
"AttachmentWithSimpleName" to "simple.txt",
72+
"AttachmentWithNumber" to "01231515_a_1241.txt",
73+
"AttachmentWithImg" to "cover-L.jpg",
74+
).forEach { (attachmentName, fileName) ->
75+
insightObjectOperator.makeSureObjectWithNameDoesNotExist(Country.id, attachmentName)
76+
try {
77+
val disclaimer = "created by Test and should only exist during test run. Deutsches ß und ä."
78+
val country = insightObjectOperator.createObject(
79+
Country.id,
80+
CountryName.attributeId toValue attachmentName,
81+
CountryShortName.attributeId toValue disclaimer,
82+
toDomain = ::identity
83+
).orFail()
84+
85+
val attachment = insightAttachmentOperator.uploadAttachment(
86+
country.id, fileName, "content".byteInputStream()
87+
).orFail()
88+
89+
assertThat(attachment.filename, equalTo(fileName))
90+
91+
val downloadContent = insightAttachmentOperator.downloadAttachment(attachment.url).orFail()
92+
val downloadContentString = String(downloadContent.readBytes())
93+
assertThat(downloadContentString, equalTo("content"))
94+
95+
insightAttachmentOperator.deleteAttachment(attachment.id).orFail()
96+
assertThat(insightAttachmentOperator.downloadAttachment(attachment.url).isLeft(), equalTo(true))
97+
} finally {
98+
insightObjectOperator.makeSureObjectWithNameDoesNotExist(Country.id, attachmentName)
99+
}
94100
}
95101
}
96102

97-
98103
@Test
99104
fun attachmentTestGetAttachmentsForNotExistingObject() = runBlocking {
100105
val responseError = insightAttachmentOperator.getAttachments(InsightObjectId.notPersistedObjectId).asError()

0 commit comments

Comments
 (0)