diff --git a/kotlin-insight-client/kotlin-insight-client-api/src/main/kotlin/com/linkedplanet/kotlininsightclient/api/model/InsightObjectExtensions.kt b/kotlin-insight-client/kotlin-insight-client-api/src/main/kotlin/com/linkedplanet/kotlininsightclient/api/model/InsightObjectExtensions.kt index 4201bb5a..717c531c 100644 --- a/kotlin-insight-client/kotlin-insight-client-api/src/main/kotlin/com/linkedplanet/kotlininsightclient/api/model/InsightObjectExtensions.kt +++ b/kotlin-insight-client/kotlin-insight-client-api/src/main/kotlin/com/linkedplanet/kotlininsightclient/api/model/InsightObjectExtensions.kt @@ -21,15 +21,16 @@ package com.linkedplanet.kotlininsightclient.api.model +import java.time.LocalDate import java.time.ZonedDateTime fun InsightObject.getAttribute(id: InsightAttributeId): InsightAttribute? = this.attributes.singleOrNull { it.attributeId == id } -inline fun InsightObject.getAttributeValue(id: InsightAttributeId): T? = +inline fun InsightObject.getAttributeAs(id: InsightAttributeId): T? = getAttribute(id) as? T -inline fun InsightObject.getAttributeValueByName(name: String): T? = +inline fun InsightObject.getAttributeByNameAs(name: String): T? = getAttributeByName(name) as? T fun InsightObject.getAttributeIdByName(name: String) = getAttributeByName(name)?.attributeId @@ -46,9 +47,18 @@ fun InsightObject.isValueAttribute(id: InsightAttributeId): Boolean = fun InsightObject.exists(id: InsightAttributeId): Boolean = getAttribute(id) != null +fun InsightObject.setUrlValues(attributeId: InsightAttributeId, values: List) { + this.attributes = attributes + .filter { it.attributeId != attributeId } + + InsightAttribute.Url(attributeId, values, null) +} + +fun InsightObject.getUrlValues(id: InsightAttributeId): List = + getAttributeAs(id)?.values ?: emptyList() + // region ObjectAttributeValue.Select fun InsightObject.getSelectValues(id: InsightAttributeId): List = - getAttributeValue(id)?.values ?: emptyList() + getAttributeAs(id)?.values ?: emptyList() fun InsightObject.setSelectValues(attributeId: InsightAttributeId, values: List) { this.attributes = attributes @@ -94,8 +104,12 @@ fun InsightObject.setValue(id: InsightAttributeId, value: Double?) { setValue(id, InsightAttribute.DoubleNumber(id, value, null)) } -fun InsightObject.setValue(id: InsightAttributeId, value: ZonedDateTime?, displayValue: String) { - setValue(id, InsightAttribute.DateTime(id, value, displayValue, null)) +fun InsightObject.setValue(id: InsightAttributeId, value: LocalDate?) { + setValue(id, InsightAttribute.Date(id, value, null, null)) +} + +fun InsightObject.setValue(id: InsightAttributeId, value: ZonedDateTime?) { + setValue(id, InsightAttribute.DateTime(id, value, null, null)) } // endregion setters @@ -111,24 +125,22 @@ fun InsightObject.getValueByName(name: String, transform: (InsightAttribute) fun InsightObject.getStringValue(id: InsightAttributeId): String? = this.getValue(id) { it.toString() } - fun InsightObject.getIntValue(id: InsightAttributeId): Int? = - getStringValue(id)?.toInt() - + getAttributeAs(id)?.value fun InsightObject.getDoubleValue(id: InsightAttributeId): Double? = - getStringValue(id)?.toDouble() - + getAttributeAs(id)?.value fun InsightObject.getBooleanValue(id: InsightAttributeId): Boolean? = - getStringValue(id)?.toBoolean() - + getAttributeAs(id)?.value +fun InsightObject.getDateValue(id: InsightAttributeId): LocalDate? = + getAttributeAs(id)?.value fun InsightObject.getDateTimeValue(id: InsightAttributeId): ZonedDateTime? = - getStringValue(id)?.let { ZonedDateTime.parse(it) } + getAttributeAs(id)?.value //endregion getters //region ObjectAttributeValue.User fun InsightObject.getUserList(id: InsightAttributeId): List = - getAttributeValue(id)?.users ?: emptyList() + getAttributeAs(id)?.users ?: emptyList() // endregion user @@ -164,7 +176,7 @@ fun InsightObject.getMultiReferenceValue(id: InsightAttributeId): List(attributeId)?.referencedObjects ?: emptyList() + val existingList = getAttributeAs(attributeId)?.referencedObjects ?: emptyList() val referenceAttributeList = existingList.filter { it.id != referencedObjectId } this.attributes = attributes .filter { it.attributeId != attributeId } + @@ -178,7 +190,7 @@ fun InsightObject.clearReferenceValue(attributeId: InsightAttributeId) { } fun InsightObject.addReference(attributeId: InsightAttributeId, referencedObjectId: InsightObjectId) { - val existingList = getAttributeValue(attributeId)?.referencedObjects ?: emptyList() + val existingList = getAttributeAs(attributeId)?.referencedObjects ?: emptyList() val referenceAttributeList = existingList + ReferencedObject(referencedObjectId, "", "", null) this.attributes = attributes .filter { it.attributeId != attributeId } + diff --git a/kotlin-insight-client/kotlin-insight-client-api/src/main/kotlin/com/linkedplanet/kotlininsightclient/api/model/Model.kt b/kotlin-insight-client/kotlin-insight-client-api/src/main/kotlin/com/linkedplanet/kotlininsightclient/api/model/Model.kt index 0e9f1f3d..a54a3541 100644 --- a/kotlin-insight-client/kotlin-insight-client-api/src/main/kotlin/com/linkedplanet/kotlininsightclient/api/model/Model.kt +++ b/kotlin-insight-client/kotlin-insight-client-api/src/main/kotlin/com/linkedplanet/kotlininsightclient/api/model/Model.kt @@ -164,11 +164,12 @@ sealed class InsightAttribute( is Reference -> referencedObjects.joinToString(",") { it.objectKey } is User -> users.joinToString(",") { it.key } - is Group -> "" // TODO - is Project -> "" - is Status -> "" - is Version -> "" - is Confluence -> "" + // TODO support additional attribute types + is Group -> "Group attributeId=$attributeId" + is Project -> "Project attributeId=$attributeId" + is Status -> "Status attributeId=$attributeId" + is Version -> "Version attributeId=$attributeId" + is Confluence -> "Confluence attributeId=$attributeId" is Unknown -> "" } @@ -201,6 +202,12 @@ sealed class InsightAttribute( infix fun InsightAttributeId.toUrlValues(values: List) = Url(this, values = values, schema = null) + infix fun InsightAttributeId.toUser(user: InsightUser?) = + User(this, listOfNotNull(user), schema = null) + + infix fun InsightAttributeId.toUsers(users: List) = + User(this, users, schema = null) + infix fun InsightAttributeId.toReference(referencedObjectId: InsightObjectId?) = Reference( this, diff --git a/kotlin-insight-client/kotlin-insight-client-http/src/main/kotlin/com/linkedplanet/kotlininsightclient/http/HttpInsightObjectOperator.kt b/kotlin-insight-client/kotlin-insight-client-http/src/main/kotlin/com/linkedplanet/kotlininsightclient/http/HttpInsightObjectOperator.kt index 4b60ce26..ea11f715 100644 --- a/kotlin-insight-client/kotlin-insight-client-http/src/main/kotlin/com/linkedplanet/kotlininsightclient/http/HttpInsightObjectOperator.kt +++ b/kotlin-insight-client/kotlin-insight-client-http/src/main/kotlin/com/linkedplanet/kotlininsightclient/http/HttpInsightObjectOperator.kt @@ -311,11 +311,11 @@ class HttpInsightObjectOperator(private val context: HttpInsightClientContext) : } InsightAttribute.User(attributeId, users, schema) } - InsightObjectAttributeType.CONFLUENCE -> TODO() - InsightObjectAttributeType.GROUP -> TODO() - InsightObjectAttributeType.VERSION -> TODO() - InsightObjectAttributeType.PROJECT -> TODO() - InsightObjectAttributeType.STATUS -> TODO() + InsightObjectAttributeType.CONFLUENCE -> InsightAttribute.Confluence(attributeId, schema) + InsightObjectAttributeType.GROUP -> InsightAttribute.Group(attributeId, schema) + InsightObjectAttributeType.VERSION -> InsightAttribute.Version(attributeId, schema) + InsightObjectAttributeType.PROJECT -> InsightAttribute.Project(attributeId, schema) + InsightObjectAttributeType.STATUS -> InsightAttribute.Status(attributeId, schema) else -> internalError("Unsupported objectTypeAttributeBean.type (${attributeType})").bind() } } diff --git a/kotlin-insight-client/kotlin-insight-client-http/src/main/kotlin/com/linkedplanet/kotlininsightclient/http/model/HttpEditModel.kt b/kotlin-insight-client/kotlin-insight-client-http/src/main/kotlin/com/linkedplanet/kotlininsightclient/http/model/HttpEditModel.kt index 651479d0..c0a5f370 100644 --- a/kotlin-insight-client/kotlin-insight-client-http/src/main/kotlin/com/linkedplanet/kotlininsightclient/http/model/HttpEditModel.kt +++ b/kotlin-insight-client/kotlin-insight-client-http/src/main/kotlin/com/linkedplanet/kotlininsightclient/http/model/HttpEditModel.kt @@ -53,14 +53,16 @@ internal fun InsightObject.getEditAttributes(): List = is InsightAttribute.Select -> attr.values is InsightAttribute.Reference -> attr.referencedObjects.map { it.id.raw } - is InsightAttribute.User -> attr.users.map { it.key } //TODO: needs a test + is InsightAttribute.User -> attr.users.map { it.key } - is InsightAttribute.Group -> TODO() - is InsightAttribute.Project -> TODO() - is InsightAttribute.Status -> TODO() - is InsightAttribute.Version -> TODO() - is InsightAttribute.Confluence -> TODO() - is InsightAttribute.Unknown -> TODO() + // TODO support additional attribute types + is InsightAttribute.Group -> emptyList() + is InsightAttribute.Project -> emptyList() + is InsightAttribute.Status -> emptyList() + is InsightAttribute.Version -> emptyList() + is InsightAttribute.Confluence -> emptyList() + + is InsightAttribute.Unknown -> emptyList() } ObjectEditItemAttribute( diff --git a/kotlin-insight-client/kotlin-insight-client-sdk/src/main/kotlin/com/linkedplanet/kotlininsightclient/sdk/SdkInsightObjectOperator.kt b/kotlin-insight-client/kotlin-insight-client-sdk/src/main/kotlin/com/linkedplanet/kotlininsightclient/sdk/SdkInsightObjectOperator.kt index dcff301c..8d61f4a2 100644 --- a/kotlin-insight-client/kotlin-insight-client-sdk/src/main/kotlin/com/linkedplanet/kotlininsightclient/sdk/SdkInsightObjectOperator.kt +++ b/kotlin-insight-client/kotlin-insight-client-sdk/src/main/kotlin/com/linkedplanet/kotlininsightclient/sdk/SdkInsightObjectOperator.kt @@ -225,12 +225,13 @@ object SdkInsightObjectOperator : InsightObjectOperator { objectAttributeBeanFactory.createUserAttributeValueByKey(ota, *userKeys.toTypedArray()) } - is InsightAttribute.Group -> TODO() - is InsightAttribute.Project -> TODO() - is InsightAttribute.Status -> TODO() - is InsightAttribute.Version -> TODO() - is InsightAttribute.Confluence -> TODO() - is InsightAttribute.Unknown -> TODO() + // TODO support additional attribute types + is InsightAttribute.Group -> objectAttributeBeanFactory.createObjectAttributeBeanForObject(bean, ota) + is InsightAttribute.Project -> objectAttributeBeanFactory.createObjectAttributeBeanForObject(bean, ota) + is InsightAttribute.Status -> objectAttributeBeanFactory.createObjectAttributeBeanForObject(bean, ota) + is InsightAttribute.Version -> objectAttributeBeanFactory.createObjectAttributeBeanForObject(bean, ota) + is InsightAttribute.Confluence -> objectAttributeBeanFactory.createObjectAttributeBeanForObject(bean, ota) + is InsightAttribute.Unknown -> objectAttributeBeanFactory.createObjectAttributeBeanForObject(bean, ota) } } bean.setObjectAttributeBeans(attributeBeans) @@ -368,11 +369,11 @@ object SdkInsightObjectOperator : InsightObjectOperator { } InsightAttribute.User(attributeId, users, schema) } - Type.CONFLUENCE -> TODO() - Type.GROUP -> TODO() - Type.VERSION -> TODO() - Type.PROJECT -> TODO() - Type.STATUS -> TODO() + Type.CONFLUENCE -> InsightAttribute.Confluence(attributeId, schema) + Type.GROUP -> InsightAttribute.Group(attributeId, schema) + Type.VERSION -> InsightAttribute.Version(attributeId, schema) + Type.PROJECT -> InsightAttribute.Project(attributeId, schema) + Type.STATUS -> InsightAttribute.Status(attributeId, schema) else -> internalError("Unsupported objectTypeAttributeBean.type (${objectTypeAttributeBean.type})").bind() } } diff --git a/kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/InsightObjectOperatorTest.kt b/kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/InsightObjectOperatorTest.kt index e4e78ce1..ed2edf9a 100644 --- a/kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/InsightObjectOperatorTest.kt +++ b/kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/InsightObjectOperatorTest.kt @@ -19,27 +19,42 @@ */ package com.linkedplanet.kotlininsightclient +import arrow.core.Either import com.linkedplanet.kotlininsightclient.AuthenticatedJiraHttpClientFactory.Companion.Credentials +import com.linkedplanet.kotlininsightclient.ObjectWithAllDefaultTypesAttributeIds.* import com.linkedplanet.kotlininsightclient.TestAttributes.* +import com.linkedplanet.kotlininsightclient.api.error.InsightClientError import com.linkedplanet.kotlininsightclient.api.interfaces.InsightObjectOperator import com.linkedplanet.kotlininsightclient.api.interfaces.InsightObjectTypeOperator import com.linkedplanet.kotlininsightclient.api.interfaces.InsightSchemaOperator import com.linkedplanet.kotlininsightclient.api.interfaces.identity +import com.linkedplanet.kotlininsightclient.api.model.InsightAttribute import com.linkedplanet.kotlininsightclient.api.model.InsightAttribute.Companion.toReference +import com.linkedplanet.kotlininsightclient.api.model.InsightAttribute.Companion.toUser +import com.linkedplanet.kotlininsightclient.api.model.InsightAttribute.Companion.toUsers import com.linkedplanet.kotlininsightclient.api.model.InsightAttribute.Companion.toValue import com.linkedplanet.kotlininsightclient.api.model.InsightObjectId import com.linkedplanet.kotlininsightclient.api.model.InsightObjectTypeId -import com.linkedplanet.kotlininsightclient.api.model.InsightAttribute +import com.linkedplanet.kotlininsightclient.api.model.InsightUser import com.linkedplanet.kotlininsightclient.api.model.ObjectTypeSchemaAttribute import com.linkedplanet.kotlininsightclient.api.model.addSelectValue +import com.linkedplanet.kotlininsightclient.api.model.getAttributeAs import com.linkedplanet.kotlininsightclient.api.model.getAttributeByName import com.linkedplanet.kotlininsightclient.api.model.getAttributeIdByName +import com.linkedplanet.kotlininsightclient.api.model.getBooleanValue +import com.linkedplanet.kotlininsightclient.api.model.getDateTimeValue +import com.linkedplanet.kotlininsightclient.api.model.getDateValue +import com.linkedplanet.kotlininsightclient.api.model.getDoubleValue +import com.linkedplanet.kotlininsightclient.api.model.getIntValue import com.linkedplanet.kotlininsightclient.api.model.getMultiReferenceValue import com.linkedplanet.kotlininsightclient.api.model.getSelectValues import com.linkedplanet.kotlininsightclient.api.model.getSingleReferenceValue import com.linkedplanet.kotlininsightclient.api.model.getStringValue +import com.linkedplanet.kotlininsightclient.api.model.getUrlValues import com.linkedplanet.kotlininsightclient.api.model.getUserList import com.linkedplanet.kotlininsightclient.api.model.removeSelectValue +import com.linkedplanet.kotlininsightclient.api.model.setSelectValues +import com.linkedplanet.kotlininsightclient.api.model.setUrlValues import com.linkedplanet.kotlininsightclient.api.model.setValue import com.linkedplanet.kotlininsightclient.repositories.CompanyRepositoryBasedOnNameMapping import com.linkedplanet.kotlininsightclient.repositories.CompanyTestRepositoryBasedOnAbstractImpl @@ -145,23 +160,12 @@ interface InsightObjectOperatorTest { assertThat(firstCompany.label, equalTo("Test GmbH")) // Name - assertThat(firstCompany.getAttributeByName(CompanyName.attributeName), notNullValue()) - assertThat( - firstCompany.getAttributeIdByName(CompanyName.attributeName), - equalTo(CompanyName.attributeId) - ) + assertThat(firstCompany.getAttributeIdByName(CompanyName.attributeName), equalTo(CompanyName.attributeId)) assertThat(firstCompany.getStringValue(CompanyName.attributeId), equalTo("Test GmbH")) // Country - assertThat(firstCompany.getAttributeByName(CompanyCountry.attributeName), notNullValue()) - assertThat( - firstCompany.getAttributeIdByName(CompanyCountry.attributeName), - equalTo(CompanyCountry.attributeId) - ) - assertThat( - firstCompany.getSingleReferenceValue(CompanyCountry.attributeId)!!.objectName, - equalTo("Germany") - ) + assertThat(firstCompany.getAttributeIdByName(CompanyCountry.attributeName), equalTo(CompanyCountry.attributeId)) + assertThat(firstCompany.getSingleReferenceValue(CompanyCountry.attributeId)!!.objectName, equalTo("Germany")) assertThat(firstCompany.attachmentsExist, equalTo(false)) val secondCompany = companies.firstOrNull { it.id == InsightObjectId(2) } @@ -171,15 +175,10 @@ interface InsightObjectOperatorTest { assertThat(secondCompany.label, equalTo("Test AG")) // Name - assertThat(secondCompany.getAttributeByName(CompanyName.attributeName), notNullValue()) - assertThat( - secondCompany.getAttributeIdByName(CompanyName.attributeName), - equalTo(CompanyName.attributeId) - ) + assertThat(secondCompany.getAttributeIdByName(CompanyName.attributeName), equalTo(CompanyName.attributeId)) assertThat(secondCompany.getStringValue(CompanyName.attributeId), equalTo("Test AG")) // Country - assertThat(secondCompany.getAttributeByName(CompanyCountry.attributeName), notNullValue()) assertThat( secondCompany.getAttributeIdByName(CompanyCountry.attributeName), equalTo(CompanyCountry.attributeId) @@ -244,34 +243,70 @@ interface InsightObjectOperatorTest { assertThat(company.attachmentsExist, equalTo(false)) } + fun domainOjectWithAllDefaultTypes() = ObjectWithAllDefaultTypes( + id = InsightObjectId.notPersistedObjectId, + name = "testObjectWithAllDefaultTypes", + testBoolean = false, + testInteger = 72, + testFloat = 3.12345678901234, // only double precision does survive this roundtrip + testDate = LocalDate.parse("1984-04-01"), + testDateTime = ZonedDateTime.parse("1983-12-07T14:55:24Z"), + testUrl = setOf("http://localhost", "http://127.0.0.1"), + testEmail = "awesome@linked-planet.com", + testTextArea = "text area text", + testSelect = listOf("Test Option 2"), + testIpAddress = "192.168.0.2", + ) + @Test fun testObjectWithAllDefaultTypes() = runBlocking { + val original = domainOjectWithAllDefaultTypes() val repository = ObjectWithAllDefaultTypesRepository(insightObjectOperator) - - val original = ObjectWithAllDefaultTypes( - id = InsightObjectId.notPersistedObjectId, - name = "testObjectWithAllDefaultTypes", - testBoolean = false, - testInteger = 72, - testFloat = 3.12345678901234, // only double precision does survive this roundtrip - testDate = LocalDate.parse("1984-04-01"), - testDateTime = ZonedDateTime.parse("1983-12-07T14:55:24Z"), - testUrl = setOf("http://localhost", "http://127.0.0.1"), - testEmail = "awesome@linked-planet.com", - testTextArea = "text area text", - testSelect = listOf("Test Option 2"), - testIpAddress = "192.168.0.2", - ) - deleteAllObjectsWithType(repository.objectTypeId) - try { + autoClean(clean = { deleteAllObjectsWithType(repository.objectTypeId) }) { val created = repository.create(original).orFail() assertThat(created, not(equalTo(null))) assertThat(created, equalTo(original.copy(id = created.id))) val byId = repository.getById(created.id!!).orFail() assertThat(byId, equalTo(original.copy(id = created.id))) - } finally { - deleteAllObjectsWithType(repository.objectTypeId) + } + } + + @Test + fun testInsightObjectExtensionsWithAllDefaultTypes() = runBlocking { + val original = domainOjectWithAllDefaultTypes() + val repository = ObjectWithAllDefaultTypesRepository(insightObjectOperator) + autoClean(clean = { deleteAllObjectsWithType(repository.objectTypeId) }) { + // test relies on testObjectWithAllDefaultTypes to be successful + val createdObj = repository.create(original).orFail() + + val objectById = insightObjectOperator.getObjectById(createdObj.id!!, ::identity).orFail()!! + objectById.setValue(Name.attributeId, "updated") + objectById.setValue(TestBoolean.attributeId, true) + objectById.setValue(TestInteger.attributeId, 999) + objectById.setValue(TestFloat.attributeId, -123.0) + objectById.setValue(TestDate.attributeId, original.testDate!!.plusDays(1)) + objectById.setValue(TestDateTime.attributeId, original.testDateTime!!.plusHours(10)) + objectById.setUrlValues(TestUrl.attributeId, listOf("http://updated.values.it")) + objectById.setValue(TestEmail.attributeId, "udpated@linked-planet.com") + objectById.setValue(TestTextArea.attributeId, "updated text area") + objectById.setSelectValues(TestSelect.attributeId, listOf("Test Option 1")) + objectById.setValue(TestIpAddress.attributeId, "10.0.0.1") + + insightObjectOperator.updateInsightObject(objectById).orFail() + val updatedObj = insightObjectOperator.getObjectById(objectById.id, ::identity).orFail()!! + + assertThat(updatedObj.getStringValue(Name.attributeId), equalTo("updated")) + assertThat(updatedObj.getBooleanValue(TestBoolean.attributeId), equalTo(true)) + assertThat(updatedObj.getIntValue(TestInteger.attributeId), equalTo(999)) + assertThat(updatedObj.getDoubleValue(TestFloat.attributeId), equalTo(-123.0)) + assertThat(updatedObj.getDateValue(TestDate.attributeId), equalTo(original.testDate.plusDays(1))) + assertThat(updatedObj.getDateTimeValue(TestDateTime.attributeId), equalTo(original.testDateTime.plusHours(10))) + assertThat(updatedObj.getUrlValues(TestUrl.attributeId), equalTo(listOf("http://updated.values.it"))) + assertThat(updatedObj.getStringValue(TestEmail.attributeId), equalTo("udpated@linked-planet.com")) + assertThat(updatedObj.getStringValue(TestTextArea.attributeId), equalTo("updated text area")) + assertThat(updatedObj.getSelectValues(TestSelect.attributeId), equalTo(listOf("Test Option 1"))) + assertThat(updatedObj.getStringValue(TestIpAddress.attributeId), equalTo("10.0.0.1")) } } @@ -502,7 +537,6 @@ interface InsightObjectOperatorTest { @Test fun testGetObjectsWithChildrenPaginated() { - // results 1 and 2 val allList = runBlocking { insightObjectOperator.getObjects( @@ -578,12 +612,10 @@ interface InsightObjectOperatorTest { assertThat(firstList.totalFilterCount, equalTo(2)) val emptyObjects = emptyList.objects assertThat(emptyObjects, equalTo(emptyList())) - } @Test - fun testUserAttribute() { - + fun testGetUserAttribute() { val obj = runBlocking { insightObjectOperator.getObjects(InsightObjectType.User.id, toDomain = ::identity) .map { it.objects.firstOrNull() }.orFail() @@ -595,9 +627,50 @@ interface InsightObjectOperatorTest { val usersAttr = obj.getUserList(UserTestUsers.attributeId) assertThat(usersAttr.size, equalTo(2)) assertThat(usersAttr.map { it.name }, hasItems("admin", "test1")) + } + + @Test + fun testUserCrud() = runBlocking { + suspend fun getUserAttributes(objectId: InsightObjectId): Pair?, List> { + val insightObject = insightObjectOperator.getObjectById(objectId, ::identity).orFail()!! + val attrUser = insightObject.getAttributeAs(UserTestUser.attributeId)?.users + val attrUsers = insightObject.getUserList(UserTestUsers.attributeId) + return Pair(attrUser, attrUsers) + } + val objectName = "createdByUnitTest" + autoClean(clean = { deleteObjectByName(InsightObjectType.User.id, objectName).orFail() }) { + val user1 = InsightUser("", "", "", "JIRAUSER10100") + val user2 = InsightUser("", "", "", "JIRAUSER10101") + val objectId = insightObjectOperator.createInsightObject( + InsightObjectType.User.id, + UserTestName.attributeId toValue objectName, + UserTestUser.attributeId toUser user1, + UserTestUsers.attributeId toUsers listOf(user1) + ).orFail() + val (attrUser, attrUsers) = getUserAttributes(objectId) + assertThat(attrUser?.firstOrNull()?.key, equalTo(user1.key)) + assertThat(attrUsers.firstOrNull()?.key, equalTo(user1.key)) + + insightObjectOperator.updateInsightObject(objectId, + UserTestUser.attributeId toUser user2, + UserTestUsers.attributeId toUsers listOf(user2, user1), + toDomain = ::identity ) + val (updatedAttrUser, updatedAttrUsers) = getUserAttributes(objectId) + assertThat(updatedAttrUser?.firstOrNull()?.key, equalTo(user2.key)) + assertThat(updatedAttrUsers.map { it.key }.toSet(), equalTo(setOf(user1.key, user2.key))) + } } + + + private suspend fun deleteObjectByName(objectTypeId: InsightObjectTypeId, name: String): Either = + arrow.core.computations.either { + insightObjectOperator.getObjectByName(objectTypeId, name, ::identity).bind()?.id?.let { id -> + insightObjectOperator.deleteObject(id).bind() + } + } + @Test fun testObjectCount() = runBlocking { val count = insightObjectOperator.getObjectCount("objectType = Many").orFail() diff --git a/kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/Model.kt b/kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/Model.kt index ada153f2..0f4cdf2b 100644 --- a/kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/Model.kt +++ b/kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/Model.kt @@ -55,6 +55,7 @@ enum class TestAttributes(val attributeId: InsightAttributeId, val attributeName SimpleObjectFirstname(InsightAttributeId(15), "Firstname"), SimpleObjectLastname(InsightAttributeId(16), "Lastname"), + UserTestName(InsightAttributeId(40), "Name"), UserTestUser(InsightAttributeId(43), "User"), UserTestUsers(InsightAttributeId(44), "Users"), } diff --git a/kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/TestUtils.kt b/kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/TestUtils.kt index a37b24cb..e76f3c9f 100644 --- a/kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/TestUtils.kt +++ b/kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/TestUtils.kt @@ -53,4 +53,13 @@ suspend fun InsightObjectOperator.makeSureObjectWithNameDoesNotExist(objectTypeI } assertThat(getObjectByName(objectTypeId, name, toDomain = ::identity).orFail(), equalTo(null)) //if the former assertion failed that means deleteObject is not working, so this attachment test fails too +} + +suspend fun autoClean(clean: suspend () -> Unit, testCode: suspend () -> Unit){ + try { + clean() + testCode() + } finally { + clean() + } } \ No newline at end of file diff --git a/kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/repositories/ObjectWithAllDefaultTypesRepository.kt b/kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/repositories/ObjectWithAllDefaultTypesRepository.kt index 60bc1eb6..a807dd92 100644 --- a/kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/repositories/ObjectWithAllDefaultTypesRepository.kt +++ b/kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/repositories/ObjectWithAllDefaultTypesRepository.kt @@ -40,7 +40,7 @@ import com.linkedplanet.kotlininsightclient.api.model.InsightObjectTypeId import com.linkedplanet.kotlininsightclient.api.model.InsightAttribute import com.linkedplanet.kotlininsightclient.api.model.InsightAttribute.Integer import com.linkedplanet.kotlininsightclient.api.model.InsightAttribute.Text -import com.linkedplanet.kotlininsightclient.api.model.getAttributeValue +import com.linkedplanet.kotlininsightclient.api.model.getAttributeAs class ObjectWithAllDefaultTypesRepository( override val insightObjectOperator: InsightObjectOperator @@ -55,18 +55,18 @@ class ObjectWithAllDefaultTypesRepository( either { ObjectWithAllDefaultTypes( id = insightObject.id, - name = insightObject.getAttributeValue(Name.attributeId)?.value!!, - testBoolean = insightObject.getAttributeValue(TestBoolean.attributeId)?.value, - testInteger = insightObject.getAttributeValue(TestInteger.attributeId)?.value, - testFloat = insightObject.getAttributeValue(TestFloat.attributeId)?.value, - testDate = insightObject.getAttributeValue(TestDate.attributeId)?.value, - testDateTime = insightObject.getAttributeValue(TestDateTime.attributeId)?.value, - testUrl = insightObject.getAttributeValue(TestUrl.attributeId)?.values?.toSet() ?: emptySet(), - testEmail = insightObject.getAttributeValue(TestEmail.attributeId)?.value, - testTextArea = insightObject.getAttributeValue(TestTextArea.attributeId)?.value, - testSelect = insightObject.getAttributeValue(TestSelect.attributeId)?.values + name = insightObject.getAttributeAs(Name.attributeId)?.value!!, + testBoolean = insightObject.getAttributeAs(TestBoolean.attributeId)?.value, + testInteger = insightObject.getAttributeAs(TestInteger.attributeId)?.value, + testFloat = insightObject.getAttributeAs(TestFloat.attributeId)?.value, + testDate = insightObject.getAttributeAs(TestDate.attributeId)?.value, + testDateTime = insightObject.getAttributeAs(TestDateTime.attributeId)?.value, + testUrl = insightObject.getAttributeAs(TestUrl.attributeId)?.values?.toSet() ?: emptySet(), + testEmail = insightObject.getAttributeAs(TestEmail.attributeId)?.value, + testTextArea = insightObject.getAttributeAs(TestTextArea.attributeId)?.value, + testSelect = insightObject.getAttributeAs(TestSelect.attributeId)?.values ?: emptyList(), - testIpAddress = insightObject.getAttributeValue(TestIpAddress.attributeId)?.value, + testIpAddress = insightObject.getAttributeAs(TestIpAddress.attributeId)?.value, ) }