cancel
Showing results for 
Search instead for 
Did you mean: 

Assert every instance of a json attribute Is UNIQUE

SOLVED
Highlighted
Community Hero

Re: Assert every instance of a json attribute Is UNIQUE

Hi,

@nmrao.....i feel like i'm letting you down here....i'm following what you're saying but my tiny brain isn't able to infer what i need to even get started!

@msiadak....that's brilliant fella....gives me almost exactly what i need....the groovy populates my properties step with each distinct attribute value along with the count....fantastic....i'd never come close to doing that script on my own!

 
I tweaked it to the following:
 // Set up a property step to contain our property names and counts.
 def propertiesStep = context.testCase.testSteps["NameCount"]


 def response = context.expand( '${REST Request#Response}' ) 
def json = new groovy.json.JsonSlurper().parseText(response)

 
 // get the list of CategoryofGrapevineProductId_Name values on current page of values

 def CategoryofGrapevineProductId_Name = json.data.CategoryofGrapevineProductId_Name;
 
 // Loop through the idNames
 for (idName in CategoryofGrapevineProductId_Name)
 {
 
	// get the property value from the properties step above.
	def propertyValue = propertiesStep.getPropertyValue(idName);

log.info(propertyValue)

	
	// if the property has a value, increment it by 1
	if (propertyValue != null)
	{
		propertiesStep.setPropertyValue(idName, (propertyValue.toInteger() + 1).toString());
	}
	// if the property does not exist, assign it a count of 1

	else
	{
		propertiesStep.setPropertyValue(idName, "1");
	}
 }

//assert propertyValue == 1 //<I'm an idiot - this was never going to work!


The photo below is an  image of my Properties file - if all the values for the repeating attribute are unique - I would expect each Property to have a 'Value' of 1 - but you can see there is one entry with a value of '3' - so this has caught the issue I needed to identify.

Capture.PNG
 
 
However....i need to create an assertion that each propertyValue=1 in the properties step to prove the values are unique. I can see the values are unique cos each propertyValue =1 in the properties step. However i need to create an assertion to prove that to automate the test.....a visual check is no check at all!
 
From @nmrao's previous help, i've used the every and it methods to verify a repeating json attribute in my response has a certain value

E.g.
assert json.data.name.every{certainvalue == it}
Where repeating json attribute 'name' jsonpath = [data][name]


So i was wondering if i could use something like the above to prove the propertyValue==1 in my properties step....but i couldn't get it working.

My rest request's response includes a count attribute (JSONPATH =  [header][count]) which = number of records in returned dataset and a total attribute (JSONPATH = [header][total]) = total number of records in db table.
 
With the above in my mind i'm wondering if i could prove the uniqueness of those idName values in my properties step by adding up the number of the propertyValues=1 and asserting that number == [header][count] attribute value in my response?

I'm sorry i'm asking for more help when you guys have given so much already.
I've googled asserting on values in a properties step and I haven't found ANYTHING - everything points back to asserting on the response.
 
Thanks guys - I hope I've been clear in my request this time!
 
And again - thanks for all the help you've given me so far!
 
cheers,
 
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: Assert every instance of a json attribute Is UNIQUE

@richie ,

 

There is no need to create properties step.

 

Just verify with in Groovy script itself.

 

def response = context.expand( '${REST Request#Response}' ) 
def json = new groovy.json.JsonSlurper().parseText(response)
def totalNamesCount = json.data.TransporterId_Name.size()
//Below removes the duplicates and does the count
def uniqueNamesCount = json.data.TransporterId_Name.unique().size()
// If you want find which name and number of times it occurs, uncomment below block of code
/*
def list = json.data.TransporterId_Name.unique()
list.each { element ->
	log.info "$element : ${json.data.TransporterId_Name.findAll { it == element}.size()}"
}
*/
//Since you wanted all the names are unique, both the counts should be same
assert totalNamesCount == uniqueNamesCount, 'There are duplicate Transporter Names' 

Please see the above if you want to check the same?



Regards,
Rao.

View solution in original post

Highlighted
Community Hero

Re: Assert every instance of a json attribute Is UNIQUE

@richie: If you want to further prove that the properties have a count of one, you can do this:

 

def propertiesStep = context.testCase.testSteps["NameCount"];

for (propertyName in propertiesStep.getPropertyNames())
{
     def value = propertiesStep.getPropertyValue(propertyName).toInteger();
     if (value > 1)
     {
          log.info("$propertyName appears $value times.")
     }
}



---

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

Re: Assert every instance of a json attribute Is UNIQUE

@msiadak @nmrao 

 

Kudos to both - thanks so much fellas - you really saved my life there!

 

 

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: Assert every instance of a json attribute Is UNIQUE

Always happy to help out!




---

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

Re: Assert every instance of a json attribute Is UNIQUE

@richie,
How did you end up the problem, finally? Solved?

The above sample code which was provided initially and the code right above are just same (only difference is instead of fixed list, extracted list from the response).


Regards,
Rao.
Highlighted
Community Hero

Re: Assert every instance of a json attribute Is UNIQUE

Hey @nmrao 

 

yeah - all sorted fella.

 

Now I've seen your code I can follow it and I can see what you meant - but I was never getting there on my own fella - I'm still at the level of generating "hello world" 🙂

 

I chucked you and @msiadak about 3 Kudos points each for bailing my behind out 🙂

 

Again - thanks man - always appreciate all your help!

 

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: Assert every instance of a json attribute Is UNIQUE

@rihie,
Ok. I know you would mark the solution if worked. That's why I was asking.


Regards,
Rao.
Highlighted
Community Hero

Re: Assert every instance of a json attribute Is UNIQUE

Can this be solved as marked solved based on the responses?


Regards,
Rao.
Highlighted
Moderator

Re: Assert every instance of a json attribute Is UNIQUE

What a great discussion!

@nmrao , thank you for keeping an eye on this topic!

We have marked several replies as solutions to it. If @richie  posts his final solution later on, we will use it as the main one here.

 


Olga Terentieva
SmartBear Assistant Community Manager

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors