Search in sources :

Example 16 with TestCaseLabel

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

the class CreateTestCase 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, CerberusException, JSONException {
    JSONObject jsonResponse = new JSONObject();
    Answer ans = new Answer();
    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);
    response.setContentType("application/json");
    // Calling Servlet Transversal Util.
    ServletUtil.servletStart(request);
    /**
     * Parsing and securing all required parameters.
     */
    String test = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("test"), "");
    String testcase = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("testCase"), "");
    String originalTest = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("originalTest"), "");
    String originalTestCase = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("originalTestCase"), "");
    // Prepare the final answer.
    MessageEvent msg1 = new MessageEvent(MessageEventEnum.GENERIC_OK);
    Answer finalAnswer = new Answer(msg1);
    /**
     * Checking all constrains before calling the services.
     */
    if (StringUtil.isNullOrEmpty(test) && StringUtil.isNullOrEmpty(testcase)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Test Case").replace("%OPERATION%", "Create").replace("%REASON%", "mandatory fields (test or testcase) are missing."));
        finalAnswer.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        testCaseService = appContext.getBean(ITestCaseService.class);
        testCaseLabelService = appContext.getBean(ITestCaseLabelService.class);
        testCaseLabelFactory = appContext.getBean(IFactoryTestCaseLabel.class);
        testCaseCountryService = appContext.getBean(ITestCaseCountryService.class);
        testCaseCountryFactory = appContext.getBean(IFactoryTestCaseCountry.class);
        testCaseCountryPropertiesService = appContext.getBean(ITestCaseCountryPropertiesService.class);
        testCaseStepService = appContext.getBean(ITestCaseStepService.class);
        testCaseStepActionService = appContext.getBean(ITestCaseStepActionService.class);
        testCaseStepActionControlService = appContext.getBean(ITestCaseStepActionControlService.class);
        TestCase testCaseData = getTestCaseFromRequest(request);
        ans = testCaseService.create(testCaseData);
        finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
        if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
            /**
             * Object created. Adding Log entry.
             */
            ILogEventService logEventService = appContext.getBean(LogEventService.class);
            logEventService.createForPrivateCalls("/CreateTestCase", "CREATE", "Create TestCase : ['" + testcase + "']", request);
            // Update labels
            if (request.getParameter("labelList") != null) {
                JSONArray objLabelArray = new JSONArray(request.getParameter("labelList"));
                List<TestCaseLabel> labelList = new ArrayList();
                labelList = getLabelListFromRequest(request, appContext, test, testcase, objLabelArray);
                // Update the Database with the new list.
                ans = testCaseLabelService.compareListAndUpdateInsertDeleteElements(test, testcase, labelList);
                finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
            }
            // Update Countries
            if (request.getParameter("countryList") != null) {
                JSONArray objCountryArray = new JSONArray(request.getParameter("countryList"));
                List<TestCaseCountry> tccList = new ArrayList();
                tccList = getCountryListFromRequest(request, appContext, test, testcase, objCountryArray);
                // Update the Database with the new list.
                ans = testCaseCountryService.compareListAndUpdateInsertDeleteElements(test, testcase, tccList);
                finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
                // Duplicate other objects.
                List<TestCaseCountryProperties> tccpList = new ArrayList();
                List<TestCaseCountryProperties> newTccpList = new ArrayList();
                if (!tccList.isEmpty() && ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                    tccpList = testCaseCountryPropertiesService.findListOfPropertyPerTestTestCase(originalTest, originalTestCase);
                    // Build a new list with the countries that exist for the testcase.
                    for (TestCaseCountryProperties curTccp : tccpList) {
                        if (testCaseCountryService.exist(test, testcase, curTccp.getCountry())) {
                            newTccpList.add(curTccp);
                        }
                    }
                    if (!newTccpList.isEmpty()) {
                        ans = testCaseCountryPropertiesService.duplicateList(newTccpList, test, testcase);
                        finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
                    }
                }
            }
            List<TestCaseStep> tcsList = new ArrayList();
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                tcsList = testCaseStepService.getListOfSteps(originalTest, originalTestCase);
                if (!tcsList.isEmpty()) {
                    ans = testCaseStepService.duplicateList(tcsList, test, testcase);
                    finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
                }
            }
            List<TestCaseStepAction> tcsaList = new ArrayList();
            if (!tcsList.isEmpty() && ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                tcsaList = testCaseStepActionService.findTestCaseStepActionbyTestTestCase(originalTest, originalTestCase);
                if (!tcsaList.isEmpty()) {
                    ans = testCaseStepActionService.duplicateList(tcsaList, test, testcase);
                    finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
                }
            }
            if (!tcsList.isEmpty() && !tcsaList.isEmpty() && ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                List<TestCaseStepActionControl> tcsacList = testCaseStepActionControlService.findControlByTestTestCase(originalTest, originalTestCase);
                if (!tcsacList.isEmpty()) {
                    ans = testCaseStepActionControlService.duplicateList(tcsacList, test, testcase);
                    finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
                }
            }
        }
    }
    /**
     * Formating and returning the json result.
     */
    jsonResponse.put("messageType", finalAnswer.getResultMessage().getMessage().getCodeString());
    jsonResponse.put("message", finalAnswer.getResultMessage().getDescription());
    response.getWriter().print(jsonResponse);
    response.getWriter().flush();
}
Also used : ITestCaseStepActionService(org.cerberus.crud.service.ITestCaseStepActionService) TestCaseStepAction(org.cerberus.crud.entity.TestCaseStepAction) TestCaseCountryProperties(org.cerberus.crud.entity.TestCaseCountryProperties) PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) TestCaseStep(org.cerberus.crud.entity.TestCaseStep) ITestCaseCountryService(org.cerberus.crud.service.ITestCaseCountryService) ApplicationContext(org.springframework.context.ApplicationContext) ITestCaseCountryPropertiesService(org.cerberus.crud.service.ITestCaseCountryPropertiesService) ITestCaseService(org.cerberus.crud.service.ITestCaseService) ILogEventService(org.cerberus.crud.service.ILogEventService) TestCaseCountry(org.cerberus.crud.entity.TestCaseCountry) IFactoryTestCaseCountry(org.cerberus.crud.factory.IFactoryTestCaseCountry) TestCaseStepActionControl(org.cerberus.crud.entity.TestCaseStepActionControl) ITestCaseLabelService(org.cerberus.crud.service.ITestCaseLabelService) ITestCaseStepService(org.cerberus.crud.service.ITestCaseStepService) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) IFactoryTestCaseLabel(org.cerberus.crud.factory.IFactoryTestCaseLabel) JSONArray(org.json.JSONArray) IFactoryTestCaseCountry(org.cerberus.crud.factory.IFactoryTestCaseCountry) ITestCaseStepActionControlService(org.cerberus.crud.service.ITestCaseStepActionControlService) Answer(org.cerberus.util.answer.Answer) JSONObject(org.json.JSONObject) TestCase(org.cerberus.crud.entity.TestCase) IFactoryTestCaseLabel(org.cerberus.crud.factory.IFactoryTestCaseLabel)

Example 17 with TestCaseLabel

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

the class CreateTestCase method getLabelListFromRequest.

private List<TestCaseLabel> getLabelListFromRequest(HttpServletRequest request, ApplicationContext appContext, String test, String testCase, JSONArray json) throws CerberusException, JSONException, UnsupportedEncodingException {
    List<TestCaseLabel> labelList = new ArrayList();
    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 labelId = objectJson.getInt("labelId");
        Timestamp creationDate = new Timestamp(new Date().getTime());
        if (!delete) {
            labelList.add(testCaseLabelFactory.create(0, test, testCase, labelId, request.getRemoteUser(), creationDate, request.getRemoteUser(), creationDate, null));
        }
    }
    return labelList;
}
Also used : JSONObject(org.json.JSONObject) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) IFactoryTestCaseLabel(org.cerberus.crud.factory.IFactoryTestCaseLabel) ArrayList(java.util.ArrayList) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Example 18 with TestCaseLabel

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

the class CreateTestCaseLabel 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, CerberusException, JSONException {
    JSONObject jsonResponse = new JSONObject();
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    ILogEventService logEventService = appContext.getBean(LogEventService.class);
    Answer ans = new Answer();
    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");
    // Calling Servlet Transversal Util.
    ServletUtil.servletStart(request);
    /**
     * Parsing and securing all required parameters.
     */
    // Parameter that are already controled by GUI (no need to decode) --> We SECURE them
    // Parameter that needs to be secured --> We SECURE+DECODE them
    // Parameter that we cannot secure as we need the html --> We DECODE them
    Integer myIdInt = 0;
    String[] myLabelIdList = request.getParameterValues("labelid");
    String[] myTestList = request.getParameterValues("test");
    String[] myTestCaseList = request.getParameterValues("testcase");
    if ((myTestList.length == 0) || (myTestCaseList.length == 0) || (myLabelIdList.length == 0)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Create").replace("%REASON%", "Missing Parameter (either test, testcase or labelid)."));
        ans.setResultMessage(msg);
    } else if (myTestList.length != myTestCaseList.length) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Create").replace("%REASON%", "Number of Test does not match number of testcase."));
        ans.setResultMessage(msg);
    }
    StringBuilder output_message = new StringBuilder();
    int massErrorCounter = 0;
    for (int i = 0; i < myLabelIdList.length; i++) {
        String myLabelId = myLabelIdList[i];
        myIdInt = 0;
        boolean label_error = true;
        try {
            if (myLabelId != null && !myLabelId.equals("")) {
                myIdInt = Integer.valueOf(policy.sanitize(myLabelId));
                label_error = false;
            }
        } catch (Exception ex) {
            label_error = true;
        }
        /**
         * Checking all constrains before calling the services.
         */
        if (label_error) {
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
            msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Update").replace("%REASON%", "Could not manage to convert labelid to an integer value or labelid is missing."));
            ans.setResultMessage(msg);
            massErrorCounter++;
            output_message.append("<br>id : ").append(myLabelId).append(" - ").append(msg.getDescription());
        } else {
            /**
             * All data seems cleans so we can call the services.
             */
            ILabelService labelService = appContext.getBean(ILabelService.class);
            IFactoryTestCaseLabel factoryTestCaseLabel = appContext.getBean(IFactoryTestCaseLabel.class);
            ITestCaseLabelService testCaseLabelService = appContext.getBean(ITestCaseLabelService.class);
            ITestCaseService testCaseService = appContext.getBean(ITestCaseService.class);
            IApplicationService applicationService = appContext.getBean(IApplicationService.class);
            AnswerItem resp = labelService.readByKey(myIdInt);
            if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {
                /**
                 * Object could not be found. We stop here and report the
                 * error.
                 */
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
                msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Create").replace("%REASON%", "Label does not exist."));
                ans.setResultMessage(msg);
                massErrorCounter++;
                output_message.append("<br>labelid : ").append(myLabelId).append(" - ").append(msg.getDescription());
            } else {
                Label myLab = (Label) resp.getItem();
                for (int j = 0; j < myTestList.length; j++) {
                    String myTest = myTestList[j];
                    String myTestCase = myTestCaseList[j];
                    resp = testCaseService.readByKey(myTest, myTestCase);
                    if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {
                        /**
                         * Object could not be found. We stop here and
                         * report the error.
                         */
                        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
                        msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Create").replace("%REASON%", "Test Case does not exist."));
                        ans.setResultMessage(msg);
                        massErrorCounter++;
                        output_message.append("<br>testcase : ").append(myLabelId).append(" - ").append(msg.getDescription());
                    } else {
                        TestCase myTestCaseObj = (TestCase) resp.getItem();
                        resp = applicationService.readByKey(myTestCaseObj.getApplication());
                        if ((resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {
                            Application myApplication = (Application) resp.getItem();
                            if ((StringUtil.isNullOrEmpty(myLab.getSystem())) || (myApplication.getSystem().equals(myLab.getSystem()))) {
                                TestCaseLabel tcLabel = factoryTestCaseLabel.create(0, myTest, myTestCase, myIdInt, request.getRemoteUser(), null, "", null, null);
                                ans = testCaseLabelService.create(tcLabel);
                                if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                                    /**
                                     * Update was successful. Adding Log
                                     * entry.
                                     */
                                    logEventService.createForPrivateCalls("/CreateTestCaseLabel", "CREATE", "Created TestCaseLabel : ['" + myIdInt + "'|'" + myTest + "'|'" + myTestCase + "']", request);
                                } else {
                                    massErrorCounter++;
                                    output_message.append("<br>Label : ").append(myLabelId).append(" Test : '").append(myTest).append("' TestCase : '").append(myTestCase).append("' - ").append(ans.getResultMessage().getDescription());
                                }
                            } else {
                                massErrorCounter++;
                                output_message.append("<br>Label : ").append(myLabelId).append(" Test : '").append(myTest).append("' TestCase : '").append(myTestCase).append("' - ").append("Label does not belong to the same system as TestCase system.");
                            }
                        }
                    }
                }
            }
        }
    }
    if (myTestList.length > 1) {
        if (massErrorCounter == myTestList.length) {
            // All updates are in ERROR.
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
            msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Mass Update").replace("%REASON%", massErrorCounter + " label links(s) out of " + (myTestList.length * myLabelIdList.length) + " failed to be created due to an issue.<br>") + output_message.toString());
            ans.setResultMessage(msg);
        } else if (massErrorCounter > 0) {
            // At least 1 update in error
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_WARNING);
            msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Mass Update").replace("%REASON%", massErrorCounter + " label links(s) out of " + (myTestList.length * myLabelIdList.length) + " failed to be created due to an issue.<br>") + output_message.toString());
            ans.setResultMessage(msg);
        } else {
            // No error detected.
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
            msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Mass Update") + "\n\nAll " + (myTestList.length * myLabelIdList.length) + " label links(s) created successfuly.");
            ans.setResultMessage(msg);
        }
        logEventService.createForPrivateCalls("/CreateTestCaseLabel", "MASSUPDATE", msg.getDescription(), request);
    }
    /**
     * Formating and returning the json result.
     */
    jsonResponse.put("messageType", ans.getResultMessage().getMessage().getCodeString());
    jsonResponse.put("message", ans.getResultMessage().getDescription());
    response.getWriter().print(jsonResponse);
    response.getWriter().flush();
}
Also used : PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) IFactoryTestCaseLabel(org.cerberus.crud.factory.IFactoryTestCaseLabel) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) Label(org.cerberus.crud.entity.Label) IFactoryTestCaseLabel(org.cerberus.crud.factory.IFactoryTestCaseLabel) AnswerItem(org.cerberus.util.answer.AnswerItem) ServletException(javax.servlet.ServletException) JSONException(org.json.JSONException) IOException(java.io.IOException) CerberusException(org.cerberus.exception.CerberusException) ILabelService(org.cerberus.crud.service.ILabelService) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) TestCase(org.cerberus.crud.entity.TestCase) ITestCaseService(org.cerberus.crud.service.ITestCaseService) ILogEventService(org.cerberus.crud.service.ILogEventService) Application(org.cerberus.crud.entity.Application) IFactoryTestCaseLabel(org.cerberus.crud.factory.IFactoryTestCaseLabel) ITestCaseLabelService(org.cerberus.crud.service.ITestCaseLabelService) IApplicationService(org.cerberus.crud.service.IApplicationService)

Example 19 with TestCaseLabel

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

the class ReadTestCase method findTestCaseByTestTestCase.

private AnswerItem findTestCaseByTestTestCase(String test, String testCase, ApplicationContext appContext, HttpServletRequest request) throws JSONException {
    AnswerItem item = new AnswerItem();
    JSONObject object = new JSONObject();
    testCaseService = appContext.getBean(ITestCaseService.class);
    testCaseCountryService = appContext.getBean(ITestCaseCountryService.class);
    testCaseLabelService = appContext.getBean(ITestCaseLabelService.class);
    // finds the project
    AnswerItem answerTestCase = testCaseService.readByKey(test, testCase);
    if (answerTestCase.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && answerTestCase.getItem() != null) {
        // if the service returns an OK message then we can get the item and convert it to JSONformat
        TestCase tc = (TestCase) answerTestCase.getItem();
        JSONObject response = convertToJSONObject(tc);
        // Country List feed.
        JSONArray countryArray = new JSONArray();
        AnswerList answerTestCaseCountryList = testCaseCountryService.readByTestTestCase(null, test, testCase);
        for (TestCaseCountry country : (List<TestCaseCountry>) answerTestCaseCountryList.getDataList()) {
            countryArray.put(convertToJSONObject(country));
        }
        response.put("countryList", countryArray);
        // Label List feed.
        JSONArray labelArray = new JSONArray();
        AnswerList answerTestCaseLabelList = testCaseLabelService.readByTestTestCase(test, testCase);
        for (TestCaseLabel label : (List<TestCaseLabel>) answerTestCaseLabelList.getDataList()) {
            labelArray.put(convertToJSONObject(label));
        }
        response.put("labelList", labelArray);
        object.put("contentTable", response);
        object.put("hasPermissionsUpdate", testCaseService.hasPermissionsUpdate(tc, request));
    }
    item.setItem(object);
    item.setResultMessage(answerTestCase.getResultMessage());
    return item;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) JSONObject(org.json.JSONObject) TestCase(org.cerberus.crud.entity.TestCase) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) ITestCaseService(org.cerberus.crud.service.ITestCaseService) JSONArray(org.json.JSONArray) TestCaseCountry(org.cerberus.crud.entity.TestCaseCountry) AnswerList(org.cerberus.util.answer.AnswerList) ArrayList(java.util.ArrayList) List(java.util.List) AnswerItem(org.cerberus.util.answer.AnswerItem) ITestCaseLabelService(org.cerberus.crud.service.ITestCaseLabelService) ITestCaseCountryService(org.cerberus.crud.service.ITestCaseCountryService)

Example 20 with TestCaseLabel

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

the class ReadTestCase method findTestCaseByTest.

// </editor-fold>
private AnswerItem findTestCaseByTest(String system, String test, ApplicationContext appContext, HttpServletRequest request) throws JSONException {
    AnswerItem answer = new AnswerItem();
    JSONObject object = new JSONObject();
    testCaseService = appContext.getBean(ITestCaseService.class);
    testCaseCountryService = appContext.getBean(ITestCaseCountryService.class);
    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"), "tec.test,tec.testcase,tec.application,project,ticket,description,behaviororvalueexpected,readonly,bugtrackernewurl,deploytype,mavengroupid");
    String[] columnToSort = sColumns.split(",");
    List<String> individualLike = new ArrayList(Arrays.asList(ParameterParserUtil.parseStringParam(request.getParameter("sLike"), "").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(",")));
            if (individualLike.contains(columnToSort[a])) {
                individualSearch.put(columnToSort[a] + ":like", search);
            } else {
                individualSearch.put(columnToSort[a], search);
            }
        }
    }
    AnswerList testCaseList = testCaseService.readByTestByCriteria(system, test, startPosition, length, sortInformation.toString(), searchParameter, individualSearch);
    AnswerList testCaseCountryList = testCaseCountryService.readByTestTestCase(system, test, null);
    /**
     * Find the list of labels
     */
    AnswerList testCaseLabelList = testCaseLabelService.readByTestTestCase(test, null);
    LinkedHashMap<String, JSONObject> testCaseWithCountry = new LinkedHashMap();
    for (TestCaseCountry country : (List<TestCaseCountry>) testCaseCountryList.getDataList()) {
        String key = country.getTest() + "_" + country.getTestCase();
        if (testCaseWithCountry.containsKey(key)) {
            testCaseWithCountry.get(key).put(country.getCountry(), country.getCountry());
        } else {
            testCaseWithCountry.put(key, new JSONObject().put(country.getCountry(), country.getCountry()));
        }
    }
    /**
     * 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));
        }
    }
    JSONArray jsonArray = new JSONArray();
    if (testCaseList.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        // the service was able to perform the query, then we should get all values
        for (TestCase testCase : (List<TestCase>) testCaseList.getDataList()) {
            String key = testCase.getTest() + "_" + testCase.getTestCase();
            JSONObject value = convertToJSONObject(testCase);
            value.put("hasPermissionsDelete", testCaseService.hasPermissionsDelete(testCase, request));
            value.put("hasPermissionsUpdate", testCaseService.hasPermissionsUpdate(testCase, request));
            value.put("hasPermissionsCreate", testCaseService.hasPermissionsCreate(testCase, request));
            value.put("countryList", testCaseWithCountry.get(key));
            value.put("labels", testCaseWithLabel.get(key));
            jsonArray.put(value);
        }
    }
    // object.put("hasPermissions", testCaseService.hasPermissions(request));
    object.put("hasPermissionsCreate", testCaseService.hasPermissionsCreate(null, request));
    object.put("contentTable", jsonArray);
    object.put("iTotalRecords", testCaseList.getTotalRows());
    object.put("iTotalDisplayRecords", testCaseList.getTotalRows());
    answer.setItem(object);
    answer.setResultMessage(testCaseList.getResultMessage());
    return answer;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) AnswerItem(org.cerberus.util.answer.AnswerItem) ITestCaseCountryService(org.cerberus.crud.service.ITestCaseCountryService) LinkedHashMap(java.util.LinkedHashMap) JSONObject(org.json.JSONObject) TestCase(org.cerberus.crud.entity.TestCase) ITestCaseService(org.cerberus.crud.service.ITestCaseService) TestCaseCountry(org.cerberus.crud.entity.TestCaseCountry) AnswerList(org.cerberus.util.answer.AnswerList) ArrayList(java.util.ArrayList) List(java.util.List) ITestCaseLabelService(org.cerberus.crud.service.ITestCaseLabelService)

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