Refactoring of input validation
Move application file validation to provisionK3s. Add an ApplicationFileRepository. Also unit tests for the functions.
This commit is contained in:
parent
1ad357ba44
commit
05821540fe
5 changed files with 52 additions and 13 deletions
|
@ -37,10 +37,6 @@ fun main(args: Array<String>) {
|
|||
println("Config file not found. Please check if path is correct.")
|
||||
exitProcess(1)
|
||||
}
|
||||
if (!(cmd as K3sCliCommand).isValidApplicationFileName()) {
|
||||
println("Application file not found. Please check if path is correct.")
|
||||
exitProcess(1)
|
||||
}
|
||||
if (!cmd.isValidServerType()) {
|
||||
throw RuntimeException("Unknown serverType. Currently only k3s is accepted.")
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
package org.domaindrivenarchitecture.provs.server.domain.k3s
|
||||
|
||||
interface ApplicationFileRepository {
|
||||
fun exists(applicationFileName: ApplicationFileName?): Boolean
|
||||
fun exists(applicationFileName: ApplicationFileName?)
|
||||
}
|
|
@ -15,9 +15,8 @@ fun Prov.provisionK3s(cli: K3sCliCommand) = task {
|
|||
// full k3s
|
||||
val k3sConfig: K3sConfig = getK3sConfig(cli.configFileName)
|
||||
val repo: ApplicationFileRepository = DefaultApplicationFileRepository()
|
||||
if(repo.exists(cli.applicationFileName)) {
|
||||
provisionK3sWorker(k3sConfig, grafanaConfigResolved, cli.applicationFileName)
|
||||
}
|
||||
repo.exists(cli.applicationFileName)
|
||||
provisionK3sWorker(k3sConfig, grafanaConfigResolved, cli.applicationFileName)
|
||||
}
|
||||
else if (cli.reprovision) { // TODO: Add logic that overrides config, when cmd option is set
|
||||
deprovisionK3sInfra()
|
||||
|
|
|
@ -5,11 +5,11 @@ import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFileRepos
|
|||
|
||||
class DefaultApplicationFileRepository : ApplicationFileRepository {
|
||||
|
||||
override fun exists(applicationFileName: ApplicationFileName?): Boolean {
|
||||
if (applicationFileName == null) {
|
||||
return true
|
||||
override fun exists(applicationFileName: ApplicationFileName?) {
|
||||
if (applicationFileName != null) {
|
||||
if (!genericFileExistenceCheck(applicationFileName.fullqualified())) {
|
||||
throw RuntimeException("Application file not found. Please check if path is correct.")
|
||||
}
|
||||
}
|
||||
return genericFileExistenceCheck(applicationFileName.fileName)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue