Search in sources :

Example 1 with InvariantService

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

the class ReadInvariant 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 {
    String echo = request.getParameter("sEcho");
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
    invariantService = appContext.getBean(InvariantService.class);
    String charset = request.getCharacterEncoding();
    response.setContentType("application/json");
    response.setCharacterEncoding("utf8");
    // type=public or private? //TODO?
    try {
        AnswerItem answer;
        JSONObject jsonResponse = new JSONObject();
        String access = request.getParameter("access");
        if (request.getParameter("idName") == null && access != null) {
            if (!Strings.isNullOrEmpty(request.getParameter("columnName"))) {
                answer = findDistinctValuesOfColumn(appContext, request, request.getParameter("columnName"), access);
                jsonResponse = (JSONObject) answer.getItem();
            } else {
                answer = findInvariantList(appContext, access, request, response);
                jsonResponse = (JSONObject) answer.getItem();
            }
        } else if (request.getParameter("value") == null) {
            // loads the list of invariants
            String idName = policy.sanitize(request.getParameter("idName"));
            answer = findInvariantListByIdName(appContext, access, idName);
            jsonResponse = (JSONObject) answer.getItem();
        } else {
            String idName = request.getParameter("idName");
            String value = request.getParameter("value");
            try {
                answer = findInvariantListBykey(appContext, idName, value);
                JSONObject inv = new JSONObject();
                inv = convertInvariantToJSONObject((Invariant) answer.getItem());
                inv.put("hasPermissionsUpdate", invariantService.hasPermissionsUpdate((Invariant) answer.getItem(), request));
                jsonResponse.put("contentTable", inv);
            } catch (CerberusException e) {
                answer = new AnswerItem();
                MessageEvent msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);
                answer.setResultMessage(msg);
            }
        }
        jsonResponse.put("messageType", answer.getResultMessage().getMessage().getCodeString());
        jsonResponse.put("message", answer.getResultMessage().getDescription());
        // TODO:FN check if this makes sense
        jsonResponse.put("sEcho", echo);
        response.getWriter().print(jsonResponse.toString());
    } catch (JSONException e) {
        LOG.warn(e);
        // 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 : ApplicationContext(org.springframework.context.ApplicationContext) CerberusException(org.cerberus.exception.CerberusException) PolicyFactory(org.owasp.html.PolicyFactory) JSONObject(org.json.JSONObject) MessageEvent(org.cerberus.engine.entity.MessageEvent) JSONException(org.json.JSONException) InvariantService(org.cerberus.crud.service.impl.InvariantService) IInvariantService(org.cerberus.crud.service.IInvariantService) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 2 with InvariantService

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

the class ReadInvariant method findInvariantListBykey.

private AnswerItem findInvariantListBykey(ApplicationContext appContext, String idName, String value) throws JSONException, CerberusException {
    AnswerItem answer = new AnswerItem();
    // finds the list of invariants by idname
    invariantService = appContext.getBean(InvariantService.class);
    answer.setItem(invariantService.convert(invariantService.readByKey(idName, value)));
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
    msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "SELECT"));
    answer.setResultMessage(msg);
    return answer;
}
Also used : MessageEvent(org.cerberus.engine.entity.MessageEvent) InvariantService(org.cerberus.crud.service.impl.InvariantService) IInvariantService(org.cerberus.crud.service.IInvariantService) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 3 with InvariantService

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

the class ReadTestCaseExecution method findValuesForColumnFilter.

/**
 * Find Values to display for Column Filter
 *
 * @param system
 * @param test
 * @param appContext
 * @param request
 * @param columnName
 * @return
 * @throws JSONException
 */
private AnswerItem findValuesForColumnFilter(String system, String test, ApplicationContext appContext, HttpServletRequest request, String columnName) throws JSONException {
    AnswerItem answer = new AnswerItem();
    JSONObject object = new JSONObject();
    AnswerList values = new AnswerList();
    Map<String, List<String>> individualSearch = new HashMap<>();
    testCaseService = appContext.getBean(TestCaseService.class);
    invariantService = appContext.getBean(InvariantService.class);
    buildRevisionInvariantService = appContext.getBean(BuildRevisionInvariantService.class);
    applicationService = appContext.getBean(ApplicationService.class);
    LOG.debug(columnName);
    switch(columnName) {
        /**
         * Columns from Status
         */
        case "exe.controlStatus":
            List<String> dataList = new ArrayList<>();
            dataList.add(TestCaseExecution.CONTROLSTATUS_CA);
            dataList.add(TestCaseExecution.CONTROLSTATUS_FA);
            dataList.add(TestCaseExecution.CONTROLSTATUS_KO);
            dataList.add(TestCaseExecution.CONTROLSTATUS_NA);
            dataList.add(TestCaseExecution.CONTROLSTATUS_NE);
            dataList.add(TestCaseExecution.CONTROLSTATUS_OK);
            dataList.add(TestCaseExecution.CONTROLSTATUS_PE);
            values.setDataList(dataList);
            MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
            msg.setDescription(msg.getDescription().replace("%ITEM%", "execution").replace("%OPERATION%", "SELECT"));
            values.setResultMessage(msg);
            break;
        /**
         * For columns test and testcase, get distinct values from test
         * table
         */
        case "exe.test":
        case "exe.testcase":
        case "exe.status":
            values = testCaseService.readDistinctValuesByCriteria(system, test, "", null, columnName.replace("exe.", "tec."));
            break;
        /**
         * For columns country, environment get values from invariant
         */
        case "exe.country":
        case "exe.environment":
            try {
                /**
                 */
                AnswerList<Invariant> invariants = invariantService.readByIdname(columnName.replace("exe.", ""));
                List<Invariant> invariantList = invariantService.convert(invariants);
                List<String> stringResult = new ArrayList();
                for (Invariant inv : invariantList) {
                    stringResult.add(inv.getValue());
                }
                values.setDataList(stringResult);
                values.setTotalRows(invariantList.size());
                values.setResultMessage(invariants.getResultMessage());
            } catch (CerberusException ex) {
                LOG.warn(ex);
            }
            break;
        /**
         * For columns build, revision get values from
         * buildrevisioninvariant
         */
        case "exe.build":
        case "exe.revision":
            individualSearch = new HashMap<>();
            individualSearch.put("level", new ArrayList(Arrays.asList(columnName.equals("exe.build") ? "1" : "2")));
            values = buildRevisionInvariantService.readDistinctValuesByCriteria(system, "", individualSearch, "versionName");
            break;
        /**
         * For columns application get values from application
         */
        case "exe.application":
            values = applicationService.readDistinctValuesByCriteria(system, "", null, columnName.replace("exe.", ""));
            break;
        /**
         * For all other columns, get distinct values from testcaseexecution
         */
        default:
            String searchParameter = ParameterParserUtil.parseStringParam(request.getParameter("sSearch"), "");
            String sColumns = ParameterParserUtil.parseStringParam(request.getParameter("sColumns"), "tec.test,tec.testcase,application,project,ticket,description,behaviororvalueexpected,readonly,bugtrackernewurl,deploytype,mavengroupid");
            String[] columnToSort = sColumns.split(",");
            List<String> individualLike = new ArrayList(Arrays.asList(ParameterParserUtil.parseStringParam(request.getParameter("sLike"), "").split(",")));
            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(",")));
                    if (individualLike.contains(columnToSort[a])) {
                        individualSearch.put(columnToSort[a] + ":like", search);
                    } else {
                        individualSearch.put(columnToSort[a], search);
                    }
                }
            }
            values = testCaseExecutionService.readDistinctValuesByCriteria(system, test, searchParameter, individualSearch, columnName);
    }
    object.put("distinctValues", values.getDataList());
    answer.setItem(object);
    answer.setResultMessage(values.getResultMessage());
    return answer;
}
Also used : Invariant(org.cerberus.crud.entity.Invariant) AnswerList(org.cerberus.util.answer.AnswerList) CerberusException(org.cerberus.exception.CerberusException) BuildRevisionInvariantService(org.cerberus.crud.service.impl.BuildRevisionInvariantService) IBuildRevisionInvariantService(org.cerberus.crud.service.IBuildRevisionInvariantService) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) ITestCaseService(org.cerberus.crud.service.ITestCaseService) TestCaseService(org.cerberus.crud.service.impl.TestCaseService) AnswerItem(org.cerberus.util.answer.AnswerItem) JSONObject(org.json.JSONObject) AnswerList(org.cerberus.util.answer.AnswerList) List(java.util.List) ArrayList(java.util.ArrayList) InvariantService(org.cerberus.crud.service.impl.InvariantService) BuildRevisionInvariantService(org.cerberus.crud.service.impl.BuildRevisionInvariantService) IInvariantService(org.cerberus.crud.service.IInvariantService) IBuildRevisionInvariantService(org.cerberus.crud.service.IBuildRevisionInvariantService) ApplicationService(org.cerberus.crud.service.impl.ApplicationService) IApplicationService(org.cerberus.crud.service.IApplicationService)

Example 4 with InvariantService

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

the class ReadInvariant method findInvariantListByIdName.

// </editor-fold>
private AnswerItem findInvariantListByIdName(ApplicationContext appContext, String access, String idName) throws JSONException {
    AnswerList answerService;
    AnswerItem answer = new AnswerItem();
    JSONObject object = new JSONObject();
    // finds the list of invariants by idname
    invariantService = appContext.getBean(InvariantService.class);
    answerService = invariantService.readByIdname(idName);
    JSONArray jsonArray = new JSONArray();
    if (answerService.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        // if the service returns an OK message then we can get the item and convert it to JSONformat
        for (Invariant inv : (List<Invariant>) answerService.getDataList()) {
            jsonArray.put(convertInvariantToJSONObject(inv));
        }
    }
    object.put("contentTable", jsonArray);
    object.put("iTotalRecords", answerService.getTotalRows());
    object.put("iTotalDisplayRecords", answerService.getTotalRows());
    answer.setItem(object);
    answer.setResultMessage(answerService.getResultMessage());
    return answer;
}
Also used : Invariant(org.cerberus.crud.entity.Invariant) AnswerList(org.cerberus.util.answer.AnswerList) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) AnswerList(org.cerberus.util.answer.AnswerList) InvariantService(org.cerberus.crud.service.impl.InvariantService) IInvariantService(org.cerberus.crud.service.IInvariantService) AnswerItem(org.cerberus.util.answer.AnswerItem)

Aggregations

IInvariantService (org.cerberus.crud.service.IInvariantService)4 InvariantService (org.cerberus.crud.service.impl.InvariantService)4 AnswerItem (org.cerberus.util.answer.AnswerItem)4 MessageEvent (org.cerberus.engine.entity.MessageEvent)3 JSONObject (org.json.JSONObject)3 Invariant (org.cerberus.crud.entity.Invariant)2 CerberusException (org.cerberus.exception.CerberusException)2 AnswerList (org.cerberus.util.answer.AnswerList)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 IApplicationService (org.cerberus.crud.service.IApplicationService)1 IBuildRevisionInvariantService (org.cerberus.crud.service.IBuildRevisionInvariantService)1 ITestCaseService (org.cerberus.crud.service.ITestCaseService)1 ApplicationService (org.cerberus.crud.service.impl.ApplicationService)1 BuildRevisionInvariantService (org.cerberus.crud.service.impl.BuildRevisionInvariantService)1 TestCaseService (org.cerberus.crud.service.impl.TestCaseService)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1