Draft! This page is a draft - a work in progress!

Simple microservice stories Subscribe

We've seen The simplest microservice you ever created++ - let's look at slightly more complex mocks and how can we specify requirements and acceptance criteria, tests etc.

A blog API

Here is an example of a full blown implementation for a 3 method API for a "blog" service, in Lagom and Java - browse it for a couple of minutes.

That's after setting up an sbt project (easy to setup if you use the templates) as well as JVM, scala etc... but let's see what we can do here, in about 1 minute... maybe this three-liner is enough to prototype things:

$mock blog.addPost(body) => (id="123")
$mock blog.getPost(id) => (post="This is a blog post id="+id)
$mock blog.updatePost(id, body) => (post=body+id)

//There, it's mocked and already up and running. Let's define some tests for it now... another 2 minutes:

$receive blog.addPost (body="This is a blog post")
$expect (id ~= "[0-9]+")

$receive blog.getPost (id="3")
$expect (post ~= ".*3")

$receive blog.updatePost (id="4",body="new body")
$expect (post ~= "new.*4")

Done. We have very clear expectations, which have already become tests, as well as simple but explicit mocks of a full component.

Click on the "Trace" tab and notice the green lights as the tests are successful (since they run against the mocks just above).

State

It's true - the bigger example persists the posts while we do not - we just simulate them. Let's fix that:

$mock blog2.addPost(id,body) => simdb.set (table="Posts", id=id, entity=body)
$mock blog2.getPost(id) => simdb.get(table="Posts", id)
$mock blog2.updatePost(id, body) => simdb.set (table="Posts", id=id, entity=body)

//There, it's mocked and already up and running. Let's define some tests for it now... another 2 minutes:

$receive blog2.addPost (id="3", body="This is a blog post")
$expect (id ~= "[0-9a-z]+")

$receive blog2.getPost (id="3")
$expect (entity ~= ".*post.*")

$receive blog2.updatePost (id="3",body="new body")
$receive blog2.getPost (id="3")
$expect (entity ~= "new.*")

$receive simdb.clear

By: Razie | 2017-04-14 | Tags: post , microservices , reactive , dsl |    


See more in: Cool Scala Subscribe

Viewed 1 times ( | Print ) this page.


This content is Copyrighted, all rights reserved.

You need to log in to post a comment!