Search in sources :

Example 11 with ITestCaseService

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

the class GetStepInLibrary 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 {
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
    // String system = policy.sanitize(request.getParameter("system"));
    String system = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("system"), null);
    String test = policy.sanitize(request.getParameter("test"));
    String testCase = policy.sanitize(request.getParameter("testCase"));
    String withTestCase = policy.sanitize(request.getParameter("withTestCase"));
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    ITestCaseStepService testCaseStepService = appContext.getBean(ITestCaseStepService.class);
    ITestCaseService testCaseService = appContext.getBean(ITestCaseService.class);
    JSONArray array = new JSONArray();
    JSONObject jsonObject = new JSONObject();
    try {
        List<TestCaseStep> tcsList;
        if (test.equals("") && testCase.equals("")) {
            tcsList = testCaseStepService.getStepLibraryBySystem(system);
        } else if (testCase.equals("")) {
            tcsList = testCaseStepService.getStepLibraryBySystemTest(system, test);
        } else {
            tcsList = testCaseStepService.getStepLibraryBySystemTestTestCase(system, test, testCase);
        }
        for (TestCaseStep list : tcsList) {
            JSONObject tcs = new JSONObject();
            tcs.put("test", list.getTest());
            tcs.put("testCase", list.getTestCase());
            tcs.put("step", list.getStep());
            tcs.put("sort", list.getSort());
            tcs.put("description", list.getDescription());
            if (list.getTestCaseObj() != null) {
                tcs.put("tcdesc", list.getTestCaseObj().getDescription());
                tcs.put("tcapp", list.getTestCaseObj().getApplication());
            }
            array.put(tcs);
        }
        jsonObject.put("testCaseStepList", array);
        response.setContentType("application/json");
        response.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) ITestCaseStepService(org.cerberus.crud.service.ITestCaseStepService) ITestCaseService(org.cerberus.crud.service.ITestCaseService) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) TestCaseStep(org.cerberus.crud.entity.TestCaseStep)

Example 12 with ITestCaseService

use of org.cerberus.crud.service.ITestCaseService 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 13 with ITestCaseService

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

the class DeleteTestCase 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.
     */
    String test = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("test"), "");
    String testCase = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("testCase"), null);
    /**
     * Checking all constrains before calling the services.
     */
    if (StringUtil.isNullOrEmpty(test) || testCase == null) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCase").replace("%OPERATION%", "Delete").replace("%REASON%", "mendatory fields is missing!"));
        ans.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        ITestCaseService testCaseService = appContext.getBean(ITestCaseService.class);
        ITestCaseStepService testCaseStepService = appContext.getBean(ITestCaseStepService.class);
        AnswerItem resp = testCaseService.readByKey(test, testCase);
        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%", "TestCase").replace("%OPERATION%", "Delete").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 delete it.
             */
            TestCase testCaseData = (TestCase) resp.getItem();
            List<TestCaseStep> tcsList = testCaseStepService.getTestCaseStepUsingTestCaseInParamter(testCaseData.getTest(), testCaseData.getTestCase());
            if (tcsList != null && !tcsList.isEmpty()) {
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
                msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCase").replace("%OPERATION%", "Delete").replace("%REASON%", "You're trying to delete a testcase which have some step used in other tests. Please remove the link before deleting this testcase."));
                ans.setResultMessage(msg);
            } else {
                ans = testCaseService.delete(testCaseData);
            }
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                /**
                 * Delete was successful. Adding Log entry.
                 */
                ILogEventService logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/DeleteTestCase", "DELETE", "Delete TestCase : ['" + 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.toString());
    response.getWriter().flush();
}
Also used : PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) ITestCaseStepService(org.cerberus.crud.service.ITestCaseStepService) TestCaseStep(org.cerberus.crud.entity.TestCaseStep) AnswerItem(org.cerberus.util.answer.AnswerItem) 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) ILogEventService(org.cerberus.crud.service.ILogEventService)

Example 14 with ITestCaseService

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

the class ReadAppService method getTestCasesUsingService.

/**
 * Auxiliary method that extracts the list of test cases that are currently
 * using one service.
 *
 * @param appContext - context object used to get the required beans
 * @param service - identifier of the service
 * @return an answer item containing the information about the test cases
 * that use the entry
 * @throws JSONException
 */
private AnswerItem getTestCasesUsingService(String service, ApplicationContext appContext) throws JSONException {
    JSONObject object = new JSONObject();
    JSONArray objectArray = new JSONArray();
    AnswerItem ansItem = new AnswerItem();
    ITestCaseService tcService = appContext.getBean(ITestCaseService.class);
    AnswerList ansList = tcService.findTestCasesThatUseService(service);
    // if the response is success then we can iterate and search for the data
    if (ansList.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        List<TestListDTO> listDTO = ansList.getDataList();
        for (TestListDTO l : listDTO) {
            JSONArray jsonArray = new JSONArray();
            JSONArray arrTestCase = new JSONArray();
            for (TestCaseListDTO testCase : l.getTestCaseList()) {
                JSONObject jsonTestCase = new JSONObject();
                jsonTestCase.put("TestCaseNumber", testCase.getTestCaseNumber());
                jsonTestCase.put("TestCaseDescription", testCase.getTestCaseDescription());
                jsonTestCase.put("Creator", testCase.getCreator());
                jsonTestCase.put("Active", testCase.isIsActive());
                jsonTestCase.put("Status", testCase.getStatus());
                jsonTestCase.put("Group", testCase.getGroup());
                jsonTestCase.put("Application", testCase.getApplication());
                arrTestCase.put(jsonTestCase);
            }
            // test details
            jsonArray.put(l.getTest());
            jsonArray.put(l.getDescription());
            jsonArray.put(l.getTestCaseList().size());
            jsonArray.put(arrTestCase);
            // test case details
            objectArray.put(jsonArray);
        }
    }
    object.put("TestCasesList", objectArray);
    ansItem.setItem(object);
    ansItem.setResultMessage(ansList.getResultMessage());
    return ansItem;
}
Also used : TestCaseListDTO(org.cerberus.dto.TestCaseListDTO) AnswerList(org.cerberus.util.answer.AnswerList) JSONObject(org.json.JSONObject) TestListDTO(org.cerberus.dto.TestListDTO) JSONArray(org.json.JSONArray) ITestCaseService(org.cerberus.crud.service.ITestCaseService) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 15 with ITestCaseService

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

the class ReadCampaign method findCampaignByKey.

private AnswerItem findCampaignByKey(String key, Boolean userHasPermissions, ApplicationContext appContext, HttpServletRequest request) throws JSONException {
    AnswerItem item = new AnswerItem();
    JSONObject object = new JSONObject();
    campaignService = appContext.getBean(ICampaignService.class);
    AnswerItem answer = campaignService.readByKey(key);
    Campaign p;
    if (answer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        // the service was able to perform the query, then we should get all values
        p = (Campaign) answer.getItem();
        JSONObject response = convertCampaigntoJSONObject(p);
        if (request.getParameter("parameter") != null) {
            ICampaignParameterService campaignParameterService = appContext.getBean(ICampaignParameterService.class);
            AnswerList resp = campaignParameterService.readByCampaign(key);
            if (resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                // the service was able to perform the query, then we should get all values
                JSONArray a = new JSONArray();
                for (Object c : resp.getDataList()) {
                    CampaignParameter cc = (CampaignParameter) c;
                    a.put(convertCampaignParametertoJSONObject(cc));
                }
                response.put("parameter", a);
            }
        }
        if (request.getParameter("label") != null) {
            ICampaignLabelService campaignLabelService = appContext.getBean(ICampaignLabelService.class);
            AnswerList resp = campaignLabelService.readByVarious(key);
            if (resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                // the service was able to perform the query, then we should get all values
                JSONArray a = new JSONArray();
                for (Object c : resp.getDataList()) {
                    CampaignLabel cc = (CampaignLabel) c;
                    a.put(convertCampaignLabeltoJSONObject(cc));
                }
                response.put("label", a);
            }
        }
        if (request.getParameter("testcase") != null) {
            ITestCaseService testCaseService = appContext.getBean(ITestCaseService.class);
            String[] campaignList = new String[1];
            campaignList[0] = key;
            AnswerItem<List<TestCase>> resp = testCaseService.findTestCaseByCampaignNameAndCountries(key, null);
            if (resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                // the service was able to perform the query, then we should get all values
                JSONArray a = new JSONArray();
                for (Object c : resp.getItem()) {
                    TestCase cc = (TestCase) c;
                    a.put(convertTestCasetoJSONObject(cc));
                }
                response.put("testcase", a);
            }
        }
        object.put("contentTable", response);
    }
    object.put("hasPermissions", userHasPermissions);
    item.setItem(object);
    item.setResultMessage(answer.getResultMessage());
    return item;
}
Also used : ICampaignService(org.cerberus.crud.service.ICampaignService) AnswerList(org.cerberus.util.answer.AnswerList) ICampaignParameterService(org.cerberus.crud.service.ICampaignParameterService) JSONArray(org.json.JSONArray) AnswerItem(org.cerberus.util.answer.AnswerItem) CampaignParameter(org.cerberus.crud.entity.CampaignParameter) Campaign(org.cerberus.crud.entity.Campaign) JSONObject(org.json.JSONObject) TestCase(org.cerberus.crud.entity.TestCase) ITestCaseService(org.cerberus.crud.service.ITestCaseService) JSONObject(org.json.JSONObject) ICampaignLabelService(org.cerberus.crud.service.ICampaignLabelService) AnswerList(org.cerberus.util.answer.AnswerList) ArrayList(java.util.ArrayList) List(java.util.List) CampaignLabel(org.cerberus.crud.entity.CampaignLabel)

Aggregations

ITestCaseService (org.cerberus.crud.service.ITestCaseService)24 ApplicationContext (org.springframework.context.ApplicationContext)21 TestCase (org.cerberus.crud.entity.TestCase)20 JSONObject (org.json.JSONObject)20 PolicyFactory (org.owasp.html.PolicyFactory)16 AnswerItem (org.cerberus.util.answer.AnswerItem)12 JSONArray (org.json.JSONArray)11 ILogEventService (org.cerberus.crud.service.ILogEventService)10 MessageEvent (org.cerberus.engine.entity.MessageEvent)9 JSONException (org.json.JSONException)9 TestCaseStep (org.cerberus.crud.entity.TestCaseStep)8 Answer (org.cerberus.util.answer.Answer)8 TestCaseCountry (org.cerberus.crud.entity.TestCaseCountry)7 CerberusException (org.cerberus.exception.CerberusException)7 ArrayList (java.util.ArrayList)5 TestCaseCountryProperties (org.cerberus.crud.entity.TestCaseCountryProperties)5 TestCaseStepAction (org.cerberus.crud.entity.TestCaseStepAction)5 TestCaseStepActionControl (org.cerberus.crud.entity.TestCaseStepActionControl)5 ITestCaseStepService (org.cerberus.crud.service.ITestCaseStepService)5 List (java.util.List)4