Refactoring of input validation

Move application file validation to provisionK3s.
Add an ApplicationFileRepository. Also unit tests for the functions.
This commit is contained in:
erik 2022-08-26 10:52:45 +02:00
parent 1ad357ba44
commit 05821540fe
5 changed files with 52 additions and 13 deletions

View file

@ -37,10 +37,6 @@ fun main(args: Array<String>) {
println("Config file not found. Please check if path is correct.") println("Config file not found. Please check if path is correct.")
exitProcess(1) exitProcess(1)
} }
if (!(cmd as K3sCliCommand).isValidApplicationFileName()) {
println("Application file not found. Please check if path is correct.")
exitProcess(1)
}
if (!cmd.isValidServerType()) { if (!cmd.isValidServerType()) {
throw RuntimeException("Unknown serverType. Currently only k3s is accepted.") throw RuntimeException("Unknown serverType. Currently only k3s is accepted.")
} }

View file

@ -1,5 +1,5 @@
package org.domaindrivenarchitecture.provs.server.domain.k3s package org.domaindrivenarchitecture.provs.server.domain.k3s
interface ApplicationFileRepository { interface ApplicationFileRepository {
fun exists(applicationFileName: ApplicationFileName?): Boolean fun exists(applicationFileName: ApplicationFileName?)
} }

View file

@ -15,9 +15,8 @@ fun Prov.provisionK3s(cli: K3sCliCommand) = task {
// full k3s // full k3s
val k3sConfig: K3sConfig = getK3sConfig(cli.configFileName) val k3sConfig: K3sConfig = getK3sConfig(cli.configFileName)
val repo: ApplicationFileRepository = DefaultApplicationFileRepository() val repo: ApplicationFileRepository = DefaultApplicationFileRepository()
if(repo.exists(cli.applicationFileName)) { repo.exists(cli.applicationFileName)
provisionK3sWorker(k3sConfig, grafanaConfigResolved, cli.applicationFileName) provisionK3sWorker(k3sConfig, grafanaConfigResolved, cli.applicationFileName)
}
} }
else if (cli.reprovision) { // TODO: Add logic that overrides config, when cmd option is set else if (cli.reprovision) { // TODO: Add logic that overrides config, when cmd option is set
deprovisionK3sInfra() deprovisionK3sInfra()

View file

@ -5,11 +5,11 @@ import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFileRepos
class DefaultApplicationFileRepository : ApplicationFileRepository { class DefaultApplicationFileRepository : ApplicationFileRepository {
override fun exists(applicationFileName: ApplicationFileName?): Boolean { override fun exists(applicationFileName: ApplicationFileName?) {
if (applicationFileName == null) { if (applicationFileName != null) {
return true if (!genericFileExistenceCheck(applicationFileName.fullqualified())) {
throw RuntimeException("Application file not found. Please check if path is correct.")
}
} }
return genericFileExistenceCheck(applicationFileName.fileName)
} }
} }

View file

@ -0,0 +1,44 @@
package org.domaindrivenarchitecture.provs.server.infrastructure
import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFileName
import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFileRepository
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.Assertions.assertEquals
import java.io.File
import java.nio.file.Paths
internal class DefaultApplicationFileRepositoryKtTest {
@Test
fun existsThrowsRuntimeException() {
//when
val invalidFileName: ApplicationFileName = ApplicationFileName("iDontExist")
val repo: ApplicationFileRepository = DefaultApplicationFileRepository()
// then
val exception = assertThrows<RuntimeException>(
"Should not find the file."
) { repo.exists(invalidFileName) }
assertEquals(
"Application file not found. Please check if path is correct.",
exception.message)
}
@Test
fun existsPasses() {
//when
val validFileName = "iExist"
File(validFileName).createNewFile()
val validFile: ApplicationFileName =
ApplicationFileName(File(validFileName).absolutePath)
val repo: ApplicationFileRepository = DefaultApplicationFileRepository()
// then
repo.exists(validFile)
File(validFileName).deleteOnExit()
}
}