Search in sources :

Example 21 with TestCaseCountry

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

the class ReadTestCase method findTestCaseByTest.

// </editor-fold>
private AnswerItem findTestCaseByTest(String system, String test, ApplicationContext appContext, HttpServletRequest request) throws JSONException {
    AnswerItem answer = new AnswerItem();
    JSONObject object = new JSONObject();
    testCaseService = appContext.getBean(ITestCaseService.class);
    testCaseCountryService = appContext.getBean(ITestCaseCountryService.class);
    testCaseLabelService = appContext.getBean(ITestCaseLabelService.class);
    int startPosition = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayStart"), "0"));
    int length = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayLength"), "0"));
    String searchParameter = ParameterParserUtil.parseStringParam(request.getParameter("sSearch"), "");
    String sColumns = ParameterParserUtil.parseStringParam(request.getParameter("sColumns"), "tec.test,tec.testcase,tec.application,project,ticket,description,behaviororvalueexpected,readonly,bugtrackernewurl,deploytype,mavengroupid");
    String[] columnToSort = sColumns.split(",");
    List<String> individualLike = new ArrayList(Arrays.asList(ParameterParserUtil.parseStringParam(request.getParameter("sLike"), "").split(",")));
    // Get Sorting information
    int numberOfColumnToSort = Integer.parseInt(ParameterParserUtil.parseStringParam(request.getParameter("iSortingCols"), "1"));
    int columnToSortParameter = 0;
    String sort = "asc";
    StringBuilder sortInformation = new StringBuilder();
    for (int c = 0; c < numberOfColumnToSort; c++) {
        columnToSortParameter = Integer.parseInt(ParameterParserUtil.parseStringParam(request.getParameter("iSortCol_" + c), "0"));
        sort = ParameterParserUtil.parseStringParam(request.getParameter("sSortDir_" + c), "asc");
        String columnName = columnToSort[columnToSortParameter];
        sortInformation.append(columnName).append(" ").append(sort);
        if (c != numberOfColumnToSort - 1) {
            sortInformation.append(" , ");
        }
    }
    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);
            }
        }
    }
    AnswerList testCaseList = testCaseService.readByTestByCriteria(system, test, startPosition, length, sortInformation.toString(), searchParameter, individualSearch);
    AnswerList testCaseCountryList = testCaseCountryService.readByTestTestCase(system, test, null);
    /**
     * Find the list of labels
     */
    AnswerList testCaseLabelList = testCaseLabelService.readByTestTestCase(test, null);
    LinkedHashMap<String, JSONObject> testCaseWithCountry = new LinkedHashMap();
    for (TestCaseCountry country : (List<TestCaseCountry>) testCaseCountryList.getDataList()) {
        String key = country.getTest() + "_" + country.getTestCase();
        if (testCaseWithCountry.containsKey(key)) {
            testCaseWithCountry.get(key).put(country.getCountry(), country.getCountry());
        } else {
            testCaseWithCountry.put(key, new JSONObject().put(country.getCountry(), country.getCountry()));
        }
    }
    /**
     * Iterate on the label retrieved and generate HashMap based on the key
     * Test_TestCase
     */
    LinkedHashMap<String, JSONArray> testCaseWithLabel = new LinkedHashMap();
    for (TestCaseLabel label : (List<TestCaseLabel>) testCaseLabelList.getDataList()) {
        String key = label.getTest() + "_" + label.getTestcase();
        if (testCaseWithLabel.containsKey(key)) {
            JSONObject jo = new JSONObject().put("name", label.getLabel().getLabel()).put("color", label.getLabel().getColor()).put("description", label.getLabel().getDescription());
            testCaseWithLabel.get(key).put(jo);
        } else {
            JSONObject jo = new JSONObject().put("name", label.getLabel().getLabel()).put("color", label.getLabel().getColor()).put("description", label.getLabel().getDescription());
            testCaseWithLabel.put(key, new JSONArray().put(jo));
        }
    }
    JSONArray jsonArray = new JSONArray();
    if (testCaseList.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        // the service was able to perform the query, then we should get all values
        for (TestCase testCase : (List<TestCase>) testCaseList.getDataList()) {
            String key = testCase.getTest() + "_" + testCase.getTestCase();
            JSONObject value = convertToJSONObject(testCase);
            value.put("hasPermissionsDelete", testCaseService.hasPermissionsDelete(testCase, request));
            value.put("hasPermissionsUpdate", testCaseService.hasPermissionsUpdate(testCase, request));
            value.put("hasPermissionsCreate", testCaseService.hasPermissionsCreate(testCase, request));
            value.put("countryList", testCaseWithCountry.get(key));
            value.put("labels", testCaseWithLabel.get(key));
            jsonArray.put(value);
        }
    }
    // object.put("hasPermissions", testCaseService.hasPermissions(request));
    object.put("hasPermissionsCreate", testCaseService.hasPermissionsCreate(null, request));
    object.put("contentTable", jsonArray);
    object.put("iTotalRecords", testCaseList.getTotalRows());
    object.put("iTotalDisplayRecords", testCaseList.getTotalRows());
    answer.setItem(object);
    answer.setResultMessage(testCaseList.getResultMessage());
    return answer;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) AnswerItem(org.cerberus.util.answer.AnswerItem) ITestCaseCountryService(org.cerberus.crud.service.ITestCaseCountryService) LinkedHashMap(java.util.LinkedHashMap) JSONObject(org.json.JSONObject) TestCase(org.cerberus.crud.entity.TestCase) ITestCaseService(org.cerberus.crud.service.ITestCaseService) TestCaseCountry(org.cerberus.crud.entity.TestCaseCountry) AnswerList(org.cerberus.util.answer.AnswerList) ArrayList(java.util.ArrayList) List(java.util.List) ITestCaseLabelService(org.cerberus.crud.service.ITestCaseLabelService)

Example 22 with TestCaseCountry

use of org.cerberus.crud.entity.TestCaseCountry 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 23 with TestCaseCountry

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

the class TestCaseCountryDAO method readByVarious1.

@Override
public AnswerList readByVarious1(String system, String test, String testCase) {
    AnswerList answer = new AnswerList();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    List<TestCaseCountry> testCaseCountryList = new ArrayList<TestCaseCountry>();
    StringBuilder query = new StringBuilder();
    query.append("SELECT * FROM testcasecountry tcc ");
    if (!Strings.isNullOrEmpty(system)) {
        query.append(" LEFT OUTER JOIN testcase tc on tc.test = tcc.test and tc.testcase = tcc.testcase  ");
        query.append(" LEFT OUTER JOIN application app on app.application = tc.application ");
    }
    query.append(" WHERE 1=1");
    if (!Strings.isNullOrEmpty(system)) {
        query.append(" AND app.`system` = ?");
    }
    if (!Strings.isNullOrEmpty(test)) {
        query.append(" AND tcc.test = ?");
    }
    if (testCase != null) {
        query.append(" AND tcc.testcase = ?");
    }
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
        LOG.debug("SQL.param.test : " + test);
        LOG.debug("SQL.param.testCase : " + testCase);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        try {
            int i = 1;
            if (!Strings.isNullOrEmpty(system)) {
                preStat.setString(i++, system);
            }
            if (!Strings.isNullOrEmpty(test)) {
                preStat.setString(i++, test);
            }
            if (testCase != null) {
                preStat.setString(i++, testCase);
            }
            ResultSet resultSet = preStat.executeQuery();
            try {
                // gets the data
                while (resultSet.next()) {
                    testCaseCountryList.add(this.loadFromResultSet(resultSet));
                }
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                answer = new AnswerList(testCaseCountryList, testCaseCountryList.size());
            } 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%", "Unable to retrieve the list of entries!"));
            } finally {
                if (resultSet != null) {
                    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%", "Unable to retrieve the list of entries!"));
        } finally {
            if (preStat != null) {
                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%", "Unable to retrieve the list of entries!"));
    } finally {
        try {
            if (!this.databaseSpring.isOnTransaction()) {
                if (connection != null) {
                    connection.close();
                }
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to close connection : " + exception.toString());
        }
    }
    answer.setResultMessage(msg);
    return answer;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) IFactoryTestCaseCountry(org.cerberus.crud.factory.IFactoryTestCaseCountry) TestCaseCountry(org.cerberus.crud.entity.TestCaseCountry) PreparedStatement(java.sql.PreparedStatement)

Example 24 with TestCaseCountry

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

the class TestCaseCountryDAO method findTestCaseCountryByKey.

@Override
public TestCaseCountry findTestCaseCountryByKey(String test, String testCase, String country) throws CerberusException {
    final String query = "SELECT * FROM testcasecountry tcc WHERE test = ? AND testcase = ? AND country = ? ";
    TestCaseCountry result = null;
    boolean throwException = false;
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            preStat.setString(1, test);
            preStat.setString(2, testCase);
            preStat.setString(3, country);
            ResultSet resultSet = preStat.executeQuery();
            try {
                if (resultSet.first()) {
                    result = factoryTestCaseCountry.create(test, testCase, country);
                } else {
                    throwException = true;
                }
            } 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 (throwException) {
        throw new CerberusException(new MessageGeneral(MessageGeneralEnum.NO_DATA_FOUND));
    }
    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) IFactoryTestCaseCountry(org.cerberus.crud.factory.IFactoryTestCaseCountry) TestCaseCountry(org.cerberus.crud.entity.TestCaseCountry) PreparedStatement(java.sql.PreparedStatement)

Example 25 with TestCaseCountry

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

the class TestCaseCountryDAO method readByKey.

@Override
public AnswerItem readByKey(String test, String testCase, String country) {
    AnswerItem answer = new AnswerItem();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    TestCaseCountry result = new TestCaseCountry();
    final String query = "SELECT * FROM testcasecountry tcc WHERE test = ? AND testcase = ? AND country = ?";
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            preStat.setString(1, test);
            preStat.setString(2, testCase);
            preStat.setString(3, country);
            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"));
                    answer.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%", "Unable to retrieve the entry!"));
        } finally {
            if (preStat != null) {
                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%", "Unable to retrieve the list of entries!"));
    } finally {
        try {
            if (!this.databaseSpring.isOnTransaction()) {
                if (connection != null) {
                    connection.close();
                }
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to close connection : " + exception.toString());
        }
    }
    answer.setResultMessage(msg);
    return answer;
}
Also used : SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) IFactoryTestCaseCountry(org.cerberus.crud.factory.IFactoryTestCaseCountry) TestCaseCountry(org.cerberus.crud.entity.TestCaseCountry) PreparedStatement(java.sql.PreparedStatement) AnswerItem(org.cerberus.util.answer.AnswerItem)

Aggregations

TestCaseCountry (org.cerberus.crud.entity.TestCaseCountry)29 ArrayList (java.util.ArrayList)20 TestCase (org.cerberus.crud.entity.TestCase)17 IFactoryTestCaseCountry (org.cerberus.crud.factory.IFactoryTestCaseCountry)15 ITestCaseService (org.cerberus.crud.service.ITestCaseService)15 JSONObject (org.json.JSONObject)15 ITestCaseCountryService (org.cerberus.crud.service.ITestCaseCountryService)13 ApplicationContext (org.springframework.context.ApplicationContext)13 MessageEvent (org.cerberus.engine.entity.MessageEvent)11 AnswerItem (org.cerberus.util.answer.AnswerItem)11 ILogEventService (org.cerberus.crud.service.ILogEventService)10 JSONArray (org.json.JSONArray)10 List (java.util.List)8 TestCaseCountryProperties (org.cerberus.crud.entity.TestCaseCountryProperties)8 TestCaseStep (org.cerberus.crud.entity.TestCaseStep)8 CerberusException (org.cerberus.exception.CerberusException)8 TestCaseStepAction (org.cerberus.crud.entity.TestCaseStepAction)7 TestCaseStepActionControl (org.cerberus.crud.entity.TestCaseStepActionControl)7 PolicyFactory (org.owasp.html.PolicyFactory)7 Connection (java.sql.Connection)5