Skip to content

builder

Lukas Cardot edited this page Feb 16, 2020 · 3 revisions

Builder

To avoid redefining your configuration on every assertion, the library provides a builder in each of the modules to be able to configure the assertion class.

Assertj-db

import static com.ekino.oss.jcv.db.assertj.util.DBComparatorBuilder;

@Test
public void shouldVerifyData() {
    Source source = new Source("jdbc://localhost:5432/database-name", "username", "password")
    Table table = new Table(source, "table_test")
    assertThatTable(table).isValidAgainst(loadFile("table_currency_expected.json")); 
}

 public DbComparatorAssertJ assertThatTable(Table table) {
     return DbComparatorBuilder.create()
         .mode(JSONCompareMode.STRICT)
         .validators(customValidators())
         .mapper(DatabaseType.POSTGRESQL, CustomMapper())
         .build(table)
 }

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;
    }
}

private class CustomMapper extends PostgreMapper {
    @Override
    public String mapTextType(Value value) {
        return value.getValue() + " CUSTOM";
    }

JDBC

import static com.ekino.oss.jcv.db.jdbc.util.DBComparatorBuilder;

@Test
public void shouldVerifyData() {
    assertThatQuery("SELECT * FROM table_test")
        .isValidAgainst(loadFile("table_currency_expected.json"));
}

 public DbComparatorJDBC assertThatQuery(String query) {
     return DBComparatorBuilder.create()
         .mode(JSONCompareMode.STRICT)
         .validators(customValidators())
         .connection(DriverManager.getConnection("jdbc://localhost:5432/database-name", "username", "password"))
         .mapper(DatabaseType.POSTGRESQL, CustomMapper())
         .build(query)
 }

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;
    }
}

public class CustomMapper extends JDBCMapper {
    @Override
    public String mapString(String value) {
       return value + " CUSTOM";
    }
}

MongoDB

import static com.ekino.oss.jcv.db.mongo.util.DBComparatorBuilder;

@Test
public void shouldVerifyData() {
    assertThatCollection(mongoClient.getDatabase("database-name").getCollection("currency-collection").find())
        .isValidAgainst(loadFile("table_currency_expected.json"));
}

 public DbComparatorMongo assertThatCollection(Collection collection) {
     return DBComparatorBuilder.create()
         .mode(JSONCompareMode.STRICT)
         .validators(customValidators())
         .mapper(CustomMapper())
         .build(collection)
 }

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;
    }
}

private class CustomMapper implements TypeMapper {

    public Object getValueFromColumn(Object value) {
       return value.toString() + " CUSTOM";
    }
}

Cassandra

import static com.ekino.oss.jcv.db.cassandra.util.DBComparatorBuilder;

@Test
public void shouldVerifyData() {
    // CQL
    assertThatCollection("select * from cassandra_table_type")
         .isValidAgainst(loadFile("table_currency_expected.json"));
    
    // Query Builder
   assertThatCollection(QueryBuilder.selectFrom("cassandratest", "cassandra_table_type").all())
        .isValidAgainst(loadFile("table_currency_expected.json"));
}

 public DbComparatorCassandra assertThatCollection(Select select) {
     return DBComparatorBuilder.create()
         .mode(JSONCompareMode.STRICT)
         .validators(customValidators())
         .mapper(CustomMapper())
         .datasource(new CassandraDataSource("cassandra_dc_test", "localhost", 5425))
         .build(select)
 }

 public DbComparatorCassandra assertThatCollection(String cqlQuery) {
     return DBComparatorBuilder.create()
         .mode(JSONCompareMode.STRICT)
         .validators(customValidators())
         .mapper(CustomMapper())
         .datasource(new CassandraDataSource("cassandra_dc_test", "localhost", 5425))
         .build(cqlQuery)
 }

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;
    }
}

private class CustomMapper implements CassandraMapper {

    public String handleInetAdress(Object value) {
       return value.toString() + " CUSTOM INET ADDRESS";
    }
}
Clone this wiki locally