diff --git a/application/src/main/kotlin/application/MigrateConfigCommand.kt b/application/src/main/kotlin/application/MigrateConfigCommand.kt index 725707135..a9e66e1a0 100644 --- a/application/src/main/kotlin/application/MigrateConfigCommand.kt +++ b/application/src/main/kotlin/application/MigrateConfigCommand.kt @@ -1,6 +1,6 @@ package application -import io.specmatic.core.config.SpecmaticConfigFactory +import io.specmatic.core.config.toSpecmaticConfig import io.specmatic.core.log.logger import picocli.CommandLine.Command import picocli.CommandLine.Option @@ -23,7 +23,7 @@ class MigrateConfigCommand : Callable { var version: Int = 1 override fun call(): Int { - val specmaticConfig = SpecmaticConfigFactory().create(inputFile) + val specmaticConfig = inputFile.toSpecmaticConfig() val newConfigYaml = specmaticConfig.transformTo(version) logger.log("Writing merged contract file to ${outputFile.path}") diff --git a/core/src/main/kotlin/io/specmatic/core/SpecmaticConfig.kt b/core/src/main/kotlin/io/specmatic/core/SpecmaticConfig.kt index 402dacba7..492da4818 100644 --- a/core/src/main/kotlin/io/specmatic/core/SpecmaticConfig.kt +++ b/core/src/main/kotlin/io/specmatic/core/SpecmaticConfig.kt @@ -5,7 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.dataformat.yaml.YAMLFactory import com.fasterxml.jackson.module.kotlin.registerKotlinModule import io.specmatic.core.Configuration.Companion.configFilePath -import io.specmatic.core.config.SpecmaticConfigFactory +import io.specmatic.core.config.toSpecmaticConfig import io.specmatic.core.config.v1.SpecmaticConfigV1 import io.specmatic.core.config.v2.ContractConfig import io.specmatic.core.config.v2.FileSystemConfig @@ -45,7 +45,6 @@ val CONTRACT_EXTENSIONS = listOf(CONTRACT_EXTENSION, WSDL) + OPENAPI_FILE_EXTENS const val DATA_DIR_SUFFIX = "_data" const val TEST_DIR_SUFFIX = "_tests" const val EXAMPLES_DIR_SUFFIX = "_examples" -const val DICTIONARY_FILE_SUFFIX = "_dictionary.json" const val SPECMATIC_GITHUB_ISSUES = "https://github.com/znsio/specmatic/issues" const val DEFAULT_WORKING_DIRECTORY = ".$APPLICATION_NAME_LOWER_CASE" @@ -415,7 +414,7 @@ fun loadSpecmaticConfig(configFileName: String? = null): SpecmaticConfig { throw ContractException("Could not find the Specmatic configuration at path ${configFile.canonicalPath}") } try { - return SpecmaticConfigFactory().create(configFile) + return configFile.toSpecmaticConfig() } catch(e: LinkageError) { logger.log(e, "A dependency version conflict has been detected. If you are using Spring in a maven project, a common resolution is to set the property to your pom project.") throw e diff --git a/core/src/main/kotlin/io/specmatic/core/config/SpecmaticConfigFactory.kt b/core/src/main/kotlin/io/specmatic/core/config/SpecmaticConfigFactory.kt deleted file mode 100644 index 6eb6a3411..000000000 --- a/core/src/main/kotlin/io/specmatic/core/config/SpecmaticConfigFactory.kt +++ /dev/null @@ -1,27 +0,0 @@ -package io.specmatic.core.config - -import com.fasterxml.jackson.databind.ObjectMapper -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory -import com.fasterxml.jackson.module.kotlin.registerKotlinModule -import io.specmatic.core.SpecmaticConfig -import io.specmatic.core.config.v1.SpecmaticConfigV1 -import io.specmatic.core.config.v2.SpecmaticConfigV2 -import java.io.File - -private const val SPECMATIC_CONFIG_VERSION = "version" - -class SpecmaticConfigFactory { - private val objectMapper = ObjectMapper(YAMLFactory()).registerKotlinModule() - - fun create(configFile: File): SpecmaticConfig { - val configYaml = configFile.readText() - return when (getVersion(configYaml)) { - 2 -> objectMapper.readValue(configYaml, SpecmaticConfigV2::class.java).transform() - else -> objectMapper.readValue(configYaml, SpecmaticConfigV1::class.java).transform() - } - } - - private fun getVersion(configYaml: String): Int? { - return objectMapper.readTree(configYaml).get(SPECMATIC_CONFIG_VERSION)?.intValue() - } -} \ No newline at end of file diff --git a/core/src/main/kotlin/io/specmatic/core/config/VersionAwareConfigParser.kt b/core/src/main/kotlin/io/specmatic/core/config/VersionAwareConfigParser.kt new file mode 100644 index 000000000..ce6f09f73 --- /dev/null +++ b/core/src/main/kotlin/io/specmatic/core/config/VersionAwareConfigParser.kt @@ -0,0 +1,25 @@ +package io.specmatic.core.config + +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory +import com.fasterxml.jackson.module.kotlin.registerKotlinModule +import io.specmatic.core.SpecmaticConfig +import io.specmatic.core.config.v1.SpecmaticConfigV1 +import io.specmatic.core.config.v2.SpecmaticConfigV2 +import java.io.File + +private const val SPECMATIC_CONFIG_VERSION = "version" + +private val objectMapper = ObjectMapper(YAMLFactory()).registerKotlinModule() + +fun File.toSpecmaticConfig(): SpecmaticConfig { + val configYaml = this.readText() + return when (configYaml.getVersion()) { + 2 -> objectMapper.readValue(configYaml, SpecmaticConfigV2::class.java).transform() + else -> objectMapper.readValue(configYaml, SpecmaticConfigV1::class.java).transform() + } +} + +private fun String.getVersion(): Int? { + return objectMapper.readTree(this).get(SPECMATIC_CONFIG_VERSION)?.intValue() +} diff --git a/core/src/test/kotlin/io/specmatic/core/config/SpecmaticConfigFactoryTest.kt b/core/src/test/kotlin/io/specmatic/core/config/VersionAwareConfigParserTest.kt similarity index 91% rename from core/src/test/kotlin/io/specmatic/core/config/SpecmaticConfigFactoryTest.kt rename to core/src/test/kotlin/io/specmatic/core/config/VersionAwareConfigParserTest.kt index 5ea1399e8..d57ccc3a2 100644 --- a/core/src/test/kotlin/io/specmatic/core/config/SpecmaticConfigFactoryTest.kt +++ b/core/src/test/kotlin/io/specmatic/core/config/VersionAwareConfigParserTest.kt @@ -6,13 +6,13 @@ import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.CsvSource import java.io.File -class SpecmaticConfigFactoryTest { +class VersionAwareConfigParserTest { @CsvSource( "./src/test/resources/specmaticConfigFiles/specmatic_alias.yaml" ) @ParameterizedTest fun `should create SpecmaticConfig given version 1 config file`(configFile: String) { - val specmaticConfig = SpecmaticConfigFactory().create(File(configFile)) + val specmaticConfig = File(configFile).toSpecmaticConfig() assertThat(specmaticConfig.version).isIn(null, 1) @@ -28,7 +28,7 @@ class SpecmaticConfigFactoryTest { ) @ParameterizedTest fun `should create SpecmaticConfig given version 2 config file`(configFile: String) { - val specmaticConfig = SpecmaticConfigFactory().create(File(configFile)) + val specmaticConfig = File(configFile).toSpecmaticConfig() assertThat(specmaticConfig.version).isEqualTo(2)