Search in sources :

Example 6 with TestCaseLabel

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

the class TestCaseLabelService method compareListAndUpdateInsertDeleteElements.

@Override
public Answer compareListAndUpdateInsertDeleteElements(String test, String testCase, List<TestCaseLabel> newList) {
    Answer ans = new Answer(null);
    MessageEvent msg1 = new MessageEvent(MessageEventEnum.GENERIC_OK);
    Answer finalAnswer = new Answer(msg1);
    List<TestCaseLabel> oldList = new ArrayList();
    try {
        oldList = this.convert(this.readByTestTestCase(test, testCase));
    } catch (CerberusException ex) {
        LOG.error(ex);
    }
    /**
     * Update and Create all objects database Objects from newList
     */
    List<TestCaseLabel> listToUpdateOrInsert = new ArrayList(newList);
    listToUpdateOrInsert.removeAll(oldList);
    List<TestCaseLabel> listToUpdateOrInsertToIterate = new ArrayList(listToUpdateOrInsert);
    for (TestCaseLabel objectDifference : listToUpdateOrInsertToIterate) {
        for (TestCaseLabel objectInDatabase : oldList) {
            if (objectDifference.hasSameKey(objectInDatabase)) {
                ans = this.update(objectDifference);
                finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
                listToUpdateOrInsert.remove(objectDifference);
            }
        }
    }
    /**
     * Delete all objects database Objects that do not exist from newList
     */
    List<TestCaseLabel> listToDelete = new ArrayList(oldList);
    listToDelete.removeAll(newList);
    List<TestCaseLabel> listToDeleteToIterate = new ArrayList(listToDelete);
    for (TestCaseLabel tcsDifference : listToDeleteToIterate) {
        for (TestCaseLabel tcsInPage : newList) {
            if (tcsDifference.hasSameKey(tcsInPage)) {
                listToDelete.remove(tcsDifference);
            }
        }
    }
    if (!listToDelete.isEmpty()) {
        ans = this.deleteList(listToDelete);
        finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
    }
    // We insert only at the end (after deletion of all potencial enreg - linked with #1281)
    if (!listToUpdateOrInsert.isEmpty()) {
        ans = this.createList(listToUpdateOrInsert);
        finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
    }
    return finalAnswer;
}
Also used : Answer(org.cerberus.util.answer.Answer) CerberusException(org.cerberus.exception.CerberusException) MessageEvent(org.cerberus.engine.entity.MessageEvent) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) ArrayList(java.util.ArrayList)

Example 7 with TestCaseLabel

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

the class ReadTestCaseLabel method findTestCaseLabelList.

// </editor-fold>
private AnswerItem findTestCaseLabelList(ApplicationContext appContext, boolean userHasPermissions, HttpServletRequest request) throws JSONException {
    AnswerItem item = new AnswerItem();
    JSONObject object = new JSONObject();
    testCaseLabelService = appContext.getBean(ITestCaseLabelService.class);
    int startPosition = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayStart"), "0"));
    int length = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayLength"), "0"));
    /*int sEcho  = Integer.valueOf(request.getParameter("sEcho"));*/
    String searchParameter = ParameterParserUtil.parseStringParam(request.getParameter("sSearch"), "");
    int columnToSortParameter = Integer.parseInt(ParameterParserUtil.parseStringParam(request.getParameter("iSortCol_0"), "0"));
    String sColumns = ParameterParserUtil.parseStringParam(request.getParameter("sColumns"), "Label,Description,sort,type,system,subsystem,svnurl,bugtrackerurl,bugtrackernewurl,deploytype,mavengroupid");
    String[] columnToSort = sColumns.split(",");
    String columnName = columnToSort[columnToSortParameter];
    String sort = ParameterParserUtil.parseStringParam(request.getParameter("sSortDir_0"), "asc");
    Map<String, List<String>> individualSearch = new HashMap<>();
    for (int a = 0; a < columnToSort.length; a++) {
        if (null != request.getParameter("sSearch_" + a) && !request.getParameter("sSearch_" + a).isEmpty()) {
            List<String> search = new ArrayList(Arrays.asList(request.getParameter("sSearch_" + a).split(",")));
            individualSearch.put(columnToSort[a], search);
        }
    }
    AnswerList resp = testCaseLabelService.readByCriteria(startPosition, length, columnName, sort, searchParameter, individualSearch);
    JSONArray jsonArray = new JSONArray();
    if (resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        // the service was able to perform the query, then we should get all values
        for (TestCaseLabel label : (List<TestCaseLabel>) resp.getDataList()) {
            jsonArray.put(convertTestCaseLabelToJSONObject(label));
        }
    }
    object.put("hasPermissions", userHasPermissions);
    object.put("contentTable", jsonArray);
    object.put("iTotalRecords", resp.getTotalRows());
    object.put("iTotalDisplayRecords", resp.getTotalRows());
    item.setItem(object);
    item.setResultMessage(resp.getResultMessage());
    return item;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) HashMap(java.util.HashMap) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) AnswerItem(org.cerberus.util.answer.AnswerItem) JSONObject(org.json.JSONObject) AnswerList(org.cerberus.util.answer.AnswerList) ArrayList(java.util.ArrayList) List(java.util.List) ITestCaseLabelService(org.cerberus.crud.service.ITestCaseLabelService)

Example 8 with TestCaseLabel

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

the class ReadTestCaseLabel method findByTestTestCase.

private AnswerItem findByTestTestCase(ApplicationContext appContext, boolean userHasPermissions, HttpServletRequest request) throws JSONException {
    AnswerItem item = new AnswerItem();
    JSONObject object = new JSONObject();
    testCaseLabelService = appContext.getBean(ITestCaseLabelService.class);
    String test = request.getParameter("test");
    String testcase = request.getParameter("testcase");
    AnswerList resp = testCaseLabelService.readByTestTestCase(test, testcase);
    JSONArray jsonArray = new JSONArray();
    if (resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        // the service was able to perform the query, then we should get all values
        for (TestCaseLabel label : (List<TestCaseLabel>) resp.getDataList()) {
            jsonArray.put(convertTestCaseLabelToJSONObject(label));
        }
    }
    object.put("hasPermissions", userHasPermissions);
    object.put("contentTable", jsonArray);
    object.put("iTotalRecords", resp.getTotalRows());
    object.put("iTotalDisplayRecords", resp.getTotalRows());
    item.setItem(object);
    item.setResultMessage(resp.getResultMessage());
    return item;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) JSONObject(org.json.JSONObject) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) JSONArray(org.json.JSONArray) 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)

Example 9 with TestCaseLabel

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

the class ReadTestCaseLabel method findLabelByKey.

private AnswerItem findLabelByKey(Integer id, ApplicationContext appContext, boolean userHasPermissions) throws JSONException, CerberusException {
    AnswerItem item = new AnswerItem();
    JSONObject object = new JSONObject();
    ITestCaseLabelService labelService = appContext.getBean(ITestCaseLabelService.class);
    // finds the project
    AnswerItem answer = labelService.readByKeyTech(id);
    if (answer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        // if the service returns an OK message then we can get the item and convert it to JSONformat
        TestCaseLabel label = (TestCaseLabel) answer.getItem();
        JSONObject response = convertTestCaseLabelToJSONObject(label);
        object.put("contentTable", response);
    }
    object.put("hasPermissions", userHasPermissions);
    item.setItem(object);
    item.setResultMessage(answer.getResultMessage());
    return item;
}
Also used : JSONObject(org.json.JSONObject) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) AnswerItem(org.cerberus.util.answer.AnswerItem) ITestCaseLabelService(org.cerberus.crud.service.ITestCaseLabelService)

Example 10 with TestCaseLabel

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

the class SaveTestCaseLabel 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);
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    String charset = request.getCharacterEncoding();
    String usr = request.getUserPrincipal().getName();
    testCaseLabelService = appContext.getBean(ITestCaseLabelService.class);
    testCaseLabelFactory = appContext.getBean(IFactoryTestCaseLabel.class);
    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
    String test = policy.sanitize(request.getParameter("test"));
    String testcase = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("testcase"), "", charset);
    String[] labelIdList = request.getParameterValues("labelid");
    /**
     * Checking all constrains before calling the services.
     */
    if (test.isEmpty() || testcase.isEmpty()) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseLabel").replace("%OPERATION%", "Save").replace("%REASON%", "Mandatory parameter is missing. [test|testcase]"));
        ans.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        List<TestCaseLabel> toSave = new ArrayList();
        if (null != labelIdList && labelIdList.length != 0) {
            Timestamp creationDate = new Timestamp(new Date().getTime());
            for (String lableId : labelIdList) {
                toSave.add(testCaseLabelFactory.create(0, test, testcase, Integer.valueOf(lableId), usr, creationDate, usr, creationDate, null));
            }
        }
        List<TestCaseLabel> existingList = testCaseLabelService.readByTestTestCase(test, testcase).getDataList();
        testCaseLabelService.compareListAndUpdateInsertDeleteElements(test, testcase, toSave);
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseLabel").replace("%OPERATION%", "Save"));
        ans.setResultMessage(msg);
    }
    /**
     * 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) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) IFactoryTestCaseLabel(org.cerberus.crud.factory.IFactoryTestCaseLabel) ArrayList(java.util.ArrayList) Timestamp(java.sql.Timestamp) Date(java.util.Date) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) ITestCaseLabelService(org.cerberus.crud.service.ITestCaseLabelService) IFactoryTestCaseLabel(org.cerberus.crud.factory.IFactoryTestCaseLabel)

Aggregations

TestCaseLabel (org.cerberus.crud.entity.TestCaseLabel)23 ArrayList (java.util.ArrayList)17 JSONObject (org.json.JSONObject)16 ITestCaseLabelService (org.cerberus.crud.service.ITestCaseLabelService)14 IFactoryTestCaseLabel (org.cerberus.crud.factory.IFactoryTestCaseLabel)12 MessageEvent (org.cerberus.engine.entity.MessageEvent)12 AnswerItem (org.cerberus.util.answer.AnswerItem)12 AnswerList (org.cerberus.util.answer.AnswerList)9 JSONArray (org.json.JSONArray)9 List (java.util.List)8 Answer (org.cerberus.util.answer.Answer)8 TestCase (org.cerberus.crud.entity.TestCase)6 PolicyFactory (org.owasp.html.PolicyFactory)6 ApplicationContext (org.springframework.context.ApplicationContext)6 TestCaseCountry (org.cerberus.crud.entity.TestCaseCountry)5 ILogEventService (org.cerberus.crud.service.ILogEventService)5 ITestCaseCountryService (org.cerberus.crud.service.ITestCaseCountryService)5 ITestCaseService (org.cerberus.crud.service.ITestCaseService)5 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4