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

Miscellaneous fixes #1021

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions core/.specmatic/web/localhost/random.yaml

This file was deleted.

16 changes: 14 additions & 2 deletions core/src/main/kotlin/in/specmatic/core/Feature.kt
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,20 @@ data class Feature(
}

fun generateContractTestScenarios(suggestions: List<Scenario>): Sequence<Scenario> {
return resolverStrategies.generation.let {
it.positiveTestScenarios(this, suggestions) + it.negativeTestScenarios(this)
return try {
resolverStrategies.generation.let {
it.positiveTestScenarios(this, suggestions) + it.negativeTestScenarios(this)
}
}
catch(e: Throwable) {
logger.log(e)
throw e
}
}

fun validateExampleRows() {
scenarios.forEach { scenario ->
scenario.validateExamples(resolverStrategies)
}
}

Expand Down
10 changes: 10 additions & 0 deletions core/src/main/kotlin/in/specmatic/core/Scenario.kt
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,16 @@ data class Scenario(
}
}

fun validateExamples(
resolverStrategies: ResolverStrategies,
) {
val rowsToValidate = examples.flatMap { it.rows }

rowsToValidate.forEach { row ->
newBasedOn(row, resolverStrategies).first()
}
}

fun generateTestScenarios(
resolverStrategies: ResolverStrategies,
variables: Map<String, String> = emptyMap(),
Expand Down
10 changes: 6 additions & 4 deletions core/src/main/kotlin/in/specmatic/proxy/Proxy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,12 @@ class Proxy(host: String, port: Int, baseURL: String, private val outputDirector
val gherkin = toGherkinFeature("New feature", stubs)
val base = "proxy_generated"
val featureFileName = "$base.yaml"
val openApi = parseGherkinStringToFeature(gherkin).toOpenApi()

if(stubs.isEmpty()) {
println("No stubs were recorded. No contract will be written.")
} else {
outputDirectory.createDirectory()

println("Writing contract to $featureFileName")
outputDirectory.writeText(featureFileName, Yaml.pretty(openApi))

val stubDataDirectory = outputDirectory.subDirectory("${base}_data")
stubDataDirectory.createDirectory()

Expand All @@ -161,6 +157,12 @@ class Proxy(host: String, port: Int, baseURL: String, private val outputDirector
println("Writing stub data to $fileName")
stubDataDirectory.writeText(fileName, namedStub.stub.toJSON().toStringLiteral())
}

val openApi = parseGherkinStringToFeature(gherkin).toOpenApi()

println("Writing specification to $featureFileName")
outputDirectory.writeText(featureFileName, Yaml.pretty(openApi))

}
}
}
4 changes: 2 additions & 2 deletions core/src/test/kotlin/in/specmatic/proxy/ProxyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ internal class ProxyTest {
)
}.doesNotThrowAnyException()
assertThatCode { parsedJSON(fakeFileWriter.receivedStub ?: "") }.doesNotThrowAnyException()
assertThat(fakeFileWriter.receivedPaths.toList()).isEqualTo(listOf("proxy_generated.yaml", "stub0.json"))
assertThat(fakeFileWriter.receivedPaths.toList()).containsExactlyInAnyOrder("proxy_generated.yaml", "stub0.json")
}

@Test
Expand All @@ -108,7 +108,7 @@ internal class ProxyTest {
)
}.doesNotThrowAnyException()
assertThatCode { parsedJSON(fakeFileWriter.receivedStub ?: "") }.doesNotThrowAnyException()
assertThat(fakeFileWriter.receivedPaths).isEqualTo(listOf("proxy_generated.yaml", "stub0.json"))
assertThat(fakeFileWriter.receivedPaths).containsExactlyInAnyOrder("proxy_generated.yaml", "stub0.json")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ open class SpecmaticJUnitSupport {

private fun loadExceptionAsTestError(e: Throwable): Stream<DynamicTest> {
return sequenceOf(DynamicTest.dynamicTest("Load Error") {
logger.log(e)
ResultAssert.assertThat(Result.Failure(exceptionCauseMessage(e))).isSuccess()
}).asStream()
}
Expand Down Expand Up @@ -251,8 +250,6 @@ open class SpecmaticJUnitSupport {
return loadExceptionAsTestError(e)
}

var checkedAPIs = false

val testBaseURL = try {
constructTestBaseURL()
} catch (e: Throwable) {
Expand All @@ -261,16 +258,30 @@ open class SpecmaticJUnitSupport {
throw(e)
}

return try {
dynamicTestStream(testScenarios, testBaseURL, timeout)
} catch(e: Throwable) {
logger.logError(e)
loadExceptionAsTestError(e)
}
}

private fun dynamicTestStream(
testScenarios: Sequence<ContractTest>,
testBaseURL: String,
timeout: Int
): Stream<DynamicTest> {
var checkedAPIs = false
return testScenarios.map { testScenario ->
DynamicTest.dynamicTest(testScenario.testDescription()) {
threads.add(Thread.currentThread().name)

if(!checkedAPIs) {
if (!checkedAPIs) {
checkedAPIs = true

try {
queryActuator()
} catch(exception: Throwable) {
} catch (exception: Throwable) {
logger.log(exception, "Failed to query actuator with error")
}
}
Expand All @@ -297,11 +308,10 @@ open class SpecmaticJUnitSupport {
else -> ResultAssert.assertThat(result).isSuccess()
}

} catch(e: Throwable) {
} catch (e: Throwable) {
throw e
}
finally {
if(testResult != null) {
} finally {
if (testResult != null) {
val (result, response) = testResult
openApiCoverageReportInput.addTestReportRecords(testScenario.testResultRecord(result, response))
}
Expand Down Expand Up @@ -406,7 +416,7 @@ open class SpecmaticJUnitSupport {
securityConfiguration
).copy(testVariables = config.variables, testBaseURLs = config.baseURLs).loadExternalisedExamples()


feature.validateExampleRows()

val suggestions = when {
suggestionsPath.isNotEmpty() -> suggestionsFromFile(suggestionsPath)
Expand Down