Search in sources :

Example 1 with ActiveDbAppender

use of com.axway.ats.log.appenders.ActiveDbAppender in project ats-framework by Axway.

the class AtsDbLogger method getCurrentTestCaseState.

/**
     * This event can not go through the regular way of sending log4j events in the case with Passive DB appenders. 
     * The reason is that we have to evaluate the result after the work of each passive appender and stop
     * calling these appenders when the first one(the only one serving this caller) has processed the event. 
     */
@SuppressWarnings("unchecked")
public TestCaseState getCurrentTestCaseState() {
    GetCurrentTestCaseEvent event = new GetCurrentTestCaseEvent(ATS_DB_LOGGER_CLASS_NAME, logger);
    Enumeration<Appender> appenders = Logger.getRootLogger().getAllAppenders();
    while (appenders.hasMoreElements()) {
        Appender appender = appenders.nextElement();
        if (appender instanceof ActiveDbAppender) {
            // Comes here on Test Executor side. There is just 1 Active appender
            return ((ActiveDbAppender) appender).getCurrentTestCaseState(event).getTestCaseState();
        } else if (appender instanceof PassiveDbAppender) {
            // Comes here on Agent side. There will be 1 Passive appender per caller
            // Pass the event to any existing appender.
            // The correct one will return result, wrong appenders will return null.
            GetCurrentTestCaseEvent resultEvent = ((PassiveDbAppender) appender).getCurrentTestCaseState(event);
            if (resultEvent != null) {
                // we found the right Passive appender
                return resultEvent.getTestCaseState();
            }
        }
    }
    // no appropriate appender found
    return null;
}
Also used : ActiveDbAppender(com.axway.ats.log.appenders.ActiveDbAppender) PassiveDbAppender(com.axway.ats.log.appenders.PassiveDbAppender) Appender(org.apache.log4j.Appender) ActiveDbAppender(com.axway.ats.log.appenders.ActiveDbAppender) GetCurrentTestCaseEvent(com.axway.ats.log.autodb.events.GetCurrentTestCaseEvent) PassiveDbAppender(com.axway.ats.log.appenders.PassiveDbAppender)

Example 2 with ActiveDbAppender

use of com.axway.ats.log.appenders.ActiveDbAppender in project ats-framework by Axway.

the class AtsDbLoggerUtilities method attachFileToCurrentTest.

/**
     * Attach a local file to the current test case in the Test Explorer DB.
     * </br>The file must not be bigger than 10MB
     * 
     * @param fileLocation the absolute path to the file
     * @param testExplorerContextName the name of the web application, e.g. "TestExplorer" or "TestExplorer-3.11.0" etc.
     * @param testExplorerPort the port of the web application, e.g. 8080
     * @return TRUE if the operation was successful and false if not. A warning will be logged on failure.
     */
@PublicAtsApi
public boolean attachFileToCurrentTest(String fileLocation, String testExplorerContextName, int testExplorerPort) {
    ERR_MSG_PREFIX = ERR_MSG_PREFIX.replace("{FILE}", fileLocation);
    if (!checkFileExist(fileLocation)) {
        return false;
    }
    if (!checkFileSizeIsNotTooLarge(fileLocation)) {
        return false;
    }
    ActiveDbAppender dbAppender = ActiveDbAppender.getCurrentInstance();
    if (dbAppender == null) {
        logger.warn(ERR_MSG_PREFIX + "Perhaps the database logging is turned off");
        return false;
    }
    final int runId = dbAppender.getRunId();
    final int suiteId = dbAppender.getSuiteId();
    final int testcaseId = dbAppender.getTestCaseId();
    if (runId < 1 || suiteId < 1 || testcaseId < 1) {
        logger.warn(ERR_MSG_PREFIX + "Perhaps the database logging is turned off or you are trying to log while a testcase is not yet started");
        return false;
    }
    final String database = dbAppender.getDatabase();
    final String host = dbAppender.getHost();
    final String URL = "http://" + host + ":" + testExplorerPort + "/" + testExplorerContextName + "/UploadServlet";
    URL url = null;
    try {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(URL);
        url = post.getURI().toURL();
        if (!isURLConnetionAvailable(url)) {
            return false;
        }
        logger.debug("POSTing " + fileLocation + " on " + URL);
        File file = new File(fileLocation);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.addBinaryBody("upfile", file, ContentType.DEFAULT_BINARY, fileLocation);
        builder.addTextBody("dbName", database);
        builder.addTextBody("runId", Integer.toString(runId));
        builder.addTextBody("suiteId", Integer.toString(suiteId));
        builder.addTextBody("testcaseId", Integer.toString(testcaseId));
        HttpEntity entity = builder.build();
        post.setEntity(entity);
        checkPostExecutedSuccessfully(client.execute(post), fileLocation);
    } catch (FileNotFoundException fnfe) {
        logger.warn(ERR_MSG_PREFIX + "it does not exist on the local file system", fnfe);
        return false;
    } catch (ClientProtocolException cpe) {
        logger.warn(ERR_MSG_PREFIX + "Upload to \"" + url + "\" failed", cpe);
        return false;
    } catch (ConnectException ce) {
        logger.warn(ERR_MSG_PREFIX + "Upload to \"" + url + "\" failed", ce);
        return false;
    } catch (IOException ioe) {
        logger.warn(ERR_MSG_PREFIX + "Upload to \"" + url + "\" failed", ioe);
        return false;
    }
    logger.info("Successfully attached \"" + fileLocation + "\" to the current Test Explorer testcase");
    return true;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) HttpEntity(org.apache.http.HttpEntity) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) URL(java.net.URL) ClientProtocolException(org.apache.http.client.ClientProtocolException) ActiveDbAppender(com.axway.ats.log.appenders.ActiveDbAppender) File(java.io.File) ConnectException(java.net.ConnectException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 3 with ActiveDbAppender

use of com.axway.ats.log.appenders.ActiveDbAppender in project ats-framework by Axway.

the class AutoLogger method getCurrentTestCaseState.

/**
     * This event can not go through the regular way of sending log4j events in the case with Passive DB appenders. 
     * The reason is that we have to evaluate the result after the work of each passive appender and stop
     * calling these appenders when the first one(the only one serving this caller) has processed the event. 
     */
@SuppressWarnings("unchecked")
public TestCaseState getCurrentTestCaseState() {
    GetCurrentTestCaseEvent event = new GetCurrentTestCaseEvent(AUTO_LOGGER_CLASS_NAME, logger);
    Enumeration<Appender> appenders = Logger.getRootLogger().getAllAppenders();
    while (appenders.hasMoreElements()) {
        Appender appender = appenders.nextElement();
        if (appender instanceof ActiveDbAppender) {
            // Comes here on Test Executor side. There is just 1 Active appender
            return ((ActiveDbAppender) appender).getCurrentTestCaseState(event).getTestCaseState();
        } else if (appender instanceof PassiveDbAppender) {
            // Comes here on Agent side. There will be 1 Passive appender per caller
            // Pass the event to any existing appender.
            // The correct one will return result, wrong appenders will return null.
            GetCurrentTestCaseEvent resultEvent = ((PassiveDbAppender) appender).getCurrentTestCaseState(event);
            if (resultEvent != null) {
                // we found the right Passive appender
                return resultEvent.getTestCaseState();
            }
        }
    }
    // no appropriate appender found
    return null;
}
Also used : ActiveDbAppender(com.axway.ats.log.appenders.ActiveDbAppender) PassiveDbAppender(com.axway.ats.log.appenders.PassiveDbAppender) Appender(org.apache.log4j.Appender) ActiveDbAppender(com.axway.ats.log.appenders.ActiveDbAppender) GetCurrentTestCaseEvent(com.axway.ats.log.autodb.events.GetCurrentTestCaseEvent) PassiveDbAppender(com.axway.ats.log.appenders.PassiveDbAppender)

Example 4 with ActiveDbAppender

use of com.axway.ats.log.appenders.ActiveDbAppender in project ats-framework by Axway.

the class ReportFormatter method getContentAsHTMLTable.

/**
     * create just the html table with the test data for the report
     */
public String getContentAsHTMLTable() {
    StringBuilder tableBody = new StringBuilder();
    tableBody.append("<table width=100% border=0 cellspacing=1 cellpadding=5>");
    for (RunWrapper run : runs) {
        // START - Run header
        tableBody.append(START_TBL_HEADERROW);
        tableBody.append(START_TBL_CELL_TWO_ROWS + "Run" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL_TWO_ROWS + "Product" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL_TWO_ROWS + "Version" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL_TWO_ROWS + "Build" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL_TWO_ROWS + "OS" + END_TBL_CELL);
        tableBody.append("<td colspan=4 align=center>" + "Scenarios" + END_TBL_CELL);
        tableBody.append("<td colspan=3 align=center>" + "Test cases" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL_TWO_ROWS + "Start" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL_TWO_ROWS + "End" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL_TWO_ROWS + "Duration" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL_TWO_ROWS + "User note" + END_TBL_CELL);
        tableBody.append(END_TBL_ROW);
        tableBody.append(START_TBL_HEADERROW);
        tableBody.append(START_TBL_CELL + "total" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + "%&nbsp;passed" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + "failed" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + "skipped" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + "total" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + "%&nbsp;passed" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + "failed" + END_TBL_CELL);
        tableBody.append(END_TBL_ROW);
        // START - Run data
        if (run.scenariosFailed + run.scenariosSkipped + run.testcasesFailed > 0) {
            tableBody.append(START_TBL_ROW_FAILED_RUN);
        } else {
            tableBody.append(START_TBL_ROW_PASSED_RUN);
        }
        if (this.dbHost != null) {
            if (testExplorerWebPort > 0) {
                testExplorerURL = "http://" + dbHost + ":" + testExplorerWebPort;
            } else {
                testExplorerURL = "http://" + dbHost;
            }
            testExplorerURL += "/" + testExplorerPath + "/suites?runId=" + run.runId + "&dbname=" + dbName;
        } else {
            ActiveDbAppender dbAppender = ActiveDbAppender.getCurrentInstance();
            ReportConfigurator reportConfigurator = ReportConfigurator.getInstance();
            if (dbAppender != null && reportConfigurator.getTestExplorerWebPath() != null) {
                if (reportConfigurator.getTestExplorerWebPort() != null) {
                    testExplorerURL = "http://" + dbAppender.getHost() + ":" + reportConfigurator.getTestExplorerWebPort();
                } else {
                    testExplorerURL = "http://" + dbAppender.getHost();
                }
                testExplorerURL += "/" + reportConfigurator.getTestExplorerWebPath() + "/suites?runId=" + dbAppender.getRunId() + "&dbname=" + dbAppender.getDatabase();
            }
        }
        if (testExplorerURL != null) {
            tableBody.append(START_TBL_CELL + "<a href =\"" + testExplorerURL + "\">" + run.runName + "</a>" + END_TBL_CELL);
        } else {
            tableBody.append(START_TBL_CELL + run.runName + END_TBL_CELL);
        }
        tableBody.append(START_TBL_CELL + run.productName + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + run.versionName + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + run.buildName + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + run.os + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + run.scenariosTotal + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + (run.scenariosTotal - run.scenariosFailed - run.scenariosSkipped) * 100 / run.scenariosTotal + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + run.scenariosFailed + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + run.scenariosSkipped + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + run.testcasesTotal + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + run.testcasesPassedPercent + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + run.testcasesFailed + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + run.dateStart + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + run.dateEnd + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + run.duration + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + (run.userNote == null ? "" : run.userNote) + END_TBL_CELL);
        tableBody.append(END_TBL_ROW);
        // END - Run data
        // put some space between run and its suites
        tableBody.append(EMPTY_TBL_ROW);
        // START - Suites header
        tableBody.append(START_TBL_HEADERROW);
        tableBody.append(START_TBL_CELL_TWO_ROWS + "Suites" + END_TBL_CELL);
        tableBody.append("<td colspan=4 rowspan=2 align=center>" + END_TBL_CELL);
        tableBody.append("<td colspan=4 align=center>" + "Scenarios" + END_TBL_CELL);
        tableBody.append("<td colspan=3 align=center>" + "Test cases" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL_TWO_ROWS + "Start" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL_TWO_ROWS + "End" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL_TWO_ROWS + "Duration" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL_TWO_ROWS + "User note" + END_TBL_CELL);
        tableBody.append(END_TBL_ROW);
        tableBody.append(START_TBL_HEADERROW);
        tableBody.append(START_TBL_CELL + "total" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + "%&nbsp;passed" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + "failed" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + "skipped" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + "total" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + "%&nbsp;passed" + END_TBL_CELL);
        tableBody.append(START_TBL_CELL + "failed" + END_TBL_CELL);
        tableBody.append(END_TBL_ROW);
        // END - Suites header
        // insert some space between run and its suites
        tableBody.append(LINE_BREAK);
        // START - Suites data
        boolean evenRow = false;
        for (Suite suite : run.getSuites()) {
            if (suite.scenariosFailed + suite.scenariosSkipped + suite.testcasesFailed > 0) {
                // the run has failed if contains a failed or skipped
                // scenario
                tableBody.append(START_TBL_ROW_FAILED_SUITE);
            } else {
                if (evenRow) {
                    tableBody.append(START_TBL_EVENROW);
                } else {
                    tableBody.append(START_TBL_ODDROW);
                }
            }
            tableBody.append(START_TBL_CELL + suite.name + END_TBL_CELL);
            tableBody.append("<td colspan=4 align=center>" + END_TBL_CELL);
            tableBody.append(START_TBL_CELL + suite.scenariosTotal + END_TBL_CELL);
            tableBody.append(START_TBL_CELL + (suite.scenariosTotal - suite.scenariosFailed - suite.scenariosSkipped) * 100 / suite.scenariosTotal + END_TBL_CELL);
            tableBody.append(START_TBL_CELL + suite.scenariosFailed + END_TBL_CELL);
            tableBody.append(START_TBL_CELL + suite.scenariosSkipped + END_TBL_CELL);
            tableBody.append(START_TBL_CELL + suite.testcasesTotal + END_TBL_CELL);
            tableBody.append(START_TBL_CELL + suite.testcasesPassedPercent + END_TBL_CELL);
            tableBody.append(START_TBL_CELL + suite.testcasesFailed + END_TBL_CELL);
            tableBody.append(START_TBL_CELL + suite.dateStart + END_TBL_CELL);
            tableBody.append(START_TBL_CELL + suite.dateEnd + END_TBL_CELL);
            tableBody.append(START_TBL_CELL + suite.duration + END_TBL_CELL);
            tableBody.append("<td align=center>" + (suite.userNote == null ? "" : suite.userNote) + END_TBL_CELL);
            evenRow = !evenRow;
        }
        // END - Suites data
        // insert some space before the next run
        tableBody.append("<tr><td colspan=16 style=\"height: 40px;\"></td></tr>");
    }
    tableBody.append(END_TBL);
    return tableBody.toString();
}
Also used : Suite(com.axway.ats.log.autodb.entities.Suite) ActiveDbAppender(com.axway.ats.log.appenders.ActiveDbAppender)

Example 5 with ActiveDbAppender

use of com.axway.ats.log.appenders.ActiveDbAppender in project ats-framework by Axway.

the class DbAccessFactory method getNewDbWriteAccessObject.

/**
     * Retrieves the DB info from the log4j system
     * and then creates a new instance for writing into the DB
     * 
     * @return
     * @throws DatabaseAccessException
     */
public DbWriteAccess getNewDbWriteAccessObject() throws DatabaseAccessException {
    // Our DB appender keeps the DB connection info
    ActiveDbAppender loggingAppender = ActiveDbAppender.getCurrentInstance();
    if (loggingAppender == null) {
        throw new DatabaseAccessException("Unable to initialize connection to the logging database as the ATS ActiveDbAppender is not attached to log4j system");
    }
    // Create DB connection based on the log4j system settings
    DbConnection dbConnection = new DbConnSQLServer(loggingAppender.getHost(), loggingAppender.getDatabase(), loggingAppender.getUser(), loggingAppender.getPassword());
    // Create the database access layer
    return new DbWriteAccess(dbConnection, false);
}
Also used : ActiveDbAppender(com.axway.ats.log.appenders.ActiveDbAppender) DbConnSQLServer(com.axway.ats.core.dbaccess.mssql.DbConnSQLServer) DatabaseAccessException(com.axway.ats.log.autodb.exceptions.DatabaseAccessException) DbConnection(com.axway.ats.core.dbaccess.DbConnection)

Aggregations

ActiveDbAppender (com.axway.ats.log.appenders.ActiveDbAppender)10 Logger (org.apache.log4j.Logger)5 Test (org.junit.Test)5 PassiveDbAppender (com.axway.ats.log.appenders.PassiveDbAppender)2 GetCurrentTestCaseEvent (com.axway.ats.log.autodb.events.GetCurrentTestCaseEvent)2 Appender (org.apache.log4j.Appender)2 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 DbConnection (com.axway.ats.core.dbaccess.DbConnection)1 DbConnSQLServer (com.axway.ats.core.dbaccess.mssql.DbConnSQLServer)1 Suite (com.axway.ats.log.autodb.entities.Suite)1 DatabaseAccessException (com.axway.ats.log.autodb.exceptions.DatabaseAccessException)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1 URL (java.net.URL)1 HttpEntity (org.apache.http.HttpEntity)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 HttpPost (org.apache.http.client.methods.HttpPost)1 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)1