Skip to content

Commit

Permalink
feat: add capability to the selector builder DSL to add raw JSON snip…
Browse files Browse the repository at this point in the history
…pets
  • Loading branch information
uglyog committed Aug 31, 2022
1 parent fa7d270 commit 416b19d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ sealed class ConsumerVersionSelectors {
val fallbackTag: String? = null
): ConsumerVersionSelectors()

/**
* Raw JSON form of a selector.
*/
data class RawSelector(val selector: JsonValue): ConsumerVersionSelectors()

fun toJson(): JsonValue {
return when (this) {
is Branch -> {
Expand Down Expand Up @@ -278,6 +283,7 @@ sealed class ConsumerVersionSelectors {
JsonValue.Object(entries)
}
is Tag -> JsonValue.Object("tag" to JsonValue.StringValue(this.tag))
is RawSelector -> this.selector
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package au.com.dius.pact.provider.junitsupport.loader

import au.com.dius.pact.core.pactbroker.ConsumerVersionSelectors
import au.com.dius.pact.core.support.json.JsonParser

/**
* Builder for setting up consumer version selectors in provider JUnit tests.
Expand Down Expand Up @@ -106,6 +107,14 @@ open class SelectorBuilder {
return this
}

/**
* Selector in raw JSON form.
*/
fun rawSelectorJson(json: String): SelectorBuilder {
selectors.add(ConsumerVersionSelectors.RawSelector(JsonParser.parseString(json)))
return this
}

/**
* Construct the final list of consumer version selectors
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package au.com.dius.pact.provider.junitsupport.loader

import spock.lang.Specification

import static au.com.dius.pact.core.support.JsonKt.jsonArray

class SelectorBuilderSpec extends Specification {

def 'allow providing selectors in raw form'() {
expect:
jsonArray(new SelectorBuilder()
.rawSelectorJson('{"iAmA": "selector"}')
.build()
.collect { it.toJson() }).serialise() == '[{"iAmA":"selector"}]'
}

}

0 comments on commit 416b19d

Please sign in to comment.