use of org.cerberus.engine.entity.MessageEvent in project cerberus-source by cerberustesting.
the class TestCaseLabelDAO method readByCriteria.
@Override
public AnswerList<List<TestCaseLabel>> readByCriteria(int start, int amount, String column, String dir, String searchTerm, Map<String, List<String>> individualSearch) {
AnswerList response = new AnswerList();
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
List<TestCaseLabel> objectList = new ArrayList<>();
StringBuilder searchSQL = new StringBuilder();
List<String> individalColumnSearchValues = new ArrayList<>();
StringBuilder query = new StringBuilder();
// SQL_CALC_FOUND_ROWS allows to retrieve the total number of columns by disrearding the limit clauses that
// were applied -- used for pagination p
query.append("SELECT SQL_CALC_FOUND_ROWS * FROM testcaselabel tel");
query.append(" LEFT OUTER JOIN label lab on lab.id = tel.labelId ");
searchSQL.append(" where 1=1 ");
if (!StringUtil.isNullOrEmpty(searchTerm)) {
searchSQL.append(" and (tel.`id` like ?");
searchSQL.append(" or tel.`test` like ?");
searchSQL.append(" or tel.`testcase` like ?");
searchSQL.append(" or tel.`labelid` like ?");
searchSQL.append(" or tel.`usrCreated` like ?");
searchSQL.append(" or tel.`dateCreated` like ?");
searchSQL.append(" or tel.`usrModif` like ?");
searchSQL.append(" or tel.`dateModif` like ?)");
}
if (individualSearch != null && !individualSearch.isEmpty()) {
searchSQL.append(" and ( 1=1 ");
for (Map.Entry<String, List<String>> entry : individualSearch.entrySet()) {
searchSQL.append(" and ");
searchSQL.append(SqlUtil.getInSQLClauseForPreparedStatement(entry.getKey(), entry.getValue()));
individalColumnSearchValues.addAll(entry.getValue());
}
searchSQL.append(" )");
}
query.append(searchSQL);
if (!StringUtil.isNullOrEmpty(column)) {
query.append(" order by `").append(column).append("` ").append(dir);
}
if ((amount <= 0) || (amount >= MAX_ROW_SELECTED)) {
query.append(" limit ").append(start).append(" , ").append(MAX_ROW_SELECTED);
} else {
query.append(" limit ").append(start).append(" , ").append(amount);
}
// Debug message on SQL.
if (LOG.isDebugEnabled()) {
LOG.debug("SQL : " + query.toString());
}
try (Connection connection = databaseSpring.connect();
PreparedStatement preStat = connection.prepareStatement(query.toString());
Statement stm = connection.createStatement()) {
int i = 1;
if (!StringUtil.isNullOrEmpty(searchTerm)) {
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
}
for (String individualColumnSearchValue : individalColumnSearchValues) {
preStat.setString(i++, individualColumnSearchValue);
}
try (ResultSet resultSet = preStat.executeQuery();
ResultSet rowSet = stm.executeQuery("SELECT FOUND_ROWS()")) {
// gets the data
while (resultSet.next()) {
Label label = labelDAO.loadFromResultSet(resultSet);
objectList.add(this.loadFromResultSet(resultSet, label));
}
// get the total number of rows
int nrTotalRows = 0;
if (rowSet != null && rowSet.next()) {
nrTotalRows = rowSet.getInt(1);
}
if (objectList.size() >= MAX_ROW_SELECTED) {
// Result of SQl was limited by MAX_ROW_SELECTED constrain. That means that we may miss some lines in the resultList.
LOG.error("Partial Result in the query.");
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_WARNING_PARTIAL_RESULT);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Maximum row reached : " + MAX_ROW_SELECTED));
response = new AnswerList(objectList, nrTotalRows);
} else if (objectList.size() <= 0) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
response = new AnswerList(objectList, nrTotalRows);
} else {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
response = new AnswerList(objectList, nrTotalRows);
}
response.setDataList(objectList);
} 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()));
}
} catch (Exception e) {
LOG.warn("Unable to readByCriteria TestCaseLabel: " + e.getMessage());
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", e.toString());
} finally {
response.setResultMessage(msg);
}
return response;
}
use of org.cerberus.engine.entity.MessageEvent in project cerberus-source by cerberustesting.
the class TestCaseLabelDAO method readByKey.
@Override
public AnswerItem<TestCaseLabel> readByKey(String test, String testCase, Integer labelId) {
AnswerItem<TestCaseLabel> ans = new AnswerItem();
TestCaseLabel result = null;
final String query = "SELECT * FROM `testcaselabel` tel WHERE `labelid` = ? and `test` = ? and `testcase` = ?";
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.label : " + labelId);
LOG.debug("SQL.param.test : " + test);
LOG.debug("SQL.param.testcase : " + testCase);
}
try (Connection connection = databaseSpring.connect();
PreparedStatement preStat = connection.prepareStatement(query)) {
// prepare and execute query
preStat.setInt(1, labelId);
preStat.setString(2, test);
preStat.setString(3, testCase);
try (ResultSet resultSet = preStat.executeQuery()) {
// parse query
if (resultSet.first()) {
result = loadFromResultSet(resultSet, null);
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()));
}
} catch (Exception e) {
LOG.warn("Unable to readByKey TestCaseLabel: " + e.getMessage());
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", e.toString());
} finally {
ans.setResultMessage(msg);
}
return ans;
}
use of org.cerberus.engine.entity.MessageEvent in project cerberus-source by cerberustesting.
the class TestCaseLabelDAO method update.
@Override
public Answer update(TestCaseLabel object) {
Answer response = new Answer();
MessageEvent msg = null;
final String query = "UPDATE testcaselabel SET `test` = ?, `testcase` = ?, `labelId` = ?, `usrModif` = ?, `dateModif` = NOW() WHERE id = ?";
// Debug message on SQL.
if (LOG.isDebugEnabled()) {
LOG.debug("SQL : " + query);
}
try (Connection connection = databaseSpring.connect();
PreparedStatement preStat = connection.prepareStatement(query.toString())) {
preStat.setString(1, object.getTest());
preStat.setString(2, object.getTestcase());
preStat.setInt(3, object.getLabelId());
preStat.setString(4, object.getUsrModif());
preStat.setInt(5, object.getId());
preStat.executeUpdate();
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "UPDATE"));
} catch (Exception e) {
LOG.warn("Unable to update TestCaseLabel: " + e.getMessage());
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", e.toString());
} finally {
response.setResultMessage(msg);
}
return response;
}
use of org.cerberus.engine.entity.MessageEvent in project cerberus-source by cerberustesting.
the class TestCaseStepActionControlDAO method create.
@Override
public Answer create(TestCaseStepActionControl testCaseStepActionControl) {
Answer ans = new Answer();
MessageEvent msg = null;
StringBuilder query = new StringBuilder();
query.append("INSERT INTO testcasestepactioncontrol (`test`, `testCase`, `step`, `sequence`, `controlSequence`, `sort`, `conditionOper`, `conditionVal1`, `conditionVal2`, `control`, `value1`, `value2`, `fatal`, `Description`, `screenshotfilename`) ");
query.append("VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
try (Connection connection = databaseSpring.connect();
PreparedStatement preStat = connection.prepareStatement(query.toString())) {
// Prepare and execute query
preStat.setString(1, testCaseStepActionControl.getTest());
preStat.setString(2, testCaseStepActionControl.getTestCase());
preStat.setInt(3, testCaseStepActionControl.getStep());
preStat.setInt(4, testCaseStepActionControl.getSequence());
preStat.setInt(5, testCaseStepActionControl.getControlSequence());
preStat.setInt(6, testCaseStepActionControl.getSort());
preStat.setString(7, testCaseStepActionControl.getConditionOper());
preStat.setString(8, testCaseStepActionControl.getConditionVal1());
preStat.setString(9, testCaseStepActionControl.getConditionVal2());
preStat.setString(10, testCaseStepActionControl.getControl());
preStat.setString(11, testCaseStepActionControl.getValue1());
preStat.setString(12, testCaseStepActionControl.getValue2());
preStat.setString(13, testCaseStepActionControl.getFatal());
preStat.setString(14, testCaseStepActionControl.getDescription());
preStat.setString(15, testCaseStepActionControl.getScreenshotFilename());
preStat.executeUpdate();
// Set the final message
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK).resolveDescription("ITEM", OBJECT_NAME).resolveDescription("OPERATION", "CREATE");
} catch (Exception e) {
LOG.warn("Unable to TestCaseStepActionControl: " + e.getMessage());
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", e.toString());
} finally {
ans.setResultMessage(msg);
}
return ans;
}
use of org.cerberus.engine.entity.MessageEvent in project cerberus-source by cerberustesting.
the class TestCaseStepActionControlExecutionDAO method readByVarious1.
@Override
public AnswerList readByVarious1(long executionId, String test, String testCase, int step, int index, int sequence) {
MessageEvent msg;
AnswerList answer = new AnswerList();
List<TestCaseStepActionControlExecution> list = new ArrayList<TestCaseStepActionControlExecution>();
StringBuilder query = new StringBuilder();
query.append("SELECT * FROM testcasestepactioncontrolexecution a ");
query.append("where id = ? and test = ? and testcase = ? and step = ? and `index` = ? ");
query.append("and sequence = ?");
// Debug message on SQL.
if (LOG.isDebugEnabled()) {
LOG.debug("SQL : " + query.toString());
}
Connection connection = this.databaseSpring.connect();
try {
PreparedStatement preStat = connection.prepareStatement(query.toString());
try {
preStat.setLong(1, executionId);
preStat.setString(2, test);
preStat.setString(3, testCase);
preStat.setInt(4, step);
preStat.setInt(5, index);
preStat.setInt(6, sequence);
ResultSet resultSet = preStat.executeQuery();
try {
while (resultSet.next()) {
list.add(this.loadFromResultset(resultSet));
}
if (list.isEmpty()) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
} else {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
}
} 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()));
list.clear();
} 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%", exception.toString()));
} 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%", exception.toString()));
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException exception) {
LOG.warn("Unable to close connection : " + exception.toString());
}
}
answer.setTotalRows(list.size());
answer.setDataList(list);
answer.setResultMessage(msg);
return answer;
}
Aggregations