Forum Discussion

tt's avatar
tt
New Contributor
9 years ago

selectively forward request from mock service

I'm trying to configure a mock service where depending on the request, either return a mock response or forward the request to the actual live web service.  I think the OnRequest script is the place to do this. 

So far, I'm able to query the mockRequest to decide if the request should be forward to actual web service.  My issue is what's the best solution for sending the request from the mock service.  I've seen some examples using groovy-wslite and some not. 

I've tried to follow the suggestions here but need more details: 

http://community.smartbear.com/t5/SoapUI-Open-Source/Making-a-live-service-call-from-a-mock-service/m-p/4147#M1995

 

Thanks!

Tim

4 Replies

  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    Hi Tim,

     

    Sounds reasonable.

     

    To make the request from the mock, I would say there are 3 main options:

    1) Call the actual service using a thrid party java/Groovy library e.g. like you said using ws-lite or similar

    2) Call the actual service using the SoapUI objects, as per Ole's solution in the post you referenced.

    3) Call the actual service using by triggering another seperate TestCase which contains a Request TestStep to call your actual service.

     

    I would say all are fine options, and I could discuss the others/provide code advice, but since you propabably already considered the first two options, I'll try to describe my idea for option 3) incase you prefer it as a less code centric option.

     

    So the main part of option 3 is how to trigger the separate TestCase, this can be done in the mock script using something like:

     

    //You can pass data when calling the TestCase e.g. the mock request
    def map = new StringToObjectMap()
    map.put("responseData", requestToMock)

     

    def testSuite = context.mockService.project.
    getTestSuiteByName("Separate TestSuite")


    def callBackTestCase = testSuite.getTestCaseByName("Separate TestCase")

    def callBackRequest = (WsdlTestRequestStep) callBackTestCase.getTestStepsOfType(WsdlTestRequestStep.class).get(0)

     

    (This code was adapted from a different solution, but similar use-case http://community.smartbear.com/t5/SoapUI-Open-Source/How-to-simulate-an-asynchronous-web-service-with-mock-services/m-p/98365#M17496)

     

    Does this make sense? So basically option 3 involves a small amout of Groovy code in the mock to get SoapUI to forward the mock request to the actual service, maybe also has the avantage of being able to add Assertions for the forwarding Request TestStep..

     

    Let me know if you'd like to discuss any points.

     

    Hope this helps,


    Cheers,

    Rupert

    • tt's avatar
      tt
      New Contributor

      Thanks for the quick reply Rupert.  In concept, I understand your approach. 

      can you please explain this line:

      def callBackRequest = (WsdlTestRequestStep) callBackTestCase.getTestStepsOfType(WsdlTestReques​tStep.class).get(0)

       

      I'm pretty new to groovy and I am not familiar with the syntax of the class name in parens in "callBackRequest = (WsdlTestRequestStep) . . . "

       

      Also, at some point, I need to package this mock into a war.  I am hoping that the extra test suite I added can be included.

       

      Thanks!!

      Tim

    • tt's avatar
      tt
      New Contributor

      Thanks for the reply Rupert. 

      can you please explain this line in your suggestion:

       

      def callBackRequest = (WsdlTestRequestStep) callBackTestCase.getTestStepsOfType(WsdlTestReques​tStep.class).get(0)

       

      I'm pretty new to Groovy and I'm not familiar with the syntax of class name in parens

      "callBackRequest = (WsdlTestRequestStep)"

       

      Thanks,

      Tim

      • rupert_anderson's avatar
        rupert_anderson
        Valued Contributor

        Hi Tim,

         

        I do appologise for that line, I think I accidentally copied that part in from the other solution (referenced from the link). What that line does (or did in the other solution) was to get hold of the request contained in the other TestCase in order to set the endpoint property to the call back url - it was using WS-Addressing to provide a call back address you see.

         

        In your case the only parts you might need are:

         

        def map = new StringToObjectMap()
        map.put("responseData", requestToMock)

         

        def testSuite = context.mockService.project.
        getTestSuiteByName("Separate TestSuite")


        def callBackTestCase = testSuite.getTestCaseByName("Separate TestCase")

        callBackTestCase.run(map, false) //true here means call asynchronously i.e. dont wait to the TestCase to finish first

         

        But... now that you have said that you need to package the mock as a war, then that makes this solution innapropriate since the separate TestCase used to forward the request could not be packaged in the mock!

         

        With that I recomend solution 1 or 2 i.e. I think you need to make a Groovy request based on the mockRequest in order to forward the request on to the actual service. If you need any tips for this please ask.

         

        Cheers,

        Rupert