I am calling a web service that uses a data file to set some of the REST parameters. The issue I am having is when I am looping through the data file, it is concatenating the previous parameters used with the new ones I am needing. I cannot figure out how to clear these out to stop the concatenation.
Here is my code with some comments for what I am attempting
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.testdata.InternalData
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.TestObjectProperty
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import internal.GlobalVariable
RequestObject request = findTestObject('Object Repository/Web Service Requests/Rate Engine Web Service Request')
// InternalData testData = findTestData("Rate Testing Data")
InternalData testData = findTestData("Temp Data")
request.setRestUrl("http://rates." + GlobalVariable.baseUrl + "/rates")
for (def index: (0..testData.getRowNumbers() - 1)) {
// Should be start of new request query parameters
List<TestObjectProperty> parameters = new ArrayList<>()
parameters.add(new TestObjectProperty('fd', ConditionType.EQUALS, GlobalVariable.startDate))
parameters.add(new TestObjectProperty('td', ConditionType.EQUALS, GlobalVariable.endDate))
parameters.add(new TestObjectProperty('tripType', ConditionType.EQUALS, 'hotel'))
parameters.add(new TestObjectProperty('tl', ConditionType.EQUALS, 'CMI'))
parameters.add(new TestObjectProperty('areaId', ConditionType.EQUALS, '36225'))
parameters.add(new TestObjectProperty('hotels', ConditionType.EQUALS, '5809755'))
parameters.add(new TestObjectProperty('service', ConditionType.EQUALS, 'new-rate-engine-results'))
parameters.add(new TestObjectProperty('nc', ConditionType.EQUALS, '1'))
numAdults = 0
totalNumPaidChild = 0
freeChild = 0
// cntr = index + 1
parameters.add(new TestObjectProperty('rm', ConditionType.EQUALS, testData.internallyGetValue("Num Rooms", index)))
numRooms = Integer.parseInt(testData.internallyGetValue("Num Rooms", index))
numChildren = Integer.parseInt(testData.internallyGetValue("Num Children", index))
numAdults = Integer.parseInt(testData.internallyGetValue("Num Adults", index))
// System.out.println('Counter: ' + cntr)
// ap1 = adults in room 1
// ap2 = adults in room 2
// mp1 = children in room 1
// mp2 = children in room 2
// ar1m1 = age of child 1 in room 1
// ar1m2 = age of child 2 in room 1
// ar1m3 = age of child 3 in room 1
// ar2m1 = age of child 1 in room 2
// ar2m2 = age of child 2 in room 2
// ar2m3 = age of child 3 in room 3
// Set parameters for each room
switch (numRooms) {
case 1:
parameters.add(new TestObjectProperty('ap1', ConditionType.EQUALS, testData.internallyGetValue("Num Adults", index)))
if (numChildren > 0) parameters.add(new TestObjectProperty('mp1', ConditionType.EQUALS, testData.internallyGetValue("Num Children", index)))
// Set parameters for each child
switch (numChildren) {
case 0:
// No children so don't add parameter
break
case 1:
// Is child set to free?
def freeChildPresent = testData.internallyGetValue("Child 1 Free", index)
parameters.add(new TestObjectProperty('ar1m1', ConditionType.EQUALS, testData.internallyGetValue("Child Age 1", index)))
if (freeChildPresent == "Yes") freeChild += 1
totalNumPaidChild = numChildren - freeChild
break
default:
// Loop through children to get ages and free status and set appropriate counter
childCntr = 1
for (def chldIndx:(0..numChildren)) {
def freeChildPresent = testData.internallyGetValue("Child " + childCntr + " Free", index)
if (testData.internallyGetValue("Child Age " + childCntr, index) != null ||
!testData.internallyGetValue("Child Age " + childCntr, index).isEmpty() ||
testData.internallyGetValue("Child Age " + childCntr, index) != '') {
parameters.add(new TestObjectProperty('ar1m' + childCntr, ConditionType.EQUALS, testData.internallyGetValue("Child Age " + childCntr, index)))
}
if (freeChildPresent == "Yes") freeChild += 1
totalNumPaidChild = numChildren - freeChild
childCntr += 1
}
break
}
break
default:
parameters.add(new TestObjectProperty('ap' + cntr, ConditionType.EQUALS, testData.internallyGetValue("Num Adults", index)))
parameters.add(new TestObjectProperty('mp' + cntr, ConditionType.EQUALS, testData.internallyGetValue("Num Children", index)))
switch (numChildren) {
case 0:
break
case 1:
break
default:
break
}
break
}
System.out.println("Num Free children: " + freeChild)
System.out.println("Num Total Children Charged: " + totalNumPaidChild)
System.out.println("Num Adults: " + numAdults)
request.setRestParameters(parameters)
for (param in request.getRestParameters()) {
System.out.println(param.getName() + ": " + param.getValue())
}
System.out.println(request.getRestUrl())
// Last iteration looks like this
// http://rates.bookitdev.com/rates?fd=2019-07-05&td=2019-07-15&tripType=hotel&tl=CMI&areaId=36225&hotels=5809755&service=new-rate-engine-results&nc=1&rm=1&ap1=1&fd=2019-07-05&td=2019-07-15&tripType=hotel&tl=CMI&areaId=36225&hotels=5809755&service=new-rate-engine-results&nc=1&rm=1&ap1=1&mp1=1&ar1m1=2&fd=2019-07-05&td=2019-07-15&tripType=hotel&tl=CMI&areaId=36225&hotels=5809755&service=new-rate-engine-results&nc=1&rm=1&ap1=1&mp1=2&ar1m1=2&ar1m2=10&ar1m3=
// should look like the following (every new request should start with a new fd=...)
// http://rates.bookitdev.com/rates?fd=2019-07-05&td=2019-07-15&tripType=hotel&tl=CMI&areaId=36225&hotels=5809755&service=new-rate-engine-results&nc=1&rm=1&ap1=1&mp1=2&ar1m1=2&ar1m2=10&ar1m3=
responseData = WS.sendRequest(request)
// Attempt to clear REST parameters to be ready for nex loop iteration
parameters.clear()
System.out.println(parameters)
chkValue = sellRate * (numAdults + totalNumPaidChild)
System.out.println("ChkValue: " + chkValue)
respBody = responseData.getResponseBodyContent()
System.out.println(respBody)
WS.verifyElementPropertyValue(responseData, 'content[0].rooms[0].sell', chkValue)
}