Forum Discussion

AAB's avatar
AAB
Regular Contributor
4 years ago
Solved

ReadyAPI - How to compare quoted array with unquoted array

Hello,

 

For my test I have a REST Request that gives back a json response.

 

 

[{
        "nationalId": "01050110250",
        "driverLicences": [{
                "model": "BC",
                "number": "1453464727",
                "deliveryType": "INITIAL",
                "deliveryDate": "2019-05-26",
                "nis": "36015",
                "codes": []
            }, {
                "model": "36",
                "number": "1401373153",
                "deliveryType": "INITIAL",
                "deliveryDate": "2018-07-11",
                "nis": "36015",
                "codes": []
            }
        ]
    }, {
        "nationalId": "91061463327",
        "driverLicences": [{
                "model": "BC",
                "number": "1453462846",
                "deliveryType": "INITIAL",
                "deliveryDate": "2019-05-26",
                "nis": "36015",
                "codes": []
            }, {
                "model": "18",
                "number": "1399476210",
                "deliveryType": "INITIAL",
                "deliveryDate": "2018-06-30",
                "nis": "36015",
                "codes": []
            }
        ]
    }, {
        "nationalId": "01020214652",
        "driverLicences": [{
                "model": "BC",
                "number": "1453461856",
                "deliveryType": "INITIAL",
                "deliveryDate": "2019-05-26",
                "nis": "33011",
                "codes": []
            }, {
                "model": "18",
                "number": "1436496259",
                "deliveryType": "EXCHANGE",
                "deliveryDate": "2019-02-13",
                "nis": "33039",
                "codes": []
            }, {
                "model": "36",
                "number": "1402915651",
                "deliveryType": "INITIAL",
                "deliveryDate": "2018-07-19",
                "nis": "33039",
                "codes": []
            }
        ]
    }, {
        "nationalId": "01052314229",
        "driverLicences": [{
                "model": "BC",
                "number": "1453472447",
                "deliveryType": "INITIAL",
                "deliveryDate": "2019-05-26",
                "nis": "11002",
                "codes": []
            }, {
                "model": "36",
                "number": "1401628719",
                "deliveryType": "INITIAL",
                "deliveryDate": "2018-07-11",
                "nis": "11002",
                "codes": []
            }
        ]
    }
}]

 

 

As you can see, this json has an array with responses. I've searched for the nationalId and stashed them in a parameter on testcase level. This results in an array without quotes.

 

The extraction was done like this:

def getNRN = context.expand( '${REST Request#Response#$[*][\'nationalId\']}' )
assert getNRN != "" : "NRN is blank"
assert getNRN != null : "NRN is null"
testRunner.testCase.setPropertyValue("p_NRN", getNRN.toString())
log.info getNRN

 

A second test step will get information from a logfile  on a  website where the same values are in it. But the problem here is that this response gives me the same array but with quotes and as an XML answer.

HTTP/1.1 200 OK
Date: Thu, 08 Oct 2020 14:18:54 GMT
Server: Apache
Strict-Transport-Security: max-age=31536000
Last-Modified: Thu, 08 Oct 2020 10:08:00 GMT
ETag: "24098a-525-5b12601c504b2"
Accept-Ranges: bytes
Content-Length: 1317
Connection: close
Content-Type: text/plain

<data>
  <Admin-data>
    <Timestamp>2020-10-08 12:02:49</Timestamp>
    <DataTreatmentId>DTO_S666_001</DataTreatmentId>
  </Admin-data>
  <URL-data>
    <URL>/MobilityServices/1.0/ComebackMomentService</URL>
  </URL-data>
  <QueryString-data>
  </QueryString-data>
  <RequestHTTPHeader-data>
    <fsbTransactionId>X37jyCwR6FDqdTz3XUxvlQAAANU</fsbTransactionId>
    <method>GET</method>
  </RequestHTTPHeader-data>
  <RequestBody-data>
  </RequestBody-data>
  <ResponseHTTPHeader-data>
    <Response-code>200</Response-code>
    <ServiceInfo>{prvateinformation}</ServiceInfo>
  </ResponseHTTPHeader-data>
  <ResponseBody-data>
    <DataSubject-01>["01050110250","91061463327","01020214652","01052314229"]</DataSubject-01>
  </ResponseBody-data>
</data>

 

I need to assert that both arrays are the same. How can  I do so please? 

 

What I have so far (but it gives me an error message of course)

import com.eviware.soapui.support.XmlHolder
import groovy.json.JsonSlurper
//import groovy.xml.XmlSlurper

def holder = new XmlHolder( messageExchange.responseContentAsXml )

def rrn = holder.getNodeValue( "//data[1]/ResponseBody-data[1]/DataSubject-01[1]" )
log.info rrn.toString()

def natRegNr = context.expand( '${#TestCase#p_NRN}' )

assert getRRN != null
assert getRRN == natRegNr

I've tried to escape the quotes from the response but the regex doesn't work ( 

rrn.replace("\"","")  )

If I parse this into a jsonSlurper, the result remains the same. The quotes are still there.

 

anybody an idea please?

Thanks in advance,

AboveAndAbove.

 

  • AAB : As i can see you are just logging the value not storing the value in the variable. Just assign the value in the variable as below

     

    def rrn = holder.getNodeValue( "//data[1]/ResponseBody-data[1]/DataSubject-01[1]" )
    rrn = rrn.toString().replace("\"","")

     

    Hope this will work.

     

5 Replies

  • AAB : As i can see you are just logging the value not storing the value in the variable. Just assign the value in the variable as below

     

    def rrn = holder.getNodeValue( "//data[1]/ResponseBody-data[1]/DataSubject-01[1]" )
    rrn = rrn.toString().replace("\"","")

     

    Hope this will work.

     

  • richie's avatar
    richie
    Community Hero

    Hey AAB 

     

    first thing that  pops in my head that if you're comparing a json to xml - have you tried using an 'Accept' header (with value 'application/json') on the request that generates the XML to convert the request's XML response to json?  Ive found this quite handy to do this before now - unsure if it would make a differerence in your example however, as your NationalId values in your XML are contained within an array, whereas the NationalId values in the json are just multiple separate name/value pairs.

     

    ignoring that for now - when you say compare quoted array against  unquoted array Im misunderstanding a little - currently you have a json response with text NationalId values that are quoted to represent that the values are text values and your XML includes a Datasubject tag and the value for this tag is an an array, right?

     

    My way is slower and less efficient than the other forum members would probably use - but I'd extract each of the values from the json to individual properties within a Properties step and I'd extract the individual values from the Datasubject tag value's array into individual properties within the Properties step and then running the comparison's fairly straightforward, right?  The complication in this scenario is that you are trying to compare individual name/value pairs to comma separated values within a single array.  I dont see how to do this other than to split them out into a map, specific properties - but obviously my groovy's rubbish relative to the others.

     

    I've no doubt the other fellas will cough up something whizzy, but if they dont respond within a day or so, I'll publish my clunky version!

     

    ta

     

    rich

    • AAB's avatar
      AAB
      Regular Contributor

      Hi richie 

       

      Thanks for your time!... once again  🙂

      A little bit of nerdy explanation in what I do know in coding (not much but I handle it  🙂  ....  At least I'm trying  😄   )

       

      For starters, this is not a simple REST Request, this is an HTTP request. (I've should have mentioned that maybe...?) This said, the response that I'm receiving is an HTML page that can be read as an XML. So your proposition

      "tried using an 'Accept' header (with value 'application/json')" is not do-able for me. (I've tried but this gives me a 'bad message' error  😉   )

       

      " as your NationalId values in your XML are contained within an array, whereas the NationalId values in the json are just multiple separate name/value pairs. "
      not true in this case, this is a json array 😉 watch the " {[ ]} ". Json always starts with " { } " but it can go on with lists or it can contain arrays " [ ] ". And as the answer is giving me [nr1, nr2, nr3, etc..] it means that the response is an array. With jsonSlurper you can read the values in that array, so no problems there, but with XmlSlurper apparently it also gives the quotes. Like said in the description, I've tried to omit the quotes with regex but it doesn't work. I think because this code is in an assertion script code....? don't know. 

       

      To the groovy guru's: any idea? preferred code wise as I already have code to fetch other ID's in this response.

       

      nmrao     HimanshuTayal   

       

      Thanks in advance,

      AboveAndBeyond

  • AAB :

     

    Easiest solution will be :

     

    def rrn = holder.getNodeValue( "//data[1]/ResponseBody-data[1]/DataSubject-01[1]" )
    log.info rrn.toString().replace("\"","")

     

     

    I hope this will work 🙂

     

    • AAB's avatar
      AAB
      Regular Contributor

      Hello HimanshuTayal 

       

      thanks for your response but I already tried that before and it didn't work.

      I tried it again but the quotes still remain.

       

       

      Kind regards,

      AboveAndBeyond