Search in sources :

Example 1 with SkipRows

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("&", "&amp;");
                stringRowValue.replace("<", "&lt;");
                stringRowValue.replace(">", "&gt;");
                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;
}
Also used : SkipRows(com.axway.ats.action.dbaccess.snapshot.rules.SkipRows) DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList)

Example 2 with SkipRows

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

Example 3 with SkipRows

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);
}
Also used : SkipRows(com.axway.ats.action.dbaccess.snapshot.rules.SkipRows) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 4 with SkipRows

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;
}
Also used : SkipRows(com.axway.ats.action.dbaccess.snapshot.rules.SkipRows) HashMap(java.util.HashMap)

Example 5 with SkipRows

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;
}
Also used : SkipIndexAttributes(com.axway.ats.action.dbaccess.snapshot.rules.SkipIndexAttributes) SkipRows(com.axway.ats.action.dbaccess.snapshot.rules.SkipRows) Element(org.w3c.dom.Element) SkipContent(com.axway.ats.action.dbaccess.snapshot.rules.SkipContent) Document(org.w3c.dom.Document) File(java.io.File) DatabaseSnapshotException(com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException) DatabaseSnapshotException(com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException) SkipColumns(com.axway.ats.action.dbaccess.snapshot.rules.SkipColumns)

Aggregations

SkipRows (com.axway.ats.action.dbaccess.snapshot.rules.SkipRows)6 SkipColumns (com.axway.ats.action.dbaccess.snapshot.rules.SkipColumns)3 SkipContent (com.axway.ats.action.dbaccess.snapshot.rules.SkipContent)3 DatabaseSnapshotException (com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException)3 Element (org.w3c.dom.Element)3 SkipIndexAttributes (com.axway.ats.action.dbaccess.snapshot.rules.SkipIndexAttributes)2 PublicAtsApi (com.axway.ats.common.PublicAtsApi)2 TableDescription (com.axway.ats.common.dbaccess.snapshot.TableDescription)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Document (org.w3c.dom.Document)2 IndexMatcher (com.axway.ats.common.dbaccess.snapshot.IndexMatcher)1 DatabaseEqualityState (com.axway.ats.common.dbaccess.snapshot.equality.DatabaseEqualityState)1 DbRecordValuesList (com.axway.ats.core.dbaccess.DbRecordValuesList)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 OutputFormat (org.apache.xml.serialize.OutputFormat)1 XMLSerializer (org.apache.xml.serialize.XMLSerializer)1