Best Testsigma code snippet using com.testsigma.service.TestCaseService.handlePreRequisiteChange
Source:TestCaseService.java
...177 if (testCaseRequest.getTags() != null) {178 tagService.updateTags(testCaseRequest.getTags(), TagType.TEST_CASE, testCase.getId());179 }180 if (testCase.getPreRequisite() != null && !testCase.getPreRequisite().equals(oldPreRequisite)){181 testSuiteService.handlePreRequisiteChange(testCase);182 }183 return testCase;184 }185 private void validatePreRequisiteIsValid(TestCase testCase, List<Long> preReqList) throws TestsigmaException {186 Long preRequisiteId = testCase.getPreRequisite();187 if (preRequisiteId != null) {188 if (preReqList.size() > 5) {189 log.debug("Testcase Prerequisite hierarchy is more than 5,Prerequisite IDs:" + preReqList);190 throw new TestsigmaException("Prerequisite hierarchy crossed the allowed limit of 5");191 } else if (preReqList.contains(testCase.getPreRequisite())) {192 log.debug("Cyclic dependency for Testsuite prerequisites found for Testsuite:" + testCase);193 throw new TestsigmaException("Prerequisite to the TestCase is not valid. This prerequisite causes cyclic dependencies for TestCase.");194 }195 preReqList.add(preRequisiteId);196 TestCase preRequisiteTestCase = find(preRequisiteId);197 if (preRequisiteTestCase.getPreRequisite() != null) {198 validatePreRequisiteIsValid(preRequisiteTestCase, preReqList);199 }200 } else {201 return;202 }203 }204 //TODO:need to revisit this code[chandra]205 private void setStatusTimeAndBy(TestCaseRequest testCaseRequest, TestCase testcase) throws ResourceNotFoundException, TestsigmaDatabaseException, SQLException {206 TestCaseStatus status = testCaseRequest.getStatus();207 Timestamp at = new Timestamp(System.currentTimeMillis());208 if (status.equals(TestCaseStatus.DRAFT)) {209 testCaseRequest.setDraftAt(at);210 } else if (status.equals(TestCaseStatus.IN_REVIEW)) {211 if (!testcase.getStatus().equals(TestCaseStatus.IN_REVIEW)) {212 testCaseRequest.setReviewSubmittedAt(at);213 }214 } else if (status.equals(TestCaseStatus.READY)) {215 if (testcase.getStatus().equals(TestCaseStatus.IN_REVIEW)) {216 testCaseRequest.setReviewedAt(at);217 }218 } else if (status.equals(TestCaseStatus.OBSOLETE)) {219 testCaseRequest.setObsoleteAt(at);220 }221 }222 public Integer markAsDelete(List<Long> ids) {223 return testCaseRepository.markAsDelete(ids);224 }225 public void restore(Long id) {226 testCaseRepository.markAsRestored(id);227 }228 public void destroy(Long id) throws ResourceNotFoundException {229 TestCase testcase = this.find(id);230 testCaseRepository.delete(testcase);231 publishEvent(testcase, EventType.DELETE);232 }233 public Long automatedCountByVersion(Long versionId) {234 return this.testCaseRepository.countByVersion(versionId);235 }236 public Long testCaseCountByPreRequisite(Long testCaseId){237 return this.testCaseRepository.countByPreRequisite(testCaseId);238 }239 public List<Long> getTestCaseIdsByPreRequisite(Long testCaseId){240 return this.testCaseRepository.getTestCaseIdsByPreRequisite(testCaseId);241 }242 public List<TestCaseStatusBreakUpDTO> breakUpByStatus(Long versionId) {243 return this.testCaseRepository.breakUpByStatus(versionId);244 }245 public List<TestCaseTypeBreakUpDTO> breakUpByType(Long versionId) {246 return this.testCaseRepository.breakUpByType(versionId);247 }248 public TestCase copy(TestCaseCopyRequest testCaseRequest) throws ResourceNotFoundException, SQLException {249 TestCase parentCase = this.find(testCaseRequest.getTestCaseId());250 TestCase testCase = this.testCaseMapper.copy(parentCase);251 testCase.setStatus(parentCase.getStatus());252 testCase.setName(testCaseRequest.getName());253 testCase.setCreatedDate(new Timestamp(Calendar.getInstance().getTimeInMillis()));254 testCase.setLastRunId(null);255 if (testCaseRequest.getIsStepGroup()) {256 testCase.setTestDataStartIndex(null);257 testCase.setTestDataId(null);258 testCase.setIsDataDriven(false);259 }260 testCase.setIsStepGroup(testCaseRequest.getIsStepGroup());261 testCase.setCopiedFrom(parentCase.getId());262 testCase.setPreRequisiteCase(null);263 testCase = create(testCase);264 List<String> tags = tagService.list(TagType.TEST_CASE, parentCase.getId());265 tagService.updateTags(tags, TagType.TEST_CASE, testCase.getId());266 List<TestStep> steps = this.fetchTestSteps(parentCase, testCaseRequest.getStepIds());267 if (steps.size()>0) {268 List<TestStep> newSteps = new ArrayList<>();269 Map<Long, TestStep> parentStepIds = new HashMap<Long, TestStep>();270 Integer position = 0;271 TestStep firstStep = steps.get(0);272 if (firstStep.getConditionType() == TestStepConditionType.LOOP_WHILE) {273 TestStep whileStep = this.testStepService.find(firstStep.getParentId());274 steps.add(0, whileStep);275 }276 for (TestStep parent : steps) {277 if (testCase.getIsStepGroup() && parent.getStepGroupId() != null)278 continue;279 TestStep step = this.testStepMapper.copy(parent);280 step.setPosition(position);281 step.setCreatedDate(new Timestamp(Calendar.getInstance().getTimeInMillis()));282 step.setTestCaseId(testCase.getId());283 TestStep parentStep = parentStepIds.get(parent.getParentId());284 step.setParentId(parentStep != null ? parentStep.getId() : null);285 TestStep prerequiste = parentStepIds.get(parentStep != null ? parent.getPreRequisiteStepId() : null);286 step.setPreRequisiteStepId(prerequiste != null ? prerequiste.getId() : null);287 step.setId(null);288 TestStep testDataProfileStep = parentStepIds.get(parentStep != null ? step.getTestDataProfileStepId() : null);289 if(testDataProfileStep != null)290 step.setTestDataProfileStepId(testDataProfileStep.getId());291 step.setParentStep(parentStep);292 step = this.testStepService.create(step);293 parentStepIds.put(parent.getId(), step);294 newSteps.add(step);295 position++;296 }297 if(testCaseRequest.getIsReplace() != null && testCaseRequest.getIsReplace().booleanValue()) {298 createAndReplace(steps, testCase, parentCase);299 }300 }301 return testCase;302 }303 private void createAndReplace(List<TestStep> steps, TestCase testCase, TestCase currentTestCase) throws ResourceNotFoundException, SQLException {304 TestStep step = new TestStep();305 step.setPosition(steps.get(0).getPosition());306 step.setTestCaseId(currentTestCase.getId());307 step.setParentId(steps.get(0).getParentId() != null ? steps.get(0).getParentId() : null);308 step.setType(TestStepType.STEP_GROUP);309 step.setStepGroupId(testCase.getId());310 step.setId(null);311 if (step.getConditionType() != null && TestStepConditionType.CONDITION_ELSE_IF.equals(step.getConditionType())312 && step.getParentId() == null) {313 step.setConditionType(TestStepConditionType.CONDITION_IF);314 }315 this.testStepService.create(step);316 for (TestStep parent : steps) {317 TestStep destroyStep = this.testStepService.find(parent.getId());318 this.testStepService.destroy(destroyStep);319 }320 }321 private List<TestStep> fetchTestSteps(TestCase testCase, List<Long> stepIds) {322 if (stepIds != null)323 return this.testStepService.findAllByTestCaseIdAndIdIn(testCase.getId(), stepIds);324 else325 return this.testStepService.findAllByTestCaseId(testCase.getId());326 }327 public void publishEvent(TestCase testCase, EventType eventType) {328 TestCaseEvent<TestCase> event = createEvent(testCase, eventType);329 log.info("Publishing event - " + event.toString());330 applicationEventPublisher.publishEvent(event);331 }332 public TestCaseEvent<TestCase> createEvent(TestCase testCase, EventType eventType) {333 TestCaseEvent<TestCase> event = new TestCaseEvent<>();334 event.setEventData(testCase);335 event.setEventType(eventType);336 return event;337 }338 public void export(BackupDTO backupDTO) throws IOException, ResourceNotFoundException {339 if (!backupDTO.getIsTestCaseEnabled()) return;340 log.debug("backup process for testcase initiated");341 writeXML("testcases", backupDTO, PageRequest.of(0, 25));342 log.debug("backup process for testcase completed");343 }344 public Specification<TestCase> getExportXmlSpecification(BackupDTO backupDTO) throws ResourceNotFoundException {345 boolean hasFilter = backupDTO.getFilterId() != null && backupDTO.getFilterId() > 0;346 if (hasFilter) return specificationBuilder(backupDTO);347 SearchCriteria criteria = new SearchCriteria("workspaceVersionId", SearchOperation.EQUALITY, backupDTO.getWorkspaceVersionId());348 List<SearchCriteria> params = new ArrayList<>();349 params.add(criteria);350 TestCaseSpecificationsBuilder testCaseSpecificationsBuilder = new TestCaseSpecificationsBuilder();351 testCaseSpecificationsBuilder.params = params;352 return testCaseSpecificationsBuilder.build();353 }354 private Specification<TestCase> specificationBuilder(BackupDTO backupDTO) throws ResourceNotFoundException {355 ListFilter filter;356 try {357 filter = testCaseFilterService.find(backupDTO.getFilterId());358 } catch (ResourceNotFoundException e) {359 filter = stepGroupFilterService.find(backupDTO.getFilterId());360 }361 WorkspaceVersion version = workspaceVersionService.find(backupDTO.getWorkspaceVersionId());362 TestCaseSpecificationsBuilder builder = new TestCaseSpecificationsBuilder();363 return builder.build(filter, version);364 }365 @Override366 protected List<TestCaseXMLDTO> mapToXMLDTOList(List<TestCase> list) {367 return mapper.mapTestcases(list);368 }369 public void importXML(BackupDTO importDTO) throws IOException, ResourceNotFoundException {370 if (!importDTO.getIsTestCaseEnabled()) return;371 log.debug("import process for testcase initiated");372 importFiles("testcases", importDTO);373 log.debug("import process for testcase completed");374 }375 @Override376 public List<TestCase> readEntityListFromXmlData(String xmlData, XmlMapper xmlMapper, BackupDTO importDTO) throws JsonProcessingException {377 if (importDTO.getIsCloudImport()) {378 return mapper.mapTestCasesCloudXMLList(xmlMapper.readValue(xmlData, new TypeReference<List<TestCaseCloudXMLDTO>>() {379 }));380 }381 else{382 return mapper.mapTestCasesXMLList(xmlMapper.readValue(xmlData, new TypeReference<List<TestCaseXMLDTO>>() {383 }));384 }385 }386 @Override387 public Optional<TestCase> findImportedEntity(TestCase testCase, BackupDTO importDTO) {388 return testCaseRepository.findAllByWorkspaceVersionIdAndImportedId(importDTO.getWorkspaceVersionId(), testCase.getId());}389 @Override390 public TestCase processBeforeSave(Optional<TestCase> previous, TestCase present, TestCase toImport, BackupDTO importDTO) {391 present.setImportedId(present.getId());392 if (previous.isPresent() && importDTO.isHasToReset()) {393 present.setId(previous.get().getId());394 } else {395 present.setId(null);396 }397 if (present.getPreRequisite() != null) {398 Optional<TestCase> recentPrerequisite = getRecentImportedEntity(importDTO, present.getPreRequisite());399 if (recentPrerequisite.isPresent())400 present.setPreRequisite(recentPrerequisite.get().getId());401 }402 present.setWorkspaceVersionId(importDTO.getWorkspaceVersionId());403 if (present.getPriority() != null) {404 Optional<TestCasePriority> priority = testCasePriorityService.getRecentImportedEntity(importDTO, present.getPriority());405 if (priority.isPresent())406 present.setPriority(priority.get().getId());407 }408 if (present.getType() != null) {409 Optional<TestCaseType> testCaseType = testCaseTypeService.getRecentImportedEntity(importDTO, present.getType());410 if (testCaseType.isPresent())411 present.setType(testCaseType.get().getId());412 }413 if (present.getTestDataId() != null) {414 Optional<TestData> testData = testDataService.getRecentImportedEntity(importDTO, present.getTestDataId());415 if (testData.isPresent())416 present.setTestDataId(testData.get().getId());417 }418 present.setLastRunId(null);419 return present;420 }421 @Override422 public TestCase copyTo(TestCase testCase) {423 Long id = testCase.getId();424 testCase = mapper.copy(testCase);425 testCase.setId(id);426 return testCase;427 }428 public TestCase save(TestCase testCase) {429 List<String> tagNames = testCase.getTagNames();430 testCase = testCaseRepository.save(testCase);431 tagService.updateTags(tagNames, TagType.TEST_CASE, testCase.getId());432 return testCase;433 }434 @Override435 public Optional<TestCase> getRecentImportedEntity(BackupDTO importDTO, Long... ids) {436 Long importedId = ids[0];437 Optional<TestCase> previous = testCaseRepository.findAllByWorkspaceVersionIdAndImportedId(importDTO.getWorkspaceVersionId(), importedId);438 return previous;439 }440 public Optional<TestCase> findImportedEntityHavingSameName(Optional<TestCase> previous, TestCase current, BackupDTO importDTO) {441 Optional<TestCase> oldEntity = testCaseRepository.findTestCaseByWorkspaceVersionIdAndName(importDTO.getWorkspaceVersionId(), current.getName());442 if (oldEntity.isPresent()) {443 return oldEntity;444 } else {445 return Optional.empty();446 }447 }448 public boolean hasImportedId(Optional<TestCase> previous) {449 return previous.isPresent() && previous.get().getImportedId() != null;450 }451 public boolean isEntityAlreadyImported(Optional<TestCase> previous, TestCase current) {452 return previous.isPresent() && previous.get().getImportedId() != null && previous.get().getImportedId().equals(current.getId());453 }454 @Override455 public boolean hasToSkip(TestCase testCase, BackupDTO importDTO) {456 return false;457 }458 @Override459 void updateImportedId(TestCase testCase, TestCase previous, BackupDTO importDTO) {460 previous.setImportedId(testCase.getId());461 save(previous);462 }463 public void handlePreRequisiteChange(TestCase testCase) {464 this.testSuiteService.handlePreRequisiteChange(testCase);465 }466}...
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!!