Search in sources :

Example 1 with TestDataLibData

use of org.cerberus.crud.entity.TestDataLibData in project cerberus-source by cerberustesting.

the class TestDataLibDataDAO method readAll.

@Override
public AnswerList<TestDataLibData> readAll() {
    AnswerList answerList = new AnswerList();
    List<TestDataLibData> list = new ArrayList<TestDataLibData>();
    MessageEvent msg;
    final String query = "SELECT * FROM testdatalibdata";
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            ResultSet resultSet = preStat.executeQuery();
            try {
                while (resultSet.next()) {
                    list.add(this.loadFromResultSet(resultSet));
                }
                if (list.isEmpty()) {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                }
            } 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()));
                list.clear();
            } 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()));
            list.clear();
        } 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()));
        list.clear();
    } finally {
        try {
            if (!this.databaseSpring.isOnTransaction()) {
                if (connection != null) {
                    connection.close();
                }
            }
        } catch (SQLException ex) {
            LOG.warn("Unable to close connection : " + ex.toString());
        }
    }
    answerList.setDataList(list);
    answerList.setTotalRows(list.size());
    answerList.setResultMessage(msg);
    return answerList;
}
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) IFactoryTestDataLibData(org.cerberus.crud.factory.IFactoryTestDataLibData) TestDataLibData(org.cerberus.crud.entity.TestDataLibData)

Example 2 with TestDataLibData

use of org.cerberus.crud.entity.TestDataLibData in project cerberus-source by cerberustesting.

the class TestDataLibDataDAO method readByKeyTech.

@Override
public AnswerItem readByKeyTech(Integer testDataLibDataID) {
    AnswerItem answer = new AnswerItem();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
    TestDataLibData result = null;
    final String query = "SELECT * FROM testdatalibdata where `testdatalibdataid`=? ";
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
        LOG.debug("SQL.param.testDataLibDataID : " + testDataLibDataID);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        preStat.setInt(1, testDataLibDataID);
        try {
            ResultSet resultSet = preStat.executeQuery();
            try {
                if (resultSet.first()) {
                    result = this.loadFromResultSet(resultSet);
                } else {
                    // specific message for gefromdatalib
                    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 {
                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 (!this.databaseSpring.isOnTransaction()) {
                if (connection != null) {
                    connection.close();
                }
            }
        } catch (SQLException ex) {
            LOG.warn("Unable to close connection : " + ex.toString());
        }
    }
    answer.setResultMessage(msg);
    answer.setItem(result);
    return answer;
}
Also used : 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) IFactoryTestDataLibData(org.cerberus.crud.factory.IFactoryTestDataLibData) TestDataLibData(org.cerberus.crud.entity.TestDataLibData)

Example 3 with TestDataLibData

use of org.cerberus.crud.entity.TestDataLibData in project cerberus-source by cerberustesting.

the class DataLibService method getSubDataFromType.

/**
 * Get the list of subData
 *
 * @param lib
 * @return
 */
private AnswerItem<HashMap<String, String>> getSubDataFromType(TestDataLib lib) {
    AnswerList answerData = new AnswerList();
    AnswerItem<HashMap<String, String>> result = new AnswerItem();
    MessageEvent msg = new MessageEvent(MessageEventEnum.PROPERTY_SUCCESS);
    List<TestDataLibData> objectDataList = new ArrayList<TestDataLibData>();
    HashMap<String, String> row = new HashMap<String, String>();
    switch(lib.getType()) {
        case TestDataLib.TYPE_CSV:
            answerData = testDataLibDataService.readByVarious(lib.getTestDataLibID(), null, null, "N");
            if ((answerData.getResultMessage().getCode() == MessageEventEnum.DATA_OPERATION_OK.getCode()) && !answerData.getDataList().isEmpty()) {
                objectDataList = answerData.getDataList();
                boolean missingKey = true;
                for (TestDataLibData tdld : objectDataList) {
                    row.put(tdld.getSubData(), tdld.getColumnPosition());
                    if (tdld.getSubData().equalsIgnoreCase("")) {
                        missingKey = false;
                    }
                }
                result.setItem(row);
                if (missingKey) {
                    msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_SUBDATACSVNOKEY);
                    result.setResultMessage(msg);
                } else {
                    msg = new MessageEvent(MessageEventEnum.PROPERTY_SUCCESS_GETFROMDATALIB_SUBDATA);
                    msg.setDescription(msg.getDescription().replace("%NBROW%", String.valueOf(answerData.getDataList().size())));
                    result.setResultMessage(msg);
                }
            } else if ((answerData.getResultMessage().getCode() == MessageEventEnum.DATA_OPERATION_OK.getCode()) && answerData.getDataList().isEmpty()) {
                msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_NOSUBDATACSV);
                result.setResultMessage(msg);
            } else {
                msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_SUBDATACSV);
                result.setResultMessage(msg);
            }
            break;
        case TestDataLib.TYPE_SQL:
            answerData = testDataLibDataService.readByVarious(lib.getTestDataLibID(), "N", null, null);
            if ((answerData.getResultMessage().getCode() == MessageEventEnum.DATA_OPERATION_OK.getCode()) && !answerData.getDataList().isEmpty()) {
                objectDataList = answerData.getDataList();
                boolean missingKey = true;
                for (TestDataLibData tdld : objectDataList) {
                    row.put(tdld.getSubData(), tdld.getColumn());
                    if (tdld.getSubData().equalsIgnoreCase("")) {
                        missingKey = false;
                    }
                }
                result.setItem(row);
                if (missingKey) {
                    msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_SUBDATASQLNOKEY);
                    result.setResultMessage(msg);
                } else {
                    msg = new MessageEvent(MessageEventEnum.PROPERTY_SUCCESS_GETFROMDATALIB_SUBDATA);
                    msg.setDescription(msg.getDescription().replace("%NBROW%", String.valueOf(answerData.getDataList().size())));
                    result.setResultMessage(msg);
                }
            } else if ((answerData.getResultMessage().getCode() == MessageEventEnum.DATA_OPERATION_OK.getCode()) && answerData.getDataList().isEmpty()) {
                msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_NOSUBDATASQL);
                result.setResultMessage(msg);
            } else {
                msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_SUBDATASQL);
                result.setResultMessage(msg);
            }
            break;
        case TestDataLib.TYPE_SERVICE:
            answerData = testDataLibDataService.readByVarious(lib.getTestDataLibID(), null, "N", null);
            if ((answerData.getResultMessage().getCode() == MessageEventEnum.DATA_OPERATION_OK.getCode()) && !answerData.getDataList().isEmpty()) {
                objectDataList = answerData.getDataList();
                boolean missingKey = true;
                for (TestDataLibData tdld : objectDataList) {
                    row.put(tdld.getSubData(), tdld.getParsingAnswer());
                    if (tdld.getSubData().equalsIgnoreCase("")) {
                        missingKey = false;
                    }
                }
                result.setItem(row);
                if (missingKey) {
                    msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_SUBDATASOAPNOKEY);
                    result.setResultMessage(msg);
                } else {
                    msg = new MessageEvent(MessageEventEnum.PROPERTY_SUCCESS_GETFROMDATALIB_SUBDATA);
                    msg.setDescription(msg.getDescription().replace("%NBROW%", String.valueOf(answerData.getDataList().size())));
                    result.setResultMessage(msg);
                }
            } else if ((answerData.getResultMessage().getCode() == MessageEventEnum.DATA_OPERATION_OK.getCode()) && answerData.getDataList().isEmpty()) {
                msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_NOSUBDATASOAP);
                result.setResultMessage(msg);
            } else {
                msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_SUBDATASOAP);
                result.setResultMessage(msg);
            }
            break;
        case TestDataLib.TYPE_INTERNAL:
            // For static Type, there is no need to fetch the subdata as subdata are loaded at the same time of the data.
            msg = new MessageEvent(MessageEventEnum.PROPERTY_SUCCESS_GETFROMDATALIB_SUBDATA);
            result.setResultMessage(msg);
            result.setItem(null);
            break;
    }
    return result;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) HashMap(java.util.HashMap) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) AnswerItem(org.cerberus.util.answer.AnswerItem) TestDataLibData(org.cerberus.crud.entity.TestDataLibData)

Example 4 with TestDataLibData

use of org.cerberus.crud.entity.TestDataLibData in project cerberus-source by cerberustesting.

the class CreateTestDataLib method getSubDataFromParameter.

private List<TestDataLibData> getSubDataFromParameter(HttpServletRequest request, ApplicationContext appContext, int testDataLibId, JSONArray json) throws JSONException {
    List<TestDataLibData> tdldList = new ArrayList();
    IFactoryTestDataLibData tdldFactory = appContext.getBean(IFactoryTestDataLibData.class);
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
    String charset = request.getCharacterEncoding();
    for (int i = 0; i < json.length(); i++) {
        JSONObject objectJson = json.getJSONObject(i);
        // Parameter that are already controled by GUI (no need to decode) --> We SECURE them
        boolean delete = objectJson.getBoolean("toDelete");
        Integer testDataLibDataId = objectJson.getInt("testDataLibDataID");
        // Parameter that needs to be secured --> We SECURE+DECODE them
        // NONE
        // Parameter that we cannot secure as we need the html --> We DECODE them
        String subdata = ParameterParserUtil.parseStringParamAndDecode(objectJson.getString("subData"), "", charset);
        String value = ParameterParserUtil.parseStringParamAndDecode(objectJson.getString("value"), "", charset);
        String column = ParameterParserUtil.parseStringParamAndDecode(objectJson.getString("column"), "", charset);
        String parsingAnswer = ParameterParserUtil.parseStringParamAndDecode(objectJson.getString("parsingAnswer"), "", charset);
        String columnPosition = ParameterParserUtil.parseStringParamAndDecode(objectJson.getString("columnPosition"), "", charset);
        String description = ParameterParserUtil.parseStringParamAndDecode(objectJson.getString("description"), "", charset);
        if (!delete) {
            TestDataLibData tdld = tdldFactory.create(testDataLibDataId, testDataLibId, subdata, value, column, parsingAnswer, columnPosition, description);
            tdldList.add(tdld);
        }
    }
    return tdldList;
}
Also used : PolicyFactory(org.owasp.html.PolicyFactory) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) IFactoryTestDataLibData(org.cerberus.crud.factory.IFactoryTestDataLibData) IFactoryTestDataLibData(org.cerberus.crud.factory.IFactoryTestDataLibData) TestDataLibData(org.cerberus.crud.entity.TestDataLibData)

Example 5 with TestDataLibData

use of org.cerberus.crud.entity.TestDataLibData in project cerberus-source by cerberustesting.

the class UpdateTestDataLib method getSubDataFromParameter.

private List<TestDataLibData> getSubDataFromParameter(HttpServletRequest request, ApplicationContext appContext, int testDataLibId, JSONArray json) throws JSONException {
    List<TestDataLibData> tdldList = new ArrayList();
    IFactoryTestDataLibData tdldFactory = appContext.getBean(IFactoryTestDataLibData.class);
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
    String charset = request.getCharacterEncoding();
    for (int i = 0; i < json.length(); i++) {
        JSONObject objectJson = json.getJSONObject(i);
        // Parameter that are already controled by GUI (no need to decode) --> We SECURE them
        boolean delete = objectJson.getBoolean("toDelete");
        Integer testDataLibDataId = objectJson.getInt("testDataLibDataID");
        // Parameter that needs to be secured --> We SECURE+DECODE them
        // NONE
        // Parameter that we cannot secure as we need the html --> We DECODE them
        String subdata = ParameterParserUtil.parseStringParamAndDecode(objectJson.getString("subData"), "", charset);
        String value = ParameterParserUtil.parseStringParamAndDecode(objectJson.getString("value"), "", charset);
        String column = ParameterParserUtil.parseStringParamAndDecode(objectJson.getString("column"), "", charset);
        String parsingAnswer = ParameterParserUtil.parseStringParam(objectJson.getString("parsingAnswer"), "");
        String columnPosition = ParameterParserUtil.parseStringParamAndDecode(objectJson.getString("columnPosition"), "", charset);
        String description = ParameterParserUtil.parseStringParamAndDecode(objectJson.getString("description"), "", charset);
        if (!delete) {
            TestDataLibData tdld = tdldFactory.create(testDataLibDataId, testDataLibId, subdata, value, column, parsingAnswer, columnPosition, description);
            tdldList.add(tdld);
        }
    }
    return tdldList;
}
Also used : PolicyFactory(org.owasp.html.PolicyFactory) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) IFactoryTestDataLibData(org.cerberus.crud.factory.IFactoryTestDataLibData) IFactoryTestDataLibData(org.cerberus.crud.factory.IFactoryTestDataLibData) TestDataLibData(org.cerberus.crud.entity.TestDataLibData)

Aggregations

TestDataLibData (org.cerberus.crud.entity.TestDataLibData)18 ArrayList (java.util.ArrayList)12 IFactoryTestDataLibData (org.cerberus.crud.factory.IFactoryTestDataLibData)12 MessageEvent (org.cerberus.engine.entity.MessageEvent)11 AnswerList (org.cerberus.util.answer.AnswerList)9 AnswerItem (org.cerberus.util.answer.AnswerItem)8 JSONObject (org.json.JSONObject)7 Connection (java.sql.Connection)6 PreparedStatement (java.sql.PreparedStatement)6 ResultSet (java.sql.ResultSet)6 SQLException (java.sql.SQLException)6 ITestDataLibDataService (org.cerberus.crud.service.ITestDataLibDataService)5 JSONArray (org.json.JSONArray)5 HashMap (java.util.HashMap)4 List (java.util.List)4 TestDataLib (org.cerberus.crud.entity.TestDataLib)4 PolicyFactory (org.owasp.html.PolicyFactory)4 Answer (org.cerberus.util.answer.Answer)3 BufferedReader (java.io.BufferedReader)2 FileReader (java.io.FileReader)2