use of org.cerberus.util.answer.AnswerItem in project cerberus-source by cerberustesting.
the class ReadTestCaseExecution method findTestCaseExecutionList.
private AnswerItem findTestCaseExecutionList(ApplicationContext appContext, boolean userHasPermissions, HttpServletRequest request) throws JSONException, CerberusException {
AnswerItem answer = new AnswerItem(new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED));
AnswerList testCaseExecutionList = new AnswerList();
JSONObject object = new JSONObject();
testCaseExecutionService = appContext.getBean(TestCaseExecutionService.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"), "");
int columnToSortParameter = Integer.parseInt(ParameterParserUtil.parseStringParam(request.getParameter("iSortCol_0"), "0"));
String sColumns = ParameterParserUtil.parseStringParam(request.getParameter("sColumns"), "test,description,active,automated,tdatecrea");
String[] columnToSort = sColumns.split(",");
String columnName = columnToSort[columnToSortParameter];
String sort = ParameterParserUtil.parseStringParam(request.getParameter("sSortDir_0"), "asc");
Map<String, List<String>> individualSearch = new HashMap<>();
List<String> individualLike = new ArrayList(Arrays.asList(ParameterParserUtil.parseStringParam(request.getParameter("sLike"), "").split(",")));
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);
}
}
}
testCaseExecutionList = testCaseExecutionService.readByCriteria(startPosition, length, columnName.concat(" ").concat(sort), searchParameter, individualSearch, individualLike);
JSONArray jsonArray = new JSONArray();
if (testCaseExecutionList.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
// the service was able to perform the query, then we should get all values
for (TestCaseExecution testCaseExecution : (List<TestCaseExecution>) testCaseExecutionList.getDataList()) {
jsonArray.put(testCaseExecution.toJson(true).put("hasPermissions", userHasPermissions));
}
}
object.put("contentTable", jsonArray);
object.put("hasPermissions", userHasPermissions);
object.put("iTotalRecords", testCaseExecutionList.getTotalRows());
object.put("iTotalDisplayRecords", testCaseExecutionList.getTotalRows());
answer.setItem(object);
answer.setResultMessage(testCaseExecutionList.getResultMessage());
return answer;
}
use of org.cerberus.util.answer.AnswerItem in project cerberus-source by cerberustesting.
the class ReadTestCaseExecutionByTag 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 {
// Calling Servlet Transversal Util.
ServletUtil.servletStart(request);
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
response.setContentType("application/json");
response.setCharacterEncoding("utf8");
String echo = request.getParameter("sEcho");
AnswerItem answer = new AnswerItem(new MessageEvent(MessageEventEnum.DATA_OPERATION_OK));
testCaseExecutionService = appContext.getBean(ITestCaseExecutionService.class);
tagService = appContext.getBean(ITagService.class);
testCaseExecutionInQueueService = appContext.getBean(ITestCaseExecutionQueueService.class);
try {
// Data/Filter Parameters.
String Tag = ParameterParserUtil.parseStringParam(request.getParameter("Tag"), "");
List<String> outputReport = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("outputReport"), new ArrayList(), "UTF-8");
JSONObject jsonResponse = new JSONObject();
JSONObject statusFilter = getStatusList(request);
JSONObject countryFilter = getCountryList(request, appContext);
// Get Data from database
List<TestCaseExecution> testCaseExecutions = testCaseExecutionService.readLastExecutionAndExecutionInQueueByTag(Tag);
// Table that contain the list of testcases and corresponding executions
if (outputReport.isEmpty() || outputReport.contains("table")) {
jsonResponse.put("table", generateTestCaseExecutionTable(appContext, testCaseExecutions, statusFilter, countryFilter));
}
// Executions per Function (or Test).
if (outputReport.isEmpty() || outputReport.contains("functionChart")) {
jsonResponse.put("functionChart", generateFunctionChart(testCaseExecutions, Tag, statusFilter, countryFilter));
}
// Global executions stats per Status
if (outputReport.isEmpty() || outputReport.contains("statsChart")) {
jsonResponse.put("statsChart", generateStats(request, testCaseExecutions, statusFilter, countryFilter, true));
}
// BugTracker Recap
if (outputReport.isEmpty() || outputReport.contains("bugTrackerStat")) {
jsonResponse.put("bugTrackerStat", generateBugStats(request, testCaseExecutions, statusFilter, countryFilter));
}
// Labels Stats
if (outputReport.isEmpty() || outputReport.contains("labelStat")) {
jsonResponse.put("labelStat", generateLabelStats(appContext, request, testCaseExecutions, statusFilter, countryFilter));
}
if (!outputReport.isEmpty()) {
// currently used to optimize the homePage
if (outputReport.contains("totalStatsCharts") && !outputReport.contains("statsChart")) {
jsonResponse.put("statsChart", generateStats(request, testCaseExecutions, statusFilter, countryFilter, false));
}
// currently used to optimize the homePage
if (outputReport.contains("resendTag")) {
jsonResponse.put("tag", Tag);
}
}
Tag mytag = tagService.convert(tagService.readByKey(Tag));
JSONObject tagJSON = convertTagToJSONObject(mytag);
jsonResponse.put("tagObject", tagJSON);
jsonResponse.put("tagDuration", (mytag.getDateEndQueue().getTime() - mytag.getDateCreated().getTime()) / 60000);
answer.setItem(jsonResponse);
answer.setResultMessage(answer.getResultMessage().resolveDescription("ITEM", "Tag Statistics").resolveDescription("OPERATION", "Read"));
jsonResponse.put("messageType", answer.getResultMessage().getMessage().getCodeString());
jsonResponse.put("message", answer.getResultMessage().getDescription());
jsonResponse.put("sEcho", echo);
response.getWriter().print(jsonResponse.toString());
} catch (ParseException ex) {
LOG.error("Error on main call : " + ex);
} catch (CerberusException ex) {
LOG.error("Error on main call : " + ex);
} catch (JSONException ex) {
LOG.error("Error on main call : " + ex);
} catch (Exception ex) {
LOG.error("Error on main call : " + ex);
}
}
use of org.cerberus.util.answer.AnswerItem in project cerberus-source by cerberustesting.
the class ReadTestCaseExecutionQueue method findExecutionQueueByKeyTech.
private AnswerItem findExecutionQueueByKeyTech(Long queueid, ApplicationContext appContext, boolean userHasPermissions) throws JSONException, CerberusException {
AnswerItem item = new AnswerItem();
JSONObject object = new JSONObject();
ITestCaseExecutionQueueService queueService = appContext.getBean(ITestCaseExecutionQueueService.class);
// finds the project
AnswerItem answer = queueService.readByKey(queueid);
if (answer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
// if the service returns an OK message then we can get the item and convert it to JSONformat
TestCaseExecutionQueue lib = (TestCaseExecutionQueue) answer.getItem();
JSONObject response = convertTestCaseExecutionInQueueToJSONObject(lib);
int nb = 0;
nb = queueService.getNbEntryToGo(lib.getId(), lib.getPriority());
response.put("nbEntryInQueueToGo", nb);
object.put("contentTable", response);
}
object.put("hasPermissions", userHasPermissions);
item.setItem(object);
item.setResultMessage(answer.getResultMessage());
return item;
}
use of org.cerberus.util.answer.AnswerItem in project cerberus-source by cerberustesting.
the class ReadTestCaseExecutionQueue method findExecutionInQueueList.
private AnswerItem findExecutionInQueueList(ApplicationContext appContext, boolean userHasPermissions, HttpServletRequest request) throws JSONException {
AnswerItem item = new AnswerItem();
JSONObject object = new JSONObject();
executionService = appContext.getBean(ITestCaseExecutionQueueService.class);
int startPosition = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayStart"), "0"));
int length = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayLength"), "0"));
/*int sEcho = Integer.valueOf(request.getParameter("sEcho"));*/
String searchParameter = ParameterParserUtil.parseStringParam(request.getParameter("sSearch"), "");
int columnToSortParameter = Integer.parseInt(ParameterParserUtil.parseStringParam(request.getParameter("iSortCol_0"), "2"));
String sColumns = ParameterParserUtil.parseStringParam(request.getParameter("sColumns"), "id,test,testcase,country,environment,browser,tag");
String[] columnToSort = sColumns.split(",");
String columnName = columnToSort[columnToSortParameter];
String sort = ParameterParserUtil.parseStringParam(request.getParameter("sSortDir_0"), "desc");
List<String> individualLike = new ArrayList(Arrays.asList(request.getParameter("sLike").split(",")));
Map<String, List<String>> individualSearch = new HashMap<>();
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 resp = executionService.readByCriteria(startPosition, length, columnName, sort, searchParameter, individualSearch);
JSONArray jsonArray = new JSONArray();
if (resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
// the service was able to perform the query, then we should get all values
for (TestCaseExecutionQueue exec : (List<TestCaseExecutionQueue>) resp.getDataList()) {
jsonArray.put(convertTestCaseExecutionInQueueToJSONObject(exec));
}
}
object.put("hasPermissions", userHasPermissions);
object.put("contentTable", jsonArray);
object.put("iTotalRecords", resp.getTotalRows());
object.put("iTotalDisplayRecords", resp.getTotalRows());
item.setItem(object);
item.setResultMessage(resp.getResultMessage());
return item;
}
use of org.cerberus.util.answer.AnswerItem in project cerberus-source by cerberustesting.
the class UpdateRobot 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();
Gson gson = new Gson();
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);
response.setContentType("application/json");
String charset = request.getCharacterEncoding();
/**
* Parsing and securing all required parameters.
*/
// Parameter that are already controled by GUI (no need to decode) --> We SECURE them
// Parameter that needs to be secured --> We SECURE+DECODE them
String robot = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("robot"), null, charset);
String port = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("port"), null, charset);
String platform = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("platform"), null, charset);
String browser = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("browser"), null, charset);
String version = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("version"), "", charset);
String active = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("active"), "Y", charset);
String description = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("description"), "", charset);
String userAgent = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("useragent"), "", charset);
String screenSize = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("screensize"), "", charset);
String hostUser = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("hostUsername"), null, charset);
String hostPassword = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("hostPassword"), null, charset);
// Parameter that we cannot secure as we need the html --> We DECODE them
String host = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("host"), null, charset);
String robotDecli = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("robotDecli"), null, charset);
List<RobotCapability> capabilities = (List<RobotCapability>) (request.getParameter("capabilities") == null ? Collections.emptyList() : gson.fromJson(request.getParameter("capabilities"), new TypeToken<List<RobotCapability>>() {
}.getType()));
// Securing capabilities by setting them the associated robot name
// Check also if there is no duplicated capability
Map<String, Object> capabilityMap = new HashMap<String, Object>();
for (RobotCapability capability : capabilities) {
capabilityMap.put(capability.getCapability(), null);
capability.setRobot(robot);
}
Integer robotid = 0;
boolean robotid_error = true;
try {
if (request.getParameter("robotid") != null && !request.getParameter("robotid").equals("")) {
robotid = Integer.valueOf(policy.sanitize(request.getParameter("robotid")));
robotid_error = false;
}
} catch (Exception ex) {
robotid_error = true;
}
/**
* Checking all constrains before calling the services.
*/
if (StringUtil.isNullOrEmpty(robot)) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Robot").replace("%OPERATION%", "Update").replace("%REASON%", "Robot name is missing."));
ans.setResultMessage(msg);
} else if (StringUtil.isNullOrEmpty(host)) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Robot").replace("%OPERATION%", "Update").replace("%REASON%", "Robot host is missing."));
ans.setResultMessage(msg);
} else if (StringUtil.isNullOrEmpty(platform)) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Robot").replace("%OPERATION%", "Update").replace("%REASON%", "Robot platform is missing."));
ans.setResultMessage(msg);
} else if (robotid_error) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Robot").replace("%OPERATION%", "Update").replace("%REASON%", "Could not manage to convert robotid to an integer value or robotid is missing."));
ans.setResultMessage(msg);
} else if (capabilityMap.size() != capabilities.size()) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Robot").replace("%OPERATION%", "Create").replace("%REASON%", "There is at least one duplicated capability. Please edit or remove it to continue."));
ans.setResultMessage(msg);
} else {
/**
* All data seems cleans so we can call the services.
*/
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IRobotService robotService = appContext.getBean(IRobotService.class);
AnswerItem resp = robotService.readByKeyTech(robotid);
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%", "Robot").replace("%OPERATION%", "Update").replace("%REASON%", "Robot 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.
*/
Robot robotData = (Robot) resp.getItem();
robotData.setRobot(robot);
robotData.setHost(host);
robotData.setPort(port);
robotData.setPlatform(platform);
robotData.setBrowser(browser);
robotData.setVersion(version);
robotData.setActive(active);
robotData.setDescription(description);
robotData.setUserAgent(userAgent);
robotData.setCapabilities(capabilities);
robotData.setScreenSize(screenSize);
robotData.setHostUser(hostUser);
robotData.setHostPassword(hostPassword);
robotData.setRobotDecli(robotDecli);
ans = robotService.update(robotData);
if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Update was successful. Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/UpdateRobot", "UPDATE", "Updated Robot : ['" + robotid + "'|'" + robot + "']", 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();
}
Aggregations