Search in sources :

Example 1 with TestCaseLabel

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

the class TestCaseLabelDAO method readByCriteria.

@Override
public AnswerList<List<TestCaseLabel>> readByCriteria(int start, int amount, String column, String dir, String searchTerm, Map<String, List<String>> individualSearch) {
    AnswerList response = new AnswerList();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    List<TestCaseLabel> objectList = new ArrayList<>();
    StringBuilder searchSQL = new StringBuilder();
    List<String> individalColumnSearchValues = new ArrayList<>();
    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 testcaselabel tel");
    query.append(" LEFT OUTER JOIN label lab on lab.id = tel.labelId  ");
    searchSQL.append(" where 1=1 ");
    if (!StringUtil.isNullOrEmpty(searchTerm)) {
        searchSQL.append(" and (tel.`id` like ?");
        searchSQL.append(" or tel.`test` like ?");
        searchSQL.append(" or tel.`testcase` like ?");
        searchSQL.append(" or tel.`labelid` like ?");
        searchSQL.append(" or tel.`usrCreated` like ?");
        searchSQL.append(" or tel.`dateCreated` like ?");
        searchSQL.append(" or tel.`usrModif` like ?");
        searchSQL.append(" or tel.`dateModif` like ?)");
    }
    if (individualSearch != null && !individualSearch.isEmpty()) {
        searchSQL.append(" and ( 1=1 ");
        for (Map.Entry<String, List<String>> entry : individualSearch.entrySet()) {
            searchSQL.append(" and ");
            searchSQL.append(SqlUtil.getInSQLClauseForPreparedStatement(entry.getKey(), entry.getValue()));
            individalColumnSearchValues.addAll(entry.getValue());
        }
        searchSQL.append(" )");
    }
    query.append(searchSQL);
    if (!StringUtil.isNullOrEmpty(column)) {
        query.append(" order by `").append(column).append("` ").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);
    }
    // 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()) {
                Label label = labelDAO.loadFromResultSet(resultSet);
                objectList.add(this.loadFromResultSet(resultSet, label));
            }
            // get the total number of rows
            int nrTotalRows = 0;
            if (rowSet != null && rowSet.next()) {
                nrTotalRows = rowSet.getInt(1);
            }
            if (objectList.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));
                response = new AnswerList(objectList, nrTotalRows);
            } else if (objectList.size() <= 0) {
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                response = new AnswerList(objectList, nrTotalRows);
            } else {
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                response = new AnswerList(objectList, nrTotalRows);
            }
            response.setDataList(objectList);
        } 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 readByCriteria TestCaseLabel: " + e.getMessage());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", e.toString());
    } finally {
        response.setResultMessage(msg);
    }
    return response;
}
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) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) IFactoryTestCaseLabel(org.cerberus.crud.factory.IFactoryTestCaseLabel) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) Label(org.cerberus.crud.entity.Label) IFactoryTestCaseLabel(org.cerberus.crud.factory.IFactoryTestCaseLabel) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) AnswerList(org.cerberus.util.answer.AnswerList) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 2 with TestCaseLabel

use of org.cerberus.crud.entity.TestCaseLabel 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 3 with TestCaseLabel

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

the class ReadTestCaseExecution method findExecutionListByTag.

private AnswerItem findExecutionListByTag(ApplicationContext appContext, HttpServletRequest request, String Tag) throws CerberusException, ParseException, JSONException {
    AnswerItem answer = new AnswerItem(new MessageEvent(MessageEventEnum.DATA_OPERATION_OK));
    testCaseLabelService = appContext.getBean(ITestCaseLabelService.class);
    int startPosition = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayStart"), "0"));
    int length = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayLength"), "0"));
    String searchParameter = ParameterParserUtil.parseStringParam(request.getParameter("sSearch"), "");
    String sColumns = ParameterParserUtil.parseStringParam(request.getParameter("sColumns"), "test,testCase,application,priority,status,description,bugId,function");
    String[] columnToSort = sColumns.split(",");
    // Get Sorting information
    int numberOfColumnToSort = Integer.parseInt(ParameterParserUtil.parseStringParam(request.getParameter("iSortingCols"), "1"));
    int columnToSortParameter = 0;
    String sort = "asc";
    StringBuilder sortInformation = new StringBuilder();
    for (int c = 0; c < numberOfColumnToSort; c++) {
        columnToSortParameter = Integer.parseInt(ParameterParserUtil.parseStringParam(request.getParameter("iSortCol_" + c), "0"));
        sort = ParameterParserUtil.parseStringParam(request.getParameter("sSortDir_" + c), "asc");
        String columnName = columnToSort[columnToSortParameter];
        sortInformation.append(columnName).append(" ").append(sort);
        if (c != numberOfColumnToSort - 1) {
            sortInformation.append(" , ");
        }
    }
    Map<String, List<String>> individualSearch = new HashMap<String, List<String>>();
    for (int a = 0; a < columnToSort.length; a++) {
        if (null != request.getParameter("sSearch_" + a) && !request.getParameter("sSearch_" + a).isEmpty()) {
            List<String> search = new ArrayList(Arrays.asList(request.getParameter("sSearch_" + a).split(",")));
            individualSearch.put(columnToSort[a], search);
        }
    }
    List<TestCaseExecution> testCaseExecutions = readExecutionByTagList(appContext, Tag, startPosition, length, sortInformation.toString(), searchParameter, individualSearch);
    JSONArray executionList = new JSONArray();
    JSONObject statusFilter = getStatusList(request);
    JSONObject countryFilter = getCountryList(request, appContext);
    LinkedHashMap<String, JSONObject> ttc = new LinkedHashMap<String, JSONObject>();
    String globalStart = "";
    String globalEnd = "";
    String globalStatus = "Finished";
    /**
     * Find the list of labels
     */
    AnswerList testCaseLabelList = testCaseLabelService.readByTestTestCase(null, null);
    for (TestCaseExecution testCaseExecution : testCaseExecutions) {
        try {
            if (testCaseExecution.getStart() != 0) {
                if ((globalStart.isEmpty()) || (globalStart.compareTo(String.valueOf(testCaseExecution.getStart())) > 0)) {
                    globalStart = String.valueOf(testCaseExecution.getStart());
                }
            }
            if (testCaseExecution.getEnd() != 0) {
                if ((globalEnd.isEmpty()) || (globalEnd.compareTo(String.valueOf(testCaseExecution.getEnd())) < 0)) {
                    globalEnd = String.valueOf(testCaseExecution.getEnd());
                }
            }
            if (testCaseExecution.getControlStatus().equalsIgnoreCase("PE")) {
                globalStatus = "Pending...";
            }
            String controlStatus = testCaseExecution.getControlStatus();
            if (statusFilter.get(controlStatus).equals("on") && countryFilter.get(testCaseExecution.getCountry()).equals("on")) {
                JSONObject execution = testCaseExecutionToJSONObject(testCaseExecution);
                String execKey = testCaseExecution.getEnvironment() + " " + testCaseExecution.getCountry() + " " + testCaseExecution.getBrowser();
                String testCaseKey = testCaseExecution.getTest() + "_" + testCaseExecution.getTestCase();
                JSONObject execTab = new JSONObject();
                executionList.put(testCaseExecutionToJSONObject(testCaseExecution));
                JSONObject ttcObject = new JSONObject();
                if (ttc.containsKey(testCaseKey)) {
                    ttcObject = ttc.get(testCaseKey);
                    execTab = ttcObject.getJSONObject("execTab");
                    execTab.put(execKey, execution);
                    ttcObject.put("execTab", execTab);
                } else {
                    ttcObject.put("test", testCaseExecution.getTest());
                    ttcObject.put("testCase", testCaseExecution.getTestCase());
                    ttcObject.put("function", testCaseExecution.getTestCaseObj().getFunction());
                    ttcObject.put("shortDesc", testCaseExecution.getTestCaseObj().getDescription());
                    ttcObject.put("status", testCaseExecution.getStatus());
                    ttcObject.put("application", testCaseExecution.getApplication());
                    ttcObject.put("priority", testCaseExecution.getTestCaseObj().getPriority());
                    ttcObject.put("bugId", new JSONObject("{\"bugId\":\"" + testCaseExecution.getTestCaseObj().getBugID() + "\",\"bugTrackerUrl\":\"" + testCaseExecution.getApplicationObj().getBugTrackerUrl().replace("%BUGID%", testCaseExecution.getTestCaseObj().getBugID()) + "\"}"));
                    ttcObject.put("comment", testCaseExecution.getTestCaseObj().getComment());
                    execTab.put(execKey, execution);
                    ttcObject.put("execTab", execTab);
                    /**
                     * Iterate on the label retrieved and generate HashMap
                     * based on the key Test_TestCase
                     */
                    LinkedHashMap<String, JSONArray> testCaseWithLabel = new LinkedHashMap();
                    for (TestCaseLabel label : (List<TestCaseLabel>) testCaseLabelList.getDataList()) {
                        String key = label.getTest() + "_" + label.getTestcase();
                        if (testCaseWithLabel.containsKey(key)) {
                            JSONObject jo = new JSONObject().put("name", label.getLabel().getLabel()).put("color", label.getLabel().getColor()).put("description", label.getLabel().getDescription());
                            testCaseWithLabel.get(key).put(jo);
                        } else {
                            JSONObject jo = new JSONObject().put("name", label.getLabel().getLabel()).put("color", label.getLabel().getColor()).put("description", label.getLabel().getDescription());
                            testCaseWithLabel.put(key, new JSONArray().put(jo));
                        }
                    }
                    ttcObject.put("labels", testCaseWithLabel.get(testCaseExecution.getTest() + "_" + testCaseExecution.getTestCase()));
                }
                ttc.put(testCaseExecution.getTest() + "_" + testCaseExecution.getTestCase(), ttcObject);
            }
        } catch (JSONException ex) {
            LOG.warn(ex);
        }
    }
    JSONObject jsonResponse = new JSONObject();
    jsonResponse.put("globalEnd", globalEnd.toString());
    jsonResponse.put("globalStart", globalStart.toString());
    jsonResponse.put("globalStatus", globalStatus);
    jsonResponse.put("testList", ttc.values());
    jsonResponse.put("iTotalRecords", ttc.size());
    jsonResponse.put("iTotalDisplayRecords", ttc.size());
    answer.setItem(jsonResponse);
    answer.setResultMessage(new MessageEvent(MessageEventEnum.DATA_OPERATION_OK));
    return answer;
}
Also used : TestCaseExecution(org.cerberus.crud.entity.TestCaseExecution) AnswerList(org.cerberus.util.answer.AnswerList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MessageEvent(org.cerberus.engine.entity.MessageEvent) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) AnswerItem(org.cerberus.util.answer.AnswerItem) LinkedHashMap(java.util.LinkedHashMap) JSONObject(org.json.JSONObject) AnswerList(org.cerberus.util.answer.AnswerList) List(java.util.List) ArrayList(java.util.ArrayList) ITestCaseLabelService(org.cerberus.crud.service.ITestCaseLabelService)

Example 4 with TestCaseLabel

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

the class ReadTestCaseExecutionByTag method generateLabelStats.

private JSONObject generateLabelStats(ApplicationContext appContext, HttpServletRequest request, List<TestCaseExecution> testCaseExecutions, JSONObject statusFilter, JSONObject countryFilter) throws JSONException {
    JSONObject jsonResult = new JSONObject();
    boolean stickers = request.getParameter("stickers") != null;
    boolean requirement = request.getParameter("requirement") != null;
    if (stickers || requirement) {
        testCaseLabelService = appContext.getBean(ITestCaseLabelService.class);
        AnswerList testCaseLabelList = testCaseLabelService.readByTestTestCase(null, null);
        SummaryStatisticsDTO total = new SummaryStatisticsDTO();
        total.setEnvironment("Total");
        /**
         * Iterate on the label retrieved and generate HashMap based on the
         * key Test_TestCase
         */
        LinkedHashMap<String, JSONArray> testCaseWithLabel = new LinkedHashMap();
        for (TestCaseLabel label : (List<TestCaseLabel>) testCaseLabelList.getDataList()) {
            if ((Label.TYPE_STICKER.equals(label.getLabel().getType()) && stickers) || (Label.TYPE_REQUIREMENT.equals(label.getLabel().getType()) && requirement)) {
                String key = label.getTest() + "_" + label.getTestcase();
                JSONObject jo = new JSONObject().put("name", label.getLabel().getLabel()).put("color", label.getLabel().getColor()).put("description", label.getLabel().getDescription());
                if (testCaseWithLabel.containsKey(key)) {
                    testCaseWithLabel.get(key).put(jo);
                } else {
                    testCaseWithLabel.put(key, new JSONArray().put(jo));
                }
            }
        }
        /**
         * For All execution, get all label and generate statistics
         */
        LinkedHashMap<String, SummaryStatisticsDTO> labelPerTestcaseExecution = new LinkedHashMap();
        for (TestCaseExecution testCaseExecution : testCaseExecutions) {
            String controlStatus = testCaseExecution.getControlStatus();
            if (statusFilter.get(controlStatus).equals("on") && countryFilter.get(testCaseExecution.getCountry()).equals("on")) {
                // Get label for current test_testcase
                JSONArray labelsForTestCase = testCaseWithLabel.get(testCaseExecution.getTest() + "_" + testCaseExecution.getTestCase());
                if (labelsForTestCase != null) {
                    for (int index = 0; index < labelsForTestCase.length(); index++) {
                        JSONObject j = labelsForTestCase.getJSONObject(index);
                        if (labelPerTestcaseExecution.containsKey(j.get("name"))) {
                            labelPerTestcaseExecution.get(j.get("name").toString()).updateStatisticByStatus(controlStatus);
                        } else {
                            SummaryStatisticsDTO stat = new SummaryStatisticsDTO();
                            stat.setLabel(j);
                            stat.updateStatisticByStatus(controlStatus);
                            labelPerTestcaseExecution.put(j.get("name").toString(), stat);
                        }
                        total.updateStatisticByStatus(controlStatus);
                    }
                }
            }
        }
        jsonResult.put("labelStats", extractSummaryData(labelPerTestcaseExecution, total, true));
    }
    return jsonResult;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) TestCaseExecution(org.cerberus.crud.entity.TestCaseExecution) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) JSONArray(org.json.JSONArray) LinkedHashMap(java.util.LinkedHashMap) JSONObject(org.json.JSONObject) SummaryStatisticsDTO(org.cerberus.dto.SummaryStatisticsDTO) AnswerList(org.cerberus.util.answer.AnswerList) ArrayList(java.util.ArrayList) List(java.util.List) ITestCaseLabelService(org.cerberus.crud.service.ITestCaseLabelService)

Example 5 with TestCaseLabel

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

the class TestCaseLabelService method duplicateList.

@Override
public Answer duplicateList(List<TestCaseLabel> dataList, String targetTest, String targetTestCase) {
    Answer ans = new Answer(null);
    List<TestCaseLabel> listToCreate = new ArrayList();
    for (TestCaseLabel objectToDuplicate : dataList) {
        objectToDuplicate.setTest(targetTest);
        objectToDuplicate.setTestcase(targetTestCase);
        listToCreate.add(objectToDuplicate);
    }
    return createList(listToCreate);
}
Also used : Answer(org.cerberus.util.answer.Answer) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) ArrayList(java.util.ArrayList)

Aggregations

TestCaseLabel (org.cerberus.crud.entity.TestCaseLabel)23 ArrayList (java.util.ArrayList)17 JSONObject (org.json.JSONObject)16 ITestCaseLabelService (org.cerberus.crud.service.ITestCaseLabelService)14 IFactoryTestCaseLabel (org.cerberus.crud.factory.IFactoryTestCaseLabel)12 MessageEvent (org.cerberus.engine.entity.MessageEvent)12 AnswerItem (org.cerberus.util.answer.AnswerItem)12 AnswerList (org.cerberus.util.answer.AnswerList)9 JSONArray (org.json.JSONArray)9 List (java.util.List)8 Answer (org.cerberus.util.answer.Answer)8 TestCase (org.cerberus.crud.entity.TestCase)6 PolicyFactory (org.owasp.html.PolicyFactory)6 ApplicationContext (org.springframework.context.ApplicationContext)6 TestCaseCountry (org.cerberus.crud.entity.TestCaseCountry)5 ILogEventService (org.cerberus.crud.service.ILogEventService)5 ITestCaseCountryService (org.cerberus.crud.service.ITestCaseCountryService)5 ITestCaseService (org.cerberus.crud.service.ITestCaseService)5 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4