Reputation: 105
I am trying to compare an array of codes with the codes I get from a response in Soapui. I was able to add an array to a groovy script containing the entries I had in an excel so basically e.g
def MAX_SIZE = 31
def myArray = new Object[MAX_SIZE]
// Code array
myArray[0] = "EU1234qSP"
myArray[1] = "ES1234qSPwe"
myArray[2] = "EF1234qSPde"
myArray[3] = "EW1234qSPpt"
Then I have expanded the properties Xpath from the web service response that returns the code from the data base. e.g:
def OfferCode1 = context.expand( '${QuerySubsPlanList - Request 1#Response#declare namespace ns=\'http://com.ztesoft.zsmart/xsd\'; //ns:QuerySubsPlanListResponse[1]/SubsPlanDtoList[1]/SubsPlanDto[1]/SubsPlanCode[1]}' )
def OfferCode2 = context.expand( '${QuerySubsPlanList - Request 1#Response#declare namespace ns=\'http://com.ztesoft.zsmart/xsd\'; //ns:QuerySubsPlanListResponse[1]/SubsPlanDtoList[1]/SubsPlanDto[2]/SubsPlanCode[1]}' )
def OfferCode3 = context.expand( '${QuerySubsPlanList - Request 1#Response#declare namespace ns=\'http://com.ztesoft.zsmart/xsd\'; //ns:QuerySubsPlanListResponse[1]/SubsPlanDtoList[1]/SubsPlanDto[3]/SubsPlanCode[1]}' )
def OfferCode4 = context.expand( '${QuerySubsPlanList - Request 1#Response#declare namespace ns=\'http://com.ztesoft.zsmart/xsd\'; //ns:QuerySubsPlanListResponse[1]/SubsPlanDtoList[1]/SubsPlanDto[4]/SubsPlanCode[1]}' )
What is the best approach to compare them and print in the log that there is a match or fail? A loop? maybe something like
for (item in myArray ) {
for (offerCode in offerCodesArray) {}
if (item == offerCode) {
store the finding
go to next item in myArray
}
}
}
}
how would I enter the expanded properties in to an array then? never did that before still a beginner at this, can anyone help?
Cheers Adi
Update: the Changes in the DB are more frequent than I thought, so I do need the nodes count to work. The structure of the response is as follows
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:QuerySubsPlanListResponse xmlns:ns="http://com.ztesoft.zsmart/xsd">
<SubsPlanDtoList>
<SubsPlanDto>
<SubsPlanCode>TEST1</SubsPlanCode>
<ServType>M</ServType>
<CustTypeList>B2B</CustTypeList>
<Price>0.0000</Price>
</SubsPlanDto>
<SubsPlanDto>
<SubsPlanCode>TEST2 </SubsPlanCode>
<ServType>M</ServType>
<CustTypeList>B2C|B2B</CustTypeList>
<Price>0.0000</Price>
</SubsPlanDto>
can anyone tell me how to count the nodes? the last suggestion above did not work. Thanks
Upvotes: 1
Views: 1812
Reputation: 18507
You can use a java.util.Collection
from groovy-jdk instead of an array, and then you can use intersect
, plus
(for union) etc. to work with this collections an get the match and fails. I give you a source sample:
def myCollection = ["1","2","3","4"]
def myCollection2 = ["1","a","3","d"]
def matches = myCollection.intersect(myCollection2)
def fails = myCollection.plus(myCollection2)
fails.removeAll(matches)
log.info matches // --> [1,3] matches
log.info fails // --> [2,4,a,d] not match
With this code you can print the matches and fails, and also you have a collection with your matches like you want.
About enter expanded properties in a collection: If you want to loop thought <SubsPlanDto>
in your response as your answer suggests, you can count the number of instances of this element with an XPath and then make a loop adding each element to the collection (Please note that you can use a wildcard *
as namespace to keep XPath simplier), see the code below:
def numElements = context.expand( '${QuerySubsPlanList - Request 1#Response#//count(*:QuerySubsPlanListResponse[1]/SubsPlanDtoList[1]/SubsPlanDto)}')
def collection2 = [];
for ( i in 1..numElements.toInteger()) {
collection2.add(context.expand( '${QuerySubsPlanList - Request 1#Response#//*:QuerySubsPlanListResponse[1]/SubsPlanDtoList[1]/SubsPlanDto['+ i + ']/SubsPlanCode[1]}'))
}
Finally you can do it all together:
// you collection definition...
def collection = ["1","2","3","4"]
// get num repetitions of <SubsPlanDto> with Xpath count
def numElements = context.expand( '${QuerySubsPlanList - Request 1#Response#//count(*:QuerySubsPlanListResponse[1]/SubsPlanDtoList[1]/SubsPlanDto)}')
def collection2 = [];
// iterate and add the values to a collection
for ( i in 1..numElements.toInteger()) {
collection2.add(context.expand( '${QuerySubsPlanList - Request 1#Response#//*:QuerySubsPlanListResponse[1]/SubsPlanDtoList[1]/SubsPlanDto['+ i + ']/SubsPlanCode[1]}'))
}
// get the matches and differences
def matches = collection.intersect(collection2)
def fails = collection.plus(collection2)
fails.removeAll(matches)
log.info matches
log.info fails
Hope this helps,
Upvotes: 1