使用SoapUI中的groovy数组动态比较Rest XML / JSON响应和JDBC [英] Dynamically compare Rest XML/JSON response and JDBC using groovy array in SoapUI

查看:101
本文介绍了使用SoapUI中的groovy数组动态比较Rest XML / JSON响应和JDBC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SoapUI中,我有一个JDBC测试步骤,它返回以下数据:

 < Results> 
< ResultSet fetchSize =128>
< Row rowNumber =1>
< ID> 1< / ID>
< NAME> TestName1< / NAME>
< DESCRIPTION />
< TYPE> Bool< / TYPE>
< ISPRODUCTTAG>真< / ISPRODUCTTAG>
< ISLOCATIONTAG> false< / ISLOCATIONTAG>
< SUBSECTION>默认子科目< / SUBSECTION>
< SECTION>默认部分< / SECTION>
< SUBGROUP>默认子组< / SUBGROUP>
< GROUP>默认群组< / GROUP>
< /行>
< Row rowNumber =2>
< ID> 2< / ID>
< NAME> TestName2< / NAME>
< DESCRIPTION />
< TYPE> Bool< / TYPE>
< ISPRODUCTTAG>真< / ISPRODUCTTAG>
< ISLOCATIONTAG> false< / ISLOCATIONTAG>
< SUBSECTION>默认子科目< / SUBSECTION>
< SECTION>默认部分< / SECTION>
< SUBGROUP>默认子组< / SUBGROUP>
< GROUP>默认群组< / GROUP>
< /行>
< /行>
< / ResultSet>



我有一个REST API XML包含以下数据的响应:

 < ArrayOfTagInfo> 
< TagInfo id =1name =TestName1type =BoolisProductTag =trueisLocationTag =falsesubsection =Default Sub Sectionsection =Default Sectionsubgroup =Default Sub组组=默认组/>
< TagInfo id =2name =TestName2type =BoolisProductTag =trueisLocationTag =falsesubsection =Default Sub Sectionsection =Default Sectionsubgroup =Default Sub组组=默认组/>
< / ArrayOfTagInfo>

我希望能够比较(断言)数据库值和响应值(响应如果可能的话,可以使用groovy数组,因为从数据库返回的数据可能非常大。



任何人都可以帮忙吗? / p>

解决方案

您可以使用以下测试步骤设计测试用例:


  1. jdbc步骤

  2. json步骤

  3. 常规脚本步骤

groovy脚本步骤中的想法/ sudo代码是:


  • 得到jdbc步骤的响应

  • 获得json步骤的响应

  • 以对象的形式构建数据,因此比较容易

  • 将对象存储在列表中,一个用于jdbc,另一个用于json

  • 对两个列表进行排序,以确保比较相同的数据
  • 比较这两个列表


这里是groovy脚本:

  / ** 
*用于比较
* /
的模型对象@ groovy.transform.Canonical
class模型{
def id
def name
def type
def isProductTag
def isLocationTag
def subSection
def section
def subGroup
def group
$ b $ / **
*这将接受jdbc行
* @param行
* @return
* /
def buildJdbcData (row){
row.with {
id = ID
name = NAME
type = TYPE
isProductTag = ISPRODUCTTAG
isLocationTag = ISLOCATIONTAG
subSection = SUBSECTION
section = SECTION
subGroup = SUBGROUP
group = GROUP
}
}

/ **
*这将接受j子TagInfo
* @param tagInfo
* @return
* /
def buildJsonData(tagInfo){
id = tagInfo。@ id
name = tagInfo 。@ name
type = tagInfo。@ type
isProductTag = tagInfo。@ isProductTag
isLocationTag = tagInfo。@ isLocationTag
subSection = tagInfo。@ subsection
section = tagInfo 。@ section
subGroup = tagInfo。@ subgroup
group = tagInfo。@ group
}
}

/ **
*创建从接收到的响应中使用jdbcResponse,使用固定值进行测试
*如果您愿意,可以直接使用下面的代替当前值分配接收的响应,并确保正确替换步骤名称
* def jdbcResponse = context.expand('$ {JdbcStepName#Response}')
* /
def jdbcResponse ='''< Results>
< ResultSet fetchSize =128>
< Row rowNumber =1>
< ID> 1< / ID>
< NAME> TestName1< / NAME>
< DESCRIPTION />
< TYPE> Bool< / TYPE>
< ISPRODUCTTAG>真< / ISPRODUCTTAG>
< ISLOCATIONTAG> false< / ISLOCATIONTAG>
< SUBSECTION>默认子科目< / SUBSECTION>
< SECTION>默认部分< / SECTION>
< SUBGROUP>默认子组< / SUBGROUP>
< GROUP>默认群组< / GROUP>
< /行>
< Row rowNumber =2>
< ID> 2< / ID>
< NAME> TestName2< / NAME>
< DESCRIPTION />
< TYPE> Bool< / TYPE>
< ISPRODUCTTAG>真< / ISPRODUCTTAG>
< ISLOCATIONTAG> false< / ISLOCATIONTAG>
< SUBSECTION>默认子科目< / SUBSECTION>
< SECTION>默认部分< / SECTION>
< SUBGROUP>默认子组< / SUBGROUP>
< GROUP>默认群组< / GROUP>
< /行>
<! - 添加第三行用于测试失败 - >
< Row rowNumber =3>
< ID> 3< ID>
< NAME> TestName3< / NAME>
< DESCRIPTION />
< TYPE> Bool< / TYPE>
< ISPRODUCTTAG> false< / ISPRODUCTTAG>
< ISLOCATIONTAG> false< / ISLOCATIONTAG>
< SUBSECTION>默认子科科3< / SUBSECTION>
< SECTION>默认章节3< / SECTION>
< SUBGROUP>默认子组3< / SUBGROUP>
< GROUP>默认群组3< / GROUP>
< /行>
< / ResultSet>
< / Results>'''

/ **
*从收到的响应中创建jsonResponse,使用固定值进行测试
*如果需要,您可以直接使用下面的代码指定接收到的响应,而不是当前的,并确保正确替换步骤名称
* def jsonResponse = context.expand('$ {JsonStepName#Response}')

* /
def restResponse ='''
< ArrayOfTagInfo>
< TagInfo id =1name =TestName1type =BoolisProductTag =trueisLocationTag =falsesubsection =Default Sub Sectionsection =Default Sectionsubgroup =Default Sub组组=默认组/>
< TagInfo id =2name =TestName2type =BoolisProductTag =trueisLocationTag =falsesubsection =Default Sub Sectionsection =Default Sectionsubgroup =Default Sub组组=默认组/>
<! - 添加第三行用于测试失败 - >
< / ArrayOfTagInfo>'''

//解析jdbc并构建jdbc模型对象列表
def results = new XmlSlurper()。parseText(jdbcResponse)
def jdbcDataObjects = []
results.ResultSet.Row.each {row - >
jdbcDataObjects.add(new Model()。buildJdbcData(row))
}

//解析json并生成json模型对象列表
def arrayOfTagInfo =新的XmlSlurper()。parseText(restResponse)
def jsonDataObjects = []
arrayOfTagInfo.TagInfo.each {tagInfo - >
jsonDataObjects.add(new Model()。buildJsonData(tagInfo))
}

//在检查相等之前对数据进行排序
jdbcDataObjects.sort()
jsonDataObjects.sort()

if(jdbcDataObjects.size()!= jsonDataObjects.size()){
System.err.println(Jdbc resultset size is:$ { jdbcDataObjects.size()}和Json结果大小为:$ {jsonDataObjects.size()})
}
断言jdbcDataObjects == jsonDataObjects,Jdbc和Json数据比较失败

在上面的第三行不匹配,所以assert会抛出以下错误:


抓住:java.lang.AssertionError:比较失败。表达式:(jdbcDataObjects == jsonDataObjects)。值:jdbcDataObjects = [默认组3,默认组,默认组],jsonDataObjects = [默认组,默认组,默认组]
java.lang.AssertionError:比较失败。表达式:(jdbcDataObjects == jsonDataObjects)。值:jdbcDataObjects = [默认组3,默认组,默认组],jsonDataObjects = [默认组,默认组,默认组]
在So31472381.run(So31472381.groovy:104)

过程完成退出代码1


如果删除3rd(来自两个响应),则看不到任何错误,这表示成功比较jdbc和json响应。


请注意, groovy脚本免费版和专业版的 SoapUI 。所以这个解决方案适用于两个版本的SoapUI。



In SoapUI, I have a JDBC Test Step that returns the following data:

<Results>
<ResultSet fetchSize="128">
    <Row rowNumber="1">
        <ID>1</ID>
        <NAME>TestName1</NAME>
        <DESCRIPTION/>
        <TYPE>Bool</TYPE>
        <ISPRODUCTTAG>true</ISPRODUCTTAG>
        <ISLOCATIONTAG>false</ISLOCATIONTAG>
        <SUBSECTION>Default Sub Section</SUBSECTION>
        <SECTION>Default Section</SECTION>
        <SUBGROUP>Default Sub Group</SUBGROUP>
        <GROUP>Default Group</GROUP>
    </Row>
    <Row rowNumber="2">
        <ID>2</ID>
        <NAME>TestName2</NAME>
        <DESCRIPTION/>
        <TYPE>Bool</TYPE>
        <ISPRODUCTTAG>true</ISPRODUCTTAG>
        <ISLOCATIONTAG>false</ISLOCATIONTAG>
        <SUBSECTION>Default Sub Section</SUBSECTION>
        <SECTION>Default Section</SECTION>
        <SUBGROUP>Default Sub Group</SUBGROUP>
        <GROUP>Default Group</GROUP>
    </Row>
    </Row>
</ResultSet>

I have an REST API XML Response that contains the following data:

    <ArrayOfTagInfo>
   <TagInfo id="1" name="TestName1" type="Bool" isProductTag="true" isLocationTag="false" subsection="Default Sub Section" section="Default Section" subgroup="Default Sub Group" group="Default Group"/>
   <TagInfo id="2" name="TestName2" type="Bool" isProductTag="true" isLocationTag="false" subsection="Default Sub Section" section="Default Section" subgroup="Default Sub Group" group="Default Group"/>
</ArrayOfTagInfo>

I would like to be able to compare(assert) both the Database Values and the Response Values (response can be in XML or JSON depending on the Request Accept Header) using groovy arrays if possible as the data returned from the database can be very large.

Can anyone help?

解决方案

You can design your test case with the following test steps:

  1. jdbc step
  2. json step
  3. groovy script step

The idea/sudo code here in groovy script step is that

  • get the response of jdbc step
  • get the response of json step
  • build the data in the form of objects, so that it would be easy for comparison
  • store the objects in list, one for jdbc and another one json
  • sort both the lists, to make sure comparing the same data
  • compare that both the list are same.

Here goes the groovy script:

/**
 * Model object for comparing
 */
@groovy.transform.Canonical
class Model {
    def id
    def name
    def type
    def isProductTag
    def isLocationTag
    def subSection
    def section
    def subGroup
    def group

    /**
     * this will acception jdbc row
     * @param row
     * @return
     */
    def buildJdbcData(row) {
        row.with {
            id = ID
            name = NAME
            type = TYPE
            isProductTag = ISPRODUCTTAG
            isLocationTag = ISLOCATIONTAG
            subSection = SUBSECTION
            section = SECTION
            subGroup = SUBGROUP
            group = GROUP
        }
    }

    /**
     * this will accept the json TagInfo
     * @param tagInfo
     * @return
     */
    def buildJsonData(tagInfo){
        id = tagInfo.@id
        name = tagInfo.@name
        type = tagInfo.@type
        isProductTag = tagInfo.@isProductTag
        isLocationTag = tagInfo.@isLocationTag
        subSection = tagInfo.@subsection
        section = tagInfo.@section
        subGroup = tagInfo.@subgroup
        group = tagInfo.@group
    }
}

/**
 * Creating the jdbcResponse from the response received, using fixed value for testing
 * If you want, you can assign the response received directly using below instead of current and make sure you replace the step name correctly
 * def jdbcResponse = context.expand('${JdbcStepName#Response}')
 */
def jdbcResponse = '''<Results>
<ResultSet fetchSize="128">
    <Row rowNumber="1">
        <ID>1</ID>
        <NAME>TestName1</NAME>
        <DESCRIPTION/>
        <TYPE>Bool</TYPE>
        <ISPRODUCTTAG>true</ISPRODUCTTAG>
        <ISLOCATIONTAG>false</ISLOCATIONTAG>
        <SUBSECTION>Default Sub Section</SUBSECTION>
        <SECTION>Default Section</SECTION>
        <SUBGROUP>Default Sub Group</SUBGROUP>
        <GROUP>Default Group</GROUP>
    </Row>
    <Row rowNumber="2">
        <ID>2</ID>
        <NAME>TestName2</NAME>
        <DESCRIPTION/>
        <TYPE>Bool</TYPE>
        <ISPRODUCTTAG>true</ISPRODUCTTAG>
        <ISLOCATIONTAG>false</ISLOCATIONTAG>
        <SUBSECTION>Default Sub Section</SUBSECTION>
        <SECTION>Default Section</SECTION>
        <SUBGROUP>Default Sub Group</SUBGROUP>
        <GROUP>Default Group</GROUP>
    </Row>
    <!--added 3rd row for testing the failure -->
    <Row rowNumber="3">
        <ID>3</ID>
        <NAME>TestName3</NAME>
        <DESCRIPTION/>
        <TYPE>Bool</TYPE>
        <ISPRODUCTTAG>false</ISPRODUCTTAG>
        <ISLOCATIONTAG>false</ISLOCATIONTAG>
        <SUBSECTION>Default Sub Section3</SUBSECTION>
        <SECTION>Default Section3</SECTION>
        <SUBGROUP>Default Sub Group3</SUBGROUP>
        <GROUP>Default Group3</GROUP>
    </Row>
</ResultSet>
</Results>'''

/**
 * Creating the jsonResponse from the response received, using fixed value for testing
 * If you want, you can assign the response received directly using below instead of current and make sure you replace the step name correctly
 * def jsonResponse = context.expand('${JsonStepName#Response}')

 */
def restResponse = '''
<ArrayOfTagInfo>
   <TagInfo id="1" name="TestName1" type="Bool" isProductTag="true" isLocationTag="false" subsection="Default Sub Section" section="Default Section" subgroup="Default Sub Group" group="Default Group"/>
   <TagInfo id="2" name="TestName2" type="Bool" isProductTag="true" isLocationTag="false" subsection="Default Sub Section" section="Default Section" subgroup="Default Sub Group" group="Default Group"/>
  <!--added 3rd row for testing the failure -->
   <TagInfo id="3" name="TestName3" type="Bool" isProductTag="true" isLocationTag="false" subsection="Default Sub Section" section="Default Section" subgroup="Default Sub Group" group="Default Group"/>
</ArrayOfTagInfo>'''

//Parsing the jdbc and build the jdbc model object list
def results = new XmlSlurper().parseText(jdbcResponse)
def jdbcDataObjects = []
results.ResultSet.Row.each { row ->
    jdbcDataObjects.add(new Model().buildJdbcData(row))
}

//Parsing the json and build the json model object list
def arrayOfTagInfo = new XmlSlurper().parseText(restResponse)
def jsonDataObjects = []
arrayOfTagInfo.TagInfo.each { tagInfo ->
    jsonDataObjects.add(new Model().buildJsonData(tagInfo))
}

//sorting the Data before checking for equality
jdbcDataObjects.sort()
jsonDataObjects.sort()

if (jdbcDataObjects.size() != jsonDataObjects.size()) {
    System.err.println("Jdbc resultset size is : ${jdbcDataObjects.size()} and Json result size is : ${jsonDataObjects.size()}")
}
assert jdbcDataObjects == jsonDataObjects, "Comparison of Jdbc and Json data is failed"

In the above, 3rd row is not matching, so assert will throw the following error:

Caught: java.lang.AssertionError: Comparison Failed. Expression: (jdbcDataObjects == jsonDataObjects). Values: jdbcDataObjects = [Default Group3, Default Group, Default Group], jsonDataObjects = [Default Group, Default Group, Default Group] java.lang.AssertionError: Comparison Failed. Expression: (jdbcDataObjects == jsonDataObjects). Values: jdbcDataObjects = [Default Group3, Default Group, Default Group], jsonDataObjects = [Default Group, Default Group, Default Group] at So31472381.run(So31472381.groovy:104)
Process finished with exit code 1

If you remove 3rd (from both responses), then you do not see any error, which is indication of successful comparison of jdbc and json responses.

Note that groovy script is available in both free and pro version of SoapUI. So this solution works for both the editions of SoapUI.

这篇关于使用SoapUI中的groovy数组动态比较Rest XML / JSON响应和JDBC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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