-
Notifications
You must be signed in to change notification settings - Fork 1
Validators
To fully understand the principle of validators I advise you to read the documentation of the JCV library. Some validators have already been created to facilitate assertion.
Allows you to check if the field value is a valid mongo id. This validator is specific to the mongo-db module.
Actual JSON :
{
"mongo_id_field": "5d8253faef748a2c0e53ebfd"
}
Expected JSON :
{
"mongo_id_field": "{#mongo_id#}"
}
Allows you to check if the field value is a valid json object. This validator is available for all modules.
Actual JSON :
{
"json_object_field": {
"name": "Nicolas"
}
}
Expected JSON :
{
"mongo_id_field": "{#json_object#}"
}
Allows you to check if the field value is a valid json array. This validator is available for all modules.
Actual JSON :
{
"json_array_field": [
{
"name": "Nicolas"
}
]
}
Expected JSON :
{
"mongo_id_field": "{#json_array#}"
}
In the same way as in JCV it is possible to define custom validators. To be able to use them there is a method in each of the modules that allows you to define the list of validators to use.
@Test
public void shouldVerifyData() {
Source source = new Source("jdbc://localhost:5432/database-name", "username", "password")
Table table = new Table(source, "table_test")
assertThatTable(table)
.using(customValidators())
.isValidAgainst(loadFile("table_currency_expected.json"));
}
private JsonValidator<String> customValidator(){
return templatedValidator("custom_not_empty", new NotEmptyComparator());
}
private List<JsonValidator> customValidators() {
List<JsonValidator> validators = new ArrayList<>();
validators.add(customValidator());
validators.addAll(DBValidators.defaultDBValidators());
return validators;
}
private class NotEmptyComparator implements JsonValueComparator<String> {
@Override
public boolean hasCorrectValue(String actual, String expected) {
if (actual == null || actual.isEmpty()) {
throw new ValueMatcherException("Value is null or empty", expected, actual);
}
return true;
}
}
@Test
public void shouldVerifyData() {
assertThatQuery("SELECT * FROM table_test")
.using(customValidators())
.isValidAgainst(loadFile("table_currency_expected.json"));
}
private JsonValidator<String> customValidator(){
return templatedValidator("custom_not_empty", new NotEmptyComparator());
}
private List<JsonValidator> customValidators() {
List<JsonValidator> validators = new ArrayList<>();
validators.add(customValidator());
validators.addAll(DBValidators.defaultDBValidators());
return validators;
}
private class NotEmptyComparator implements JsonValueComparator<String> {
@Override
public boolean hasCorrectValue(String actual, String expected) {
if (actual == null || actual.isEmpty()) {
throw new ValueMatcherException("Value is null or empty", expected, actual);
}
return true;
}
}
@Test
public void shouldVerifyData() {
assertThatCollection(mongoClient.getDatabase("database-name").getCollection("currency-collection").find())
.using(customValidators())
.isValidAgainst(loadFile("table_currency_expected.json"));
}
private JsonValidator<String> customValidator(){
return templatedValidator("custom_not_empty", new NotEmptyComparator());
}
private List<JsonValidator> customValidators() {
List<JsonValidator> validators = new ArrayList<>();
validators.add(customValidator());
validators.addAll(DBMongoValidators.defaultDBValidators());
return validators;
}
private class NotEmptyComparator implements JsonValueComparator<String> {
@Override
public boolean hasCorrectValue(String actual, String expected) {
if (actual == null || actual.isEmpty()) {
throw new ValueMatcherException("Value is null or empty", expected, actual);
}
return true;
}
}
@Test
public void shouldVerifyData() {
// CQL
assertThatCollection("select * from cassandra_table_type")
.using(customValidators())
.isValidAgainst(loadFile("table_currency_expected.json"));
// Query Builder
assertThatCollection(QueryBuilder.selectFrom("cassandratest", "cassandra_table_type").all())
.using(customValidators())
.isValidAgainst(loadFile("table_currency_expected.json"));
}
private JsonValidator<String> customValidator(){
return templatedValidator("custom_not_empty", new NotEmptyComparator());
}
private List<JsonValidator> customValidators() {
List<JsonValidator> validators = new ArrayList<>();
validators.add(customValidator());
validators.addAll(DBValidators.defaultDBValidators());
return validators;
}
private class NotEmptyComparator implements JsonValueComparator<String> {
@Override
public boolean hasCorrectValue(String actual, String expected) {
if (actual == null || actual.isEmpty()) {
throw new ValueMatcherException("Value is null or empty", expected, actual);
}
return true;
}
}