Skip to content

Commit

Permalink
Merge pull request #1506 from znsio/entity_store_support_for_other_libs
Browse files Browse the repository at this point in the history
Add ability to store and update entity in other libs
  • Loading branch information
joelrosario authored Dec 27, 2024
2 parents 2bdead4 + fb58844 commit 3240189
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions core/src/main/kotlin/io/specmatic/test/ExamplePreProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ val SUBSTITUTE_PATTERN = Regex("^\\$(\\w+)?\\((.*)\\)$")

enum class SubstitutionType { SIMPLE, DELAYED_RANDOM }

enum class StoreType { REPLACE, MERGE }
enum class StoreType(val type: String, val grammar: String) {
REPLACE("save", "as"), MERGE("merge", "with");
}

object ExampleProcessor {
private var runningEntity: Map<String, Value> = mapOf()
Expand Down Expand Up @@ -157,12 +159,7 @@ object ExampleProcessor {
}

private fun toStoreErrorMessage(exampleRow: Row, type: StoreType): String {
val (storeType, storeGrammar) = when (type) {
StoreType.REPLACE -> "save" to "as"
StoreType.MERGE -> "merge" to "with"
}

return "Could not $storeType http response body $storeGrammar ENTITY for example ${exampleRow.name.quote()}"
return "Could not ${type.type} http response body ${type.grammar} ENTITY for example ${exampleRow.name.quote()}"
}

/* STORE HELPERS */
Expand Down Expand Up @@ -192,8 +189,15 @@ object ExampleProcessor {
}
}

fun store(exampleValue: Value) {
runningEntity = exampleValue.toFactStore(prefix = "ENTITY")
fun store(actualValue: Value, exampleValue: Value) {
exampleValue.ifContainsStoreToken { type ->
val actualJsonObjectValue = actualValue.getJsonObjectIfExists()
?: throw ContractException(errorMessage = "Could not ${type.type} value ${type.grammar} ENTITY")
runningEntity = when (type) {
StoreType.REPLACE -> actualJsonObjectValue.toFactStore(prefix = "ENTITY")
StoreType.MERGE -> runningEntity.plus(actualJsonObjectValue.toFactStore(prefix = "ENTITY"))
}
}
}

private fun Value.ifContainsStoreToken(block: (storeType: StoreType) -> Unit) {
Expand Down

0 comments on commit 3240189

Please sign in to comment.