Skip to content

Commit

Permalink
Adding api endpoint for current coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
craigatk committed Mar 29, 2024
1 parent 34d5f99 commit a2130c1
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 1 deletion.
2 changes: 1 addition & 1 deletion server/server-app/src/main/kotlin/projektor/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ fun Application.main(meterRegistry: MeterRegistry? = null) {
val codeQualityReportRepository: CodeQualityReportRepository by inject()

routing {
api(repositoryCoverageService)
api(organizationCoverageService, repositoryCoverageService)
attachments(attachmentService, authService)
badge(coverageBadgeService)
codeQuality(codeQualityReportRepository)
Expand Down
14 changes: 14 additions & 0 deletions server/server-app/src/main/kotlin/projektor/route/ApiRoutes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,26 @@ import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.util.*
import projektor.organization.coverage.OrganizationCoverageService
import projektor.repository.coverage.RepositoryCoverageService
import projektor.server.api.repository.BranchSearch

fun Route.api(
organizationCoverageService: OrganizationCoverageService,
repositoryCoverageService: RepositoryCoverageService,
) {
get("/api/org/{orgName}/coverage/current") {
val orgName = call.parameters.getOrFail("orgName")

val organizationCoverage = organizationCoverageService.getCoverage(orgName)

if (organizationCoverage.repositories.isNotEmpty()) {
call.respond(HttpStatusCode.OK, organizationCoverage)
} else {
call.respond(HttpStatusCode.NoContent)
}
}

get("/api/repo/{orgPart}/{repoPart}/coverage/current") {
val orgPart = call.parameters.getOrFail("orgPart")
val repoPart = call.parameters.getOrFail("repoPart")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package projektor.api

import io.ktor.http.*
import io.ktor.server.testing.*
import org.apache.commons.lang3.RandomStringUtils
import org.junit.jupiter.api.Test
import projektor.ApplicationTestCase
import projektor.incomingresults.randomPublicId
import projektor.server.api.organization.OrganizationCoverage
import projektor.server.example.coverage.JacocoXmlLoader
import strikt.api.expectThat
import strikt.assertions.hasSize
import strikt.assertions.isEqualTo
import strikt.assertions.isNotNull
import kotlin.test.assertNotNull

class ApiOrganizationApplicationTestCase : ApplicationTestCase() {
@Test
fun `when three repos in org should find their coverage data`() {
val orgName = RandomStringUtils.randomAlphabetic(12)

val publicId1 = randomPublicId()
val repo1 = "$orgName/repo1"
val olderRunRepo1 = randomPublicId()
val otherProjectRepo1 = randomPublicId()

val publicId2 = randomPublicId()
val repo2 = "$orgName/repo2"
val olderRunRepo2 = randomPublicId()

val publicId3 = randomPublicId()
val repo3 = "$orgName/repo3"
val olderRunRepo3 = randomPublicId()

val anotherPublicId = randomPublicId()
val anotherRepo = "another-org/repo"

withTestApplication(::createTestApplication) {
handleRequest(HttpMethod.Get, "/api/org/$orgName/coverage/current") {

testRunDBGenerator.createTestRunWithCoverageAndGitMetadata(
publicId = olderRunRepo1,
coverageText = JacocoXmlLoader().serverApp(),
repoName = repo1,
projectName = "proj1"
)
testRunDBGenerator.createTestRunWithCoverageAndGitMetadata(
publicId = publicId1,
coverageText = JacocoXmlLoader().serverApp(),
repoName = repo1,
projectName = "proj1"
)
testRunDBGenerator.createTestRunWithCoverageAndGitMetadata(
publicId = otherProjectRepo1,
coverageText = JacocoXmlLoader().serverAppReduced(),
repoName = repo1,
projectName = "proj2"
)

testRunDBGenerator.createTestRunWithCoverageAndGitMetadata(
publicId = olderRunRepo2,
coverageText = JacocoXmlLoader().jacocoXmlParser(),
repoName = repo2
)
testRunDBGenerator.createTestRunWithCoverageAndGitMetadata(
publicId = publicId2,
coverageText = JacocoXmlLoader().jacocoXmlParser(),
repoName = repo2
)

testRunDBGenerator.createTestRunWithCoverageAndGitMetadata(
publicId = olderRunRepo3,
coverageText = JacocoXmlLoader().junitResultsParser(),
repoName = repo3
)
testRunDBGenerator.createTestRunWithCoverageAndGitMetadata(
publicId = publicId3,
coverageText = JacocoXmlLoader().junitResultsParser(),
repoName = repo3
)

testRunDBGenerator.createTestRunWithCoverageAndGitMetadata(
publicId = anotherPublicId,
coverageText = JacocoXmlLoader().serverAppReduced(),
repoName = anotherRepo
)
}.apply {
expectThat(response.status()).isEqualTo(HttpStatusCode.OK)

val organizationCoverage = objectMapper.readValue(response.content, OrganizationCoverage::class.java)
assertNotNull(organizationCoverage)

expectThat(organizationCoverage.repositories).hasSize(4)

val repositoryDatas1 = organizationCoverage.repositories.filter { it.repoName == repo1 }
expectThat(repositoryDatas1).hasSize(2)

val repo1DataProj1 = repositoryDatas1.find { it.projectName == "proj1" }
assertNotNull(repo1DataProj1)
expectThat(repo1DataProj1.publicId).isEqualTo(publicId1.id)
expectThat(repo1DataProj1.coverage).isNotNull().and {
get { overallStats }.get { lineStat }.get { coveredPercentage }.isEqualTo(JacocoXmlLoader.serverAppLineCoveragePercentage)
}

val repo1DataProj2 = repositoryDatas1.find { it.projectName == "proj2" }
assertNotNull(repo1DataProj2)
expectThat(repo1DataProj2.publicId).isEqualTo(otherProjectRepo1.id)
expectThat(repo1DataProj2.coverage).isNotNull().and {
get { overallStats }.get { lineStat }.get { coveredPercentage }.isEqualTo(JacocoXmlLoader.serverAppReducedLineCoveragePercentage)
}

val repositoryData2 = organizationCoverage.repositories.find { it.repoName == repo2 }
assertNotNull(repositoryData2)

expectThat(repositoryData2.publicId).isEqualTo(publicId2.id)

expectThat(repositoryData2.coverage).isNotNull().and {
get { overallStats }.get { lineStat }.get { coveredPercentage }.isEqualTo(JacocoXmlLoader.jacocoXmlParserLineCoveragePercentage)
}

val repositoryData3 = organizationCoverage.repositories.find { it.repoName == repo3 }
assertNotNull(repositoryData3)

expectThat(repositoryData3.publicId).isEqualTo(publicId3.id)

expectThat(repositoryData3.coverage).isNotNull().and {
get { overallStats }.get { lineStat }.get { coveredPercentage }.isEqualTo(JacocoXmlLoader.junitResultsParserLineCoveragePercentage)
}
}
}
}
}

0 comments on commit a2130c1

Please sign in to comment.