Best Cerberus-source code snippet using org.cerberus.util.ParamRequestMaker.size
Source:ExecutionQueueWorkerThread.java
1/**2 * Cerberus Copyright (C) 2013 - 2017 cerberustesting3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4 *5 * This file is part of Cerberus.6 *7 * Cerberus is free software: you can redistribute it and/or modify8 * it under the terms of the GNU General Public License as published by9 * the Free Software Foundation, either version 3 of the License, or10 * (at your option) any later version.11 *12 * Cerberus is distributed in the hope that it will be useful,13 * but WITHOUT ANY WARRANTY; without even the implied warranty of14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15 * GNU General Public License for more details.16 *17 * You should have received a copy of the GNU General Public License18 * along with Cerberus. If not, see <http://www.gnu.org/licenses/>.19 */20package org.cerberus.engine.queuemanagement.impl;21import java.io.UnsupportedEncodingException;22import java.net.URLEncoder;23import java.util.concurrent.Future;24import java.util.regex.Matcher;25import java.util.regex.Pattern;26import org.apache.http.HttpEntity;27import org.apache.http.client.HttpResponseException;28import org.apache.http.client.config.RequestConfig;29import org.apache.http.client.methods.CloseableHttpResponse;30import org.apache.http.client.methods.HttpGet;31import org.apache.http.impl.client.CloseableHttpClient;32import org.apache.http.impl.client.HttpClientBuilder;33import org.apache.http.util.EntityUtils;34import org.cerberus.crud.entity.TestCaseExecutionQueue;35import org.cerberus.crud.service.ITestCaseExecutionQueueService;36import org.cerberus.crud.service.ITestCaseExecutionQueueDepService;37import org.cerberus.engine.execution.IRetriesService;38import org.cerberus.exception.CerberusException;39import org.cerberus.servlet.zzpublic.RunTestCaseV001;40import org.cerberus.util.ParamRequestMaker;41import org.cerberus.util.ParameterParserUtil;42import org.cerberus.util.StringUtil;43/**44 *45 * @author bcivel46 */47public class ExecutionQueueWorkerThread implements Runnable {48 private static final org.apache.logging.log4j.Logger LOG = org.apache.logging.log4j.LogManager.getLogger(ExecutionQueueWorkerThread.class);49 private ITestCaseExecutionQueueService queueService;50 private IRetriesService retriesService;51 private ITestCaseExecutionQueueDepService queueDepService;52 private ExecutionQueueThreadPool execThreadPool;53 private long queueId;54 private String robotExecutor;55 private String selectedRobotHost;56 private String selectedRobotExtHost;57 private TestCaseExecutionQueue toExecute;58 private String cerberusExecutionUrl;59 private int toExecuteTimeout;60 private Future<?> future;61 private static final Pattern EXECUTION_ID_FROM_ANSWER_PATTERN = Pattern.compile("^id = (\\d+)$", Pattern.MULTILINE);62 private static final Pattern RETURN_CODE_DESCRIPTION_FROM_ANSWER_PATTERN = Pattern.compile("^controlMessage = (.*)$", Pattern.MULTILINE);63 public static String PARAMETER_OUTPUT_FORMAT_VALUE = "verbose-txt";64 private ParamRequestMaker makeParamRequest() {65 ParamRequestMaker paramRequestMaker = new ParamRequestMaker();66 try {67 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_TEST, URLEncoder.encode(getToExecute().getTest(), "UTF-8"));68 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_TEST_CASE, URLEncoder.encode(getToExecute().getTestCase(), "UTF-8"));69 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_COUNTRY, getToExecute().getCountry());70 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_ENVIRONMENT, getToExecute().getEnvironment());71 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_ROBOT, getToExecute().getRobot());72 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_ROBOTEXECUTOR, getRobotExecutor());73 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_ROBOT_IP, getToExecute().getRobotIP());74 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_ROBOT_PORT, getToExecute().getRobotPort());75 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_BROWSER, getToExecute().getBrowser());76 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_BROWSER_VERSION, getToExecute().getBrowserVersion());77 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_PLATFORM, getToExecute().getPlatform());78 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_SCREEN_SIZE, getToExecute().getScreenSize());79 if (getToExecute().getManualURL() >= 1) { // 1 (Activate) or 2 (Override)80 if (getToExecute().getManualURL() == 1) { // set manual url only if 1. if 2, manual url == false and, we ovveride host, contextroot, login and env data if attributs available81 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_MANUAL_URL, ParameterParserUtil.DEFAULT_BOOLEAN_TRUE_VALUE);82 }83 addIfNotNullOrEmpty(paramRequestMaker, RunTestCaseV001.PARAMETER_MANUAL_HOST, getToExecute().getManualHost(), true);84 addIfNotNullOrEmpty(paramRequestMaker, RunTestCaseV001.PARAMETER_MANUAL_CONTEXT_ROOT, getToExecute().getManualContextRoot(), true);85 addIfNotNullOrEmpty(paramRequestMaker, RunTestCaseV001.PARAMETER_MANUAL_LOGIN_RELATIVE_URL, getToExecute().getManualLoginRelativeURL(), true);86 addIfNotNullOrEmpty(paramRequestMaker, RunTestCaseV001.PARAMETER_MANUAL_ENV_DATA, getToExecute().getManualEnvData(), false);87 }88 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_TAG, URLEncoder.encode(getToExecute().getTag(), "UTF-8"));89 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_SCREENSHOT, Integer.toString(getToExecute().getScreenshot()));90 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_VERBOSE, Integer.toString(getToExecute().getVerbose()));91 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_TIMEOUT, getToExecute().getTimeout());92 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_PAGE_SOURCE, Integer.toString(getToExecute().getPageSource()));93 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_SELENIUM_LOG, Integer.toString(getToExecute().getSeleniumLog()));94 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_EXECUTION_QUEUE_ID, Long.toString(getToExecute().getId()));95 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_NUMBER_OF_RETRIES, Long.toString(getToExecute().getRetries()));96 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_EXECUTOR, getToExecute().getUsrCreated());97 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_MANUAL_EXECUTION, getToExecute().getManualExecution());98 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_OUTPUT_FORMAT, PARAMETER_OUTPUT_FORMAT_VALUE);99 paramRequestMaker.addParam(RunTestCaseV001.PARAMETER_SYNCHRONEOUS, ParameterParserUtil.DEFAULT_BOOLEAN_TRUE_VALUE);100 } catch (UnsupportedEncodingException ex) {101 LOG.error("Error when encoding string in URL : ", ex);102 }103 return paramRequestMaker;104 }105 /**106 * The associated {@link RuntimeException} for any errors during the run107 * process108 */109 public static class RunQueueProcessException extends RuntimeException {110 public RunQueueProcessException(String message) {111 super(message);112 }113 public RunQueueProcessException(String message, Throwable cause) {114 super(message, cause);115 }116 }117 public ITestCaseExecutionQueueDepService getQueueDepService() {118 return queueDepService;119 }120 public void setQueueDepService(ITestCaseExecutionQueueDepService queueDepService) {121 this.queueDepService = queueDepService;122 }123 public String getRobotExecutor() {124 return robotExecutor;125 }126 public void setRobotExecutor(String robotExecutor) {127 this.robotExecutor = robotExecutor;128 }129 public String getSelectedRobotHost() {130 return selectedRobotHost;131 }132 public void setSelectedRobotHost(String selectedRobotHost) {133 this.selectedRobotHost = selectedRobotHost;134 }135 public String getSelectedRobotExtHost() {136 return selectedRobotExtHost;137 }138 public void setSelectedRobotExtHost(String selectedRobotExtHost) {139 this.selectedRobotExtHost = selectedRobotExtHost;140 }141 public TestCaseExecutionQueue getToExecute() {142 return toExecute;143 }144 private void setToExecute(TestCaseExecutionQueue toExecute) {145 this.toExecute = toExecute;146 }147 public ITestCaseExecutionQueueService getQueueService() {148 return queueService;149 }150 public void setQueueService(ITestCaseExecutionQueueService queueService) {151 this.queueService = queueService;152 }153 public void setRetriesService(IRetriesService retriesService) {154 this.retriesService = retriesService;155 }156 public void setCerberusExecutionUrl(String url) {157 this.cerberusExecutionUrl = url;158 }159 public void setQueueId(long queueId) {160 this.queueId = queueId;161 }162 public void setExecThreadPool(ExecutionQueueThreadPool etp) {163 this.execThreadPool = etp;164 }165 public void setFuture(Future<?> future) {166 this.future = future;167 }168 public int getToExecuteTimeout() {169 return toExecuteTimeout;170 }171 public void setToExecuteTimeout(int toExecuteTimeout) {172 this.toExecuteTimeout = toExecuteTimeout;173 }174 @Override175 public void run() {176 try {177 LOG.debug("Start to execute : " + queueId + " with RobotHost : " + selectedRobotHost+ " with RobotExtensionHost : " + selectedRobotExtHost);178 // Flag the queue entry to STARTING179 queueService.updateToStarting(queueId, selectedRobotHost, selectedRobotExtHost);180 LOG.debug("Get queue exe to execute : " + queueId);181 // Getting the queue full object.182 setToExecute(queueService.convert(queueService.readByKey(queueId, false)));183 StringBuilder url = new StringBuilder();184 url.append(cerberusExecutionUrl);185 url.append(RunTestCaseV001.SERVLET_URL);186 url.append("?");187 url.append(makeParamRequest().mkString().replace(" ", "+"));188 LOG.debug("Make http call : " + queueId);189 // Make the http call and parse the output.190 runParseAnswer(runExecution(url), cerberusExecutionUrl + RunTestCaseV001.SERVLET_URL, url.toString());191 } catch (Exception e) {192 LOG.warn("Execution in queue " + queueId + " has finished with error");193 try {194 queueService.updateToError(queueId, e.getMessage());195 queueDepService.manageDependenciesEndOfQueueExecution(queueId);196 } catch (CerberusException again) {197 LOG.error("Unable to mark execution in queue " + queueId + " as in error", again);198 }199 }200 }201 /**202 * Request execution of the inner {@link TestCaseExecutionQueue} to the203 * {@link RunTestCase} servlet204 *205 * @return the execution answer from the {@link RunTestCase} servlet206 * @throws RunQueueProcessException if an error occurred during request207 * execution208 * @see #run()209 */210 private String runExecution(StringBuilder url) {211 try {212 LOG.debug("Trigger Execution to URL : " + url.toString());213 LOG.debug("Trigger Execution with TimeOut : " + toExecuteTimeout);214 CloseableHttpClient httpclient = HttpClientBuilder.create().disableAutomaticRetries().build();215 RequestConfig requestConfig = RequestConfig.custom()216 .setConnectTimeout(toExecuteTimeout)217 .setConnectionRequestTimeout(toExecuteTimeout)218 .setSocketTimeout(toExecuteTimeout)219 .build();220 HttpGet httpGet = new HttpGet(url.toString());221 httpGet.setConfig(requestConfig);222 CloseableHttpResponse response = httpclient.execute(httpGet);223 HttpEntity entity = response.getEntity();224 String responseContent = EntityUtils.toString(entity);225 return responseContent;226 } catch (Exception e) {227 final StringBuilder errorMessage = new StringBuilder("An unexpected error occurred during test case execution: ");228 if (e instanceof HttpResponseException) {229 errorMessage.append(String.format("%d (%s)", ((HttpResponseException) e).getStatusCode(), e.getMessage()));230 } else {231 errorMessage.append(e.getMessage());232 errorMessage.append(". Check server logs");233 }234 LOG.error(errorMessage.toString(), e);235 throw new RunQueueProcessException(errorMessage.toString(), e);236 }237 }238 /**239 * Parse the answer given by the {@link RunTestCase}240 * <p>241 * @param answer the {@link RunTestCase}'s answer242 * @throws RunQueueProcessException if an error occurred if execution was on243 * failure or if answer cannot be parsed244 * @see #run()245 */246 private void runParseAnswer(String answer, String cerberusUrl, String cerberusFullUrl) {247 // Check answer format248 Matcher matcher = EXECUTION_ID_FROM_ANSWER_PATTERN.matcher(answer);249 if (!matcher.find()) {250 LOG.error("Bad answer format (could not find 'RunID = '). URL Called: " + cerberusFullUrl);251 LOG.error("Bad answer format (could not find 'RunID = '). Answer: " + answer);252 throw new RunQueueProcessException("Error occured when calling the service to run the testcase. Service answer did not have the expected format (missing 'RunID = '). Probably due to bad cerberus_url value. URL Called: '" + cerberusUrl + "'.");253 }254 // Extract the return code255 Long executionID;256 try {257 executionID = Long.parseLong(matcher.group(1));258 } catch (NumberFormatException e) {259 LOG.error("Bad answer format (executionId is not numeric). Answer: " + answer);260 throw new RunQueueProcessException("Bad return code format: " + matcher.group(1));261 }262 // Check if return code is in error263 if (executionID == 0) {264 Matcher descriptionMatcher = RETURN_CODE_DESCRIPTION_FROM_ANSWER_PATTERN.matcher(answer);265 if (!descriptionMatcher.find()) {266 LOG.error("Bad answer format (could not find 'ReturnCodeDescription = '). URL Called: " + cerberusFullUrl);267 LOG.error("Bad answer format (could not find 'ReturnCodeDescription = '). Answer: " + answer);268 throw new RunQueueProcessException("Error occured when calling the service to run the testcase. Service answer did not have the expected format (missing 'ReturnCodeDescription = '). Probably due to bad cerberus_url value. URL Called: '" + cerberusUrl + "'.");269 }270 throw new RunQueueProcessException(descriptionMatcher.group(1));271 }272 }273 @Override274 public String toString() {275 return this.cerberusExecutionUrl;276 }277 private void addIfNotNullOrEmpty(ParamRequestMaker paramRequestMaker, String key, String value, boolean encode) throws UnsupportedEncodingException {278 if (!StringUtil.isNullOrEmpty(value)) {279 paramRequestMaker.addParam(key, encode ? URLEncoder.encode(value, "UTF-8") : value);280 }281 }282}...
size
Using AI Code Generation
1import org.cerberus.util.ParamRequestMaker2ParamRequestMaker prm = new ParamRequestMaker()3List<String> paramList = new ArrayList<String>()4paramList.add("param1")5paramList.add("param2")6paramList.add("param3")7paramList.add("param4")8prm.size(paramList)9prm.size("param1","param2","param3","param4")10prm.size("param1","param2","param3","param4","param5")11prm.size("param1","param2","param3","param4","param5","param6")12prm.size("param1","param2","param3","param4","param5","param6","param7")13prm.size("param1","param2","param3","param4","param5","param6","param7","param8")14prm.size("param1","param2","param3","param4","param5","param6","param7","param8","param9")15prm.size("param1","param2","param3","param4","param5","param6","param7","param8","param9","param10")16prm.size("param1","param2","param3","param4","param5","param6","param7","param8","param9","param10","param11")17prm.size("param1","param2","param3","param4","param5","param6","param7","param8","param9","param10","param11","param12")
size
Using AI Code Generation
1import org.cerberus.util.ParamRequestMaker;2import org.cerberus.util.ParamRequest;3ParamRequestMaker prm = new ParamRequestMaker();4ParamRequest pr = prm.makeParamRequest();5int size = pr.size();6System.out.println("Size of ParamRequest object: " + size);
size
Using AI Code Generation
1ParamRequestMaker prm = new ParamRequestMaker();2List<String> list = prm.getParamsList();3int size = prm.size();4System.out.println("size: " + size);5System.out.println("list: " + list);6for (String param : list) {7 System.out.println("param: " + param);8}9for (int i = 0; i < size; i++) {10 System.out.println("param: " + prm.getParam(i));11}12for (int i = 0; i < size; i++) {13 System.out.println("param: " + prm.getParam(list.get(i)));14}15for (int i = 0; i < size; i++) {16 System.out.println("param: " + prm.getParam(list.get(i), ""));17}18for (int i = 0; i < size; i++) {19 System.out.println("param: " + prm.getParam(list.get(i), "", ""));20}21for (int i = 0; i < size; i++) {22 System.out.println("param: " + prm.getParam(list.get(i), "", "", ""));23}24for (int i = 0; i < size; i++) {25 System.out.println("param: " + prm.getParam(list.get(i), "", "", "", ""));26}27for (int i = 0; i < size; i++) {28 System.out.println("param: " + prm.getParam(list.get(i), "", "", "", "", ""));29}30for (int i = 0; i < size; i++) {31 System.out.println("param: " + prm.getParam(list.get(i), "", "", "", "", "", ""));32}33for (int i = 0; i < size; i++) {
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!!