API First: From Zero to Hero (Part 1)

Tools

  • Kong as the API Gateway
  • Keycloak as the IAM solution
  • Postman for the creation of the integration tests
  • Newman for the execution of the integration tests

Requirements

  • Docker installed
  • Postman installed
  • Curl installed
  • Linux Make installed

Postman

Importing the OpenApi spec on Postman

  1. On Postman console, click on Import
  2. On the popup, tab Import File click on Choose files

API Mocking with Prism

cd tutorial-apifirst 
docker run --rm --name prism -d -p 4010:4010 -v :/etc/openapi/spec.file stoplight/prism:3 mock -h 0.0.0.0 docker logs prism
[CLI] ... awaiting Starting Prism...
[HTTP SERVER] ℹ info Server listening at http://0.0.0.0:4010
[CLI] ● note GET http://0.0.0.0:4010/pets
[CLI] ● note POST http://0.0.0.0:4010/pets
[CLI] ● note GET http://0.0.0.0:4010/pets/{petId}
[CLI] ▶ start Prism is listening on http://0.0.0.0:4010
curl http://localhost:4010/pets/1
{"id":-9223372036854776000,"name":"string","tag":"string"}

Exploring our API through Postman

Changing the API target URL to our Mock

  1. On the navigation bar, right click on the collection Swagger Petstore and than click on Edit
  2. On the popup, click on tab Variables
  3. Replace the current value of the variable baseUrl with the address: http://localhost:4010
  4. Click in Update to confirm the changes

First exploratory test

  1. On the navigation sidebar, choose one of the available requests under the collection Swagger Petstore on the folder pets, like List all pets for example.
  2. Change the parameter limit to 1
  3. Click on Send to execute the request
  4. Check the result on the tab Body

First API Test

  1. On the request screen click on the tab Tests
  2. Paste the content below on the text field.
pm.test('expect response be 200', function () {
pm.response.to.be.ok
});

pm.test('expect response json valid', function () {
pm.expect(pm.response.json()).to.be.an('array').that.have.length(1)
pm.expect(pm.response.json()[0]).to.include({'id':-9223372036854776000,'name':'string','tag':'string'})
})
  1. Assert that the HTTP Status Code should be equal to ok (HTTP 200)
  2. Assert that our return is a valid JSON document
  3. Assert that the returned json is an array
  4. Assert that the number of itens on the array is 1
  5. Assert that the object on the position 0 of the array should be: {'id':-9223372036854776000,'name':'string','tag':'string'}

Exporting the collection

  • The API Mock running
  • The API imported on Postman and configured to target our Mock
  • Some API tests executed against our Mock
  1. On the navigation sidebar, right click on the collection Swagger Petstore and then click on Export
  2. Keep the default options
  3. Click on Export to confirm
  4. Save the collection under the same folder of this tutorial, with the name petstore-collection.json

Creating and exporting a Postman Environment

  1. On postman’s workspace, on the right corner click on the icon Manage environments (gear tool icon)
  2. On the popup Manage environments, click on Add to add a new environment
  3. On the popup Add Environment, type the environment’s name, on this tutorial we will use localhost
  4. Add the variable baseUrl with value http://localhost:4010 for the INITIAL VALUE field
  5. Click on Add to confirm the configuration
  6. On the popup Manage environments, click on the option Download Environment over the just created environment localhost
  7. Save the collection under the same folder, with the name localhost.postman_environment.json

Executing the collection via command line

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
3Bit Technologies

3Bit Technologies

Cloud Specialists providing professional services with DevOps, BigData, Cloud Native Applications and Security. https://www.3bit.com.br/3bit