Skip to content

Commit

Permalink
Rename SpecmaticConfigFactory to VersionAwareConfigParser and moved c…
Browse files Browse the repository at this point in the history
…urrent implementation to extension functions
  • Loading branch information
sukesh2000 committed Jan 14, 2025
1 parent 579d903 commit 1d861cd
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,7 +23,7 @@ class MigrateConfigCommand : Callable<Int> {
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}")
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/kotlin/io/specmatic/core/SpecmaticConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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 <kotlin.version></kotlin.version> to your pom project.")
throw e
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit 1d861cd

Please sign in to comment.