To add resiliency you are required to save a user's information in case the legacy system is down. You need to be able to handle restarts so this cache will need to be persistent.
You're going to use Cassandra as your persistence, so how do test this? You sit down your your analyst and QA and come up with the following feature:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Feature: Retrieve User Information | |
Scenario: User information is retrieved from the company wide repository | |
Given The company wide user repository is available | |
When User information is requested | |
Then The user information is returned | |
Scenario: Company wide information store is down | |
Given The company wide user repository is unavailable | |
When User information is requested | |
Then Unable to retrieve user information is returned | |
Scenario: Company wide information store is down but the information has been saved previously | |
Given The user had been retrieved previously | |
And The company wide user repository is unavailable | |
When User information is requested | |
Then The user information is returned |
For example here is how to mock the legacy system being up with Wiremock:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Given("^The company wide user repository is available$") | |
public void setupUserRepository() throws Throwable { | |
givenThat(get(urlEqualTo("/user/" + id)).willReturn( | |
aResponse().withBody("{\"firstName\":\"Chris\",\"lastName\":\"Batey\"}") | |
)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Given("^The company wide user repository is unavailable$") | |
public void userServiceDown() throws Throwable { | |
givenThat(get(urlEqualTo("/user/" + id)).willReturn( | |
aResponse().withStatus(500) | |
)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Scenario: Company wide information store is up but data store down | |
Given The data store is down | |
When User information is requested | |
Then The user information is returned | |
Scenario: Data store being slow should not cause slow down user transactions | |
Given The data store has problems writing | |
When User information is requested | |
Then The user information is returned |
That is where Stubbed Cassandra comes in handy. To get is started you can add some code like this to start it before the tests and close it after tests:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private final static Scassandra SCASSANDRA = ScassandraFactory.createServer(); | |
... | |
@Before | |
public void beforeScenario() { | |
SCASSANDRA.start(); | |
} | |
... | |
@After | |
public void after() { | |
SCASSANDRA.stop(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Given("^The data store is down$") | |
public void cassandraDown() throws Throwable { | |
SCASSANDRA.stop(); | |
} |
Now to mimic Write time outs in Cassandra:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Given("^The data store has problems writing$") | |
public void cassandraWriteTimeouts() throws Throwable { | |
PrimingRequest existingUser = PrimingRequest.queryBuilder() | |
.withQuery("insert into users(id, firstName, lastName) values ('chbatey','Chris','Batey')") | |
.withResult(PrimingRequest.Result.write_request_timeout) | |
.build(); | |
SCASSANDRA.primingClient().primeQuery(existingUser); | |
} |
This article gave you an insight to how you can behaviour drive features relating to Cassandra being down. The full code and running tests are here. Full information on how to use Stubbed Cassandra is here, you'll probably want to documentation for the Java client for Scassandra which is here.