Search in sources :

Example 11 with TestDataLibData

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

the class CreateTestDataLib method processRequest.

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    IFactoryTestDataLibData tdldFactory = appContext.getBean(IFactoryTestDataLibData.class);
    ITestDataLibDataService tdldService = appContext.getBean(ITestDataLibDataService.class);
    IParameterService parameterService = appContext.getBean(IParameterService.class);
    JSONObject jsonResponse = new JSONObject();
    Answer ans = new Answer();
    AnswerItem ansItem = new AnswerItem();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    ans.setResultMessage(msg);
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
    String charset = request.getCharacterEncoding();
    response.setContentType("application/json");
    Map<String, String> fileData = new HashMap<String, String>();
    FileItem file = null;
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    try {
        List<FileItem> fields = upload.parseRequest(request);
        Iterator<FileItem> it = fields.iterator();
        if (!it.hasNext()) {
            return;
        }
        while (it.hasNext()) {
            FileItem fileItem = it.next();
            boolean isFormField = fileItem.isFormField();
            if (isFormField) {
                fileData.put(fileItem.getFieldName(), ParameterParserUtil.parseStringParamAndDecode(fileItem.getString("UTF-8"), "", charset));
            } else {
                file = fileItem;
            }
        }
    } catch (FileUploadException e) {
        e.printStackTrace();
    }
    try {
        /**
         * Parsing and securing all required parameters.
         */
        // Parameter that are already controled by GUI (no need to decode) --> We SECURE them
        String type = policy.sanitize(fileData.get("type"));
        String system = policy.sanitize(fileData.get("system"));
        String environment = policy.sanitize(fileData.get("environment"));
        String country = policy.sanitize(fileData.get("country"));
        String database = policy.sanitize(fileData.get("database"));
        String databaseUrl = policy.sanitize(fileData.get("databaseUrl"));
        String databaseCsv = policy.sanitize(fileData.get("databaseCsv"));
        // Parameter that needs to be secured --> We SECURE+DECODE them
        // this is mandatory
        String name = fileData.get("name");
        String group = fileData.get("group");
        String description = fileData.get("libdescription");
        String service = fileData.get("service");
        // Parameter that we cannot secure as we need the html --> We DECODE them
        String script = fileData.get("script");
        String servicePath = fileData.get("servicepath");
        String method = fileData.get("method");
        String envelope = fileData.get("envelope");
        String csvUrl = fileData.get("csvUrl");
        String separator = fileData.get("separator");
        String test = fileData.get("subdataCheck");
        /**
         * Checking all constrains before calling the services.
         */
        // Prepare the final answer.
        MessageEvent msg1 = new MessageEvent(MessageEventEnum.GENERIC_OK);
        Answer finalAnswer = new Answer(msg1);
        if (StringUtil.isNullOrEmpty(name)) {
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
            msg.setDescription(msg.getDescription().replace("%ITEM%", "Test Data Library").replace("%OPERATION%", "Create").replace("%REASON%", "Test data library name is missing! "));
            finalAnswer.setResultMessage(msg);
        } else {
            /**
             * All data seems cleans so we can call the services.
             */
            ITestDataLibService libService = appContext.getBean(ITestDataLibService.class);
            IFactoryTestDataLib factoryLibService = appContext.getBean(IFactoryTestDataLib.class);
            TestDataLib lib = factoryLibService.create(0, name, system, environment, country, group, type, database, script, databaseUrl, service, servicePath, method, envelope, databaseCsv, csvUrl, separator, description, request.getRemoteUser(), null, "", null, null, null, null, null);
            // Creates the entries and the subdata list
            ansItem = libService.create(lib);
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ansItem);
            /**
             * Object created. Adding Log entry.
             */
            if (ansItem.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                ILogEventService logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/CreateTestDataLib", "CREATE", "Create TestDataLib  : " + request.getParameter("name"), request);
            }
            List<TestDataLibData> tdldList = new ArrayList();
            TestDataLib dataLibWithUploadedFile = (TestDataLib) ansItem.getItem();
            if (file != null) {
                ans = libService.uploadFile(dataLibWithUploadedFile.getTestDataLibID(), file);
                if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                    dataLibWithUploadedFile.setCsvUrl(File.separator + dataLibWithUploadedFile.getTestDataLibID() + File.separator + file.getName());
                    libService.update(dataLibWithUploadedFile);
                }
            }
            // Getting list of SubData from JSON Call
            if (fileData.get("subDataList") != null) {
                JSONArray objSubDataArray = new JSONArray(fileData.get("subDataList"));
                tdldList = getSubDataFromParameter(request, appContext, dataLibWithUploadedFile.getTestDataLibID(), objSubDataArray);
            }
            if (file != null && test.equals("1")) {
                String firstLine = "";
                String secondLine = "";
                try (BufferedReader reader = new BufferedReader(new FileReader(parameterService.getParameterStringByKey("cerberus_testdatalibCSV_path", "", null) + lib.getCsvUrl()))) {
                    firstLine = reader.readLine();
                    secondLine = reader.readLine();
                    String[] firstLineSubData = (!dataLibWithUploadedFile.getSeparator().isEmpty()) ? firstLine.split(dataLibWithUploadedFile.getSeparator()) : firstLine.split(",");
                    String[] secondLineSubData = (!dataLibWithUploadedFile.getSeparator().isEmpty()) ? secondLine.split(dataLibWithUploadedFile.getSeparator()) : secondLine.split(",");
                    int i = 0;
                    int y = 1;
                    TestDataLibData firstLineLibData = tdldList.get(0);
                    tdldList = new ArrayList();
                    if (StringUtil.isNullOrEmpty(firstLineLibData.getColumnPosition())) {
                        firstLineLibData.setColumnPosition("1");
                    }
                    if (StringUtil.isNullOrEmpty(firstLineLibData.getValue())) {
                        firstLineLibData.setValue(secondLineSubData[0]);
                    }
                    if (StringUtil.isNullOrEmpty(firstLineLibData.getColumn())) {
                        firstLineLibData.setColumn(firstLineSubData[0]);
                    }
                    tdldList.add(firstLineLibData);
                    for (String item : firstLineSubData) {
                        TestDataLibData tdld = tdldFactory.create(null, dataLibWithUploadedFile.getTestDataLibID(), item + "_" + y, secondLineSubData[i], item, null, Integer.toString(y), null);
                        tdldList.add(tdld);
                        i++;
                        y++;
                    }
                // Update the Database with the new list.
                } finally {
                    try {
                        file.getInputStream().close();
                    } catch (Throwable ignore) {
                    }
                }
            }
            ans = tdldService.compareListAndUpdateInsertDeleteElements(dataLibWithUploadedFile.getTestDataLibID(), tdldList);
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
        }
        /**
         * Formating and returning the json result.
         */
        // sets the message returned by the operations
        jsonResponse.put("messageType", finalAnswer.getResultMessage().getMessage().getCodeString());
        jsonResponse.put("message", finalAnswer.getResultMessage().getDescription());
        response.getWriter().print(jsonResponse);
        response.getWriter().flush();
    } catch (JSONException ex) {
        LOG.warn(ex);
        response.getWriter().print(AnswerUtil.createGenericErrorAnswer());
        response.getWriter().flush();
    }
}
Also used : PolicyFactory(org.owasp.html.PolicyFactory) HashMap(java.util.HashMap) MessageEvent(org.cerberus.engine.entity.MessageEvent) IFactoryTestDataLibData(org.cerberus.crud.factory.IFactoryTestDataLibData) ArrayList(java.util.ArrayList) IParameterService(org.cerberus.crud.service.IParameterService) ITestDataLibDataService(org.cerberus.crud.service.ITestDataLibDataService) ApplicationContext(org.springframework.context.ApplicationContext) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) ILogEventService(org.cerberus.crud.service.ILogEventService) FileReader(java.io.FileReader) TestDataLib(org.cerberus.crud.entity.TestDataLib) IFactoryTestDataLib(org.cerberus.crud.factory.IFactoryTestDataLib) IFactoryTestDataLib(org.cerberus.crud.factory.IFactoryTestDataLib) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) AnswerItem(org.cerberus.util.answer.AnswerItem) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) Answer(org.cerberus.util.answer.Answer) FileItem(org.apache.commons.fileupload.FileItem) JSONObject(org.json.JSONObject) BufferedReader(java.io.BufferedReader) ITestDataLibService(org.cerberus.crud.service.ITestDataLibService) FileUploadException(org.apache.commons.fileupload.FileUploadException) IFactoryTestDataLibData(org.cerberus.crud.factory.IFactoryTestDataLibData) TestDataLibData(org.cerberus.crud.entity.TestDataLibData)

Example 12 with TestDataLibData

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

the class ReadTestDataLibData method readByName.

private AnswerItem readByName(ApplicationContext appContext, String testDataLibName) throws JSONException {
    JSONObject jsonResponse = new JSONObject();
    ITestDataLibDataService testDataLibDataService = appContext.getBean(ITestDataLibDataService.class);
    AnswerList answer = testDataLibDataService.readByName(testDataLibName);
    // retrieves the data for the entry
    JSONArray jsonArray = new JSONArray();
    for (TestDataLibData subdata : (List<TestDataLibData>) answer.getDataList()) {
        jsonArray.put(convertTestDataLibDataToJSONObject(subdata));
    }
    jsonResponse.put("contentTable", jsonArray);
    jsonResponse.put("iTotalRecords", answer.getTotalRows());
    jsonResponse.put("iTotalDisplayRecords", answer.getTotalRows());
    AnswerItem item = new AnswerItem();
    item.setItem(jsonResponse);
    item.setResultMessage(answer.getResultMessage());
    return item;
}
Also used : ITestDataLibDataService(org.cerberus.crud.service.ITestDataLibDataService) AnswerList(org.cerberus.util.answer.AnswerList) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) AnswerList(org.cerberus.util.answer.AnswerList) List(java.util.List) AnswerItem(org.cerberus.util.answer.AnswerItem) TestDataLibData(org.cerberus.crud.entity.TestDataLibData)

Example 13 with TestDataLibData

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

the class TestDataLibService method readINTERNALWithSubdataByCriteria.

@Override
public AnswerList<HashMap<String, String>> readINTERNALWithSubdataByCriteria(String dataName, String dataSystem, String dataCountry, String dataEnvironment, int rowLimit, String system) {
    AnswerList answer = new AnswerList();
    AnswerList answerData = new AnswerList();
    MessageEvent msg;
    List<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();
    // We start by calculating the max nb of row we can fetch. Either specified by rowLimit either defined by a parameter.
    int maxSecurityFetch = 100;
    try {
        String maxSecurityFetch1 = parameterService.findParameterByKey("cerberus_testdatalib_fetchmax", system).getValue();
        maxSecurityFetch = Integer.valueOf(maxSecurityFetch1);
    } catch (CerberusException ex) {
        LOG.error(ex);
    }
    int maxFetch = maxSecurityFetch;
    if (rowLimit > 0 && rowLimit < maxSecurityFetch) {
        maxFetch = rowLimit;
    } else {
        maxFetch = maxSecurityFetch;
    }
    answer = this.readByVariousByCriteria(dataName, dataSystem, dataEnvironment, dataCountry, "INTERNAL", 0, maxFetch, null, null, null, null);
    List<TestDataLib> objectList = new ArrayList<TestDataLib>();
    objectList = answer.getDataList();
    for (TestDataLib tdl : objectList) {
        answerData = testDataLibDataService.readByVarious(tdl.getTestDataLibID(), null, null, null);
        List<TestDataLibData> objectDataList = new ArrayList<TestDataLibData>();
        objectDataList = answerData.getDataList();
        HashMap<String, String> row = new HashMap<String, String>();
        for (TestDataLibData tdld : objectDataList) {
            row.put(tdld.getSubData(), tdld.getValue());
        }
        row.put("TestDataLibID", String.valueOf(tdl.getTestDataLibID()));
        result.add(row);
    }
    answer.setDataList(result);
    return answer;
}
Also used : TestDataLib(org.cerberus.crud.entity.TestDataLib) AnswerList(org.cerberus.util.answer.AnswerList) CerberusException(org.cerberus.exception.CerberusException) HashMap(java.util.HashMap) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) IFactoryTestDataLibData(org.cerberus.crud.factory.IFactoryTestDataLibData) TestDataLibData(org.cerberus.crud.entity.TestDataLibData)

Example 14 with TestDataLibData

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

the class TestDataLibDataDAO method readByKey.

@Override
public AnswerItem readByKey(Integer testDataLibID, String subData) {
    AnswerItem answer = new AnswerItem();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
    TestDataLibData result = null;
    final String query = "SELECT * FROM testdatalibdata where `testdatalibID`=? and `subData` like ? ";
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
        LOG.debug("SQL.param.testDataLibID : " + testDataLibID);
        LOG.debug("SQL.param.subData : " + subData);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        preStat.setInt(1, testDataLibID);
        preStat.setString(2, subData);
        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 15 with TestDataLibData

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

the class TestDataLibDataDAO method readByCriteria.

@Override
public AnswerList readByCriteria(int start, int amount, String colName, String dir, String searchTerm, String individualSearch) {
    AnswerList answer = new AnswerList();
    MessageEvent msg;
    int nrTotalRows = 0;
    List<TestDataLibData> testDataLibListData = new ArrayList<TestDataLibData>();
    StringBuilder gSearch = new StringBuilder();
    StringBuilder searchSQL = new StringBuilder();
    StringBuilder query = new StringBuilder();
    // SQL_CALC_FOUND_ROWS allows to retrieve the total number of columns by disrearding the limit clauses that
    // were applied -- used for pagination p
    query.append("SELECT SQL_CALC_FOUND_ROWS * FROM testdatalibdata ");
    gSearch.append(" where (`subdata` like '%");
    gSearch.append(searchTerm);
    gSearch.append("%'");
    gSearch.append(" or `value` like '%");
    gSearch.append(searchTerm);
    gSearch.append("%'");
    gSearch.append(" or `column` like '%");
    gSearch.append(searchTerm);
    gSearch.append("%'");
    gSearch.append(" or `parsinganswer` like '%");
    gSearch.append(searchTerm);
    gSearch.append("%'");
    gSearch.append(" or `columnPosition` like '%");
    gSearch.append(searchTerm);
    gSearch.append("%'");
    gSearch.append(" or `description` like '%");
    gSearch.append("%') ");
    if (!searchTerm.equals("") && !individualSearch.equals("")) {
        searchSQL.append(gSearch.toString());
        searchSQL.append(" and ");
        searchSQL.append(individualSearch);
    } else if (!individualSearch.equals("")) {
        searchSQL.append(" where `");
        searchSQL.append(individualSearch);
        searchSQL.append("`");
    } else if (!searchTerm.equals("")) {
        searchSQL.append(gSearch.toString());
    }
    query.append(searchSQL);
    query.append("order by `");
    query.append(colName);
    query.append("` ");
    query.append(dir);
    if ((amount <= 0) || (amount >= MAX_ROW_SELECTED)) {
        query.append(" limit ").append(start).append(" , ").append(MAX_ROW_SELECTED);
    } else {
        query.append(" limit ").append(start).append(" , ").append(amount).append(" ");
    }
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        try {
            ResultSet resultSet = preStat.executeQuery();
            try {
                // gets the data
                while (resultSet.next()) {
                    testDataLibListData.add(this.loadFromResultSet(resultSet));
                }
                // get the total number of rows
                resultSet = preStat.executeQuery("SELECT FOUND_ROWS()");
                if (resultSet != null && resultSet.next()) {
                    nrTotalRows = resultSet.getInt(1);
                }
                if (testDataLibListData.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.warn("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));
                } else if (testDataLibListData.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()));
                testDataLibListData.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()));
        } 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.setTotalRows(nrTotalRows);
    answer.setResultMessage(msg);
    answer.setDataList(testDataLibListData);
    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) 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