Stateful mock servers to the
rescue on REST ecosystems
netponto - 2018-04-07
Nuno Caneco
Nuno Caneco
/@nuno.caneco
/nunocaneco
nuno.caneco@gmail.com
Engineering Manager / Cluster Lead
@nunocaneco
Raise your hands
The Monolith
A monolith is a geological feature consisting of a single massive stone or rock, such as some
mountains, or a single large piece of rock placed as, or within, a monument or building.
https://en.wikipedia.org/wiki/Monolith
A software system is called "monolithic" if it has a monolithic architecture, in which functionally
distinguishable aspects (for example data input and output, data processing, error handling, and the
user interface) are all interwoven, rather than containing architecturally separate components.
https://en.wikipedia.org/wiki/Monolithic_system
Monolithic architecture
Storefront
UI
Backoffice
UI
Catalog Service
User Service
Order Service
(...)
Database
Mobile
Web API
Microservices architecture
Storefront
UI
Backoffice
UI
User
Service
Mobile
Web API
Catalog
Service
Order
Service
REST
REST has become the default technology to support Microservices Architecture
● It's HTTP(S)
● Use HTTP Verbs for actions: GET, PUT, POST, DELETE, PATCH, ...
● Use HTTP Status Codes to provide meaningful and normalized responses to
clients
● Easily extensible using HTTP Headers
● Support multiple authentication mechanisms
● JSON is more simple and less verbose than XML
The growth of a
Microservice
Application
Ecosystem
User interfacePublic API
Backend Services
Publish / Subscribe
Databases
BLOB Storage
3rd party systems
Component Dependency
System Complexity ~= Business Complexity
Loose coupling ; High dependency
Dependency
level = 0
Dependency
level = 10
Dependency
level = 7
Teams & Components
Combine dependency level with
● Development environment
○ Setup
○ Maintain
● Keep services up to date
● Keep database schema and data
● Test
○ Time it takes to finish tests
○ Dependency from data
● CI/CD
○ Keep a stable environment for end-to-end tests
● Contract stability
● Communication between maintainers
Fakes to the
rescue
Terminology
Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.
Fake objects actually have working implementations, but usually take some shortcut which makes them not
suitable for production (an in memory database is a good example).
Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside
what's programmed in for the test.
Spies are stubs that also record some information based on how they were called. One form of this might be an
email service that records how many messages it was sent.
Mocks are objects pre-programmed with expectations which form a specification of the calls they are expected to
receive.
https://martinfowler.com/articles/mocksArentStubs.html
Mock your dependencies
Dependencies with real implementations are
replaced by mocked implementations that
provide similar behavior at the interface level.
Example
Example
Fake Techniques - Client Fake
Client App
XptoAPIClient
Xpto Web API
HTTP
FakeXptoAPI
Client
IXptoApiClient
Quick to implement and simple to use
No new components on the system
Using a Mock framework allows control of
results on automated tests
Doesn't test the HTTP layer
Requires coding to implement statefulness
The Production Code is not stimulated
Potential risk to "Go Live" with the Fake
instead of Real implementation
Fake Techniques - Proxy
Client App
XptoAPIClient
Xpto Web API
HTTP
Quick to implement and simple to use
Saved responses are similar to the real API
No coding necessary (although possible)
Easy to implement with containers
Very flexible: easy to modify responses
Contract changes invalidate previous saved
responses
Requires coding to implement statefulness
Difficult to implement Expectations
mechanism to integrated with automated tests
Store
and
Forward
Proxy
HTTP
Responses
Fake Techniques - Fake Server
Client App
XptoAPIClient
Xpto Web API
HTTP Not so flexible to change responses
Requires coding
Need to implement Expectations to integrate
with automated tests
Code generation accelerates development
Easy support for contract changes:
(just regenerate the code and change implementation)
Easy to create and share containers
Easy to implement statefulness
Fake Xpto Web
API
Production&Integration
Development&Tests
HTTP
Demo I
Proxy
Demo - Proxy
Movie Rating UI
MovieRating
APIClient
Movie Rating Web API
HTTP
Store
and
Forward
Proxy
Responses
Omdb API
Demo II
Fake Server
Demo - Fake Server
Movie Rating UI
MovieRating
APIClient
Movie Rating Web
API
Fake Xpto Web
API
Omdb API
Wrapping up
Integrating with docker
Using docker to launch every 1st dependency allows easy
setup of the project.
$> git clone <repo>
$> docker-compose up deps
Open solution in Visual Studio and hit F5
No expensive setup of databases, messaging systems or other services
Docker images can be versioned alongside with the versions of the API
Can be used to run component tests (not integration tests)
Summary
● Fake the 1st level dependencies to avoid having the entire dependency tree
● Choose your strategy: Client Fake, Proxy, Stateful Fake or any other that suits
● Consider generating as much code as possible
● Potentially high upfront investment - big gains in the long run
● Make it easy for the developers to
○ Run the project
○ Manage the versions of the dependencies they are using
○ Write automated tests
● Make components tests running against Fakes instead
ANY QUESTIONS?
/@nuno.caneco
/nunocaneco
nuno.caneco@gmail.com
THANK YOU
@nunocaneco
Patrocinadores “GOLD”
Twitter: @PTMicrosoft http://www.microsoft.com/portugal
Patrocinadores “GOLD”
Twitter: @FindmoreC http://www.findmore.pt/pt
Patrocinadores “Silver”
Patrocinadores “Bronze”
http://bit.ly/netponto-aval-76
* Para quem não puder preencher durante a reunião,
iremos enviar um email com o link à tarde
Próximas reuniões presenciais
07/04/2018 – Lisboa
23/06/2018 – Lisboa
15/09/2018 – Lisboa
24/11/2018 – Lisboa
Reserva estes dias na agenda! :)

Stateful mock servers to the rescue on REST ecosystems

  • 1.
    Stateful mock serversto the rescue on REST ecosystems netponto - 2018-04-07 Nuno Caneco
  • 2.
  • 3.
  • 5.
    The Monolith A monolithis a geological feature consisting of a single massive stone or rock, such as some mountains, or a single large piece of rock placed as, or within, a monument or building. https://en.wikipedia.org/wiki/Monolith A software system is called "monolithic" if it has a monolithic architecture, in which functionally distinguishable aspects (for example data input and output, data processing, error handling, and the user interface) are all interwoven, rather than containing architecturally separate components. https://en.wikipedia.org/wiki/Monolithic_system
  • 6.
    Monolithic architecture Storefront UI Backoffice UI Catalog Service UserService Order Service (...) Database Mobile Web API
  • 7.
  • 8.
    REST REST has becomethe default technology to support Microservices Architecture ● It's HTTP(S) ● Use HTTP Verbs for actions: GET, PUT, POST, DELETE, PATCH, ... ● Use HTTP Status Codes to provide meaningful and normalized responses to clients ● Easily extensible using HTTP Headers ● Support multiple authentication mechanisms ● JSON is more simple and less verbose than XML
  • 9.
    The growth ofa Microservice Application
  • 10.
    Ecosystem User interfacePublic API BackendServices Publish / Subscribe Databases BLOB Storage 3rd party systems
  • 11.
    Component Dependency System Complexity~= Business Complexity Loose coupling ; High dependency
  • 12.
    Dependency level = 0 Dependency level= 10 Dependency level = 7 Teams & Components
  • 13.
    Combine dependency levelwith ● Development environment ○ Setup ○ Maintain ● Keep services up to date ● Keep database schema and data ● Test ○ Time it takes to finish tests ○ Dependency from data ● CI/CD ○ Keep a stable environment for end-to-end tests ● Contract stability ● Communication between maintainers
  • 14.
  • 15.
    Terminology Dummy objects arepassed around but never actually used. Usually they are just used to fill parameter lists. Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example). Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test. Spies are stubs that also record some information based on how they were called. One form of this might be an email service that records how many messages it was sent. Mocks are objects pre-programmed with expectations which form a specification of the calls they are expected to receive. https://martinfowler.com/articles/mocksArentStubs.html
  • 16.
    Mock your dependencies Dependencieswith real implementations are replaced by mocked implementations that provide similar behavior at the interface level.
  • 17.
  • 18.
  • 19.
    Fake Techniques -Client Fake Client App XptoAPIClient Xpto Web API HTTP FakeXptoAPI Client IXptoApiClient Quick to implement and simple to use No new components on the system Using a Mock framework allows control of results on automated tests Doesn't test the HTTP layer Requires coding to implement statefulness The Production Code is not stimulated Potential risk to "Go Live" with the Fake instead of Real implementation
  • 20.
    Fake Techniques -Proxy Client App XptoAPIClient Xpto Web API HTTP Quick to implement and simple to use Saved responses are similar to the real API No coding necessary (although possible) Easy to implement with containers Very flexible: easy to modify responses Contract changes invalidate previous saved responses Requires coding to implement statefulness Difficult to implement Expectations mechanism to integrated with automated tests Store and Forward Proxy HTTP Responses
  • 21.
    Fake Techniques -Fake Server Client App XptoAPIClient Xpto Web API HTTP Not so flexible to change responses Requires coding Need to implement Expectations to integrate with automated tests Code generation accelerates development Easy support for contract changes: (just regenerate the code and change implementation) Easy to create and share containers Easy to implement statefulness Fake Xpto Web API Production&Integration Development&Tests HTTP
  • 22.
  • 23.
    Demo - Proxy MovieRating UI MovieRating APIClient Movie Rating Web API HTTP Store and Forward Proxy Responses Omdb API
  • 24.
  • 25.
    Demo - FakeServer Movie Rating UI MovieRating APIClient Movie Rating Web API Fake Xpto Web API Omdb API
  • 26.
  • 27.
    Integrating with docker Usingdocker to launch every 1st dependency allows easy setup of the project. $> git clone <repo> $> docker-compose up deps Open solution in Visual Studio and hit F5 No expensive setup of databases, messaging systems or other services Docker images can be versioned alongside with the versions of the API Can be used to run component tests (not integration tests)
  • 28.
    Summary ● Fake the1st level dependencies to avoid having the entire dependency tree ● Choose your strategy: Client Fake, Proxy, Stateful Fake or any other that suits ● Consider generating as much code as possible ● Potentially high upfront investment - big gains in the long run ● Make it easy for the developers to ○ Run the project ○ Manage the versions of the dependencies they are using ○ Write automated tests ● Make components tests running against Fakes instead
  • 29.
  • 30.
  • 31.
    Patrocinadores “GOLD” Twitter: @PTMicrosofthttp://www.microsoft.com/portugal
  • 32.
  • 33.
  • 34.
  • 35.
    http://bit.ly/netponto-aval-76 * Para quemnão puder preencher durante a reunião, iremos enviar um email com o link à tarde
  • 36.
    Próximas reuniões presenciais 07/04/2018– Lisboa 23/06/2018 – Lisboa 15/09/2018 – Lisboa 24/11/2018 – Lisboa Reserva estes dias na agenda! :)