Search in sources :

Example 1 with DeleteTestCaseExecutionFile

use of org.cerberus.servlet.manualtestcase.DeleteTestCaseExecutionFile 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)1 ILogEventService (org.cerberus.crud.service.ILogEventService)1 IParameterService (org.cerberus.crud.service.IParameterService)1 ITestCaseExecutionFileService (org.cerberus.crud.service.ITestCaseExecutionFileService)1 MessageEvent (org.cerberus.engine.entity.MessageEvent)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 JSONObject (org.json.JSONObject)1 PolicyFactory (org.owasp.html.PolicyFactory)1 ApplicationContext (org.springframework.context.ApplicationContext)1