Skip to content

Commit

Permalink
Merge pull request #1406 from znsio/additional-example-params-file-in…
Browse files Browse the repository at this point in the history
…-config

Additional example params file in config
  • Loading branch information
joelrosario authored Oct 31, 2024
2 parents 434d822 + 5555c05 commit 0397657
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ExampleFromFile(val json: JSONObjectValue, val file: File) {

}

val requestExample = mockFromJSON(json.jsonObject).getRequestWithAdditionalParamsIfAny()
val requestExample = mockFromJSON(json.jsonObject).getRequestWithAdditionalParamsIfAny(request, specmaticConfig.additionalExampleParamsFilePath)

return Row(
columnNames,
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/kotlin/io/specmatic/core/SpecmaticConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ data class SpecmaticConfig(
val stub: StubConfiguration = StubConfiguration(),
val examples: List<String> = getStringValue(EXAMPLE_DIRECTORIES)?.split(",") ?: emptyList(),
val workflow: WorkflowConfiguration? = null,
val ignoreInlineExamples: Boolean = getBooleanValue(Flags.IGNORE_INLINE_EXAMPLES)
val ignoreInlineExamples: Boolean = getBooleanValue(Flags.IGNORE_INLINE_EXAMPLES),
val additionalExampleParamsFilePath: String? = getStringValue(Flags.ADDITIONAL_EXAMPLE_PARAMS_FILE)
) {
@JsonIgnore
fun isExtensibleSchemaEnabled(): Boolean {
Expand Down
1 change: 1 addition & 0 deletions core/src/main/kotlin/io/specmatic/core/utilities/Flags.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Flags {
const val EXAMPLE_DIRECTORIES = "EXAMPLE_DIRECTORIES"

const val EXTENSIBLE_QUERY_PARAMS = "EXTENSIBLE_QUERY_PARAMS"
const val ADDITIONAL_EXAMPLE_PARAMS_FILE = "ADDITIONAL_EXAMPLE_PARAMS_FILE"

fun getStringValue(flagName: String): String? = System.getenv(flagName) ?: System.getProperty(flagName)

Expand Down
49 changes: 32 additions & 17 deletions core/src/main/kotlin/io/specmatic/mock/ScenarioStub.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package io.specmatic.mock

import com.fasterxml.jackson.databind.ObjectMapper
import io.specmatic.core.*
import io.specmatic.core.log.logger
import io.specmatic.core.pattern.ContractException
import io.specmatic.core.pattern.Pattern
import io.specmatic.core.pattern.parsedJSONObject
import io.specmatic.core.utilities.exceptionCauseMessage
import io.specmatic.core.utilities.readEnvVarOrProperty
import io.specmatic.core.value.*
import io.specmatic.stub.stringToMockScenario
import java.io.File
Expand Down Expand Up @@ -43,25 +43,40 @@ data class ScenarioStub(
return JSONObjectValue(mockInteraction)
}

fun getRequestWithAdditionalParamsIfAny(): HttpRequest {
fun getRequestWithAdditionalParamsIfAny(request: HttpRequest, additionalExampleParamsFilePath: String?): HttpRequest {
if(additionalExampleParamsFilePath == null)
return this.request

val additionalExampleParamsFile = File(additionalExampleParamsFilePath)

if (!additionalExampleParamsFile.exists() || !additionalExampleParamsFile.isFile) {
return this.request
}

try {
val additionalExampleParamsFilePath = readEnvVarOrProperty(
"ADDITIONAL_EXAMPLE_PARAMS_FILE",
"ADDITIONAL_EXAMPLE_PARAMS_FILE"
)

if (additionalExampleParamsFilePath != null && File(additionalExampleParamsFilePath).exists()) {
val additionalExampleParams = ObjectMapper().readValue(
File(additionalExampleParamsFilePath).readText(),
Map::class.java
) as Map<String, Any>
val additionalHeaders = (additionalExampleParams["headers"] ?: emptyMap<String, String>()) as Map<String, String>
val updatedHeaders = request.headers.plus(additionalHeaders)
return request.copy(headers = updatedHeaders)
val additionalExampleParams = ObjectMapper().readValue(
File(additionalExampleParamsFilePath).readText(),
Map::class.java
) as? Map<String, Any>

if(additionalExampleParams == null) {
logger.log("WARNING: The content of $additionalExampleParamsFilePath is not a valid JSON object")
return this.request
}
return request

val additionalHeaders = (additionalExampleParams["headers"] ?: emptyMap<String, String>()) as? Map<String, String>

if(additionalHeaders == null) {
logger.log("WARNING: The content of \"headers\" in $additionalExampleParamsFilePath is not a valid JSON object")
return this.request
}

val updatedHeaders = this.request.headers.plus(additionalHeaders)

return this.request.copy(headers = updatedHeaders)
} catch (e: Exception) {
return request
logger.log(e, "WARNING: Could not read additional example params file $additionalExampleParamsFilePath")
return this.request
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.specmatic.conversions

import io.specmatic.core.NoBodyValue
import io.specmatic.core.SpecmaticConfig
import io.specmatic.core.pattern.parsedJSONObject
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -107,4 +108,32 @@ class ExampleFromFileTest {
assertThat(request.queryParams.asMap()).containsExactlyEntriesOf(mapOf("filter" to "active"))
}

@Test
fun `should load extra header params from additional params file`() {
val example = """
{
"http-request": {
"method": "POST",
"path": "/api/resource?filter=active",
"headers": {
"Content-Type": "application/json"
},
"body": {
"key": "value"
}
},
"http-response": {
"status": 200,
"body": "ok"
}
}
""".trimIndent().let {
ExampleFromFile(parsedJSONObject(it), File("./data.json"))
}

val row =
example.toRow(SpecmaticConfig(additionalExampleParamsFilePath = "src/test/resources/additionalParamsFile.json"))

assertThat(row.requestExample?.headers.orEmpty()).containsEntry("X-Tra", "info")
}
}
5 changes: 5 additions & 0 deletions core/src/test/resources/additionalParamsFile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"headers": {
"X-Tra": "info"
}
}

0 comments on commit 0397657

Please sign in to comment.