How to use processRequest method of org.cerberus.servlet.crud.test.UpdateTestCaseWithDependencies class

Best Cerberus-source code snippet using org.cerberus.servlet.crud.test.UpdateTestCaseWithDependencies.processRequest

Source:UpdateTestCaseWithDependencies.java Github

copy

Full Screen

...76 * @throws ServletException if a servlet-specific error occurs77 * @throws IOException if an I/O error occurs78 * @throws org.cerberus.exception.CerberusException79 */80 protected void processRequest(HttpServletRequest request, HttpServletResponse response)81 throws ServletException, IOException, CerberusException, JSONException {82 JSONObject jsonResponse = new JSONObject();83 Answer ans = new Answer();84 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);85 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));86 ans.setResultMessage(msg);87 PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);88 response.setContentType("application/json");89 // Calling Servlet Transversal Util.90 ServletUtil.servletStart(request);91 /**92 * Parsing and securing all required parameters.93 */94 StringBuilder sb = new StringBuilder();95 BufferedReader br = request.getReader();96 String str;97 while ((str = br.readLine()) != null) {98 sb.append(str);99 }100 JSONObject jObj = new JSONObject(sb.toString());101 String initialTest = jObj.getString("informationInitialTest");102 String initialTestCase = jObj.getString("informationInitialTestCase");103 String test = jObj.getString("informationTest");104 String testCase = jObj.getString("informationTestCase");105 JSONArray properties = jObj.getJSONArray("propArr");106 JSONArray stepArray = jObj.getJSONArray("stepArray");107 boolean duplicate = false;108 /**109 * Checking all constrains before calling the services.110 */111 if (StringUtil.isNullOrEmpty(test) || StringUtil.isNullOrEmpty(testCase)) {112 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);113 msg.setDescription(msg.getDescription().replace("%ITEM%", "Test Case")114 .replace("%OPERATION%", "Update")115 .replace("%REASON%", "mandatory fields are missing."));116 ans.setResultMessage(msg);117 } else {118 ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());119 ITestCaseService testCaseService = appContext.getBean(ITestCaseService.class);120 ITestCaseCountryPropertiesService tccpService = appContext.getBean(ITestCaseCountryPropertiesService.class);121 ITestCaseStepService tcsService = appContext.getBean(ITestCaseStepService.class);122 ITestCaseStepActionService tcsaService = appContext.getBean(ITestCaseStepActionService.class);123 ITestCaseStepActionControlService tcsacService = appContext.getBean(ITestCaseStepActionControlService.class);124 AnswerItem resp = testCaseService.readByKey(test, testCase);125 TestCase tc = (TestCase) resp.getItem();126 if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {127 /**128 * Object could not be found. We stop here and report the error.129 */130 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);131 msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCase")132 .replace("%OPERATION%", "Update")133 .replace("%REASON%", "TestCase does not exist."));134 ans.setResultMessage(msg);135 } else /**136 * The service was able to perform the query and confirm the object137 * exist, then we can update it.138 */139 if (!testCaseService.hasPermissionsUpdate(tc, request)) { // We cannot update the testcase if the user is not at least in Test role.140 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);141 msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCase")142 .replace("%OPERATION%", "Update")143 .replace("%REASON%", "Not enought privilege to update the testcase. You mut belong to Test Privilege or even TestAdmin in case the test is in WORKING status."));144 ans.setResultMessage(msg);145 } else {146 // Test Case exist and we can update it so Global update start here //147 /**148 * TestcaseCountryProperties Update.149 */150 List<TestCaseCountryProperties> tccpFromPage = getTestCaseCountryPropertiesFromParameter(request, appContext, test, testCase, properties);151 tccpService.compareListAndUpdateInsertDeleteElements(initialTest, initialTestCase, tccpFromPage);152 /*153 * Get steps, actions and controls from page by:154 * - generating a new step, action or control number,155 * - setting the correct related step and action for action or control156 */157 List<TestCaseStep> tcsFromPage = getTestCaseStepFromParameter(request, appContext, test, testCase, duplicate, stepArray);158 List<TestCaseStepAction> tcsaFromPage = new ArrayList<>();159 List<TestCaseStepActionControl> tcsacFromPage = new ArrayList<>();160 int nextStepNumber = getMaxStepNumber(tcsFromPage);161 for (TestCaseStep tcs : tcsFromPage) {162 if (tcs.getStep() == -1) {163 tcs.setStep(++nextStepNumber);164 }165 if (tcs.getActions() != null) {166 int nextSequenceNumber = getMaxSequenceNumber(tcs.getActions());167 for (TestCaseStepAction tcsa : tcs.getActions()) {168 if (tcsa.getSequence() == -1) {169 tcsa.setSequence(++nextSequenceNumber);170 }171 tcsa.setStep(tcs.getStep());172 if (tcsa.getControls() != null) {173 int nextControlNumber = getMaxControlNumber(tcsa.getControls());174 for (TestCaseStepActionControl tscac : tcsa.getControls()) {175 if (tscac.getControlSequence() == -1) {176 tscac.setControlSequence(++nextControlNumber);177 }178 tscac.setStep(tcs.getStep());179 tscac.setSequence(tcsa.getSequence());180 }181 tcsacFromPage.addAll(tcsa.getControls());182 }183 }184 tcsaFromPage.addAll(tcs.getActions());185 }186 }187 /*188 * Create, update or delete step, action and control according to the needs189 */190 List<TestCaseStep> tcsFromDtb = new ArrayList<>(tcsService.getListOfSteps(initialTest, initialTestCase));191 tcsService.compareListAndUpdateInsertDeleteElements(tcsFromPage, tcsFromDtb, duplicate);192 List<TestCaseStepAction> tcsaFromDtb = new ArrayList<>(tcsaService.findTestCaseStepActionbyTestTestCase(initialTest, initialTestCase));193 tcsaService.compareListAndUpdateInsertDeleteElements(tcsaFromPage, tcsaFromDtb, duplicate);194 List<TestCaseStepActionControl> tcsacFromDtb = new ArrayList<>(tcsacService.findControlByTestTestCase(initialTest, initialTestCase));195 tcsacService.compareListAndUpdateInsertDeleteElements(tcsacFromPage, tcsacFromDtb, duplicate);196 tc.setUsrModif(request.getUserPrincipal().getName());197 tc.setVersion(tc.getVersion() + 1);198 testCaseService.update(tc.getTest(), tc.getTestCase(), tc);199 /**200 * Adding Log entry.201 */202 if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {203 /**204 * Update was successful. Adding Log entry.205 */206 ILogEventService logEventService = appContext.getBean(LogEventService.class);207 logEventService.createForPrivateCalls("/UpdateTestCaseWithDependencies", "UPDATE", "Update TestCase Script : ['" + tc.getTest() + "'|'" + tc.getTestCase() + "'] version : " + tc.getVersion(), request);208 }209 }210 }211 /**212 * Formating and returning the json result.213 */214 jsonResponse.put("messageType", ans.getResultMessage().getMessage().getCodeString());215 jsonResponse.put("message", ans.getResultMessage().getDescription());216 response.getWriter().print(jsonResponse);217 response.getWriter().flush();218 }219 /**220 * Get the highest step number from the given steps221 *222 * @param steps a collection of steps from which get the highest step number223 * @return the highest step number from the given steps224 */225 private int getMaxStepNumber(Collection<TestCaseStep> steps) {226 int nextStepNumber = 0;227 if (steps != null) {228 for (TestCaseStep step : steps) {229 if (nextStepNumber < step.getStep()) {230 nextStepNumber = step.getStep();231 }232 }233 }234 return nextStepNumber;235 }236 /**237 * Get the highest action sequence from the given actions238 *239 * @param actions a collection of actions from which get the highest action240 * sequence241 * @return the highest action sequence from the given actions242 */243 private int getMaxSequenceNumber(Collection<TestCaseStepAction> actions) {244 int nextSequenceNumber = 0;245 if (actions != null) {246 for (TestCaseStepAction action : actions) {247 if (nextSequenceNumber < action.getSequence()) {248 nextSequenceNumber = action.getSequence();249 }250 }251 }252 return nextSequenceNumber;253 }254 /**255 * Get the highest control number from the given controls256 *257 * @param controls a collection of controls from which get the highest258 * control number259 * @return the highest control number from the given controls260 */261 private int getMaxControlNumber(Collection<TestCaseStepActionControl> controls) {262 int nextControlNumber = 0;263 if (controls != null) {264 for (TestCaseStepActionControl control : controls) {265 if (nextControlNumber < control.getControlSequence()) {266 nextControlNumber = control.getControlSequence();267 }268 }269 }270 return nextControlNumber;271 }272 private List<TestCaseCountryProperties> getTestCaseCountryPropertiesFromParameter(HttpServletRequest request, ApplicationContext appContext, String test, String testCase, JSONArray properties) throws JSONException {273 List<TestCaseCountryProperties> testCaseCountryProp = new ArrayList<>();274// String[] testcase_properties_increment = getParameterValuesIfExists(request, "property_increment");275 IFactoryTestCaseCountryProperties testCaseCountryPropertiesFactory = appContext.getBean(IFactoryTestCaseCountryProperties.class);276 for (int i = 0; i < properties.length(); i++) {277 JSONObject propJson = properties.getJSONObject(i);278 boolean delete = propJson.getBoolean("toDelete");279 String property = propJson.getString("property");280 String description = propJson.getString("description");281 int cacheExpire = propJson.getInt("cacheExpire");282 String type = propJson.getString("type");283 String value = propJson.getString("value1");284 String value2 = propJson.getString("value2");285 String length = propJson.getString("length");286 int rowLimit = propJson.getInt("rowLimit");287 int retryNb = propJson.optInt("retryNb");288 int retryPeriod = propJson.optInt("retryPeriod");289 int rank = propJson.optInt("rank");290 String nature = propJson.getString("nature");291 String database = propJson.getString("database");292 JSONArray countries = propJson.getJSONArray("country");293 if (!delete && !property.equals("")) {294 for (int j = 0; j < countries.length(); j++) {295 String country = countries.getString(j);296 testCaseCountryProp.add(testCaseCountryPropertiesFactory.create(test, testCase, country, property, description, type, database, value, value2, length, rowLimit, nature,297 retryNb, retryPeriod, cacheExpire, rank));298 }299 }300 }301 return testCaseCountryProp;302 }303 private List<TestCaseStep> getTestCaseStepFromParameter(HttpServletRequest request, ApplicationContext appContext, String test, String testCase, boolean duplicate, JSONArray stepArray) throws JSONException {304 List<TestCaseStep> testCaseStep = new ArrayList<>();305 ITestCaseStepService tcsService = appContext.getBean(ITestCaseStepService.class);306 IFactoryTestCaseStep testCaseStepFactory = appContext.getBean(IFactoryTestCaseStep.class);307 for (int i = 0; i < stepArray.length(); i++) {308 JSONObject step = stepArray.getJSONObject(i);309 boolean delete = step.getBoolean("toDelete");310 int stepNumber = step.isNull("stepId") ? -1 : step.getInt("stepId");311 int sort = step.isNull("sort") ? -1 : step.getInt("sort");312 String loop = step.getString("loop");313 String conditionOperator = step.getString("conditionOperator");314 String conditionVal1 = step.getString("conditionVal1");315 String conditionVal2 = step.getString("conditionVal2");316 String conditionVal3 = step.getString("conditionVal3");317 String description = step.getString("description");318 String isUsedStep = step.getString("isUsedStep");319 String libraryStepTest = step.getString("libraryStepTest");320 String libraryStepTestCase = step.getString("libraryStepTestCase");321 int libraryStepStepId = step.getInt("libraryStepStepId");322 String isLibraryStep = step.getString("isLibraryStep");323 String isExecutionForced = step.getString("isExecutionForced");324 JSONArray stepActions = step.getJSONArray("actionArr");325 if (!delete) {326 TestCaseStep tcStep = testCaseStepFactory.create(test, testCase, stepNumber, sort, loop, conditionOperator, conditionVal1, conditionVal2, conditionVal3, description, isUsedStep, libraryStepTest,327 libraryStepTestCase, libraryStepStepId, isLibraryStep, isExecutionForced , null, null, request.getUserPrincipal().getName(), null);328 if (isUsedStep.equals("N")) {329 tcStep.setActions(getTestCaseStepActionFromParameter(request, appContext, test, testCase, stepActions));330 } else {331 TestCaseStep tcs = null;332 if (libraryStepStepId != -1 && !libraryStepTest.equals("") && !libraryStepTestCase.equals("")) {333 tcs = tcsService.findTestCaseStep(libraryStepTest, libraryStepTestCase, libraryStepStepId);334 if (tcs != null) {335 tcStep.setUseStepTest(tcs.getTest());336 tcStep.setUseStepTestCase(tcs.getTestCase());337 tcStep.setUseStepStep(tcs.getStep());338 }339 }340 }341 testCaseStep.add(tcStep);342 }343 }344 return testCaseStep;345 }346 private List<TestCaseStepAction> getTestCaseStepActionFromParameter(HttpServletRequest request, ApplicationContext appContext, String test, String testCase, JSONArray testCaseStepActionJson) throws JSONException {347 List<TestCaseStepAction> testCaseStepAction = new ArrayList<>();348 IFactoryTestCaseStepAction testCaseStepActionFactory = appContext.getBean(IFactoryTestCaseStepAction.class);349 for (int i = 0; i < testCaseStepActionJson.length(); i++) {350 JSONObject tcsaJson = testCaseStepActionJson.getJSONObject(i);351 352 boolean delete = tcsaJson.getBoolean("toDelete");353 int step = tcsaJson.isNull("stepId") ? -1 : tcsaJson.getInt("stepId");354 int sequence = tcsaJson.isNull("sequence") ? -1 : tcsaJson.getInt("sequence");355 int sort = tcsaJson.isNull("sort") ? -1 : tcsaJson.getInt("sort");356 String conditionOperator = tcsaJson.getString("conditionOperator");357 String conditionVal1 = tcsaJson.getString("conditionVal1");358 String conditionVal2 = tcsaJson.getString("conditionVal2");359 String conditionVal3 = tcsaJson.getString("conditionVal3");360 String action = tcsaJson.getString("action");361 String object = tcsaJson.getString("object");362 String property = tcsaJson.getString("property");363 String value3 = tcsaJson.getString("value3");364 String forceExeStatus = tcsaJson.getString("forceExeStatus");365 String description = tcsaJson.getString("description");366 String screenshot = tcsaJson.getString("screenshotFileName");367 JSONArray controlArray = tcsaJson.getJSONArray("controlArr");368 if (!delete) {369 TestCaseStepAction tcsa = testCaseStepActionFactory.create(test, testCase, step, sequence, sort, conditionOperator, conditionVal1, conditionVal2, conditionVal3, action, object, property, value3, forceExeStatus, description, screenshot);370 tcsa.setControls(getTestCaseStepActionControlFromParameter(request, appContext, test, testCase, controlArray));371 testCaseStepAction.add(tcsa);372 }373 }374 return testCaseStepAction;375 }376 private List<TestCaseStepActionControl> getTestCaseStepActionControlFromParameter(HttpServletRequest request, ApplicationContext appContext, String test, String testCase, JSONArray controlArray) throws JSONException {377 List<TestCaseStepActionControl> testCaseStepActionControl = new ArrayList<>();378 IFactoryTestCaseStepActionControl testCaseStepActionControlFactory = appContext.getBean(IFactoryTestCaseStepActionControl.class);379 for (int i = 0; i < controlArray.length(); i++) {380 JSONObject controlJson = controlArray.getJSONObject(i);381 boolean delete = controlJson.getBoolean("toDelete");382 int step = controlJson.isNull("stepId") ? -1 : controlJson.getInt("stepId");383 int sequence = controlJson.isNull("sequence") ? -1 : controlJson.getInt("sequence");384 int control = controlJson.isNull("controlSequence") ? -1 : controlJson.getInt("controlSequence");385 int sort = controlJson.isNull("sort") ? -1 : controlJson.getInt("sort");386 String conditionOperator = controlJson.isNull("conditionOperator") ? "always" : controlJson.getString("conditionOperator");387 String conditionVal1 = controlJson.isNull("conditionVal1") ? "" : controlJson.getString("conditionVal1");388 String conditionVal2 = controlJson.isNull("conditionVal2") ? "" : controlJson.getString("conditionVal2");389 String conditionVal3 = controlJson.isNull("conditionVal3") ? "" : controlJson.getString("conditionVal3");390 //String type = controlJson.getString("objType");391 String controlValue = controlJson.getString("control");392 String value1 = controlJson.getString("value1");393 String value2 = controlJson.getString("value2");394 String value3 = controlJson.isNull("value3") ? "" : controlJson.getString("value3");395 String fatal = controlJson.getString("fatal");396 String description = controlJson.getString("description");397 String screenshot = controlJson.getString("screenshotFileName");398 if (!delete) {399 testCaseStepActionControl.add(testCaseStepActionControlFactory.create(test, testCase, step, sequence, control, sort, conditionOperator, conditionVal1, conditionVal2, conditionVal3, controlValue, value1, value2, value3, fatal, description, screenshot));400 }401 }402 return testCaseStepActionControl;403 }404// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">405 /**406 * Handles the HTTP <code>GET</code> method.407 *408 * @param request servlet request409 * @param response servlet response410 * @throws ServletException if a servlet-specific error occurs411 * @throws IOException if an I/O error occurs412 */413 @Override414 protected void doGet(HttpServletRequest request, HttpServletResponse response)415 throws ServletException, IOException {416 try {417 processRequest(request, response);418 } catch (CerberusException ex) {419 LOG.warn(ex);420 } catch (JSONException ex) {421 LOG.warn(ex);422 }423 }424 /**425 * Handles the HTTP <code>POST</code> method.426 *427 * @param request servlet request428 * @param response servlet response429 * @throws ServletException if a servlet-specific error occurs430 * @throws IOException if an I/O error occurs431 */432 @Override433 protected void doPost(HttpServletRequest request, HttpServletResponse response)434 throws ServletException, IOException {435 try {436 processRequest(request, response);437 } catch (CerberusException ex) {438 LOG.warn(ex);439 } catch (JSONException ex) {440 LOG.warn(ex);441 }442 }443 /**444 * Returns a short description of the servlet.445 *446 * @return a String containing servlet description447 */448 @Override449 public String getServletInfo() {450 return "Short description";...

Full Screen

Full Screen

processRequest

Using AI Code Generation

copy

Full Screen

1import org.cerberus.servlet.crud.test.UpdateTestCaseWithDependencies2UpdateTestCaseWithDependencies processRequest = new UpdateTestCaseWithDependencies()3processRequest.processRequest(request, response)4import org.cerberus.servlet.crud.test.UpdateTestCaseWithDependencies5UpdateTestCaseWithDependencies processRequest = new UpdateTestCaseWithDependencies()6processRequest.processRequest(request, response)7import org.cerberus.servlet.crud.test.UpdateTestCaseWithDependencies8UpdateTestCaseWithDependencies processRequest = new UpdateTestCaseWithDependencies()9processRequest.processRequest(request, response)10import org.cerberus.servlet.crud.test.UpdateTestCaseWithDependencies11UpdateTestCaseWithDependencies processRequest = new UpdateTestCaseWithDependencies()12processRequest.processRequest(request, response)13import org.cerberus.servlet.crud.test.UpdateTestCaseWithDependencies14UpdateTestCaseWithDependencies processRequest = new UpdateTestCaseWithDependencies()15processRequest.processRequest(request, response)16import org.cerberus.servlet.crud.test.UpdateTestCaseWithDependencies17UpdateTestCaseWithDependencies processRequest = new UpdateTestCaseWithDependencies()18processRequest.processRequest(request, response)19import org.cerberus.servlet.crud.test.UpdateTestCaseWithDependencies20UpdateTestCaseWithDependencies processRequest = new UpdateTestCaseWithDependencies()21processRequest.processRequest(request, response)22import org.cerberus.servlet.crud.test.UpdateTestCaseWithDependencies23UpdateTestCaseWithDependencies processRequest = new UpdateTestCaseWithDependencies()24processRequest.processRequest(request, response)25import org.cerberus.servlet.crud.test.UpdateTestCaseWithDependencies26UpdateTestCaseWithDependencies processRequest = new UpdateTestCaseWithDependencies()27processRequest.processRequest(request, response)28import org.cerberus.servlet.crud.test.UpdateTestCaseWithDependencies29UpdateTestCaseWithDependencies processRequest = new UpdateTestCaseWithDependencies()30processRequest.processRequest(request, response)31import org.cerberus.servlet.crud.test.UpdateTestCaseWithDependencies32UpdateTestCaseWithDependencies processRequest = new UpdateTestCaseWithDependencies()33processRequest.processRequest(request, response)34import org.cerberus.servlet.crud.test.UpdateTestCaseWithDependencies35UpdateTestCaseWithDependencies processRequest = new UpdateTestCaseWithDependencies()36processRequest.processRequest(request, response)37import org.cerberus.servlet.crud.test.UpdateTestCaseWithDependencies38UpdateTestCaseWithDependencies processRequest = new UpdateTestCaseWithDependencies()39processRequest.processRequest(request, response)40import org.cerberus.servlet.crud.test.UpdateTestCaseWithDependencies41UpdateTestCaseWithDependencies processRequest = new UpdateTestCaseWithDependencies()

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Cerberus-source automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful