Search in sources :

Example 6 with Invariant

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

the class FindTestImplementationStatusPerApplication method processRequest.

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
    String echo = policy.sanitize(request.getParameter("sEcho"));
    String mySystem = policy.sanitize(request.getParameter("MySystem"));
    String application = policy.sanitize(request.getParameter("Application"));
    Connection connection = null;
    JSONObject jsonResponse = new JSONObject();
    try {
        List<String> sArray = new ArrayList<String>();
        if (!mySystem.equals("")) {
            String smySystem = " `system` like '%" + mySystem + "%'";
            sArray.add(smySystem);
        }
        JSONArray data = new JSONArray();
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        IInvariantService invariantService = appContext.getBean(InvariantService.class);
        DatabaseSpring database = appContext.getBean(DatabaseSpring.class);
        connection = database.connect();
        AnswerList answer = invariantService.readByIdnameGp1("TCSTATUS", "Y");
        List<Invariant> myInvariants = answer.getDataList();
        StringBuilder SQL = new StringBuilder();
        StringBuilder SQLa = new StringBuilder();
        StringBuilder SQLb = new StringBuilder();
        SQLa.append("SELECT t.test, count(*) as TOTAL ");
        SQLb.append(" FROM testcase t ");
        for (Invariant i : myInvariants) {
            i.getSort();
            SQLa.append(", Col");
            SQLa.append(String.valueOf(i.getSort()));
            SQLb.append(" LEFT JOIN (SELECT g.test, count(*) as Col");
            SQLb.append(String.valueOf(i.getSort()));
            SQLb.append(" FROM testcase g WHERE Status = '");
            SQLb.append(i.getValue());
            SQLb.append("' and application ='");
            SQLb.append(application);
            SQLb.append("' GROUP BY g.test) Tab");
            SQLb.append(String.valueOf(i.getSort()));
            SQLb.append(" ON Tab");
            SQLb.append(String.valueOf(i.getSort()));
            SQLb.append(".test=t.test ");
        }
        SQLb.append(" where t.application ='");
        SQLb.append(application);
        SQLb.append("'");
        SQLb.append(" group by t.test");
        SQL.append(SQLa);
        SQL.append(SQLb);
        LOG.debug(" SQL1 : " + SQL.toString());
        PreparedStatement stmt_teststatus = connection.prepareStatement(SQL.toString());
        try (ResultSet rs_teststatus = stmt_teststatus.executeQuery()) {
            // Integer tot = 0;
            List<Integer> totLine;
            totLine = new ArrayList<Integer>();
            for (Invariant i : myInvariants) {
                totLine.add(0);
            }
            try {
                while (rs_teststatus.next()) {
                    JSONArray row = new JSONArray();
                    StringBuilder testLink = new StringBuilder();
                    testLink.append("<a href=\"TestCaseList.jsp?test=");
                    testLink.append(rs_teststatus.getString("t.test"));
                    testLink.append("\">");
                    testLink.append(rs_teststatus.getString("t.test"));
                    testLink.append("</a>");
                    row.put(testLink.toString());
                    row.put(rs_teststatus.getString("TOTAL"));
                    for (Invariant i : myInvariants) {
                        i.getSort();
                        row.put(rs_teststatus.getString("Col" + String.valueOf(i.getSort())));
                    }
                    data.put(row);
                }
                // data that will be shown in the table
                jsonResponse.put("aaData", data);
                jsonResponse.put("sEcho", echo);
                jsonResponse.put("iTotalRecords", data.length());
                jsonResponse.put("iTotalDisplayRecords", data.length());
                response.setContentType("application/json");
                response.getWriter().print(jsonResponse.toString());
            } catch (JSONException ex) {
                LOG.warn(ex.toString());
            } finally {
                out.close();
            }
        } catch (SQLException ex) {
            LOG.warn(" Exception trying to query '" + SQL.toString() + "' : " + ex);
        } finally {
            stmt_teststatus.close();
        }
    } catch (Exception ex) {
        LOG.warn(" Exception catched : " + ex);
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
        }
    }
}
Also used : Invariant(org.cerberus.crud.entity.Invariant) AnswerList(org.cerberus.util.answer.AnswerList) PolicyFactory(org.owasp.html.PolicyFactory) SQLException(java.sql.SQLException) IInvariantService(org.cerberus.crud.service.IInvariantService) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) PreparedStatement(java.sql.PreparedStatement) ServletException(javax.servlet.ServletException) SQLException(java.sql.SQLException) JSONException(org.json.JSONException) IOException(java.io.IOException) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) DatabaseSpring(org.cerberus.database.DatabaseSpring) ResultSet(java.sql.ResultSet) PrintWriter(java.io.PrintWriter)

Example 7 with Invariant

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

the class GetEnvironmentsLastChangePerCountry method findBuildRevList.

private AnswerItem findBuildRevList(String system, String envGp, Integer nbDays, ApplicationContext appContext, boolean userHasPermissions, HttpServletRequest request) throws JSONException {
    AnswerItem item = new AnswerItem();
    JSONObject object = new JSONObject();
    invariantService = appContext.getBean(IInvariantService.class);
    ceplService = appContext.getBean(ICountryEnvParam_logService.class);
    AnswerList resp = invariantService.readCountryListEnvironmentLastChanges(system, nbDays);
    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 (Invariant countryInvariant : (List<Invariant>) resp.getDataList()) {
            JSONObject countryJSON;
            countryJSON = convertToJSONObject(countryInvariant);
            AnswerList resp1 = ceplService.readLastChanges(system, countryInvariant.getValue(), nbDays, envGp);
            JSONArray jsonArray1 = new JSONArray();
            if (resp1.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                // the service was able to perform the query, then we should get all values
                for (CountryEnvParam_log countryepl : (List<CountryEnvParam_log>) resp1.getDataList()) {
                    jsonArray1.put(convertToJSONObject(countryepl));
                }
            }
            countryJSON.put("contentTable", jsonArray1);
            jsonArray.put(countryJSON);
        }
    }
    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 : ICountryEnvParam_logService(org.cerberus.crud.service.ICountryEnvParam_logService) Invariant(org.cerberus.crud.entity.Invariant) AnswerList(org.cerberus.util.answer.AnswerList) JSONObject(org.json.JSONObject) IInvariantService(org.cerberus.crud.service.IInvariantService) JSONArray(org.json.JSONArray) AnswerList(org.cerberus.util.answer.AnswerList) List(java.util.List) AnswerItem(org.cerberus.util.answer.AnswerItem) CountryEnvParam_log(org.cerberus.crud.entity.CountryEnvParam_log)

Example 8 with Invariant

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

the class Homepage method readApplicationList.

private AnswerItem readApplicationList(String system, ApplicationContext appContext) throws JSONException {
    AnswerItem item = new AnswerItem();
    JSONObject jsonResponse = new JSONObject();
    IApplicationService applicationService = appContext.getBean(ApplicationService.class);
    AnswerItem resp = applicationService.readTestCaseCountersBySystemByStatus(system);
    JSONArray jsonArray = new JSONArray();
    HashMap<String, HashMap<String, Integer>> totalMap = (HashMap<String, HashMap<String, Integer>>) resp.getItem();
    if (resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null) {
        IInvariantService invariantService = appContext.getBean(InvariantService.class);
        AnswerList<Invariant> answerList = invariantService.readByIdnameGp1("TCSTATUS", "Y");
        List<Invariant> myInvariants = answerList.getDataList();
        for (String application : totalMap.keySet()) {
            JSONObject row = extractRow(application, totalMap, myInvariants);
            jsonArray.put(row);
        }
    }
    jsonResponse.put("aaData", jsonArray);
    jsonResponse.put("iTotalRecords", totalMap.size());
    jsonResponse.put("iTotalDisplayRecords", totalMap.size());
    item.setItem(jsonResponse);
    item.setResultMessage(resp.getResultMessage());
    return item;
}
Also used : Invariant(org.cerberus.crud.entity.Invariant) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) IInvariantService(org.cerberus.crud.service.IInvariantService) JSONArray(org.json.JSONArray) AnswerItem(org.cerberus.util.answer.AnswerItem) IApplicationService(org.cerberus.crud.service.IApplicationService)

Example 9 with Invariant

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

the class UpdateInvariant 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();
    // Calling Servlet Transversal Util.
    ServletUtil.servletStart(request);
    String id = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("idName"), "", charset);
    String value = request.getParameter("value");
    String oriId = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("originalIdName"), "", charset);
    String oriValue = request.getParameter("originalValue");
    String description = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("description"), "", charset);
    String veryShortDescField = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("veryShortDesc"), "", charset);
    String gp1 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp1"), "", charset);
    String gp2 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp2"), "", charset);
    String gp3 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp3"), "", charset);
    String gp4 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp4"), "", charset);
    String gp5 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp5"), "", charset);
    String gp6 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp6"), "", charset);
    String gp7 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp7"), "", charset);
    String gp8 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp8"), "", charset);
    String gp9 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp9"), "", charset);
    Integer sort = 10;
    boolean sort_error = false;
    try {
        if (request.getParameter("sort") != null && !request.getParameter("sort").equals("")) {
            sort = Integer.valueOf(policy.sanitize(request.getParameter("sort")));
        }
    } catch (Exception ex) {
        sort_error = true;
    }
    boolean userHasPermissions = request.isUserInRole("Administrator");
    // Prepare the final answer.
    MessageEvent msg1 = new MessageEvent(MessageEventEnum.GENERIC_OK);
    Answer finalAnswer = new Answer(msg1);
    /**
     * Checking all constrains before calling the services.
     */
    if (StringUtil.isNullOrEmpty(id)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Update").replace("%REASON%", "Invariant name is missing!"));
        finalAnswer.setResultMessage(msg);
    } else if (sort_error) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Update").replace("%REASON%", "Could not manage to convert sort to an integer value!"));
        finalAnswer.setResultMessage(msg);
    } else if (!userHasPermissions) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Update").replace("%REASON%", "You don't have the right to do that"));
        finalAnswer.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        IInvariantService invariantService = appContext.getBean(IInvariantService.class);
        AnswerItem resp = invariantService.readByKey(oriId, oriValue);
        if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {
            /**
             * Object could not be found. We stop here and report the error.
             */
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) resp);
        } else {
            Invariant invariantData = (Invariant) 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.
                 */
                finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) resp);
            } else {
                if (invariantService.hasPermissionsUpdate(invariantData, request)) {
                    invariantData.setIdName(id);
                    invariantData.setValue(value);
                    invariantData.setSort(sort);
                    invariantData.setDescription(description);
                    invariantData.setVeryShortDesc(veryShortDescField);
                    invariantData.setGp1(gp1);
                    invariantData.setGp2(gp2);
                    invariantData.setGp3(gp3);
                    invariantData.setGp4(gp4);
                    invariantData.setGp5(gp5);
                    invariantData.setGp6(gp6);
                    invariantData.setGp7(gp7);
                    invariantData.setGp8(gp8);
                    invariantData.setGp9(gp9);
                    ans = invariantService.update(oriId, oriValue, invariantData);
                    finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
                    if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                        /**
                         * Object updated. Adding Log entry.
                         */
                        ILogEventService logEventService = appContext.getBean(LogEventService.class);
                        logEventService.createForPrivateCalls("/UpdateInvariant2", "UPDATE", "Update Invariant : ['" + id + "']", request);
                    }
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Update").replace("%REASON%", "The Invariant is not Public!"));
                    ans.setResultMessage(msg);
                    finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
                }
            }
        }
    }
    /**
     * Formating and returning the json result.
     */
    jsonResponse.put("messageType", finalAnswer.getResultMessage().getMessage().getCodeString());
    jsonResponse.put("message", finalAnswer.getResultMessage().getDescription());
    response.getWriter().print(jsonResponse);
    response.getWriter().flush();
}
Also used : Invariant(org.cerberus.crud.entity.Invariant) PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) IInvariantService(org.cerberus.crud.service.IInvariantService) AnswerItem(org.cerberus.util.answer.AnswerItem) ServletException(javax.servlet.ServletException) JSONException(org.json.JSONException) IOException(java.io.IOException) CerberusException(org.cerberus.exception.CerberusException) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) ILogEventService(org.cerberus.crud.service.ILogEventService)

Example 10 with Invariant

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

the class DeleteInvariant 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");
    String id = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("idName"), "", charset);
    String value = request.getParameter("value");
    boolean userHasPermissions = request.isUserInRole("Administrator");
    /**
     * Checking all constrains before calling the services.
     */
    if (StringUtil.isNullOrEmpty(id)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Delete").replace("%REASON%", "Invariant name is missing!"));
        ans.setResultMessage(msg);
    } else if (!userHasPermissions) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Delete").replace("%REASON%", "You don't have the right to do that"));
        ans.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        IInvariantService invariantService = appContext.getBean(IInvariantService.class);
        Invariant invariantData = invariantService.convert(invariantService.readByKey(id, value));
        if (invariantService.hasPermissionsDelete(invariantData, request)) {
            ans = invariantService.delete(invariantData);
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                /**
                 * Object updated. Adding Log entry.
                 */
                ILogEventService logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/DeleteInvariant2", "DELETE", "Delete Invariant : ['" + id + "']", request);
            }
        } else {
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
            msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Delete").replace("%REASON%", "You don't have the right to do that."));
            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.toString());
    response.getWriter().flush();
}
Also used : Answer(org.cerberus.util.answer.Answer) Invariant(org.cerberus.crud.entity.Invariant) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) IInvariantService(org.cerberus.crud.service.IInvariantService) LogEventService(org.cerberus.crud.service.impl.LogEventService) ILogEventService(org.cerberus.crud.service.ILogEventService) ILogEventService(org.cerberus.crud.service.ILogEventService)

Aggregations

Invariant (org.cerberus.crud.entity.Invariant)28 AnswerList (org.cerberus.util.answer.AnswerList)18 IInvariantService (org.cerberus.crud.service.IInvariantService)15 JSONObject (org.json.JSONObject)15 ArrayList (java.util.ArrayList)13 List (java.util.List)11 MessageEvent (org.cerberus.engine.entity.MessageEvent)10 IFactoryInvariant (org.cerberus.crud.factory.IFactoryInvariant)8 JSONArray (org.json.JSONArray)8 ApplicationContext (org.springframework.context.ApplicationContext)8 Connection (java.sql.Connection)7 PreparedStatement (java.sql.PreparedStatement)7 ResultSet (java.sql.ResultSet)7 SQLException (java.sql.SQLException)7 AnswerItem (org.cerberus.util.answer.AnswerItem)7 JSONException (org.json.JSONException)7 HashMap (java.util.HashMap)6 CerberusException (org.cerberus.exception.CerberusException)5 PolicyFactory (org.owasp.html.PolicyFactory)4 IOException (java.io.IOException)3