Restful Mock Services

Sample Number

106

Level

Intermediary

Description

This sample demonstrates the usage of UltraESB to create REST mock services

Use Case

I want to quickly create some mock services to respond to my REST calls with GET, POST, PUT and DELETE requests, for testing purposes to emulate a real back-end.

Creating RESTful mock services is made a trivial task with the UltraESB, as RESTful services can easily be created with just a couple of lines of configuration. The objective of this sample is to introduce the user to the UltraESB for mock RESTful services, and not to convey the most efficient way to develop RESTful services.

sample 106

UltraESB will respond to your REST requests without requiring any back-end as shown in the above diagram, and you will easily be able to change the behaviour of the mock response etc. with the configuration.

Sample Configuration

The configuration is straight forward as it just requires a proxy service configured to respond to the request messages within the in sequence itself.

REST mock service configuration

 1<u:proxy id="rest-mock">
 2  <u:transport id="http-8280">
 3    <u:property name="ultra.transport.url" value="rest-mock*"/>
 4  </u:transport>
 5  <u:target>
 6    <u:inSequence>
 7      <u:java import="org.adroitlogic.ultraesb.api.transport.http.HttpConstants;"><![CDATA[
 8          String method = (String) msg.getMessageProperty(HttpConstants.METHOD);
 9          if ("GET".equals(method)) {
10              Message res = msg.createDefaultResponseMessage();
11              if (msg.getDestinationURL().endsWith("customers/1")) {
12                  mediation.setPayloadFromFile(res, "samples/resources/mock-response-1.xml");
13              } else if (msg.getDestinationURL().endsWith("customers/2")) {
14                  mediation.setPayloadFromFile(res, "samples/resources/mock-response-2.xml");
15              } else {
16                  mediation.setPayloadFromFile(res, "samples/resources/mock-response-3.xml");
17              }
18              mediation.sendResponse(res, 200);
19          } else if ("POST".equals(method)) {
20              msg = msg.createDefaultResponseMessage();
21              msg.addTransportHeader("Location", "http://localhost:8280/service/rest-mock/customers/1");
22              mediation.sendResponse(msg, 201);
23          } else if ("PUT".equals(method)) {
24              msg = msg.createDefaultResponseMessage();
25              mediation.sendResponse(msg, 204);
26          } else if ("DELETE".equals(method)) {
27              msg = msg.createDefaultResponseMessage();
28              mediation.sendResponse(msg, 204);
29          } else {
30              msg.createDefaultResponseMessage();
31              mediation.sendResponse(msg, 500);
32          }
33      ]]></u:java>
34    </u:inSequence>
35  </u:target>
36</u:proxy>

The configuration defines a proxy service named "rest-mock" accepting messages with any URL starts with "http://localhost:8280/serivce/rest-mock". It only defines an in sequence where it checks the HTTP method, to decide on the response. The GET method supports different types of responses based on the tail of the destination URL. Finally each and every block uses the Mediation API, to be specific the "sendResponse" call to send response back with specifying different status codes again depending on the method.

In Action

To run the example, start the UltraESB sample configuration 106 on the command line as follows.

Running the sample from startup script

$ cd /opt/ultraesb-2.6.1/bin
$ ./ultraesb.sh -sample 106

Now start the SOA Toolbox and launch an HTTP/S client to invoke the rest mock service over the following URLs to test the mock responses;

GET on the resource URL, http://localhost:8280/service/rest-mock/customers/1 would result in the following response;

Response for the GET on /customers/1 on rest-mock

HTTP/1.0 200 OK
Date: Sun, 12 Feb 2012 03:51:19 GMT
Server: UltraESB/2.6.1
Content-Length: 243
Connection: close

<customer>
  <first-name>Asankha</first-name>
  <last-name>Perera</last-name>
  <street>12 A 1 Pirivena Road</street>
  <city>Mount Lavinia</city>
  <state>LK</state>
  <zip>10370</zip>
  <country>Sri Lanka</country>
</customer>

GET on the resource URL, http://localhost:8280/service/rest-mock/customers/2 would result in the following response;

Response for the GET on /customers/2 on rest-mock

HTTP/1.0 200 OK
Date: Sun, 12 Feb 2012 03:55:36 GMT
Server: UltraESB/2.6.1
Content-Length: 233
Connection: close

<customer>
  <first-name>Avanka</first-name>
  <last-name>Perera</last-name>
  <street>45 Tickle Road</street>
  <city>Colombo 20</city>
  <state>LK</state>
  <zip>20350</zip>
  <country>Sri Lanka</country>
</customer>

POST on the resource URL, http://localhost:8280/service/rest-mock/customers/1 would result in the following response;

Response for the POST on /customers/1 on rest-mock

HTTP/1.0 201 Created
Location: http://localhost:8280/service/rest-mock/customers/1
Date: Sun, 12 Feb 2012 04:02:24 GMT
Server: UltraESB/2.6.1
Content-Length: 0
Connection: close

PUT on the resource URL, http://localhost:8280/service/rest-mock/customers/1 would result in the following response;

Response for the PUT on /customers/1 on rest-mock

HTTP/1.0 204 No Content
Date: Sun, 12 Feb 2012 04:06:42 GMT
Server: UltraESB/2.6.1
Connection: close

DELETE on the resource URL, http://localhost:8280/service/rest-mock/customers/1 would result in the following response;

Response for the DELETE on /customers/1 on the rest-mock

HTTP/1.0 204 No Content
Date: Sun, 12 Feb 2012 04:08:48 GMT
Server: UltraESB/2.6.1
Connection: close

As can be seen, this example illustrates only the very basics on replying to RESTful requests using some of the mock service support. It would be very easy to extend this same sample to say - perform Content Base Routing (CBR) and/or then use XSLT transformation to generate the result to be returned etc. In addition, there is no compiling, no bunding or creation of JAR files, execution of Maven scripts etc.. Just edit the script for any changes, and run!

Related Samples

Sample Number

Sample Title

101

Restful Proxy Services

In this topic
In this topic
Contact Us