AuditManager { public Path generateAuditReport() throws ServerException, ConflictException { if (!inProgress.compareAndSet(false, true)) { throw new ConflictException("Generating report is already in progress"); } Path auditReport = null; try { auditReport = createEmptyAuditReportFile(); printSystemInfo(auditReport); printAllUsersInfo(auditReport); } catch (Exception exception) { if (auditReport != null) { deleteReportDirectory(auditReport); } LOG.error(exception.getMessage(), exception); throw new ServerException(exception.getMessage(), exception); } finally { inProgress.set(false); } return auditReport; } @Inject  AuditManager(
      UserManager userManager,
      WorkspaceManager workspaceManager,
      PermissionsManager permissionsManager); Path generateAuditReport();  }
@Test public void shouldReturnFullAuditReportWithWorkspaceThatBelongsToUserButWithoutPermissionsToUser() throws Exception { List<WorkspaceImpl> workspaces = new ArrayList<>(); workspaces.add(workspace2); when(workspace1.getNamespace()).thenReturn("User2"); when(workspace2.getNamespace()).thenReturn("User2"); when(workspaceManager.getWorkspaces(eq("User2Id"), eq(false))).thenReturn(workspaces); when(workspaceManager.getByNamespace(eq("User2"), eq(false))) .thenReturn(asList(workspace1, workspace2)); auditReport = auditManager.generateAuditReport(); assertEquals( readFileToString(auditReport.toFile()), "Number of users: 2\n" + "user@email.com is owner of 0 workspaces and has permissions in 2 workspaces\n" + " └ Workspace1Name, is owner: false, permissions: [read, use, run, configure, setPermissions, delete]\n" + " └ Workspace2Name, is owner: false, permissions: [read, use, run, configure, setPermissions]\n" + "user2@email.com is owner of 2 workspaces and has permissions in 1 workspace\n" + " └ Workspace1Name, is owner: true, permissions: []\n" + " └ Workspace2Name, is owner: true, permissions: [read, use, run, configure, setPermissions, delete]\n"); }
@Test public void shouldReturnAuditReportWithoutUserWorkspacesIfFailedToRetrieveTheListOfHisWorkspaces() throws Exception { when(workspaceManager.getWorkspaces(eq("User1Id"), eq(false))) .thenThrow(new ServerException("Failed to retrieve workspaces")); auditReport = auditManager.generateAuditReport(); assertEquals( readFileToString(auditReport.toFile()), "Number of users: 2\n" + "[ERROR] Failed to retrieve the list of related workspaces for user User1Id!\n" + "user2@email.com is owner of 1 workspace and has permissions in 1 workspace\n" + " └ Workspace2Name, is owner: true, permissions: [read, use, run, configure, setPermissions, delete]\n"); }
@Test public void shouldReturnFullAuditReport() throws Exception { auditReport = auditManager.generateAuditReport(); assertEquals( readFileToString(auditReport.toFile()), "Number of users: 2\n" + "user@email.com is owner of 1 workspace and has permissions in 2 workspaces\n" + " └ Workspace1Name, is owner: true, permissions: [read, use, run, configure, setPermissions, delete]\n" + " └ Workspace2Name, is owner: false, permissions: [read, use, run, configure, setPermissions]\n" + "user2@email.com is owner of 1 workspace and has permissions in 1 workspace\n" + " └ Workspace2Name, is owner: true, permissions: [read, use, run, configure, setPermissions, delete]\n"); }