Generating Mocks to have a realistic idea of how an API will work is a challenge as it requires functionally consistent examples.
We will see in this article how to manage examples to feed tools such as Microcks to generate API Mocks.
In the rest of this article we will use the following types:
type Person{ name:String firstName:String age:Integer address:Address }
type Address { street:String city:String zipCode:String country:String }
on which we define the following data examples
example xLeslie on Person{ name: "Desforge" firstName:"Leslie" age: 26 address:xLyon }
example xLyon on Address{ street:"Avenue Carnot" zipCode: "69000" city: "Lyon" country: "France" }
As well as the following facade:
facade PersonDTO on Person{ familyName:name city:address.city age }
Declaration of routes, parameters and use of examples
Tools like Microcks use the ability to name examples in OpenAPI statements to reconstruct scenarios.
In the following example
- one of the values of the Id parameter is declared with the name Leslie (value "1")
- a Json example of a response is also named Leslie. (xLesli)
The scenario can then be built, when the Id parameter will be passed with the value "1" in parameter for this route, the Mock will answer with the Json xLesli.
Note that no example has ever been defined for the PersonDTO facade that is used for the response.
You have directly used the example defined on the Person type and Akwatype has automatically transformed it to match the PersonDTO facade.
example
service Person{ url:"person" expose Persons{ url:"persons" route person{ url:"person/id:String" path{ id:String example{ id { Leslie:"1" , leo:"2" } } } responses{ "200":PersonDTO{ examples { Leslie:xLesli, Leo:xLeo } } } } }
Generated OpenAPI
Mocks in Microcks after uploading the OpenAPI into Microcks
The OpenAPI descriptions generated by Akwatype can be downloaded and imported manually into Microcks, but it is also possible to integrate Akwatype and Microcks via a Git repository
With a shared Git repository, exporting an Open API description with the Push to Git option from Akwatype will update Microcks directly.