Forum Discussion
What is the expected value as per your request? And what is it showing?
- stevieoh8 months agoNew Contributor
Hey nmrao!
I managed to get it working, so I got the value and saved to a variable and it appears to be the value I wanted.Here's my solution (probably stolen from an older response from you in some form or another!).
// create XmlHolder for request content def holder = new com.eviware.soapui.support.XmlHolder( mockRequest.requestContent ) def tempURL = "http://" + mockRequest.requestHeaders.get("Host")[0] //as value is saved is array we must access the content directly mockRunner.mockService.project.setPropertyValue('requestURL',tempURL) //save to Project Variable for now
So I'm happy enough in my Mock Service that I can access details of the incoming request and use them for certain decisions and save things to variables.
In this example I save at a project level, which may change but I'm struggling to find documentation to set/save this at a test suite or test case level which I think would be more suitable / useful.
This might be a separate topic /question and it's one I have seen you and another user answer on in some form for previous queries but I am looking to test/mock an asynchronous service and do it for multiple potential SOAP Actions.
So the real end system I am mocking will send an ACK and then later send additional updates (effectively order progress). I will also be mocking a decetly large number of SOAP Actions (10+ let's say) that update in this way.
I have handled this through a Test Suite and Test Case setup that is called via the OnRequest Script and AfterRequest Script functionality in the SOAP UI Mock Service and the decision on which Test Case to call is based on the SOAP Action and a switch statement. I'm not 100% happy with these choices for below reasons.
1. Currently I save the values of host and SOAP Action in the OnRequest Script and then let my mock service respond with the ACK. In this response I generate a UUID (<ORDER_ID>${=java.util.UUID.randomUUID()}</ORDER_ID>).
In the script for the mock response I save a UUID at a project level which needs to be reused in future "expected" asynch responses for this specific request (such as subsequent Update, Complete notifications).
I then use AfterRequest Script to kick off the test case to send suitable subsequent responses of Update and Complete using the same UUID.
// create XmlHolder for request content def holder = new com.eviware.soapui.support.XmlHolder( mockRequest.requestContent ) def tempURL = "http://" + mockRequest.requestHeaders.get("Host")[0] def tempAction = mockRequest.requestHeaders.get("SOAP-Action")[0] mockRunner.mockService.project.setPropertyValue('requestURL',tempURL) log.info ("Step 1: Saved request URL and SOAP Action...") // get target testcase by Action switch(tempAction){ case "Service 1": def testCase = mockRunner.mockService.project.getTestSuiteByName("ASync_Responses").getTestCaseByName("Service 1") log.info ("Step 2: Selected test case...") // run testCase def runner = testCase.run( new com.eviware.soapui.support.types.StringToObjectMap(), false ) log.info ("Step 5: Scripts completed!") //Step 3 and Step 4 is in the ASyncResponses Test Suite Test Case selected by script above case "Service 2": def testCase = mockRunner.mockService.project.getTestSuiteByName("ASync_Responses").getTestCaseByName("Service 2") log.info ("Step 2: Selected test case...") // run testCase def runner = testCase.run( new com.eviware.soapui.support.types.StringToObjectMap(), false ) log.info ("Step 5: Scripts completed!") //Step 3 and Step 4 is in the ASyncResponses Test Suite Test Case selected by script above ... case "Service X": etc. }
The problem:
I know I need to move this UUID to be saved at some form of a context level within a test case as I could receive two requests in quick succession and could end up mixing up which UUID to use for the following updates (i.e. the second request to the MockService will overwrite the UUID for the first so the requesting system receives duplicate updates for Order 2 and not all Updates for Order 1).
I think that ideally my Mock Service wouldn't send any response and will only select a test case to run which will send all the responses instead. This should mean everything can be contained within the test case itself and at a context level.
EDIT: On further thought perhaps I do most of my work in OnRequest Script, which calls the test case and then the final response (Completion) is populated by the test case as called out here:
https://www.soapui.org/docs/soap-mocking/creating-dynamic-mockservices/#4-Creating-the-response-from-the-result-of-a-TestCaseSo my last step of the test case can be to generate the final asynch response body but let the MockService send it (no clue how that would work ha!).
What do you think? Any suggestion?- nmrao8 months agoChampion Level 3
- Never used handling of async services. So, really nothing much to offer.
- Just noticed this documentation, please see if not already.
In this example I save at a project level, which may change but I'm struggling to find documentation to set/save this at a test suite or test case level which I think would be more suitable / useful.
Think this developing mock service as independent activity or entirely using different technology, then also neither we have project, nor test suite / case. So need to come up with different way of holding required data with in the mock response handler itself such as Map or some file based serialization, load the at start and serialize at the end in case if losing the state over the time.
As per the above documentation link, use context to add your properties
say, to set a value
context.myprop = 1
and later get it context.myprop
Similarly context can hold uuid's too as long as unique key is used.
MockResponse Response Scripts
The context script variable available in the MockResponse Response Script, acts both as a MockRunContext and a TestRunContext, allowing it to access the same context variables as other Groovy scripts in the TestCase
Noticed in the script that you are trying to run test case from mock service which I am not sure if it is right to do so. Also didn't have complete context, so don't want to jump into conclusions.
- nmrao8 months agoChampion Level 3
Looks like below might help what you may want to achieve. Please check and give it a try.
Related Content
- 4 years ago
Recent Discussions
- 3 days ago