Search in sources :

Example 1 with DatabaseSnapshotException

use of com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException in project ats-framework by Axway.

the class DatabaseSnapshot method compare.

/**
     * Compare both snapshots
     * Both snapshots will be loaded from the database and compared table by table.
     * But if they were saved in files, they will be loaded from the files and compared table by table
     * 
     * @param that the snapshot to compare to
     * @throws DatabaseSnapshotException
     */
@PublicAtsApi
public void compare(DatabaseSnapshot that) 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");
        }
        log.debug("Comparing snapshots [" + this.name + "] taken on " + DatabaseSnapshotUtils.dateToString(this.metadataTimestamp) + " and [" + that.name + "] taken on " + DatabaseSnapshotUtils.dateToString(that.metadataTimestamp));
        this.equality = new EqualityState(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);
        // Here we put all skip rules into one list. It doesn't make sense to ask people 
        // to add same skip rules for same tables in both snapshot instances.
        Map<String, SkipRules> allSkipRules = mergeSkipRules(that.skipRulesPerTable);
        Set<String> allTablesContentSkip = mergeTableContentToSkip(that.skipTableContent);
        Map<String, Map<String, String>> allSkipRows = mergeSkipRows(that.skipRows);
        Set<String> allTablesToSkip = getAllTablesToSkip(allSkipRules);
        // We can use just one index name matcher
        IndexNameMatcher actualIndexNameMatcher = mergeIndexNameMatchers(that.indexNameMatcher);
        compareTables(this.name, thisTables, that.name, thatTables, that.dbProvider, allTablesToSkip, allSkipRules, allTablesContentSkip, allSkipRows, actualIndexNameMatcher, that.backupXmlFile, equality);
        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");
        if (that != null) {
            disconnect(that.dbProvider, "after comparing database snapshots");
        }
    }
}
Also used : EqualityState(com.axway.ats.common.dbaccess.snapshot.equality.EqualityState) ArrayList(java.util.ArrayList) IndexNameMatcher(com.axway.ats.common.dbaccess.snapshot.IndexNameMatcher) TableDescription(com.axway.ats.common.dbaccess.snapshot.TableDescription) HashMap(java.util.HashMap) Map(java.util.Map) DatabaseSnapshotException(com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 2 with DatabaseSnapshotException

use of com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException in project ats-framework by Axway.

the class DatabaseSnapshot method loadTableData.

List<String> loadTableData(String snapshotName, TableDescription table, Map<String, SkipRules> allSkipRules, Map<String, Map<String, String>> allSkipRows, DbProvider dbProvider, Document backupXmlFile) {
    if (dbProvider == null) {
        // DB provider not specified, use the one from this instance 
        dbProvider = this.dbProvider;
    }
    List<String> valuesList = new ArrayList<String>();
    if (backupXmlFile == null) {
        // load table row data from database
        String sqlQuery = construcSelectStatement(table, allSkipRules);
        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 a file
                stringRowValue.replace("&", "&amp;");
                stringRowValue.replace("<", "&lt;");
                stringRowValue.replace(">", "&gt;");
                DatabaseSnapshotBackupUtils dbUtil = new DatabaseSnapshotBackupUtils();
                if (allSkipRows != null && !dbUtil.skipRow(allSkipRows.get(table.getName()), stringRowValue)) {
                    valuesList.add(stringRowValue);
                }
            }
            log.debug("[" + snapshotName + " from database] Loaded " + valuesList.size() + " rows for table " + table.getName());
        } else {
            log.warn("Skip table " + table.getName() + " because of excluding all columns.");
        }
    } else {
        // load table row data from backup file
        List<Element> dbSnapshotNodeList = DatabaseSnapshotUtils.getChildrenByTagName(backupXmlFile, DatabaseSnapshotUtils.NODE_DB_SNAPSHOT);
        if (dbSnapshotNodeList.size() != 1) {
            throw new DatabaseSnapshotException("Bad dabase snapshot backup file. It must have 1 '" + DatabaseSnapshotUtils.NODE_DB_SNAPSHOT + "' node, but it has" + dbSnapshotNodeList.size());
        }
        for (Element tableNode : DatabaseSnapshotUtils.getChildrenByTagName(dbSnapshotNodeList.get(0), "TABLE")) {
            String tableName = tableNode.getAttribute("name");
            if (table.getName().equalsIgnoreCase(tableName)) {
                List<Element> tableRows = DatabaseSnapshotUtils.getChildrenByTagName(tableNode, "row");
                log.debug("[" + snapshotName + " from file] Loaded " + tableRows.size() + " rows for table " + tableName);
                for (Element tableRow : DatabaseSnapshotUtils.getChildrenByTagName(tableNode, "row")) {
                    valuesList.add(tableRow.getTextContent());
                }
                break;
            }
        }
    }
    return valuesList;
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) DatabaseSnapshotException(com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException)

Example 3 with DatabaseSnapshotException

use of com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException 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);
    }
    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));
    }
    log.info("Load database snapshot from file " + sourceFile + " - END");
    return doc;
}
Also used : Element(org.w3c.dom.Element) 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)

Example 4 with DatabaseSnapshotException

use of com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException in project ats-framework by Axway.

the class DatabaseSnapshotBackupUtils method saveToFile.

/**
     * Save a snapshot into a file
     * @param snapshot the snapshot to save
     * @param backupFile the backup file name
     * @return the XML document
     */
public Document saveToFile(DatabaseSnapshot snapshot, String backupFile) {
    log.info("Save database snapshot into file " + backupFile + " - START");
    // create the directory if does not exist
    File dirPath = new File(IoUtils.getFilePath(backupFile));
    if (!dirPath.exists()) {
        dirPath.mkdirs();
    }
    Document doc;
    try {
        doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (Exception e) {
        throw new DatabaseSnapshotException("Error creating DOM parser for " + backupFile, e);
    }
    // TODO - add DTD or schema for manual creation and easy validation
    Element dbNode = doc.createElement(DatabaseSnapshotUtils.NODE_DB_SNAPSHOT);
    dbNode.setAttribute(DatabaseSnapshotUtils.ATTR_SNAPSHOT_NAME, snapshot.name);
    // this timestamp comes when user takes a snapshot from database
    dbNode.setAttribute(DatabaseSnapshotUtils.ATTR_METADATA_TIME, DatabaseSnapshotUtils.dateToString(snapshot.metadataTimestamp));
    // this timestamp now
    snapshot.contentTimestamp = System.currentTimeMillis();
    dbNode.setAttribute(DatabaseSnapshotUtils.ATTR_CONTENT_TIME, DatabaseSnapshotUtils.dateToString(snapshot.contentTimestamp));
    doc.appendChild(dbNode);
    // append table descriptions
    for (TableDescription tableDescription : snapshot.tables) {
        Element tableNode = doc.createElement(DatabaseSnapshotUtils.NODE_TABLE);
        dbNode.appendChild(tableNode);
        tableDescription.toXmlNode(doc, tableNode);
        // if the current table is in the skipTableContent we will do not need to get its all data
        if (snapshot.skipTableContent.contains(tableDescription.getName())) {
            // no rows have to be saved, so we skip this iteration
            continue;
        }
        List<String> valuesList = new ArrayList<String>();
        valuesList.addAll(snapshot.loadTableData(snapshot.name, tableDescription, snapshot.skipRulesPerTable, snapshot.skipRows, null, null));
        for (String values : valuesList) {
            Element rowNode = doc.createElement(DatabaseSnapshotUtils.NODE_ROW);
            if (!skipRow(snapshot.skipRows.get(tableDescription.getName()), values)) {
                rowNode.setTextContent(values);
            }
            tableNode.appendChild(rowNode);
        }
    }
    // append skip rules
    for (String tableName : snapshot.skipRulesPerTable.keySet()) {
        Element skipRuleNode = doc.createElement(DatabaseSnapshotUtils.NODE_SKIP_RULE);
        dbNode.appendChild(skipRuleNode);
        skipRuleNode.setAttribute(DatabaseSnapshotUtils.ATT_SKIP_RULE_TABLE, tableName);
        snapshot.skipRulesPerTable.get(tableName).toXmlNode(doc, skipRuleNode);
    }
    // save the XML file
    try {
        OutputFormat format = new OutputFormat(doc);
        format.setIndenting(true);
        format.setIndent(4);
        format.setLineWidth(1000);
        XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File(backupFile)), format);
        serializer.serialize(doc);
    } catch (Exception e) {
        throw new DatabaseSnapshotException("Error saving " + backupFile, e);
    }
    log.info("Save database snapshot into file " + backupFile + " - END");
    return doc;
}
Also used : XMLSerializer(org.apache.xml.serialize.XMLSerializer) Element(org.w3c.dom.Element) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) OutputFormat(org.apache.xml.serialize.OutputFormat) Document(org.w3c.dom.Document) File(java.io.File) TableDescription(com.axway.ats.common.dbaccess.snapshot.TableDescription) DatabaseSnapshotException(com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException) DatabaseSnapshotException(com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException)

Aggregations

DatabaseSnapshotException (com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException)4 ArrayList (java.util.ArrayList)3 Element (org.w3c.dom.Element)3 TableDescription (com.axway.ats.common.dbaccess.snapshot.TableDescription)2 File (java.io.File)2 Document (org.w3c.dom.Document)2 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 IndexNameMatcher (com.axway.ats.common.dbaccess.snapshot.IndexNameMatcher)1 EqualityState (com.axway.ats.common.dbaccess.snapshot.equality.EqualityState)1 DbRecordValuesList (com.axway.ats.core.dbaccess.DbRecordValuesList)1 FileOutputStream (java.io.FileOutputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 OutputFormat (org.apache.xml.serialize.OutputFormat)1 XMLSerializer (org.apache.xml.serialize.XMLSerializer)1