Search in sources :

Example 1 with Parameter

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

the class ParameterDAO method findAllParameter.

@Override
public List<Parameter> findAllParameter() throws CerberusException {
    boolean throwExep = true;
    List<Parameter> result = null;
    Parameter paramet;
    final String query = "SELECT * FROM parameter p ";
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            ResultSet resultSet = preStat.executeQuery();
            try {
                result = new ArrayList<Parameter>();
                while (resultSet.next()) {
                    String system = resultSet.getString("system");
                    String param = resultSet.getString("param");
                    String value = resultSet.getString("value");
                    String desc = resultSet.getString("description");
                    paramet = factoryParameter.create(system, param, value, desc);
                    result.add(paramet);
                    throwExep = false;
                }
            } catch (SQLException exception) {
                LOG.warn("Unable to execute query : " + exception.toString());
            } finally {
                resultSet.close();
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to execute query : " + exception.toString());
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.warn("Unable to execute query : " + exception.toString());
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
        }
    }
    if (throwExep) {
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.NO_DATA_FOUND);
        mes.setDescription(mes.getDescription() + " Parameter table empty.");
        throw new CerberusException(mes);
    }
    return result;
}
Also used : CerberusException(org.cerberus.exception.CerberusException) MessageGeneral(org.cerberus.engine.entity.MessageGeneral) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Parameter(org.cerberus.crud.entity.Parameter) IFactoryParameter(org.cerberus.crud.factory.IFactoryParameter) FactoryParameter(org.cerberus.crud.factory.impl.FactoryParameter) PreparedStatement(java.sql.PreparedStatement)

Example 2 with Parameter

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

the class ParameterDAO method findAllParameterWithSystem1.

@Override
public List<Parameter> findAllParameterWithSystem1(String mySystem, String mySystem1) throws CerberusException {
    boolean throwExep = true;
    List<Parameter> result = null;
    Parameter paramet;
    StringBuilder mySQL = new StringBuilder();
    mySQL.append("SELECT par.param param, par.`value` valC, par2.`value` valS, par2.description FROM parameter par ");
    mySQL.append("LEFT OUTER JOIN ( SELECT * from parameter par2 WHERE par2.system= ? ) as par2 ON par.param = par.param ");
    mySQL.append(" WHERE par.system= ?; ");
    final String query = mySQL.toString();
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        preStat.setString(1, mySystem1);
        preStat.setString(1, mySystem);
        try {
            ResultSet resultSet = preStat.executeQuery();
            try {
                result = new ArrayList<Parameter>();
                while (resultSet.next()) {
                    String param = resultSet.getString("param");
                    String valueC = resultSet.getString("valC");
                    String valueS = resultSet.getString("valS");
                    String desc = resultSet.getString("description");
                    paramet = factoryParameter.create(param, "", valueC, desc, mySystem, valueS);
                    result.add(paramet);
                    throwExep = false;
                }
            } catch (SQLException exception) {
                LOG.warn("Unable to execute query : " + exception.toString());
            } finally {
                resultSet.close();
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to execute query : " + exception.toString());
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.warn("Unable to execute query : " + exception.toString());
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
        }
    }
    if (throwExep) {
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.NO_DATA_FOUND);
        mes.setDescription(mes.getDescription() + " Parameter table empty.");
        throw new CerberusException(mes);
    }
    return result;
}
Also used : CerberusException(org.cerberus.exception.CerberusException) MessageGeneral(org.cerberus.engine.entity.MessageGeneral) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Parameter(org.cerberus.crud.entity.Parameter) IFactoryParameter(org.cerberus.crud.factory.IFactoryParameter) FactoryParameter(org.cerberus.crud.factory.impl.FactoryParameter) PreparedStatement(java.sql.PreparedStatement)

Example 3 with Parameter

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

the class TestDataLibDAO method uploadFile.

@Override
public Answer uploadFile(int id, FileItem file) {
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", "cerberus_testdatalibcsv_path Parameter not found");
    AnswerItem a = parameterService.readByKey("", "cerberus_testdatalibcsv_path");
    if (a.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        Parameter p = (Parameter) a.getItem();
        String uploadPath = p.getValue();
        File appDir = new File(uploadPath + File.separator + id);
        if (!appDir.exists()) {
            try {
                appDir.mkdirs();
            } catch (SecurityException se) {
                LOG.warn("Unable to create testdatalib csv dir: " + se.getMessage());
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", se.toString());
                a.setResultMessage(msg);
            }
        }
        if (a.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
            deleteFolder(appDir, false);
            File picture = new File(uploadPath + File.separator + id + File.separator + file.getName());
            try {
                file.write(picture);
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK).resolveDescription("DESCRIPTION", "TestDataLib CSV file uploaded");
                msg.setDescription(msg.getDescription().replace("%ITEM%", "testDatalib CSV").replace("%OPERATION%", "Upload"));
            } catch (Exception e) {
                LOG.warn("Unable to upload testdatalib csv file: " + e.getMessage());
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", e.toString());
            }
        }
    } else {
        LOG.warn("cerberus_testdatalibCSV_path Parameter not found");
    }
    a.setResultMessage(msg);
    return a;
}
Also used : MessageEvent(org.cerberus.engine.entity.MessageEvent) Parameter(org.cerberus.crud.entity.Parameter) AnswerItem(org.cerberus.util.answer.AnswerItem) File(java.io.File) SQLException(java.sql.SQLException)

Example 4 with Parameter

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

the class AndroidAppiumService method swipe.

@Override
public MessageEvent swipe(Session session, SwipeAction action) {
    try {
        SwipeAction.Direction direction = this.getDirectionForSwipe(session, action);
        // Get the parametrized swipe duration
        Parameter duration = parameters.findParameterByKey(CERBERUS_APPIUM_SWIPE_DURATION_PARAMETER, "");
        // Do the swipe thanks to the Appium driver
        TouchAction dragNDrop = new TouchAction(session.getAppiumDriver()).press(direction.getX1(), direction.getY1()).waitAction(Duration.ofMillis(duration == null ? DEFAULT_CERBERUS_APPIUM_SWIPE_DURATION : Integer.parseInt(duration.getValue()))).moveTo(direction.getX2(), direction.getY2()).release();
        dragNDrop.perform();
        return new MessageEvent(MessageEventEnum.ACTION_SUCCESS_SWIPE).resolveDescription("DIRECTION", action.getActionType().name());
    } catch (IllegalArgumentException e) {
        return new MessageEvent(MessageEventEnum.ACTION_FAILED_SWIPE).resolveDescription("DIRECTION", action.getActionType().name()).resolveDescription("REASON", "Unknown direction");
    } catch (Exception e) {
        LOG.warn("Unable to swipe screen due to " + e.getMessage(), e);
        return new MessageEvent(MessageEventEnum.ACTION_FAILED_SWIPE).resolveDescription("DIRECTION", action.getActionType().name()).resolveDescription("REASON", e.getMessage());
    }
}
Also used : MessageEvent(org.cerberus.engine.entity.MessageEvent) SwipeAction(org.cerberus.engine.entity.SwipeAction) Parameter(org.cerberus.crud.entity.Parameter) TouchAction(io.appium.java_client.TouchAction)

Example 5 with Parameter

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

the class UpdateParameter 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();
    ILogEventService logEventService;
    String id = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("id"), "", charset);
    String value = ParameterParserUtil.parseStringParam(request.getParameter("value"), "");
    String system = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("system"), "", charset);
    String system1value = ParameterParserUtil.parseStringParam(request.getParameter("system1Value"), null);
    String system1 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("system1"), null, charset);
    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) || StringUtil.isNullOrEmpty(system1)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Parameter").replace("%OPERATION%", "Update").replace("%REASON%", "Parameter id or system1 is missing!"));
        finalAnswer.setResultMessage(msg);
    } else if (!userHasPermissions) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Parameter").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());
        IParameterService parameterService = appContext.getBean(IParameterService.class);
        FactoryParameter factoryparameter = appContext.getBean(FactoryParameter.class);
        Parameter para = factoryparameter.create(system, id, value, "");
        ans = parameterService.save(para);
        if (!ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && !ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED.getCode())) {
            /**
             * Object could not be found. We stop here and report the error.
             */
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
        } else {
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/UpdateParameter", "UPDATE", "Update Parameter : ['" + id + "','" + system + "']", request);
            }
            if (system1 != null && system1value != null) {
                Parameter para1 = factoryparameter.create(system1, id, system1value, "");
                ans = parameterService.save(para1);
                if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                    /**
                     * Object updated. Adding Log entry.
                     */
                    logEventService = appContext.getBean(LogEventService.class);
                    logEventService.createForPrivateCalls("/UpdateParameter", "UPDATE", "Update Parameter : ['" + id + "','" + system1 + "']", request);
                }
            }
        }
    }
    /**
     * 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 : FactoryParameter(org.cerberus.crud.factory.impl.FactoryParameter) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) ILogEventService(org.cerberus.crud.service.ILogEventService) Parameter(org.cerberus.crud.entity.Parameter) FactoryParameter(org.cerberus.crud.factory.impl.FactoryParameter) IParameterService(org.cerberus.crud.service.IParameterService)

Aggregations

Parameter (org.cerberus.crud.entity.Parameter)22 CerberusException (org.cerberus.exception.CerberusException)12 MessageEvent (org.cerberus.engine.entity.MessageEvent)9 SQLException (java.sql.SQLException)8 AnswerItem (org.cerberus.util.answer.AnswerItem)8 IFactoryParameter (org.cerberus.crud.factory.IFactoryParameter)7 FactoryParameter (org.cerberus.crud.factory.impl.FactoryParameter)7 Connection (java.sql.Connection)6 PreparedStatement (java.sql.PreparedStatement)6 ResultSet (java.sql.ResultSet)6 IParameterService (org.cerberus.crud.service.IParameterService)4 JSONObject (org.json.JSONObject)4 File (java.io.File)3 MessageGeneral (org.cerberus.engine.entity.MessageGeneral)3 AnswerList (org.cerberus.util.answer.AnswerList)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 ParameterService (org.cerberus.crud.service.impl.ParameterService)2