Updating the automated tests of the microservice landscape
The automated tests of the microservice landscape, test-em-all.bash
, need to be updated so that they ensure that the database of each microservice has a known state before it runs the tests.
The script is extended with a setup function, setupTestdata()
, which uses the composite create and delete APIs to set up test data used by the tests.
The setupTestdata
function looks like this:
function setupTestdata() {
body=\
'{"productId":1,"name":"product 1","weight":1, "recommendations":[
{"recommendationId":1,"author":"author
1","rate":1,"content":"content 1"},
{"recommendationId":2,"author":"author
2","rate":2,"content":"content 2"},
{"recommendationId":3,"author":"author
3","rate":3,"content":"content 3"}
], "reviews":[
{"reviewId":1,"author":"author 1","subject":"subject
1","content":"content 1"},
{"reviewId":2,"author":"author 2","subject":"subject
2","content":"content 2"},
{"reviewId":3,"author":"author 3","subject":"subject
3","content":"content 3"}
]}'
recreateComposite 1 "$body"
body=\
'{"productId":113,"name":"product 113","weight":113, "reviews":[
{"reviewId":1,"author":"author 1","subject":"subject
1","content":"content 1"},
{"reviewId":2,"author":"author 2","subject":"subject
2","content":"content 2"},
{"reviewId":3,"author":"author 3","subject":"subject
3","content":"content 3"}
]}'
recreateComposite 113 "$body"
body=\
'{"productId":213,"name":"product 213","weight":213,
"recommendations":[
{"recommendationId":1,"author":"author
1","rate":1,"content":"content 1"},
{"recommendationId":2,"author":"author
2","rate":2,"content":"content 2"},
{"recommendationId":3,"author":"author
3","rate":3,"content":"content 3"}
]}'
recreateComposite 213 "$body"
}
It uses a helper function, recreateComposite()
, to perform the actual requests to the delete and create APIs:
function recreateComposite() {
local productId=$1
local composite=$2
assertCurl 200 "curl -X DELETE http://$HOST:$PORT/product-
composite/${productId} -s"
curl -X POST http://$HOST:$PORT/product-composite -H "Content-Type:
application/json" --data "$composite"
}
The setupTestdata
function is called directly after the waitForService
function:
waitForService curl -X DELETE http://$HOST:$PORT/product-composite/13
setupTestdata
The main purpose of the waitForService
function is to verify that all microservices are up and running. In the previous chapter, the get API on the composite product service was used. In this chapter, the delete API is used instead. When using the get API, only the product
core microservice is called if the entity is not found; the recommendation
and review
services will not be called to verify that they are up and running. The call to the delete API will also ensure that the Not Found test on productId 13
will succeed. In the next chapter, we will see how we can define specific APIs for checking the health state of a microservice landscape.
Execute the updated test script with the following command:
cd $BOOK_HOME/Chapter06
./test-em-all.bash start stop
The execution should end by writing a log message like this:
Figure 6.12: Log message at the end of test execution
This concludes the updates on the automated tests of the microservice landscape.