Forum Discussion

CCHATTERTON's avatar
CCHATTERTON
Occasional Contributor
12 years ago

Event Handler to Remove/Add Elements if Null in Datasource

Hi,

I am looping through a CSV datasource and passing values into a request on each loop. Each row in the CSV file contains a main driver, and up to 4 optional additional drivers. The below is an example of how the main and additional drivers section of the request looks.

<mainDriver>
<name>Main Driver</name>
<ageyears>35</ageyears>
<agemonths>4</agemonths>
</mainDriver>
<additionalDrivers>
<driver>
<name>John Smith</name>
<ageyears>30</ageyears>
<agemonths>5</agemonths>
</driver>
<driver>
<name>Bill Smith</name>
<ageyears>25</ageyears>
<agemonths>3</agemonths>
</driver>
<driver>
<name>Will Smith</name>
<ageyears>28</ageyears>
<agemonths>1</agemonths>
</driver>
</additionalDrivers>

If there are no additional drivers, then the entire <additionalDrivers> section must not be in the request, otherwise it fails validation against the schema.

I need to write an event handler to remove the entire additional drivers section from the request at the start of each loop, and then add in the additional driver section if there are any additional drivers in the CSV file, plus the <driver> tag for each driver. I am aware that it needs to be written in groovy script but I have no experience in using this and am on a tight deadline. Can anyone point me in the right direction or provide the syntax to remove or add a specific element via groovy?

Regards

4 Replies

  • CCHATTERTON's avatar
    CCHATTERTON
    Occasional Contributor
    Hi,

    I actually worked out my own solution to this issue, perhaps a little long winded but it works..

    I inserted a groovy script test step between the datasource and request steps. I then wrote a script to grab the relevant property values from the csv datasource and store as local variables, with if statements to determine if there is data in the csv file or not. If there is then the xml was put together for that entire section of the request by combining the local variables with the XML brackets and element names as strings. These strings were then passed to global properties which were called in the xml request. If there were no values in the csv then nothing was passed to the global property (each global property is set to null at the start of the script) This way if there was nothing in the global variable for a particular section then that section would not be included in the request.

    Script below for any other noobs who may be struggling to understand this as much as I was!!

    def nd1_motoring_org = context.expand( '${PC Risks#NamedDriverRecord01_MotorOrganisationCode}' )
    def nd1_rel_to_prop = context.expand( '${PC Risks#NamedDriverRecord01_RelationToProposer}' )
    def nd1_full_time_occ = context.expand( '${PC Risks#NamedDriverRecord01_MainOccupationRecord_ABIOccupationCode}' )
    def nd1_full_time_emp = context.expand( '${PC Risks#NamedDriverRecord01_MainOccupationRecord_ABIEmployersCode}' )
    def nd1_full_time_status = context.expand( '${PC Risks#NamedDriverRecord01_MainOccupationRecord_EmploymentStatus}' )
    def nd1_part_time_occ = context.expand( '${PC Risks#NamedDriverRecord01_OtherOccupationRecord01_ABIEmployersCode}' )
    def nd1_part_time_emp = context.expand( '${PC Risks#NamedDriverRecord01_OtherOccupationRecord01_ABIOccupationCode}' )
    def nd1_part_time_status = context.expand( '${PC Risks#NamedDriverRecord01_OtherOccupationRecord01_EmploymentStatus}' )
    def nd1_licence_type = context.expand( '${PC Risks#NamedDriverRecord01_LicenceType}' )
    def nd1_licence_date = context.expand( '${PC Risks#NamedDriverRecord01_LicenceDate}' )
    def nd1_licence_cat = "CatB"
    def nd1_title = "Mr"
    def nd1_forename = "Dan QAT Rel45"
    def nd1_surname = "Dan QAT Rel45"
    def nd1_salutation = "Dan QAT Rel45"
    def nd1_dob = context.expand( '${PC Risks#NamedDriverRecord01_DOB}' )
    def nd1_gender = context.expand( '${PC Risks#NamedDriverRecord01_Gender}' )
    def nd1_residence = context.expand( '${PC Risks#NamedDriverRecord01_UKResidenceDate}' )
    def nd1_refused = context.expand( '${PC Risks#NamedDriverRecord01_RefusedInsurance}' )
    def nd1_marital_status = context.expand( '${PC Risks#NamedDriverRecord01_MaritalStatus}' )
    def nd1_nonrta = context.expand( '${PC Risks#NamedDriverRecord01_nonRTAConvictions}' )
    def nd1_smoker = context.expand( '${PC Risks#NamedDriverRecord01_Smoker}' )
    def nd1_access_other = context.expand( '${PC Risks#NamedDriverRecord01_AccessToOtherVehicles}' )
    def nd1_tests = context.expand( '${PC Risks#NamedDriverRecord01_NumberOfTests}' )
    def nd1_veh_use = context.expand( '${PC Risks#NamedDriverRecord01_UseOfVehicle}' )
    def nd1_driver_type = "NmdDrvr"
    def nd1_star_rider = "No"
    def nd1_iamcert = "false"

    def nd2_motoring_org = context.expand( '${PC Risks#NamedDriverRecord02_MotorOrganisationCode}' )
    def nd2_rel_to_prop = context.expand( '${PC Risks#NamedDriverRecord02_RelationToProposer}' )
    def nd2_full_time_occ = context.expand( '${PC Risks#NamedDriverRecord02_MainOccupationRecord_ABIOccupationCode}' )
    def nd2_full_time_emp = context.expand( '${PC Risks#NamedDriverRecord02_MainOccupationRecord_ABIEmployersCode}' )
    def nd2_full_time_status = context.expand( '${PC Risks#NamedDriverRecord02_MainOccupationRecord_EmploymentStatus}' )
    def nd2_part_time_occ = context.expand( '${PC Risks#NamedDriverRecord02_OtherOccupationRecord01_ABIEmployersCode}' )
    def nd2_part_time_emp = context.expand( '${PC Risks#NamedDriverRecord02_OtherOccupationRecord01_ABIOccupationCode}' )
    def nd2_part_time_status = context.expand( '${PC Risks#NamedDriverRecord02_OtherOccupationRecord01_EmploymentStatus}' )
    def nd2_licence_type = context.expand( '${PC Risks#NamedDriverRecord02_LicenceType}' )
    def nd2_licence_date = context.expand( '${PC Risks#NamedDriverRecord02_LicenceDate}' )
    def nd2_licence_cat = "CatB"
    def nd2_title = "Mr"
    def nd2_forename = "Dan QAT Rel45"
    def nd2_surname = "Dan QAT Rel45"
    def nd2_salutation = "Dan QAT Rel45"
    def nd2_dob = context.expand( '${PC Risks#NamedDriverRecord02_DOB}' )
    def nd2_gender = context.expand( '${PC Risks#NamedDriverRecord02_Gender}' )
    def nd2_residence = context.expand( '${PC Risks#NamedDriverRecord02_UKResidenceDate}' )
    def nd2_refused = context.expand( '${PC Risks#NamedDriverRecord02_RefusedInsurance}' )
    def nd2_marital_status = context.expand( '${PC Risks#NamedDriverRecord02_MaritalStatus}' )
    def nd2_nonrta = context.expand( '${PC Risks#NamedDriverRecord02_nonRTAConvictions}' )
    def nd2_smoker = context.expand( '${PC Risks#NamedDriverRecord02_Smoker}' )
    def nd2_access_other = context.expand( '${PC Risks#NamedDriverRecord02_AccessToOtherVehicles}' )
    def nd2_tests = context.expand( '${PC Risks#NamedDriverRecord02_NumberOfTests}' )
    def nd2_veh_use = context.expand( '${PC Risks#NamedDriverRecord02_UseOfVehicle}' )
    def nd2_driver_type = "NmdDrvr"
    def nd2_star_rider = "No"
    def nd2_iamcert = "false"

    def nd3_motoring_org = context.expand( '${PC Risks#NamedDriverRecord03_MotorOrganisationCode}' )
    def nd3_rel_to_prop = context.expand( '${PC Risks#NamedDriverRecord03_RelationToProposer}' )
    def nd3_full_time_occ = context.expand( '${PC Risks#NamedDriverRecord03_MainOccupationRecord_ABIOccupationCode}' )
    def nd3_full_time_emp = context.expand( '${PC Risks#NamedDriverRecord03_MainOccupationRecord_ABIEmployersCode}' )
    def nd3_full_time_status = context.expand( '${PC Risks#NamedDriverRecord03_MainOccupationRecord_EmploymentStatus}' )
    def nd3_part_time_occ = context.expand( '${PC Risks#NamedDriverRecord03_OtherOccupationRecord01_ABIEmployersCode}' )
    def nd3_part_time_emp = context.expand( '${PC Risks#NamedDriverRecord03_OtherOccupationRecord01_ABIOccupationCode}' )
    def nd3_part_time_status = context.expand( '${PC Risks#NamedDriverRecord03_OtherOccupationRecord01_EmploymentStatus}' )
    def nd3_licence_type = context.expand( '${PC Risks#NamedDriverRecord03_LicenceType}' )
    def nd3_licence_date = context.expand( '${PC Risks#NamedDriverRecord03_LicenceDate}' )
    def nd3_licence_cat = "CatB"
    def nd3_title = "Mr"
    def nd3_forename = "Dan QAT Rel45"
    def nd3_surname = "Dan QAT Rel45"
    def nd3_salutation = "Dan QAT Rel45"
    def nd3_dob = context.expand( '${PC Risks#NamedDriverRecord03_DOB}' )
    def nd3_gender = context.expand( '${PC Risks#NamedDriverRecord03_Gender}' )
    def nd3_residence = context.expand( '${PC Risks#NamedDriverRecord03_UKResidenceDate}' )
    def nd3_refused = context.expand( '${PC Risks#NamedDriverRecord03_RefusedInsurance}' )
    def nd3_marital_status = context.expand( '${PC Risks#NamedDriverRecord03_MaritalStatus}' )
    def nd3_nonrta = context.expand( '${PC Risks#NamedDriverRecord03_nonRTAConvictions}' )
    def nd3_smoker = context.expand( '${PC Risks#NamedDriverRecord03_Smoker}' )
    def nd3_access_other = context.expand( '${PC Risks#NamedDriverRecord03_AccessToOtherVehicles}' )
    def nd3_tests = context.expand( '${PC Risks#NamedDriverRecord03_NumberOfTests}' )
    def nd3_veh_use = context.expand( '${PC Risks#NamedDriverRecord03_UseOfVehicle}' )
    def nd3_driver_type = "NmdDrvr"
    def nd3_star_rider = "No"
    def nd3_iamcert = "false"

    def nd4_motoring_org = context.expand( '${PC Risks#NamedDriverRecord04_MotorOrganisationCode}' )
    def nd4_rel_to_prop = context.expand( '${PC Risks#NamedDriverRecord04_RelationToProposer}' )
    def nd4_full_time_occ = context.expand( '${PC Risks#NamedDriverRecord04_MainOccupationRecord_ABIOccupationCode}' )
    def nd4_full_time_emp = context.expand( '${PC Risks#NamedDriverRecord04_MainOccupationRecord_ABIEmployersCode}' )
    def nd4_full_time_status = context.expand( '${PC Risks#NamedDriverRecord04_MainOccupationRecord_EmploymentStatus}' )
    def nd4_part_time_occ = context.expand( '${PC Risks#NamedDriverRecord04_OtherOccupationRecord01_ABIEmployersCode}' )
    def nd4_part_time_emp = context.expand( '${PC Risks#NamedDriverRecord04_OtherOccupationRecord01_ABIOccupationCode}' )
    def nd4_part_time_status = context.expand( '${PC Risks#NamedDriverRecord04_OtherOccupationRecord01_EmploymentStatus}' )
    def nd4_licence_type = context.expand( '${PC Risks#NamedDriverRecord04_LicenceType}' )
    def nd4_licence_date = context.expand( '${PC Risks#NamedDriverRecord04_LicenceDate}' )
    def nd4_licence_cat = "CatB"
    def nd4_title = "Mr"
    def nd4_forename = "Dan QAT Rel45"
    def nd4_surname = "Dan QAT Rel45"
    def nd4_salutation = "Dan QAT Rel45"
    def nd4_dob = context.expand( '${PC Risks#NamedDriverRecord04_DOB}' )
    def nd4_gender = context.expand( '${PC Risks#NamedDriverRecord04_Gender}' )
    def nd4_residence = context.expand( '${PC Risks#NamedDriverRecord04_UKResidenceDate}' )
    def nd4_refused = context.expand( '${PC Risks#NamedDriverRecord04_RefusedInsurance}' )
    def nd4_marital_status = context.expand( '${PC Risks#NamedDriverRecord04_MaritalStatus}' )
    def nd4_nonrta = context.expand( '${PC Risks#NamedDriverRecord04_nonRTAConvictions}' )
    def nd4_smoker = context.expand( '${PC Risks#NamedDriverRecord04_Smoker}' )
    def nd4_access_other = context.expand( '${PC Risks#NamedDriverRecord04_AccessToOtherVehicles}' )
    def nd4_tests = context.expand( '${PC Risks#NamedDriverRecord04_NumberOfTests}' )
    def nd4_veh_use = context.expand( '${PC Risks#NamedDriverRecord04_UseOfVehicle}' )
    def nd4_driver_type = "NmdDrvr"
    def nd4_star_rider = "No"
    def nd4_iamcert = "false"

    com.eviware.soapui.SoapUI.globalProperties['namedDrivers'].value = ""
    com.eviware.soapui.SoapUI.globalProperties['namedDriver1'].value = ""
    com.eviware.soapui.SoapUI.globalProperties['namedDriver2'].value = ""
    com.eviware.soapui.SoapUI.globalProperties['namedDriver3'].value = ""
    com.eviware.soapui.SoapUI.globalProperties['namedDriver4'].value = ""


    if ( nd1_dob != "" )
    {
    com.eviware.soapui.SoapUI.globalProperties['namedDriver1'].value = '<additionalInsured xsi:type="ns23:Person" xmlns:ns23="http://cdl.co.uk/model/party"><motoringOrg>' + nd1_motoring_org + '</motoringOrg><relationshipToProposer>' + nd1_rel_to_prop + '</relationshipToProposer><fullTimeOccupation><type>' + nd1_full_time_status + '</type><occCode>' + nd1_full_time_occ + '</occCode><empCode>' + nd1_full_time_emp + '</empCode></fullTimeOccupation><partTimeOccupation><type>' + nd1_part_time_status + '</type><occCode>' + nd1_part_time_occ + '</occCode><empCode>' + nd1_part_time_emp + '</empCode></partTimeOccupation><licence><type>' + nd1_licence_type + '</type><date>' + nd1_licence_date + '</date><category>' + nd1_licence_cat + '</category>' + com.eviware.soapui.SoapUI.globalProperties['namedDriver1_Convictions'].value + '</licence><name><title>' + nd1_title + '</title><forenames>' + nd1_forename + '</forenames><surname>' + nd1_surname + '</surname><salutation>' + nd1_salutation + '</salutation></name><dateOfBirth>' + nd1_dob + '</dateOfBirth><gender>' + nd1_gender + '</gender><residentSince>' + nd1_residence + '</residentSince><insuranceRefused>' + nd1_refused + '</insuranceRefused><maritalStatus>' + nd1_marital_status + '</maritalStatus><nonRTAConviction>' + nd1_nonrta + '</nonRTAConviction><smoker>' + nd1_smoker + '</smoker><accessToOtherVehicles>' + nd1_access_other + '</accessToOtherVehicles><tests>' + nd1_tests + '</tests><vehicleUse>' + nd1_veh_use + '</vehicleUse><driverType>' + nd1_driver_type + '</driverType><starRider>' + nd1_star_rider + '</starRider><iamCert>' + nd1_iamcert + '</iamCert>' + com.eviware.soapui.SoapUI.globalProperties['namedDriver1_Claims'].value + '</additionalInsured>'
    }


    if ( nd2_dob != "" )
    {
    com.eviware.soapui.SoapUI.globalProperties['namedDriver2'].value = '<additionalInsured xsi:type="ns23:Person" xmlns:ns23="http://cdl.co.uk/model/party"><motoringOrg>' + nd2_motoring_org + '</motoringOrg><relationshipToProposer>' + nd2_rel_to_prop + '</relationshipToProposer><fullTimeOccupation><type>' + nd2_full_time_status + '</type><occCode>' + nd2_full_time_occ + '</occCode><empCode>' + nd2_full_time_emp + '</empCode></fullTimeOccupation><partTimeOccupation><type>' + nd2_part_time_status + '</type><occCode>' + nd2_part_time_occ + '</occCode><empCode>' + nd2_part_time_emp + '</empCode></partTimeOccupation><licence><type>' + nd2_licence_type + '</type><date>' + nd2_licence_date + '</date><category>' + nd2_licence_cat + '</category>' + com.eviware.soapui.SoapUI.globalProperties['namedDriver2_Convictions'].value + '</licence><name><title>' + nd2_title + '</title><forenames>' + nd2_forename + '</forenames><surname>' + nd2_surname + '</surname><salutation>' + nd2_salutation + '</salutation></name><dateOfBirth>' + nd2_dob + '</dateOfBirth><gender>' + nd2_gender + '</gender><residentSince>' + nd2_residence + '</residentSince><insuranceRefused>' + nd2_refused + '</insuranceRefused><maritalStatus>' + nd2_marital_status + '</maritalStatus><nonRTAConviction>' + nd2_nonrta + '</nonRTAConviction><smoker>' + nd2_smoker + '</smoker><accessToOtherVehicles>' + nd2_access_other + '</accessToOtherVehicles><tests>' + nd2_tests + '</tests><vehicleUse>' + nd2_veh_use + '</vehicleUse><driverType>' + nd2_driver_type + '</driverType><starRider>' + nd2_star_rider + '</starRider><iamCert>' + nd2_iamcert + '</iamCert>' + com.eviware.soapui.SoapUI.globalProperties['namedDriver2_Claims'].value + '</additionalInsured>'
    }


    if ( nd3_dob != "" )
    {
    com.eviware.soapui.SoapUI.globalProperties['namedDriver3'].value = '<additionalInsured xsi:type="ns23:Person" xmlns:ns23="http://cdl.co.uk/model/party"><motoringOrg>' + nd3_motoring_org + '</motoringOrg><relationshipToProposer>' + nd3_rel_to_prop + '</relationshipToProposer><fullTimeOccupation><type>' + nd3_full_time_status + '</type><occCode>' + nd3_full_time_occ + '</occCode><empCode>' + nd3_full_time_emp + '</empCode></fullTimeOccupation><partTimeOccupation><type>' + nd3_part_time_status + '</type><occCode>' + nd3_part_time_occ + '</occCode><empCode>' + nd3_part_time_emp + '</empCode></partTimeOccupation><licence><type>' + nd3_licence_type + '</type><date>' + nd3_licence_date + '</date><category>' + nd3_licence_cat + '</category>' + com.eviware.soapui.SoapUI.globalProperties['namedDriver3_Convictions'].value + '</licence><name><title>' + nd3_title + '</title><forenames>' + nd3_forename + '</forenames><surname>' + nd3_surname + '</surname><salutation>' + nd3_salutation + '</salutation></name><dateOfBirth>' + nd3_dob + '</dateOfBirth><gender>' + nd3_gender + '</gender><residentSince>' + nd3_residence + '</residentSince><insuranceRefused>' + nd3_refused + '</insuranceRefused><maritalStatus>' + nd3_marital_status + '</maritalStatus><nonRTAConviction>' + nd3_nonrta + '</nonRTAConviction><smoker>' + nd3_smoker + '</smoker><accessToOtherVehicles>' + nd3_access_other + '</accessToOtherVehicles><tests>' + nd3_tests + '</tests><vehicleUse>' + nd3_veh_use + '</vehicleUse><driverType>' + nd3_driver_type + '</driverType><starRider>' + nd3_star_rider + '</starRider><iamCert>' + nd3_iamcert + '</iamCert>' + com.eviware.soapui.SoapUI.globalProperties['namedDriver3_Claims'].value + '</additionalInsured>'
    }


    if ( nd4_dob != "" )
    {
    com.eviware.soapui.SoapUI.globalProperties['namedDriver4'].value = '<additionalInsured xsi:type="ns23:Person" xmlns:ns23="http://cdl.co.uk/model/party"><motoringOrg>' + nd4_motoring_org + '</motoringOrg><relationshipToProposer>' + nd4_rel_to_prop + '</relationshipToProposer><fullTimeOccupation><type>' + nd4_full_time_status + '</type><occCode>' + nd4_full_time_occ + '</occCode><empCode>' + nd4_full_time_emp + '</empCode></fullTimeOccupation><partTimeOccupation><type>' + nd4_part_time_status + '</type><occCode>' + nd4_part_time_occ + '</occCode><empCode>' + nd4_part_time_emp + '</empCode></partTimeOccupation><licence><type>' + nd4_licence_type + '</type><date>' + nd4_licence_date + '</date><category>' + nd4_licence_cat + '</category>' + com.eviware.soapui.SoapUI.globalProperties['namedDriver4_Convictions'].value + '</licence><name><title>' + nd4_title + '</title><forenames>' + nd4_forename + '</forenames><surname>' + nd4_surname + '</surname><salutation>' + nd4_salutation + '</salutation></name><dateOfBirth>' + nd4_dob + '</dateOfBirth><gender>' + nd4_gender + '</gender><residentSince>' + nd4_residence + '</residentSince><insuranceRefused>' + nd4_refused + '</insuranceRefused><maritalStatus>' + nd4_marital_status + '</maritalStatus><nonRTAConviction>' + nd4_nonrta + '</nonRTAConviction><smoker>' + nd4_smoker + '</smoker><accessToOtherVehicles>' + nd4_access_other + '</accessToOtherVehicles><tests>' + nd4_tests + '</tests><vehicleUse>' + nd4_veh_use + '</vehicleUse><driverType>' + nd4_driver_type + '</driverType><starRider>' + nd4_star_rider + '</starRider><iamCert>' + nd4_iamcert + '</iamCert>' + com.eviware.soapui.SoapUI.globalProperties['namedDriver4_Claims'].value + '</additionalInsured>'
    }

    if ( com.eviware.soapui.SoapUI.globalProperties['namedDriver1'].value == "" & com.eviware.soapui.SoapUI.globalProperties['namedDriver2'].value == "" & com.eviware.soapui.SoapUI.globalProperties['namedDriver3'].value == "" & com.eviware.soapui.SoapUI.globalProperties['namedDriver4'].value == "" )
    {
    com.eviware.soapui.SoapUI.globalProperties['namedDrivers'].value = ""
    } else
    com.eviware.soapui.SoapUI.globalProperties['namedDrivers'].value = '<additionalInsuredSet>' + com.eviware.soapui.SoapUI.globalProperties['namedDriver1'].value + com.eviware.soapui.SoapUI.globalProperties['namedDriver2'].value + com.eviware.soapui.SoapUI.globalProperties['namedDriver3'].value + com.eviware.soapui.SoapUI.globalProperties['namedDriver4'].value + '</additionalInsuredSet>'

  • Hi,

    We're glad that you found a solution. Thanks for posting it here!

    Kind regards,
    Manne Fagerlind
    SmartBear