Trying the API with the Mock Service

<< Click to Display Table of Contents >>

Navigation:  »No topics above this level«

Trying the API with the Mock Service

Previous pageReturn to chapter overviewNext page

This section introduces the RepreZen™ API Studio Mock Service - what it is, why we need it and how to use it.

RepreZen API Studio enables us to define RESTful APIs - a set of resources with their supported HTTP methods that provide an interface to a service of some kind.  The Mock Service enables us to play with an API we are designing without having to implement it first.  It's good for basic testing, especially by a group of people who might be consumers of the API, and whose feedback you want. The basic steps for using the Mock Service are:

1.Add static, example response data to the rapidModel we wish to test.
2.Run a local Mock Service for this rapidModel.
3.Exercise the mocked API using an HTTP client of some kind (e.g. curl, the Postman chrome extension or a plain web browser).

In this way we get a chance to test drive our API, to experience and evolve the design from the outside, perhaps see how it integrates with other services, before making further development efforts.

Adding example data to our rapidModel

To demonstrate this we'll add some simple XML data to our rapidModel using the example keyword in our PersonObject resource.

1. Go to the bottom right hand corner of PersonObject > getPersonObject > response. Press Enter, tab in once and use Code Assist to select example:
 
MockServer_01
2. Add a space and use Code Assist again to add the ''' ''' within which the example XML will sit:
 
MockServer_02
3. And finally, paste in the following XML fragment:
 
<?xml version="1.0" encoding="UTF-8"?>
<person>
       <firstName>John</firstName>
       <lastName>Smith</lastName>
       <otherNames>
               <element>Jean Poupon</element>
       </otherNames>
       <id>user1</id>
</person>
4. Your code will now look like this:
 
MockServer_03

Note: The primary purpose of a RepreZen API Studio rapidModel is to fully represent, and document, all salient aspects of our API. The example keyword enables us to extend this documentation to show how the data passing back and forth across the API will look, given the supported media types and the data structures involved. The code highlighted above is therefore the equivalent of saying, "This is what a response from this method will look like." Such examples can then be used to Mock the service described by our model for purposes of testing and demonstration.

Running the local Mock Service

We can now make our API live to testing purposes by simply turning on the Mock Service for the rapidModel clicking the ON/OFF button at the top:
 
MockServer_04
 
MockServer_05

The Mock Service console should now open in the panel under the main code view. (If it does not we can open it manually from Window > Show View > Other... and then select Console.) The URI for the resource we just loaded with example data is highlighted:
 
MockServer_06

Note: As a convenience, the Mock Service finds the first available port on the host computer and runs on this. The port is then displayed in the console header.

Exercising the API

We can now try the API and see how it looks, for example by using a web browser:
 
MockServer_07

Note: To be well-formed, the URI of this request has to contain an {id} clause, but the value is irrelevant because the Mock Service always returns the same, statically defined example response.

You can also try the API using the Swagger UI view:

MockServer_08

Scroll down to find the GET method on PersonObjectResource to which we just added an example message:

MockServer_09

Press the GET button, enter a Person id, press the Try it out! button and see the example response data appear in the UI:

MockServer_10

Additional Notes

To turn the Mock Service on or off for a particular rapidModel use the ON/OFF switch at the top of the model editor.
You cannot run the Mock Service for multiple models simultaneously. The Mock Service for one model will be shut down if you start a Mock Service for another model.
The Mock Service will continue running even if no Mock Services are running. To terminate this service use the red button in the console:
MockServer_11
An example data element can be added to all methods supported by a resource, or it can be added to the root level.
An example data element added to the root level of a resource will apply to all methods in that resource except those that define their own example data.
The Mock Service will only use example data to Mock methods where the response includes a payload.
Copyright © 2016 ModelSolv, Inc.  All rights reserved. RepreZen and RAPID-ML are trademarks of ModelSolv, Inc. Swagger is a registered trademark of SmartBear Software, Inc. RepreZen API Studio is not associated with nor endorsed by SmartBear Software, Inc.