OrientCleanupServiceImpl extends ComponentSupport implements CleanupService { @Override public void cleanup(final BooleanSupplier cancelledCheck) { AtomicLong totalDeletedCount = new AtomicLong(0L); repositoryManager.browse().forEach(repository -> { if (!cancelledCheck.getAsBoolean() && !repository.getType().equals(groupType)) { totalDeletedCount.addAndGet(this.cleanup(repository, cancelledCheck)); } }); log.info("{} components cleaned up across all repositories", totalDeletedCount.get()); } @Inject  OrientCleanupServiceImpl(final RepositoryManager repositoryManager,
                            final OrientCleanupComponentBrowse browseService,
                            final CleanupPolicyStorage cleanupPolicyStorage,
                            final CleanupMethod cleanupMethod,
                            final GroupType groupType,
                            @Named("${nexus.cleanup.retries:-3}") final int cleanupRetryLimit); @Override void cleanup(final BooleanSupplier cancelledCheck); static final String CLEANUP_ATTRIBUTES_KEY; static final String CLEANUP_NAME_KEY; }
@Test public void fetchPolicyForEachRepositoryAndRunCleanup() throws Exception { underTest.cleanup(cancelledCheck); verify(cleanupMethod).run(repository1, ImmutableList.of(component1, component2), cancelledCheck); verify(cleanupMethod).run(repository2, ImmutableList.of(component3), cancelledCheck); }
@Test public void fetchMultiplePoliciesForEachRepositoryAndRunCleanup() { String[] policyNamesForRepo1 = {"abc", "def", "ghi"}; List<EntityId> componentsForRepo1 = setupForMultiplePolicies(repository1, policyNamesForRepo1); String[] policyNamesForRepo2 = {"qwe", "rty", "uio"}; List<EntityId> componentsForRepo2 = setupForMultiplePolicies(repository2, policyNamesForRepo2); underTest.cleanup(cancelledCheck); verify(cleanupMethod, times(policyNamesForRepo1.length)).run(repository1, componentsForRepo1, cancelledCheck); verify(cleanupMethod, times(policyNamesForRepo2.length)).run(repository2, componentsForRepo2, cancelledCheck); }
@Test public void ignoreRepositoryWhenPolicyNull() throws Exception { when(cleanupPolicyStorage.get(POLICY_2_NAME)).thenReturn(null); underTest.cleanup(cancelledCheck); verify(cleanupMethod).run(repository1, ImmutableList.of(component1, component2), cancelledCheck); }
@Test public void ignoreRepositoryWhenPolicyNameNull() throws Exception { setupRepository(repository2, new String[]{null}); when(cleanupPolicyStorage.get(null)).thenThrow(new NullPointerException()); underTest.cleanup(cancelledCheck); verify(cleanupMethod).run(repository1, ImmutableList.of(component1, component2), cancelledCheck); }
@Test public void ignoreRepositoryWhenPolicyNameListIsNull() throws Exception { when(repositoryManager.browse()).thenReturn(ImmutableList.of(repository3)); underTest.cleanup(cancelledCheck); verify(cleanupMethod, never()).run(repository3, ImmutableList.of(component2), cancelledCheck); }
@Test public void ignoreRepositoryWhenPolicyNameAttributeIsNotPresent() throws Exception { repository1.getConfiguration().setAttributes(emptyMap()); underTest.cleanup(cancelledCheck); verify(cleanupMethod).run(repository2, ImmutableList.of(component3), cancelledCheck); }
@Test public void ignoreRepositoryWhenAttributesNull() throws Exception { when(repository1.getConfiguration()).thenReturn(mock(Configuration.class)); underTest.cleanup(cancelledCheck); verify(cleanupMethod).run(repository2, ImmutableList.of(component3), cancelledCheck); }
@Test public void ignoreRepositoryWhenCancelled() throws Exception { doAnswer(i -> { when(cancelledCheck.getAsBoolean()).thenReturn(true); return deletionProgress; }).when(cleanupMethod).run(repository1, ImmutableList.of(component1, component2), cancelledCheck); underTest.cleanup(cancelledCheck); verify(cleanupMethod).run(repository1, ImmutableList.of(component1, component2), cancelledCheck); verify(cleanupMethod, never()).run(repository2, ImmutableList.of(component3), cancelledCheck); }
@Test public void doNothingWhenNoComponentsFound() throws Exception { when(browseService.browse(cleanupPolicy1, repository1)).thenReturn(emptyList()); when(browseService.browse(cleanupPolicy2, repository2)).thenReturn(emptyList()); underTest.cleanup(cancelledCheck); verify(cleanupMethod, never()).run(repository1, ImmutableList.of(component1, component2), cancelledCheck); verify(cleanupMethod, never()).run(repository2, ImmutableList.of(component3), cancelledCheck); }
@Test public void doNotDeleteAnythingWhenCriteriaEmpty() throws Exception { when(cleanupPolicy1.getCriteria()).thenReturn(emptyMap()); when(cleanupPolicy2.getCriteria()).thenReturn(emptyMap()); underTest.cleanup(cancelledCheck); verify(cleanupMethod, never()).run(repository1, ImmutableList.of(component1, component2), cancelledCheck); verify(cleanupMethod, never()).run(repository2, ImmutableList.of(component3), cancelledCheck); }
@Test public void retryDeletionIfFailure() { when(deletionProgress.isFailed()).thenReturn(true).thenReturn(false); when(repositoryManager.browse()).thenReturn(ImmutableList.of(repository1)); when(cleanupMethod.run(any(), any(), any())).thenReturn(deletionProgress); underTest.cleanup(cancelledCheck); verify(cleanupMethod, times(2)).run(any(), any(), any()); }
@Test public void retryAttemptsExceeded() { when(deletionProgress.isFailed()).thenReturn(true); when(repositoryManager.browse()).thenReturn(ImmutableList.of(repository1)); when(cleanupMethod.run(any(), any(), any())).thenReturn(deletionProgress); underTest.cleanup(cancelledCheck); verify(cleanupMethod, times(3)).run(any(), any(), any()); }
@Test public void cleanupRetriedOnScrollTimeout() { when(cleanupMethod.run(any(), any(), any())) .thenThrow(new RuntimeException(new SearchContextMissingException(10L))).thenReturn(deletionProgress); underTest.cleanup(cancelledCheck); verify(cleanupMethod, times(2)).run(repository1, ImmutableList.of(component1, component2), cancelledCheck); verify(cleanupMethod).run(repository2, ImmutableList.of(component3), cancelledCheck); }