Search in sources :

Example 11 with AnswerItem

use of org.cerberus.util.answer.AnswerItem 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 12 with AnswerItem

use of org.cerberus.util.answer.AnswerItem in project cerberus-source by cerberustesting.

the class TestCaseLabelDAO method readByKey.

@Override
public AnswerItem<TestCaseLabel> readByKey(String test, String testCase, Integer labelId) {
    AnswerItem<TestCaseLabel> ans = new AnswerItem();
    TestCaseLabel result = null;
    final String query = "SELECT * FROM `testcaselabel` tel WHERE `labelid` = ? and `test` = ? and `testcase` = ?";
    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);
        LOG.debug("SQL.param.label : " + labelId);
        LOG.debug("SQL.param.test : " + test);
        LOG.debug("SQL.param.testcase : " + testCase);
    }
    try (Connection connection = databaseSpring.connect();
        PreparedStatement preStat = connection.prepareStatement(query)) {
        // prepare and execute query
        preStat.setInt(1, labelId);
        preStat.setString(2, test);
        preStat.setString(3, testCase);
        try (ResultSet resultSet = preStat.executeQuery()) {
            // parse query
            if (resultSet.first()) {
                result = loadFromResultSet(resultSet, null);
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                ans.setItem(result);
            } else {
                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()));
        }
    } catch (Exception e) {
        LOG.warn("Unable to readByKey TestCaseLabel: " + e.getMessage());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", e.toString());
    } finally {
        ans.setResultMessage(msg);
    }
    return ans;
}
Also used : SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) IFactoryTestCaseLabel(org.cerberus.crud.factory.IFactoryTestCaseLabel) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) AnswerItem(org.cerberus.util.answer.AnswerItem) SQLException(java.sql.SQLException)

Example 13 with AnswerItem

use of org.cerberus.util.answer.AnswerItem in project cerberus-source by cerberustesting.

the class TestCaseStepActionControlExecutionDAO method readByKey.

@Override
public AnswerItem readByKey(long executionId, String test, String testCase, int step, int index, int sequence, int controlSequence) {
    MessageEvent msg;
    AnswerItem answer = new AnswerItem();
    TestCaseStepActionControlExecution tcsa = null;
    StringBuilder query = new StringBuilder();
    query.append("SELECT * FROM testcasestepactioncontrolexecution a ");
    query.append("where id = ? and test = ? and testcase = ? and step = ? and `index` = ? and controlSequence = ?");
    query.append("and sequence = ?");
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query.toString());
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        try {
            preStat.setLong(1, executionId);
            preStat.setString(2, test);
            preStat.setString(3, testCase);
            preStat.setInt(4, step);
            preStat.setInt(5, index);
            preStat.setInt(6, sequence);
            preStat.setInt(7, controlSequence);
            ResultSet resultSet = preStat.executeQuery();
            try {
                while (resultSet.next()) {
                    tcsa = this.loadFromResultset(resultSet);
                }
                if (tcsa == null) {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                }
            } 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 {
                if (resultSet != null) {
                    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 {
            if (preStat != null) {
                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());
        }
    }
    answer.setItem(tcsa);
    answer.setResultMessage(msg);
    return answer;
}
Also used : IFactoryTestCaseStepActionControlExecution(org.cerberus.crud.factory.IFactoryTestCaseStepActionControlExecution) TestCaseStepActionControlExecution(org.cerberus.crud.entity.TestCaseStepActionControlExecution) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 14 with AnswerItem

use of org.cerberus.util.answer.AnswerItem in project cerberus-source by cerberustesting.

the class TestCaseStepActionExecutionDAO method readByKey.

@Override
public AnswerItem readByKey(long executionId, String test, String testCase, int step, int index, int sequence) {
    MessageEvent msg;
    AnswerItem answer = new AnswerItem();
    TestCaseStepActionExecution tcsa = null;
    StringBuilder query = new StringBuilder();
    query.append("SELECT * FROM testcasestepactionexecution exa ");
    query.append("where exa.id = ? and exa.test = ? and exa.testcase = ? and exa.step = ? and exa.index = ? and exa.sequence = .");
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query.toString());
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        try {
            preStat.setLong(1, executionId);
            preStat.setString(2, test);
            preStat.setString(3, testCase);
            preStat.setInt(4, step);
            preStat.setInt(5, index);
            preStat.setInt(6, sequence);
            ResultSet resultSet = preStat.executeQuery();
            try {
                while (resultSet.next()) {
                    tcsa = this.loadFromResultset(resultSet);
                }
                if (tcsa == null) {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                }
            } 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 {
                if (resultSet != null) {
                    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 {
            if (preStat != null) {
                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());
        }
    }
    answer.setItem(tcsa);
    answer.setResultMessage(msg);
    return answer;
}
Also used : SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) IFactoryTestCaseStepActionExecution(org.cerberus.crud.factory.IFactoryTestCaseStepActionExecution) TestCaseStepActionExecution(org.cerberus.crud.entity.TestCaseStepActionExecution) PreparedStatement(java.sql.PreparedStatement) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 15 with AnswerItem

use of org.cerberus.util.answer.AnswerItem in project cerberus-source by cerberustesting.

the class TestDAO method readByKey.

@Override
public AnswerItem readByKey(String test) {
    AnswerItem ans = new AnswerItem();
    Test result;
    final String query = "SELECT * FROM `test` WHERE `test` = ?";
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            preStat.setString(1, test);
            ResultSet resultSet = preStat.executeQuery();
            try {
                if (resultSet.first()) {
                    result = loadFromResultSet(resultSet);
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                    ans.setItem(result);
                } else {
                    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 ans;
}
Also used : Test(org.cerberus.crud.entity.Test) IFactoryTest(org.cerberus.crud.factory.IFactoryTest) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) AnswerItem(org.cerberus.util.answer.AnswerItem)

Aggregations

AnswerItem (org.cerberus.util.answer.AnswerItem)322 MessageEvent (org.cerberus.engine.entity.MessageEvent)212 JSONObject (org.json.JSONObject)206 ApplicationContext (org.springframework.context.ApplicationContext)98 AnswerList (org.cerberus.util.answer.AnswerList)90 ArrayList (java.util.ArrayList)78 JSONArray (org.json.JSONArray)74 PolicyFactory (org.owasp.html.PolicyFactory)74 List (java.util.List)72 JSONException (org.json.JSONException)69 HashMap (java.util.HashMap)60 ILogEventService (org.cerberus.crud.service.ILogEventService)58 SQLException (java.sql.SQLException)57 Connection (java.sql.Connection)55 PreparedStatement (java.sql.PreparedStatement)53 Answer (org.cerberus.util.answer.Answer)53 ResultSet (java.sql.ResultSet)52 CerberusException (org.cerberus.exception.CerberusException)44 IOException (java.io.IOException)34 ServletException (javax.servlet.ServletException)24