Forum Discussion

Apoorva6's avatar
Apoorva6
Frequent Contributor
9 years ago

Need extended help on a earlier solution with groovy

My earlier problem and solution: (Solution was provided from Rao, and thanks to him)

I had to get mockresponse from an external file based on input request and below was the solution,

 

Below was the request:

 

<v2:BillAccountPackageReadRequest>
<v2:BillAccountNodeId></v2:BillAccountNodeId>
<v2:ContractSolutionNumber>SOL1</v2:ContractSolutionNumber>
<v2:HierarchyOnlySolutionNumber></v2:HierarchyOnlySolutionNumber>
</v2:BillAccountPackageReadRequest>

 

Mock response : ${content}

 

Mock response Script:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder(mockRequest.requestContent)
def soNumber = holder.getNodeValue("//*:BillAccountPackageReadRequest/*:ContractSolutionNumber")
def file = new File (groovyUtils.projectPath+"/MOCK/PATH1/${soNumber}.xml")
def fileToLoad = 'soapFault'
if (file.exists()) {
fileToLoad = soNumber
}
context.content = groovy.xml.XmlUtil.serialize(new XmlParser().parse(groovyUtils.projectPath+"/MOCK/PATH1/${fileToLoad}.xml"))

 

if SOL1.xml was present in projectpath/MOCK/PATH1 then content of SOL1.xml was given as mock response. Else soapFault.xml was loaded.

============================

 

NewProblem:

From above request, we can get any of the 3 fields now (Note: only one field can come in request at a time with value and not all )
So if <v2:ContractSolutionNumber>SOL1</v2:ContractSolutionNumber> then load SOL1.xml from PATH1
if <v2:BillAccountNodeId>SOL2</v2:BillAccountNodeId> then load SOL2.xml from PATH2
if <v2:HierarchyOnlySolutionNumber>SOL3</v2:HierarchyOnlySolutionNumber> then load SOL3.xml from PATH3
else SoapFault.xml

5 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    So, have 3 different variables. Read value from 3 different xpaths.
    Then have conditions based on the priority, for eg. what if you have have all 3 values in the received request?
    • Apoorva6's avatar
      Apoorva6
      Frequent Contributor

      Yes, 3 different XPath of same request.

       

      No we never get all 3 values and so we need not worry. We get any one value among 3 each time.

      So we need to check which one is populated, check if its variable.xml is present and then load.

      • Apoorva6's avatar
        Apoorva6
        Frequent Contributor

        I tried for 2 variables, but did not work.

         

        def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
        def groovyUtils1 = new com.eviware.soapui.support.GroovyUtils(context)
        def holder = groovyUtils.getXmlHolder(mockRequest.requestContent)
        def holder1 = groovyUtils1.getXmlHolder(mockRequest.requestContent)
        def soNumber = holder.getNodeValue("//*:BillAccountPackageReadRequest/*:ContractSolutionNumber")
        def hsonum = holder1.getNodeValue("//*:BillAccountPackageReadRequest/*:HierarchyOnlySolutionNumber")
        def file = new File (groovyUtils.projectPath+"/MOCK/SOL/${soNumber}.xml")
        def hsolfile = new File (groovyUtils1.projectPath+"/MOCK/HSOL/${hsonum}.xml")
        def fileToLoad = 'soapFault'
        if (file.exists()) {
        fileToLoad = soNumber
        context.content = groovy.xml.XmlUtil.serialize(new XmlParser().parse(groovyUtils.projectPath+"/MOCK/SOL/${fileToLoad}.xml"))
        }

        else if (hsolfile.exists()) {
        fileToLoad = hsonum
        context.content = groovy.xml.XmlUtil.serialize(new XmlParser().parse(groovyUtils1.projectPath+"/MOCK/HSOL/${hsonum}.xml"))
        }