Search in sources :

Example 41 with TestCase

use of org.cerberus.crud.entity.TestCase in project cerberus-source by cerberustesting.

the class UpdateTestCaseMass 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();
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    ILogEventService logEventService = appContext.getBean(LogEventService.class);
    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);
    ITestCaseService testCaseService = appContext.getBean(ITestCaseService.class);
    /**
     * 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 function = request.getParameter("massFunction");
    String status = request.getParameter("massStatus");
    String application = request.getParameter("massApplication");
    // Parameter that we cannot secure as we need the html --> We DECODE them
    String[] myTest = request.getParameterValues("test");
    String[] myTestCase = request.getParameterValues("testcase");
    StringBuilder output_message = new StringBuilder();
    int massErrorCounter = 0;
    int tcCounter = 0;
    for (String myTest1 : myTest) {
        String cur_test = myTest1;
        String cur_testcase = myTestCase[tcCounter];
        /**
         * All data seems cleans so we can call the services.
         */
        AnswerItem resp = testCaseService.readByKey(cur_test, cur_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%", OBJECT_NAME).replace("%OPERATION%", "Update").replace("%REASON%", "TestCase does not exist."));
            ans.setResultMessage(msg);
            massErrorCounter++;
            output_message.append("<br>id : ").append(cur_test).append("|").append(cur_testcase).append(" - ").append(msg.getDescription());
        } else {
            /**
             * The service was able to perform the query and confirm the
             * object exist, then we can update it.
             */
            TestCase tcData = (TestCase) resp.getItem();
            /**
             * Before updating, we check that the old entry can be modified.
             * If old entry point to a build/revision that already been
             * deployed, we cannot update it.
             */
            if (!testCaseService.hasPermissionsUpdate(tcData, request)) {
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
                msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Update").replace("%REASON%", "Not enought privilege to update the testcase. You must belong to Test Privilege or even TestAdmin in case the test is in WORKING status."));
                ans.setResultMessage(msg);
                massErrorCounter++;
                output_message.append("<br>id : ").append(cur_test).append("|").append(cur_testcase).append(" - ").append(msg.getDescription());
            } else // We test that at least a data to update has been defined.
            if ((function != null) || (status != null) || (application != null)) {
                tcData.setUsrModif(request.getRemoteUser());
                tcData.setFunction(ParameterParserUtil.parseStringParamAndDecodeAndSanitize(function, tcData.getFunction(), charset));
                tcData.setStatus(ParameterParserUtil.parseStringParamAndDecodeAndSanitize(status, tcData.getStatus(), charset));
                tcData.setApplication(ParameterParserUtil.parseStringParamAndDecodeAndSanitize(application, tcData.getApplication(), charset));
                ans = testCaseService.update(tcData.getTest(), tcData.getTestCase(), tcData);
                if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                    /**
                     * Update was successful. Adding Log entry.
                     */
                    logEventService.createForPrivateCalls("/UpdateTestCaseMass", "UPDATE", "Updated TestCase : ['" + tcData.getTest() + "'|'" + tcData.getTestCase() + "']", request);
                } else {
                    massErrorCounter++;
                    output_message.append("<br>id : ").append(cur_test).append("|").append(cur_testcase).append(" - ").append(ans.getResultMessage().getDescription());
                }
            }
        }
        tcCounter++;
    }
    if (myTest.length > 1) {
        if (massErrorCounter == myTest.length) {
            // All updates are in ERROR.
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
            msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Mass Update").replace("%REASON%", massErrorCounter + " objects(s) out of " + myTest.length + " failed to update due to an issue.<br>") + output_message.toString());
            ans.setResultMessage(msg);
        } else if (massErrorCounter > 0) {
            // At least 1 update in error
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_WARNING);
            msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Mass Update").replace("%REASON%", massErrorCounter + " objects(s) out of " + myTest.length + " failed to update due to an issue.<br>") + output_message.toString());
            ans.setResultMessage(msg);
        } else {
            // No error detected.
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
            msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Mass Update") + "\n\nAll " + myTest.length + " object(s) updated successfuly.");
            ans.setResultMessage(msg);
        }
        logEventService.createForPrivateCalls("/UpdateTestCaseMass", "MASSUPDATE", msg.getDescription(), 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) 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 42 with TestCase

use of org.cerberus.crud.entity.TestCase in project cerberus-source by cerberustesting.

the class GetTestCaseForTest 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 system = policy.sanitize(httpServletRequest.getParameter("system"));
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    ITestCaseService testService = appContext.getBean(ITestCaseService.class);
    JSONArray array = new JSONArray();
    JSONObject jsonObject = new JSONObject();
    try {
        List<TestCase> tcaseList;
        if (system == null) {
            tcaseList = testService.findTestCaseByTest(testName);
        } else {
            tcaseList = testService.findTestCaseActiveAutomatedBySystem(testName, system);
        }
        for (TestCase list : tcaseList) {
            JSONObject testCase = new JSONObject();
            testCase.put("testCase", list.getTestCase());
            testCase.put("description", list.getTestCase().concat(" [").concat(list.getApplication()).concat("] : ").concat(list.getDescription()));
            testCase.put("application", list.getApplication());
            array.put(testCase);
        }
        jsonObject.put("testCaseList", 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) TestCase(org.cerberus.crud.entity.TestCase) ITestCaseService(org.cerberus.crud.service.ITestCaseService) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 43 with TestCase

use of org.cerberus.crud.entity.TestCase in project cerberus-source by cerberustesting.

the class ReadTestCase method findTestCaseByTestTestCase.

private AnswerItem findTestCaseByTestTestCase(String test, String testCase, ApplicationContext appContext, HttpServletRequest request) throws JSONException {
    AnswerItem item = new AnswerItem();
    JSONObject object = new JSONObject();
    testCaseService = appContext.getBean(ITestCaseService.class);
    testCaseCountryService = appContext.getBean(ITestCaseCountryService.class);
    testCaseLabelService = appContext.getBean(ITestCaseLabelService.class);
    // finds the project
    AnswerItem answerTestCase = testCaseService.readByKey(test, testCase);
    if (answerTestCase.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && answerTestCase.getItem() != null) {
        // if the service returns an OK message then we can get the item and convert it to JSONformat
        TestCase tc = (TestCase) answerTestCase.getItem();
        JSONObject response = convertToJSONObject(tc);
        // Country List feed.
        JSONArray countryArray = new JSONArray();
        AnswerList answerTestCaseCountryList = testCaseCountryService.readByTestTestCase(null, test, testCase);
        for (TestCaseCountry country : (List<TestCaseCountry>) answerTestCaseCountryList.getDataList()) {
            countryArray.put(convertToJSONObject(country));
        }
        response.put("countryList", countryArray);
        // Label List feed.
        JSONArray labelArray = new JSONArray();
        AnswerList answerTestCaseLabelList = testCaseLabelService.readByTestTestCase(test, testCase);
        for (TestCaseLabel label : (List<TestCaseLabel>) answerTestCaseLabelList.getDataList()) {
            labelArray.put(convertToJSONObject(label));
        }
        response.put("labelList", labelArray);
        object.put("contentTable", response);
        object.put("hasPermissionsUpdate", testCaseService.hasPermissionsUpdate(tc, request));
    }
    item.setItem(object);
    item.setResultMessage(answerTestCase.getResultMessage());
    return item;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) JSONObject(org.json.JSONObject) TestCase(org.cerberus.crud.entity.TestCase) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) ITestCaseService(org.cerberus.crud.service.ITestCaseService) JSONArray(org.json.JSONArray) TestCaseCountry(org.cerberus.crud.entity.TestCaseCountry) AnswerList(org.cerberus.util.answer.AnswerList) ArrayList(java.util.ArrayList) List(java.util.List) AnswerItem(org.cerberus.util.answer.AnswerItem) ITestCaseLabelService(org.cerberus.crud.service.ITestCaseLabelService) ITestCaseCountryService(org.cerberus.crud.service.ITestCaseCountryService)

Example 44 with TestCase

use of org.cerberus.crud.entity.TestCase in project cerberus-source by cerberustesting.

the class ReadTestCase method findTestCaseByVarious.

private AnswerItem findTestCaseByVarious(ApplicationContext appContext, HttpServletRequest request) throws JSONException {
    AnswerItem item = new AnswerItem();
    JSONObject object = new JSONObject();
    JSONArray dataArray = new JSONArray();
    String[] test = request.getParameterValues("test");
    String[] idProject = request.getParameterValues("project");
    String[] app = request.getParameterValues("application");
    String[] creator = request.getParameterValues("creator");
    String[] implementer = request.getParameterValues("implementer");
    String[] system = request.getParameterValues("system");
    String[] testBattery = request.getParameterValues("testBattery");
    String[] campaign = request.getParameterValues("campaign");
    String[] priority = request.getParameterValues("priority");
    String[] group = request.getParameterValues("group");
    String[] status = request.getParameterValues("status");
    String[] labelid = request.getParameterValues("labelid");
    int length = ParameterParserUtil.parseIntegerParam(request.getParameter("length"), -1);
    testCaseService = appContext.getBean(ITestCaseService.class);
    AnswerList answer = testCaseService.readByVarious(test, idProject, app, creator, implementer, system, campaign, labelid, priority, group, status, length);
    if (answer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        for (TestCase tc : (List<TestCase>) answer.getDataList()) {
            dataArray.put(convertToJSONObject(tc));
        }
    }
    object.put("contentTable", dataArray);
    item.setItem(object);
    item.setResultMessage(answer.getResultMessage());
    return item;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) JSONObject(org.json.JSONObject) TestCase(org.cerberus.crud.entity.TestCase) JSONArray(org.json.JSONArray) ITestCaseService(org.cerberus.crud.service.ITestCaseService) AnswerList(org.cerberus.util.answer.AnswerList) ArrayList(java.util.ArrayList) List(java.util.List) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 45 with TestCase

use of org.cerberus.crud.entity.TestCase in project cerberus-source by cerberustesting.

the class ReadTestCase method findTestCaseByCampaign.

private AnswerItem findTestCaseByCampaign(ApplicationContext appContext, String campaign) throws JSONException {
    AnswerItem answer = new AnswerItem();
    JSONObject jsonResponse = new JSONObject();
    JSONArray dataArray = new JSONArray();
    String[] campaignList = new String[1];
    campaignList[0] = campaign;
    testCaseService = appContext.getBean(ITestCaseService.class);
    final AnswerItem<Map<String, List<String>>> parsedCampaignParameters = campaignParameterService.parseParametersByCampaign(campaign);
    List<String> countries = parsedCampaignParameters.getItem().get(CampaignParameter.COUNTRY_PARAMETER);
    AnswerItem<List<TestCase>> resp = null;
    if (countries != null && !countries.isEmpty()) {
        resp = testCaseService.findTestCaseByCampaignNameAndCountries(campaign, countries.toArray(new String[countries.size()]));
    } else {
        resp = testCaseService.findTestCaseByCampaignNameAndCountries(campaign, null);
    }
    if (resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        // the service was able to perform the query, then we should get all values
        for (Object c : resp.getItem()) {
            TestCase cc = (TestCase) c;
            dataArray.put(convertToJSONObject(cc));
        }
    }
    jsonResponse.put("contentTable", dataArray);
    answer.setItem(jsonResponse);
    answer.setResultMessage(resp.getResultMessage());
    return answer;
}
Also used : JSONArray(org.json.JSONArray) AnswerItem(org.cerberus.util.answer.AnswerItem) JSONObject(org.json.JSONObject) TestCase(org.cerberus.crud.entity.TestCase) ITestCaseService(org.cerberus.crud.service.ITestCaseService) AnswerList(org.cerberus.util.answer.AnswerList) ArrayList(java.util.ArrayList) List(java.util.List) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

TestCase (org.cerberus.crud.entity.TestCase)68 ITestCaseService (org.cerberus.crud.service.ITestCaseService)30 ArrayList (java.util.ArrayList)29 IFactoryTestCase (org.cerberus.crud.factory.IFactoryTestCase)29 JSONObject (org.json.JSONObject)26 AnswerItem (org.cerberus.util.answer.AnswerItem)24 ApplicationContext (org.springframework.context.ApplicationContext)24 MessageEvent (org.cerberus.engine.entity.MessageEvent)23 SQLException (java.sql.SQLException)17 TestCaseCountry (org.cerberus.crud.entity.TestCaseCountry)17 JSONArray (org.json.JSONArray)17 PolicyFactory (org.owasp.html.PolicyFactory)17 Connection (java.sql.Connection)16 PreparedStatement (java.sql.PreparedStatement)16 ResultSet (java.sql.ResultSet)16 CerberusException (org.cerberus.exception.CerberusException)16 TestCaseStep (org.cerberus.crud.entity.TestCaseStep)15 ILogEventService (org.cerberus.crud.service.ILogEventService)15 AnswerList (org.cerberus.util.answer.AnswerList)15 List (java.util.List)13