TemporaryWorkspaceRemover { @VisibleForTesting void removeTemporaryWs() throws ServerException { for (WorkspaceImpl workspace : Pages.iterate( (maxItems, skipCount) -> workspaceDao.getWorkspaces(true, maxItems, skipCount))) { WorkspaceStatus status = runtimes.getStatus(workspace.getId()); if (status == WorkspaceStatus.STOPPED) { try { workspaceDao.remove(workspace.getId()); } catch (ServerException e) { LOG.error( "Unable to cleanup temporary workspace {}. Reason is {}", workspace.getId(), e.getMessage()); } } } } @Inject  TemporaryWorkspaceRemover(WorkspaceDao workspaceDao, WorkspaceRuntimes runtimes);   }
@Test public void shouldRemoveStoppedTemporaryWorkspaces() throws Exception { doReturn(WorkspaceStatus.STOPPED).when(runtimes).getStatus(any()); when(workspaceDao.getWorkspaces(eq(true), anyInt(), anyLong())) .thenReturn( new Page<>( createStoppedWorkspaces(DEFAULT_PAGE_SIZE), 0, DEFAULT_PAGE_SIZE, COUNT_OF_WORKSPACES)) .thenReturn( new Page<>( createStoppedWorkspaces(DEFAULT_PAGE_SIZE), DEFAULT_PAGE_SIZE, DEFAULT_PAGE_SIZE, COUNT_OF_WORKSPACES)) .thenReturn( new Page<>( createStoppedWorkspaces(DEFAULT_PAGE_SIZE), DEFAULT_PAGE_SIZE * 2, DEFAULT_PAGE_SIZE, COUNT_OF_WORKSPACES)); remover.removeTemporaryWs(); verify(workspaceDao, times(COUNT_OF_WORKSPACES)).remove(anyString()); }
@Test(dataProvider = "activeWorkspaceStatuses") public void shouldNotRemoveActiveWorkspace(WorkspaceStatus status) throws Exception { WorkspaceImpl workspace = WorkspaceImpl.builder().setId("ws123").build(); when(workspaceDao.getWorkspaces(eq(true), anyInt(), anyLong())) .thenReturn(new Page<>(singletonList(workspace), 0, 1, 1)); doReturn(status).when(runtimes).getStatus("ws123"); remover.removeTemporaryWs(); verify(workspaceDao, never()).remove(anyString()); }