Search in sources :

Example 56 with MessageEvent

use of org.cerberus.engine.entity.MessageEvent in project cerberus-source by cerberustesting.

the class TestCaseExecutionQueueDAO method getNbEntryToGo.

@Override
public int getNbEntryToGo(long id, int prio) {
    AnswerItem<TestCaseExecutionQueue> ans = new AnswerItem<>();
    TestCaseExecutionQueue result = null;
    final String query = "SELECT count(*)  FROM testcaseexecutionqueue WHERE State = 'QUEUED' and (ID < ? and Priority <= ?);";
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            int i = 1;
            preStat.setLong(i++, id);
            preStat.setInt(i++, prio);
            ResultSet resultSet = preStat.executeQuery();
            try {
                if (resultSet.first()) {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                    return resultSet.getInt(1);
                } else {
                    LOG.error("No record found : " + query);
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                }
            } catch (SQLException exception) {
                LOG.error("Unable to execute query : " + exception.toString());
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
                msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
            } finally {
                resultSet.close();
            }
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to close connection : " + exception.toString());
        }
    }
    // sets the message
    ans.setResultMessage(msg);
    return 999999;
}
Also used : SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) TestCaseExecutionQueue(org.cerberus.crud.entity.TestCaseExecutionQueue) IFactoryTestCaseExecutionQueue(org.cerberus.crud.factory.IFactoryTestCaseExecutionQueue) PreparedStatement(java.sql.PreparedStatement) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 57 with MessageEvent

use of org.cerberus.engine.entity.MessageEvent in project cerberus-source by cerberustesting.

the class TestCaseExecutionQueueDAO method readDistinctValuesByCriteria.

@Override
public AnswerList readDistinctValuesByCriteria(String columnName, String sort, String searchTerm, Map<String, List<String>> individualSearch, String column) {
    AnswerList answer = new AnswerList();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    List<String> distinctValues = new ArrayList<>();
    StringBuilder searchSQL = new StringBuilder();
    List<String> individalColumnSearchValues = new ArrayList<String>();
    StringBuilder query = new StringBuilder();
    query.append("SELECT distinct exq.");
    query.append(columnName);
    query.append(" as distinctValues FROM testcaseexecutionqueue exq");
    query.append(" where 1=1");
    if (!StringUtil.isNullOrEmpty(searchTerm)) {
        searchSQL.append(" and (exq.ID like ?");
        searchSQL.append(" or exq.Test like ?");
        searchSQL.append(" or exq.TestCase like ?");
        searchSQL.append(" or exq.Country like ?");
        searchSQL.append(" or exq.Environment like ?");
        searchSQL.append(" or exq.Browser like ?");
        searchSQL.append(" or exq.Tag like ?");
        searchSQL.append(" or exq.State like ?)");
    }
    if (individualSearch != null && !individualSearch.isEmpty()) {
        searchSQL.append(" and ( 1=1 ");
        for (Map.Entry<String, List<String>> entry : individualSearch.entrySet()) {
            searchSQL.append(" and exq.");
            searchSQL.append(SqlUtil.getInSQLClauseForPreparedStatement(entry.getKey(), entry.getValue()));
            individalColumnSearchValues.addAll(entry.getValue());
        }
        searchSQL.append(" )");
    }
    query.append(searchSQL);
    query.append(" group by ifnull(exq.").append(columnName).append(",'')");
    query.append(" order by exq.").append(columnName).append(" asc");
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query.toString());
    }
    try (Connection connection = databaseSpring.connect();
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        Statement stm = connection.createStatement()) {
        int i = 1;
        if (!StringUtil.isNullOrEmpty(searchTerm)) {
            preStat.setString(i++, "%" + searchTerm + "%");
            preStat.setString(i++, "%" + searchTerm + "%");
            preStat.setString(i++, "%" + searchTerm + "%");
            preStat.setString(i++, "%" + searchTerm + "%");
            preStat.setString(i++, "%" + searchTerm + "%");
            preStat.setString(i++, "%" + searchTerm + "%");
            preStat.setString(i++, "%" + searchTerm + "%");
            preStat.setString(i++, "%" + searchTerm + "%");
        }
        for (String individualColumnSearchValue : individalColumnSearchValues) {
            preStat.setString(i++, individualColumnSearchValue);
        }
        try (ResultSet resultSet = preStat.executeQuery();
            ResultSet rowSet = stm.executeQuery("SELECT FOUND_ROWS()")) {
            // gets the data
            while (resultSet.next()) {
                distinctValues.add(resultSet.getString("distinctValues") == null ? "" : resultSet.getString("distinctValues"));
            }
            // get the total number of rows
            int nrTotalRows = 0;
            if (rowSet != null && rowSet.next()) {
                nrTotalRows = rowSet.getInt(1);
            }
            if (distinctValues.size() >= MAX_ROW_SELECTED) {
                // Result of SQl was limited by MAX_ROW_SELECTED constrain. That means that we may miss some lines in the resultList.
                LOG.error("Partial Result in the query.");
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_WARNING_PARTIAL_RESULT);
                msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Maximum row reached : " + MAX_ROW_SELECTED));
                answer = new AnswerList(distinctValues, nrTotalRows);
            } else if (distinctValues.size() <= 0) {
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                answer = new AnswerList(distinctValues, nrTotalRows);
            } else {
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                answer = new AnswerList(distinctValues, nrTotalRows);
            }
        } catch (SQLException e) {
            LOG.warn("Unable to execute query : " + e.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", e.toString());
        }
    } catch (Exception e) {
        LOG.warn("Unable to execute query : " + e.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", e.toString());
    } finally {
        // We always set the result message
        answer.setResultMessage(msg);
    }
    answer.setResultMessage(msg);
    answer.setDataList(distinctValues);
    return answer;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) FactoryCreationException(org.cerberus.exception.FactoryCreationException) CerberusException(org.cerberus.exception.CerberusException) ResultSet(java.sql.ResultSet) AnswerList(org.cerberus.util.answer.AnswerList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Map(java.util.Map)

Example 58 with MessageEvent

use of org.cerberus.engine.entity.MessageEvent in project cerberus-source by cerberustesting.

the class TestCaseExecutionQueueDAO method readDistinctEnvCountryBrowserByTag.

@Override
public AnswerList readDistinctEnvCountryBrowserByTag(String tag) {
    AnswerList answer = new AnswerList();
    StringBuilder query = new StringBuilder();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
    query.append("SELECT exq.* FROM testcaseexecutionqueue exq WHERE tag = ? GROUP BY Environment, Country, Browser");
    Connection connection = this.databaseSpring.connect();
    List<TestCaseExecutionQueue> EnvCountryBrowserList = new ArrayList<TestCaseExecutionQueue>();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        preStat.setString(1, tag);
        try {
            ResultSet resultSet = preStat.executeQuery();
            try {
                while (resultSet.next()) {
                    EnvCountryBrowserList.add(this.loadFromResultSet(resultSet));
                }
                msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseExecutionInQueue").replace("%OPERATION%", "SELECT"));
                answer = new AnswerList(EnvCountryBrowserList, EnvCountryBrowserList.size());
            } catch (SQLException exception) {
                LOG.warn("Unable to execute query : " + exception.toString());
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
                msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to retrieve the list of entries!"));
                EnvCountryBrowserList = null;
            } catch (FactoryCreationException ex) {
                LOG.warn("Unable to execute query : " + ex.toString());
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
                msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to retrieve the list of entries!"));
                EnvCountryBrowserList = null;
            } finally {
                if (resultSet != null) {
                    resultSet.close();
                }
            }
        } catch (SQLException ex) {
            LOG.warn("Unable to execute query : " + ex.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to retrieve the list of entries!"));
            EnvCountryBrowserList = null;
        } finally {
            if (preStat != null) {
                preStat.close();
            }
        }
    } catch (SQLException ex) {
        LOG.warn(ex.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to retrieve the list of entries!"));
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException ex) {
            LOG.warn("Unable to execute query : " + ex.toString());
        }
    }
    answer.setResultMessage(msg);
    return answer;
}
Also used : FactoryCreationException(org.cerberus.exception.FactoryCreationException) AnswerList(org.cerberus.util.answer.AnswerList) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) TestCaseExecutionQueue(org.cerberus.crud.entity.TestCaseExecutionQueue) IFactoryTestCaseExecutionQueue(org.cerberus.crud.factory.IFactoryTestCaseExecutionQueue) PreparedStatement(java.sql.PreparedStatement)

Example 59 with MessageEvent

use of org.cerberus.engine.entity.MessageEvent in project cerberus-source by cerberustesting.

the class TestCaseExecutionQueueDAO method readByVarious2.

@Override
public AnswerList readByVarious2(List<String> stateList) throws CerberusException {
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
    AnswerList answer = new AnswerList();
    final StringBuilder query = new StringBuilder();
    query.append("SELECT exq.id, exq.manualexecution, app.System, cea.environment, cea.country, cea.application, cea.poolsize, exq.robotIP, rbt.host, exq.DebugFlag, app.type ");
    query.append("from testcaseexecutionqueue exq ");
    query.append("left join testcase tec on tec.test=exq.test and tec.testcase=exq.testcase ");
    query.append("left join application app on app.application=tec.application ");
    query.append("left join robot rbt on rbt.robot=exq.robot ");
    query.append("left join countryenvironmentparameters cea on cea.system=app.system and cea.environment=exq.environment and cea.country=exq.country and cea.application=tec.application ");
    query.append("WHERE 1=1 ");
    query.append(SqlUtil.createWhereInClause(" AND exq.state", stateList, true));
    query.append("order by exq.priority, exq.id asc;");
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query.toString());
    }
    List<TestCaseExecutionQueueToTreat> testCaseExecutionInQueueList = new ArrayList<TestCaseExecutionQueueToTreat>();
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        try {
            ResultSet resultSet = preStat.executeQuery();
            try {
                while (resultSet.next()) {
                    testCaseExecutionInQueueList.add(loadQueueToTreatFromResultSet(resultSet));
                }
                msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseExecutionInQueue").replace("%OPERATION%", "SELECT"));
                answer = new AnswerList(testCaseExecutionInQueueList, testCaseExecutionInQueueList.size());
            } catch (SQLException exception) {
                LOG.error("Unable to execute query : " + exception.toString());
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
                msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to retrieve the list of entries!"));
                testCaseExecutionInQueueList = null;
            } finally {
                resultSet.close();
            }
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to retrieve the list of entries!"));
            testCaseExecutionInQueueList = null;
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to retrieve the list of entries!"));
        testCaseExecutionInQueueList = null;
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to retrieve the list of entries!"));
        }
    }
    answer.setResultMessage(msg);
    return answer;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) TestCaseExecutionQueueToTreat(org.cerberus.engine.threadpool.entity.TestCaseExecutionQueueToTreat)

Example 60 with MessageEvent

use of org.cerberus.engine.entity.MessageEvent in project cerberus-source by cerberustesting.

the class TestCaseExecutionQueueDAO method readDistinctColumnByTag.

@Override
public AnswerList readDistinctColumnByTag(String tag, boolean env, boolean country, boolean browser, boolean app) {
    AnswerList answer = new AnswerList();
    StringBuilder query = new StringBuilder();
    StringBuilder distinct = new StringBuilder();
    int prev = 0;
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
    if (!(!env && !country && !app && !browser)) {
        if (env) {
            distinct.append("exq.Environment");
            prev++;
        }
        if (country) {
            if (prev != 0) {
                prev = 0;
                distinct.append(",");
            }
            distinct.append("exq.Country");
            prev++;
        }
        if (browser) {
            if (prev != 0) {
                prev = 0;
                distinct.append(",");
            }
            distinct.append("exq.Browser");
            prev++;
        }
        if (app) {
            if (prev != 0) {
                prev = 0;
                distinct.append(",");
            }
            distinct.append("tec.Application");
        }
        query.append("SELECT tec.test, tec.testcase, exq.tag,  ");
        query.append(distinct.toString());
        query.append(" FROM testcase tec LEFT JOIN testcaseexecutionqueue exq ON exq.Test = tec.Test AND exq.TestCase = tec.TestCase WHERE tag = ? GROUP BY ");
        query.append(distinct.toString());
    } else {
        // If there is no distinct, select nothing
        query.append("SELECT * FROM testcaseexecutionqueue exq WHERE 1=0 AND tag = ?");
    }
    Connection connection = this.databaseSpring.connect();
    List<TestCaseExecutionQueue> column = new ArrayList<TestCaseExecutionQueue>();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        preStat.setString(1, tag);
        try {
            ResultSet resultSet = preStat.executeQuery();
            try {
                while (resultSet.next()) {
                    TestCaseExecutionQueue tmp = new TestCaseExecutionQueue();
                    tmp.setTest(resultSet.getString("tec.test"));
                    tmp.setTestCase(resultSet.getString("tec.testcase"));
                    tmp.setTag(resultSet.getString("exq.tag"));
                    if (env) {
                        tmp.setEnvironment(resultSet.getString("Environment"));
                    } else {
                        tmp.setEnvironment("");
                    }
                    if (country) {
                        tmp.setCountry(resultSet.getString("Country"));
                    } else {
                        tmp.setCountry("");
                    }
                    if (browser) {
                        tmp.setBrowser(resultSet.getString("Browser"));
                    } else {
                        tmp.setBrowser("");
                    }
                    if (app) {
                        Application application = factoryApplication.create(resultSet.getString("Application"));
                        tmp.setApplicationObj(application);
                    }
                    column.add(tmp);
                }
                msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseExecution").replace("%OPERATION%", "SELECT"));
                answer = new AnswerList(column, column.size());
            } catch (SQLException exception) {
                LOG.warn("Unable to execute query : " + exception.toString());
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
                msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to retrieve the list of entries!"));
                column = null;
            } finally {
                if (resultSet != null) {
                    resultSet.close();
                }
            }
        } catch (SQLException ex) {
            LOG.warn("Unable to execute query : " + ex.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to retrieve the list of entries!"));
            column = null;
        } finally {
            if (preStat != null) {
                preStat.close();
            }
        }
    } catch (SQLException ex) {
        LOG.warn(ex.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to retrieve the list of entries!"));
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException ex) {
            LOG.warn("Unable to execute query : " + ex.toString());
        }
    }
    answer.setResultMessage(msg);
    return answer;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) TestCaseExecutionQueue(org.cerberus.crud.entity.TestCaseExecutionQueue) IFactoryTestCaseExecutionQueue(org.cerberus.crud.factory.IFactoryTestCaseExecutionQueue) PreparedStatement(java.sql.PreparedStatement) IFactoryApplication(org.cerberus.crud.factory.IFactoryApplication) Application(org.cerberus.crud.entity.Application)

Aggregations

MessageEvent (org.cerberus.engine.entity.MessageEvent)629 SQLException (java.sql.SQLException)306 Connection (java.sql.Connection)304 PreparedStatement (java.sql.PreparedStatement)301 Answer (org.cerberus.util.answer.Answer)227 AnswerItem (org.cerberus.util.answer.AnswerItem)212 ArrayList (java.util.ArrayList)180 ResultSet (java.sql.ResultSet)172 AnswerList (org.cerberus.util.answer.AnswerList)152 JSONObject (org.json.JSONObject)144 ApplicationContext (org.springframework.context.ApplicationContext)122 CerberusException (org.cerberus.exception.CerberusException)95 PolicyFactory (org.owasp.html.PolicyFactory)93 List (java.util.List)83 ILogEventService (org.cerberus.crud.service.ILogEventService)79 JSONException (org.json.JSONException)79 Map (java.util.Map)71 CerberusEventException (org.cerberus.exception.CerberusEventException)51 IOException (java.io.IOException)48 WebDriverException (org.openqa.selenium.WebDriverException)38