-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TS-38628 Migrate artifactory-git-properties-detection
- Loading branch information
1 parent
e841307
commit 4eea509
Showing
4 changed files
with
123 additions
and
113 deletions.
There are no files selected for viewing
39 changes: 0 additions & 39 deletions
39
...s-detection/src/test/java/com/teamscale/client/ArtifactoryGitPropertiesDetectionTest.java
This file was deleted.
Oops, something went wrong.
74 changes: 0 additions & 74 deletions
74
...ry-git-properties-detection/src/test/java/com/teamscale/client/ArtifactoryMockServer.java
This file was deleted.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
...s-detection/src/test/kotlin/com/teamscale/client/ArtifactoryGitPropertiesDetectionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...ry-git-properties-detection/src/test/kotlin/com/teamscale/client/ArtifactoryMockServer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
||
/** 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) | ||
} | ||
} | ||
} | ||
} | ||
} |