- Duration: 20 mins
- Audience: Developers, Architects, Testers, Quality Engineers
When building and working with APIS, you often need to simulate the responses of the system before it has been fully completed. This is what we explore in this workshop - mocking up API structures quickly so they can be subjected to testing withouth having to create all the final service code.
Red hat combines and number of commercial and Open Source tools to cover each part of the API Design lifecycle. In this lab we'll be using the Microcks open source tool.
We know sometime we don't have enough time to go over step by step on the labs. So here is a short video where you can see how to mock the API you previously created using Microcks.
If you are planning to follow the next lab, here is a link to the REST mock service running the Location API.
URLs:
Check with your instruction the GUID number of your current workshop environment. Replace the actual number on all the URLs where you find GUID.
Example in case of GUID = 1234:
https://master.GUID.openshiftworkshop.com
becomes =>
https://master.1234.openshiftworkshop.com
Credentials:
Your username is your asigned user number. For example, if you are assigned user number 1, your username is:
user1
The password to login is always the same:
openshift
For this lab we require a collaboration environment based on Git. You can use GitHub, GitLab or other Git provider to finish this lab. If you don't want to use your personal account, the provided lab environment has an user provided for you in Gogs.
Follow this instructions to set up the repository.
-
Open a browser window and navigate to:
http://gogs.apps.GUID.openshiftworkshop.com/user/login?redirect_to=
-
Log into Gogs using your designated user and password. Click on Sign In.
-
In the main page, click in the + sign in the right top corner to display the New menu. Click the New Migration option.
-
Fill in the information of the repository migration with the following values:
- Clone Address: https://github.com/hguerrero/3scaleworkshop-openapi.git
- Owner: UserX
- Repository Name: locations-api
-
Click on Migrate Repository to fork the GitHub repo into Gogs.
-
Click on Upload File to load the OpenAPI Specification you created and download in Lab 1.
-
Select the file from your local disk folder where you downloaded the file.
-
Click on the Commit Changes button to commit the file.
-
Click the filename link Locations-UserX.yaml to open and review the file.
-
If everything is fine, click the RAW button to get the raw download version of the file.
-
Copy the browser tab URL. Store that URL address as you will use it in the next steps of the lab. The URL should look like the following:
http://gogs.apps.GUID.openshiftworkshop.com/user1/locations-api/raw/master/Locations-UserX.yaml
If you feel more comfortable, you can also copy and paste the RAW button link from the previous step.
-
Open a browser window and navigate to:
http://microcks.apps.GUID.openshiftworkshop.com/
-
Log in into Microks using your designated user and password.
-
You are now in the main Microcks page. Click the Jobs button to access the Jobs page.
-
Click the ADD JOB... button to create your first job.
-
In the Add Job dialog, type in the following information replacing X with your user number and GUID with your working environment:
- Name: Locations-UserX
- Repository URL: http://gogs.apps.GUID.openshiftworkshop.com/userX/locations-api/raw/master/Locations-UserX.yaml
You can also copy and paste the raw url you saved from the Gogs repository (Step 0).
-
After your job is created, click the ACTION menu and select the Activate option.
-
Repet the last step, but now select the Start option. This will start the synchronization job.
-
Refresh your window to get it to the latest state.
-
You will se 3 labels next to your Job. Click the Services label.
-
In the dialog you will see your service listed. Click on the Locations-UserX - 1.0.0. link.
-
Click OK to dismiss the dialog.
-
This is your new REST mock service based on the OpenAPI definition you just loaded to Microcks. Click the Operation GET /locations link to check the example under that operation.
-
You can check that the example we added to the definition in Lab 1 will be used to return the mock values. Copy and save the Mocks URL, we will use that endopoint to test the REST mock service.
We now have a working REST mock service listening for requests. We will use an online cURL tool to test it.
-
Open a browser window and navigate to:
https://onlinecurl.com/
-
Enter the following URL: http://microcks.apps.GUID.openshiftworkshop.com + {{your-user-api-mocks-url}}. Remember to replace the GUID with your environment values and your user number. It should look like this:
http://microcks.apps.GUID.openshiftworkshop.com/rest/Locations-UserX/1.0.0/locations
-
Click the START YOUR CURL button.
-
The page will load the response information from the service. You will be able to see the RESPONSE HEADERS and the actual RESPONSE_BODY. This last part contains the examples we add during the design phase.
Congratulations! You have successfully configure a Microcks Job to create a REST mock service to test your API.
So, you want more? ...
In this lab you used Microcks to configure a REST mock service for the API definition you created in the previous lab. REST mock services allows you to simulate a REST API service when you are in a prototyping stage of your API program journey.
Microcks allows you to test a number of various responses for client application requests. When deploying API, micro-services or SOA practices at large scale, Microcks solves the problems of providing and sharing consistent documentation and mocks to the involved teams. It acts as a central repository and server that can be used for browsing but also by your Continuous Integration builds or pipelines.
You can now proceed to Lab 2
- Microcks