Search in sources :

Example 16 with Invariant

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

the class CreateInvariant 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();
    String id = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("idName"), "", charset);
    String value = request.getParameter("value");
    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;
    }
    /**
     * 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%", "Create").replace("%REASON%", "Invariant name is missing!"));
        ans.setResultMessage(msg);
    } else if (sort_error) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Create").replace("%REASON%", "Could not manage to convert sort to an integer value!"));
        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);
        IFactoryInvariant factoryInvariant = appContext.getBean(IFactoryInvariant.class);
        Invariant invariantData = factoryInvariant.create(id, value, sort, description, veryShortDescField, gp1, gp2, gp3, gp4, gp5, gp6, gp7, gp8, gp9);
        if (invariantService.hasPermissionsCreate(invariantData, request)) {
            ans = invariantService.create(invariantData);
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                /**
                 * Object updated. Adding Log entry.
                 */
                ILogEventService logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/CreateInvariant2", "CREATE", "Create Invariant : ['" + id + "']", request);
            }
        } else {
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
            msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Create").replace("%REASON%", "You are not allowed to do that or invariant is not public."));
            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 : Invariant(org.cerberus.crud.entity.Invariant) IFactoryInvariant(org.cerberus.crud.factory.IFactoryInvariant) 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) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) CerberusException(org.cerberus.exception.CerberusException) JSONException(org.json.JSONException) IFactoryInvariant(org.cerberus.crud.factory.IFactoryInvariant) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) ILogEventService(org.cerberus.crud.service.ILogEventService)

Example 17 with Invariant

use of org.cerberus.crud.entity.Invariant 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)

Example 18 with Invariant

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

the class ReadInvariant method findInvariantList.

private AnswerItem findInvariantList(ApplicationContext appContext, String access, HttpServletRequest request, HttpServletResponse response) throws JSONException {
    AnswerItem item = new AnswerItem();
    JSONObject jsonResponse = new JSONObject();
    invariantService = appContext.getBean(IInvariantService.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"), "idname,value,sort,description,VeryShortDesc, gp1,gp2,gp3");
    String[] columnToSort = sColumns.split(",");
    String columnName = columnToSort[columnToSortParameter];
    String sort = ParameterParserUtil.parseStringParam(request.getParameter("sSortDir_0"), "asc");
    AnswerList answerService;
    List<String> individualLike = new ArrayList(Arrays.asList(ParameterParserUtil.parseStringParam(request.getParameter("sLike"), "").split(",")));
    Map<String, List<String>> individualSearch = new HashMap<String, List<String>>();
    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);
            }
        }
    }
    if ("PUBLIC".equals(access)) {
        answerService = invariantService.readByPublicByCriteria(startPosition, length, columnName, sort, searchParameter, individualSearch);
    } else if ("PRIVATE".equals(access)) {
        answerService = invariantService.readByPrivateByCriteria(startPosition, length, columnName, sort, searchParameter, individualSearch);
    } else {
        answerService = invariantService.readByCriteria(startPosition, length, columnName, sort, searchParameter, "");
    }
    JSONArray jsonArray = new JSONArray();
    // boolean userHasPermissions = request.isUserInRole("Integrator"); //TODO:need to chec
    if (answerService.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        // the service was able to perform the query, then we should get all values
        for (Invariant inv : (List<Invariant>) answerService.getDataList()) {
            jsonArray.put(convertInvariantToJSONObject(inv));
        }
    }
    // jsonResponse.put("hasPermissions", userHasPermissions);
    jsonResponse.put("contentTable", jsonArray);
    jsonResponse.put("iTotalRecords", answerService.getTotalRows());
    jsonResponse.put("iTotalDisplayRecords", answerService.getTotalRows());
    item.setItem(jsonResponse);
    item.setResultMessage(answerService.getResultMessage());
    return item;
}
Also used : Invariant(org.cerberus.crud.entity.Invariant) AnswerList(org.cerberus.util.answer.AnswerList) IInvariantService(org.cerberus.crud.service.IInvariantService) JSONArray(org.json.JSONArray) AnswerItem(org.cerberus.util.answer.AnswerItem) JSONObject(org.json.JSONObject) AnswerList(org.cerberus.util.answer.AnswerList)

Example 19 with Invariant

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

the class DuplicateTestCase method getCountryList.

private List<TestCaseCountry> getCountryList(String targetTest, String targetTestCase, HttpServletRequest request) throws CerberusException, JSONException, UnsupportedEncodingException {
    List<TestCaseCountry> countryList = new ArrayList();
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    IInvariantService invariantService = appContext.getBean(InvariantService.class);
    IFactoryTestCaseCountry testCaseCountryFactory = appContext.getBean(IFactoryTestCaseCountry.class);
    // TODO: handle if the response does not turn ok
    AnswerList answer = invariantService.readByIdname("COUNTRY");
    for (Invariant country : (List<Invariant>) answer.getDataList()) {
        String countrySelected = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter(country.getValue()), "");
        if ("".equals(countrySelected)) {
            countryList.add(testCaseCountryFactory.create(targetTest, targetTestCase, country.getValue()));
        }
    }
    return countryList;
}
Also used : Invariant(org.cerberus.crud.entity.Invariant) ApplicationContext(org.springframework.context.ApplicationContext) AnswerList(org.cerberus.util.answer.AnswerList) IInvariantService(org.cerberus.crud.service.IInvariantService) ArrayList(java.util.ArrayList) TestCaseCountry(org.cerberus.crud.entity.TestCaseCountry) IFactoryTestCaseCountry(org.cerberus.crud.factory.IFactoryTestCaseCountry) IFactoryTestCaseCountry(org.cerberus.crud.factory.IFactoryTestCaseCountry) AnswerList(org.cerberus.util.answer.AnswerList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 20 with Invariant

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

the class GetInvariantsForTest method doGet.

@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    IInvariantService invariantService = appContext.getBean(InvariantService.class);
    try {
        List<String> values = new ArrayList<String>();
        values.add("COUNTRY");
        values.add("RUNQA");
        values.add("RUNUAT");
        values.add("RUNPROD");
        values.add("PRIORITY");
        values.add("GROUP");
        values.add("TCSTATUS");
        values.add("TCACTIVE");
        values.add("BUILD");
        values.add("REVISION");
        JSONObject jsonResponse = new JSONObject();
        HashMap<String, List<String>> invariants = new HashMap<String, List<String>>();
        List<Invariant> l = invariantService.readByPrivateByCriteria(0, 0, "sort", "ASC", "%", "idname " + SqlUtil.getInSQLClause(values)).getDataList();
        for (Invariant myInvariant : l) {
            if (invariants.containsKey(myInvariant.getIdName())) {
                invariants.get(myInvariant.getIdName()).add(myInvariant.getValue());
            } else {
                List<String> list = new ArrayList<String>();
                list.add(myInvariant.getValue());
                invariants.put(myInvariant.getIdName(), list);
            }
        }
        l = invariantService.readByPublicByCriteria(0, 0, "sort", "ASC", "%", "idname " + SqlUtil.getInSQLClause(values)).getDataList();
        for (Invariant myInvariant : l) {
            if (invariants.containsKey(myInvariant.getIdName())) {
                invariants.get(myInvariant.getIdName()).add(myInvariant.getValue());
            } else {
                List<String> list = new ArrayList<String>();
                list.add(myInvariant.getValue());
                invariants.put(myInvariant.getIdName(), list);
            }
        }
        for (Map.Entry<String, List<String>> key : invariants.entrySet()) {
            JSONArray jSONArray = new JSONArray(key.getValue());
            jsonResponse.put(key.getKey(), jSONArray);
        }
        httpServletResponse.setContentType("application/json");
        httpServletResponse.getWriter().print(jsonResponse.toString());
    } catch (JSONException e) {
        LOG.warn(e);
        httpServletResponse.setContentType("text/html");
        httpServletResponse.getWriter().print(e.getMessage());
    }
}
Also used : Invariant(org.cerberus.crud.entity.Invariant) HashMap(java.util.HashMap) IInvariantService(org.cerberus.crud.service.IInvariantService) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

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