AssetExtractionCleanupService { public void cleanupOldAssetExtractions() { logger.debug("cleanupOldAssetExtractions"); List<Long> assetExtractionIdsToDelete; do { PageRequest pageable = PageRequest.of(0, 5); assetExtractionIdsToDelete = assetExtractionRepository.findFinishedAndOldAssetExtractions(pageable); for (Long assetExtractionIdToDelete : assetExtractionIdsToDelete) { deleteAssetExtractionAndRelatedEntities(assetExtractionIdToDelete); } } while (!assetExtractionIdsToDelete.isEmpty()); }  void cleanupOldAssetExtractions();  }
@Test public void testCleanupOldAssetExtractionsWhenAllExtractionsHaveFinished() throws Exception { String assetPath = "path/to/asset.xliff"; Repository repository = createRepoWithThreeAssetExtractions(assetPath); Asset asset = assetRepository.findByPathAndRepositoryId(assetPath, repository.getId()); List<AssetExtraction> originalAssetExtractions = assetExtractionRepository.findByAsset(asset); AssetExtraction lastSuccessfulAssetExtraction = asset.getLastSuccessfulAssetExtraction(); assertEquals("There should be 3 asset extractions", 3, originalAssetExtractions.size()); assetExtractionCleanupService.cleanupOldAssetExtractions(); List<AssetExtraction> assetExtractionsAfterCleanup = assetExtractionRepository.findByAsset(asset); assertEquals("There should be only 1 asset extraction remaining", 1, assetExtractionsAfterCleanup.size()); AssetExtraction remainingAssetExtraction = assetExtractionsAfterCleanup.get(0); assertEquals("The remaining asset extraction should be the last successful one", lastSuccessfulAssetExtraction.getId(), remainingAssetExtraction.getId()); List<Long> assetExtractionIdsAfterCleanup = new ArrayList<>(); for (AssetExtraction assetExtraction : assetExtractionsAfterCleanup) { assetExtractionIdsAfterCleanup.add(assetExtraction.getId()); } for (AssetExtraction assetExtraction : originalAssetExtractions) { if (!assetExtractionIdsAfterCleanup.contains(assetExtraction.getId())) { List<AssetTextUnit> oldAssetTextUnits = assetTextUnitRepository.findByAssetExtraction(assetExtraction); assertTrue("There should be no AssetTextUnits belonging to old and fully processed assetExtractions remaining", oldAssetTextUnits.isEmpty()); } } }
@Test public void testCleanupOldAssetExtractionsMultipleBranch() throws Exception { String assetPath = "path/to/asset.xliff"; Repository repository = createRepoWithThreeAssetExtractions(assetPath); Asset asset = assetRepository.findByPathAndRepositoryId(assetPath, repository.getId()); List<AssetExtraction> originalAssetExtractions = assetExtractionRepository.findByAsset(asset); AssetExtraction lastSuccessfulAssetExtraction = asset.getLastSuccessfulAssetExtraction(); assertEquals("There should be 3 asset extractions", 3, originalAssetExtractions.size()); List<String> branches = Arrays.asList("branch1", "branch2"); branches.forEach(branch -> createOrUpdateAssetAndWaitUntilProcessingEnds(repository, assetPath, 1, branch)); branches.forEach(branch -> createOrUpdateAssetAndWaitUntilProcessingEnds(repository, assetPath, 2, branch)); List<AssetExtraction> afterBranches = assetExtractionRepository.findByAsset(asset); assertEquals("There should be 7 asset extractions (3 orginal + 4 x per branch)", 11, afterBranches.size()); assetExtractionCleanupService.cleanupOldAssetExtractions(); List<AssetExtraction> assetExtractionsAfterCleanup = assetExtractionRepository.findByAsset(asset); assertEquals("There should be 1 assets extraction per branch (3) and one merged asset extraction", 4, assetExtractionsAfterCleanup.size()); }
@Test public void testCleanupOldAssetExtractionsWhenNotAllExtractionsHaveFinished() throws Exception { String assetPath = "path/to/asset.xliff"; Repository repository = createRepoWithThreeAssetExtractions(assetPath); Asset asset = assetRepository.findByPathAndRepositoryId(assetPath, repository.getId()); AssetExtraction notFinishedAssetExtraction = setNotLastSuccessfulExtractionStateToNotFinished(asset); assertFalse("Asset extraction's state should now be NOT ALL FINISHED", notFinishedAssetExtraction.getPollableTask().isAllFinished()); List<AssetExtraction> originalAssetExtractions = assetExtractionRepository.findByAsset(asset); AssetExtraction lastSuccessfulAssetExtraction = asset.getLastSuccessfulAssetExtraction(); assertEquals("There should be 3 asset extractions", 3, originalAssetExtractions.size()); assetExtractionCleanupService.cleanupOldAssetExtractions(); List<AssetExtraction> assetExtractionsAfterCleanup = assetExtractionRepository.findByAsset(asset); assertEquals("There should be 2 assets extraction remaining", 2, assetExtractionsAfterCleanup.size()); assertEquals("The 1st remaining asset extraction should be the non finished one", assetExtractionsAfterCleanup.get(0).getId(), notFinishedAssetExtraction.getId()); assertEquals("The 2nd remaining asset extraction should be the last successful one", assetExtractionsAfterCleanup.get(1).getId(), lastSuccessfulAssetExtraction.getId()); List<Long> assetExtractionIdsAfterCleanup = new ArrayList<>(); for (AssetExtraction assetExtraction : assetExtractionsAfterCleanup) { assetExtractionIdsAfterCleanup.add(assetExtraction.getId()); } for (AssetExtraction assetExtraction : originalAssetExtractions) { if (!assetExtractionIdsAfterCleanup.contains(assetExtraction.getId())) { List<AssetTextUnit> oldAssetTextUnits = assetTextUnitRepository.findByAssetExtraction(assetExtraction); assertTrue("There should be no AssetTextUnits belonging to old and fully processed assetExtractions remaining", oldAssetTextUnits.isEmpty()); } } }