Search in sources :

Example 1 with ITestCaseExecutionQueueService

use of org.cerberus.crud.service.ITestCaseExecutionQueueService in project cerberus-source by cerberustesting.

the class ReadTestCaseExecution method findExecutionListBySystem.

private AnswerItem findExecutionListBySystem(String system, ApplicationContext appContext, HttpServletRequest request) throws ParseException, JSONException {
    AnswerItem answer = new AnswerItem(new MessageEvent(MessageEventEnum.DATA_OPERATION_OK));
    /**
     * Parse all parameters used in the search.
     */
    String charset = request.getCharacterEncoding();
    /**
     * Parse parameters - list of values
     */
    List<String> testList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("test"), null, charset);
    List<String> applicationList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("application"), null, charset);
    List<String> projectList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("project"), null, charset);
    List<String> tcstatusList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("tcstatus"), null, charset);
    List<String> groupList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("group"), null, charset);
    List<String> tcactiveList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("tcactive"), null, charset);
    List<String> priorityList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("priority"), null, charset);
    List<String> targetsprintList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("targetsprint"), null, charset);
    List<String> targetrevisionList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("targetrevision"), null, charset);
    List<String> creatorList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("creator"), null, charset);
    List<String> implementerList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("implementer"), null, charset);
    List<String> environmentList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("environment"), null, charset);
    List<String> buildList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("build"), null, charset);
    List<String> revisionList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("revision"), null, charset);
    List<String> countryList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("country"), null, charset);
    List<String> browserList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("browser"), null, charset);
    List<String> tcestatusList = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("tcestatus"), null, charset);
    // Sorts the lists
    if (countryList != null) {
        Collections.sort(countryList);
    }
    if (browserList != null) {
        Collections.sort(browserList);
    }
    /**
     * Parse parameters - free text
     */
    String bugid = StringEscapeUtils.escapeHtml4(request.getParameter("bugid"));
    String ticket = StringEscapeUtils.escapeHtml4(request.getParameter("ticket"));
    String ip = StringEscapeUtils.escapeHtml4(request.getParameter("ip"));
    String port = StringEscapeUtils.escapeHtml4(request.getParameter("port"));
    String tag = StringEscapeUtils.escapeHtml4(request.getParameter("tag"));
    String browserversion = StringEscapeUtils.escapeHtml4(request.getParameter("browserversion"));
    String comment = StringEscapeUtils.escapeHtml4(request.getParameter("comment"));
    /**
     * Gets regular executions (not in queue)
     */
    AnswerList answerExecutions = testCaseExecutionService.readBySystemByVarious(system, testList, applicationList, projectList, tcstatusList, groupList, tcactiveList, priorityList, targetsprintList, targetrevisionList, creatorList, implementerList, buildList, revisionList, environmentList, countryList, browserList, tcestatusList, ip, port, tag, browserversion, comment, bugid, ticket);
    List<TestCaseExecution> testCaseExecutions = (List<TestCaseExecution>) answerExecutions.getDataList();
    /**
     * Get list of Execution in Queue by Tag
     */
    ITestCaseExecutionQueueService testCaseExecutionInQueueService = appContext.getBean(ITestCaseExecutionQueueService.class);
    AnswerList answerExecutionsInQueue = testCaseExecutionInQueueService.readBySystemByVarious(system, testList, applicationList, projectList, tcstatusList, groupList, tcactiveList, priorityList, targetsprintList, targetrevisionList, creatorList, implementerList, buildList, revisionList, environmentList, countryList, browserList, tcestatusList, ip, port, tag, browserversion, comment, bugid, ticket);
    List<TestCaseExecutionQueue> testCaseExecutionsInQueue = (List<TestCaseExecutionQueue>) answerExecutionsInQueue.getDataList();
    /**
     * Merge Test Case Executions
     */
    List<TestCaseExecution> allTestCaseExecutions = hashExecution(testCaseExecutions, testCaseExecutionsInQueue);
    JSONArray executionList = new JSONArray();
    LinkedHashMap<String, JSONObject> ttc = new LinkedHashMap<String, JSONObject>();
    for (TestCaseExecution testCaseExecution : allTestCaseExecutions) {
        try {
            JSONObject execution = testCaseExecutionToJSONObject(testCaseExecution);
            // the key is country and browser
            String execKey = 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.getTestCaseObj().getStatus());
                ttcObject.put("application", testCaseExecution.getApplication());
                ttcObject.put("bugId", testCaseExecution.getTestCaseObj().getBugID());
                ttcObject.put("ticket", testCaseExecution.getTestCaseObj().getTicket());
                ttcObject.put("comment", testCaseExecution.getTestCaseObj().getComment());
                ttcObject.put("priority", testCaseExecution.getTestCaseObj().getPriority());
                ttcObject.put("status", testCaseExecution.getStatus());
                ttcObject.put("group", testCaseExecution.getTestCaseObj().getGroup());
                execTab.put(execKey, execution);
                ttcObject.put("execTab", execTab);
            }
            ttc.put(testCaseExecution.getTest() + "_" + testCaseExecution.getTestCase(), ttcObject);
        } catch (JSONException ex) {
            LOG.warn(ex);
        }
    }
    JSONObject jsonResponse = new JSONObject();
    jsonResponse.put("contentTable", 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 : AnswerList(org.cerberus.util.answer.AnswerList) TestCaseExecution(org.cerberus.crud.entity.TestCaseExecution) MessageEvent(org.cerberus.engine.entity.MessageEvent) 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) TestCaseExecutionQueue(org.cerberus.crud.entity.TestCaseExecutionQueue) ITestCaseExecutionQueueService(org.cerberus.crud.service.ITestCaseExecutionQueueService)

Example 2 with ITestCaseExecutionQueueService

use of org.cerberus.crud.service.ITestCaseExecutionQueueService in project cerberus-source by cerberustesting.

the class ReadTestCaseExecution method readExecutionByTagList.

private List<TestCaseExecution> readExecutionByTagList(ApplicationContext appContext, String Tag, int startPosition, int length, String sortInformation, String searchParameter, Map<String, List<String>> individualSearch) throws ParseException, CerberusException {
    AnswerList<TestCaseExecution> testCaseExecution;
    AnswerList<TestCaseExecutionQueue> testCaseExecutionInQueue;
    ITestCaseExecutionService testCaseExecService = appContext.getBean(ITestCaseExecutionService.class);
    ITestCaseExecutionQueueService testCaseExecutionInQueueService = appContext.getBean(ITestCaseExecutionQueueService.class);
    /**
     * Get list of execution by tag, env, country, browser
     */
    testCaseExecution = testCaseExecService.readByTagByCriteria(Tag, startPosition, length, sortInformation, searchParameter, individualSearch);
    List<TestCaseExecution> testCaseExecutions = testCaseExecution.getDataList();
    /**
     * Get list of Execution in Queue by Tag
     */
    testCaseExecutionInQueue = testCaseExecutionInQueueService.readByTagByCriteria(Tag, startPosition, length, sortInformation, searchParameter, individualSearch);
    List<TestCaseExecutionQueue> testCaseExecutionsInQueue = testCaseExecutionInQueue.getDataList();
    /**
     * Feed hash map with execution from the two list (to get only one by
     * test,testcase,country,env,browser)
     */
    testCaseExecutions = hashExecution(testCaseExecutions, testCaseExecutionsInQueue);
    return testCaseExecutions;
}
Also used : TestCaseExecution(org.cerberus.crud.entity.TestCaseExecution) TestCaseExecutionQueue(org.cerberus.crud.entity.TestCaseExecutionQueue) ITestCaseExecutionService(org.cerberus.crud.service.ITestCaseExecutionService) ITestCaseExecutionQueueService(org.cerberus.crud.service.ITestCaseExecutionQueueService)

Example 3 with ITestCaseExecutionQueueService

use of org.cerberus.crud.service.ITestCaseExecutionQueueService in project cerberus-source by cerberustesting.

the class ReadTestCaseExecutionQueue method findExecutionQueueByKeyTech.

private AnswerItem findExecutionQueueByKeyTech(Long queueid, ApplicationContext appContext, boolean userHasPermissions) throws JSONException, CerberusException {
    AnswerItem item = new AnswerItem();
    JSONObject object = new JSONObject();
    ITestCaseExecutionQueueService queueService = appContext.getBean(ITestCaseExecutionQueueService.class);
    // finds the project
    AnswerItem answer = queueService.readByKey(queueid);
    if (answer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        // if the service returns an OK message then we can get the item and convert it to JSONformat
        TestCaseExecutionQueue lib = (TestCaseExecutionQueue) answer.getItem();
        JSONObject response = convertTestCaseExecutionInQueueToJSONObject(lib);
        int nb = 0;
        nb = queueService.getNbEntryToGo(lib.getId(), lib.getPriority());
        response.put("nbEntryInQueueToGo", nb);
        object.put("contentTable", response);
    }
    object.put("hasPermissions", userHasPermissions);
    item.setItem(object);
    item.setResultMessage(answer.getResultMessage());
    return item;
}
Also used : JSONObject(org.json.JSONObject) TestCaseExecutionQueue(org.cerberus.crud.entity.TestCaseExecutionQueue) AnswerItem(org.cerberus.util.answer.AnswerItem) ITestCaseExecutionQueueService(org.cerberus.crud.service.ITestCaseExecutionQueueService)

Example 4 with ITestCaseExecutionQueueService

use of org.cerberus.crud.service.ITestCaseExecutionQueueService in project cerberus-source by cerberustesting.

the class CreateTestCaseExecutionQueue 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();
    JSONObject executionQueue = new JSONObject();
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    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");
    // 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
    String actionState = policy.sanitize(request.getParameter("actionState"));
    String actionSave = policy.sanitize(request.getParameter("actionSave"));
    String environment = policy.sanitize(request.getParameter("environment"));
    String country = policy.sanitize(request.getParameter("country"));
    String manualEnvData = policy.sanitize(request.getParameter("manualEnvData"));
    // Parameter that needs to be secured --> We SECURE+DECODE them
    String test = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("test"), "", charset);
    String testcase = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("testCase"), "", charset);
    int manualURL = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("manualURL"), 0, charset);
    String manualHost = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("manualHost"), "", charset);
    String manualContextRoot = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("manualContextRoot"), "", charset);
    String manualLoginRelativeURL = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("manualLoginRelativeURL"), "", charset);
    String tag = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("tag"), "", charset);
    String robot = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("robot"), "", charset);
    String robotIP = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("robotIP"), "", charset);
    String robotPort = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("robotPort"), "", charset);
    String browser = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("browser"), "", charset);
    String browserVersion = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("browserVersion"), "", charset);
    String platform = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("platform"), "", charset);
    String screenSize = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("screenSize"), "", charset);
    int verbose = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("verbose"), 1, charset);
    int screenshot = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("screenshot"), 0, charset);
    int pageSource = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("pageSource"), 0, charset);
    int seleniumLog = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("seleniumLog"), 0, charset);
    String timeout = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("timeout"), "", charset);
    int retries = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("retries"), 0, charset);
    String manualExecution = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("manualExecution"), "", charset);
    String debugFlag = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("debugFlag"), "N", charset);
    // Parameter that we cannot secure as we need the html --> We DECODE them
    String[] myIds = request.getParameterValues("id");
    if (myIds == null) {
        myIds = new String[1];
        myIds[0] = "0";
    }
    long id = 0;
    Integer priority = TestCaseExecutionQueue.PRIORITY_DEFAULT;
    boolean prio_error = false;
    try {
        if (request.getParameter("priority") != null && !request.getParameter("priority").equals("")) {
            priority = Integer.valueOf(policy.sanitize(request.getParameter("priority")));
        }
    } catch (Exception ex) {
        prio_error = true;
    }
    boolean id_error = false;
    IExecutionThreadPoolService executionThreadPoolService = appContext.getBean(IExecutionThreadPoolService.class);
    // Create Tag when exist.
    if (!StringUtil.isNullOrEmpty(tag)) {
        // We create or update it.
        ITagService tagService = appContext.getBean(ITagService.class);
        tagService.createAuto(tag, "", request.getRemoteUser());
    }
    // Prepare the final answer.
    MessageEvent msg1 = new MessageEvent(MessageEventEnum.GENERIC_OK);
    Answer finalAnswer = new Answer(msg1);
    List<TestCaseExecutionQueue> insertedList = new ArrayList();
    for (String myId : myIds) {
        id_error = false;
        id = 0;
        try {
            id = Long.valueOf(myId);
        } catch (NumberFormatException ex) {
            id_error = true;
        }
        /**
         * Checking all constrains before calling the services.
         */
        if (id_error) {
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
            msg.setDescription(msg.getDescription().replace("%ITEM%", "Execution Queue").replace("%OPERATION%", "Update").replace("%REASON%", "Could not manage to convert id to an integer value."));
            ans.setResultMessage(msg);
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
        } else if (prio_error || priority > 2147483647 || priority < -2147483648) {
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
            msg.setDescription(msg.getDescription().replace("%ITEM%", "Execution Queue").replace("%OPERATION%", "Update").replace("%REASON%", "Could not manage to convert priority to an integer value."));
            ans.setResultMessage(msg);
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
        } else {
            try {
                /**
                 * All data seems cleans so we can call the services.
                 */
                ITestCaseExecutionQueueService executionQueueService = appContext.getBean(ITestCaseExecutionQueueService.class);
                IFactoryTestCaseExecutionQueue executionQueueFactory = appContext.getBean(IFactoryTestCaseExecutionQueue.class);
                if (actionSave.equals("save")) {
                    /**
                     * The service was able to perform the query and confirm
                     * the object exist, then we can update it.
                     */
                    TestCaseExecutionQueue executionQueueData;
                    if (id == 0) {
                        // If id is not defined, we build the execution queue from all request datas.
                        executionQueueData = executionQueueFactory.create(test, testcase, country, environment, robot, robotIP, robotPort, browser, browserVersion, platform, screenSize, manualURL, manualHost, manualContextRoot, manualLoginRelativeURL, manualEnvData, tag, screenshot, verbose, timeout, pageSource, seleniumLog, 0, retries, manualExecution, priority, request.getRemoteUser(), null, null, null);
                        executionQueueData.setDebugFlag(debugFlag);
                    } else {
                        // If id is defined, we get the execution queue from database.
                        executionQueueData = executionQueueService.convert(executionQueueService.readByKey(id));
                        executionQueueData.setState(TestCaseExecutionQueue.State.QUEUED);
                        executionQueueData.setComment("");
                        executionQueueData.setDebugFlag("N");
                        executionQueueData.setPriority(TestCaseExecutionQueue.PRIORITY_DEFAULT);
                        executionQueueData.setUsrCreated(request.getRemoteUser());
                    }
                    ansItem = executionQueueService.create(executionQueueData);
                    finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ansItem);
                    if (ansItem.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                        TestCaseExecutionQueue addedExecution = (TestCaseExecutionQueue) ansItem.getItem();
                        insertedList.add(addedExecution);
                    }
                    if (myIds.length <= 1) {
                        if (ansItem.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                            /**
                             * Update was successful. Adding Log entry.
                             */
                            ILogEventService logEventService = appContext.getBean(LogEventService.class);
                            logEventService.createForPrivateCalls("/CreateTestCaseExecutionQueue", "CREATE", "Created ExecutionQueue : ['" + id + "']", request);
                        }
                    }
                }
            } catch (FactoryCreationException ex) {
                LOG.warn(ex);
            }
        }
    }
    // Update is done, we now check what action needs to be performed.
    if (actionState.equals("toQUEUED")) {
        executionThreadPoolService.executeNextInQueueAsynchroneously(false);
    }
    /**
     * Formating and returning the json result.
     */
    jsonResponse.put("messageType", finalAnswer.getResultMessage().getMessage().getCodeString());
    jsonResponse.put("message", finalAnswer.getResultMessage().getDescription());
    if (insertedList.isEmpty()) {
        jsonResponse.put("addedEntries", 0);
    } else {
        JSONArray executionList = new JSONArray();
        for (TestCaseExecutionQueue testCaseExecutionQueue : insertedList) {
            JSONObject myExecution = new JSONObject();
            myExecution.append("id", testCaseExecutionQueue.getId());
            executionList.put(myExecution);
        }
        jsonResponse.put("testCaseExecutionQueueList", executionList);
        jsonResponse.put("addedEntries", insertedList.size());
    }
    response.getWriter().print(jsonResponse);
    response.getWriter().flush();
}
Also used : PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) ILogEventService(org.cerberus.crud.service.ILogEventService) LogEventService(org.cerberus.crud.service.impl.LogEventService) FactoryCreationException(org.cerberus.exception.FactoryCreationException) ApplicationContext(org.springframework.context.ApplicationContext) ILogEventService(org.cerberus.crud.service.ILogEventService) TestCaseExecutionQueue(org.cerberus.crud.entity.TestCaseExecutionQueue) IFactoryTestCaseExecutionQueue(org.cerberus.crud.factory.IFactoryTestCaseExecutionQueue) ITestCaseExecutionQueueService(org.cerberus.crud.service.ITestCaseExecutionQueueService) JSONArray(org.json.JSONArray) AnswerItem(org.cerberus.util.answer.AnswerItem) ServletException(javax.servlet.ServletException) JSONException(org.json.JSONException) FactoryCreationException(org.cerberus.exception.FactoryCreationException) IOException(java.io.IOException) CerberusException(org.cerberus.exception.CerberusException) IFactoryTestCaseExecutionQueue(org.cerberus.crud.factory.IFactoryTestCaseExecutionQueue) Answer(org.cerberus.util.answer.Answer) JSONObject(org.json.JSONObject) IExecutionThreadPoolService(org.cerberus.engine.threadpool.IExecutionThreadPoolService) ITagService(org.cerberus.crud.service.ITagService)

Example 5 with ITestCaseExecutionQueueService

use of org.cerberus.crud.service.ITestCaseExecutionQueueService in project cerberus-source by cerberustesting.

the class UpdateTestCaseExecutionQueue 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());
    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
    String actionState = policy.sanitize(request.getParameter("actionState"));
    String actionSave = policy.sanitize(request.getParameter("actionSave"));
    String environment = policy.sanitize(request.getParameter("environment"));
    String country = policy.sanitize(request.getParameter("country"));
    String manualEnvData = policy.sanitize(request.getParameter("manualEnvData"));
    // Parameter that needs to be secured --> We SECURE+DECODE them
    String test = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("test"), null, charset);
    String testcase = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("testCase"), null, charset);
    int manualURL = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("manualURL"), 0, charset);
    String manualHost = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("manualHost"), null, charset);
    String manualContextRoot = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("manualContextRoot"), "", charset);
    String manualLoginRelativeURL = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("manualLoginRelativeURL"), "", charset);
    String tag = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("tag"), null, charset);
    String robot = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("robot"), null, charset);
    String robotIP = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("robotIP"), null, charset);
    String robotPort = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("robotPort"), null, charset);
    String browser = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("browser"), null, charset);
    String browserVersion = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("browserVersion"), null, charset);
    String platform = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("platform"), null, charset);
    String screenSize = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("screenSize"), null, charset);
    int verbose = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("verbose"), 1, charset);
    int screenshot = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("screenshot"), 0, charset);
    int pageSource = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("pageSource"), 0, charset);
    int seleniumLog = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("seleniumLog"), 0, charset);
    String timeout = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("timeout"), "", charset);
    int retries = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("retries"), 0, charset);
    String manualExecution = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("manualExecution"), "", charset);
    String debugFlag = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("debugFlag"), "N", charset);
    Integer priority = TestCaseExecutionQueue.PRIORITY_DEFAULT;
    boolean prio_error = false;
    try {
        if (request.getParameter("priority") != null && !request.getParameter("priority").equals("")) {
            priority = Integer.valueOf(policy.sanitize(request.getParameter("priority")));
        }
    } catch (Exception ex) {
        prio_error = true;
    }
    // Parameter that we cannot secure as we need the html --> We DECODE them
    String[] myIds = request.getParameterValues("id");
    long id = 0;
    // Create Tag when exist.
    if (!StringUtil.isNullOrEmpty(tag)) {
        // We create or update it.
        ITagService tagService = appContext.getBean(ITagService.class);
        tagService.createAuto(tag, "", request.getRemoteUser());
    }
    // Prepare the final answer.
    MessageEvent msg1 = new MessageEvent(MessageEventEnum.GENERIC_OK);
    Answer finalAnswer = new Answer(msg1);
    boolean id_error = false;
    for (String myId : myIds) {
        id_error = false;
        try {
            id = Long.valueOf(myId);
        } catch (NumberFormatException ex) {
            id_error = true;
        }
        /**
         * Checking all constrains before calling the services.
         */
        if (id_error) {
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
            msg.setDescription(msg.getDescription().replace("%ITEM%", "Execution Queue").replace("%OPERATION%", "Update").replace("%REASON%", "Could not manage to convert id to an integer value."));
            ans.setResultMessage(msg);
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
        } else if (prio_error || priority > 2147483647 || priority < -2147483648) {
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
            msg.setDescription(msg.getDescription().replace("%ITEM%", "Execution Queue").replace("%OPERATION%", "Update").replace("%REASON%", "Could not manage to convert priority to an integer value."));
            ans.setResultMessage(msg);
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
        } else {
            /**
             * All data seems cleans so we can call the services.
             */
            ITestCaseExecutionQueueService executionQueueService = appContext.getBean(ITestCaseExecutionQueueService.class);
            IExecutionThreadPoolService executionThreadPoolService = appContext.getBean(IExecutionThreadPoolService.class);
            AnswerItem resp = executionQueueService.readByKey(id);
            if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {
                /**
                 * Object could not be found. We stop here and report the
                 * error.
                 */
                finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) resp);
            } else {
                TestCaseExecutionQueue executionQueueData = (TestCaseExecutionQueue) resp.getItem();
                if (actionSave.equals("save")) {
                    /**
                     * The service was able to perform the query and confirm
                     * the object exist, then we can update it.
                     */
                    executionQueueData.setTest(ParameterParserUtil.parseStringParam(test, executionQueueData.getTest()));
                    executionQueueData.setTestCase(ParameterParserUtil.parseStringParam(testcase, executionQueueData.getTestCase()));
                    executionQueueData.setTag(ParameterParserUtil.parseStringParam(tag, executionQueueData.getTag()));
                    executionQueueData.setEnvironment(ParameterParserUtil.parseStringParam(environment, executionQueueData.getEnvironment()));
                    executionQueueData.setCountry(ParameterParserUtil.parseStringParam(country, executionQueueData.getCountry()));
                    executionQueueData.setManualURL(ParameterParserUtil.parseIntegerParam(manualURL, executionQueueData.getManualURL()));
                    executionQueueData.setManualHost(ParameterParserUtil.parseStringParam(manualHost, executionQueueData.getManualHost()));
                    executionQueueData.setManualContextRoot(ParameterParserUtil.parseStringParam(manualContextRoot, executionQueueData.getManualContextRoot()));
                    executionQueueData.setManualLoginRelativeURL(ParameterParserUtil.parseStringParam(manualLoginRelativeURL, executionQueueData.getManualLoginRelativeURL()));
                    executionQueueData.setManualEnvData(ParameterParserUtil.parseStringParam(manualEnvData, executionQueueData.getManualEnvData()));
                    executionQueueData.setRobot(ParameterParserUtil.parseStringParam(robot, executionQueueData.getRobot()));
                    executionQueueData.setRobotIP(ParameterParserUtil.parseStringParam(robotIP, executionQueueData.getRobotIP()));
                    executionQueueData.setRobotPort(ParameterParserUtil.parseStringParam(robotPort, executionQueueData.getRobotPort()));
                    executionQueueData.setBrowser(ParameterParserUtil.parseStringParam(browser, executionQueueData.getBrowser()));
                    executionQueueData.setBrowserVersion(ParameterParserUtil.parseStringParam(browserVersion, executionQueueData.getBrowserVersion()));
                    executionQueueData.setPlatform(ParameterParserUtil.parseStringParam(platform, executionQueueData.getPlatform()));
                    executionQueueData.setScreenSize(ParameterParserUtil.parseStringParam(screenSize, executionQueueData.getScreenSize()));
                    executionQueueData.setVerbose(ParameterParserUtil.parseIntegerParam(verbose, executionQueueData.getVerbose()));
                    executionQueueData.setScreenshot(screenshot);
                    executionQueueData.setPageSource(pageSource);
                    executionQueueData.setSeleniumLog(seleniumLog);
                    executionQueueData.setTimeout(timeout);
                    executionQueueData.setRetries(retries);
                    executionQueueData.setManualExecution(manualExecution);
                    executionQueueData.setDebugFlag(debugFlag);
                    executionQueueData.setPriority(priority);
                    executionQueueData.setUsrModif(request.getRemoteUser());
                    ans = executionQueueService.update(executionQueueData);
                    finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
                    if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                        /**
                         * Update was successfull. Adding Log entry.
                         */
                        ILogEventService logEventService = appContext.getBean(LogEventService.class);
                        logEventService.createForPrivateCalls("/UpdateTestCaseExecutionQueue", "UPDATE", "Updated ExecutionQueue : ['" + id + "']", request);
                    }
                }
                // Update is done, we now check what action needs to be performed.
                if (actionState.equals("toQUEUED")) {
                    LOG.debug("toQUEUED");
                    ans = executionQueueService.updateToQueued(id, "Trigered by user " + request.getRemoteUser() + ".");
                    finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
                    executionThreadPoolService.executeNextInQueueAsynchroneously(false);
                }
                // Priority Update.
                if (actionSave.equals("priority")) {
                    executionQueueData.setPriority(priority);
                    ans = executionQueueService.update(executionQueueData);
                    finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
                }
                // Update is done, we now check what action needs to be performed.
                if (actionState.equals("toCANCELLED")) {
                    LOG.debug("toCANCELLED");
                    ans = executionQueueService.updateToCancelled(id, "Cancelled by user " + request.getRemoteUser() + ".");
                    finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
                }
                // Update is done, we now check what action needs to be performed.
                if (actionState.equals("toCANCELLEDForce")) {
                    LOG.debug("toCANCELLEDForce");
                    ans = executionQueueService.updateToCancelledForce(id, "Forced Cancelled by user " + request.getRemoteUser() + ".");
                    finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
                }
                // Update is done, we now check what action needs to be performed.
                if (actionState.equals("toERRORForce")) {
                    LOG.debug("toERRORForce");
                    ans = executionQueueService.updateToErrorForce(id, "Forced Eroor by user " + request.getRemoteUser() + ".");
                    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 : PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) ILogEventService(org.cerberus.crud.service.ILogEventService) LogEventService(org.cerberus.crud.service.impl.LogEventService) AnswerItem(org.cerberus.util.answer.AnswerItem) ServletException(javax.servlet.ServletException) JSONException(org.json.JSONException) IOException(java.io.IOException) CerberusException(org.cerberus.exception.CerberusException) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) IExecutionThreadPoolService(org.cerberus.engine.threadpool.IExecutionThreadPoolService) ITagService(org.cerberus.crud.service.ITagService) ILogEventService(org.cerberus.crud.service.ILogEventService) TestCaseExecutionQueue(org.cerberus.crud.entity.TestCaseExecutionQueue) ITestCaseExecutionQueueService(org.cerberus.crud.service.ITestCaseExecutionQueueService)

Aggregations

TestCaseExecutionQueue (org.cerberus.crud.entity.TestCaseExecutionQueue)6 ITestCaseExecutionQueueService (org.cerberus.crud.service.ITestCaseExecutionQueueService)6 AnswerItem (org.cerberus.util.answer.AnswerItem)5 JSONObject (org.json.JSONObject)5 MessageEvent (org.cerberus.engine.entity.MessageEvent)4 ArrayList (java.util.ArrayList)3 TestCaseExecution (org.cerberus.crud.entity.TestCaseExecution)3 ITagService (org.cerberus.crud.service.ITagService)3 IExecutionThreadPoolService (org.cerberus.engine.threadpool.IExecutionThreadPoolService)3 CerberusException (org.cerberus.exception.CerberusException)3 JSONArray (org.json.JSONArray)3 JSONException (org.json.JSONException)3 ApplicationContext (org.springframework.context.ApplicationContext)3 IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2 IFactoryTestCaseExecutionQueue (org.cerberus.crud.factory.IFactoryTestCaseExecutionQueue)2 ILogEventService (org.cerberus.crud.service.ILogEventService)2 ITestCaseExecutionService (org.cerberus.crud.service.ITestCaseExecutionService)2 LogEventService (org.cerberus.crud.service.impl.LogEventService)2 FactoryCreationException (org.cerberus.exception.FactoryCreationException)2