Best Cerberus-source code snippet using org.cerberus.servlet.crud.test.ReadTestCaseStat.processRequest
Source:ReadTestCaseStat.java
...85 * @throws ServletException if a servlet-specific error occurs86 * @throws IOException if an I/O error occurs87 * @throws org.cerberus.exception.CerberusException88 */89 protected void processRequest(HttpServletRequest request, HttpServletResponse response)90 throws ServletException, IOException, CerberusException {91 String echo = request.getParameter("sEcho");92 ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());93 PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);94 response.setContentType("application/json");95 response.setCharacterEncoding("utf8");96 // Calling Servlet Transversal Util.97 ServletUtil.servletStart(request);98 // Default message to unexpected error.99 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);100 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));101 /**102 * Parsing and securing all required parameters.103 */104 factoryTestCase = appContext.getBean(IFactoryTestCase.class);105 testCaseService = appContext.getBean(ITestCaseService.class);106 List<String> system = ParameterParserUtil.parseListParam(request.getParameterValues("system"), new ArrayList<String>(), "UTF8");107 // Init Answer with potencial error from Parsing parameter.108 AnswerItem<JSONObject> answer = new AnswerItem<>(msg);109 String from = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("from"), null, "UTF8");110 String to = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("to"), null, "UTF8");111 LOG.debug("from : " + from);112 LOG.debug("to : " + to);113 Date fromD;114 try {115 TimeZone tz = TimeZone.getTimeZone("UTC");116 DateFormat df = new SimpleDateFormat(DATE_FORMAT);117 df.setTimeZone(tz);118 fromD = df.parse(from);119 } catch (ParseException ex) {120 fromD = Date.from(ZonedDateTime.now().minusMonths(1).toInstant());121 LOG.debug("Exception when parsing date", ex);122 }123 Date toD;124 try {125 TimeZone tz = TimeZone.getTimeZone("UTC");126 DateFormat df = new SimpleDateFormat(DATE_FORMAT);127 df.setTimeZone(tz);128 toD = df.parse(to);129 } catch (ParseException ex) {130 toD = Date.from(ZonedDateTime.now().toInstant());131 LOG.debug("Exception when parsing date", ex);132 }133 LOG.debug("from : " + fromD);134 LOG.debug("to : " + toD);135 List<TestCase> tescaseL = testCaseService.convert(testCaseService.readStatsBySystem(system, toD));136 try {137 JSONObject jsonResponse = new JSONObject();138 answer = findTCStatList(appContext, request, tescaseL, fromD);139 jsonResponse = (JSONObject) answer.getItem();140 jsonResponse.put("messageType", answer.getResultMessage().getMessage().getCodeString());141 jsonResponse.put("message", answer.getResultMessage().getDescription());142 jsonResponse.put("sEcho", echo);143 response.getWriter().print(jsonResponse.toString());144 } catch (JSONException e) {145 LOG.warn(e);146 //returns a default error message with the json format that is able to be parsed by the client-side147 response.getWriter().print(AnswerUtil.createGenericErrorAnswer());148 }149 }150 private AnswerItem<JSONObject> findTCStatList(ApplicationContext appContext, HttpServletRequest request, List<TestCase> tagExeList, Date startingDay) throws JSONException {151 // Default message to unexpected error.152 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);153 msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCase").replace("%OPERATION%", "Read"));154 AnswerItem<JSONObject> item = new AnswerItem<>(msg);155 JSONObject object = new JSONObject();156 testCaseExecutionHttpStatService = appContext.getBean(ITestCaseExecutionHttpStatService.class);157 applicationService = appContext.getBean(IApplicationService.class);158 testCaseService = appContext.getBean(ITestCaseService.class);159 factoryTestCase = appContext.getBean(IFactoryTestCase.class);160 HashMap<String, JSONArray> curveMap = new HashMap<>();161 HashMap<String, JSONObject> curveObjMap = new HashMap<>();162 String curveKey = "";163 JSONArray curArray = new JSONArray();164 JSONObject curveObj = new JSONObject();165 JSONObject pointObj = new JSONObject();166 HashMap<String, JSONObject> curveStatusObjMap = new HashMap<>();167 List<String> curveBarMap = new ArrayList<>();168 HashMap<String, Tag> curveTagObjMap = new HashMap<>();169 HashMap<String, List<Integer>> curveTagObjValMap = new HashMap<>();170 HashMap<String, Integer> curveTagObjValTotMap = new HashMap<>();171 TreeMap<String, Boolean> curveDateMap = new TreeMap<>();172 TreeMap<String, Integer> curveDateStatusMap = new TreeMap<>();173 HashMap<String, Integer> curveStatusMap = new HashMap<>();174 String curveKeyStatus = "";175 JSONObject curveStatObj = new JSONObject();176 HashMap<String, Boolean> statList = new HashMap<>();177 HashMap<String, Boolean> statDateList = new HashMap<>();178 TimeZone tz = TimeZone.getTimeZone("UTC");179 DateFormat df = new SimpleDateFormat(DATE_FORMAT_DAY);180 df.setTimeZone(tz);181 for (TestCase exeCur : tagExeList) {182 statList.put(exeCur.getStatus(), false);183 Date d = new Date(exeCur.getDateCreated().getTime());184 if (d.before(startingDay)) {185 d = startingDay;186 }187 statDateList.put(df.format(d), false);188 }189 for (TestCase exeCur : tagExeList) {190 /**191 * Bar Charts per control status.192 */193 for (Map.Entry<String, Boolean> entry : statList.entrySet()) {194 String key = entry.getKey();195 if (key.equals(exeCur.getStatus())) {196 for (Map.Entry<String, Boolean> entryDate : statDateList.entrySet()) {197 String keyDate = entryDate.getKey();198 curveKeyStatus = key.toUpperCase();199 Date d = new Date(exeCur.getDateCreated().getTime());200 if (d.before(startingDay)) {201 d = startingDay;202 }203 df.setTimeZone(tz);204 String dday = df.format(d);205 curveDateMap.put(dday, false);206 int x = 0;207 if (keyDate.compareTo(dday) >= 0) {208 x = 1;209 }210 String keyDateStatus = curveKeyStatus + "-" + keyDate;211 if (curveDateStatusMap.containsKey(keyDateStatus)) {212 curveDateStatusMap.put(keyDateStatus, curveDateStatusMap.get(keyDateStatus) + x);213 } else {214 curveDateStatusMap.put(keyDateStatus, x);215 }216 if (!curveStatusObjMap.containsKey(curveKeyStatus)) {217 curveStatObj = new JSONObject();218 curveStatObj.put("key", curveKeyStatus);219 curveStatObj.put("unit", "nbTC");220 curveStatusObjMap.put(curveKeyStatus, curveStatObj);221 }222 }223 }224 }225 }226 /**227 * Bar Charts per control status to JSON.228 */229 JSONArray curvesArray = new JSONArray();230 for (Map.Entry<String, Boolean> entry : curveDateMap.entrySet()) {231 curvesArray.put(entry.getKey());232 }233 object.put("curvesDatesNb", curvesArray);234 curvesArray = new JSONArray();235 for (Map.Entry<String, JSONObject> entry : curveStatusObjMap.entrySet()) {236 String key = entry.getKey();237 JSONObject val = entry.getValue();238 JSONArray valArray = new JSONArray();239 for (Map.Entry<String, Boolean> entry1 : curveDateMap.entrySet()) {240 String key1 = entry1.getKey(); // Date241 if (curveDateStatusMap.containsKey(key + "-" + key1)) {242 valArray.put(curveDateStatusMap.get(key + "-" + key1));243 } else {244 valArray.put(0);245 }246 }247 JSONObject localcur = new JSONObject();248 localcur.put("key", val);249 localcur.put("points", valArray);250 curvesArray.put(localcur);251 }252 object.put("curvesNb", curvesArray);253 item.setItem(object);254 return item;255 }256 257 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">258 /**259 * Handles the HTTP <code>GET</code> method.260 *261 * @param request servlet request262 * @param response servlet response263 * @throws ServletException if a servlet-specific error occurs264 * @throws IOException if an I/O error occurs265 */266 @Override267 protected void doGet(HttpServletRequest request, HttpServletResponse response)268 throws ServletException, IOException {269 try {270 processRequest(request, response);271 } catch (CerberusException ex) {272 LOG.warn(ex);273 }274 }275 /**276 * Handles the HTTP <code>POST</code> method.277 *278 * @param request servlet request279 * @param response servlet response280 * @throws ServletException if a servlet-specific error occurs281 * @throws IOException if an I/O error occurs282 */283 @Override284 protected void doPost(HttpServletRequest request, HttpServletResponse response)285 throws ServletException, IOException {286 try {287 processRequest(request, response);288 } catch (CerberusException ex) {289 LOG.warn(ex);290 }291 }292 /**293 * Returns a short description of the servlet.294 *295 * @return a String containing servlet description296 */297 @Override298 public String getServletInfo() {299 return "Short description";300 }// </editor-fold>301}...
processRequest
Using AI Code Generation
1import org.cerberus.servlet.crud.test.ReadTestCaseStat;2import org.cerberus.util.StringUtil;3import org.cerberus.util.answer.AnswerItem;4import org.cerberus.util.answer.AnswerList;5import org.cerberus.util.answer.AnswerUtil;6import org.json.JSONArray;7import org.json.JSONObject;8import java.util.List;9import java.util.Map;10import static spark.Spark.get;11import static spark.Spark.port;12public class TestCasesStat {13 public static void main(String[] args) {14 port(8080);15 get("/testcases", (request, response) -> {16 AnswerItem<Map<String, List<String>>> answer = ReadTestCaseStat.processRequest(request.queryMap().toMap());17 if (answer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {18 Map<String, List<String>> map = answer.getItem();19 JSONArray jsonArray = new JSONArray();20 for (String key : map.keySet()) {21 JSONObject jsonObject = new JSONObject();22 jsonObject.put("key", key);23 jsonObject.put("value", StringUtil.join(map.get(key), ","));24 jsonArray.put(jsonObject);25 }26 return jsonArray.toString();27 } else {28 return AnswerUtil.createGenericError(answer);29 }30 });31 }32}
processRequest
Using AI Code Generation
1public class ReadTestCaseStat extends HttpServlet {2 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {3 JSONObject result = processRequest(request);4 response.setContentType("application/json");5 response.getWriter().print(result.toString());6 }7 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {8 doGet(request, response);9 }10 public String getServletInfo() {11 return "Short description";12 protected JSONObject processRequest(HttpServletRequest request) {13 JSONObject jsonResponse = new JSONObject();14 try {15 List<TestCaseStat> testCaseStatList = testCaseStatService.readByVariousByCriteria(request.getParameter("test"), request.getParameter("testcase"), request.getParameter("country"), request.getParameter("environment"), request.getParameter("tag"), request.getParameter("active"), request.getParameter("inactive"), request.getParameter("bugId"));16 JSONArray jsonArray = new JSONArray();17 for (TestCaseStat testCaseStat : testCaseStatList) {18 jsonArray.put(convertTestCaseStatToJSONObject(testCaseStat));19 }20 jsonResponse.put("contentTable", jsonArray);21 jsonResponse.put("messageType", "OK");22 jsonResponse.put("message", "");23 } catch (JSONException ex) {24 LOG.error(ex.toString(), ex);25 try {26 jsonResponse.put("messageType", "KO");27 jsonResponse.put("message", ex.toString());28 } catch (JSONException ex1) {29 LOG.error(ex1.toString(), ex1);30 }31 }32 return jsonResponse;33 }34 private JSONObject convertTestCaseStatToJSONObject(TestCaseStat testCaseStat) throws JSONException {35 JSONObject result = new JSONObject();36 result.put("Test", testCaseStat.getTest());37 result.put("TestCase", testCaseStat.getTestCase());38 result.put("Country", testCaseStat.getCountry());39 result.put("Environment", testCaseStat.getEnvironment());40 result.put("Tag", testCaseStat.getTag());41 result.put("Active", testCaseStat.getActive());42 result.put("Inactive", testCaseStat.getInactive());43 result.put("BugID", testCaseStat.getBugId());44 return result;45 }46}
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!!