private Table<Long, String, Integer> userCountByRootComponentIdAndPermission(DbSession dbSession, List<Long> rootComponentIds) { final Table<Long, String, Integer> userCountByRootComponentIdAndPermission = TreeBasedTable.create(); dbClient.userPermissionDao().countUsersByProjectPermission(dbSession, rootComponentIds).forEach( row -> userCountByRootComponentIdAndPermission.put(row.getComponentId(), row.getPermission(), row.getCount())); return userCountByRootComponentIdAndPermission; }
@Test public void countUsersByProjectPermission() { OrganizationDto organization = db.organizations().insert(); UserDto user1 = insertUser(organization); UserDto user2 = insertUser(organization); ComponentDto project1 = db.components().insertPrivateProject(organization); ComponentDto project2 = db.components().insertPrivateProject(organization); addGlobalPermission(organization, SYSTEM_ADMIN, user1); addProjectPermission(organization, USER, user1, project1); addProjectPermission(organization, ISSUE_ADMIN, user1, project1); addProjectPermission(organization, ISSUE_ADMIN, user2, project1); addProjectPermission(organization, ISSUE_ADMIN, user2, project2); // no projects -> return empty list assertThat(underTest.countUsersByProjectPermission(dbSession, emptyList())).isEmpty(); // one project expectCount(singletonList(project1.getId()), new CountPerProjectPermission(project1.getId(), USER, 1), new CountPerProjectPermission(project1.getId(), ISSUE_ADMIN, 2)); // multiple projects expectCount(asList(project1.getId(), project2.getId(), -1L), new CountPerProjectPermission(project1.getId(), USER, 1), new CountPerProjectPermission(project1.getId(), ISSUE_ADMIN, 2), new CountPerProjectPermission(project2.getId(), ISSUE_ADMIN, 1)); }
private Table<Long, String, Integer> groupCountByRootComponentIdAndPermission(DbSession dbSession, List<Long> rootComponentIds) { final Table<Long, String, Integer> userCountByRootComponentIdAndPermission = TreeBasedTable.create(); dbClient.groupPermissionDao().groupsCountByComponentIdAndPermission(dbSession, rootComponentIds, context -> { CountPerProjectPermission row = (CountPerProjectPermission) context.getResultObject(); userCountByRootComponentIdAndPermission.put(row.getComponentId(), row.getPermission(), row.getCount()); }); return userCountByRootComponentIdAndPermission; }
private void expectCount(List<Long> projectIds, CountPerProjectPermission... expected) { List<CountPerProjectPermission> got = underTest.countUsersByProjectPermission(dbSession, projectIds); assertThat(got).hasSize(expected.length); for (CountPerProjectPermission expect : expected) { boolean found = got.stream().anyMatch(b -> b.getPermission().equals(expect.getPermission()) && b.getCount() == expect.getCount() && b.getComponentId() == expect.getComponentId()); assertThat(found).isTrue(); } }
private Table<Long, String, Integer> userCountByRootComponentIdAndPermission(DbSession dbSession, List<Long> rootComponentIds) { final Table<Long, String, Integer> userCountByRootComponentIdAndPermission = TreeBasedTable.create(); dbClient.userPermissionDao().countUsersByProjectPermission(dbSession, rootComponentIds).forEach( row -> userCountByRootComponentIdAndPermission.put(row.getComponentId(), row.getPermission(), row.getCount())); return userCountByRootComponentIdAndPermission; }
private Table<Long, String, Integer> groupCountByRootComponentIdAndPermission(DbSession dbSession, List<Long> rootComponentIds) { final Table<Long, String, Integer> userCountByRootComponentIdAndPermission = TreeBasedTable.create(); dbClient.groupPermissionDao().groupsCountByComponentIdAndPermission(dbSession, rootComponentIds, context -> { CountPerProjectPermission row = (CountPerProjectPermission) context.getResultObject(); userCountByRootComponentIdAndPermission.put(row.getComponentId(), row.getPermission(), row.getCount()); }); return userCountByRootComponentIdAndPermission; }