Search in sources :

Example 1 with IRecorderService

use of org.cerberus.engine.execution.IRecorderService in project cerberus-source by cerberustesting.

the class SaveManualExecutionPicture method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // using commons-fileupload http://commons.apache.org/proper/commons-fileupload/using.html
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    // Collection<Part> parts = req.getParts(); //for sevlet 3.0, in glassfish 2.x does not work
    TestCaseStepActionExecution tcsae = new TestCaseStepActionExecution();
    FileItem uploadedFile = null;
    // if is multipart we need to handle the upload data
    IParameterService parameterService = appContext.getBean(IParameterService.class);
    IRecorderService recorderService = appContext.getBean(IRecorderService.class);
    DiskFileItemFactory factory = new DiskFileItemFactory();
    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);
    Integer imgPathMaxSize = parameterService.getParameterIntegerByKey("cerberus_screenshot_max_size", "", UPLOAD_PICTURE_MAXSIZE);
    // Set overall request size constraint
    // max size for the file
    upload.setFileSizeMax(imgPathMaxSize);
    try {
        // Parse the request
        List<FileItem> items = upload.parseRequest(req);
        // Process the uploaded items
        Iterator<FileItem> iter = items.iterator();
        while (iter.hasNext()) {
            FileItem item = iter.next();
            if (item.isFormField()) {
                tcsae = processFormField(tcsae, item);
            } else {
                // uploadFileName = processUploadedFile(item);
                uploadedFile = item;
            }
        }
    // this handles an action each time
    } catch (FileSizeLimitExceededException ex) {
        LOG.warn("File size exceed the limit: " + ex.toString());
    } catch (FileUploadException ex) {
        LOG.warn("Exception occurred while uploading file: " + ex.toString());
    }
    if (uploadedFile != null) {
        if (uploadedFile.getContentType().startsWith("image/")) {
            // TODO:FN verify if this is the best approach or if we should
            // check if the mime types for images can be configured in the web.xml and then obtain the valid mime types from the servletContext
            // getServletContext().getMimeType(fileName);
            recorderService.recordUploadedFile(tcsae.getId(), tcsae, uploadedFile);
        } else {
            LOG.warn("Problem with the file you're trying to upload. It is not an image." + "Name: " + uploadedFile.getName() + "; Content-type: " + uploadedFile.getContentType());
        }
    }
// old version : TODO to be deleted after testing
// Collection<Part> parts = req.getParts();
// String runId = req.getParameter("runId");
// String test = req.getParameter("picTest");
// String testCase = req.getParameter("picTestCase");
// String step = req.getParameter("pictStep");
// String action = req.getParameter("pictAction");
// String control = req.getParameter("pictControl") == null ? "" : req.getParameter("pictControl");
// String returnCode = req.getParameter("returnCode");
// 
// ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
// IParameterService parameterService = appContext.getBean(IParameterService.class);
// //        ITestCaseStepExecutionService testCaseStepExecutionService = appContext.getBean(ITestCaseStepExecutionService.class);
// //        ITestCaseStepActionExecutionService testCaseStepActionExecutionService = appContext.getBean(ITestCaseStepActionExecutionService.class);
// 
// try {
// String imgPath = parameterService.findParameterByKey("cerberus_exeautomedia_path", "").getValue();
// 
// File dir = new File(imgPath + runId);
// dir.mkdirs();
// 
// int seq = 1;
// long runID = ParameterParserUtil.parseLongParam(runId, 0);
// //TestCaseStepActionExecution testCaseStepActionExecution = new TestCaseStepActionExecution();
// for (Part p : parts) {
// if (p.getName().equalsIgnoreCase("files[]")) {
// if (seq == 1) {
// //                        TestCaseStepExecution tcse = new TestCaseStepExecution();
// //                        tcse.setId(runID);
// //                        tcse.setTest(test);
// //                        tcse.setTestCase(testCase);
// //                        tcse.setStep(1);
// //                        testCaseStepExecutionService.insertTestCaseStepExecution(tcse);
// }
// 
// InputStream inputStream = p.getInputStream();
// String controlName = control.equals("") ? "" : "Ct"+control;
// String name = test + "-" + testCase + "-St"+step+"Sq" + action + controlName + ".jpg";
// OutputStream outputStream = new FileOutputStream(new File(this.buildScreenshotPath(imgPath, runId, name)));
// 
// int read;
// byte[] bytes = new byte[1024];
// while ((read = inputStream.read(bytes)) != -1) {
// outputStream.write(bytes, 0, read);
// }
// outputStream.close();
// 
// Date now = new Date();
// 
// //create action
// //                    testCaseStepActionExecution.setId(runID);
// //                    testCaseStepActionExecution.setTest(test);
// //                    testCaseStepActionExecution.setTestCase(testCase);
// //                    testCaseStepActionExecution.setStep(1);
// //                    testCaseStepActionExecution.setSequence(seq);
// //                    testCaseStepActionExecution.setReturnCode(returnCode);
// //                    testCaseStepActionExecution.setReturnMessage("");
// //                    testCaseStepActionExecution.setAction("screenshot");
// //                    testCaseStepActionExecution.setObject("");
// //                    testCaseStepActionExecution.setProperty("");
// //                    testCaseStepActionExecution.setStart(now.getTime());
// //                    testCaseStepActionExecution.setEnd(now.getTime());
// //                    testCaseStepActionExecution.setStartLong(now.getTime());
// //                    testCaseStepActionExecution.setEndLong(now.getTime());
// //                    testCaseStepActionExecutionService.insertTestCaseStepActionExecution(testCaseStepActionExecution);
// //
// //                    seq++;
// }
// }
// 
// } catch (CerberusException e) {
// MyLogger.log(SaveManualExecutionPicture.class.getName(), Level.ERROR, e.toString());
// }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) ApplicationContext(org.springframework.context.ApplicationContext) IRecorderService(org.cerberus.engine.execution.IRecorderService) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileSizeLimitExceededException(org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException) TestCaseStepActionExecution(org.cerberus.crud.entity.TestCaseStepActionExecution) IParameterService(org.cerberus.crud.service.IParameterService) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 2 with IRecorderService

use of org.cerberus.engine.execution.IRecorderService in project cerberus-source by cerberustesting.

the class CreateUpdateTestCaseExecutionFile 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 org.cerberus.exception.CerberusException
 * @throws org.json.JSONException
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, CerberusException, JSONException {
    JSONObject jsonResponse = new JSONObject();
    Answer ans = new Answer();
    Gson gson = new Gson();
    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");
    String charset = request.getCharacterEncoding();
    Map<String, String> fileData = new HashMap<String, String>();
    FileItem file = null;
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    try {
        List<FileItem> fields = upload.parseRequest(request);
        Iterator<FileItem> it = fields.iterator();
        if (!it.hasNext()) {
            return;
        }
        while (it.hasNext()) {
            FileItem fileItem = it.next();
            boolean isFormField = fileItem.isFormField();
            if (isFormField) {
                fileData.put(fileItem.getFieldName(), fileItem.getString("UTF-8"));
            } else {
                file = fileItem;
            }
        }
    } catch (FileUploadException e) {
        e.printStackTrace();
    }
    /**
     * Parsing and securing all required parameters
     */
    // Parameter that needs to be secured --> We SECURE+DECODE them
    String description = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(fileData.get("desc"), null, charset);
    String extension = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(fileData.get("type"), "", charset);
    String fileName = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(fileData.get("fileName"), null, charset);
    Integer fileID = ParameterParserUtil.parseIntegerParam(fileData.get("fileID"), 0);
    Integer idex = ParameterParserUtil.parseIntegerParam(fileData.get("idex"), 0);
    boolean action = fileData.get("action") != null ? true : false;
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    IRecorderService recorderService = appContext.getBean(IRecorderService.class);
    TestCaseStepActionExecution testCaseStepActionExecution = null;
    TestCaseStepActionControlExecution testCaseStepActionControlExecution = null;
    JSONArray obj = null;
    if (action) {
        obj = new JSONArray(fileData.get("action"));
        testCaseStepActionExecution = updateTestCaseStepActionExecutionFromJsonArray(obj, appContext);
    } else {
        obj = new JSONArray(fileData.get("control"));
        testCaseStepActionControlExecution = updateTestCaseStepActionControlExecutionFromJsonArray(obj, appContext);
    }
    if (description.isEmpty()) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "manual testcase execution file").replace("%OPERATION%", "Create/Update").replace("%REASON%", "desc is missing!"));
        ans.setResultMessage(msg);
    } else {
        ans = recorderService.recordManuallyFile(testCaseStepActionExecution, testCaseStepActionControlExecution, extension, description, file, idex, fileName, fileID);
    }
    if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        /**
         * Object created. Adding Log entry.
         */
        ILogEventService logEventService = appContext.getBean(LogEventService.class);
        logEventService.createForPrivateCalls("/CreateUpdateTestCaseExecutionFile", "CREATE", "Create execution file", 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) HashMap(java.util.HashMap) MessageEvent(org.cerberus.engine.entity.MessageEvent) JSONArray(org.json.JSONArray) Gson(com.google.gson.Gson) TestCaseStepActionExecution(org.cerberus.crud.entity.TestCaseStepActionExecution) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) Answer(org.cerberus.util.answer.Answer) FileItem(org.apache.commons.fileupload.FileItem) ApplicationContext(org.springframework.context.ApplicationContext) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) IRecorderService(org.cerberus.engine.execution.IRecorderService) JSONObject(org.json.JSONObject) TestCaseStepActionControlExecution(org.cerberus.crud.entity.TestCaseStepActionControlExecution) ILogEventService(org.cerberus.crud.service.ILogEventService) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 3 with IRecorderService

use of org.cerberus.engine.execution.IRecorderService 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)

Example 4 with IRecorderService

use of org.cerberus.engine.execution.IRecorderService in project cerberus-source by cerberustesting.

the class RunManualTest method getTestCaseStepActionExecution.

private List<TestCaseStepActionExecution> getTestCaseStepActionExecution(HttpServletRequest request, ApplicationContext appContext, String test, String testCase, long executionId, int stepSort) {
    List<TestCaseStepActionExecution> result = new ArrayList();
    IRecorderService recorderService = appContext.getBean(IRecorderService.class);
    long now = new Date().getTime();
    IFactoryTestCaseStepActionExecution testCaseStepActionExecutionFactory = appContext.getBean(IFactoryTestCaseStepActionExecution.class);
    // IRecorderService recorderService = appContext.getBean(IRecorderService.class);
    String[] stepAction_increment = getParameterValuesIfExists(request, "action_increment_" + stepSort);
    if (stepAction_increment != null) {
        for (String inc : stepAction_increment) {
            int step = Integer.valueOf(getParameterIfExists(request, "action_technical_step_" + stepSort + "_" + inc) == null ? "0" : getParameterIfExists(request, "action_technical_step_" + stepSort + "_" + inc));
            int sequence = Integer.valueOf(getParameterIfExists(request, "action_technical_sequence_" + stepSort + "_" + inc) == null ? "0" : getParameterIfExists(request, "action_technical_sequence_" + stepSort + "_" + inc));
            int sort = Integer.valueOf(getParameterIfExists(request, "action_sequence_" + stepSort + "_" + inc) == null ? "0" : getParameterIfExists(request, "action_sequence_" + stepSort + "_" + inc));
            String actionReturnCode = getParameterIfExists(request, "actionStatus_" + stepSort + "_" + inc);
            String actionReturnMessage = getParameterIfExists(request, "actionResultMessage_" + stepSort + "_" + inc);
            result.add(testCaseStepActionExecutionFactory.create(executionId, test, testCase, step, 1, sequence, sort, actionReturnCode, actionReturnMessage, "", "", "", "", "", "Manual Action", "", "", "", "", "", now, now, now, now, null, "", null, null));
        }
    }
    return result;
}
Also used : IRecorderService(org.cerberus.engine.execution.IRecorderService) ArrayList(java.util.ArrayList) IFactoryTestCaseStepActionExecution(org.cerberus.crud.factory.IFactoryTestCaseStepActionExecution) IFactoryTestCaseStepActionExecution(org.cerberus.crud.factory.IFactoryTestCaseStepActionExecution) Date(java.util.Date)

Example 5 with IRecorderService

use of org.cerberus.engine.execution.IRecorderService in project cerberus-source by cerberustesting.

the class RunManualTest method getTestCaseStepActionControlExecution.

private List<TestCaseStepActionControlExecution> getTestCaseStepActionControlExecution(HttpServletRequest request, ApplicationContext appContext, String test, String testCase, long executionId, int stepSort, int actionSort) {
    List<TestCaseStepActionControlExecution> result = new ArrayList();
    IRecorderService recorderService = appContext.getBean(IRecorderService.class);
    long now = new Date().getTime();
    IFactoryTestCaseStepActionControlExecution testCaseStepActionExecutionFactory = appContext.getBean(IFactoryTestCaseStepActionControlExecution.class);
    // IRecorderService recorderService = appContext.getBean(IRecorderService.class);
    String[] stepActionControl_increment = getParameterValuesIfExists(request, "control_increment_" + stepSort + "_" + actionSort);
    if (stepActionControl_increment != null) {
        for (String inc : stepActionControl_increment) {
            int step = Integer.valueOf(getParameterIfExists(request, "control_technical_step_" + stepSort + "_" + actionSort + "_" + inc) == null ? "0" : getParameterIfExists(request, "control_technical_step_" + stepSort + "_" + actionSort + "_" + inc));
            int sequence = Integer.valueOf(getParameterIfExists(request, "control_technical_sequence_" + stepSort + "_" + actionSort + "_" + inc) == null ? "0" : getParameterIfExists(request, "control_technical_sequence_" + stepSort + "_" + actionSort + "_" + inc));
            int control = Integer.valueOf(getParameterIfExists(request, "control_technical_control_" + stepSort + "_" + actionSort + "_" + inc) == null ? "0" : getParameterIfExists(request, "control_technical_control_" + stepSort + "_" + actionSort + "_" + inc));
            int sort = Integer.valueOf(getParameterIfExists(request, "control_control_" + stepSort + "_" + actionSort + "_" + inc) == null ? "0" : getParameterIfExists(request, "control_control_" + stepSort + "_" + actionSort + "_" + inc));
            String controlReturnCode = getParameterIfExists(request, "controlStatus_" + stepSort + "_" + actionSort + "_" + inc);
            String controlReturnMessage = getParameterIfExists(request, "controlResultMessage_" + stepSort + "_" + actionSort + "_" + inc);
            result.add(testCaseStepActionExecutionFactory.create(executionId, test, testCase, step, 1, sequence, control, sort, controlReturnCode, controlReturnMessage, "", "", "", "", "", "Manual Control", null, null, null, null, null, now, now, now, now, "", null, null));
        }
    }
    return result;
}
Also used : IRecorderService(org.cerberus.engine.execution.IRecorderService) IFactoryTestCaseStepActionControlExecution(org.cerberus.crud.factory.IFactoryTestCaseStepActionControlExecution) ArrayList(java.util.ArrayList) IFactoryTestCaseStepActionControlExecution(org.cerberus.crud.factory.IFactoryTestCaseStepActionControlExecution) Date(java.util.Date)

Aggregations

IRecorderService (org.cerberus.engine.execution.IRecorderService)5 ApplicationContext (org.springframework.context.ApplicationContext)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 FileItem (org.apache.commons.fileupload.FileItem)2 FileUploadException (org.apache.commons.fileupload.FileUploadException)2 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)2 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)2 TestCaseStepActionExecution (org.cerberus.crud.entity.TestCaseStepActionExecution)2 ILogEventService (org.cerberus.crud.service.ILogEventService)2 IParameterService (org.cerberus.crud.service.IParameterService)2 MessageEvent (org.cerberus.engine.entity.MessageEvent)2 Answer (org.cerberus.util.answer.Answer)2 JSONObject (org.json.JSONObject)2 PolicyFactory (org.owasp.html.PolicyFactory)2 Gson (com.google.gson.Gson)1 HashMap (java.util.HashMap)1 FileItemFactory (org.apache.commons.fileupload.FileItemFactory)1 FileSizeLimitExceededException (org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException)1 TestCaseExecutionFile (org.cerberus.crud.entity.TestCaseExecutionFile)1