Forum Discussion

Apoorva6's avatar
Apoorva6
Frequent Contributor
8 years ago
Solved

Need help in groovy script to fetch proper response.

I need help in below issue.

I have 2 attributes in request orderComponentId and orderDataType.
orderDataType can take 2 values (TTTN and PORT) and orderComponentId can take any value.
I have 2 path where response is stored in XML. This response name will be same as orderComponentId value

path1= groovyUtils.projectPath+"/MOCK/CSI/IETN/
path2= groovyUtils.projectPath+"/MOCK/CSI/IETN/PORTEDTN/


Now based on orderDataType value i.e TTTN or PORT, I need to fetch response from path1 or path2 and if not present throw a fault message as No data found
If orderDataType is TTTN, then fetch response from path1 and if orderDataType is PORT, fetch response from path2
I am trying with below script and always i get fault message



def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder(mockRequest.requestContent)
def soNumber = holder.getNodeValue("//*:InquireEnterpriseTNOrderDataRequest/*:orderComponentId")
def orderType = holder.getNodeValue("//*:InquireEnterpriseTNOrderDataRequest/*:orderDataType")
def file = new File (groovyUtils.projectPath+"/MOCK/CSI/IETN/${soNumber}.xml")

def file1 = new File (groovyUtils.projectPath+"/MOCK/CSI/IETN/PORTEDTN/${soNumber}.xml")

def fileToLoad = 'soapFault'
if (orderType == "TTTN" and file.exists ) {
        fileToLoad = soNumber
    context.content = groovy.xml.XmlUtil.serialize(new XmlParser().parse(groovyUtils.projectPath+"/MOCK/CSI/IETN/${fileToLoad}.xml"))
}

else if (orderType == "PORT" and file1.exists ) {

fileToLoad = soNumber
context.content = groovy.xml.XmlUtil.serialize(new XmlParser().parse(groovyUtils.projectPath+"/MOCK/CSI/IETN/PORTEDTN/${fileToLoad}.xml"))
}

  • nmrao's avatar
    nmrao
    8 years ago

    Apoorva6,

     

    Can you try below script ? Note that I could not test before providing as I do not have artifacts.

     

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
    def holder = groovyUtils.getXmlHolder(mockRequest.requestContent)
    def soNumber = holder.getNodeValue("//*:InquireEnterpriseTNOrderDataRequest/*:orderComponentId")
    def orderType = holder.getNodeValue("//*:InquireEnterpriseTNOrderDataRequest/*:orderDataType")
    def prefix = "${groovyUtils.projectPath}/MOCK/CSI/IETN/"
    
    def getPrefix = {  orderType == 'PORT' ? ("${prefix}PORTEDTN/" as String) : prefix }
    
    def getFileName = {
      def fileName = "${getPrefix()}${soNumber}.xml" as String
      def file = new File(fileName)
      file.exists() ? fileName : ("${getPrefix()}soapFault.xml" as String)
    }
    
    def getResponseContent = { new File(getFileName()).text }
    
    context.content = getResponseContent()
    log.info "Response content is set to : ${context.content}"

     

     

16 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    You need to provide useful info such as request and context etc.

    The above is not really helpful.
    • Apoorva6's avatar
      Apoorva6
      Frequent Contributor

      Hi Rao,

       

      Thanks for your reply. Below are 2 sample requests

       

      <soapenv:Body>
            <ns10:InquireEnterpriseTNOrderDataRequest>
               <ns10:orderComponentId>1523225</ns10:orderComponentId>
               <ns10:orderComponentIdVersion>2</ns10:orderComponentIdVersion>
               <ns10:orderDataType>PORT</ns10:orderDataType>
               <ns10:startRowId>1</ns10:startRowId>
               <ns10:endRowId>2</ns10:endRowId>
            </ns10:InquireEnterpriseTNOrderDataRequest>
         </soapenv:Body>

       

      In above request orderDataType is PORT and orderComponentId is 1523225. So our script need to get the response name stored at path groovyUtils.projectPath+"/MOCK/CSI/IETN/PORTEDTN/ and look at 1523225.xml and give the response back. Else throw the fault message.

       

      <soapenv:Body>
            <ns10:InquireEnterpriseTNOrderDataRequest>
               <ns10:orderComponentId>15253545</ns10:orderComponentId>
               <ns10:orderComponentIdVersion>2</ns10:orderComponentIdVersion>
               <ns10:orderDataType>TTTN</ns10:orderDataType>
               <ns10:startRowId>1</ns10:startRowId>
               <ns10:endRowId>2</ns10:endRowId>
            </ns10:InquireEnterpriseTNOrderDataRequest>
         </soapenv:Body>

       

      In above request orderDataType is TTTN and orderComponentId is 15253545. Hence script should look the response 15253545.xml in path groovyUtils.projectPath+"/MOCK/CSI/IETN/

       

      Hope above details helps. Thanks in advance.

      • nmrao's avatar
        nmrao
        Champion Level 3
        Are you accessing these requests in mock service? And the above script is related to that?
  • Apoorva6's avatar
    Apoorva6
    Frequent Contributor
    Hi Rao,

    Yes. Above script is from mock service.
  • nmrao's avatar
    nmrao
    Champion Level 3
    Apoorva6, can you please specify the problem with above script? or what is happening?
  • Apoorva6's avatar
    Apoorva6
    Frequent Contributor
    With above script, i am always getting soap fault message as no response found even when proper response are placed in both the path.
    • Apoorva6's avatar
      Apoorva6
      Frequent Contributor

      Can someone please check what is wrong with above script please ? As I mentioned in my earlier comment, control is always going to invoke fault message and never reaching file path.

      • nmrao's avatar
        nmrao
        Champion Level 3

        Apoorva6,

         

        Can you try below script ? Note that I could not test before providing as I do not have artifacts.

         

        def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
        def holder = groovyUtils.getXmlHolder(mockRequest.requestContent)
        def soNumber = holder.getNodeValue("//*:InquireEnterpriseTNOrderDataRequest/*:orderComponentId")
        def orderType = holder.getNodeValue("//*:InquireEnterpriseTNOrderDataRequest/*:orderDataType")
        def prefix = "${groovyUtils.projectPath}/MOCK/CSI/IETN/"
        
        def getPrefix = {  orderType == 'PORT' ? ("${prefix}PORTEDTN/" as String) : prefix }
        
        def getFileName = {
          def fileName = "${getPrefix()}${soNumber}.xml" as String
          def file = new File(fileName)
          file.exists() ? fileName : ("${getPrefix()}soapFault.xml" as String)
        }
        
        def getResponseContent = { new File(getFileName()).text }
        
        context.content = getResponseContent()
        log.info "Response content is set to : ${context.content}"