Reputation: 43
I'm trying to make a solution(Mock Service Script Dispatch) for my application with following features:
user Groovy script accepts "request" and generates "response", for example,
if(request.xxx == 5) { response.yyy = 6 }
What is the best solution for the application?
@Rao
Example, user uploads
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="urn:test">
<xs:element name="TestRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="data" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="TestResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="data" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
and script:
response.data = request.data + 'xxx'
There are some applications which can creates a properly formed "requests" (here TestRequest). So I can parse this requests as XML DOM, for example. Then the "request" is passed as argument to Groovy script above.
request:
<?xml version="1.0" encoding="UTF-8"?>
<TestRequest xmlns="urn:test">
<data>data</data>
</TestRequest>
response:
<?xml version="1.0" encoding="UTF-8"?>
<TestResponse xmlns="urn:test">
<data>dataxxx</data>
</TestResponse>
(actually this is Java application and Groovy script would be run via Java Scripting API)
Upvotes: 0
Views: 1480
Reputation: 21389
Here is the Script for dispatch of Mock Service:
assert mockRequest.requestContent, 'Request is null or empty'
log.info mockRequest.requestContent
def xml = new XmlSlurper().parseText(mockRequest.requestContent)
context.data = xml.'**'.find {it.name() == 'price'}?.text() + 'xxx'
In the response have property expansion as shown below as place holder. As soon as request hits mock service, the values is read from the request, and set the desired value (NOTE: ${data}
) for <data>
element of the response and sent it in the response.
<?xml version="1.0" encoding="UTF-8"?>
<TestResponse xmlns="urn:test">
<data>${data}</data>
</TestResponse>
Upvotes: 1