Groovy:比较SOAP响应和XML文件 [英] Groovy : Compare SOAP Response with XML file

查看:164
本文介绍了Groovy:比较SOAP响应和XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的代码:

  import org.custommonkey.xmlunit.Stuff 
import org.xmlunit.Stuff

// ExpectedString是我的xml转换为文本,相同对于ResponseString

Diff diff = DiffBuilder.compare(ExpectedString)
.withTest(ResponseString)
.ignoreComments()
.ignoreWhitespace()
.checkForSimilar ()
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName))
.build();

assertFalse(XML similar+ diff.toString(),diff.hasDifferences())

因此,正如你所看到的,我使用了DefaultNodeMatcher,我使用了XMLUnit2.0 ...没有结果(甚至不会忽略顺序或比较时发生异常错误)

是否有解决方案?解决这个问题



由于我不顾一切地找到一个直接的,我可以排序我的XML和我的肥皂响应,所以我可以有一个简单的差异?有没有一种方法可以按字母顺序逐行排序?如果是的话,怎么样?



谢谢大家!

更新:

以下是我简化的XML结构

 < body> 
< stuff>
< miniStuff>< / miniStuff>
< miniStuff>< / miniStuff>
< / stuff>
<服务>
<服务>
< tag1> ValueA< / tag1>
< tag2> ValueAA< / tag2>
< / Service>
<服务>
< tag1> ValueB< / tag1>
< tag2> ValueBB< / tag2>
< / Service>
< / services>
< / body>

我的问题是我无法保证ValueA是第一个而不是第二个

解决方案

以下是您可能要查找的内容:use ByNameAndTextRecSelector

  withNodeMatcher(new DefaultNodeMatcher(new ByNameAndTextRecSelector(),ElementSelectors.byName))

单元测试:

  @Test 
public void testDiffOrder(){
final String control =
< r>
< ser>
< t1> a< / t1>
< t2> b< / t2>
< / ser>
< ser>
< t1> d< / t1>
< t2> ; e< / t2>
< / ser>
< / r>
final String test =
< r>
< ser>
< t1> d< / t1>
< t2> e< / t2>
< / ser>
< ser>
< t1> a< / t1>
< t2> b< / t2>
< / ser>
< / r>
Diff diff = DiffBuilder.compare(Input.fromString(control))
.withTest(Input.fromString(test))
.ignoreComments ()
.ignoreWhitespace()
.checkForSimilar()
.withNodeMatcher(new DefaultNodeMatcher(new ByNameAndTextRecSelector(),ElementSelectors.byName))
.build()

assertFalse(XML differ+ diff.toString(),diff.hasDifferences())
}

点数到@bodewig。
来自 here



更新:更多groovified版本与OP原始XML片段。

  import org.xmlunit.builder.DiffBuilder 
import org.xmlunit.builder.Input
import org.xmlunit.diff.ByNameAndTextRecSelector
import org.xmlunit.diff.DefaultNodeMatcher
import org.xmlunit.diff.ElementSelectors

def control = <身体GT;
< stuff>
< miniStuff />
< miniStuff />
< / stuff>
<服务>
<服务>
< tag1> ValueB< / tag1>
< tag2> ValueBB< / tag2>
< / Service>
<服务>
< tag1> ValueA< / tag1>
< tag2> ValueAA< / tag2>
< / Service>
< /服务>
< / body>
def test =< body>
< stuff>
< miniStuff />
< miniStuff />
< / stuff>
<服务>
<服务>
< tag1> ValueA< / tag1>
< tag2> ValueAA< / tag2>
< / Service>
<服务>
< tag1> ValueB< / tag1>
< tag2> ValueBB< / tag2>
< / Service>
< /服务>
< / body>
def myDiff = DiffBuilder.compare(Input.fromString(control))
.withTest(Input.fromString(test))
.checkForSimilar ()
.withNodeMatcher(new DefaultNodeMatcher(new ByNameAndTextRecSelector(),ElementSelectors.byName))
.build()
println myDiff.toString()
println myDiff.hasDifferences()
assert!myDiff.hasDifferences()


I want to compare in groovy code my Soap Response with xml file ignoring order :

Here is my code :

import org.custommonkey.xmlunit.Stuff
import org.xmlunit.Stuff

//ExpectedString is my xml converted to text, same for ResponseString

Diff diff = DiffBuilder.compare(ExpectedString)
           .withTest(ResponseString)
           .ignoreComments()
           .ignoreWhitespace()
           .checkForSimilar()
           .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName))
           .build();

assertFalse("XML similar " + diff.toString(), diff.hasDifferences())

So, as you can see, I used DefaultNodeMatcher, I used XMLUnit2.0 ... with no result (even not ignoring order or having exception error while comparing)

Is there a solution ? to resolve this

As I'm desperate to find a direct one, can I sort my xml and my soap response so i can have a simple diff ? is there a way to sort it line by line alphabetically ? If yes, how ?

Thank you guys !

Update :

Here is my XML structure simplified

<body>
<stuff>
  <miniStuff></miniStuff>
  <miniStuff></miniStuff>
</stuff>
<Services>
  <Service>
    <tag1>ValueA</tag1>
    <tag2>ValueAA</tag2>
  </Service>
  <Service>
    <tag1>ValueB</tag1>
    <tag2>ValueBB</tag2>
  </Service>
</services>
</body>

My problem is I can't guarantee that ValueA is the first one and not the second

解决方案

Here is the one you might looking for: use ByNameAndTextRecSelector

withNodeMatcher(new DefaultNodeMatcher(new ByNameAndTextRecSelector(),ElementSelectors.byName))

Unit Test:

    @Test
    public void testDiffOrder() {
        final String control = """
            <r>
                <ser>
                    <t1>a</t1>
                    <t2>b</t2>
                </ser>
                <ser>
                    <t1>d</t1>
                    <t2>e</t2>
                </ser>
            </r>"""
        final String test = """
            <r>
                <ser>
                    <t1>d</t1>
                    <t2>e</t2>
                </ser>
                <ser>
                    <t1>a</t1>
                    <t2>b</t2>
                </ser>
            </r>"""
        Diff diff = DiffBuilder.compare(Input.fromString(control))
                .withTest(Input.fromString(test))
                .ignoreComments()
                .ignoreWhitespace()
                .checkForSimilar()
                .withNodeMatcher(new DefaultNodeMatcher(new ByNameAndTextRecSelector(),ElementSelectors.byName))
                .build()

        assertFalse("XML differ " + diff.toString(), diff.hasDifferences())
    }

Credits to @bodewig. Sample from here

UPDATE: more groovified versionwith OPs original xml snippet.

import org.xmlunit.builder.DiffBuilder
import org.xmlunit.builder.Input
import org.xmlunit.diff.ByNameAndTextRecSelector
import org.xmlunit.diff.DefaultNodeMatcher
import org.xmlunit.diff.ElementSelectors

def control = """<body>
   <stuff>
      <miniStuff />
      <miniStuff />
   </stuff>
   <Services>
      <Service>
         <tag1>ValueB</tag1>
         <tag2>ValueBB</tag2>
      </Service>
      <Service>
         <tag1>ValueA</tag1>
         <tag2>ValueAA</tag2>
      </Service>
   </Services>
</body>"""
def test = """<body>
   <stuff>
      <miniStuff />
      <miniStuff />
   </stuff>
   <Services>
      <Service>
         <tag1>ValueA</tag1>
         <tag2>ValueAA</tag2>
      </Service>
      <Service>
         <tag1>ValueB</tag1>
         <tag2>ValueBB</tag2>
      </Service>
   </Services>
</body>"""
def myDiff = DiffBuilder.compare(Input.fromString(control))
            .withTest(Input.fromString(test))
            .checkForSimilar()
            .withNodeMatcher(new DefaultNodeMatcher(new ByNameAndTextRecSelector(),ElementSelectors.byName))
            .build()
println myDiff.toString()
println myDiff.hasDifferences()
assert !myDiff.hasDifferences()

这篇关于Groovy:比较SOAP响应和XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆