add spec from resource

This commit is contained in:
ansgarz 2022-03-25 21:25:00 +01:00
parent 5f897a3be5
commit df124f0c25
5 changed files with 83 additions and 4 deletions

View file

@ -106,7 +106,7 @@ publish-snapshot-lib:
rules:
- if: $CI_PIPELINE_SOURCE != "push"
when: never
- if: $CI_COMMIT_TAG !~ /^[0-9]+[.][0-9]+([.][0-9]+-SNAPSHOT)?$/
- if: $CI_COMMIT_TAG !~ /^release-[0-9]+[.][0-9]+([.][0-9]+)?$/
script:
- ./gradlew -x assemble -x test jar
- ./gradlew -x assemble -x test publish

View file

@ -4,14 +4,27 @@ import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName
import org.domaindrivenarchitecture.provs.framework.core.Prov
import org.domaindrivenarchitecture.provs.framework.core.ProvResult
import org.domaindrivenarchitecture.provs.syspec.infrastructure.findSpecConfigFromFile
import org.domaindrivenarchitecture.provs.syspec.infrastructure.findSpecConfigFromResource
import org.domaindrivenarchitecture.provs.syspec.infrastructure.verifySpecConfig
fun Prov.verifySpec(config: ConfigFileName?) = task {
val spec = findSpecConfigFromFile(config)
fun Prov.verifySpec(configFile: ConfigFileName? = null) = task {
val spec = findSpecConfigFromFile(configFile)
if (spec == null) {
ProvResult(false, "Could not read file: ${config?.fileName}")
ProvResult(false, "Could not read file: ${configFile?.fileName}")
} else {
verifySpecConfig(spec)
}
}
@Suppress("unused") // Api
fun Prov.verifySpecFromResource(resourceName: String) = task {
val spec = findSpecConfigFromResource(resourceName)
if (spec == null) {
ProvResult(false, "Could not read resource: ${resourceName}")
} else {
verifySpecConfig(spec)
}

View file

@ -45,3 +45,19 @@ internal fun findSpecConfigFromFile(file: ConfigFileName? = null): SpecConfig? {
null
}
}
internal fun getSpecConfigFromResource(resourcePath: String): SpecConfig {
val resource = Thread.currentThread().contextClassLoader.getResource(resourcePath)
requireNotNull(resource) { "Resource $resourcePath not found" }
return resource.readText().yamlToType()
}
internal fun findSpecConfigFromResource(resourcePath: String): SpecConfig? {
return try {
val config = getSpecConfigFromResource(resourcePath)
config
} catch (e: IllegalArgumentException) {
println("Error: " + e.message)
null
}
}

View file

@ -0,0 +1,47 @@
package org.domaindrivenarchitecture.provs.syspec.infrastructure
import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName
import org.domaindrivenarchitecture.provs.syspec.domain.CommandSpec
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
internal class SyspecConfigRepoKtTest {
@Test
fun getSpecConfigFromFile_success() {
// when
@Suppress("RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS") // null would reveal test error
val filePath = javaClass.classLoader.getResource("syspec-config.yaml").file
val res = getSpecConfigFromFile(ConfigFileName(filePath))
// then
assertEquals(listOf(CommandSpec("echo just_for_test", "just_for_test")), res.command)
}
@Test
fun getSpecConfigFromResource_success() {
// when
val res = getSpecConfigFromResource("syspec-config.yaml")
// then
assertEquals(listOf(CommandSpec("echo just_for_test", "just_for_test")), res.command)
}
@Test
fun findSpecConfigFromFile_null() {
// when
val res = findSpecConfigFromFile(ConfigFileName("dontexist"))
// then
assertNull(res)
}
@Test
fun findSpecConfigFromResource_null() {
// when
val res = findSpecConfigFromResource("dontexist")
// then
assertNull(res)
}
}

View file

@ -0,0 +1,3 @@
command:
- command: "echo just_for_test"
out: "just_for_test"