Skip to content

Commit

Permalink
add tests for Status, Project, Version
Browse files Browse the repository at this point in the history
  • Loading branch information
HighKo committed Jun 4, 2024
1 parent 60eee84 commit 7b5e6f1
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ data class ProjectVersion(
@field:NotNull val id: Int,
@field:NotNull val name: String,
@field:NotNull val avatarUrl: String,
@field:NotNull val url: String,
// @field:NotNull val url: String, // http api produces weird values, so we ignore it for now
)

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
package com.linkedplanet.kotlininsightclient.api.model

import com.linkedplanet.kotlinatlassianclientcore.common.api.JiraGroup
import com.linkedplanet.kotlinatlassianclientcore.common.api.JiraProject
import com.linkedplanet.kotlinatlassianclientcore.common.api.JiraUser
import com.linkedplanet.kotlinatlassianclientcore.common.api.ProjectVersion
import com.linkedplanet.kotlinatlassianclientcore.common.api.StatusAttribute
import java.time.LocalDate
import java.time.ZonedDateTime

Expand Down Expand Up @@ -152,6 +155,21 @@ fun InsightObject.getGroupList(id: InsightAttributeId): List<JiraGroup> =
getAttributeAs<InsightAttribute.Group>(id)?.groups ?: emptyList()
// endregion user

//region ObjectAttributeValue.Status
fun InsightObject.getStatus(id: InsightAttributeId): StatusAttribute? =
getAttributeAs<InsightAttribute.Status>(id)?.status
// endregion user

//region ObjectAttributeValue.Project
fun InsightObject.getProjectList(id: InsightAttributeId): List<JiraProject> =
getAttributeAs<InsightAttribute.Project>(id)?.projects ?: emptyList()
// endregion user

//region ObjectAttributeValue.Version
fun InsightObject.getVersionList(id: InsightAttributeId): List<ProjectVersion> =
getAttributeAs<InsightAttribute.Version>(id)?.versions ?: emptyList()
// endregion user

// region ObjectAttributeValue.Reference
fun InsightObject.getSingleReferenceValue(id: InsightAttributeId): InsightReference? =
getAttribute(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ sealed class InsightAttribute(
data class Version(
@get:JvmName("getAttributeId")
@field:NotNull override val attributeId: InsightAttributeId,
@field:NotNull val version: List<ProjectVersion>,
@field:NotNull val versions: List<ProjectVersion>,
override val schema: ObjectTypeSchemaAttribute?
) :
InsightAttribute(attributeId, schema, AttributeTypeEnum.Version) {
Expand Down Expand Up @@ -380,6 +380,21 @@ sealed class InsightAttribute(
infix fun InsightAttributeId.toGroups(groups: List<JiraGroup>) =
Group(this, groups, schema = null)

infix fun InsightAttributeId.toProject(project: JiraProject?) =
Project(this, listOfNotNull(project), schema = null)

infix fun InsightAttributeId.toProjects(projects: List<JiraProject>) =
Project(this, projects, schema = null)

infix fun InsightAttributeId.toStatus(status: StatusAttribute?) =
Status(this, status, schema = null)

infix fun InsightAttributeId.toVersion(version: ProjectVersion?) =
Version(this, listOfNotNull(version), schema = null)

infix fun InsightAttributeId.toVersions(versions: List<ProjectVersion>) =
Version(this, versions, schema = null)

infix fun InsightAttributeId.toReference(referencedObjectId: InsightObjectId?) =
Reference(
this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ class HttpInsightObjectOperator(private val context: HttpInsightClientContext) :
}
VERSION -> {
val version = apiAttribute.objectAttributeValues.mapNotNull { av: ObjectAttributeValueApiResponse ->
av.version?.run { ProjectVersion(id.toInt(), name = name, avatarUrl = avatarUrl, url = url) }
av.version?.run { ProjectVersion(id.toInt(), name = name, avatarUrl = avatarUrl) }
}
InsightAttribute.Version(attributeId, version, schema)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ internal fun InsightObject.getEditAttributes(): List<ObjectEditItemAttribute> =
is InsightAttribute.Reference -> attr.referencedObjects.map { it.id.raw }
is InsightAttribute.User -> attr.users.map { it.key }
is InsightAttribute.Group -> attr.groups.map { it.name }

// TODO test additional types
is InsightAttribute.Project -> attr.projects.map { it.id }
is InsightAttribute.Status -> listOfNotNull(attr.status?.id)
is InsightAttribute.Version -> attr.version.map { it.id }
is InsightAttribute.Project -> attr.projects.map { it.id }
is InsightAttribute.Version -> attr.versions.map { it.id }
is InsightAttribute.Confluence -> attr.pages.map { it.id }

is InsightAttribute.Unknown -> emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import arrow.core.flatMap
import arrow.core.left
import arrow.core.raise.either
import arrow.core.right
import com.atlassian.jira.bc.project.ProjectService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor.getOSGiComponentInstanceOfType
import com.atlassian.jira.config.properties.ApplicationProperties
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.user.util.UserManager
import com.linkedplanet.kotlinatlassianclientcore.common.api.StatusAttribute
import com.linkedplanet.kotlinatlassianclientcore.common.api.StatusCategory
Expand Down Expand Up @@ -93,7 +93,9 @@ object SdkInsightObjectOperator : InsightObjectOperator {
private val insightRestBaseUrl = baseUrl + INSIGHT_REST_BASE_URL // see BaseUrlResolverServiceInJira
private val avatarService = ComponentAccessor.getAvatarService()
private val dateTimeFormatter = ReverseEngineeredDateTimeFormatterInJira()
private val projectManager = getOSGiComponentInstanceOfType(ProjectManager::class.java)
private val projectService = getOSGiComponentInstanceOfType(ProjectService::class.java)
private val jiraAuthenticationContext = ComponentAccessor.getJiraAuthenticationContext()
private fun user() = jiraAuthenticationContext.loggedInUser

private val zoneId: ZoneId by lazy { ZoneId.of("Z") }

Expand Down Expand Up @@ -253,7 +255,7 @@ object SdkInsightObjectOperator : InsightObjectOperator {
objectAttributeBeanFactory.createStatusAttributeValue(ota) { statusId != null && it.id == statusId }
}
is InsightAttribute.Version -> {
val versionIds = attr.version.map { it.id.toLong() } // TODO:hg convert to long?
val versionIds = attr.versions.map { it.id.toLong() } // TODO:hg convert to long?
objectAttributeBeanFactory.createVersionAttributeValue(ota) { versionIds.contains(it.id()) }
}
is InsightAttribute.Confluence -> {
Expand Down Expand Up @@ -416,14 +418,11 @@ object SdkInsightObjectOperator : InsightObjectOperator {
}
Type.VERSION -> { // TODO: add full support
val versions = objectAttributeBean.objectAttributeValueBeans.mapNotNull { attribute: ObjectAttributeValueBean ->
val avatarUrl = avatarUrlFor("version-logo.png")
val versionId = attribute.integerValue
ProjectVersion(
id = versionId,
id = attribute.integerValue,
// we would need access to VersionAssemblerInJira or VersionService access to resolve name and url
name = "UNKNOWN", // see hidden class VersionAssemblerInJira.class in plugins:insight:10.4.2
url = "#", // see hidden class VersionAssemblerInJira.class in plugins:insight:10.4.2
avatarUrl = avatarUrl
name = "Unknown", // see hidden class VersionAssemblerInJira.class in plugins:insight:10.4.2
avatarUrl = avatarUrlFor("version-logo.png")
)
}
InsightAttribute.Version(attributeId, versions, schema)
Expand All @@ -436,9 +435,9 @@ object SdkInsightObjectOperator : InsightObjectOperator {
}
Type.PROJECT -> { // see ProjectAssembler.class and ObjectAttributeBeanFactoryImpl.createProjectAttributeValue
val projects = objectAttributeBean.objectAttributeValueBeans
.mapNotNull { projectManager.getProjectObj(it.id) }
.mapNotNull { projectService.getProjectById(user(), it.id).project }
.map {
val url = "$insightRestBaseUrl/browse/${it.key}"
val url = "$insightRestBaseUrl/browse/${it.key}"
val avatarUrl = "$insightRestBaseUrl/secure/projectavatar?pid=${it.id}"
JiraProject(it.id, it.key, it.name, url, avatarUrl)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package com.linkedplanet.kotlininsightclient

import arrow.core.Either
import com.linkedplanet.kotlinatlassianclientcore.common.api.JiraGroup
import com.linkedplanet.kotlinatlassianclientcore.common.api.JiraProject
import com.linkedplanet.kotlininsightclient.AuthenticatedJiraHttpClientFactory.Companion.Credentials
import com.linkedplanet.kotlininsightclient.ObjectWithAllDefaultTypesAttributeIds.*
import com.linkedplanet.kotlininsightclient.TestAttributes.*
Expand All @@ -39,8 +40,16 @@ import com.linkedplanet.kotlininsightclient.api.model.InsightObject
import com.linkedplanet.kotlininsightclient.api.model.InsightObjectId
import com.linkedplanet.kotlininsightclient.api.model.InsightObjectTypeId
import com.linkedplanet.kotlinatlassianclientcore.common.api.JiraUser
import com.linkedplanet.kotlinatlassianclientcore.common.api.ProjectVersion
import com.linkedplanet.kotlinatlassianclientcore.common.api.StatusAttribute
import com.linkedplanet.kotlinatlassianclientcore.common.api.StatusCategory
import com.linkedplanet.kotlininsightclient.api.model.InsightAttribute.Companion.toGroup
import com.linkedplanet.kotlininsightclient.api.model.InsightAttribute.Companion.toGroups
import com.linkedplanet.kotlininsightclient.api.model.InsightAttribute.Companion.toProject
import com.linkedplanet.kotlininsightclient.api.model.InsightAttribute.Companion.toProjects
import com.linkedplanet.kotlininsightclient.api.model.InsightAttribute.Companion.toStatus
import com.linkedplanet.kotlininsightclient.api.model.InsightAttribute.Companion.toVersion
import com.linkedplanet.kotlininsightclient.api.model.InsightAttribute.Companion.toVersions
import com.linkedplanet.kotlininsightclient.api.model.ObjectTypeSchemaAttribute
import com.linkedplanet.kotlininsightclient.api.model.addSelectValue
import com.linkedplanet.kotlininsightclient.api.model.getAttributeAs
Expand All @@ -53,11 +62,14 @@ import com.linkedplanet.kotlininsightclient.api.model.getDoubleValue
import com.linkedplanet.kotlininsightclient.api.model.getGroupList
import com.linkedplanet.kotlininsightclient.api.model.getIntValue
import com.linkedplanet.kotlininsightclient.api.model.getMultiReferenceValue
import com.linkedplanet.kotlininsightclient.api.model.getProjectList
import com.linkedplanet.kotlininsightclient.api.model.getSelectValues
import com.linkedplanet.kotlininsightclient.api.model.getSingleReferenceValue
import com.linkedplanet.kotlininsightclient.api.model.getStatus
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.getVersionList
import com.linkedplanet.kotlininsightclient.api.model.removeSelectValue
import com.linkedplanet.kotlininsightclient.api.model.setSelectValues
import com.linkedplanet.kotlininsightclient.api.model.setUrlValues
Expand Down Expand Up @@ -720,6 +732,103 @@ interface InsightObjectOperatorTest {
}
}

@Test
fun testStatusCrud() = runBlocking {
suspend fun getStatusAttributes(objectId: InsightObjectId): StatusAttribute? {
val insightObject = insightObjectOperator.getObjectById(objectId, ::identity).orFail()!!
return insightObject.getStatus(TestStatusStatus.attributeId)
}

val objectName = "createdByUnitTest"
autoClean(clean = { deleteObjectByName(InsightObjectType.Status.id, objectName).orFail() }) {
val status1 = StatusAttribute(2, "Running", StatusCategory.ACTIVE, -1, "")
val status2 = StatusAttribute(6, "Stopped", StatusCategory.ACTIVE, -1, "")
val objectId = insightObjectOperator.createInsightObject(
InsightObjectType.Status.id,
TestStatusName.attributeId toValue objectName,
TestStatusStatus.attributeId toStatus status1,
).orFail()
val attrStatus = getStatusAttributes(objectId)
assertThat(attrStatus?.id, equalTo(status1.id))

insightObjectOperator.updateInsightObject(objectId,
TestStatusStatus.attributeId toStatus status2,
toDomain = ::identity )
val updatedAttrStatus = getStatusAttributes(objectId)
assertThat(updatedAttrStatus?.id, equalTo(status2.id))
}
}

@Test
fun testProjectCrud() = runBlocking {
suspend fun getProjectAttributes(objectId: InsightObjectId): Pair<List<JiraProject>?, List<JiraProject>> {
val insightObject = insightObjectOperator.getObjectById(objectId, ::identity).orFail()!!
val attrProject = insightObject.getAttributeAs<InsightAttribute.Project>(TestProjectProject.attributeId)?.projects
val attrProjectList = insightObject.getProjectList(TestProjectProjects.attributeId)
return Pair(attrProject, attrProjectList)
}

val objectName = "createdByUnitTest"
autoClean(clean = { deleteObjectByName(InsightObjectType.Project.id, objectName).orFail() }) {
val project1 = JiraProject(10000, "TEST", "Test", "http://localhost:2990/browse/TEST", "http://localhost:2990/secure/projectavatar?pid=10000")
val project2 = JiraProject(10000, "TEST", "Test", "http://localhost:2990/browse/TEST", "http://localhost:2990/secure/projectavatar?pid=10000")
val objectId = insightObjectOperator.createInsightObject(
InsightObjectType.Project.id,
TestProjectName.attributeId toValue objectName,
TestProjectProject.attributeId toProject project1,
TestProjectProjects.attributeId toProjects listOf(project1)
).orFail()
val (attrProject, attrProjects) = getProjectAttributes(objectId)
assertThat(attrProject?.firstOrNull()?.name, equalTo(project1.name))
assertThat(attrProjects.firstOrNull()?.name, equalTo(project1.name))

insightObjectOperator.updateInsightObject(
objectId,
TestProjectProject.attributeId toProject project2,
TestProjectProjects.attributeId toProjects listOfNotNull(project2, project1),
toDomain = ::identity
)
val (updatedAttrProject, updatedAttrProjects) = getProjectAttributes(objectId)
assertThat(updatedAttrProject?.firstOrNull()?.name, equalTo(project2.name))
assertThat(updatedAttrProjects.map { it.name }.toSet(), equalTo(setOf(project1.name, project2.name)))
}
}

@Test
fun testVersionCrud() = runBlocking {
suspend fun getVersionAttributes(objectId: InsightObjectId): Pair<List<ProjectVersion>?, List<ProjectVersion>> {
val insightObject = insightObjectOperator.getObjectById(objectId, ::identity).orFail()!!
val attrVersion = insightObject.getAttributeAs<InsightAttribute.Version>(TestVersionVersion.attributeId)?.versions
val attrVersionList = insightObject.getVersionList(TestVersionVersions.attributeId)
return Pair(attrVersion, attrVersionList)
}

val objectName = "createdByUnitTest"
autoClean(clean = { deleteObjectByName(InsightObjectType.Version.id, objectName).orFail() }) {
val version1 = ProjectVersion(-1, "Unknown", "") // no name resolution for now
val version2 = ProjectVersion(-2, "Unknown", "") // no name resolution for now
val objectId = insightObjectOperator.createInsightObject(
InsightObjectType.Version.id,
TestVersionName.attributeId toValue objectName,
TestVersionVersion.attributeId toVersion version1,
TestVersionVersions.attributeId toVersions listOf(version1)
).orFail()
val (attrVersion, attrVersions) = getVersionAttributes(objectId)
assertThat(attrVersion?.firstOrNull()?.id, equalTo(version1.id))
assertThat(attrVersions.firstOrNull()?.name, equalTo(version1.name))
assertThat(attrVersions.firstOrNull()?.avatarUrl, endsWith("version-logo.png"))
// assertThat(attrVersions.firstOrNull()?.url, startsWith("http")) // this always failed

insightObjectOperator.updateInsightObject(objectId,
TestVersionVersion.attributeId toVersion version2,
TestVersionVersions.attributeId toVersions listOf(version2, version1),
toDomain = ::identity )
val (updatedAttrVersion, updatedAttrVersions) = getVersionAttributes(objectId)
assertThat(updatedAttrVersion?.firstOrNull()?.id, equalTo(version2.id))
assertThat(updatedAttrVersions.map { it.id }.toSet(), equalTo(setOf(version1.id, version2.id)))
}
}

private suspend fun deleteObjectByName(objectTypeId: InsightObjectTypeId, name: String): Either<InsightClientError, Unit> =
arrow.core.raise.either {
insightObjectOperator.getObjectByName(objectTypeId, name, ::identity).bind()?.id?.let { id ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ enum class InsightObjectType(val id: InsightObjectTypeId) {
Many(InsightObjectTypeId(5)),
Abstract(InsightObjectTypeId(6)),
User(InsightObjectTypeId(9)),
Group(InsightObjectTypeId(41)),
Group(InsightObjectTypeId(42)),
Status(InsightObjectTypeId(44)),
Project(InsightObjectTypeId(43)),
Version(InsightObjectTypeId(47)),
Confluence(InsightObjectTypeId(46)),
ObjectWithAllDefaultTypes(InsightObjectTypeId(40))
}

Expand All @@ -60,9 +64,20 @@ enum class TestAttributes(val attributeId: InsightAttributeId, val attributeName
UserTestUser(InsightAttributeId(43), "User"),
UserTestUsers(InsightAttributeId(44), "Users"),

TestGroupName(InsightAttributeId(82), "Name"),
TestGroupGroup(InsightAttributeId(85), "Group"),
TestGroupGroups(InsightAttributeId(86), "Groups"),
TestGroupName(InsightAttributeId(86), "Name"),
TestGroupGroup(InsightAttributeId(105), "Group"),
TestGroupGroups(InsightAttributeId(106), "Groups"),

TestStatusName(InsightAttributeId(94), "Name"),
TestStatusStatus(InsightAttributeId(109), "Status"),

TestVersionName(InsightAttributeId(114), "Name"),
TestVersionVersion(InsightAttributeId(117), "Version"),
TestVersionVersions(InsightAttributeId(118), "Versions"),

TestProjectName(InsightAttributeId(90), "Name"),
TestProjectProject(InsightAttributeId(107), "Project"),
TestProjectProjects(InsightAttributeId(108), "Projects"),
}

enum class ObjectWithAllDefaultTypesAttributeIds(val attributeId: InsightAttributeId) {
Expand Down

0 comments on commit 7b5e6f1

Please sign in to comment.