From df124f0c25c37d85117bc16f3a7c8e5f212106e1 Mon Sep 17 00:00:00 2001 From: ansgarz Date: Fri, 25 Mar 2022 21:25:00 +0100 Subject: [PATCH] add spec from resource --- .gitlab-ci.yml | 2 +- .../provs/syspec/domain/SyspecService.kt | 19 ++++++-- .../syspec/infrastructure/SyspecConfigRepo.kt | 16 +++++++ .../infrastructure/SyspecConfigRepoKtTest.kt | 47 +++++++++++++++++++ src/test/resources/syspec-config.yaml | 3 ++ 5 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 src/test/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepoKtTest.kt create mode 100644 src/test/resources/syspec-config.yaml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 55fa346..1e8d530 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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 diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/domain/SyspecService.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/domain/SyspecService.kt index 389e3bc..dd9ef83 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/domain/SyspecService.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/domain/SyspecService.kt @@ -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) } diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepo.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepo.kt index b8296b3..9168544 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepo.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepo.kt @@ -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 + } +} diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepoKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepoKtTest.kt new file mode 100644 index 0000000..580e7b0 --- /dev/null +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepoKtTest.kt @@ -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) + } +} \ No newline at end of file diff --git a/src/test/resources/syspec-config.yaml b/src/test/resources/syspec-config.yaml new file mode 100644 index 0000000..c450d70 --- /dev/null +++ b/src/test/resources/syspec-config.yaml @@ -0,0 +1,3 @@ +command: + - command: "echo just_for_test" + out: "just_for_test"