Search in sources :

Example 6 with ITestCaseCountryService

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

the class DuplicateTestCase method processRequest.

// </editor-fold>
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.
     */
    String test = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("test"), "");
    String testCase = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("testCase"), "");
    String originalTest = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("originalTest"), "");
    String originalTestCase = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("originalTestCase"), null);
    /**
     * Checking all constrains before calling the services.
     */
    if (StringUtil.isNullOrEmpty(test) || StringUtil.isNullOrEmpty(testCase) || StringUtil.isNullOrEmpty(originalTest) || originalTestCase != null) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Test Case").replace("%OPERATION%", "Duplicate").replace("%REASON%", "mandatory fields are missing."));
        ans.setResultMessage(msg);
    } else {
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        ITestCaseService testCaseService = appContext.getBean(ITestCaseService.class);
        ITestCaseCountryService testCaseCountryService = appContext.getBean(ITestCaseCountryService.class);
        ITestCaseCountryPropertiesService testCaseCountryPropertiesService = appContext.getBean(ITestCaseCountryPropertiesService.class);
        ITestCaseStepService testCaseStepService = appContext.getBean(ITestCaseStepService.class);
        ITestCaseStepActionService testCaseStepActionService = appContext.getBean(ITestCaseStepActionService.class);
        ITestCaseStepActionControlService testCaseStepActionControlService = appContext.getBean(ITestCaseStepActionControlService.class);
        ITestCaseLabelService testCaseLabelService = appContext.getBean(ITestCaseLabelService.class);
        AnswerItem originalTestAI = testCaseService.readByKey(originalTest, originalTestCase);
        AnswerItem targetTestAI = testCaseService.readByKey(test, testCase);
        TestCase originalTC = (TestCase) originalTestAI.getItem();
        TestCase targetTC = (TestCase) targetTestAI.getItem();
        if (!(originalTestAI.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && originalTestAI.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%", "TestCase").replace("%OPERATION%", "Duplicate").replace("%REASON%", "TestCase 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.
         */
        if (!request.isUserInRole("Test")) {
            // We cannot update the testcase if the user is not at least in Test role.
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
            msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCase").replace("%OPERATION%", "Duplicate").replace("%REASON%", "Not enought privilege to duplicate the testcase. You must belong to Test Privilege."));
            ans.setResultMessage(msg);
        } else if (targetTC != null) {
            // If target Test Case already exists.
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
            msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCase").replace("%OPERATION%", "Duplicate").replace("%REASON%", "The test case you try to create already exists. Please define a test/testcase that is not already existing."));
            ans.setResultMessage(msg);
        } else {
            getInfo(request, originalTC);
            // Update object with new testcase id and insert it in db
            originalTC.setTest(test);
            originalTC.setTestCase(testCase);
            ans = testCaseService.create(originalTC);
            List<TestCaseCountry> countryList = new ArrayList();
            countryList = testCaseCountryService.findTestCaseCountryByTestTestCase(originalTest, originalTestCase);
            boolean success = true;
            if (!countryList.isEmpty()) {
                ans = testCaseCountryService.duplicateList(countryList, test, testCase);
            }
            // List<TestCaseCountry> countryList = getCountryList(test, testCase, request);
            // boolean success = false;
            // if (countryList.isEmpty()) {
            // success = true;
            // } else {
            // success = testCaseCountryService.insertListTestCaseCountry(countryList);
            // }
            List<TestCaseCountryProperties> tccpList = new ArrayList();
            if (!countryList.isEmpty() && ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && success) {
                tccpList = testCaseCountryPropertiesService.findListOfPropertyPerTestTestCase(originalTest, originalTestCase);
                if (!tccpList.isEmpty()) {
                    ans = testCaseCountryPropertiesService.duplicateList(tccpList, test, testCase);
                }
            }
            List<TestCaseStep> tcsList = new ArrayList();
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && success) {
                tcsList = testCaseStepService.getListOfSteps(originalTest, originalTestCase);
                if (!tcsList.isEmpty()) {
                    ans = testCaseStepService.duplicateList(tcsList, test, testCase);
                }
            }
            List<TestCaseStepAction> tcsaList = new ArrayList();
            if (!tcsList.isEmpty() && ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && success) {
                tcsaList = testCaseStepActionService.findTestCaseStepActionbyTestTestCase(originalTest, originalTestCase);
                if (!tcsaList.isEmpty()) {
                    ans = testCaseStepActionService.duplicateList(tcsaList, test, testCase);
                }
            }
            if (!tcsList.isEmpty() && !tcsaList.isEmpty() && ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && success) {
                List<TestCaseStepActionControl> tcsacList = testCaseStepActionControlService.findControlByTestTestCase(originalTest, originalTestCase);
                if (!tcsacList.isEmpty()) {
                    ans = testCaseStepActionControlService.duplicateList(tcsacList, test, testCase);
                }
            }
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && success) {
                List<TestCaseLabel> tclList = testCaseLabelService.readByTestTestCase(originalTest, originalTestCase).getDataList();
                if (!tclList.isEmpty()) {
                    ans = testCaseLabelService.duplicateList(tclList, test, testCase);
                }
            }
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && success) {
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCase").replace("%OPERATION%", "Duplicate"));
                ans.setResultMessage(msg);
                /**
                 * Update was successful. Adding Log entry.
                 */
                ILogEventService logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/DuplicateTestCase", "CREATE", "Create testcase : ['" + test + "'|'" + testCase + "']", 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 : ITestCaseStepActionService(org.cerberus.crud.service.ITestCaseStepActionService) TestCaseStepAction(org.cerberus.crud.entity.TestCaseStepAction) TestCaseCountryProperties(org.cerberus.crud.entity.TestCaseCountryProperties) PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) TestCaseStep(org.cerberus.crud.entity.TestCaseStep) ITestCaseCountryService(org.cerberus.crud.service.ITestCaseCountryService) ApplicationContext(org.springframework.context.ApplicationContext) ITestCaseCountryPropertiesService(org.cerberus.crud.service.ITestCaseCountryPropertiesService) ITestCaseService(org.cerberus.crud.service.ITestCaseService) TestCaseCountry(org.cerberus.crud.entity.TestCaseCountry) IFactoryTestCaseCountry(org.cerberus.crud.factory.IFactoryTestCaseCountry) ILogEventService(org.cerberus.crud.service.ILogEventService) TestCaseStepActionControl(org.cerberus.crud.entity.TestCaseStepActionControl) ITestCaseLabelService(org.cerberus.crud.service.ITestCaseLabelService) ITestCaseStepService(org.cerberus.crud.service.ITestCaseStepService) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) ITestCaseStepActionControlService(org.cerberus.crud.service.ITestCaseStepActionControlService) AnswerItem(org.cerberus.util.answer.AnswerItem) Answer(org.cerberus.util.answer.Answer) JSONObject(org.json.JSONObject) TestCase(org.cerberus.crud.entity.TestCase)

Example 7 with ITestCaseCountryService

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

the class GetCountryForTestCase method doGet.

@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
    String testName = policy.sanitize(httpServletRequest.getParameter("test"));
    String testCaseName = policy.sanitize(httpServletRequest.getParameter("testCase"));
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    ITestCaseCountryService testCaseCountryService = appContext.getBean(ITestCaseCountryService.class);
    JSONArray array = new JSONArray();
    JSONObject jsonObject = new JSONObject();
    for (String country : testCaseCountryService.findListOfCountryByTestTestCase(testName, testCaseName)) {
        array.put(country);
    }
    try {
        jsonObject.put("countriesList", array);
        httpServletResponse.setContentType("application/json");
        httpServletResponse.getWriter().print(jsonObject.toString());
    } catch (JSONException exception) {
        LOG.warn(exception.toString());
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) PolicyFactory(org.owasp.html.PolicyFactory) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) ITestCaseCountryService(org.cerberus.crud.service.ITestCaseCountryService)

Example 8 with ITestCaseCountryService

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

the class CreateTestCaseCountry 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();
    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
    // 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"), null, charset);
    String country = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("country"), "", charset);
    String usrCreated = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("usrCreated"), "", charset);
    // Parameter that we cannot secure as we need the html --> We DECODE them
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    /**
     * Checking all constrains before calling the services.
     */
    if ((StringUtil.isNullOrEmpty(testcase)) || (StringUtil.isNullOrEmpty(test)) || (StringUtil.isNullOrEmpty(country))) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseCountry").replace("%OPERATION%", "Create").replace("%REASON%", "test or testcase or country is missing!"));
        ans.setResultMessage(msg);
    } else {
        // Checking the autorities here.
        ITestCaseService testCaseService = appContext.getBean(ITestCaseService.class);
        AnswerItem resp = testCaseService.readByKey(test, testcase);
        TestCase tc = (TestCase) resp.getItem();
        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%", "TestCaseCountry").replace("%OPERATION%", "Create").replace("%REASON%", "TestCase does not exist."));
            ans.setResultMessage(msg);
        } else if (!request.isUserInRole("Test")) {
            // We cannot update the testcase if the user is not at least in Test role.
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
            msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseCountry").replace("%OPERATION%", "Create").replace("%REASON%", "Not enought privilege to create the testCaseCountry. You must belong to Test Privilege."));
            ans.setResultMessage(msg);
        } else if ((tc.getStatus().equalsIgnoreCase("WORKING")) && !(request.isUserInRole("TestAdmin"))) {
            // If Test Case is WORKING we need TestAdmin priviliges.
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
            msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseCountry").replace("%OPERATION%", "Create").replace("%REASON%", "Not enought privilege to create the testCaseCountry. The test case is in WORKING status and needs TestAdmin privilege to be updated"));
            ans.setResultMessage(msg);
        } else {
            /**
             * All data seems cleans so we can call the services.
             */
            ITestCaseCountryService testCaseCountryService = appContext.getBean(ITestCaseCountryService.class);
            IFactoryTestCaseCountry factoryTestCaseCountry = appContext.getBean(IFactoryTestCaseCountry.class);
            TestCaseCountry testCaseCountryData = factoryTestCaseCountry.create(test, testcase, country);
            ans = testCaseCountryService.create(testCaseCountryData);
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                /**
                 * Object created. Adding Log entry.
                 */
                ILogEventService logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/CreateTestCaseCountry", "CREATE", "Create TestCaseCountry : ['" + test + "'|'" + testcase + "'|'" + country + "']", 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) MessageEvent(org.cerberus.engine.entity.MessageEvent) IFactoryTestCaseCountry(org.cerberus.crud.factory.IFactoryTestCaseCountry) AnswerItem(org.cerberus.util.answer.AnswerItem) ITestCaseCountryService(org.cerberus.crud.service.ITestCaseCountryService) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) TestCase(org.cerberus.crud.entity.TestCase) ITestCaseService(org.cerberus.crud.service.ITestCaseService) TestCaseCountry(org.cerberus.crud.entity.TestCaseCountry) IFactoryTestCaseCountry(org.cerberus.crud.factory.IFactoryTestCaseCountry) ILogEventService(org.cerberus.crud.service.ILogEventService)

Example 9 with ITestCaseCountryService

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

the class CreateNotDefinedProperty 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 {
    JSONObject jsonResponse = new JSONObject();
    MessageEvent rs = null;
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    ITestCaseCountryPropertiesService testCaseCountryPropertiesService = appContext.getBean(TestCaseCountryPropertiesService.class);
    ITestCaseCountryService testCaseCountryService = appContext.getBean(TestCaseCountryService.class);
    IFactoryTestCaseCountryProperties factoryTestCaseCountryProperties = appContext.getBean(FactoryTestCaseCountryProperties.class);
    try {
        String propertyName = request.getParameter("property");
        if (propertyName != null) {
            propertyName = propertyName.replace("%", "");
        }
        String toTest = request.getParameter("totest");
        String toTestCase = request.getParameter("totestcase");
        String propertyType = request.getParameter("propertyType");
        String userLanguage = request.getParameter("userLanguage");
        // We retrieve all country of the destination TestCase
        List<String> toCountriesAll = testCaseCountryService.findListOfCountryByTestTestCase(toTest, toTestCase);
        if (toCountriesAll != null && toCountriesAll.size() > 0) {
            // Variable for the properties list of the destination TestCase
            List<TestCaseCountryProperties> listOfPropertiesToInsert = new ArrayList<TestCaseCountryProperties>();
            // Variable for the countries of a property of the destination TestCase
            List<String> toCountriesProp;
            IDocumentationService docService = appContext.getBean(DocumentationService.class);
            String notDefinedProperty = docService.findLabel("page_testcase", "txt_property_not_defined", "** Property not defined **", userLanguage);
            // List of all country of the destination test for the current property
            List<String> toCountries = new ArrayList<String>();
            toCountries.addAll(toCountriesAll);
            // Retrieve the country of the destination TestCase for the property,
            // if not empty remove it (property aleady exists for these countries)
            toCountriesProp = testCaseCountryPropertiesService.findCountryByPropertyNameAndTestCase(toTest, toTestCase, propertyName);
            if (toCountriesProp != null && toCountriesProp.size() > 0) {
                toCountries.removeAll(toCountriesProp);
            }
            for (String country : toCountries) {
                listOfPropertiesToInsert.add(factoryTestCaseCountryProperties.create(toTest, toTestCase, country, propertyName, "", propertyType, "---", notDefinedProperty, "", "0", 0, "STATIC", 0, 10000, 0));
            }
            Answer answer = testCaseCountryPropertiesService.createListTestCaseCountryPropertiesBatch(listOfPropertiesToInsert);
            rs = answer.getResultMessage();
            // then a new entry should be added by the log service
            if (answer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                // Adding Log entry.
                ILogEventService logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/CreateNotDefinedProperty", "CREATE", "Create NotDefinedProperty:" + " " + propertyName, request);
            }
        } else {
            rs = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
            rs.setDescription(rs.getDescription().replace("%ITEM%", "Property ").replace("%OPERATION%", "CREATE").replace("%REASON%", "No countries were defined for the test case."));
        }
        // sets the message returned by the operations
        jsonResponse.put("messageType", rs.getMessage().getCodeString());
        jsonResponse.put("message", rs.getDescription());
        response.setContentType("application/json");
        response.getWriter().print(jsonResponse);
        response.getWriter().flush();
    } catch (JSONException ex) {
        LOG.warn(ex);
        // returns a default error message with the json format that is able to be parsed by the client-side
        response.getWriter().print(AnswerUtil.createGenericErrorAnswer());
    }
}
Also used : FactoryTestCaseCountryProperties(org.cerberus.crud.factory.impl.FactoryTestCaseCountryProperties) TestCaseCountryProperties(org.cerberus.crud.entity.TestCaseCountryProperties) IFactoryTestCaseCountryProperties(org.cerberus.crud.factory.IFactoryTestCaseCountryProperties) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) ITestCaseCountryService(org.cerberus.crud.service.ITestCaseCountryService) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) ITestCaseCountryPropertiesService(org.cerberus.crud.service.ITestCaseCountryPropertiesService) ILogEventService(org.cerberus.crud.service.ILogEventService) IFactoryTestCaseCountryProperties(org.cerberus.crud.factory.IFactoryTestCaseCountryProperties) IDocumentationService(org.cerberus.crud.service.IDocumentationService)

Example 10 with ITestCaseCountryService

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

the class DeleteTestCaseCountry 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();
    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.
     */
    String test = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("test"), "", charset);
    String testcase = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("testCase"), null, charset);
    String country = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("country"), "", charset);
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    /**
     * Checking all constrains before calling the services.
     */
    if (testcase == null || (StringUtil.isNullOrEmpty(test)) || (StringUtil.isNullOrEmpty(country))) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseCountry").replace("%OPERATION%", "Delete").replace("%REASON%", "test or testCase or country is missing!"));
        ans.setResultMessage(msg);
    } else {
        // Checking the autorities here.
        ITestCaseService testCaseService = appContext.getBean(ITestCaseService.class);
        AnswerItem resp = testCaseService.readByKey(test, testcase);
        TestCase tc = (TestCase) resp.getItem();
        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%", "TestCaseCountry").replace("%OPERATION%", "Create").replace("%REASON%", "TestCase does not exist."));
            ans.setResultMessage(msg);
        } else if (!request.isUserInRole("Test")) {
            // We cannot update the testcase if the user is not at least in Test role.
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
            msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseCountry").replace("%OPERATION%", "Create").replace("%REASON%", "Not enought privilege to create the testCaseCountry. You must belong to Test Privilege."));
            ans.setResultMessage(msg);
        } else if ((tc.getStatus().equalsIgnoreCase("WORKING")) && !(request.isUserInRole("TestAdmin"))) {
            // If Test Case is WORKING we need TestAdmin priviliges.
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
            msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseCountry").replace("%OPERATION%", "Create").replace("%REASON%", "Not enought privilege to create the testCaseCountry. The test case is in WORKING status and needs TestAdmin privilege to be updated"));
            ans.setResultMessage(msg);
        } else {
            /**
             * All data seems cleans so we can call the services.
             */
            ITestCaseCountryService testCaseCountryService = appContext.getBean(ITestCaseCountryService.class);
            resp = testCaseCountryService.readByKey(test, testcase, country);
            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%", "TestCaseCountry").replace("%OPERATION%", "Delete").replace("%REASON%", "TestCaseCountry 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.
                 */
                TestCaseCountry testCaseCountryData = (TestCaseCountry) resp.getItem();
                ans = testCaseCountryService.delete(testCaseCountryData);
                if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                    /**
                     * Delete was successful. Adding Log entry.
                     */
                    ILogEventService logEventService = appContext.getBean(LogEventService.class);
                    logEventService.createForPrivateCalls("/DeleteTestCaseCountry", "DELETE", "Delete TestCaseCountry : ['" + test + "'|'" + testcase + "'|'" + country + "']", 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 : Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) PolicyFactory(org.owasp.html.PolicyFactory) TestCase(org.cerberus.crud.entity.TestCase) MessageEvent(org.cerberus.engine.entity.MessageEvent) ITestCaseService(org.cerberus.crud.service.ITestCaseService) TestCaseCountry(org.cerberus.crud.entity.TestCaseCountry) ILogEventService(org.cerberus.crud.service.ILogEventService) AnswerItem(org.cerberus.util.answer.AnswerItem) ITestCaseCountryService(org.cerberus.crud.service.ITestCaseCountryService)

Aggregations

ITestCaseCountryService (org.cerberus.crud.service.ITestCaseCountryService)10 ApplicationContext (org.springframework.context.ApplicationContext)8 ILogEventService (org.cerberus.crud.service.ILogEventService)7 ArrayList (java.util.ArrayList)6 TestCaseCountryProperties (org.cerberus.crud.entity.TestCaseCountryProperties)6 ITestCaseCountryPropertiesService (org.cerberus.crud.service.ITestCaseCountryPropertiesService)6 JSONObject (org.json.JSONObject)6 TestCase (org.cerberus.crud.entity.TestCase)5 TestCaseCountry (org.cerberus.crud.entity.TestCaseCountry)5 ITestCaseService (org.cerberus.crud.service.ITestCaseService)5 MessageEvent (org.cerberus.engine.entity.MessageEvent)5 Answer (org.cerberus.util.answer.Answer)5 TestCaseStep (org.cerberus.crud.entity.TestCaseStep)4 TestCaseStepAction (org.cerberus.crud.entity.TestCaseStepAction)4 ITestCaseStepActionService (org.cerberus.crud.service.ITestCaseStepActionService)4 ITestCaseStepService (org.cerberus.crud.service.ITestCaseStepService)4 PolicyFactory (org.owasp.html.PolicyFactory)4 TestCaseStepActionControl (org.cerberus.crud.entity.TestCaseStepActionControl)3 IFactoryTestCaseCountry (org.cerberus.crud.factory.IFactoryTestCaseCountry)3 ITestCaseStepActionControlService (org.cerberus.crud.service.ITestCaseStepActionControlService)3