cancel
Showing results for 
Search instead for 
Did you mean: 

Sub : how to do regular expression assertions for variable elements in an array

SOLVED
New Contributor

Sub : how to do regular expression assertions for variable elements in an array

 

Description : Trying to write an assertion for fields code and unit.
code can be an integer or empty
and unit can be a string or empty.
i.e code and unit has no fixed values.
And have to assert based on assertion using regex.
the assertion have to match all the object properties in the response array.

 

 

[
{
"code": "0",
"unit": "meter"
},
{
"code": "1",
"unit": "meter"
},
{
"code": "2",
"unit": "meter"
},
{
"code": "",
"unit": ""
}
]

1 ACCEPTED SOLUTION

Accepted Solutions
Community Leader

Re: Sub : how to do regular expression assertions for variable elements in an array

Hi,

 

Add a script assertion to the test step of interest.

 

The below will get you started, but be warned, it won't work first time.  You will need to tailor this to your needs.

 

import groovy.json.JsonSlurper
import groovy.json.*
import java.util.regex.*

// User Json Slurper to get the JSON from the response.
def jsonSlurper = new JsonSlurper().parseText(messageExchange.response.responseContent)

// Just log how many items to check.  If you get an error here, replace message with 
// root node of interest in the response.
log.info("Checking ${jsonSlurper.message.size()} items in response.");

// Let's iterate over the array of items...
jsonSlurper.message.each{

	// Have a look at the current items
	log.info("Item. Code ${it.code}.  Unit ${it.unit}.);
	
	// Check it.code for numeric only.
	if (it.code.matches("[0-9]+") && it.code.length() > 0) {
		log.info("Code ${it.code} passes");
	} else {
		log.info("Code ${it.code} fails")
	}

	// Check it.unit for lowercase chars only.
	if (it.unit.matches("[a-z]+") && it.unit.length() > 0) {
		log.info("Code ${it.unit} passes");
	} else {
		log.info("Code ${it.unit} fails")
	}
	// You decide how to report failure.  E.g. dump to a file
	// or assert.
	
}

 

View solution in original post

5 REPLIES 5
Community Hero

Re: Sub : how to do regular expression assertions for variable elements in an array

Hey @jaya_kiran,

I can do this, but only in 2 separate assertions, not 1 assertion.
One of the real groovy scripters on the forum (@HimanshuTayal, @nmrao, @ChrisA) might be better placed to answer. I'll check tomorrow to see yours and their responses and go from there.

Cheers

Rich
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 Contributor

Re: Sub : how to do regular expression assertions for variable elements in an array

sure, 2 assertions are fine for me. 

New Contributor

Re: Sub : how to do regular expression assertions for variable elements in an array

need solution thanks.

Community Leader

Re: Sub : how to do regular expression assertions for variable elements in an array

Hi,

 

Add a script assertion to the test step of interest.

 

The below will get you started, but be warned, it won't work first time.  You will need to tailor this to your needs.

 

import groovy.json.JsonSlurper
import groovy.json.*
import java.util.regex.*

// User Json Slurper to get the JSON from the response.
def jsonSlurper = new JsonSlurper().parseText(messageExchange.response.responseContent)

// Just log how many items to check.  If you get an error here, replace message with 
// root node of interest in the response.
log.info("Checking ${jsonSlurper.message.size()} items in response.");

// Let's iterate over the array of items...
jsonSlurper.message.each{

	// Have a look at the current items
	log.info("Item. Code ${it.code}.  Unit ${it.unit}.);
	
	// Check it.code for numeric only.
	if (it.code.matches("[0-9]+") && it.code.length() > 0) {
		log.info("Code ${it.code} passes");
	} else {
		log.info("Code ${it.code} fails")
	}

	// Check it.unit for lowercase chars only.
	if (it.unit.matches("[a-z]+") && it.unit.length() > 0) {
		log.info("Code ${it.unit} passes");
	} else {
		log.info("Code ${it.unit} fails")
	}
	// You decide how to report failure.  E.g. dump to a file
	// or assert.
	
}

 

View solution in original post

New Contributor

Sub : how to do regular expression assertions for variable elements in an array

thank you for the response.

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