Search in sources :

Example 1 with ITestCaseExecutionFileService

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

the class ReadTestCaseExecutionMedia 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
 * @throws CerberusException
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, CerberusException {
    String charset = request.getCharacterEncoding();
    boolean auto = ParameterParserUtil.parseBooleanParam(request.getParameter("auto"), true);
    String type = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("type"), "", charset);
    String test = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("test"), "", charset);
    String testcase = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("testcase"), "", charset);
    String fileName = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("filename"), "", charset);
    String fileType = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("filetype"), "", charset);
    String fileDesc = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("filedesc"), "", charset);
    int step = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("step"), 0, charset);
    int index = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("index"), 1, charset);
    int sequence = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("sequence"), 0, charset);
    int sequenceControl = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("sequenceControl"), 0, charset);
    int iterator = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("iterator"), 0, charset);
    boolean autoContentType = ParameterParserUtil.parseBooleanParam(request.getParameter("autoContentType"), true);
    long id = ParameterParserUtil.parseLongParamAndDecode(request.getParameter("id"), 0, charset);
    // Calling Servlet Transversal Util.
    ServletUtil.servletStart(request);
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    IParameterService parameterService = appContext.getBean(IParameterService.class);
    BufferedImage b = null;
    AnswerList al = new AnswerList<>(new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED));
    TestCaseExecutionFile tceFile = null;
    if (!(fileName.equals(""))) {
        IFactoryTestCaseExecutionFile factoryTestCaseExecutionFile = appContext.getBean(IFactoryTestCaseExecutionFile.class);
        tceFile = factoryTestCaseExecutionFile.create(0, 0, "", fileDesc, fileName, fileType, "", null, "", null);
    } else {
        ITestCaseExecutionFileService testCaseExecutionFileService = appContext.getBean(ITestCaseExecutionFileService.class);
        String levelFile = "";
        if (type.equals("action")) {
            levelFile = test + "-" + testcase + "-" + step + "-" + index + "-" + sequence;
        } else if (type.equals("control")) {
            levelFile = test + "-" + testcase + "-" + step + "-" + index + "-" + sequence + "-" + sequenceControl;
        }
        al = testCaseExecutionFileService.readByVarious(id, levelFile);
        if (al.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && !al.getDataList().isEmpty()) {
            Iterator i = al.getDataList().iterator();
            int indexIterator = -1;
            while (i.hasNext() && indexIterator != iterator) {
                indexIterator++;
                TestCaseExecutionFile tctemp = (TestCaseExecutionFile) i.next();
                if (indexIterator == iterator) {
                    tceFile = tctemp;
                }
            }
        } else {
            // If previous read failed we try without index. (that can be removed few moths after step index has been introduced in Jan 2017)
            if (type.equals("action")) {
                levelFile = test + "-" + testcase + "-" + step + "-" + sequence;
            } else if (type.equals("control")) {
                levelFile = test + "-" + testcase + "-" + step + "-" + sequence + "-" + sequenceControl;
            }
            al = testCaseExecutionFileService.readByVarious(id, levelFile);
            if (al.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && !al.getDataList().isEmpty()) {
                Iterator i = al.getDataList().iterator();
                int indexIterator = -1;
                while (i.hasNext() && indexIterator != iterator) {
                    indexIterator++;
                    TestCaseExecutionFile tctemp = (TestCaseExecutionFile) i.next();
                    if (indexIterator == iterator) {
                        tceFile = tctemp;
                    }
                }
            }
        }
    }
    if (tceFile != null) {
        String pathString = "";
        if (auto) {
            pathString = parameterService.getParameterStringByKey("cerberus_exeautomedia_path", "", "");
        } else {
            pathString = parameterService.getParameterStringByKey("cerberus_exemanualmedia_path", "", "");
        }
        switch(tceFile.getFileType()) {
            case "JPG":
            case "JPEG":
                if (autoContentType) {
                    response.setContentType("image/jpeg");
                }
                returnImage(request, response, tceFile, pathString);
                break;
            case "PNG":
                if (autoContentType) {
                    response.setContentType("image/png");
                }
                returnImage(request, response, tceFile, pathString);
                break;
            case "GIF":
                if (autoContentType) {
                    response.setContentType("image/gif");
                }
                returnImage(request, response, tceFile, pathString);
                break;
            case "HTML":
                if (autoContentType) {
                    response.setContentType("text/html");
                }
                returnFile(request, response, tceFile, pathString);
                break;
            case "XML":
                if (autoContentType) {
                    response.setContentType("application/xml");
                }
                returnFile(request, response, tceFile, pathString);
                break;
            case "JSON":
                if (autoContentType) {
                    response.setContentType("application/json");
                }
                returnFile(request, response, tceFile, pathString);
                break;
            case "TXT":
                returnFile(request, response, tceFile, pathString);
                break;
            case "PDF":
                returnPDF(request, response, tceFile, pathString);
            default:
                returnNotSupported(request, response, tceFile, pathString);
        }
    }
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) MessageEvent(org.cerberus.engine.entity.MessageEvent) IFactoryTestCaseExecutionFile(org.cerberus.crud.factory.IFactoryTestCaseExecutionFile) ITestCaseExecutionFileService(org.cerberus.crud.service.ITestCaseExecutionFileService) IParameterService(org.cerberus.crud.service.IParameterService) BufferedImage(java.awt.image.BufferedImage) ApplicationContext(org.springframework.context.ApplicationContext) IFactoryTestCaseExecutionFile(org.cerberus.crud.factory.IFactoryTestCaseExecutionFile) TestCaseExecutionFile(org.cerberus.crud.entity.TestCaseExecutionFile)

Example 2 with ITestCaseExecutionFileService

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

the class DeleteTestCaseExecutionFile 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, JSONException, CerberusException {
    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.
     */
    Long fileId = ParameterParserUtil.parseLongParam(request.getParameter("fileID"), 0);
    /**
     * Checking all constrains before calling the services.
     */
    if (fileId == null) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseExecutionFile").replace("%OPERATION%", "Delete").replace("%REASON%", "field fileID is missing!"));
        ans.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        ITestCaseExecutionFileService testCaseExecutionFileService = appContext.getBean(ITestCaseExecutionFileService.class);
        IParameterService parameterService = appContext.getBean(IParameterService.class);
        IRecorderService recorderService = appContext.getBean(IRecorderService.class);
        AnswerItem resp = testCaseExecutionFileService.readByKey(fileId);
        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%", "TestCaseExecutionFile").replace("%OPERATION%", "Delete").replace("%REASON%", "TestCaseExecutionFile with this ID does not exist."));
            ans.setResultMessage(msg);
        } else {
            /**
             * The service was able to perform the query and confirm the
             * object exist, then we can delete it.
             */
            TestCaseExecutionFile testCaseExecutionFile = (TestCaseExecutionFile) resp.getItem();
            String rootFolder = parameterService.getParameterStringByKey("cerberus_exemanualmedia_path", "", "");
            testCaseExecutionFileService.deleteFile(rootFolder, testCaseExecutionFile.getFileName());
            ans = testCaseExecutionFileService.delete(testCaseExecutionFile);
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                /**
                 * Delete was successful. Adding Log entry.
                 */
                ILogEventService logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/DeleteTestCaseExecutionFile", "DELETE", "Delete TestCase Execution File : ['" + testCaseExecutionFile + "']", request);
            }
        }
    }
    /**
     * Formating and returning the json result.
     */
    jsonResponse.put("messageType", ans.getResultMessage().getMessage().getCodeString());
    jsonResponse.put("message", ans.getResultMessage().getDescription());
    response.getWriter().print(jsonResponse.toString());
    response.getWriter().flush();
}
Also used : PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) ITestCaseExecutionFileService(org.cerberus.crud.service.ITestCaseExecutionFileService) IParameterService(org.cerberus.crud.service.IParameterService) AnswerItem(org.cerberus.util.answer.AnswerItem) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) IRecorderService(org.cerberus.engine.execution.IRecorderService) JSONObject(org.json.JSONObject) ILogEventService(org.cerberus.crud.service.ILogEventService) DeleteTestCaseExecutionFile(org.cerberus.servlet.manualtestcase.DeleteTestCaseExecutionFile) TestCaseExecutionFile(org.cerberus.crud.entity.TestCaseExecutionFile)

Aggregations

TestCaseExecutionFile (org.cerberus.crud.entity.TestCaseExecutionFile)2 IParameterService (org.cerberus.crud.service.IParameterService)2 ITestCaseExecutionFileService (org.cerberus.crud.service.ITestCaseExecutionFileService)2 MessageEvent (org.cerberus.engine.entity.MessageEvent)2 ApplicationContext (org.springframework.context.ApplicationContext)2 BufferedImage (java.awt.image.BufferedImage)1 IFactoryTestCaseExecutionFile (org.cerberus.crud.factory.IFactoryTestCaseExecutionFile)1 ILogEventService (org.cerberus.crud.service.ILogEventService)1 IRecorderService (org.cerberus.engine.execution.IRecorderService)1 DeleteTestCaseExecutionFile (org.cerberus.servlet.manualtestcase.DeleteTestCaseExecutionFile)1 Answer (org.cerberus.util.answer.Answer)1 AnswerItem (org.cerberus.util.answer.AnswerItem)1 AnswerList (org.cerberus.util.answer.AnswerList)1 JSONObject (org.json.JSONObject)1 PolicyFactory (org.owasp.html.PolicyFactory)1