Best Cerberus-source code snippet using org.cerberus.servlet.crud.test.testcase.ReadTestCase.processRequest
Source:ReadTestCase.java
...64 * @throws ServletException if a servlet-specific error occurs65 * @throws IOException if an I/O error occurs66 */67 @Override68 protected void processRequest(HttpServletRequest request, HttpServletResponse response)69 throws ServletException, IOException {70 71 response.setContentType("application/json");72 response.setCharacterEncoding("utf8");73 // Calling Servlet Transversal Util.74 ServletUtil.servletStart(request);75 // Default message to unexpected error.76 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);77 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));78 /**79 * Parsing and securing all required parameters.80 */81 int sEcho = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("sEcho"), "0"));82 String test = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("test"), "");83 List<String> system = ParameterParserUtil.parseListParamAndDecodeAndDeleteEmptyValue(request.getParameterValues("system"), Arrays.asList("DEFAULT"), "UTF-8");84 String testCase = ParameterParserUtil.parseStringParam(request.getParameter("testCase"), null);85 String campaign = ParameterParserUtil.parseStringParam(request.getParameter("campaign"), "");86 boolean getMaxTC = ParameterParserUtil.parseBooleanParam(request.getParameter("getMaxTC"), false);87 boolean filter = ParameterParserUtil.parseBooleanParam(request.getParameter("filter"), false);88 boolean withSteps = ParameterParserUtil.parseBooleanParam(request.getParameter("withSteps"), false);89 String columnName = ParameterParserUtil.parseStringParam(request.getParameter("columnName"), "");90 // Init Answer with potencial error from Parsing parameter.91 AnswerItem<JSONObject> answer = new AnswerItem<>(msg);92 JSONObject jsonResponse = new JSONObject();93 try {94 if (!Strings.isNullOrEmpty(test) && testCase != null) {95 answer = findTestCaseByTestTestCase(test, testCase, request, withSteps);96 } else if (!Strings.isNullOrEmpty(test) && getMaxTC) {97 String max = testCaseService.getNextAvailableTestcaseId(test);98 jsonResponse.put("nextAvailableTestcaseId", max);99 answer.setResultMessage(new MessageEvent(MessageEventEnum.DATA_OPERATION_OK));100 } else if (filter) {101 answer = findTestCaseByVarious(request);102 } else if (!Strings.isNullOrEmpty(campaign)) {103 answer = findTestCaseByCampaign(campaign);104 } else if (!Strings.isNullOrEmpty(columnName)) {105 //If columnName is present, then return the distinct value of this column.106 answer = findDistinctValuesOfColumn(system, test, request, columnName);107 } else {108 // Page TestCaseList109 answer = findTestCaseByTest(system, test, request);110 }111 if (!getMaxTC) {112 jsonResponse = (answer.getItem() == null) ? new JSONObject() : answer.getItem();113 }114 jsonResponse.put("messageType", answer.getResultMessage().getMessage().getCodeString());115 jsonResponse.put("message", answer.getResultMessage().getDescription());116 jsonResponse.put("sEcho", sEcho);117 response.getWriter().print(jsonResponse.toString());118 } catch (JSONException e) {119 LOG.warn(e, e);120 //returns a default error message with the json format that is able to be parsed by the client-side121 response.getWriter().print(AnswerUtil.createGenericErrorAnswer());122 } catch (CerberusException ex) {123 LOG.error(ex, ex);124 // TODO return to the gui125 } catch (Exception ex) {126 LOG.error(ex, ex);127 // TODO return to the gui128 }129 }130 private AnswerItem<JSONObject> findTestCaseByTest(List<String> system, String test, HttpServletRequest request) throws JSONException, CerberusException {131 AnswerItem<JSONObject> answer = new AnswerItem<>();132 JSONObject object = new JSONObject();133 boolean isCalledFromListPage = (request.getParameter("sColumns") != null);134 int startPosition = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayStart"), "0"));135 int length = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayLength"), "0"));136 String searchParameter = ParameterParserUtil.parseStringParam(request.getParameter("sSearch"), "");137 String sColumns = ParameterParserUtil.parseStringParam(request.getParameter("sColumns"), "tec.test,tec.testcase,tec.application,project,ticket,description,detailedDescription,readonly,bugtrackernewurl,deploytype,mavengroupid");138 String columnToSort[] = sColumns.split(",");139 List<String> individualLike = new ArrayList<>(Arrays.asList(ParameterParserUtil.parseStringParam(request.getParameter("sLike"), "").split(",")));140 StringBuilder sortInformation = getSortingInformation(columnToSort, request);141 Map<String, List<String>> individualSearch = getIndivualSearch(request, columnToSort, individualLike);142 AnswerList<TestCase> testCases = testCaseService.findTestCasesByTestByCriteriaWithDependencies(system, test, startPosition, length, sortInformation.toString(), searchParameter, individualSearch, isCalledFromListPage);143 JSONArray jsonArray = new JSONArray();144 for (TestCase testCase : testCases.getDataList()) {145 JSONObject jsonTestCase = testCase.toJson();146 jsonTestCase.put("hasPermissionsUpdate", testCaseService.hasPermissionsUpdate(testCase, request));147 jsonTestCase.put("hasPermissionsDelete", testCaseService.hasPermissionsDelete(testCase, request));148 jsonArray.put(jsonTestCase);149 }150 object.put("hasPermissionsCreate", testCaseService.hasPermissionsCreate(null, request));151 object.put("contentTable", jsonArray);152 object.put("iTotalRecords", testCases.getTotalRows());153 object.put("iTotalDisplayRecords", testCases.getTotalRows());154 answer.setItem(object);155 answer.setResultMessage(testCases.getResultMessage());156 return answer;157 }158 private AnswerItem<JSONObject> findTestCaseByTestTestCase(String test, String testCase, HttpServletRequest request, boolean withSteps) throws JSONException, CerberusException {159 AnswerItem<JSONObject> answerItem = new AnswerItem<>();160 JSONObject jsonResponse = new JSONObject();161 AnswerItem<TestCase> answerTestCase;162 answerTestCase = testCaseService.findTestCaseByKeyWithDependencies(test, testCase, withSteps);163 if (answerTestCase.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && answerTestCase.getItem() != null) {164 TestCase tc = answerTestCase.getItem();165 if (withSteps) {166 jsonResponse.put("hasPermissionsStepLibrary", (request.isUserInRole("TestStepLibrary")));167 }168 jsonResponse.put("hasPermissionsUpdate", testCaseService.hasPermissionsUpdate(tc, request));169 jsonResponse.put("hasPermissionsDelete", testCaseService.hasPermissionsDelete(tc, request));170 jsonResponse.put("contentTable", new JSONArray().put(tc.toJson()));171 } else {172 answerItem.setResultMessage(new MessageEvent(MessageEventEnum.DATA_OPERATION_NOT_FOUND_OR_NOT_AUTHORIZE));173 return answerItem;174 }175 answerItem.setItem(jsonResponse);176 answerItem.setResultMessage(answerTestCase.getResultMessage());177 LOG.debug(answerItem.getItem());178 return answerItem;179 }180 private AnswerItem<JSONObject> findTestCaseByVarious(HttpServletRequest request) throws JSONException {181 AnswerItem<JSONObject> item = new AnswerItem<>();182 JSONObject object = new JSONObject();183 JSONArray dataArray = new JSONArray();184 String[] test = request.getParameterValues("test");185 String[] idProject = request.getParameterValues("project");186 String[] app = request.getParameterValues("application");187 String[] creator = request.getParameterValues("creator");188 String[] implementer = request.getParameterValues("implementer");189 String[] system = request.getParameterValues("system");190 String[] campaign = request.getParameterValues("campaign");191 String[] priority = request.getParameterValues("priority");192 String[] type = request.getParameterValues("type");193 String[] status = request.getParameterValues("status");194 String[] labelid = request.getParameterValues("labelid");195 List<Integer> labels = new ArrayList<>();196 if (labelid != null) {197 for (int i = 0; i < labelid.length; i++) {198 String string = labelid[i];199 labels.add(Integer.valueOf(string));200 }201 labels = labelService.enrichWithChild(labels);202 }203 int length = ParameterParserUtil.parseIntegerParam(request.getParameter("length"), -1);204 AnswerList<TestCase> answer = testCaseService.readByVarious(test, app, creator, implementer, system, campaign, labels, priority, type, status, length);205 if (answer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {206 for (TestCase tc : answer.getDataList()) {207 JSONObject value = tc.toJson();208 dataArray.put(value);209 }210 }211 object.put("contentTable", dataArray);212 item.setItem(object);213 item.setResultMessage(answer.getResultMessage());214 return item;215 }216 private AnswerItem<JSONObject> findTestCaseByCampaign(String campaign) throws JSONException {217 AnswerItem<JSONObject> answerItem = new AnswerItem<>();218 JSONObject jsonResponse = new JSONObject();219 JSONArray dataArray = new JSONArray();220 AnswerList<TestCase> testCases = testCaseService.findTestCaseByCampaign(campaign);221 if (testCases.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {//the service was able to perform the query, then we should get all values222 for (TestCase testCase : testCases.getDataList()) {223 JSONObject jsonTestCase = testCase.toJson();224 dataArray.put(jsonTestCase);225 }226 }227 jsonResponse.put("contentTable", dataArray);228 answerItem.setItem(jsonResponse);229 answerItem.setResultMessage(testCases.getResultMessage());230 return answerItem;231 }232 private AnswerItem<JSONObject> findDistinctValuesOfColumn(List<String> system, String test, HttpServletRequest request, String columnName) throws JSONException {233 AnswerItem<JSONObject> answerItem = new AnswerItem<>();234 JSONObject jsonResponse = new JSONObject();235 String searchParameter = ParameterParserUtil.parseStringParam(request.getParameter("sSearch"), "");236 String sColumns = ParameterParserUtil.parseStringParam(request.getParameter("sColumns"), "tec.test,tec.testcase,application,project,ticket,description,detailedDescription,readonly,bugtrackernewurl,deploytype,mavengroupid");237 String columnToSort[] = sColumns.split(",");238 List<String> individualLike = new ArrayList<>(Arrays.asList(ParameterParserUtil.parseStringParam(request.getParameter("sLike"), "").split(",")));239 Map<String, List<String>> individualSearch = getIndivualSearch(request, columnToSort, individualLike);240 AnswerList testCaseList = testCaseService.readDistinctValuesByCriteria(system, test, searchParameter, individualSearch, columnName);241 jsonResponse.put("distinctValues", testCaseList.getDataList());242 answerItem.setItem(jsonResponse);243 answerItem.setResultMessage(testCaseList.getResultMessage());244 return answerItem;245 }246 private StringBuilder getSortingInformation(String columnToSort[], HttpServletRequest request) {247 int numberOfColumnToSort = Integer.parseInt(ParameterParserUtil.parseStringParam(request.getParameter("iSortingCols"), "1"));248 int columnToSortParameter = 0;249 String sort = "asc";250 StringBuilder sortInformation = new StringBuilder();251 for (int i = 0; i < numberOfColumnToSort; i++) {252 columnToSortParameter = Integer.parseInt(ParameterParserUtil.parseStringParam(request.getParameter("iSortCol_" + i), "0"));253 sort = ParameterParserUtil.parseStringParam(request.getParameter("sSortDir_" + i), "asc");254 String columnName = columnToSort[columnToSortParameter];255 sortInformation.append(columnName).append(" ").append(sort);256 if (i != numberOfColumnToSort - 1) {257 sortInformation.append(" , ");258 }259 }260 return sortInformation;261 }262 private Map<String, List<String>> getIndivualSearch(HttpServletRequest request, String columnToSort[], List<String> individualLike) {263 Map<String, List<String>> individualSearch = new HashMap<>();264 for (int i = 0; i < columnToSort.length; i++) {265 if (null != request.getParameter("sSearch_" + i) && !request.getParameter("sSearch_" + i).isEmpty()) {266 List<String> search = new ArrayList<>(Arrays.asList(request.getParameter("sSearch_" + i).split(",")));267 if (individualLike.contains(columnToSort[i])) {268 individualSearch.put(columnToSort[i] + ":like", search);269 } else {270 individualSearch.put(columnToSort[i], search);271 }272 }273 }274 return individualSearch;275 }276 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">277 /**278 * Handles the HTTP <code>GET</code> method.279 *280 * @param request servlet request281 * @param response servlet response282 * @throws ServletException if a servlet-specific error occurs283 * @throws IOException if an I/O error occurs284 */285 @Override286 protected void doGet(HttpServletRequest request, HttpServletResponse response)287 throws ServletException, IOException {288 processRequest(request, response);289 }290 /**291 * Handles the HTTP <code>POST</code> method.292 *293 * @param request servlet request294 * @param response servlet response295 * @throws ServletException if a servlet-specific error occurs296 * @throws IOException if an I/O error occurs297 */298 @Override299 protected void doPost(HttpServletRequest request, HttpServletResponse response)300 throws ServletException, IOException {301 processRequest(request, response);302 }303 /**304 * Returns a short description of the servlet.305 *306 * @return a String containing servlet description307 */308 @Override309 public String getServletInfo() {310 return "Short description";311 }// </editor-fold>312}...
processRequest
Using AI Code Generation
1 public void testReadTestCase() throws Exception {2 HttpServletRequest request = mock(HttpServletRequest.class);3 HttpServletResponse response = mock(HttpServletResponse.class);4 ReadTestCase instance = new ReadTestCase();5 String test = "TEST";6 String testCase = "TC";7 String system = "SYS";8 String country = "CTRY";9 when(request.getParameter("test")).thenReturn(test);10 when(request.getParameter("testCase")).thenReturn(testCase);11 when(request.getParameter("system")).thenReturn(system);12 when(request.getParameter("country")).thenReturn(country);13 instance.processRequest(request, response);14 }15 public void testReadTestCaseWithEmptyTestCase() throws Exception {16 HttpServletRequest request = mock(HttpServletRequest.class);17 HttpServletResponse response = mock(HttpServletResponse.class);18 ReadTestCase instance = new ReadTestCase();19 String test = "TEST";20 String testCase = "";21 String system = "SYS";22 String country = "CTRY";23 when(request.getParameter("test")).thenReturn(test);24 when(request.getParameter("testCase")).thenReturn(testCase);25 when(request.getParameter("system")).thenReturn(system);26 when(request.getParameter("country")).thenReturn(country);27 instance.processRequest(request, response);28 }29 [ERROR] testReadTestCase(org.cerberus.servlet.crud.test.testcase.ReadTestCaseTest) Time elapsed: 0.006 s <<< ERROR!30 at org.cerberus.servlet.crud.test.testcase.ReadTestCase.processRequest(ReadTestCase.java:49)31 at org.cerberus.servlet.crud.test.testcase.ReadTestCaseTest.testReadTestCase(ReadTestCaseTest.java:40)32 when(request.getParameter("test")).thenReturn(test);33 when(request.getParameter("test")).thenReturn(test);34 when(request.getParameter("testCase")).thenReturn(testCase);35 when(request.getParameter("system")).thenReturn(system);36 when(request.getParameter("country")).thenReturn(country);37 [ERROR] testReadTestCase(org.cerberus.servlet.crud
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!