使用groovy脚本在SoapUI中将JDBC响应与REST API响应进行比较

时间:2019-01-18 15:48:30

标签: rest groovy soapui

我一直在尝试制作可解析jdbc和REST响应的常规脚本,将结果放入模型中,然后进行比较。我正在遵循以下答案:Dynamically compare Rest XML/JSON response and JDBC using groovy array in SoapUI,但没有取得太大的成功。我的jdbc响应如下:

<Results>
    <ResultSet fetchSize="128">
        <Row rowNumber="1">
            <ID>BCE448A4DFB94C6892D957DB8275D2AC</ID>
            <NAME>SevDealRecord</NAME>
            <AMOUNT/>
            <CREATIONDATE>2012-06-20 11:31:48.0</CREATIONDATE>
            <MODIFICATIONDATE>2012-06-20 15:20:02.0</MODIFICATIONDATE>
            <CURRENCY>EUR</CURRENCY>
            <REFERENCEDATE>2012-06-20 00:00:00.0</REFERENCEDATE>
            <STATUSCODE>DPE_2</STATUSCODE>
            <STATUSDESCRIPTION>2 - Preliminary evaluation in progress (Direct PE)</STATUSDESCRIPTION>
            <ASSIGNEDTOUSERIQID>E506565555A6486FBA8FCC431F4E979E</ASSIGNEDTOUSERIQID>
            <ASSIGNTOUSERDISPLAYNAME>NMISO</ASSIGNTOUSERDISPLAYNAME>
            <WORKFLOWID>140AE208F9334FB9946BFEAF5C89CE18</WORKFLOWID>
            <WORKFLOWNAME>1 - Direct Private Equity</WORKFLOWNAME>
        </Row>
        <Row rowNumber="2">
            <ID>D4DBB1B906A04DE49AB1FF3EE4653180</ID>
            <NAME>T28678</NAME>
            <AMOUNT/>
            <CREATIONDATE>2012-06-21 13:45:36.0</CREATIONDATE>
            <MODIFICATIONDATE>2012-06-21 13:46:03.0</MODIFICATIONDATE>
            <CURRENCY>EUR</CURRENCY>
            <REFERENCEDATE>2012-06-21 00:00:00.0</REFERENCEDATE>
            <STATUSCODE>DRAFT</STATUSCODE>
            <STATUSDESCRIPTION>Draft{F}Brouillon</STATUSDESCRIPTION>
            <ASSIGNEDTOUSERIQID>E506565555A6486FBA8FCC431F4E979E</ASSIGNEDTOUSERIQID>
            <ASSIGNTOUSERDISPLAYNAME>NMISO</ASSIGNTOUSERDISPLAYNAME>
            <WORKFLOWID/>
            <WORKFLOWNAME/>
        </Row>

这是REST响应:

[{
    "id": "12CF6F8DA3B148D98D63A428EC7F8D7B",
    "name": "アコム株式会社",
    "amount1": null,
    "creationDate": null,
    "modificationDate": "2019-01-14T16:28:21.027+00:00",
    "currency": "USD",
    "referenceDate": "2019-01-04T00:00:00+00:00",
    "status": {
        "code": "DRAFT",
        "description": "Draft"
    },
    "assignedToUser": {
        "id": "E506565555A6486FBA8FCC431F4E979E",
        "displayName": "NMISO"
    },
    "assignedToGroup": null,
    "workflow": null
}, {
    "id": "AA4F19E5C8B34222865EFED293D52146",
    "name": "Lürssen",
    "amount1": null,
    "creationDate": null,
    "modificationDate": "2019-01-14T16:28:20.963+00:00",
    "currency": "USD",
    "referenceDate": "2019-01-04T00:00:00+00:00",
    "status": {
        "code": "DRAFT",
        "description": "Draft"
    },
    "assignedToUser": {
        "id": "E506565555A6486FBA8FCC431F4E979E",
        "displayName": "NMISO"
    },
    "assignedToGroup": null,
    "workflow": null
},

我尝试过的事情:

@groovy.transform.Canonical
class Model {
    def id
    def name
    def amount1
    def creationDate
    def modificationDate
    def currency
    def referenceDate
    def statusCode
    def statusDescription
    def assignedToUserIqid
    def assignedToUserDisplayName
    def assignedToGroup
    def workflowId
    def workflowName

    // this will accept jdbc row
    def buildJdbcData(row) {
        row.with {
            id = ID
            name = NAME
            amount1 = AMOUNT
            creationDate = CREATIONDATE
            modificationDate = MODIFICATIONDATE
            currency = CURRENCY
            referenceDate = REFERENCEDATE
            statusCode = STATUSCODE
            statusDescription = STATUSDESCRIPTION
            assignedToUserDisplayName = ASSIGNTOUSERDISPLAYNAME
            assignedToGroup = ASSIGNTOUSERDISPLAYNAME
            workflowId = WORKFLOWID
            workflowName = WORKFLOWNAME
        }
    }

    def buildJsonData(slurp){
        id = slurp.id
        name = slurp.name
        amount1 = slurp.amount1
        creationDate = slurp.creationDate
        modificationDate = slurp.modificationDate
        currency = slurp.currency
        referenceDate = slurp.referenceDate
        statusCode = slurp.status.code
        statusDescription = slurp.status.description
        assignedToUserIqid = slurp.assignedToUser.id
        assignedToUserDisplayName = slurp.assignedToUser.displayName
        assignedToGroup = slurp.assignedToGroup
        workflowId = slurp.workflow
    }
}

def jdbcResponse = context.expand('${JDBC_DealList#ResponseAsXml}')
def results = new XmlSlurper().parseText(jdbcResponse)

def jdbcDataObjects = []
results.ResultSet.Row.each { row ->
    jdbcDataObjects.add(new Model().buildJdbcData(row)) //Objects not added properly to the model
}
log.info jdbcDataObjects

def jsonResponse = testRunner.testCase.testSteps["Deals"].testRequest.response.contentAsString
def jsonObjects = new JsonSlurper().parseText(jsonResponse)
log.info jsonObjects



def jsonDataObjects = [] jsonDataObjects.add(new Model().buildJsonData(jsonObjects))

现在,log.info jdbcDataObjects从jdbc响应中给了我WORKFLOWNAME元素。 log.info jsonObjects给了我整个JSON模型,而且我不确定如何将所有元素添加到上面定义的Model中吗?一些帮助将不胜感激。

1 个答案:

答案 0 :(得分:-1)

在上一个项目中,我们这样做,但是对于Soap而言不是REST。但是我相信,您可以采用相同的方法。 1.我们把查询放在Execel -ESMQuery1中 2.Execel有2列。要比较的第一个节点,要比较的第二个DB值。 例如-// soap / xmlnode1 ESMQuery1(UserName) 这里的UserName是列名。 3.您只需要为提到的所有节点创建循环并解析ESMQuery(UserName)。

您可以使用Json路径来代替xmlpath。

谢谢。