Search in sources :

Example 1 with DatabaseEqualityState

use of com.axway.ats.common.dbaccess.snapshot.equality.DatabaseEqualityState 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");
    }
}
Also used : IndexMatcher(com.axway.ats.common.dbaccess.snapshot.IndexMatcher) SkipRows(com.axway.ats.action.dbaccess.snapshot.rules.SkipRows) SkipContent(com.axway.ats.action.dbaccess.snapshot.rules.SkipContent) ArrayList(java.util.ArrayList) TableDescription(com.axway.ats.common.dbaccess.snapshot.TableDescription) DatabaseSnapshotException(com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException) DatabaseEqualityState(com.axway.ats.common.dbaccess.snapshot.equality.DatabaseEqualityState) DatabaseSnapshotException(com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException) SkipColumns(com.axway.ats.action.dbaccess.snapshot.rules.SkipColumns) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Aggregations

SkipColumns (com.axway.ats.action.dbaccess.snapshot.rules.SkipColumns)1 SkipContent (com.axway.ats.action.dbaccess.snapshot.rules.SkipContent)1 SkipRows (com.axway.ats.action.dbaccess.snapshot.rules.SkipRows)1 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 DatabaseSnapshotException (com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException)1 IndexMatcher (com.axway.ats.common.dbaccess.snapshot.IndexMatcher)1 TableDescription (com.axway.ats.common.dbaccess.snapshot.TableDescription)1 DatabaseEqualityState (com.axway.ats.common.dbaccess.snapshot.equality.DatabaseEqualityState)1 ArrayList (java.util.ArrayList)1