Sunday, October 27, 2013

Mocking an Asynchronous Web Service using SoapUI.


This is an example scenario.

Echo service is an asynchronous web service and this is the service which we are going to mock using SoapUI. Clients send requests to the echoService using ws-Addressing. Echo service uses ws-addressing headers to identify client's callback information and sends back echo-Response to client's callback services.






Tools used:

Step 1 : Obtain or Create wsdls files of the services which you are going to mock and client callback service.


Here I am using EchoService.wsdl file as my service's wsdl and ClientCBService.wsdl as client's callback service wsdl.

Step 2 : create a new SoapUI project using service and client WSDLs.
  • Open SoapUI.
  • Then goto File -> new SoapUI project or use shortcut key Ctrl + N
  • Give the your service wsdl path in initial wsdl/wadl filed and give an appropriate project name. ( Let's say EchoService ) and Click OK

    Now Let's add client's wsdl into the same project.
    • right click on your SoapUI project and select "Add wsdl"

    • In "Add WSDL" window select your client wsdl and Click OK

      ( Note: Create Sample Requests for both server and client's callback operations if they were not created by default )



       Step 3 : Generate Mock service for EchoService

      • Right click on EchoService and select Generate MockService
      •   In Next windows click OK to create mock service with default values.


      • Now we have created the mock service. In Mock service window, click configure icon ( the most right icon) to set host name and port.

      • Give host name as localhost and give port which you prefer.


      Step 4: Create a TestCase to invoke Client's callback service with echoService response.  

      • Right click on the client wsdl project and select Generate TestSuite.
      •  In next windows Click Ok to reate test suite with default values.


      •  Now You have create the TestSuite. Open the test-step which was created by default. SetechoString and messageID parameters in the request message. ( you can set parameter using ${parameter_name} )

      •  We will extract and set values to the above parameters in next step.


      Step 5 : Write a groovy script to handle asynchronous behavior of the service.

      •  Double click on echo Operation in Mock-Service
      •   In opened Window select Dispatch type as Script.

      •  Then enter following code as our dispatch logic. It extract values from incoming request and parse to the testcase using a map. Also it sets testcase's end point address to the value that came with ReplyTo header.

      12345678910111213141516171819202122
      import com.eviware.soapui.support.GroovyUtils
      import com.eviware.soapui.support.types.StringToObjectMap
      import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep
       
      def groovyUtils = new GroovyUtils( context )
      def holder = groovyUtils.getXmlHolder( mockRequest.requestContent )
      def map = new StringToObjectMap()
      holder.declareNamespace("wsa","http://www.w3.org/2005/08/addressing")
      holder.declareNamespace("ech","http://www.example.org/EchoService/")
      map.put("messageID", holder.getNodeValue("//wsa:MessageID/text()"))
      def address = holder.getNodeValue("//wsa:ReplyTo/wsa:Address/text()")
      map.put("address", java.net.URLDecoder.decode(address, "UTF-8"))
      map.put("echoString", holder.getNodeValue("//ech:echo/in"))
      def testsuite = context.mockService.project.getTestSuiteByName("ClientCBServiceSOAP TestSuite")
      def testcase = testsuite.getTestCaseByName("echoResult TestCase")
      def testrequest = (WsdlTestRequestStep) testcase.getTestStepsOfType(WsdlTestRequestStep.class).get(0)
      testrequest.testRequest.setEndpoint(address)
      testcase.run(map, false)
      view rawsoapUI.groovy hosted with ❤ by GitHub
      Note: I wrote above script by modifying an answer in stackoverflow.com


      Step 6 : Testing out Asynchronous Service

      Now we have finished mocking our asynchronous service. But we need to have a callback service to get the response back.

      • Let's create another soapUI project to mock the callback wsdl. Follow above steps to create a mock service for client callback. For your reference I have added only few screenshots of most important steps. Note that I am using port as 8087 and host as localhost.





      • Start client Callback service by clicking green arrow of the mock service.


      •  Start our asynchronous echo (mock) service.

      • Now we are ready send a request to our mock service. Open request in EchoServiceSOAP and add mockservice endpoint to it.



      •  Click WS-A tab and enable ws-addressing. Then set "Action", "To", "ReplyTo", and "MessageID" fields in the request message as follow.

      •  Now we are ready to invoke our service. Click submit button in request window to send the request to mock service.
      • It returns an empty response. But if you check your callback service, you can see that actual echo response was sent to it.
      • You can view incoming request messages in both mock-services windows. Open both echoService request and echoReult message and Compare them for echoed string and ws-a messageID. ( see bellow screenshot.)



      Links to Files:

      No comments: