Skip to content
Lukas Cardot edited this page Feb 16, 2020 · 6 revisions

What is JCV-DB ?

JCV-DB is a test library that validates the content of a database. It uses the JCV library allowing you to validate JSON content by encapsulating validators inside the reference JSON. It supports both SQL and No-SQL databases such as PostgreSQL, MySQL, MSSQL, Cassandra and MongoDB. You will be able to use the validators already defined in JCV plus those which have been defined for specific databases to validate database content.

What problem does JCV-DB solve ?

During a test that can modify the data in the database, we get the following steps:

  • The method we want to test is called and makes a modification in one or more databases.
  • We check whether the returned value is correct.
  • We check the modified value in the database.

If we focus on this last step, which is the one we are interested in, it may be interesting to check it in its entirety. There are several ways to do this, which is up to the developers, such as libraries (Assertj-db, dbunit, ...) or use the DAOs defined in your application.

Imagine for example, we have a Currency table with different information and a REST API that allows you to add a currency to this table (available on "http://localhost:8080/currencies" POST). Here is an example of a possible table:

id code label created_date created_by version
UUID String String Date String Integer
90c659f0-a16e-41a6-a38e-3c67d7b3c600 EUR euro 2019-11-01T00:00:00Z username 1

Here is a test to verify that, when adding a currency, the data returned by the REST API (using the JCV library) and the database data are correct (using the assertj-DB library).

@Test
@DisplayName("Should create a currency")
void testCreate() throws IOException {
    when()
        .contentType("application/json")
        .body(loadFile("currency_create.json"))
        .post("/currencies")
        .assertThat()
        .statusCode(201)
        .body("label", equalTo("label"), 
            "code", equalTo("code"))
    ;
    
    assertTable()
        .column("id").hasOnlyNotNullValues()
        .column("code").containsValues("EUR", "USD")
        .column("label").containsValues("euro", "Dollar Americano")
    ;
}

And the file currency_create.json:

{
  "code": "USD",
  "label": "Dollar Americano"
}

We should therefore have the following data in our table:

id code label created_date created_by version
UUID String String Date String Integer
90c659f0-a16e-41a6-a38e-3c67d7b3c600 EUR euro 2019-11-01T00:00:00Z username 1
40b09a91-b226-4dc5-90cb-fba012197557 USD Dollar Americano 2019-11-01T00:00:00Z username 1

In this case we can observe that we call the endpoint which is supposed to modify the data in the database. However, the above test does not allow us to check all the data, and we are in a simple case which can quickly become more complicated if we have several modified tables or that particular types are returned to us such as JSON fields or geometric fields.

This method can therefore work but may generate some issues:

  • The test may be difficult for another developer or even the developer himself to read again.
  • Not all values are verified and it may be difficult to identify the fields that have not been verified.
  • If we decide to check the data completely it can take a long time to write and most libraries do not support the so-called special types (Geometry and JSON).

How can jcv-db help you ?

There are a total of five modules, each of which is useful:

  • jcv-db-assertj-db: a module that supports the module assertj-db and allows to validate the content of a table assertj-db with a JSON file.
  • jcv-db-jdbc: a module that allows you to execute SQL queries and validate the return of the query with a JSON file.
  • jcv-db-cassandra: an exclusive module that allows you to validate the return of requests made on a No-SQL database Cassandra. It supports CQL requests and Java Query Builder client.
  • jcv-db-mongo: an exclusive module that validates the content of a No-SQL Mongo DB database. It supports only Java mongoDB client.
  • jcv-db-core: a common module to all the others which allows to validate the model extracted from the different databases against the JSON file.
Clone this wiki locally