Search in sources :

Example 11 with Tag

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

the class TagDAO method readByKey.

@Override
public AnswerItem<Tag> readByKey(String tag) {
    AnswerItem ans = new AnswerItem();
    Tag result = null;
    final String query = "SELECT * FROM `tag` tag WHERE `tag` = ?";
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
        LOG.debug("SQL.param.tag : " + tag);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            preStat.setString(1, tag);
            ResultSet resultSet = preStat.executeQuery();
            try {
                if (resultSet.first()) {
                    result = loadFromResultSet(resultSet);
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                    ans.setItem(result);
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                }
            } 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 {
                resultSet.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 {
            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());
        }
    }
    // sets the message
    ans.setResultMessage(msg);
    return ans;
}
Also used : SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Tag(org.cerberus.crud.entity.Tag) IFactoryTag(org.cerberus.crud.factory.IFactoryTag) FactoryTag(org.cerberus.crud.factory.impl.FactoryTag) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 12 with Tag

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

the class TagDAO method readByKeyTech.

@Override
public AnswerItem<Tag> readByKeyTech(long id) {
    AnswerItem ans = new AnswerItem();
    Tag result = null;
    final String query = "SELECT * FROM `tag` tag WHERE `id` = ?";
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
        LOG.debug("SQL.param.id : " + id);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            preStat.setLong(1, id);
            ResultSet resultSet = preStat.executeQuery();
            try {
                if (resultSet.first()) {
                    result = loadFromResultSet(resultSet);
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                    ans.setItem(result);
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                }
            } 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 {
                resultSet.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 {
            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());
        }
    }
    // sets the message
    ans.setResultMessage(msg);
    return ans;
}
Also used : SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Tag(org.cerberus.crud.entity.Tag) IFactoryTag(org.cerberus.crud.factory.IFactoryTag) FactoryTag(org.cerberus.crud.factory.impl.FactoryTag) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 13 with Tag

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

the class ResultCIV003 method processRequest.

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
    // Calling Servlet Transversal Util.
    ServletUtil.servletStart(request);
    /**
     * Adding Log entry.
     */
    ILogEventService logEventService = appContext.getBean(ILogEventService.class);
    logEventService.createForPublicCalls("/ResultCIV003", "CALL", "ResultCIV003 called : " + request.getRequestURL(), request);
    try {
        JSONObject jsonResponse = new JSONObject();
        String tag = policy.sanitize(request.getParameter("tag"));
        String campaign = policy.sanitize(request.getParameter("campaign"));
        String outputFormat = policy.sanitize(request.getParameter("outputformat"));
        String helpMessage = "This servlet is used to provide various execution counters as well as a global OK or KO status based on the number and status of the execution done on a specific tag. \n" + "The number of executions are ponderated by parameters by priority from cerberus_ci_okcoefprio1 to cerberus_ci_okcoefprio4. \n" + "Formula used is the following : \n" + "Nb Exe Prio 1 testcases * cerberus_ci_okcoefprio1 + Nb Exe Prio 2 testcases * cerberus_ci_okcoefprio2 + " + "Nb Exe Prio 3 testcases * cerberus_ci_okcoefprio3 + Nb Exe Prio 4 testcases * cerberus_ci_okcoefprio4.\n" + "If no executions are found, the result is KO.\n" + "With at least 1 execution, if result is < 1 then global servlet result is OK. If not, it is KO.\n" + "All execution needs to have a status equal to KO, FA, NA, PE or NE.\n" + "If at least 1 PE or 1 NE if found, global status will be PE\n" + "Output format is json by default, or SVG if outputFormat=svg is defined\n" + "Parameter list :\n" + "- tag : Execution Tag to filter the test cases execution. [" + tag + "]\n" + "- campaign : If you feed the campaign, the lastest Tag from that campaign will be selected. [" + campaign + "]\n" + "- outputformat : ['text','json', 'svg']. Output format of the result. [" + outputFormat + "]\n";
        jsonResponse.put("helpMessage", helpMessage);
        boolean error = false;
        String error_message = "";
        // Checking the parameter validity. Tag is a mandatory parameter
        if (StringUtil.isNullOrEmpty(tag)) {
            if (!StringUtil.isNullOrEmpty(campaign)) {
                ITagService tagService = appContext.getBean(ITagService.class);
                List<Tag> myList;
                AnswerList myAnswerList = tagService.readByVariousByCriteria(campaign, 0, 1, "id", "desc", null, null);
                if (myAnswerList.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                    // the service was able to perform the query, then we should get all values
                    for (Tag tagCur : (List<Tag>) myAnswerList.getDataList()) {
                        tag = tagCur.getTag();
                    }
                }
            }
        }
        if (StringUtil.isNullOrEmpty(tag)) {
            error_message += "Error - Either specify a tag or specify a campaign to get the latest tag from that campaign.";
            error = true;
        }
        if (!error) {
            ICIService ciService = appContext.getBean(ICIService.class);
            jsonResponse = ciService.getCIResult(tag);
            // Log the result with calculation detail.
            logEventService.createForPublicCalls("/ResultCIV003", "CALLRESULT", "ResultCIV003 calculated for tag " + tag + " result [" + jsonResponse.getString("result") + "]", request);
        } else {
            jsonResponse.put("messageType", "KO");
            jsonResponse.put("message", error_message);
        }
        generateResponse(response, outputFormat, jsonResponse, error);
    } 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 : AnswerList(org.cerberus.util.answer.AnswerList) PolicyFactory(org.owasp.html.PolicyFactory) JSONException(org.json.JSONException) ICIService(org.cerberus.service.ciresult.ICIService) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) ITagService(org.cerberus.crud.service.ITagService) ILogEventService(org.cerberus.crud.service.ILogEventService) AnswerList(org.cerberus.util.answer.AnswerList) List(java.util.List) Tag(org.cerberus.crud.entity.Tag)

Aggregations

Tag (org.cerberus.crud.entity.Tag)13 AnswerItem (org.cerberus.util.answer.AnswerItem)8 IFactoryTag (org.cerberus.crud.factory.IFactoryTag)6 ITagService (org.cerberus.crud.service.ITagService)5 MessageEvent (org.cerberus.engine.entity.MessageEvent)5 JSONObject (org.json.JSONObject)5 ArrayList (java.util.ArrayList)4 AnswerList (org.cerberus.util.answer.AnswerList)4 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 List (java.util.List)3 FactoryTag (org.cerberus.crud.factory.impl.FactoryTag)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TestCaseExecution (org.cerberus.crud.entity.TestCaseExecution)2 CerberusException (org.cerberus.exception.CerberusException)2 JSONException (org.json.JSONException)2 IOException (java.io.IOException)1