ScreenshotService { @Transactional public ScreenshotRun createOrUpdateScreenshotRun(ScreenshotRun screenshotRun, boolean setLastSuccessfulScreenshotRun) { ScreenshotRun currentScreenshotRun; if (screenshotRun.getId() != null) { currentScreenshotRun = screenshotRunRepository.findById(screenshotRun.getId()).orElse(null); } else { currentScreenshotRun = screenshotRunRepository.findByName(screenshotRun.getName()); } if (currentScreenshotRun != null) { logger.debug("Screenshot run already exsits, id: {}, update it", currentScreenshotRun.getId()); screenshotRun.setId(currentScreenshotRun.getId()); screenshotRun.setRepository(currentScreenshotRun.getRepository()); } else { logger.debug("Save screenshot run for repository id: {}", screenshotRun.getRepository().getId()); screenshotRun = screenshotRunRepository.save(screenshotRun); } logger.debug("Start adding screenshots sorted by sequence"); List<Screenshot> screenshotsToAdd = new ArrayList<>(screenshotRun.getScreenshots()); sortScreenshotBySequence(screenshotsToAdd); for (Screenshot screenshot : screenshotsToAdd) { completeAndAddScreenshotToRun(screenshot, screenshotRun); } if (setLastSuccessfulScreenshotRun) { logger.debug("Update the last successful screenshot import"); updateLastSucessfulScreenshotRun(screenshotRun.getRepository(), screenshotRun); } screenshotRun.setScreenshots(null); return screenshotRun; }  @Transactional ScreenshotRun createOrUpdateScreenshotRun(ScreenshotRun screenshotRun, boolean setLastSuccessfulScreenshotRun); @Transactional List<Screenshot> searchScreenshots(
            List<Long> repositoryIds,
            List<String> bcp47Tags,
            String screenshotName,
            Screenshot.Status status,
            String name,
            String source,
            String target,
            SearchType searchType,
            ScreenshotRunType screenshotRunType, int offset, int limit); Map<Long, Set<Screenshot>> getScreenshotsByTmTextUnitId(Set<Long> tmTextUnitIds); void updateScreenshot(Screenshot screenshot);  }
@Test public void testCreateScreenshotRun() throws Exception { Repository repository = repositoryService.createRepository(testIdWatcher.getEntityName("repository")); ScreenshotRun screenshotRun = new ScreenshotRun(); screenshotRun.setRepository(repository); Screenshot screenshot1 = new Screenshot(); screenshot1.setName("s1"); Screenshot screenshot2 = new Screenshot(); screenshot2.setName("s2"); screenshotRun.getScreenshots().add(screenshot1); screenshotRun.getScreenshots().add(screenshot2); ScreenshotRun createScreenshotRun = screenshotService.createOrUpdateScreenshotRun(screenshotRun, true); ScreenshotRun createdFromDB = screenshotRunRepository.findById(createScreenshotRun.getId()).orElse(null); ArrayList<Screenshot> arrayList = new ArrayList<>(createdFromDB.getScreenshots()); Assert.assertNotNull(arrayList.get(0).getId()); Assert.assertNotNull(arrayList.get(1).getId()); }
@Test public void testUpdateScreenshotRun() throws Exception { Repository repository = repositoryService.createRepository(testIdWatcher.getEntityName("repository")); ScreenshotRun screenshotRun = new ScreenshotRun(); screenshotRun.setRepository(repository); screenshotRun.setName(UUID.randomUUID().toString()); Screenshot screenshot1 = new Screenshot(); screenshot1.setName("s1"); Screenshot screenshot2 = new Screenshot(); screenshot2.setName("s2"); screenshotRun.getScreenshots().add(screenshot1); screenshotRun.getScreenshots().add(screenshot2); screenshotService.createOrUpdateScreenshotRun(screenshotRun, true); ScreenshotRun forUpdate = new ScreenshotRun(); forUpdate.setRepository(repository); forUpdate.setName(screenshotRun.getName()); Screenshot screenshot3 = new Screenshot(); screenshot3.setName("s3"); forUpdate.getScreenshots().add(screenshot3); ScreenshotRun updatedScreenshotRun = screenshotService.createOrUpdateScreenshotRun(forUpdate, true); ScreenshotRun updatedFromDB = screenshotRunRepository.findById(updatedScreenshotRun.getId()).orElse(null); ArrayList<Screenshot> arrayList = new ArrayList<>(updatedFromDB.getScreenshots()); Assert.assertNotNull(arrayList.get(0).getId()); Assert.assertNotNull(arrayList.get(1).getId()); Assert.assertNotNull(arrayList.get(2).getId()); }