use of com.axway.ats.action.dbaccess.snapshot.rules.SkipRows in project ats-framework by Axway.
the class DatabaseSnapshot method loadTableData.
/**
* Return list with all rows of some particular table
*
* @param snapshotName snapshot name
* @param table the table of question
* @param skipColumns skip rules
* @param skipRows rows to skip
* @param dbProvider DB connection to use
* @param backupXmlFile backup file to use
* @return
*/
List<String> loadTableData(String snapshotName, TableDescription table, Map<String, SkipColumns> skipColumns, Map<String, SkipRows> skipRows, DbProvider dbProvider, Document backupXmlFile) {
List<String> valuesList = new ArrayList<String>();
if (backupXmlFile == null) {
if (dbProvider == null) {
// DB provider not specified, use the one from this instance
dbProvider = this.dbProvider;
}
String sqlQuery = constructSelectStatement(table, skipColumns);
if (sqlQuery != null) {
for (DbRecordValuesList rowValues : dbProvider.select(sqlQuery)) {
// if there are rows for skipping we will find them and remove them from the list
String stringRowValue = rowValues.toString();
// escaping special characters that may
// cause some trouble while saving the snapshot into XML file
stringRowValue.replace("&", "&");
stringRowValue.replace("<", "<");
stringRowValue.replace(">", ">");
SkipRows skipRow = skipRows.get(table.getName().toLowerCase());
if (skipRow == null || !skipRow.skipRow(stringRowValue)) {
valuesList.add(stringRowValue);
}
}
log.debug("[" + snapshotName + "] Loaded " + valuesList.size() + " rows for table " + table.getName());
} else {
log.warn("[" + snapshotName + "] No data will be loaded for table " + table.getName() + " because all its columns are pointed to be skipped");
}
} else {
// load table row data from backup file
Element tableNode = loadTableNode(table, backupXmlFile);
List<Element> tableRows = DatabaseSnapshotUtils.getChildrenByTagName(tableNode, "row");
log.debug("[" + snapshotName + " from file] Loaded " + tableRows.size() + " rows for table " + table.getName());
for (Element tableRow : DatabaseSnapshotUtils.getChildrenByTagName(tableNode, "row")) {
valuesList.add(tableRow.getTextContent());
}
}
return valuesList;
}
use of com.axway.ats.action.dbaccess.snapshot.rules.SkipRows in project ats-framework by Axway.
the class DatabaseSnapshot method compare.
/**
* Compare both snapshots and throw error if unexpected differences are found.
* Snapshots are compared table by table.
* In the usual case the tables are loaded from database prior to comparing.
* But if a snapshot was saved into a file, then its tables are loaded from the file, not from the database.
*
* @param that the snapshot to compare to
* @param compareOptions - (optional) additional options that change the comparison. by default is null, so the compare is as-is (e.g. if there is an error, the comparison fails)
* @throws DatabaseSnapshotException
*/
@PublicAtsApi
public void compare(DatabaseSnapshot that, CompareOptions compareOptions) throws DatabaseSnapshotException {
try {
if (that == null) {
throw new DatabaseSnapshotException("Snapshot to compare is null");
}
if (this.name.equals(that.name)) {
throw new DatabaseSnapshotException("You are trying to compare snapshots with same name: " + this.name);
}
if (this.metadataTimestamp == -1) {
throw new DatabaseSnapshotException("You are trying to compare snapshots but [" + this.name + "] snapshot is still not created");
}
if (that.metadataTimestamp == -1) {
throw new DatabaseSnapshotException("You are trying to compare snapshots but [" + that.name + "] snapshot is still not created");
}
if (log.isDebugEnabled()) {
log.debug("Comparing snapshots [" + this.name + "] taken on " + DatabaseSnapshotUtils.dateToString(this.metadataTimestamp) + " and [" + that.name + "] taken on " + DatabaseSnapshotUtils.dateToString(that.metadataTimestamp));
}
this.equality = new DatabaseEqualityState(this.name, that.name);
// make copies of the table info, as we will remove from these lists,
// but we do not want to remove from the original table info lists
List<TableDescription> thisTables = new ArrayList<TableDescription>(this.tables);
List<TableDescription> thatTables = new ArrayList<TableDescription>(that.tables);
// Merge all skip rules from both snapshot instances,
// so it is not needed to add same skip rules in both snapshot instances.
// merge all columns to be skipped(per table)
Map<String, SkipColumns> skipColumns = mergeSkipColumns(that.skipColumnsPerTable);
// merge all content to be skipper(per table)
Map<String, SkipContent> skipContent = mergeSkipContent(that.skipContentPerTable);
// merge all rows to be skipped(per table)
Map<String, SkipRows> skipRows = mergeSkipRows(that.skipRowsPerTable);
Set<String> tablesToSkip = getAllTablesToSkip(skipColumns);
// We can use just one index name matcher
IndexMatcher actualIndexNameMatcher = mergeIndexMatchers(that.indexMatcher);
compareTables(this.name, thisTables, that.name, thatTables, that.dbProvider, tablesToSkip, skipColumns, skipContent, skipRows, actualIndexNameMatcher, that.backupXmlFile, equality);
if (compareOptions != null) {
try {
// handle expected differences
handleExpectedMissingRows(compareOptions, equality);
} catch (Exception e) {
log.error("Error occured while handling missing rows", e);
}
}
if (equality.hasDifferences()) {
// there are some unexpected differences
throw new DatabaseSnapshotException(equality);
} else {
log.info("Successful verification");
}
} finally {
// close the database connections
disconnect(this.dbProvider, "after comparing database snapshots");
disconnect(that.dbProvider, "after comparing database snapshots");
}
}
use of com.axway.ats.action.dbaccess.snapshot.rules.SkipRows in project ats-framework by Axway.
the class DatabaseSnapshot method skipTableRows.
/**
* Allows skipping rows in a table that contain some value at some column.
* <p>
* It causes checks on each row whether it matches the expected value at the specified column.
* On match, this row is not loaded from the database.
* <p>
* Note: you can use a regular expression for value to match.
*
* @param table the table
* @param column the column where the value will be searched
* @param value the value to match
*/
@PublicAtsApi
public void skipTableRows(String table, String column, String value) {
SkipRows skipRowsForThisTable = skipRowsPerTable.get(table.toLowerCase());
if (skipRowsForThisTable == null) {
skipRowsForThisTable = new SkipRows(table);
skipRowsPerTable.put(table.toLowerCase(), skipRowsForThisTable);
}
skipRowsForThisTable.addRowToSkip(column, value);
}
use of com.axway.ats.action.dbaccess.snapshot.rules.SkipRows in project ats-framework by Axway.
the class DatabaseSnapshot method mergeSkipRows.
private Map<String, SkipRows> mergeSkipRows(Map<String, SkipRows> thatSkipRowsPerTable) {
// we will return a new instance containing all rules
Map<String, SkipRows> allSkipRowsPerTable = new HashMap<>();
// first add all rules for this instance
for (String table : this.skipRowsPerTable.keySet()) {
allSkipRowsPerTable.put(table, this.skipRowsPerTable.get(table));
}
// then add all rules for the other instance
for (String table : thatSkipRowsPerTable.keySet()) {
SkipRows allSkipRows = allSkipRowsPerTable.get(table);
SkipRows thatSkipRows = thatSkipRowsPerTable.get(table);
if (allSkipRows != null) {
// there is already a rule for this table
// we have to merge both rules
allSkipRows.addRowsToSkip(thatSkipRows.getSkipExpressions());
} else {
// no current rule for this table, now we add one
allSkipRowsPerTable.put(table, thatSkipRows);
}
}
return allSkipRowsPerTable;
}
use of com.axway.ats.action.dbaccess.snapshot.rules.SkipRows in project ats-framework by Axway.
the class DatabaseSnapshotBackupUtils method loadFromFile.
/**
* Load a snapshot from a file
*
* @param newSnapshotName the name of the new snapshot
* @param snapshot the snapshot instance to fill with new data
* @param sourceFile the backup file name
* @return the XML document
*/
public Document loadFromFile(String newSnapshotName, DatabaseSnapshot snapshot, String sourceFile) {
log.info("Load database snapshot from file " + sourceFile + " - START");
// first clean up the current instance, in case some snapshot was taken before
snapshot.tables.clear();
Document doc;
try {
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(sourceFile));
doc.getDocumentElement().normalize();
} catch (Exception e) {
throw new DatabaseSnapshotException("Error reading database snapshot backup file " + sourceFile, e);
}
Element databaseNode = doc.getDocumentElement();
if (!DatabaseSnapshotUtils.NODE_DB_SNAPSHOT.equals(databaseNode.getNodeName())) {
throw new DatabaseSnapshotException("Bad backup file. Root node name is expeced to be '" + DatabaseSnapshotUtils.NODE_DB_SNAPSHOT + "', but it is '" + databaseNode.getNodeName() + "'");
}
if (StringUtils.isNullOrEmpty(newSnapshotName)) {
snapshot.name = databaseNode.getAttribute(DatabaseSnapshotUtils.ATTR_SNAPSHOT_NAME);
} else {
// user wants to change the snapshot name
snapshot.name = newSnapshotName;
}
// the timestamps
snapshot.metadataTimestamp = DatabaseSnapshotUtils.stringToDate(databaseNode.getAttribute(DatabaseSnapshotUtils.ATTR_METADATA_TIME));
snapshot.contentTimestamp = DatabaseSnapshotUtils.stringToDate(databaseNode.getAttribute(DatabaseSnapshotUtils.ATTR_CONTENT_TIME));
// the tables
List<Element> tableNodes = DatabaseSnapshotUtils.getChildrenByTagName(databaseNode, DatabaseSnapshotUtils.NODE_TABLE);
for (Element tableNode : tableNodes) {
snapshot.tables.add(TableDescription.fromXmlNode(snapshot.name, tableNode));
}
// any skip table content rules
snapshot.skipContentPerTable.clear();
List<Element> skipContentNodes = DatabaseSnapshotUtils.getChildrenByTagName(databaseNode, DatabaseSnapshotUtils.NODE_SKIP_CONTENT);
for (Element skipContentNode : skipContentNodes) {
SkipContent skipContent = SkipContent.fromXmlNode(skipContentNode);
snapshot.skipContentPerTable.put(skipContent.getTable().toLowerCase(), skipContent);
}
// any skip table column rules
snapshot.skipColumnsPerTable.clear();
List<Element> skipColumnNodes = DatabaseSnapshotUtils.getChildrenByTagName(databaseNode, DatabaseSnapshotUtils.NODE_SKIP_COLUMNS);
for (Element skipColumnNode : skipColumnNodes) {
SkipColumns skipColumns = SkipColumns.fromXmlNode(skipColumnNode);
snapshot.skipColumnsPerTable.put(skipColumns.getTable().toLowerCase(), skipColumns);
}
// any skip index attribute rules
snapshot.skipIndexAttributesPerTable.clear();
List<Element> skipIndexAttributesNodes = DatabaseSnapshotUtils.getChildrenByTagName(databaseNode, DatabaseSnapshotUtils.NODE_SKIP_INDEX_ATTRIBUTES);
for (Element skipIndexAttributesNode : skipIndexAttributesNodes) {
SkipIndexAttributes skipIndexAttributes = SkipIndexAttributes.fromXmlNode(skipIndexAttributesNode);
snapshot.skipIndexAttributesPerTable.put(skipIndexAttributes.getTable().toLowerCase(), skipIndexAttributes);
}
// any skip table row rules
snapshot.skipRowsPerTable.clear();
List<Element> skipRowNodes = DatabaseSnapshotUtils.getChildrenByTagName(databaseNode, DatabaseSnapshotUtils.NODE_SKIP_ROWS);
for (Element skipRowNode : skipRowNodes) {
SkipRows skipRows = SkipRows.fromXmlNode(skipRowNode);
snapshot.skipRowsPerTable.put(skipRows.getTable().toLowerCase(), skipRows);
}
log.info("Load database snapshot from file " + sourceFile + " - END");
return doc;
}
Aggregations