Thursday, November 19, 2015

Groovy Script to generate Multiple Response in SOAP UI

Recently I had a scenario where I had to generate multiple responses on the basis of number of request's element occurrence.

For example the request is as below containing 5 messageId element

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="***">
   <soapenv:Header/>
   <soapenv:Body>
      <web:getSmsStatusList>
        <SmsStatusRequest>
            <messageId>4869754</messageId>
            <messageId>3079075</messageId>
            <messageId>7129441</messageId>
            <messageId>2928236</messageId>
            <messageId>7682597</messageId>
         </SmsStatusRequest>
      </web:getSmsStatusList>
   </soapenv:Body>
</soapenv:Envelope>


I was expecting a response like 5 response element smsStatusItem,

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="****">
   <soapenv:Header/>
   <soapenv:Body>
      <web:getSmsStatusListResponse>
         <!--Optional:-->
         <SmsStatusResponse>
            <!--Zero or more repetitions:-->
            <smsStatusItem>
               <messageId>4869754</messageId>
               <smsStatus>
                  <statusCode>0</statusCode>
               </smsStatus>
            </smsStatusItem>
            <smsStatusItem>
               <messageId>3079075</messageId>
               <smsStatus>
                  <statusCode>0</statusCode>
               </smsStatus>
            </smsStatusItem>
            <smsStatusItem>
               <messageId>7129441</messageId>
               <smsStatus>
                  <statusCode>2</statusCode>
               </smsStatus>
            </smsStatusItem>
            <smsStatusItem>
               <messageId>2928236</messageId>
               <smsStatus>
                  <statusCode>0</statusCode>
               </smsStatus>
            </smsStatusItem>
            <smsStatusItem>
               <messageId>7682597</messageId>
               <smsStatus>
                  <statusCode>0</statusCode>
               </smsStatus>
            </smsStatusItem>
         </SmsStatusResponse>
      </web:getSmsStatusListResponse>
   </soapenv:Body>
</soapenv:Envelope>

For this purpose, SOAP UI supports groovy scripting, and I created the following groovy script.

import groovy.xml.MarkupBuilder

// An array from which county and city will be drawn randomly
def statusArray = [2,0]

def random = new Random()

// create XmlHolder for request content
def holder = new com.eviware.soapui.support.XmlHolder( mockRequest.requestContent )

// Get the name and age values from the request
def requestItems = holder.getNodeValues( "//*:getSmsStatusList/SmsStatusRequest/messageId")

def writer = new StringWriter()
def smsElements = new MarkupBuilder(writer)

// Build the response elements
for (int index = 0; index < requestItems.size(); index =index + 1) {

    smsElements.'smsStatusItem'() {
        'messageId'(Integer.parseInt(requestItems[index]))
         'smsStatus'()
         {
          def randomIndex = random.nextInt(statusArray.size())
       'statusCode'(statusArray[randomIndex])
         }
                                                     }
                                                                                                         }

// Add the newly created elements to the response
context.smsElements = writer.toString()

Below screenshot tells you where you can define the groovy script and how you can add its element to the response of the mock service.



 

1 comment: