Skip to content

Commit

Permalink
fix: allow matchers to be used on the root node with XML builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Aug 10, 2020
1 parent 9cfe575 commit ff62a8d
Showing 1 changed file with 8 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,30 +90,16 @@ class PactXmlBuilder @JvmOverloads constructor (
}

class XmlNode(private val builder: PactXmlBuilder, private val element: Element, private val path: List<String>) {
fun setAttributes(attributes: Map<String, String>) {
attributes.forEach {
element.setAttribute(it.key, it.value)
}
fun setAttributes(attributes: Map<String, Any?>) {
setElementAttributes(attributes, element)
}

@JvmOverloads
fun eachLike(name: String, examples: Int = 1, attributes: Map<String, Any?> = emptyMap(), cl: Consumer<XmlNode>? = null) {
builder.matchingRules.addRule(matcherKey(path, name), TypeMatcher)
val element = builder.doc.createElement(name)
attributes.forEach {
if (it.value is Matcher) {
val matcherDef = it.value as Matcher
builder.matchingRules.addRule(matcherKey(path, name, "['@" + it.key + "']"), matcherDef.matcher!!)
if (matcherDef.generator != null) {
builder.generators.addGenerator(BODY, matcherKey(path, name, "['@" + it.key + "']"), matcherDef.generator!!)
}
element.setAttribute(it.key, matcherDef.value.toString())
} else {
element.setAttribute(it.key, it.value.toString())
}
}

val node = XmlNode(builder, element, this.path + element.tagName)
node.setAttributes(attributes)
cl?.accept(node)
this.element.appendChild(element)
(2..examples).forEach { _ ->
Expand All @@ -124,17 +110,17 @@ class XmlNode(private val builder: PactXmlBuilder, private val element: Element,
@JvmOverloads
fun appendElement(name: String, attributes: Map<String, Any?> = emptyMap(), cl: Consumer<XmlNode>? = null) {
val element = builder.doc.createElement(name)
setElementAttributes(attributes, element)

val node = XmlNode(builder, element, this.path + element.tagName)
node.setAttributes(attributes)
cl?.accept(node)
this.element.appendChild(element)
}

@JvmOverloads
fun appendElement(name: String, attributes: Map<String, Any?> = emptyMap(), contents: String) {
val element = builder.doc.createElement(name)
setElementAttributes(attributes, element)
val node = XmlNode(builder, element, this.path + element.tagName)
node.setAttributes(attributes)
element.textContent = contents
this.element.appendChild(element)
}
Expand All @@ -155,9 +141,9 @@ class XmlNode(private val builder: PactXmlBuilder, private val element: Element,
attributes.forEach {
if (it.value is Matcher) {
val matcherDef = it.value as Matcher
builder.matchingRules.addRule(matcherKey(path, "@" + it.key), matcherDef.matcher!!)
builder.matchingRules.addRule(matcherKey(path, "['@${it.key}']"), matcherDef.matcher!!)
if (matcherDef.generator != null) {
builder.generators.addGenerator(BODY, matcherKey(path, "@" + it.key), matcherDef.generator!!)
builder.generators.addGenerator(BODY, matcherKey(path, "['@${it.key}']"), matcherDef.generator!!)
}
element.setAttribute(it.key, matcherDef.value.toString())
} else {
Expand Down

0 comments on commit ff62a8d

Please sign in to comment.