Skip to content

Commit

Permalink
feat: Add sys prop to set default Pact spec version; deprecate PactSp…
Browse files Browse the repository at this point in the history
…ecVersion.UNSPECIFIED #1705
  • Loading branch information
rholshausen committed Jul 31, 2023
1 parent 52cb552 commit bd2f380
Show file tree
Hide file tree
Showing 23 changed files with 378 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ object ArrayContainsJsonGenerator : KLogging(), Generator {
}
}

override fun toMap(pactSpecVersion: PactSpecVersion) = emptyMap<String, Any>()
override fun toMap(pactSpecVersion: PactSpecVersion?) = emptyMap<String, Any>()

private fun findMatchingVariant(
example: JsonValue,
Expand Down
1 change: 1 addition & 0 deletions core/model/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies {
testImplementation 'com.amazonaws:aws-java-sdk-s3:1.12.232'
testImplementation 'io.kotlintest:kotlintest-runner-junit5:3.4.2'
testImplementation 'junit:junit'
testImplementation 'org.hamcrest:hamcrest:2.2'
}

task pactsJar(type: Jar, dependsOn: testClasses) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ abstract class HttpPart: IHttpPart {
}
}

fun validateForVersion(pactVersion: PactSpecVersion): List<String> {
fun validateForVersion(pactVersion: PactSpecVersion?): List<String> {
val errors = mutableListOf<String>()
errors.addAll(matchingRules.validateForVersion(pactVersion))
errors.addAll(generators.validateForVersion(pactVersion))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ interface Interaction {
/**
* Converts this interaction to a Map
*/
fun toMap(pactSpecVersion: PactSpecVersion): Map<String, *>
fun toMap(pactSpecVersion: PactSpecVersion?): Map<String, *>

/**
* Generates a unique key for this interaction
Expand All @@ -78,7 +78,7 @@ interface Interaction {
val comments: MutableMap<String, JsonValue>

/** Validates if this Interaction can be used with the provided Pact specification version */
fun validateForVersion(pactVersion: PactSpecVersion): List<String>
fun validateForVersion(pactVersion: PactSpecVersion?): List<String>

/** Converts this interaction to a V4 format */
fun asV4Interaction(): V4Interaction
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package au.com.dius.pact.core.model

import au.com.dius.pact.core.support.Utils.lookupEnvironmentValue
import io.github.oshai.kotlinlogging.KLogging

/**
* Pact Specification Version
*/
@Suppress("EnumNaming")
enum class PactSpecVersion {
UNSPECIFIED, V1, V1_1, V2, V3, V4;
@Deprecated("Use a null value instead of this value") UNSPECIFIED,
V1,
V1_1,
V2,
V3,
V4;

fun versionString(): String {
return when (this) {
Expand All @@ -14,19 +22,19 @@ enum class PactSpecVersion {
V2 -> "2.0.0"
V3 -> "3.0.0"
V4 -> "4.0"
else -> "3.0.0"
else -> defaultVersion().versionString()
}
}

fun or(other: PactSpecVersion?): PactSpecVersion {
return if (this == UNSPECIFIED) {
other ?: UNSPECIFIED
other?.or(defaultVersion()) ?: defaultVersion()
} else {
this
}
}

companion object {
companion object: KLogging() {
@JvmStatic
fun fromInt(version: Int): PactSpecVersion {
return when (version) {
Expand All @@ -36,5 +44,31 @@ enum class PactSpecVersion {
else -> V3
}
}

@JvmStatic
fun defaultVersion(): PactSpecVersion {
val defaultVer = lookupEnvironmentValue("pact.defaultVersion")
return if (defaultVer.isNullOrEmpty()) {
V3
} else {
valueOf(defaultVer)
}
}
}
}

fun PactSpecVersion?.atLeast(version: PactSpecVersion): Boolean {
return if (this == null || this == PactSpecVersion.UNSPECIFIED) {
PactSpecVersion.defaultVersion().atLeast(version)
} else {
this >= version
}
}

fun PactSpecVersion?.lessThan(version: PactSpecVersion): Boolean {
return if (this == null || this == PactSpecVersion.UNSPECIFIED) {
PactSpecVersion.defaultVersion().lessThan(version)
} else {
this < version
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@ open class RequestResponseInteraction @JvmOverloads constructor(

override fun uniqueKey() = "${displayState()}_$description"

override fun toMap(pactSpecVersion: PactSpecVersion): Map<String, Any> {
override fun toMap(pactSpecVersion: PactSpecVersion?): Map<String, Any> {
val interactionJson = mutableMapOf(
"description" to description,
"request" to requestToMap(request, pactSpecVersion),
"response" to responseToMap(response, pactSpecVersion)
)
if (pactSpecVersion < PactSpecVersion.V3 && providerStates.isNotEmpty()) {
interactionJson["providerState"] = providerStates.first().name.toString()
} else if (providerStates.isNotEmpty()) {
interactionJson["providerStates"] = providerStates.map { it.toMap() }

if (providerStates.isNotEmpty()) {
if (pactSpecVersion.lessThan(PactSpecVersion.V3)) {
interactionJson["providerState"] = providerStates.first().name.toString()
} else if (providerStates.isNotEmpty()) {
interactionJson["providerStates"] = providerStates.map { it.toMap() }
}
}

return interactionJson
}

Expand All @@ -59,7 +63,7 @@ open class RequestResponseInteraction @JvmOverloads constructor(
return result
}

override fun validateForVersion(pactVersion: PactSpecVersion): List<String> {
override fun validateForVersion(pactVersion: PactSpecVersion?): List<String> {
val errors = mutableListOf<String>()
errors.addAll(request.validateForVersion(pactVersion))
errors.addAll(response.validateForVersion(pactVersion))
Expand All @@ -83,7 +87,7 @@ open class RequestResponseInteraction @JvmOverloads constructor(
const val COMMA = ", "

@JvmStatic
fun requestToMap(request: Request, pactSpecVersion: PactSpecVersion): Map<String, Any?> {
fun requestToMap(request: Request, pactSpecVersion: PactSpecVersion?): Map<String, Any?> {
val map = mutableMapOf<String, Any?>(
"method" to request.method.toUpperCase(),
"path" to request.path
Expand All @@ -92,7 +96,7 @@ open class RequestResponseInteraction @JvmOverloads constructor(
map["headers"] = request.headers.entries.associate { (key, value) -> key to value.joinToString(COMMA) }
}
if (request.query.isNotEmpty()) {
map["query"] = if (pactSpecVersion >= PactSpecVersion.V3) request.query else mapToQueryStr(request.query)
map["query"] = if (pactSpecVersion.atLeast(PactSpecVersion.V3)) request.query else mapToQueryStr(request.query)
}

if (request.body.isPresent()) {
Expand All @@ -104,15 +108,15 @@ open class RequestResponseInteraction @JvmOverloads constructor(
if (request.matchingRules.isNotEmpty()) {
map["matchingRules"] = request.matchingRules.toMap(pactSpecVersion)
}
if (request.generators.isNotEmpty() && pactSpecVersion >= PactSpecVersion.V3) {
if (request.generators.isNotEmpty() && pactSpecVersion.atLeast(PactSpecVersion.V3)) {
map["generators"] = request.generators.toMap(pactSpecVersion)
}

return map
}

@JvmStatic
fun responseToMap(response: Response, pactSpecVersion: PactSpecVersion): Map<String, Any?> {
fun responseToMap(response: Response, pactSpecVersion: PactSpecVersion?): Map<String, Any?> {
val map = mutableMapOf<String, Any?>("status" to response.status)
if (response.headers.isNotEmpty()) {
map["headers"] = response.headers.entries.associate { (key, value) -> key to value.joinToString(COMMA) }
Expand All @@ -127,7 +131,7 @@ open class RequestResponseInteraction @JvmOverloads constructor(
if (response.matchingRules.isNotEmpty()) {
map["matchingRules"] = response.matchingRules.toMap(pactSpecVersion)
}
if (response.generators.isNotEmpty() && pactSpecVersion >= PactSpecVersion.V3) {
if (response.generators.isNotEmpty() && pactSpecVersion.atLeast(PactSpecVersion.V3)) {
map["generators"] = response.generators.toMap(pactSpecVersion)
}
return map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ data class HttpRequest @JvmOverloads constructor(
override val matchingRules: MatchingRules = MatchingRulesImpl(),
override val generators: Generators = Generators()
): IRequest, IHttpPart, KLogging() {
fun validateForVersion(pactVersion: PactSpecVersion): List<String> {
fun validateForVersion(pactVersion: PactSpecVersion?): List<String> {
val errors = mutableListOf<String>()
errors.addAll(matchingRules.validateForVersion(pactVersion))
errors.addAll(generators.validateForVersion(pactVersion))
Expand Down Expand Up @@ -167,7 +167,7 @@ data class HttpResponse @JvmOverloads constructor(
override val matchingRules: MatchingRules = MatchingRulesImpl(),
override val generators: Generators = Generators()
) : IResponse, IHttpPart {
fun validateForVersion(pactVersion: PactSpecVersion): List<String> {
fun validateForVersion(pactVersion: PactSpecVersion?): List<String> {
val errors = mutableListOf<String>()
errors.addAll(matchingRules.validateForVersion(pactVersion))
errors.addAll(generators.validateForVersion(pactVersion))
Expand Down
14 changes: 7 additions & 7 deletions core/model/src/main/kotlin/au/com/dius/pact/core/model/V4Pact.kt
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ sealed class V4Interaction(
override fun isInteractionType(interactionType: V4InteractionType) =
interactionType == V4InteractionType.SynchronousHTTP

override fun toMap(pactSpecVersion: PactSpecVersion): Map<String, *> {
override fun toMap(pactSpecVersion: PactSpecVersion?): Map<String, *> {
val map = mutableMapOf(
"type" to V4InteractionType.SynchronousHTTP.toString(),
"key" to uniqueKey(),
Expand Down Expand Up @@ -274,7 +274,7 @@ sealed class V4Interaction(
return map
}

override fun validateForVersion(pactVersion: PactSpecVersion): List<String> {
override fun validateForVersion(pactVersion: PactSpecVersion?): List<String> {
val errors = mutableListOf<String>()
errors.addAll(request.validateForVersion(pactVersion))
errors.addAll(response.validateForVersion(pactVersion))
Expand Down Expand Up @@ -363,7 +363,7 @@ sealed class V4Interaction(

override fun updateProperties(values: Map<String, Any?>) { }

override fun toMap(pactSpecVersion: PactSpecVersion): Map<String, *> {
override fun toMap(pactSpecVersion: PactSpecVersion?): Map<String, *> {
val map = (mapOf(
"type" to V4InteractionType.AsynchronousMessages.toString(),
"key" to key,
Expand Down Expand Up @@ -394,7 +394,7 @@ sealed class V4Interaction(
return map
}

override fun validateForVersion(pactVersion: PactSpecVersion): List<String> {
override fun validateForVersion(pactVersion: PactSpecVersion?): List<String> {
val errors = mutableListOf<String>()
errors.addAll(contents.matchingRules.validateForVersion(pactVersion))
errors.addAll(contents.generators.validateForVersion(pactVersion))
Expand Down Expand Up @@ -473,8 +473,8 @@ sealed class V4Interaction(
return builder.build().toUInt().toString(16)
}

override fun toMap(pactSpecVersion: PactSpecVersion): Map<String, *> {
require(pactSpecVersion >= PactSpecVersion.V4) {
override fun toMap(pactSpecVersion: PactSpecVersion?): Map<String, *> {
require(pactSpecVersion.atLeast(PactSpecVersion.V4)) {
"A Synchronous Messages interaction can not be written to a $pactSpecVersion pact file"
}
val map = mutableMapOf(
Expand Down Expand Up @@ -509,7 +509,7 @@ sealed class V4Interaction(
return map
}

override fun validateForVersion(pactVersion: PactSpecVersion): List<String> {
override fun validateForVersion(pactVersion: PactSpecVersion?): List<String> {
val errors = mutableListOf<String>()
errors.addAll(request.matchingRules.validateForVersion(pactVersion))
errors.addAll(request.generators.validateForVersion(pactVersion))
Expand Down
Loading

0 comments on commit bd2f380

Please sign in to comment.