Automated Testing is a key element in a micro IT company with scarce resources.
customer,user etc.) which then are loaded into the database-For each business domain object which needs test data a ‘Plain Old Java Object? (POJO) must be defined. The definition of a business domain POJO is straightforward and and requires the following steps
getand putmethod which are provided by underlying com.mongodb.BasicDBObject parent class.getCSVCellProcessormethod which will return the cell processor required when extracting the data out of a CSV file.getDBCollectionNamemethod which will return the collection/table name into wich the business domain POJO will be stored in the DB.The code sample looks like follows:
public class CustomerDBObject extends DomainDBObject {
// The CSV Cell Processor
private static CellProcessor[] csvCellProcessor = new CellProcessor[] {
new StrMinMax(1,40), // Expect at least a name with 1-40 characters
new StrMinMax(1,40), // Expect at least a surname with 1-40 characters
null,
null,
null,
null,
null,
null,
null,
new ParseDate("dd.MM.yyyy")
};
public CellProcessor[] getCSVCellProcessor() { return csvCellProcessor;
}
public String getDBCollectionName() {
return "customers";
}
public void setName(String name) {
this.put("name", name);
}
public String getName() {
return (String)this.get("name");
}
public void setSurname(String surname) {
this.put("surname", surname);
}
The DomainDBObjectis an abstract class which is inherited from the com.mongodb.BasicDBObject and which will ensure that the busienss domain POJO’s will provide a org.supercsv.cellprocessor.ift.CellProcessorwhich is required when the test data is loaded from CSV files, as well as the DB collection-/table name.
public abstract class DomainDBObject extends BasicDBObject {
public CellProcessor[] csvCellProcessor;
public abstract CellProcessor[] getCSVCellProcessor();
public abstract String getDBCollectionName();
public DomainDBObject() {
this.csvCellProcessor = getCSVCellProcessor();
}
}
Test data files are defined per business domain POJO type (e.g. com.cloudburo.test.dataobj.CustomerDBObject) and test set (e.g. fullregression), as for example
com.cloudburo.test.dataobj.CustomerDBObject-fullregression.csvcom.cloudburo.test.dataobj.CustomerDBObject-smallcheck.csvThe files will be loaded when the corresponding test case is executed (during the test case setup phase)
The CSV data loader part is a generic method, which will make sure that the data are loaded in our Mongo based POJO domain objects, which can be persisted in the DB in a subsequent processin step.
public List<DBObject> loadTestDataSet(String set, DomainDBObject dboClass) throws Exception {
List<DBObject> list = new ArrayList<DBObject>();
ICsvBeanReader inFile = new CsvBeanReader(new FileReader("testdata/"+dboClass.getClass().getName()+"-" +set+".csv"), CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);
try {
final String[] header = inFile.getCSVHeader(true);
DBObject dbo;
while( (dbo = inFile.read(dboClass.getClass(), header, dboClass.getCSVCellProcessor())) != null) {
list.add(dbo);
}
} finally {
inFile.close();
}
return list;
}
The CsvBeanReader will extract each row and create the corresponding DomainDBObject (in this example the CustomerDBObject) and passes in the cell data to the attributes as defined in the header of the file.
name;surname;address;plz;location;telephone;mobile;email;citizenship;birthdate
Küstahler;Felix;Zumikerstrasse;8700;Küsnacht;449123567;796249158;[email protected];Swiss;17.05.1965
Meierhans;Albert;Bergstrasse 5;8706;Meilen;449123056;796229108;[email protected];USA;22.08.1996