Search in sources :

Example 51 with Answer

use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.

the class TestDataLibDataDAO method delete.

@Override
public Answer delete(TestDataLibData object) {
    MessageEvent msg = null;
    final String query = "DELETE FROM testdatalibdata WHERE `testdatalibdataid`= ? ";
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query.toString());
        LOG.debug("SQL.param.TestDataLibDataID : " + object.getTestDataLibDataID());
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            preStat.setInt(1, object.getTestDataLibDataID());
            preStat.executeUpdate();
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
            msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "DELETE"));
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to close connection : " + exception.toString());
        }
    }
    return new Answer(msg);
}
Also used : Answer(org.cerberus.util.answer.Answer) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 52 with Answer

use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.

the class TestDataLibDataDAO method create.

@Override
public Answer create(TestDataLibData object) {
    MessageEvent msg = null;
    StringBuilder query = new StringBuilder();
    query.append("INSERT INTO testdatalibdata (`TestDataLibID`, `subData`, `value`, `column`, `parsinganswer`, `columnPosition`,`description`) ");
    query.append("VALUES (?,?,?,?,?,?,?)");
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query.toString());
        LOG.debug("SQL.param.TestDataLibID : " + object.getTestDataLibID());
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        try {
            preStat.setInt(1, object.getTestDataLibID());
            preStat.setString(2, ParameterParserUtil.returnEmptyStringIfNull(object.getSubData()));
            preStat.setString(3, ParameterParserUtil.returnEmptyStringIfNull(object.getValue()));
            preStat.setString(4, ParameterParserUtil.returnEmptyStringIfNull(object.getColumn()));
            preStat.setString(5, ParameterParserUtil.returnEmptyStringIfNull(object.getParsingAnswer()));
            preStat.setString(6, ParameterParserUtil.returnEmptyStringIfNull(object.getColumnPosition()));
            preStat.setString(7, ParameterParserUtil.returnEmptyStringIfNull(object.getDescription()));
            preStat.executeUpdate();
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
            msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "INSERT"));
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
            if (exception.getSQLState().equals(SQL_DUPLICATED_CODE)) {
                // 23000 is the sql state for duplicate entries
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_DUPLICATE);
                msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "INSERT").replace("%REASON%", exception.toString()));
            } else {
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
                msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
            }
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException exception) {
            LOG.error("Unable to close connection : " + exception.toString());
        }
    }
    return new Answer(msg);
}
Also used : Answer(org.cerberus.util.answer.Answer) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 53 with Answer

use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.

the class ExportServiceFactory method exportToXLS.

private Answer exportToXLS() {
    Answer ans = new Answer();
    // Blank workbook
    Workbook workbook = new XSSFWorkbook();
    if (data.equals("TestCaseWithExecution")) {
        createReportByTagExport(workbook);
    }
    String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new java.util.Date());
    try (FileOutputStream outputStream = new FileOutputStream(this.fileName + "_" + timeStamp + type.getFileExtension())) {
        workbook.write(outputStream);
    } catch (FileNotFoundException ex) {
        LOG.warn(ex);
    } catch (IOException e) {
        LOG.warn(e.toString());
    } finally {
        try {
            workbook.close();
        } catch (IOException e) {
            LOG.warn(e.toString());
        }
    }
    // each country will be a page in the xls file
    return ans;
}
Also used : Answer(org.cerberus.util.answer.Answer) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 54 with Answer

use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.

the class CreateBuildRevisionInvariant 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
 * @throws org.cerberus.exception.CerberusException
 * @throws org.json.JSONException
 */
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");
    // 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
    Integer seq = -1;
    boolean seq_error = false;
    try {
        if (request.getParameter("seq") != null && !request.getParameter("seq").equals("")) {
            seq = Integer.valueOf(policy.sanitize(request.getParameter("seq")));
        }
    } catch (Exception ex) {
        seq_error = true;
    }
    Integer level = -1;
    boolean level_error = false;
    try {
        if (request.getParameter("level") != null && !request.getParameter("level").equals("")) {
            level = Integer.valueOf(policy.sanitize(request.getParameter("level")));
        }
    } catch (Exception ex) {
        level_error = true;
    }
    // Parameter that needs to be secured --> We SECURE+DECODE them
    String system = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("system"), "", charset);
    String versionName = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("versionname"), "", charset);
    /**
     * Checking all constrains before calling the services.
     */
    if (StringUtil.isNullOrEmpty(system)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "BuildRevisionInvariant").replace("%OPERATION%", "Create").replace("%REASON%", "System name is missing!"));
        ans.setResultMessage(msg);
    } else if (level_error) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "BuildRevisionInvariant").replace("%OPERATION%", "Create").replace("%REASON%", "Could not manage to convert level to an integer value!"));
        ans.setResultMessage(msg);
    } else if (seq_error) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "BuildRevisionInvariant").replace("%OPERATION%", "Create").replace("%REASON%", "Could not manage to convert sequence to an integer value!"));
        ans.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        IBuildRevisionInvariantService buildRevisionInvariantService = appContext.getBean(IBuildRevisionInvariantService.class);
        IFactoryBuildRevisionInvariant factoryBuildRevisionInvariant = appContext.getBean(IFactoryBuildRevisionInvariant.class);
        BuildRevisionInvariant buildRevisionInvariantData = factoryBuildRevisionInvariant.create(system, level, seq, versionName);
        ans = buildRevisionInvariantService.create(buildRevisionInvariantData);
        if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
            /**
             * Object created. Adding Log entry.
             */
            ILogEventService logEventService = appContext.getBean(LogEventService.class);
            logEventService.createForPrivateCalls("/CreateBuildRevisionInvariant", "CREATE", "Create BuildRevisionInvariant : ['" + system + "'|'" + level + "'|'" + seq + "']", 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) IFactoryBuildRevisionInvariant(org.cerberus.crud.factory.IFactoryBuildRevisionInvariant) ServletException(javax.servlet.ServletException) JSONException(org.json.JSONException) IOException(java.io.IOException) CerberusException(org.cerberus.exception.CerberusException) IBuildRevisionInvariantService(org.cerberus.crud.service.IBuildRevisionInvariantService) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) IFactoryBuildRevisionInvariant(org.cerberus.crud.factory.IFactoryBuildRevisionInvariant) BuildRevisionInvariant(org.cerberus.crud.entity.BuildRevisionInvariant) ILogEventService(org.cerberus.crud.service.ILogEventService)

Example 55 with Answer

use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.

the class UpdateBuildRevisionInvariant 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");
    // 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
    Integer seq = -1;
    boolean seq_error = false;
    try {
        if (request.getParameter("seq") != null && !request.getParameter("seq").equals("")) {
            seq = Integer.valueOf(policy.sanitize(request.getParameter("seq")));
        }
    } catch (Exception ex) {
        seq_error = true;
    }
    Integer level = -1;
    boolean level_error = false;
    try {
        if (request.getParameter("level") != null && !request.getParameter("level").equals("")) {
            level = Integer.valueOf(policy.sanitize(request.getParameter("level")));
        }
    } catch (Exception ex) {
        level_error = true;
    }
    // Parameter that needs to be secured --> We SECURE+DECODE them
    String system = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("system"), "", charset);
    String versionName = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("versionname"), "", charset);
    /**
     * Checking all constrains before calling the services.
     */
    if (StringUtil.isNullOrEmpty(system)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "BuildRevisionInvariant").replace("%OPERATION%", "Update").replace("%REASON%", "System name is missing!"));
        ans.setResultMessage(msg);
    } else if (level_error) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "BuildRevisionInvariant").replace("%OPERATION%", "Update").replace("%REASON%", "Could not manage to convert level to an integer value!"));
        ans.setResultMessage(msg);
    } else if (seq_error) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "BuildRevisionInvariant").replace("%OPERATION%", "Update").replace("%REASON%", "Could not manage to convert sequence to an integer value!"));
        ans.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        IBuildRevisionInvariantService buildRevisionInvariantService = appContext.getBean(IBuildRevisionInvariantService.class);
        AnswerItem resp = buildRevisionInvariantService.readByKey(system, level, seq);
        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%", "BuildRevisionInvariant").replace("%OPERATION%", "Update").replace("%REASON%", "BuildRevisionInvariant does not exist."));
            ans.setResultMessage(msg);
        } else {
            /**
             * The service was able to perform the query and confirm the
             * object exist, then we can update it.
             */
            BuildRevisionInvariant buildRevisionInvariantData = (BuildRevisionInvariant) resp.getItem();
            buildRevisionInvariantData.setSystem(system);
            buildRevisionInvariantData.setLevel(level);
            buildRevisionInvariantData.setSeq(seq);
            buildRevisionInvariantData.setVersionName(versionName);
            ans = buildRevisionInvariantService.update(buildRevisionInvariantData.getSystem(), buildRevisionInvariantData.getLevel(), buildRevisionInvariantData.getSeq(), buildRevisionInvariantData);
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                /**
                 * Update was successful. Adding Log entry.
                 */
                ILogEventService logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/UpdateBuildRevisionInvariant", "UPDATE", "Updated BuildRevisionInvariant : ['" + system + "'|'" + level + "'|'" + seq + "']", 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) ServletException(javax.servlet.ServletException) JSONException(org.json.JSONException) IOException(java.io.IOException) CerberusException(org.cerberus.exception.CerberusException) IBuildRevisionInvariantService(org.cerberus.crud.service.IBuildRevisionInvariantService) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) BuildRevisionInvariant(org.cerberus.crud.entity.BuildRevisionInvariant) ILogEventService(org.cerberus.crud.service.ILogEventService)

Aggregations

Answer (org.cerberus.util.answer.Answer)241 MessageEvent (org.cerberus.engine.entity.MessageEvent)227 Connection (java.sql.Connection)127 PreparedStatement (java.sql.PreparedStatement)127 SQLException (java.sql.SQLException)127 ApplicationContext (org.springframework.context.ApplicationContext)77 JSONObject (org.json.JSONObject)75 ILogEventService (org.cerberus.crud.service.ILogEventService)74 PolicyFactory (org.owasp.html.PolicyFactory)60 AnswerItem (org.cerberus.util.answer.AnswerItem)53 CerberusException (org.cerberus.exception.CerberusException)45 ArrayList (java.util.ArrayList)35 JSONException (org.json.JSONException)26 IOException (java.io.IOException)23 ServletException (javax.servlet.ServletException)19 JSONArray (org.json.JSONArray)15 LogEventService (org.cerberus.crud.service.impl.LogEventService)13 TestCase (org.cerberus.crud.entity.TestCase)10 ITestCaseService (org.cerberus.crud.service.ITestCaseService)10 CountryEnvParam (org.cerberus.crud.entity.CountryEnvParam)8