Search in sources :

Example 6 with ITestService

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

the class UpdateTest 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.json.JSONException
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, JSONException {
    JSONObject jsonResponse = new JSONObject();
    Answer ans = new Answer();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    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 originalTest = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("originalTest"), "");
    String test = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("test"), "");
    String description = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("Description"), "");
    String active = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("Active"), "");
    String automated = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("Automated"), "");
    /**
     * Checking all constrains before calling the services.
     */
    if (StringUtil.isNullOrEmpty(test)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Test").replace("%OPERATION%", "Update").replace("%REASON%", "Test name (test) is missing"));
        ans.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        ITestService testService = appContext.getBean(ITestService.class);
        AnswerItem resp = testService.readByKey(originalTest);
        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%", "Application").replace("%OPERATION%", "Update").replace("%REASON%", "Test does not exist."));
            ans.setResultMessage(msg);
        } else {
            /**
             * The service was able to perform the query and confirm the
             * object exist, then we can update it.
             */
            Test testData = (Test) resp.getItem();
            testData.setTest(test);
            testData.setDescription(description);
            testData.setActive(active);
            testData.setAutomated(automated);
            ans = testService.update(originalTest, testData);
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                /**
                 * Update was successful. Adding Log entry.
                 */
                ILogEventService logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/UpdateTest", "UPDATE", "Updated Test : ['" + originalTest + "']", 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 : Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) PolicyFactory(org.owasp.html.PolicyFactory) ITestService(org.cerberus.crud.service.ITestService) Test(org.cerberus.crud.entity.Test) MessageEvent(org.cerberus.engine.entity.MessageEvent) ILogEventService(org.cerberus.crud.service.ILogEventService) AnswerItem(org.cerberus.util.answer.AnswerItem)

Aggregations

ITestService (org.cerberus.crud.service.ITestService)6 ApplicationContext (org.springframework.context.ApplicationContext)6 JSONObject (org.json.JSONObject)5 Test (org.cerberus.crud.entity.Test)4 ILogEventService (org.cerberus.crud.service.ILogEventService)4 MessageEvent (org.cerberus.engine.entity.MessageEvent)4 Answer (org.cerberus.util.answer.Answer)3 AnswerItem (org.cerberus.util.answer.AnswerItem)3 PolicyFactory (org.owasp.html.PolicyFactory)3 ArrayList (java.util.ArrayList)2 TestCase (org.cerberus.crud.entity.TestCase)2 TestCaseStep (org.cerberus.crud.entity.TestCaseStep)2 IInvariantService (org.cerberus.crud.service.IInvariantService)2 ITestCaseService (org.cerberus.crud.service.ITestCaseService)2 CerberusException (org.cerberus.exception.CerberusException)2 JSONArray (org.json.JSONArray)2 Function (com.google.common.base.Function)1 Date (java.util.Date)1 List (java.util.List)1 Nullable (javax.annotation.Nullable)1