Skip to content

Commit

Permalink
Merge pull request #1448 from znsio/test-assert-fixes
Browse files Browse the repository at this point in the history
Adjustments to the logic of custom assertions within response values.
  • Loading branch information
joelrosario authored Nov 23, 2024
2 parents 28eb2cd + e6e1bd9 commit 06dbcf6
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class ExampleFromFile(val json: JSONObjectValue, val file: File) {
values,
name = testName,
fileSource = this.file.canonicalPath,
responseExampleForValidation = responseExample,
exactResponseExample = responseExample.takeUnless { this.isPartial() },
responseExampleForAssertion = response,
requestExample = scenarioStub.getRequestWithAdditionalParamsIfAny(specmaticConfig.additionalExampleParamsFilePath),
responseExample = response.takeUnless { this.isPartial() },
isPartial = scenarioStub.partial != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ class OpenApiSpecification(
} else valueString
},
name = exampleName,
responseExampleForValidation = if(resolvedResponseExample != null && responseExample.isNotEmpty()) resolvedResponseExample else null,
exactResponseExample = if(resolvedResponseExample != null && responseExample.isNotEmpty()) resolvedResponseExample else null,
requestExample = requestExampleAsHttpRequests[exampleName]?.first(),
responseExample = responseExample
)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/io/specmatic/core/Feature.kt
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ data class Feature(
val scenarioStub = ScenarioStub.readFromFile(File(filePath))

val originalScenario = scenarios.firstOrNull { scenario ->
scenario.matches(scenarioStub.request, scenarioStub.response) is Result.Success
scenario.matches(scenarioStub.request, scenarioStub.response, DefaultMismatchMessages, flagsBased) is Result.Success
} ?: return HasFailure(Result.Failure("Could not find an API matching example $filePath"))

val concreteTestScenario = Scenario(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ data class HttpResponsePattern(

fun withResponseExampleValue(row: Row, resolver: Resolver): HttpResponsePattern =
attempt(breadCrumb = "RESPONSE") {
val responseExample: ResponseExample = row.responseExampleForValidation.takeIf { !row.isPartial } ?: return@attempt this
val responseExample: ResponseExample = row.exactResponseExample ?: return@attempt this

val responseExampleMatchResult = matches(responseExample.responseExample, resolver)

Expand Down
10 changes: 7 additions & 3 deletions core/src/main/kotlin/io/specmatic/core/Scenario.kt
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,14 @@ data class Scenario(
}
}

fun matches(httpRequest: HttpRequest, httpResponse: HttpResponse, mismatchMessages: MismatchMessages = DefaultMismatchMessages, unexpectedKeyCheck: UnexpectedKeyCheck? = null): Result {
val resolver = updatedResolver(mismatchMessages, unexpectedKeyCheck).copy(context = RequestContext(httpRequest))
fun matchesResponse(httpRequest: HttpRequest, httpResponse: HttpResponse, mismatchMessages: MismatchMessages = DefaultMismatchMessages, unexpectedKeyCheck: UnexpectedKeyCheck? = null): Result {
val updatedUnexpectedKeyCheck = if (isRequestAttributeSelected(httpRequest)) {
ValidateUnexpectedKeys
} else unexpectedKeyCheck

return matches(httpResponse, mismatchMessages, unexpectedKeyCheck, resolver)
val resolver = updatedResolver(mismatchMessages, updatedUnexpectedKeyCheck).copy(context = RequestContext(httpRequest))

return matches(httpResponse, mismatchMessages, updatedUnexpectedKeyCheck, resolver)
}

fun matches(httpRequest: HttpRequest, httpResponse: HttpResponse, mismatchMessages: MismatchMessages, flagsBased: FlagsBased): Result {
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/kotlin/io/specmatic/core/pattern/Row.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ data class Row(
val name: String = "",
val fileSource: String? = null,
val requestBodyJSONExample: JSONExample? = null,
val responseExampleForValidation: ResponseExample? = null,
val responseExampleForAssertion: HttpResponse? = null,
val exactResponseExample: ResponseExample? = null,
val requestExample: HttpRequest? = null,
val responseExample: HttpResponse? = null,
val isPartial: Boolean = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object ExamplePostValidator: ResponseValidator {
}

private fun Row.toAsserts(): List<Assert> {
val responseExampleBody = this.responseExampleForValidation?.responseExample ?: return emptyList()
val responseExampleBody = this.responseExampleForAssertion ?: return emptyList()

val headerAsserts = responseExampleBody.headers.map {
parsedAssert("RESPONSE.HEADERS", it.key, StringValue(it.value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ object ExampleProcessor {
return
}

val bodyToCheck = exampleRow.responseExample?.body ?: exampleRow.responseExampleForValidation?.responseExample?.body
val bodyToCheck = exampleRow.responseExampleForAssertion?.body
bodyToCheck?.ifContainsStoreToken { type ->
runningEntity = when (type) {
StoreType.REPLACE -> httpResponse.body.toFactStore(prefix = "ENTITY")
Expand Down
11 changes: 2 additions & 9 deletions core/src/main/kotlin/io/specmatic/test/ScenarioAsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,10 @@ data class ScenarioAsTest(
}
}

private fun testResult(
request: HttpRequest,
response: HttpResponse,
testScenario: Scenario,
flagsBased: FlagsBased? = null
): Result {

private fun testResult(request: HttpRequest, response: HttpResponse, testScenario: Scenario, flagsBased: FlagsBased): Result {
val result = when {
response.specmaticResultHeaderValue() == "failure" -> Result.Failure(response.body.toStringLiteral()).updateScenario(testScenario)
else -> testScenario.matches(request, response, ContractAndResponseMismatch, flagsBased?.unexpectedKeyCheck ?: ValidateUnexpectedKeys)
else -> testScenario.matchesResponse(request, response, ContractAndResponseMismatch, flagsBased.unexpectedKeyCheck ?: ValidateUnexpectedKeys)
}

if (result is Result.Success && result.isPartialSuccess()) {
Expand All @@ -143,7 +137,6 @@ data class ScenarioAsTest(

return result
}

}

private fun LogMessage.withComment(comment: String?): LogMessage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ Pet:
val (scenarioInfos, _) = openApiSpecification.toScenarioInfos()

val examples = scenarioInfos.first().examples.flatMap {
it.rows.map { row -> row.responseExampleForValidation }
it.rows.map { row -> row.exactResponseExample }
}
examples.forEach {
assertThat(it).isInstanceOf(ResponseValueExample::class.java)
Expand All @@ -364,7 +364,7 @@ Pet:
val (scenarioInfos, _) = openApiSpecification.toScenarioInfos()

val examples = scenarioInfos.first().examples.flatMap {
it.rows.map { row -> row.responseExampleForValidation }
it.rows.map { row -> row.exactResponseExample }
}
examples.forEach {
assertThat(it).isNull()
Expand Down

0 comments on commit 06dbcf6

Please sign in to comment.