如何断言节点值与另一个节点值相关联

时间:2016-12-30 01:34:55

标签: groovy soapui

我有来自查询数据库的以下XML

<Results>
   <ResultSet fetchSize="128">
      <Row rowNumber="1">
         <REGISTRATIONKEY>1644606</REGISTRATIONKEY>
         <WORKFLOWACTIVITY>Fraud Identity Validation (NegMatch)</WORKFLOWACTIVITY>
         <WORKFLOWACTIVITYKEY>3</WORKFLOWACTIVITYKEY>
         <WORKFLOWACTIVITYSTATUSKEY>2</WORKFLOWACTIVITYSTATUSKEY>
         <WORKFLOWACTIVITYSTATUS>Passed</WORKFLOWACTIVITYSTATUS>
         <WORKFLOWPROCESSKEY>1</WORKFLOWPROCESSKEY>
         <WORKFLOWPROCESSSTATUSKEY>1</WORKFLOWPROCESSSTATUSKEY>
         <WORKFLOWPROCESS>CardAccountRegistration</WORKFLOWPROCESS>
         <WORKFLOWPROCESSSTATUS>Started</WORKFLOWPROCESSSTATUS>
         <CREATEDATE>2016-12-29 15:29:25.3</CREATEDATE>
      </Row>
      <Row rowNumber="2">
         <REGISTRATIONKEY>1644606</REGISTRATIONKEY>
         <WORKFLOWACTIVITY>Age Verification</WORKFLOWACTIVITY>
         <WORKFLOWACTIVITYKEY>6</WORKFLOWACTIVITYKEY>
         <WORKFLOWACTIVITYSTATUSKEY>2</WORKFLOWACTIVITYSTATUSKEY>
         <WORKFLOWACTIVITYSTATUS>Passed</WORKFLOWACTIVITYSTATUS>
         <WORKFLOWPROCESSKEY>1</WORKFLOWPROCESSKEY>
         <WORKFLOWPROCESSSTATUSKEY>1</WORKFLOWPROCESSSTATUSKEY>
         <WORKFLOWPROCESS>CardAccountRegistration</WORKFLOWPROCESS>
         <WORKFLOWPROCESSSTATUS>Started</WORKFLOWPROCESSSTATUS>
         <CREATEDATE>2016-12-29 15:29:25.3</CREATEDATE>
      </Row>
      <Row rowNumber="3">
         <REGISTRATIONKEY>1644606</REGISTRATIONKEY>
         <WORKFLOWACTIVITY>Address Validation</WORKFLOWACTIVITY>
         <WORKFLOWACTIVITYKEY>2</WORKFLOWACTIVITYKEY>
         <WORKFLOWACTIVITYSTATUSKEY>2</WORKFLOWACTIVITYSTATUSKEY>
         <WORKFLOWACTIVITYSTATUS>Passed</WORKFLOWACTIVITYSTATUS>
         <WORKFLOWPROCESSKEY>1</WORKFLOWPROCESSKEY>
         <WORKFLOWPROCESSSTATUSKEY>1</WORKFLOWPROCESSSTATUSKEY>
         <WORKFLOWPROCESS>CardAccountRegistration</WORKFLOWPROCESS>
         <WORKFLOWPROCESSSTATUS>Started</WORKFLOWPROCESSSTATUS>
         <CREATEDATE>2016-12-29 15:29:25.3</CREATEDATE>
      </Row>
      <Row rowNumber="4">
         <REGISTRATIONKEY>1644606</REGISTRATIONKEY>
         <WORKFLOWACTIVITY>Card Limit Validation</WORKFLOWACTIVITY>
         <WORKFLOWACTIVITYKEY>4</WORKFLOWACTIVITYKEY>
         <WORKFLOWACTIVITYSTATUSKEY>2</WORKFLOWACTIVITYSTATUSKEY>
         <WORKFLOWACTIVITYSTATUS>Passed</WORKFLOWACTIVITYSTATUS>
         <WORKFLOWPROCESSKEY>1</WORKFLOWPROCESSKEY>
         <WORKFLOWPROCESSSTATUSKEY>1</WORKFLOWPROCESSSTATUSKEY>
         <WORKFLOWPROCESS>CardAccountRegistration</WORKFLOWPROCESS>
         <WORKFLOWPROCESSSTATUS>Started</WORKFLOWPROCESSSTATUS>
         <CREATEDATE>2016-12-29 15:29:25.3</CREATEDATE>
      </Row>
      <Row rowNumber="5">
         <REGISTRATIONKEY>1644606</REGISTRATIONKEY>
         <WORKFLOWACTIVITY>Customer Identity Validation (CIP)</WORKFLOWACTIVITY>
         <WORKFLOWACTIVITYKEY>5</WORKFLOWACTIVITYKEY>
         <WORKFLOWACTIVITYSTATUSKEY>3</WORKFLOWACTIVITYSTATUSKEY>
         <WORKFLOWACTIVITYSTATUS>Failed</WORKFLOWACTIVITYSTATUS>
         <WORKFLOWPROCESSKEY>1</WORKFLOWPROCESSKEY>
         <WORKFLOWPROCESSSTATUSKEY>1</WORKFLOWPROCESSSTATUSKEY>
         <WORKFLOWPROCESS>CardAccountRegistration</WORKFLOWPROCESS>
         <WORKFLOWPROCESSSTATUS>Started</WORKFLOWPROCESSSTATUS>
         <CREATEDATE>2016-12-29 15:29:25.503</CREATEDATE>
      </Row>
      <Row rowNumber="6">
         <REGISTRATIONKEY>1644606</REGISTRATIONKEY>
         <WORKFLOWACTIVITY>Out Of Wallet Quiz</WORKFLOWACTIVITY>
         <WORKFLOWACTIVITYKEY>11</WORKFLOWACTIVITYKEY>
         <WORKFLOWACTIVITYSTATUSKEY>4</WORKFLOWACTIVITYSTATUSKEY>
         <WORKFLOWACTIVITYSTATUS>Skipped</WORKFLOWACTIVITYSTATUS>
         <WORKFLOWPROCESSKEY>1</WORKFLOWPROCESSKEY>
         <WORKFLOWPROCESSSTATUSKEY>1</WORKFLOWPROCESSSTATUSKEY>
         <WORKFLOWPROCESS>CardAccountRegistration</WORKFLOWPROCESS>
         <WORKFLOWPROCESSSTATUS>Started</WORKFLOWPROCESSSTATUS>
         <CREATEDATE>2016-12-29 15:29:25.613</CREATEDATE>
      </Row>
      <Row rowNumber="7">
         <REGISTRATIONKEY>1644606</REGISTRATIONKEY>
         <WORKFLOWACTIVITY>OFAC</WORKFLOWACTIVITY>
         <WORKFLOWACTIVITYKEY>7</WORKFLOWACTIVITYKEY>
         <WORKFLOWACTIVITYSTATUSKEY>2</WORKFLOWACTIVITYSTATUSKEY>
         <WORKFLOWACTIVITYSTATUS>Passed</WORKFLOWACTIVITYSTATUS>
         <WORKFLOWPROCESSKEY>1</WORKFLOWPROCESSKEY>
         <WORKFLOWPROCESSSTATUSKEY>1</WORKFLOWPROCESSSTATUSKEY>
         <WORKFLOWPROCESS>CardAccountRegistration</WORKFLOWPROCESS>
         <WORKFLOWPROCESSSTATUS>Started</WORKFLOWPROCESSSTATUS>
         <CREATEDATE>2016-12-29 15:29:25.613</CREATEDATE>
      </Row>
   </ResultSet>
</Results>

如何在groovy中断言一个节点值与另一个节点值相关联或绑定。因此,例如,我想验证价值&#34;客户身份验证(CIP)&#34;来自WORKFLOWACTIVITY节点,与值&#34;失败&#34;相关联。来自WORKFLOWACTIVITYSTATUS节点。而且我不完全确定这块是否可以:

</Row>
<Row rowNumber="5">
<REGISTRATIONKEY>1644606</REGISTRATIONKEY>
<WORKFLOWACTIVITY>Customer Identity Validation (CIP)</WORKFLOWACTIVITY>
<WORKFLOWACTIVITYKEY>5</WORKFLOWACTIVITYKEY>
<WORKFLOWACTIVITYSTATUSKEY>3</WORKFLOWACTIVITYSTATUSKEY>
<WORKFLOWACTIVITYSTATUS>Failed</WORKFLOWACTIVITYSTATUS>
<WORKFLOWPROCESSKEY>1</WORKFLOWPROCESSKEY>
<WORKFLOWPROCESSSTATUSKEY>1</WORKFLOWPROCESSSTATUSKEY>
<WORKFLOWPROCESS>CardAccountRegistration</WORKFLOWPROCESS>
<WORKFLOWPROCESSSTATUS>Started</WORKFLOWPROCESSSTATUS>
<CREATEDATE>2016-12-29 15:29:25.503</CREATEDATE>
</Row>

总是会回到第5行

1 个答案:

答案 0 :(得分:1)

根据上述SoapUI步骤结果,您似乎正在使用jdbc 您可以使用以下Script Assertion进行相同的测试步骤。

脚本断言

//Check if the result is not empty  
assert context.result

//Closure to get the sibling element data
def getSiblingData = { data, elementName, elementValue, siblingName ->
    def parsedResult = new XmlSlurper().parseText(data)
    (parsedResult.'**'.findAll {it.name() == elementName && it.text()== elementValue }*.parent()."$siblingName").inject( [] ) { result, item -> result << item.text(); result }
}

//Assert the result
//Note that data is passed to getSiblingData closure
assert ['Customer Identity Validation (CIP)'] == getSiblingData(context.response, 'WORKFLOWACTIVITYSTATUS', 'Failed', 'WORKFLOWACTIVITY')

如果您只想要standalone groovy script

def results = """<Results>
   <ResultSet fetchSize="128">
      <Row rowNumber="1">
         <REGISTRATIONKEY>1644606</REGISTRATIONKEY>
         <WORKFLOWACTIVITY>Fraud Identity Validation (NegMatch)</WORKFLOWACTIVITY>
         <WORKFLOWACTIVITYKEY>3</WORKFLOWACTIVITYKEY>
         <WORKFLOWACTIVITYSTATUSKEY>2</WORKFLOWACTIVITYSTATUSKEY>
         <WORKFLOWACTIVITYSTATUS>Passed</WORKFLOWACTIVITYSTATUS>
         <WORKFLOWPROCESSKEY>1</WORKFLOWPROCESSKEY>
         <WORKFLOWPROCESSSTATUSKEY>1</WORKFLOWPROCESSSTATUSKEY>
         <WORKFLOWPROCESS>CardAccountRegistration</WORKFLOWPROCESS>
         <WORKFLOWPROCESSSTATUS>Started</WORKFLOWPROCESSSTATUS>
         <CREATEDATE>2016-12-29 15:29:25.3</CREATEDATE>
      </Row>
      <Row rowNumber="2">
         <REGISTRATIONKEY>1644606</REGISTRATIONKEY>
         <WORKFLOWACTIVITY>Age Verification</WORKFLOWACTIVITY>
         <WORKFLOWACTIVITYKEY>6</WORKFLOWACTIVITYKEY>
         <WORKFLOWACTIVITYSTATUSKEY>2</WORKFLOWACTIVITYSTATUSKEY>
         <WORKFLOWACTIVITYSTATUS>Passed</WORKFLOWACTIVITYSTATUS>
         <WORKFLOWPROCESSKEY>1</WORKFLOWPROCESSKEY>
         <WORKFLOWPROCESSSTATUSKEY>1</WORKFLOWPROCESSSTATUSKEY>
         <WORKFLOWPROCESS>CardAccountRegistration</WORKFLOWPROCESS>
         <WORKFLOWPROCESSSTATUS>Started</WORKFLOWPROCESSSTATUS>
         <CREATEDATE>2016-12-29 15:29:25.3</CREATEDATE>
      </Row>
      <Row rowNumber="3">
         <REGISTRATIONKEY>1644606</REGISTRATIONKEY>
         <WORKFLOWACTIVITY>Address Validation</WORKFLOWACTIVITY>
         <WORKFLOWACTIVITYKEY>2</WORKFLOWACTIVITYKEY>
         <WORKFLOWACTIVITYSTATUSKEY>2</WORKFLOWACTIVITYSTATUSKEY>
         <WORKFLOWACTIVITYSTATUS>Passed</WORKFLOWACTIVITYSTATUS>
         <WORKFLOWPROCESSKEY>1</WORKFLOWPROCESSKEY>
         <WORKFLOWPROCESSSTATUSKEY>1</WORKFLOWPROCESSSTATUSKEY>
         <WORKFLOWPROCESS>CardAccountRegistration</WORKFLOWPROCESS>
         <WORKFLOWPROCESSSTATUS>Started</WORKFLOWPROCESSSTATUS>
         <CREATEDATE>2016-12-29 15:29:25.3</CREATEDATE>
      </Row>
      <Row rowNumber="4">
         <REGISTRATIONKEY>1644606</REGISTRATIONKEY>
         <WORKFLOWACTIVITY>Card Limit Validation</WORKFLOWACTIVITY>
         <WORKFLOWACTIVITYKEY>4</WORKFLOWACTIVITYKEY>
         <WORKFLOWACTIVITYSTATUSKEY>2</WORKFLOWACTIVITYSTATUSKEY>
         <WORKFLOWACTIVITYSTATUS>Passed</WORKFLOWACTIVITYSTATUS>
         <WORKFLOWPROCESSKEY>1</WORKFLOWPROCESSKEY>
         <WORKFLOWPROCESSSTATUSKEY>1</WORKFLOWPROCESSSTATUSKEY>
         <WORKFLOWPROCESS>CardAccountRegistration</WORKFLOWPROCESS>
         <WORKFLOWPROCESSSTATUS>Started</WORKFLOWPROCESSSTATUS>
         <CREATEDATE>2016-12-29 15:29:25.3</CREATEDATE>
      </Row>
      <Row rowNumber="5">
         <REGISTRATIONKEY>1644606</REGISTRATIONKEY>
         <WORKFLOWACTIVITY>Customer Identity Validation (CIP)</WORKFLOWACTIVITY>
         <WORKFLOWACTIVITYKEY>5</WORKFLOWACTIVITYKEY>
         <WORKFLOWACTIVITYSTATUSKEY>3</WORKFLOWACTIVITYSTATUSKEY>
         <WORKFLOWACTIVITYSTATUS>Failed</WORKFLOWACTIVITYSTATUS>
         <WORKFLOWPROCESSKEY>1</WORKFLOWPROCESSKEY>
         <WORKFLOWPROCESSSTATUSKEY>1</WORKFLOWPROCESSSTATUSKEY>
         <WORKFLOWPROCESS>CardAccountRegistration</WORKFLOWPROCESS>
         <WORKFLOWPROCESSSTATUS>Started</WORKFLOWPROCESSSTATUS>
         <CREATEDATE>2016-12-29 15:29:25.503</CREATEDATE>
      </Row>
      <Row rowNumber="6">
         <REGISTRATIONKEY>1644606</REGISTRATIONKEY>
         <WORKFLOWACTIVITY>Out Of Wallet Quiz</WORKFLOWACTIVITY>
         <WORKFLOWACTIVITYKEY>11</WORKFLOWACTIVITYKEY>
         <WORKFLOWACTIVITYSTATUSKEY>4</WORKFLOWACTIVITYSTATUSKEY>
         <WORKFLOWACTIVITYSTATUS>Skipped</WORKFLOWACTIVITYSTATUS>
         <WORKFLOWPROCESSKEY>1</WORKFLOWPROCESSKEY>
         <WORKFLOWPROCESSSTATUSKEY>1</WORKFLOWPROCESSSTATUSKEY>
         <WORKFLOWPROCESS>CardAccountRegistration</WORKFLOWPROCESS>
         <WORKFLOWPROCESSSTATUS>Started</WORKFLOWPROCESSSTATUS>
         <CREATEDATE>2016-12-29 15:29:25.613</CREATEDATE>
      </Row>
      <Row rowNumber="7">
         <REGISTRATIONKEY>1644606</REGISTRATIONKEY>
         <WORKFLOWACTIVITY>OFAC</WORKFLOWACTIVITY>
         <WORKFLOWACTIVITYKEY>7</WORKFLOWACTIVITYKEY>
         <WORKFLOWACTIVITYSTATUSKEY>2</WORKFLOWACTIVITYSTATUSKEY>
         <WORKFLOWACTIVITYSTATUS>Passed</WORKFLOWACTIVITYSTATUS>
         <WORKFLOWPROCESSKEY>1</WORKFLOWPROCESSKEY>
         <WORKFLOWPROCESSSTATUSKEY>1</WORKFLOWPROCESSSTATUSKEY>
         <WORKFLOWPROCESS>CardAccountRegistration</WORKFLOWPROCESS>
         <WORKFLOWPROCESSSTATUS>Started</WORKFLOWPROCESSSTATUS>
         <CREATEDATE>2016-12-29 15:29:25.613</CREATEDATE>
      </Row>
   </ResultSet>
</Results>"""

def getSiblingData = { data, elementName, elementValue, siblingName ->
    def parsedResult = new XmlSlurper().parseText(data)
    (parsedResult.'**'.findAll {it.name() == elementName && it.text()== elementValue }*.parent()."$siblingName").inject( [] ) { result, item -> result << item.text(); result }
}

assert ['Customer Identity Validation (CIP)'] == getSiblingData(results, 'WORKFLOWACTIVITYSTATUS', 'Failed', 'WORKFLOWACTIVITY')