Best Cerberus-source code snippet using org.cerberus.util.VersionComparator
Source:ImportTestCase.java
...43import org.cerberus.crud.service.ITestCaseService;44import org.cerberus.engine.entity.MessageEvent;45import org.cerberus.enums.MessageEventEnum;46import org.cerberus.exception.CerberusException;47import org.cerberus.util.VersionComparator;48import org.cerberus.util.answer.Answer;49import org.cerberus.util.answer.AnswerUtil;50import org.cerberus.version.Infos;51import org.json.JSONException;52import org.json.JSONObject;53import org.springframework.context.ApplicationContext;54import org.springframework.web.context.support.WebApplicationContextUtils;55/**56 *57 * @author bcivel58 */59public class ImportTestCase extends HttpServlet {60 private static final Logger LOG = LogManager.getLogger(ImportTestCase.class);61 /**62 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>63 * methods.64 *65 * @param httpServletRequest66 * @param httpServletResponse67 * @throws ServletException if a servlet-specific error occurs68 * @throws IOException if an I/O error occurs69 */70 protected void processRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)71 throws ServletException, IOException {72 JSONObject jsonResponse = new JSONObject();73 try {74 try {75 ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());76 ITestCaseService tcService = appContext.getBean(ITestCaseService.class);77 Answer ans = new Answer();78 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);79 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));80 ans.setResultMessage(msg);81 ///Get files82 List<String> files = getFiles(httpServletRequest);83 // Prepare the final answer.84 MessageEvent msg1 = new MessageEvent(MessageEventEnum.GENERIC_OK);85 Answer finalAnswer = new Answer(msg1);86 for (String fileContent : files) {87 JSONObject json = new JSONObject(fileContent);88 if (isCompatible(json)) {89 90 //Remove attribute not in the Object91 json.remove("cerberus_version");92 json.remove("user");93 ObjectMapper mapper = new ObjectMapper();94 TestCase tcInfo = mapper.readValue(json.toString(), TestCase.class);95 try {96 tcService.importWithDependency(tcInfo);97 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);98 msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCase " + tcInfo.getTest() + " - " + tcInfo.getTestCase())99 .replace("%OPERATION%", "Import"));100 ans.setResultMessage(msg);101 finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);102 } catch (CerberusException ex) {103 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);104 msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCase " + tcInfo.getTest() + " - " + tcInfo.getTestCase())105 .replace("%OPERATION%", "Import")106 .replace("%REASON%", ex.getMessageError().getDescription()));107 ans.setResultMessage(msg);108 finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);109 }110 } else {111 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);112 msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCase ")113 .replace("%OPERATION%", "Import")114 .replace("%REASON%", "File you're trying to import is not supported or in a compatible version."));115 ans.setResultMessage(msg);116 finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);117 }118 }119 jsonResponse.put("messageType", finalAnswer.getResultMessage().getMessage().getCodeString());120 jsonResponse.put("message", finalAnswer.getResultMessage().getDescription());121 } catch (JSONException ex) {122 jsonResponse.put("messageType", MessageEventEnum.GENERIC_ERROR.getCodeString());123 jsonResponse.put("message", MessageEventEnum.GENERIC_ERROR.getDescription());124 }125 } catch (JSONException e) {126 LOG.warn(e);127 //returns a default error message with the json format that is able to be parsed by the client-side128 httpServletResponse.setContentType("application/json");129 httpServletResponse.getWriter().print(AnswerUtil.createGenericErrorAnswer());130 }131 httpServletResponse.getWriter().print(jsonResponse.toString());132 }133 public static JSONObject parseJSONFile(String filename) throws JSONException, IOException {134 String content = new String(Files.readAllBytes(Paths.get(filename)));135 return new JSONObject(content);136 }137 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">138 /**139 * Handles the HTTP <code>GET</code> method.140 *141 * @param request servlet request142 * @param response servlet response143 * @throws ServletException if a servlet-specific error occurs144 * @throws IOException if an I/O error occurs145 */146 @Override147 protected void doGet(HttpServletRequest request, HttpServletResponse response)148 throws ServletException, IOException {149 processRequest(request, response);150 }151 /**152 * Handles the HTTP <code>POST</code> method.153 *154 * @param request servlet request155 * @param response servlet response156 * @throws ServletException if a servlet-specific error occurs157 * @throws IOException if an I/O error occurs158 */159 @Override160 protected void doPost(HttpServletRequest request, HttpServletResponse response)161 throws ServletException, IOException {162 processRequest(request, response);163 }164 /**165 * Returns a short description of the servlet.166 *167 * @return a String containing servlet description168 */169 @Override170 public String getServletInfo() {171 return "Short description";172 }// </editor-fold>173 private List<String> getFiles(HttpServletRequest httpServletRequest) {174 List<String> result = new ArrayList<>();175 try {176 if (ServletFileUpload.isMultipartContent(httpServletRequest)) {177 DiskFileItemFactory factory = new DiskFileItemFactory();178 ServletContext servletContext = this.getServletConfig().getServletContext();179 File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");180 factory.setRepository(repository);181 ServletFileUpload upload = new ServletFileUpload(factory);182 List<FileItem> formItems = upload.parseRequest(httpServletRequest);183 System.out.println(formItems.size());184 if (formItems != null && formItems.size() > 0) {185 System.out.println(formItems.toString());186 for (FileItem item : formItems) {187 if (!item.isFormField()) {188 result.add(item.getString());189 }190 }191 }192 }193 } catch (FileUploadException ex) {194 java.util.logging.Logger.getLogger(ImportTestCase.class.getName()).log(Level.SEVERE, null, ex);195 }196 return result;197 }198 private boolean isCompatible(JSONObject json) {199 try {200 String fileVersion = json.getString("cerberus_version");201 String projectVersion = Infos.getInstance().getProjectVersion();202 LOG.info(fileVersion);203 LOG.info(projectVersion);204 205 //Compatibility Matrix. To update if testcase (including dependencies) model change.206 Map<String, String> compatibilityMatrix = new HashMap<>();207 compatibilityMatrix.put("1.0", "4.0");208 compatibilityMatrix.put("4.1", "100.0");209 210 //Check fileVersion and projectVersion are in the same rank in the compatibility Matrix211 for (Map.Entry<String,String> entry : compatibilityMatrix.entrySet()){ 212 LOG.info("File - Key : " + VersionComparator.compare(fileVersion, entry.getKey()));213 LOG.info("File - Value : " + VersionComparator.compare(fileVersion, entry.getValue()));214 LOG.info("Project - Key : " + VersionComparator.compare(projectVersion, entry.getKey()));215 LOG.info("Project - Value : " + VersionComparator.compare(projectVersion, entry.getValue()));216 if(VersionComparator.compare(fileVersion, entry.getKey())*VersionComparator.compare(fileVersion, entry.getValue())<0){217 return VersionComparator.compare(projectVersion, entry.getKey())*VersionComparator.compare(projectVersion, entry.getValue())<0;218 }219 }220 return false;221 222 } catch (JSONException ex) {223 LOG.warn(ex);224 return false;225 }226 }227 228 229}...
VersionComparator
Using AI Code Generation
1String version1 = "1.0.0";2String version2 = "1.0.1";3VersionComparator versionComparator = new VersionComparator();4int result = versionComparator.compare(version1, version2);5if(result < 0){6}7else if(result > 0){8}9else{10}11result = versionComparator.compare(version1, version1);12if(result < 0){13}14else if(result > 0){15}16else{17}18result = versionComparator.compare(version2, version1);19if(result < 0){20}21else if(result > 0){22}23else{24}
VersionComparator
Using AI Code Generation
1String currentVersion = appService.GetSystemVersion();2String versionToCompare = "1.1.10";3VersionComparator versionComparator = new VersionComparator();4int result = versionComparator.compare(currentVersion, versionToCompare);5if (result == 0) {6 out.println("The 2 versions are equal");7} else if (result == -1) {8 out.println("The 1st version is older than the 2nd version");9} else if (result == 1) {10 out.println("The 1st version is newer than the 2nd version");11} else if (result == -2) {12 out.println("The 2 versions are not comparable");13}
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!!