-
Notifications
You must be signed in to change notification settings - Fork 1
mapper
Mappers are classes that allow you to receive the data from the database and transform it into a format that can be used in a JSON file. There is a mapper implemented in each database module. It is possible for you to modify the existing mappers or create new ones if the desired database is not supported by the library.
The assertj-db library already allows us to split the data received into large type groups:
- UUID
- BOOLEAN
- NUMBER
- TEXT
- DATE_TIME
- TIME
- BYTES
- NOT_IDENTIFIED
To be able to create a new mapper you have to inherit the class AssertJBaseMapper
and override the available methods. By default, the values will be returned in their raw form, without processing.
In order to use a new mapper in your assertion, you must use the utility methods available to you.
Source source = new Source("jdbc://localhost:5432/database-name", "username", "password")
Table table = new Table(source, "table_test")
assertThatTable(table)
.using(DatabaseType.POSTGRESQL, new CustomMapper())
.isValidAgainst(loadFile("table_currency_expected.json"));
public class CustomMapper extends AssertJBaseMapper {
@Override
public String mapTextType(@NotNull Value value) {
return value.getValue() + " CUSTOM";
}
}
You can rewrite the existing configuration of a mapper as follows:
Source source = new Source("jdbc://localhost:5432/database-name", "username", "password")
Table table = new Table(source, "table_test")
assertThatTable(table)
.using(DatabaseType.POSTGRESQL, new CustomMapper())
.isValidAgainst(loadFile("table_currency_expected.json"));
public class CustomMapper extends PostgresMapper {
@Override
public String mapTextType(Value value) {
return value.getValue() + " CUSTOM";
}
}
Here are the predefined mappers available:
-
PostgresMapper
for PostgreSQL -
MySQLMapper
for MySQL -
MSSQLMapper
for MSSQL
In JDBC module, we split the data in six groups:
- Number
- Boolean
- String
- ByteArray
- UUID
- Other
To be able to create a new mapper and support a new database, you have to inherit the class JDBCMapper
and override the necessary methods.
By default, the values will be returned in their raw form, without processing. To be able to use a new mapper in your assertions, you must use the utility methods available to you.
assertThatQuery("SELECT * FROM table_test")
.using(DriverManager.getConnection("jdbc://localhost:5432/database-name", "username", "password"))
.using(DatabaseType.POSTGRESQL, new CustomMapper())
.isValidAgainst(loadFile("table_currency_expected.json"));
public class CustomMapper extends JDBCMapper {
@Override
public String mapString(String value) {
return value + " CUSTOM";
}
}
You can rewrite the existing configuration of a mapper as follows:
assertThatQuery("SELECT * FROM table_test")
.using(DriverManager.getConnection("jdbc://localhost:5432/database-name", "username", "password"))
.using(DatabaseType.POSTGRESQL, new CustomMapper())
.isValidAgainst(loadFile("table_currency_expected.json"));
public class CustomMapper extends PostgresMapper {
@Override
public String mapString(String value) {
return value + " CUSTOM";
}
}
Here are the predefined mappers available:
-
PostgresMapper
for PostgreSQL -
MySQLMapper
for MySQL -
MSSQLMapper
for MSSQL
For the MongoDB module, there is one defined mapper: MongoMapper
.
To be able to create a new mapping configuration you will need to implement the TypeMapper
interface. To define a new mapping, you must use the utility methods at your disposal:
assertThatCollection(mongoClient.getDatabase("database-name").getCollection("currency-collection").find())
.using(new CustomMapper())
.isValidAgainst(loadFile("table_currency_expected.json"));
public class CustomMapper implements TypeMapper {
public Object getValueFromColumn(Object value) {
return value.toString() + " CUSTOM";
}
}
For the Cassandra module, there is one defined mapper: CassandraMapper
.
To be able to create a new configuration you must inherit the class CassandraMapper
if you want to partially override the existing configuration as follows:
CQL:
assertThatCollection("select * from cassandra_table_type")
.using(new CassandraDataSource("cassandra_dc_test", "localhost", 5425))
.using(new CustomMapper())
.isValidAgainst(loadFile("table_currency_expected.json"));
public class CustomMapper implements CassandraMapper {
public String handleInetAdress(Object value) {
return value.toString() + " CUSTOM INET ADDRESS"";
}
}
Query Builder:
assertThatCollection(QueryBuilder.selectFrom("cassandratest", "cassandra_table_type").all())
.using(new CassandraDataSource("cassandra_dc_test", "localhost", 5425))
.using(new CustomMapper())
.isValidAgainst(loadFile("table_currency_expected.json"));
public class CustomMapper implements CassandraMapper {
public String handleInetAdress(Object value) {
return value.toString() + " CUSTOM INET ADDRESS";
}
}
It is possible to create your own configuration by implementing the TypeMapper
interface.