cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
Highlighted
Community Hero

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

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

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

@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()
}


Regards,
Rao.

View solution in original post

11 REPLIES 11
Highlighted
Community Hero

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

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?



If my answer helped please click on the 'Accept as Solution' button.
Highlighted
Community Hero

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

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

 

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Community Hero

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

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.



If my answer helped please click on the 'Accept as Solution' button.
Highlighted
Frequent Contributor

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

@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
}
Highlighted
Community Hero

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

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

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Community Hero

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

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. 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
Highlighted
Community Hero

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

@msiadak

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
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Community Hero

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

@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()
}


Regards,
Rao.

View solution in original post

Highlighted
Community Hero

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

@nmrao 

 

perfect!

 

Thank you!  I finally found the UpperCase() method - but I couldn't work out how to map it to every instance of the attribute.

 

saved my life again!

 

richie

 

 

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors