Forum Discussion

richie's avatar
richie
Community Hero
6 years ago
Solved

An Assertion To Verify Repeating Attribute Contains All Upper Case Chars?

Hey!

 

nmrao  and New2API have repeatedly helped me with some scripts to check repeating attributes in my .json response has certain attributes.

 

I now have tests that require me to verify a repeating attribute in the response contains only upper case characters.

 

The following is a script to verify that the repeating 'Name' attribute (repeated 1000s of times) in my .json response is always null 

//All Rao's work
assert context.response, 'Request parameter is correct'
def json = new groovy.json.JsonSlurper().parseText(context.response)
assert json.data.Name.every{null == it}
//assert json.data.Name.every{context.expand('${REST Request#Name}').toInteger() == it}

I was wondering if the above could be altered to assert that an attribute in the attached .json response (DataSourceId_Name) is all UPPER CASE ONLY?

 

I tried googling for groovy methods relating to upper case - but I didn't find anything that I could tailor to what I need - I found the following link but I don't know how to alter the above assertion to get what I need

 

Cheers lads/ladies,

 

richie

  • richie,

    See adding this to existing script and helps!

    //assumes json is defined and field name is DataSourceId_Name
    json.data.DataSourceId_Name.each {
    assert it == it.toUpperCase()
    }

11 Replies

  • Lucian's avatar
    Lucian
    Community Hero

    Hi richie ,

     

    Scripts definetely help. However, it is a good practice to keep the tests simple and use in built functionality instead. This would make the whole thing easier to maintain and easier to understand for a new tester for instance.

     

    In your particular case, why would you use code and not an in-built regex assertion?

    • richie's avatar
      richie
      Community Hero

      Hey man  I'll take anything - I'd been up all night working when  I posted this -  regex didn't even enter my head! ;)

       

      However - its on a repeating attribute - there's maybe 20,000 records of data - yes I can use a contains assertion using regex  but how would I use the embedded functionality to assert a repeating 'whatever' attribute is upper case for 20,000 records?

       

      I don't see how you can..... unless you have some whizzy ideas?

       

      nice one!

       

      richie

       

      • Lucian's avatar
        Lucian
        Community Hero

        Well it's not about the Contains assertion. It is about the JsonPath RegEx Match assertion which is created just for that. If you can provide me a sample response I might be able to help you out.

         

        Lucian.

  • New2API's avatar
    New2API
    Frequent Contributor

    richie, here is what you can do to overcome this Uppercase' issue. it is bit ugly but may work.

     

    import net.sf.*
    import net.sf.json.*
    import net.sf.json.groovy.*
    import groovy.json.JsonSlurper
    
    
    def response = new File(context.expand( '${#TestSuite#ResponsePath}') + 'Untitled1.json').text
        def slurper = new JsonSlurper()
        def json = slurper.parseText response
    
    
    def actual = []
    
    json.data.DataSourceId_Name.each{
    	                            actual = actual+it //Get all DataSourceId_Name
    	
    }
    
    
    //Get unique value from the 'actual' array and loop through
    actual.unique().each{
    	                 log.info it
    	                 it.toUpperCase() == it //Convert each array element to uppercase and compare it to itself
    }
    • richie's avatar
      richie
      Community Hero

      Hey lads! 

       

      New2API  - thank you - I appreciate your efforts on my behalf - but I don't think I explained it clearly enough.

       

      The attached file was an example - but I won't be using it for comparison - that is one of the responses I need to verify the fields display only upper case., so I'm grateful for your help fella - but that's not what I need.

       

      Lucian - another reason I'm using scripts for a lot of my assertions it that I've a mix of OTB functionality REST requests and requests build by a script nmrao put together cos I can't use the OTB functionality to build and submit some of my requests - it doesn't do what I need - hence the reason for the scripting - @Rao's script builds the URI and query string from parameters set in a Properties step.

       

      I do have REST requests that use the OTB functionality - so using the OTB assertion functionality for those requests is fine, but for the requests built and submitted by @Rao's script I can't use the OTB assertions for those.

       

      I have attached an example file

       

      There is a field - JSONPATH = x.data[x].DataSourceId_Name OR $[data][x][DataSourceId_Name] that should ONLY ever have upper case chars.

       

      I am hoping to create an assertion that does this verification - however the response can have many, many records in there - so I can't just add in loads of values to an array as a possible way to assert these values against.

       

      I hope I've been clear in my request!

       

      nice one,

       

      richie

  • groovyguy's avatar
    groovyguy
    Champion Level 1

    What is the endgoal then? To find the field NAME anywhere in a response and validate it as upper case? Otherwise you may want to provide more details. 

    • richie's avatar
      richie
      Community Hero
      groovyguy

      You got it......spot on fella.

      I just need to put an assertion together to verify the contents of a repeating attribute in the response (1 instance of attribute per record, can be many, many records in the response) is always UPPER CASE, whatever the value is.

      Hope i've been clearer than i usually am!

      Nice one,

      richie
      • nmrao's avatar
        nmrao
        Champion Level 3
        richie,

        See adding this to existing script and helps!

        //assumes json is defined and field name is DataSourceId_Name
        json.data.DataSourceId_Name.each {
        assert it == it.toUpperCase()
        }