basic test for builds

master
Josha von Gizycki 3 weeks ago
parent 94c177234e
commit 7b1d7c5dc9

@ -7,18 +7,11 @@ import org.springframework.beans.factory.annotation.Value
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import org.springframework.web.bind.annotation.ResponseStatus
import java.lang.RuntimeException
import java.nio.file.Files.createFile
import java.nio.file.Paths
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.*
import kotlin.collections.associate
import kotlin.collections.filter
import kotlin.io.inputStream
import kotlin.jvm.java
import kotlin.text.toLong
import kotlin.text.uppercase
@Service
class Builds(
@ -65,9 +58,9 @@ class Builds(
private fun Properties.toBuildConfig(env: Map<String, String>) =
BuildConfig(
user = getProperty("user"),
workspace = getProperty("workspace"),
apikey = getProperty("apikey") ?: "",
user = getProperty("user") ?: throw MissingProperty("user"),
workspace = getProperty("workspace") ?: throw MissingProperty("workspace"),
apikey = getProperty("apikey") ?: throw MissingProperty("apikey"),
script = getProperty("script"),
gitRepo = getProperty("git.repo.url"),
gitCloneTimeout = getProperty("git.close.timeout")?.toLong() ?: 30L,
@ -75,10 +68,10 @@ private fun Properties.toBuildConfig(env: Map<String, String>) =
)
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Unknown Build Id")
class UnknownBuild(val build: BuildId) : RuntimeException() {
override fun toString() =
"unknown build: $build"
}
class UnknownBuild(build: BuildId) : RuntimeException("unknown build: $build")
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR, reason = "Missing Build property")
class MissingProperty(key: String) : RuntimeException("missing property $key")
data class BuildConfig(
val user: String,

@ -0,0 +1,52 @@
package alfred.web.core.build
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.nio.file.Paths
import kotlin.test.assertEquals
class BuildsTest {
@Test
fun `reads minimal build config`() {
val cfg = builds().buildConfig("minimal")
assertEquals("Gandalf", cfg.user)
assertEquals("Mellon", cfg.apikey)
assertEquals("/tmp", cfg.workspace)
assertEquals(30L, cfg.gitCloneTimeout, "Default value for clone timeout expected")
assertEquals(0, cfg.env.count(), "No env entries expected")
}
@Test
fun `unknown builds are reported`() {
val builds = builds()
assertThrows<UnknownBuild> {
builds.buildConfig("unknown")
}
}
@Test
fun `reads maximum build config`() {
val cfg = builds().buildConfig("maximum")
assertEquals("Merri", cfg.user)
assertEquals("Shire", cfg.apikey)
assertEquals("/tmp/far-away", cfg.workspace)
assertEquals("echo Breakfast", cfg.script)
assertEquals("https://something.nowhere", cfg.gitRepo)
assertEquals(10, cfg.gitCloneTimeout)
assertEquals(1, cfg.env.count())
assertEquals("env1-value", cfg.env["ENV1"])
}
private fun builds() =
Builds(
Paths.get("")
.toAbsolutePath()
.resolve("src/test/resources/home-1")
.toString()
)
}

@ -0,0 +1,12 @@
user=Merri
workspace=/tmp/far-away
apikey=Shire
script=echo Breakfast
git.repo.url=https://something.nowhere
git.close.timeout=10
ignored=true
ENV1=env1-value

@ -0,0 +1,3 @@
user=Gandalf
workspace=/tmp
apikey=Mellon
Loading…
Cancel
Save