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

Validate externalized example responses #1164

Merged
merged 2 commits into from
Jul 5, 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
49 changes: 44 additions & 5 deletions core/src/main/kotlin/in/specmatic/conversions/ExampleFromFile.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package `in`.specmatic.conversions

import `in`.specmatic.core.HttpResponse
import `in`.specmatic.core.log.logger
import `in`.specmatic.core.pattern.ContractException
import `in`.specmatic.core.pattern.Row
import `in`.specmatic.core.pattern.attempt
import `in`.specmatic.core.pattern.parsedJSONObject
import `in`.specmatic.core.pattern.*
import `in`.specmatic.core.value.EmptyString
import `in`.specmatic.core.value.JSONObjectValue
import `in`.specmatic.core.value.Value
import java.io.File
Expand All @@ -25,18 +24,58 @@ class ExampleFromFile(val json: JSONObjectValue, val file: File) {
entry.map { it.key } to entry.map { it.value }
}

val environmentAndPropertiesConfiguration: EnvironmentAndPropertiesConfiguration = EnvironmentAndPropertiesConfiguration()

val responseExample = response?.let { httpResponse ->
when {
environmentAndPropertiesConfiguration.validateResponseValue() ->
ResponseValueExample(httpResponse)

else ->
ResponseSchemaExample(httpResponse)
}

}

return Row(
columnNames,
values,
name = testName,
fileSource = this.file.canonicalPath
fileSource = this.file.canonicalPath,
responseExample = responseExample
)
}

constructor(file: File) : this(parsedJSONObject(file.readText()), file)

val expectationFilePath: String = file.canonicalPath

val response: HttpResponse?
get() {
if(responseBody == null && responseHeaders == null)
return null

val body = responseBody ?: EmptyString
val headers = responseHeaders ?: JSONObjectValue()

return HttpResponse(responseStatus, headers.jsonObject.mapValues { it.value.toStringLiteral() }, body)
}

val responseBody: Value? = attempt("Error reading response body in file ${file.parentFile.canonicalPath}") {
json.findFirstChildByPath("http-response.body")
}

val responseHeaders: JSONObjectValue? = attempt("Error reading response headers in file ${file.parentFile.canonicalPath}") {
val headers = json.findFirstChildByPath("http-response.headers")
if(headers == null)
return@attempt null

if(headers !is JSONObjectValue)
throw ContractException("http-response.headers should be a JSON object, but instead it was ${headers.toStringLiteral()}")

headers
}

val responseStatus: Int = attempt("Error reading status in file ${file.parentFile.canonicalPath}") {
json.findFirstChildByPath("http-response.status")?.toStringLiteral()?.toInt()
} ?: throw ContractException("Response status code was not found.")
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/kotlin/in/specmatic/core/Scenario.kt
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ data class Scenario(
override fun expectedKeyWasMissing(keyLabel: String, keyName: String): String {
return "The $keyLabel $keyName in the specification was missing in example ${row.name}"
}

}
},
mockMode = true
)

try {
Expand Down
254 changes: 254 additions & 0 deletions core/src/test/kotlin/in/specmatic/core/ScenarioTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
package `in`.specmatic.core

import com.fasterxml.jackson.annotation.JsonFormat.Value
import `in`.specmatic.core.pattern.*
import org.assertj.core.api.Assertions.*
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import java.util.function.Consumer

class ScenarioTest {
@Test
fun `should validate and reject an invalid response in an example row`() {
val scenario = Scenario(
"",
HttpRequestPattern(
method = "POST",
body = JSONObjectPattern(
pattern = mapOf(
"id" to NumberPattern()
)
)
),
HttpResponsePattern(
status = 200,
body = JSONObjectPattern(
pattern = mapOf(
"id" to NumberPattern()
)
)
),
emptyMap(),
listOf<Examples>(
Examples(
listOf("(REQUEST-BODY)"),
listOf(Row(
mapOf("(REQUEST-BODY)" to """{"id": 10}""")
).copy(responseExample = ResponseSchemaExample(HttpResponse(200, """{"id": "abc123"}"""))))
)
),
emptyMap(),
emptyMap()
)

assertThatThrownBy {
scenario.validExamplesOrException(DefaultStrategies)
}.satisfies(Consumer {
assertThat(it)
.hasMessageContaining("abc123")
.hasMessageContaining("RESPONSE.BODY.id")
})
}

@Test
fun `should validate and reject an invalid request in an example row`() {
val scenario = Scenario(
"",
HttpRequestPattern(
method = "POST",
body = JSONObjectPattern(
pattern = mapOf(
"id" to NumberPattern()
)
)
),
HttpResponsePattern(
status = 200,
body = JSONObjectPattern(
pattern = mapOf(
"id" to NumberPattern()
)
)
),
emptyMap(),
listOf<Examples>(
Examples(
listOf("(REQUEST-BODY)"),
listOf(Row(
mapOf("(REQUEST-BODY)" to """{"id": "abc123" }""")
).copy(responseExample = ResponseSchemaExample(HttpResponse(200, """{"id": 10}"""))))
)
),
emptyMap(),
emptyMap()
)

assertThatThrownBy {
scenario.validExamplesOrException(DefaultStrategies)
}.satisfies(Consumer {
assertThat(it)
.hasMessageContaining("abc123")
.hasMessageContaining("REQUEST.BODY.id")
})
}

@Test
fun `should validate and accept a response in an example row with pattern specification as value`() {
val scenario = Scenario(
"",
HttpRequestPattern(
method = "POST",
body = JSONObjectPattern(
pattern = mapOf(
"id" to NumberPattern()
)
)
),
HttpResponsePattern(
status = 200,
body = JSONObjectPattern(
pattern = mapOf(
"id" to NumberPattern()
)
)
),
emptyMap(),
listOf<Examples>(
Examples(
listOf("(REQUEST-BODY)"),
listOf(Row(
mapOf("(REQUEST-BODY)" to """{"id": 10}""")
).copy(responseExample = ResponseSchemaExample(HttpResponse(200, """{"id": "(number)"}"""))))
)
),
emptyMap(),
emptyMap()
)

assertThatCode {
scenario.validExamplesOrException(DefaultStrategies)
}.doesNotThrowAnyException()
}

@Test
fun `should validate and accept a request in an example row with pattern specification as value`() {
val scenario = Scenario(
"",
HttpRequestPattern(
method = "POST",
body = JSONObjectPattern(
pattern = mapOf(
"id" to NumberPattern()
)
)
),
HttpResponsePattern(
status = 200,
body = JSONObjectPattern(
pattern = mapOf(
"id" to NumberPattern()
)
)
),
emptyMap(),
listOf<Examples>(
Examples(
listOf("(REQUEST-BODY)"),
listOf(Row(
mapOf("(REQUEST-BODY)" to """{"id": "(number)" }""")
).copy(responseExample = ResponseSchemaExample(HttpResponse(200, """{"id": 10}"""))))
)
),
emptyMap(),
emptyMap()
)

assertThatCode {
scenario.validExamplesOrException(DefaultStrategies)
}.doesNotThrowAnyException()
}

@Test
fun `should validate and reject a response in an example row with a non-matching pattern specification as value`() {
val scenario = Scenario(
"",
HttpRequestPattern(
method = "POST",
body = JSONObjectPattern(
pattern = mapOf(
"id" to NumberPattern()
)
)
),
HttpResponsePattern(
status = 200,
body = JSONObjectPattern(
pattern = mapOf(
"id" to NumberPattern()
)
)
),
emptyMap(),
listOf<Examples>(
Examples(
listOf("(REQUEST-BODY)"),
listOf(Row(
mapOf("(REQUEST-BODY)" to """{"id": 10}""")
).copy(responseExample = ResponseSchemaExample(HttpResponse(200, """{"id": "(string)"}"""))))
)
),
emptyMap(),
emptyMap()
)

assertThatCode {
scenario.validExamplesOrException(DefaultStrategies)
}.satisfies(Consumer {
assertThat(it)
.hasMessageContaining("string")
.hasMessageContaining("RESPONSE.BODY.id")
})
}

@Test
fun `should validate and accept a request in an example row with a non-matching pattern specification as value`() {
val scenario = Scenario(
"",
HttpRequestPattern(
method = "POST",
body = JSONObjectPattern(
pattern = mapOf(
"id" to NumberPattern()
)
)
),
HttpResponsePattern(
status = 200,
body = JSONObjectPattern(
pattern = mapOf(
"id" to NumberPattern()
)
)
),
emptyMap(),
listOf<Examples>(
Examples(
listOf("(REQUEST-BODY)"),
listOf(Row(
mapOf("(REQUEST-BODY)" to """{"id": "(string)" }""")
).copy(responseExample = ResponseSchemaExample(HttpResponse(200, """{"id": 10}"""))))
)
),
emptyMap(),
emptyMap()
)

assertThatThrownBy {
scenario.validExamplesOrException(DefaultStrategies)
}.satisfies(Consumer {
assertThat(it)
.hasMessageContaining("string")
.hasMessageContaining("REQUEST.BODY.id")
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
},
"http-response": {
"status": 200,
"body": {
"id": 10
}
"body": "success"
}
}