Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Test Field of Specmatic Config Private #1581

Merged
merged 8 commits into from
Feb 6, 2025
10 changes: 1 addition & 9 deletions core/src/main/kotlin/io/specmatic/core/Feature.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,11 @@ data class Feature(
val strictMode: Boolean = false
): IFeature {
fun enableGenerativeTesting(onlyPositive: Boolean = false): Feature {
val updatedSpecmaticConfig = specmaticConfig.copy(
test = specmaticConfig.test?.copy(
resiliencyTests = specmaticConfig.test.resiliencyTests?.copy(
enable = if(onlyPositive) ResiliencyTestSuite.positiveOnly else ResiliencyTestSuite.all
)
)
)

return this.copy(flagsBased = this.flagsBased.copy(
generation = GenerativeTestsEnabled(onlyPositive),
positivePrefix = POSITIVE_TEST_DESCRIPTION_PREFIX,
negativePrefix = NEGATIVE_TEST_DESCRIPTION_PREFIX),
specmaticConfig = updatedSpecmaticConfig
specmaticConfig = specmaticConfig.copyResiliencyTestsConfig(onlyPositive)
)
}

Expand Down
67 changes: 54 additions & 13 deletions core/src/main/kotlin/io/specmatic/core/SpecmaticConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ data class SpecmaticConfig(
private val repository: RepositoryInfo? = null,
val report: ReportConfiguration? = null,
private val security: SecurityConfiguration? = null,
val test: TestConfiguration? = TestConfiguration(),
private val test: TestConfiguration? = TestConfiguration(),
val stub: StubConfiguration = StubConfiguration(),
val virtualService: VirtualServiceConfiguration = VirtualServiceConfiguration(),
private val examples: List<String>? = null,
Expand All @@ -145,6 +145,11 @@ data class SpecmaticConfig(
fun getSecurityConfiguration(specmaticConfig: SpecmaticConfig?): SecurityConfiguration? {
return specmaticConfig?.security
}

@JsonIgnore
fun getTestConfiguration(specmaticConfig: SpecmaticConfig): TestConfiguration? {
return specmaticConfig.test
}
}

@JsonIgnore
Expand All @@ -154,7 +159,7 @@ data class SpecmaticConfig(

@JsonIgnore
fun isExtensibleSchemaEnabled(): Boolean {
return test?.allowExtensibleSchema ?: getBooleanValue(EXTENSIBLE_SCHEMA)
return test?.getAllowExtensibleSchema() ?: getBooleanValue(EXTENSIBLE_SCHEMA)
}

@JsonIgnore
Expand All @@ -169,7 +174,7 @@ data class SpecmaticConfig(

@JsonIgnore
fun isResponseValueValidationEnabled(): Boolean {
return test?.validateResponseValues ?: getBooleanValue(VALIDATE_RESPONSE_VALUE)
return test?.getValidateResponseValues() ?: getBooleanValue(VALIDATE_RESPONSE_VALUE)
}

@JsonIgnore
Expand All @@ -184,7 +189,23 @@ data class SpecmaticConfig(

@JsonIgnore
fun getResiliencyTestsEnable(): ResiliencyTestSuite {
return test?.resiliencyTests?.enable ?: ResiliencyTestSuite.none
return test?.getResiliencyTests()?.getEnableTestSuite() ?: ResiliencyTestSuite.none
}

@JsonIgnore
fun getTestTimeoutInMilliseconds(): Long? {
return test?.getTimeoutInMilliseconds()
}

@JsonIgnore
fun copyResiliencyTestsConfig(onlyPositive: Boolean): SpecmaticConfig {
return this.copy(
test = test?.copy(
resiliencyTests = test.getResiliencyTests().copy(
enable = if (onlyPositive) ResiliencyTestSuite.positiveOnly else ResiliencyTestSuite.all
)
)
)
}

@JsonIgnore
Expand Down Expand Up @@ -278,26 +299,46 @@ data class SpecmaticConfig(
}

data class TestConfiguration(
val resiliencyTests: ResiliencyTestsConfig? = ResiliencyTestsConfig(
isResiliencyTestFlagEnabled = getBooleanValue(SPECMATIC_GENERATIVE_TESTS),
isOnlyPositiveFlagEnabled = getBooleanValue(ONLY_POSITIVE)
),
val validateResponseValues: Boolean? = null,
val allowExtensibleSchema: Boolean? = null,
val timeoutInMilliseconds: Long? = getLongValue(SPECMATIC_TEST_TIMEOUT)
)
private val resiliencyTests: ResiliencyTestsConfig? = null,
private val validateResponseValues: Boolean? = null,
private val allowExtensibleSchema: Boolean? = null,
private val timeoutInMilliseconds: Long? = null
) {
fun getResiliencyTests(): ResiliencyTestsConfig {
joelrosario marked this conversation as resolved.
Show resolved Hide resolved
return resiliencyTests ?: ResiliencyTestsConfig(
isResiliencyTestFlagEnabled = getBooleanValue(SPECMATIC_GENERATIVE_TESTS),
isOnlyPositiveFlagEnabled = getBooleanValue(ONLY_POSITIVE)
)
}

fun getValidateResponseValues(): Boolean? {
return validateResponseValues
}

fun getAllowExtensibleSchema(): Boolean? {
return allowExtensibleSchema
}

fun getTimeoutInMilliseconds(): Long? {
return timeoutInMilliseconds ?: getLongValue(SPECMATIC_TEST_TIMEOUT)
}
}

enum class ResiliencyTestSuite {
all, positiveOnly, none
}

data class ResiliencyTestsConfig(
val enable: ResiliencyTestSuite? = null
private val enable: ResiliencyTestSuite? = null
) {
constructor(isResiliencyTestFlagEnabled: Boolean, isOnlyPositiveFlagEnabled: Boolean) : this(
enable = getEnableFrom(isResiliencyTestFlagEnabled, isOnlyPositiveFlagEnabled)
)

fun getEnableTestSuite(): ResiliencyTestSuite? {
return enable
}

companion object {
private fun getEnableFrom(
isResiliencyTestFlagEnabled: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.specmatic.core.*
import io.specmatic.core.SpecmaticConfig.Companion.getPipeline
import io.specmatic.core.SpecmaticConfig.Companion.getRepository
import io.specmatic.core.SpecmaticConfig.Companion.getSecurityConfiguration
import io.specmatic.core.SpecmaticConfig.Companion.getTestConfiguration
import io.specmatic.core.config.SpecmaticConfigVersion
import io.specmatic.core.config.SpecmaticVersionedConfig
import io.specmatic.core.config.SpecmaticVersionedConfigLoader
Expand Down Expand Up @@ -72,7 +73,7 @@ data class SpecmaticConfigV2(
repository = getRepository(config),
report = config.report,
security = getSecurityConfiguration(config),
test = config.test,
test = getTestConfiguration(config),
stub = config.stub,
virtualService = config.virtualService,
examples = config.getExamples(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ internal class SpecmaticConfigKtTest {
assertThat(htmlConfig?.heading).isEqualTo("Test Results")
assertThat(htmlConfig?.outputDirectory).isEqualTo("output")

assertThat(config.test?.timeoutInMilliseconds).isEqualTo(3000)
assertThat(config.getTestTimeoutInMilliseconds()).isEqualTo(3000)
}

@Test
Expand Down Expand Up @@ -191,7 +191,7 @@ internal class SpecmaticConfigKtTest {
assertThat(config.isExtensibleSchemaEnabled()).isFalse()
assertThat(config.getExamples()).isEqualTo(listOf("folder1/examples", "folder2/examples"))
assertThat(config.stub.delayInMilliseconds).isEqualTo(1000L)
assertThat(config.test?.timeoutInMilliseconds).isEqualTo(5000)
assertThat(config.getTestTimeoutInMilliseconds()).isEqualTo(5000)
} finally {
properties.forEach { System.clearProperty(it.key) }
}
Expand Down Expand Up @@ -240,7 +240,7 @@ internal class SpecmaticConfigKtTest {
assertThat(config.isExtensibleSchemaEnabled()).isTrue()
assertThat(config.getExamples()).isEqualTo(listOf("folder1/examples", "folder2/examples"))
assertThat(config.stub.delayInMilliseconds).isEqualTo(1000L)
assertThat(config.test?.timeoutInMilliseconds).isEqualTo(3000)
assertThat(config.getTestTimeoutInMilliseconds()).isEqualTo(3000)
} finally {
props.forEach { System.clearProperty(it.key) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ open class SpecmaticJUnitSupport {

specmaticConfig = getSpecmaticConfig()

val timeoutInMilliseconds = specmaticConfig?.test?.timeoutInMilliseconds ?: try {
val timeoutInMilliseconds = specmaticConfig?.getTestTimeoutInMilliseconds() ?: try {
getLongValue(SPECMATIC_TEST_TIMEOUT)
} catch (e: NumberFormatException) {
throw ContractException("$SPECMATIC_TEST_TIMEOUT should be a value of type long")
Expand Down