From 4db1f837f302675a998516430ef75654952e69e5 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Sat, 21 Aug 2021 14:51:13 +1000 Subject: [PATCH] feat: add standalone setting to the XML builder #1414 --- .../dius/pact/consumer/xml/PactXmlBuilder.kt | 60 +++++++++++++++++-- .../consumer/xml/PactXmlBuilderSpec.groovy | 22 +++++++ 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/consumer/src/main/kotlin/au/com/dius/pact/consumer/xml/PactXmlBuilder.kt b/consumer/src/main/kotlin/au/com/dius/pact/consumer/xml/PactXmlBuilder.kt index 75913511ac..261b7b42e6 100644 --- a/consumer/src/main/kotlin/au/com/dius/pact/consumer/xml/PactXmlBuilder.kt +++ b/consumer/src/main/kotlin/au/com/dius/pact/consumer/xml/PactXmlBuilder.kt @@ -29,11 +29,12 @@ private fun matcherKey(path: List, vararg key: String): String { } class PactXmlBuilder @JvmOverloads constructor ( - val rootName: String, - val rootNameSpace: String? = null, - val namespaces: Map = emptyMap(), - val version: String? = null, - val charset: String? = null + var rootName: String, + var rootNameSpace: String? = null, + var namespaces: Map = emptyMap(), + var version: String? = null, + var charset: String? = null, + var standalone: Boolean = false ) { val generators: Generators = Generators() val matchingRules: Category = Category("body") @@ -53,6 +54,7 @@ class PactXmlBuilder @JvmOverloads constructor ( if (version != null) { doc.xmlVersion = version } + doc.xmlStandalone = standalone val root = if (doc.documentElement == null) { val element = doc.createElement(rootName) doc.appendChild(element) @@ -87,6 +89,54 @@ class PactXmlBuilder @JvmOverloads constructor ( } override fun toString() = String(asBytes()) + + /** + * Sets the name of the root name + */ + fun withRootName(name: String): PactXmlBuilder { + this.rootName = name + return this + } + + /** + * Sets the namespace of the root node + */ + fun withRootNameSpace(nameSpace: String): PactXmlBuilder { + this.rootNameSpace = nameSpace + return this + } + + /** + * Namespaces to define on the root name + */ + fun withNamespaces(namespaces: Map): PactXmlBuilder { + this.namespaces = namespaces + return this + } + + /** + * Sets the version on the XML descriptor. Defaults to '1.0'. + */ + fun withVersion(version: String): PactXmlBuilder { + this.version = version + return this + } + + /** + * Sets the charset on the XML descriptor. Defaults to 'UTF-8' + */ + fun withCharset(charset: String): PactXmlBuilder { + this.charset = charset + return this + } + + /** + * Sets the standalone flag on the XML descriptor. Default is set ('yes') + */ + fun withStandalone(standalone: Boolean): PactXmlBuilder { + this.standalone = standalone + return this + } } class XmlNode(private val builder: PactXmlBuilder, private val element: Element, private val path: List) { diff --git a/consumer/src/test/groovy/au/com/dius/pact/consumer/xml/PactXmlBuilderSpec.groovy b/consumer/src/test/groovy/au/com/dius/pact/consumer/xml/PactXmlBuilderSpec.groovy index 5340bb87ea..70451f8b2a 100644 --- a/consumer/src/test/groovy/au/com/dius/pact/consumer/xml/PactXmlBuilderSpec.groovy +++ b/consumer/src/test/groovy/au/com/dius/pact/consumer/xml/PactXmlBuilderSpec.groovy @@ -115,4 +115,26 @@ class PactXmlBuilderSpec extends Specification { ['$', 'one'] | ['two', "['@id']"] | "\$.one.two['@id']" ['$', 'one'] | ['two', '#text'] | "\$.one.two.#text" } + + @Unroll + def 'standalone declaration - #standalone'() { + given: + def builder = new PactXmlBuilder('projects') + .withStandalone(standalone) + .build { node -> + node.setAttributes([id: '1234']) + } + + when: + def result = builder.toString() + + then: + result.startsWith(value) + + where: + + standalone | value + true | '' + false | '' + } }