SoapUI

SoapUI is a free and open source cross-platform Functional Testing solution.

http://www.soapui.org/

Rest OnRequest sample Groovy

import com.eviware.soapui.impl.rest.mock.RestMockResult

RestMockResult res = new RestMockResult( mockRequest )
String val ="{test:1234}"
mockRequest.httpResponse.writer << val
mockRequest.httpResponse.status = 200 
mockRequest.httpResponse.header = "Content-type: application/json"
mockRequest 
return res

WAR generation from SoapUI project

SoapUI is able to generate a WAR to create mock servers to run on Tomcat. The SoapUI project is stored inside the WAR in WEB-INF/soapui/soapuiprj.xml .

Sample onRequest script to call groovy scripts inside WEB-INF/soapui/. They can be changed on runtime after Tomcat has exploded the WAR file.

Inside the onRequestScript

import groovy.util.GroovyScriptEngine
import com.eviware.soapui.support.GroovyUtils
def gru = new GroovyUtils(context)
GroovyScriptEngine ge = new GroovyScriptEngine(gru.projectPath)
Binding b = new Binding()
b.setVariable("mockRequest", mockRequest);
ge.run( "scriptx.groovy" , b )

scriptx.groovy

import com.eviware.soapui.impl.rest.mock.RestMockResult

RestMockResult res = new RestMockResult( mockRequest )
println "Message in tomcat console"
def servletContext = mockRequest.httpRequest.getServletContext()
println servletContext.getServerInfo()
println servletContext.getServletContextName()
servletContext.log("servlet context log") //INFO: in file logs\localhost.XXXX.log in apache

String paramx = mockRequest.httpRequest.getParameter("paramx")
String rendered = String.format("<html><body><p>Page with %s</p></body></html>",paramx)
mockRequest.httpResponse.writer << rendered
mockRequest.httpResponse.setStatus(200)
mockRequest.httpResponse.setContentType("text/html")
mockRequest.httpResponse.setContentLength( rendered.length() )

Install SOAP UI

Mock REST service

Dispatch script for action that has a Default response , in the MockResponse script area add the stuff below

import com.eviware.soapui.impl.rest.mock.RestMockResult

def getNumberArgument(){
  def requestPath = mockRequest.getPath()
  def split = requestPath.tokenize('/')
  
  def index =0
  for(item in split){   
        if(item=="number"){
                break
        }
        index++
  }
  def nr = split[index+1].toInteger()
  return nr
}

def buildResponse(nr){
  def val =""

  if(nr % 2 ==0){
    val = '{"number":"evenxy"}'
  }
  else{
    val = '{"number":"oddxy"}'
  }
  log.info("called build resp")
  return val
}

def createMockResponse(status,contentType,payload){
  RestMockResult res = new RestMockResult( mockRequest )
  // HttpServletResponse
  // ServletResponse
  response = mockRequest.httpResponse
  response.setStatus(status)
  response.setContentType(contentType)
  response.writer.print(payload)
  response.writer.flush()
  return res
}

def nr = getNumberArgument()
def val = buildResponse(nr)
def res = createMockResponse(200,"application/json",val)

return res

test endpoint

import com.eviware.soapui.impl.rest.mock.RestMockResult

RestMockResult res = new RestMockResult( mockRequest )

String paramx = mockRequest.httpRequest.getParameter("paramx")
String rendered = String.format("<html><body><p>Page with %s</p></body></html>",paramx)
mockRequest.httpResponse.writer.print(rendered)
mockRequest.httpResponse.writer.flush()
mockRequest.httpResponse.setStatus(200)
mockRequest.httpResponse.setContentType("text/html")
mockRequest.httpResponse.setContentLength( rendered.length() )

get json endpoint

import com.eviware.soapui.impl.rest.mock.RestMockResult
import groovy.json.JsonOutput
/* comment*/
// comment
httpRes = mockRequest.httpResponse
class Out{
  int id
  String name
}

class Other{
  Out[] stuff
}

String paramx = mockRequest.httpRequest.getParameter("paramx")
def elements = [ new Out(id:1,name:"aaaak1") ]
elements << new Out(id:2,name: paramx )
elements.add( new Out(id:3,name:"aaaak3") )

def data = new Other(stuff : elements)

log.info( data.stuff.size() )
log.info( data.stuff.getClass() )

String payload =  JsonOutput.toJson(data)
httpRes.setStatus(404)
httpRes.setContentType("application/json")
httpRes.setContentLength( payload.length() )
httpRes.writer.print(payload)
httpRes.writer.flush()

multipart form data

import com.eviware.soapui.impl.rest.mock.RestMockResult

def values=""

for(def attach in mockRequest.getRequestAttachments() ){
  log.info( attach.getAttachmentType().toString() )
  log.info("name " + attach.getName() )
  log.info("part " + attach.getPart() )
  log.info("content type  " + attach.getContentType() )
  def r = new BufferedReader( new InputStreamReader(attach.getInputStream() ) )
  def line=""
  while(line != null){
    line = r.readLine()
    if(line!=null){ 
         values += " " + line
    }
  } 
}

String payload =  " <<<<<<<< " + values + ">>>>>>>>> \n"
log.info(payload)
httpRes = mockRequest.httpResponse
httpRes.setStatus(200)
httpRes.setContentType("text/plain")
httpRes.setContentLength( payload.length() )
httpRes.writer.print(payload)
httpRes.writer.flush()

SoapUI (last edited 2020-05-04 23:39:09 by localhost)