From 8c965dca6e00820996e2bef8a197ae0906e2e465 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Thu, 2 Feb 2023 16:17:10 +1100 Subject: [PATCH] fix(regression): Changes for #1641 broke the use of plugin mock servers --- .../consumer/junit5/PactConsumerTestExt.kt | 6 +++- .../consumer/model/MockHttpsProviderConfig.kt | 31 +++++++++++++++++++ .../pact/consumer/model/MockProviderConfig.kt | 18 +++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/consumer/junit5/src/main/kotlin/au/com/dius/pact/consumer/junit5/PactConsumerTestExt.kt b/consumer/junit5/src/main/kotlin/au/com/dius/pact/consumer/junit5/PactConsumerTestExt.kt index 015f922415..16fcde6e4a 100644 --- a/consumer/junit5/src/main/kotlin/au/com/dius/pact/consumer/junit5/PactConsumerTestExt.kt +++ b/consumer/junit5/src/main/kotlin/au/com/dius/pact/consumer/junit5/PactConsumerTestExt.kt @@ -228,7 +228,7 @@ class PactConsumerTestExt : Extension, BeforeTestExecutionCallback, BeforeAllCal return when { store[key] != null -> store[key] as AbstractBaseMockServer else -> { - val config = providerInfo.mockServerConfig() + val config = mockServerConfigFromAnnotation(context).merge(providerInfo.mockServerConfig()) store.put("mockServerConfig:${providerInfo.providerName}", config) val mockServer = mockServer(lookupPact(providerInfo, pactMethod, context), config) store.put(key, JUnit5MockServerSupport(mockServer)) @@ -605,3 +605,7 @@ class PactConsumerTestExt : Extension, BeforeTestExecutionCallback, BeforeAllCal val NAMESPACE: ExtensionContext.Namespace = ExtensionContext.Namespace.create("pact-jvm") } } + +fun MockProviderConfig?.merge(config: MockProviderConfig): MockProviderConfig { + return this?.mergeWith(config) ?: config +} diff --git a/consumer/src/main/kotlin/au/com/dius/pact/consumer/model/MockHttpsProviderConfig.kt b/consumer/src/main/kotlin/au/com/dius/pact/consumer/model/MockHttpsProviderConfig.kt index a51521f332..451c5c48ed 100644 --- a/consumer/src/main/kotlin/au/com/dius/pact/consumer/model/MockHttpsProviderConfig.kt +++ b/consumer/src/main/kotlin/au/com/dius/pact/consumer/model/MockHttpsProviderConfig.kt @@ -19,6 +19,37 @@ class MockHttpsProviderConfig @JvmOverloads constructor( override val mockServerImplementation: MockServerImplementation = MockServerImplementation.KTorServer ) : MockProviderConfig(hostname, port, pactVersion, "https", mockServerImplementation) { + @Suppress("ComplexMethod") + override fun mergeWith(config: MockProviderConfig): MockProviderConfig { + return if (config is MockHttpsProviderConfig) { + MockHttpsProviderConfig( + if (hostname.isEmpty() || hostname == LOCALHOST) config.hostname else hostname, + if (port == 0) config.port else port, + if (pactVersion == PactSpecVersion.UNSPECIFIED) config.pactVersion else pactVersion, + keyStore ?: config.keyStore, + if (keyStoreAlias.isEmpty() || keyStoreAlias == "alias") config.keyStoreAlias else keyStoreAlias, + if (keystorePassword.isEmpty() || keystorePassword == "changeme") + config.keystorePassword + else keystorePassword, + if (privateKeyPassword.isEmpty() || privateKeyPassword == "changeme") + config.privateKeyPassword + else privateKeyPassword, + mockServerImplementation + ) + } else { + MockHttpsProviderConfig( + if (hostname.isEmpty() || hostname == LOCALHOST) config.hostname else hostname, + if (port == 0) config.port else port, + if (pactVersion == PactSpecVersion.UNSPECIFIED) config.pactVersion else pactVersion, + keyStore, + keyStoreAlias, + keystorePassword, + privateKeyPassword, + mockServerImplementation + ) + } + } + companion object { @JvmStatic @JvmOverloads diff --git a/consumer/src/main/kotlin/au/com/dius/pact/consumer/model/MockProviderConfig.kt b/consumer/src/main/kotlin/au/com/dius/pact/consumer/model/MockProviderConfig.kt index f5bf26dcd6..12b7f1ea8d 100644 --- a/consumer/src/main/kotlin/au/com/dius/pact/consumer/model/MockProviderConfig.kt +++ b/consumer/src/main/kotlin/au/com/dius/pact/consumer/model/MockProviderConfig.kt @@ -75,6 +75,24 @@ open class MockProviderConfig @JvmOverloads constructor ( ) } + open fun mergeWith(config: MockProviderConfig): MockProviderConfig { + return if (config is MockHttpsProviderConfig) { + config.mergeWith(this) + } else { + MockProviderConfig( + if (hostname.isEmpty() || hostname == LOCALHOST) config.hostname else hostname, + if (port == 0) config.port else port, + if (pactVersion == PactSpecVersion.UNSPECIFIED) config.pactVersion else pactVersion, + if (scheme.isEmpty() || scheme == HTTP && config.scheme != HTTP) config.scheme else scheme, + if (mockServerImplementation == MockServerImplementation.Default) + config.mockServerImplementation + else mockServerImplementation, + addCloseHeader, + transportRegistryEntry.ifEmpty { config.transportRegistryEntry } + ) + } + } + companion object { const val LOCALHOST = "127.0.0.1" const val HTTP = "http"