Skip to content

Commit

Permalink
TS-38628 Migrate artifactory-git-properties-detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Avanatiker committed Jan 31, 2025
1 parent e841307 commit 4eea509
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 113 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.teamscale.client

import com.teamscale.test.commons.SystemTestUtils
import com.teamscale.test.commons.SystemTestUtils.dumpCoverage
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import systemundertest.SystemUnderTest

class ArtifactoryGitPropertiesDetectionTest {
private var artifactoryMockServer: ArtifactoryMockServer? = null

@BeforeEach
fun startFakeTeamscaleServer() {
artifactoryMockServer = ArtifactoryMockServer(FAKE_ARTIFACTORY_PORT)
}

@AfterEach
fun shutdownFakeTeamscaleServer() {
artifactoryMockServer?.shutdown()
}

@Test
@Throws(Exception::class)
fun systemTest() {
SystemUnderTest.foo()

dumpCoverage(SystemTestUtils.AGENT_PORT)

Assertions.assertThat(
artifactoryMockServer?.uploadedReports
).hasSize(1)
val reports = artifactoryMockServer?.uploadedReports
Assertions.assertThat(reports).hasSize(1)
// Ensure infos from src/main/resources/git.properties are picked up
Assertions.assertThat(reports?.getFirst(0))
.startsWith("uploads/master/1645713803000-86f9d655bf8a204d98bc3542e0d15cea38cc7c74/")
}

companion object {
private val FAKE_ARTIFACTORY_PORT: Int = Integer.getInteger("artifactoryPort")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.teamscale.client

import org.conqat.lib.commons.collections.PairList
import spark.Request
import spark.Response
import spark.Service
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.InputStream
import java.util.function.BiConsumer
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import javax.servlet.http.HttpServletResponse

/**
* Mocks an Artifactory server: stores all uploaded reports so tests can run assertions on them.
*/
class ArtifactoryMockServer(port: Int) {

Check warning on line 19 in system-tests/artifactory-git-properties-detection/src/test/kotlin/com/teamscale/client/ArtifactoryMockServer.kt

View check run for this annotation

cqse.teamscale.io / teamscale-findings

system-tests/artifactory-git-properties-detection/src/test/kotlin/com/teamscale/client/ArtifactoryMockServer.kt#L19

[New] Interface comment missing https://cqse.teamscale.io/findings/details/teamscale-jacoco-agent?t=ts%2F38628_iterative_kotlin_migration_system_tests%3AHEAD&id=3661F99FCDE9E473D27B7DC4BCCF61C0
/** All reports uploaded to this Teamscale instance. */
val uploadedReports: PairList<String, String> = PairList()
private val service = Service.ignite()

init {
service.port(port)
service.put(":path", ::handleReport)
service.exception(Exception::class.java) { exception, _, response ->
response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR)
response.body("Exception: " + exception.message)
}
service.notFound { request, response ->
response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR)
"Unexpected request: ${request.requestMethod()} ${request.uri()}"
}
service.awaitInitialization()
}

@Throws(IOException::class)
private fun handleReport(request: Request, response: Response): String {
processZipEntries(
ByteArrayInputStream(request.bodyAsBytes())
) { entry, content ->
if (!entry.isDirectory) {
uploadedReports.add("${request.params("path")} -> ${entry.name}", content.toString())
}
}
return "success"
}

/**
* Shuts down the mock server and waits for it to be stopped.
*/
fun shutdown() {
service.stop()
service.awaitStop()
}

companion object {
@Throws(IOException::class)
private fun processZipEntries(
zipStream: InputStream,
entryConsumer: (ZipEntry, ByteArrayOutputStream) -> Unit
) {
val bufferSize = 1024
ZipInputStream(zipStream).use { zipInput ->
while (true) {
val zipEntry = zipInput.nextEntry ?: break
val entryContent = ByteArrayOutputStream()
val buffer = ByteArray(bufferSize)
var bytesRead: Int
while (zipInput.read(buffer).also { bytesRead = it } != -1) {
entryContent.write(buffer, 0, bytesRead)
}
entryConsumer(zipEntry, entryContent)
}
}
}
}
}

0 comments on commit 4eea509

Please sign in to comment.