diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-09-07 18:03:11 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-09-28 16:35:00 +0200 |
commit | f16b5672621fa152414a1f8afb24340c67668029 (patch) | |
tree | e8e8e514a760262243f70e8ff1e6d21a94c865f3 | |
parent | e7af578fb13af943b5beb6f10299cdd5ec4d876e (diff) | |
download | sonarqube-f16b5672621fa152414a1f8afb24340c67668029.tar.gz sonarqube-f16b5672621fa152414a1f8afb24340c67668029.zip |
Remove business classes in permission domain
- GroupWithPermissionQueryResult
- UserWithPermissionQueryResult
- UserWithPermission
- GroupWithPermission
14 files changed, 42 insertions, 890 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/GroupWithPermissionQueryResult.java b/server/sonar-server/src/main/java/org/sonar/server/permission/GroupWithPermissionQueryResult.java deleted file mode 100644 index e5c9b5d97d5..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/GroupWithPermissionQueryResult.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.permission; - -import java.util.List; -import org.sonar.core.permission.GroupWithPermission; - -public class GroupWithPermissionQueryResult { - - private final List<GroupWithPermission> groups; - private final int total; - - public GroupWithPermissionQueryResult(List<GroupWithPermission> groups, int total) { - this.groups = groups; - this.total = total; - } - - public List<GroupWithPermission> groups() { - return groups; - } - - public int total() { - return total; - } -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionFinder.java b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionFinder.java index fc13eee3374..b7d885d99e2 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionFinder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionFinder.java @@ -29,8 +29,6 @@ import org.sonar.api.security.DefaultGroups; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.Paging; import org.sonar.core.permission.GlobalPermissions; -import org.sonar.core.permission.GroupWithPermission; -import org.sonar.core.permission.UserWithPermission; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ResourceDao; @@ -39,8 +37,6 @@ import org.sonar.db.component.ResourceQuery; import org.sonar.db.permission.GroupWithPermissionDto; import org.sonar.db.permission.PermissionDao; import org.sonar.db.permission.PermissionQuery; -import org.sonar.db.permission.PermissionTemplateDao; -import org.sonar.db.permission.PermissionTemplateDto; import org.sonar.db.permission.UserWithPermissionDto; import org.sonar.server.exceptions.NotFoundException; @@ -53,56 +49,26 @@ public class PermissionFinder { private final PermissionDao permissionDao; private final ResourceDao resourceDao; - private final PermissionTemplateDao permissionTemplateDao; public PermissionFinder(DbClient dbClient) { this.resourceDao = dbClient.resourceDao(); this.permissionDao = dbClient.permissionDao(); - this.permissionTemplateDao = dbClient.permissionTemplateDao(); } - public UserWithPermissionQueryResult findUsersWithPermission(DbSession dbSession, PermissionQuery query) { + public List<UserWithPermissionDto> findUsersWithPermission(DbSession dbSession, PermissionQuery query) { Long componentId = componentId(query.component()); int limit = query.pageSize(); - int total = permissionDao.countUsers(dbSession, query, componentId); - return toUserQueryResult(permissionDao.selectUsers(dbSession, query, componentId, offset(query), limit), total); - } - - public UserWithPermissionQueryResult findUsersWithPermissionTemplate(DbSession dbSession, PermissionQuery query) { - Long permissionTemplateId = templateId(query.template()); - int limit = query.pageSize(); - int total = permissionTemplateDao.countUsers(dbSession, query, permissionTemplateId); - return toUserQueryResult(permissionTemplateDao.selectUsers(dbSession, query, permissionTemplateId, offset(query), limit), total); + return permissionDao.selectUsers(dbSession, query, componentId, offset(query), limit); } /** * Paging for groups search is done in Java in order to correctly handle the 'Anyone' group */ - public GroupWithPermissionQueryResult findGroupsWithPermission(DbSession dbSession, PermissionQuery query) { + public List<GroupWithPermissionDto> findGroupsWithPermission(DbSession dbSession, PermissionQuery query) { Long componentId = componentId(query.component()); return toGroupQueryResult(permissionDao.selectGroups(dbSession, query, componentId), query); } - /** - * Paging for groups search is done in Java in order to correctly handle the 'Anyone' group - */ - public GroupWithPermissionQueryResult findGroupsWithPermissionTemplate(DbSession dbSession, PermissionQuery query) { - Long permissionTemplateId = templateId(query.template()); - return toGroupQueryResult(permissionTemplateDao.selectGroups(dbSession, query, permissionTemplateId), query); - } - - private static UserWithPermissionQueryResult toUserQueryResult(List<UserWithPermissionDto> dtos, int total) { - return new UserWithPermissionQueryResult(toUserWithPermissionList(dtos), total); - } - - private static List<UserWithPermission> toUserWithPermissionList(List<UserWithPermissionDto> dtos) { - List<UserWithPermission> users = newArrayList(); - for (UserWithPermissionDto dto : dtos) { - users.add(dto.toUserWithPermission()); - } - return users; - } - @Nullable private Long componentId(@Nullable String componentKey) { if (componentKey == null) { @@ -116,7 +82,7 @@ public class PermissionFinder { } } - private GroupWithPermissionQueryResult toGroupQueryResult(List<GroupWithPermissionDto> dtos, PermissionQuery query) { + private List<GroupWithPermissionDto> toGroupQueryResult(List<GroupWithPermissionDto> dtos, PermissionQuery query) { addAnyoneGroup(dtos, query); List<GroupWithPermissionDto> filteredDtos = filterMembership(dtos, query); @@ -124,16 +90,7 @@ public class PermissionFinder { .withPageSize(query.pageSize()) .andTotal(filteredDtos.size()); - List<GroupWithPermission> pagedGroups = pagedGroups(filteredDtos, paging); - return new GroupWithPermissionQueryResult(pagedGroups, filteredDtos.size()); - } - - private Long templateId(String templateKey) { - PermissionTemplateDto dto = permissionTemplateDao.selectByUuid(templateKey); - if (dto == null) { - throw new NotFoundException(String.format("Template '%s' does not exist", templateKey)); - } - return dto.getId(); + return pagedGroups(filteredDtos, paging); } private static int offset(PermissionQuery query) { @@ -159,12 +116,12 @@ public class PermissionFinder { } } - private static List<GroupWithPermission> pagedGroups(Collection<GroupWithPermissionDto> dtos, Paging paging) { - List<GroupWithPermission> groups = newArrayList(); + private static List<GroupWithPermissionDto> pagedGroups(Collection<GroupWithPermissionDto> dtos, Paging paging) { + List<GroupWithPermissionDto> groups = newArrayList(); int index = 0; for (GroupWithPermissionDto dto : dtos) { if (index >= paging.offset() && groups.size() < paging.pageSize()) { - groups.add(dto.toGroupWithPermission()); + groups.add(dto); } else if (groups.size() >= paging.pageSize()) { break; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/UserWithPermissionQueryResult.java b/server/sonar-server/src/main/java/org/sonar/server/permission/UserWithPermissionQueryResult.java deleted file mode 100644 index 7b06087aa1b..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/UserWithPermissionQueryResult.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.server.permission; - -import java.util.List; -import org.sonar.core.permission.UserWithPermission; - -public class UserWithPermissionQueryResult { - - private final List<UserWithPermission> users; - private final int total; - - public UserWithPermissionQueryResult(List<UserWithPermission> users, int total) { - this.users = users; - this.total = total; - } - - public List<UserWithPermission> users() { - return users; - } - - public int total() { - return total; - } -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/GroupsAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/GroupsAction.java index 7cddc442a8e..66b8defa643 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/GroupsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/GroupsAction.java @@ -28,12 +28,11 @@ import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.server.ws.WebService.Param; import org.sonar.api.server.ws.WebService.SelectionMode; -import org.sonar.core.permission.GroupWithPermission; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; +import org.sonar.db.permission.GroupWithPermissionDto; import org.sonar.db.permission.PermissionQuery; -import org.sonar.server.permission.GroupWithPermissionQueryResult; import org.sonar.server.permission.PermissionFinder; import org.sonar.server.permission.ws.PermissionRequest.Builder; import org.sonar.server.user.UserSession; @@ -89,8 +88,10 @@ public class GroupsAction implements PermissionsWsAction { checkProjectAdminUserByComponentDto(userSession, project); PermissionQuery permissionQuery = buildPermissionQuery(request, project); - GroupWithPermissionQueryResult groupsResult = permissionFinder.findGroupsWithPermission(dbSession, permissionQuery); - WsGroupsResponse groupsResponse = buildResponse(groupsResult, request); + Long projectIdIfPresent = project.isPresent() ? project.get().getId() : null; + int total = dbClient.permissionDao().countGroups(dbSession, permissionQuery.permission(), projectIdIfPresent); + List<GroupWithPermissionDto> groupsWithPermission = permissionFinder.findGroupsWithPermission(dbSession, permissionQuery); + WsGroupsResponse groupsResponse = buildResponse(groupsWithPermission, request, total); writeProtobuf(groupsResponse, wsRequest, wsResponse); } finally { @@ -98,24 +99,22 @@ public class GroupsAction implements PermissionsWsAction { } } - private WsGroupsResponse buildResponse(GroupWithPermissionQueryResult groupsResult, PermissionRequest permissionRequest) { - List<GroupWithPermission> groupsWithPermission = groupsResult.groups(); - + private WsGroupsResponse buildResponse(List<GroupWithPermissionDto> groupsWithPermission, PermissionRequest permissionRequest, int total) { WsGroupsResponse.Builder groupsResponse = WsGroupsResponse.newBuilder(); WsGroupsResponse.Group.Builder group = WsGroupsResponse.Group.newBuilder(); Common.Paging.Builder paging = Common.Paging.newBuilder(); - for (GroupWithPermission groupWithPermission : groupsWithPermission) { + for (GroupWithPermissionDto groupWithPermission : groupsWithPermission) { group .clear() - .setName(groupWithPermission.name()) - .setSelected(groupWithPermission.hasPermission()); + .setName(groupWithPermission.getName()) + .setSelected(groupWithPermission.getPermission() != null); // anyone group return with id = 0 - if (groupWithPermission.id() != 0) { - group.setId(String.valueOf(groupWithPermission.id())); + if (groupWithPermission.getId() != 0) { + group.setId(String.valueOf(groupWithPermission.getId())); } - if (groupWithPermission.description() != null) { - group.setDescription(groupWithPermission.description()); + if (groupWithPermission.getDescription() != null) { + group.setDescription(groupWithPermission.getDescription()); } groupsResponse.addGroups(group); @@ -125,7 +124,7 @@ public class GroupsAction implements PermissionsWsAction { paging .setPageIndex(permissionRequest.page()) .setPageSize(permissionRequest.pageSize()) - .setTotal(groupsResult.total()) + .setTotal(total) ); return groupsResponse.build(); diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/UsersAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/UsersAction.java index c359deacfa6..d1093b05e73 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/UsersAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/UsersAction.java @@ -27,20 +27,20 @@ import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.server.ws.WebService.Param; import org.sonar.api.server.ws.WebService.SelectionMode; -import org.sonar.core.permission.UserWithPermission; +import org.sonar.api.utils.Paging; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; import org.sonar.db.permission.PermissionQuery; +import org.sonar.db.permission.UserWithPermissionDto; import org.sonar.server.permission.PermissionFinder; -import org.sonar.server.permission.UserWithPermissionQueryResult; import org.sonar.server.permission.ws.PermissionRequest.Builder; import org.sonar.server.user.UserSession; -import org.sonarqube.ws.Common.Paging; import org.sonarqube.ws.WsPermissions.WsUsersResponse; import static com.google.common.base.Objects.firstNonNull; import static com.google.common.base.Strings.nullToEmpty; +import static org.sonar.api.utils.Paging.forPageIndex; import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdminUserByComponentDto; import static org.sonar.server.permission.ws.PermissionQueryParser.fromSelectionModeToMembership; import static org.sonar.server.permission.ws.WsPermissionParameters.createPermissionParameter; @@ -89,8 +89,10 @@ public class UsersAction implements PermissionsWsAction { Optional<ComponentDto> project = dependenciesFinder.searchProject(dbSession, request); checkProjectAdminUserByComponentDto(userSession, project); PermissionQuery permissionQuery = buildPermissionQuery(request, project); - UserWithPermissionQueryResult usersResult = permissionFinder.findUsersWithPermission(dbSession, permissionQuery); - WsUsersResponse wsUsersResponse = buildResponse(usersResult, request.page(), request.pageSize()); + Long projectIdIfPresent = project.isPresent() ? project.get().getId() : null; + int total = dbClient.permissionDao().countUsers(dbSession, permissionQuery, projectIdIfPresent); + List<UserWithPermissionDto> usersWithPermission = permissionFinder.findUsersWithPermission(dbSession, permissionQuery); + WsUsersResponse wsUsersResponse = buildResponse(usersWithPermission, forPageIndex(request.page()).withPageSize(request.pageSize()).andTotal(total)); writeProtobuf(wsUsersResponse, wsRequest, wsResponse); } finally { @@ -98,27 +100,25 @@ public class UsersAction implements PermissionsWsAction { } } - private WsUsersResponse buildResponse(UserWithPermissionQueryResult usersResult, int page, int pageSize) { - List<UserWithPermission> usersWithPermission = usersResult.users(); - + private WsUsersResponse buildResponse(List<UserWithPermissionDto> usersWithPermission, Paging paging) { WsUsersResponse.Builder userResponse = WsUsersResponse.newBuilder(); WsUsersResponse.User.Builder user = WsUsersResponse.User.newBuilder(); - for (UserWithPermission userWithPermission : usersWithPermission) { + for (UserWithPermissionDto userWithPermission : usersWithPermission) { userResponse.addUsers( user .clear() - .setLogin(userWithPermission.login()) - .setName(nullToEmpty(userWithPermission.name())) - .setEmail(nullToEmpty(userWithPermission.email())) - .setSelected(userWithPermission.hasPermission())); + .setLogin(userWithPermission.getLogin()) + .setName(nullToEmpty(userWithPermission.getName())) + .setEmail(nullToEmpty(userWithPermission.getEmail())) + .setSelected(userWithPermission.getPermission() != null)); } - userResponse.setPaging( - Paging.newBuilder() - .clear() - .setPageIndex(page) - .setPageSize(pageSize) - .setTotal(usersResult.total()) - ); + + userResponse.getPagingBuilder() + .clear() + .setPageIndex(paging.pageIndex()) + .setPageSize(paging.pageSize()) + .setTotal(paging.total()) + .build(); return userResponse.build(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionFinderTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionFinderTest.java deleted file mode 100644 index 5c350e0a857..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionFinderTest.java +++ /dev/null @@ -1,295 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.permission; - -import java.util.List; -import org.junit.Before; -import org.junit.Test; -import org.mockito.ArgumentCaptor; -import org.sonar.core.permission.GroupWithPermission; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.component.ResourceDao; -import org.sonar.db.component.ResourceDto; -import org.sonar.db.component.ResourceQuery; -import org.sonar.db.permission.GroupWithPermissionDto; -import org.sonar.db.permission.PermissionDao; -import org.sonar.db.permission.PermissionQuery; -import org.sonar.db.permission.PermissionTemplateDao; -import org.sonar.db.permission.PermissionTemplateDto; -import org.sonar.db.permission.UserWithPermissionDto; -import org.sonar.server.exceptions.NotFoundException; - -import static com.google.common.collect.Lists.newArrayList; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.fail; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyInt; -import static org.mockito.Matchers.anyLong; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public class PermissionFinderTest { - - PermissionDao permissionDao = mock(PermissionDao.class); - ResourceDao resourceDao = mock(ResourceDao.class); - PermissionTemplateDao permissionTemplateDao = mock(PermissionTemplateDao.class); - DbSession dbSession = mock(DbSession.class); - - PermissionFinder underTest; - - @Before - public void setUp() { - DbClient dbClient = mock(DbClient.class); - when(dbClient.resourceDao()).thenReturn(resourceDao); - when(dbClient.permissionDao()).thenReturn(permissionDao); - when(dbClient.permissionTemplateDao()).thenReturn(permissionTemplateDao); - when(resourceDao.selectResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setId(100L).setName("org.sample.Sample")); - underTest = new PermissionFinder(dbClient); - } - - @Test - public void find_users() { - when(permissionDao.selectUsers(any(DbSession.class), any(PermissionQuery.class), anyLong(), anyInt(), anyInt())).thenReturn( - newArrayList(new UserWithPermissionDto().setName("user1").setPermission("user")) - ); - when(permissionDao.countUsers(any(DbSession.class), any(PermissionQuery.class), anyLong())).thenReturn(1); - - UserWithPermissionQueryResult result = underTest.findUsersWithPermission(dbSession, PermissionQuery.builder().permission("user").build()); - assertThat(result.users()).hasSize(1); - assertThat(result.total()).isEqualTo(1); - } - - @Test - public void fail_to_find_users_when_component_not_found() { - when(resourceDao.selectResource(any(ResourceQuery.class))).thenReturn(null); - - try { - underTest.findUsersWithPermission(dbSession, PermissionQuery.builder().permission("user").component("Unknown").build()); - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(NotFoundException.class).hasMessage("Project 'Unknown' does not exist"); - } - } - - @Test - public void find_users_with_paging() { - underTest.findUsersWithPermission(dbSession, PermissionQuery.builder().permission("user").pageIndex(3).pageSize(10).build()); - - ArgumentCaptor<Integer> argumentOffset = ArgumentCaptor.forClass(Integer.class); - ArgumentCaptor<Integer> argumentLimit = ArgumentCaptor.forClass(Integer.class); - verify(permissionDao).selectUsers(any(DbSession.class), any(PermissionQuery.class), anyLong(), argumentOffset.capture(), argumentLimit.capture()); - - assertThat(argumentOffset.getValue()).isEqualTo(20); - assertThat(argumentLimit.getValue()).isEqualTo(10); - } - - @Test - public void find_users_with_paging_having_more_results() { - when(permissionDao.selectUsers(any(DbSession.class), any(PermissionQuery.class), anyLong(), anyInt(), anyInt())).thenReturn(newArrayList( - new UserWithPermissionDto().setName("user1").setPermission("user"), - new UserWithPermissionDto().setName("user2").setPermission("user"), - new UserWithPermissionDto().setName("user3").setPermission("user")) - ); - when(permissionDao.countUsers(any(DbSession.class), any(PermissionQuery.class), anyLong())).thenReturn(3); - UserWithPermissionQueryResult result = underTest.findUsersWithPermission(dbSession, PermissionQuery.builder().permission("user").pageIndex(1).pageSize(2).build()); - - ArgumentCaptor<Integer> argumentOffset = ArgumentCaptor.forClass(Integer.class); - ArgumentCaptor<Integer> argumentLimit = ArgumentCaptor.forClass(Integer.class); - verify(permissionDao).selectUsers(any(DbSession.class), any(PermissionQuery.class), anyLong(), argumentOffset.capture(), argumentLimit.capture()); - - assertThat(argumentOffset.getValue()).isEqualTo(0); - assertThat(argumentLimit.getValue()).isEqualTo(2); - assertThat(result.total()).isEqualTo(3); - } - - @Test - public void find_users_with_paging_having_no_more_results() { - when(permissionDao.selectUsers(any(DbSession.class), any(PermissionQuery.class), anyLong(), anyInt(), anyInt())).thenReturn(newArrayList( - new UserWithPermissionDto().setName("user1").setPermission("user"), - new UserWithPermissionDto().setName("user2").setPermission("user"), - new UserWithPermissionDto().setName("user4").setPermission("user"), - new UserWithPermissionDto().setName("user3").setPermission("user")) - ); - when(permissionDao.countUsers(any(DbSession.class), any(PermissionQuery.class), anyLong())).thenReturn(4); - - UserWithPermissionQueryResult result = underTest.findUsersWithPermission(dbSession, PermissionQuery.builder().permission("user").pageIndex(1).pageSize(10).build()); - - ArgumentCaptor<Integer> argumentOffset = ArgumentCaptor.forClass(Integer.class); - ArgumentCaptor<Integer> argumentLimit = ArgumentCaptor.forClass(Integer.class); - verify(permissionDao).selectUsers(any(DbSession.class), any(PermissionQuery.class), anyLong(), argumentOffset.capture(), argumentLimit.capture()); - - assertThat(argumentOffset.getValue()).isEqualTo(0); - assertThat(argumentLimit.getValue()).isEqualTo(10); - assertThat(result.total()).isEqualTo(4); - } - - @Test - public void find_groups() { - when(permissionDao.selectGroups(any(DbSession.class), any(PermissionQuery.class), anyLong())).thenReturn( - newArrayList(new GroupWithPermissionDto().setName("users").setPermission("user")) - ); - - GroupWithPermissionQueryResult result = underTest.findGroupsWithPermission(dbSession, PermissionQuery.builder().permission("user").membership(PermissionQuery.IN).build()); - - assertThat(result.groups()).hasSize(1); - } - - @Test - public void find_groups_should_be_paginated() { - when(permissionDao.selectGroups(any(DbSession.class), any(PermissionQuery.class), anyLong())).thenReturn(newArrayList( - new GroupWithPermissionDto().setName("Anyone").setPermission("user"), - new GroupWithPermissionDto().setName("Admin").setPermission("user"), - new GroupWithPermissionDto().setName("Users").setPermission(null), - new GroupWithPermissionDto().setName("Reviewers").setPermission(null), - new GroupWithPermissionDto().setName("Other").setPermission(null) - )); - - GroupWithPermissionQueryResult result = underTest.findGroupsWithPermission(dbSession, - PermissionQuery.builder() - .permission("user") - .pageSize(2) - .pageIndex(2) - .build()); - - List<GroupWithPermission> groups = result.groups(); - assertThat(groups).hasSize(2); - assertThat(groups.get(0).name()).isEqualTo("Users"); - assertThat(groups.get(1).name()).isEqualTo("Reviewers"); - } - - @Test - public void find_groups_should_filter_membership() { - when(permissionDao.selectGroups(any(DbSession.class), any(PermissionQuery.class), anyLong())).thenReturn(newArrayList( - new GroupWithPermissionDto().setName("Anyone").setPermission("user"), - new GroupWithPermissionDto().setName("Admin").setPermission("user"), - new GroupWithPermissionDto().setName("Users").setPermission(null), - new GroupWithPermissionDto().setName("Reviewers").setPermission(null), - new GroupWithPermissionDto().setName("Other").setPermission(null) - )); - - assertThat(underTest.findGroupsWithPermission(dbSession, PermissionQuery.builder().permission("user").membership(PermissionQuery.IN).build()).groups()).hasSize(2); - assertThat(underTest.findGroupsWithPermission(dbSession, PermissionQuery.builder().permission("user").membership(PermissionQuery.OUT).build()).groups()).hasSize(3); - assertThat(underTest.findGroupsWithPermission(dbSession, PermissionQuery.builder().permission("user").membership(PermissionQuery.ANY).build()).groups()).hasSize(5); - } - - @Test - public void find_groups_with_added_anyone_group() { - when(permissionDao.selectGroups(any(DbSession.class), any(PermissionQuery.class), anyLong())).thenReturn( - newArrayList(new GroupWithPermissionDto().setName("users").setPermission("user")) - ); - - GroupWithPermissionQueryResult result = underTest.findGroupsWithPermission(dbSession, PermissionQuery.builder().permission("user").pageIndex(1).membership(PermissionQuery.ANY) - .build()); - assertThat(result.groups()).hasSize(2); - GroupWithPermission first = result.groups().get(0); - assertThat(first.name()).isEqualTo("Anyone"); - assertThat(first.hasPermission()).isFalse(); - } - - @Test - public void find_groups_without_adding_anyone_group_when_search_text_do_not_matched() { - when(permissionDao.selectGroups(any(DbSession.class), any(PermissionQuery.class), anyLong())).thenReturn( - newArrayList(new GroupWithPermissionDto().setName("users").setPermission("user")) - ); - - GroupWithPermissionQueryResult result = underTest.findGroupsWithPermission(dbSession, PermissionQuery.builder().permission("user").search("other") - .pageIndex(1).membership(PermissionQuery.ANY).build()); - // Anyone group should not be added - assertThat(result.groups()).hasSize(1); - } - - @Test - public void find_groups_with_added_anyone_group_when_search_text_matched() { - when(permissionDao.selectGroups(any(DbSession.class), any(PermissionQuery.class), anyLong())).thenReturn( - newArrayList(new GroupWithPermissionDto().setName("MyAnyGroup").setPermission("user")) - ); - - GroupWithPermissionQueryResult result = underTest.findGroupsWithPermission(dbSession, PermissionQuery.builder().permission("user").search("any") - .pageIndex(1).membership(PermissionQuery.ANY).build()); - assertThat(result.groups()).hasSize(2); - } - - @Test - public void find_groups_without_adding_anyone_group_when_out_membership_selected() { - when(permissionDao.selectGroups(any(DbSession.class), any(PermissionQuery.class), anyLong())).thenReturn( - newArrayList(new GroupWithPermissionDto().setName("users").setPermission("user")) - ); - - GroupWithPermissionQueryResult result = underTest.findGroupsWithPermission(dbSession, PermissionQuery.builder().permission("user") - .pageIndex(1).membership(PermissionQuery.OUT).build()); - // Anyone group should not be added - assertThat(result.groups()).hasSize(1); - } - - @Test - public void find_users_from_permission_template() { - when(permissionTemplateDao.selectByUuid(anyString())).thenReturn(new PermissionTemplateDto().setId(1L).setUuid("my_template")); - - when(permissionTemplateDao.selectUsers(any(DbSession.class), any(PermissionQuery.class), anyLong(), anyInt(), anyInt())).thenReturn( - newArrayList(new UserWithPermissionDto().setName("user1").setPermission("user")) - ); - - when(permissionTemplateDao.countUsers(any(DbSession.class), any(PermissionQuery.class), anyLong())).thenReturn(1); - - UserWithPermissionQueryResult result = underTest.findUsersWithPermissionTemplate(dbSession, PermissionQuery.builder().permission("user").template("my_template").build()); - assertThat(result.users()).hasSize(1); - assertThat(result.total()).isEqualTo(1); - } - - @Test - public void fail_to_find_users_from_permission_template_when_template_not_found() { - when(permissionTemplateDao.selectByUuid(anyString())).thenReturn(null); - - try { - underTest.findUsersWithPermissionTemplate(dbSession, PermissionQuery.builder().permission("user").template("Unknown").build()); - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(NotFoundException.class).hasMessage("Template 'Unknown' does not exist"); - } - } - - @Test - public void find_groups_from_permission_template() { - when(permissionTemplateDao.selectByUuid(anyString())).thenReturn(new PermissionTemplateDto().setId(1L).setUuid("my_template")); - - when(permissionTemplateDao.selectGroups(any(DbSession.class), any(PermissionQuery.class), anyLong())).thenReturn( - newArrayList(new GroupWithPermissionDto().setName("users").setPermission("user")) - ); - - GroupWithPermissionQueryResult result = underTest.findGroupsWithPermissionTemplate(dbSession, - PermissionQuery.builder().permission("user").template("my_template").membership(PermissionQuery.OUT).build()); - assertThat(result.groups()).hasSize(1); - } - - @Test - public void fail_to_find_groups_from_permission_template_when_template_not_found() { - when(permissionTemplateDao.selectByUuid(anyString())).thenReturn(null); - - try { - underTest.findGroupsWithPermissionTemplate(dbSession, PermissionQuery.builder().permission("user").template("Unknown").build()); - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(NotFoundException.class).hasMessage("Template 'Unknown' does not exist"); - } - } -} diff --git a/sonar-db/src/main/java/org/sonar/core/permission/GroupWithPermission.java b/sonar-db/src/main/java/org/sonar/core/permission/GroupWithPermission.java deleted file mode 100644 index 29f3a296c2b..00000000000 --- a/sonar-db/src/main/java/org/sonar/core/permission/GroupWithPermission.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.core.permission; - -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import org.apache.commons.lang.builder.ToStringBuilder; -import org.apache.commons.lang.builder.ToStringStyle; - -public class GroupWithPermission { - - private long id; - private String name; - private String description; - private boolean hasPermission; - - public long id() { - return id; - } - - public GroupWithPermission setId(Long id) { - this.id = id; - return this; - } - - public String name() { - return name; - } - - public GroupWithPermission setName(String name) { - this.name = name; - return this; - } - - @CheckForNull - public String description() { - return description; - } - - public GroupWithPermission setDescription(@Nullable String description) { - this.description = description; - return this; - } - - public boolean hasPermission() { - return hasPermission; - } - - public GroupWithPermission hasPermission(boolean hasPermission) { - this.hasPermission = hasPermission; - return this; - } - - @Override - public String toString() { - return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - GroupWithPermission that = (GroupWithPermission) o; - return name.equals(that.name); - } - - @Override - public int hashCode() { - return name.hashCode(); - } -} diff --git a/sonar-db/src/main/java/org/sonar/core/permission/UserWithPermission.java b/sonar-db/src/main/java/org/sonar/core/permission/UserWithPermission.java deleted file mode 100644 index 7e8080d4ccb..00000000000 --- a/sonar-db/src/main/java/org/sonar/core/permission/UserWithPermission.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.core.permission; - -import org.apache.commons.lang.builder.ToStringBuilder; -import org.apache.commons.lang.builder.ToStringStyle; - -public class UserWithPermission { - - private String login; - private String name; - private String email; - private boolean hasPermission; - - public String login() { - return login; - } - - public UserWithPermission setLogin(String login) { - this.login = login; - return this; - } - - public String name() { - return name; - } - - public UserWithPermission setName(String name) { - this.name = name; - return this; - } - - public String email() { - return email; - } - - public UserWithPermission setEmail(String email) { - this.email = email; - return this; - } - - public boolean hasPermission() { - return hasPermission; - } - - public UserWithPermission hasPermission(boolean hasPermission) { - this.hasPermission = hasPermission; - return this; - } - - @Override - public String toString() { - return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - UserWithPermission that = (UserWithPermission) o; - return login.equals(that.login); - } - - @Override - public int hashCode() { - return login.hashCode(); - } -} diff --git a/sonar-db/src/main/java/org/sonar/db/permission/GroupWithPermissionDto.java b/sonar-db/src/main/java/org/sonar/db/permission/GroupWithPermissionDto.java index 3df694d5814..521a87439d8 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/GroupWithPermissionDto.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/GroupWithPermissionDto.java @@ -22,7 +22,6 @@ package org.sonar.db.permission; import javax.annotation.CheckForNull; import javax.annotation.Nullable; -import org.sonar.core.permission.GroupWithPermission; public class GroupWithPermissionDto { @@ -67,12 +66,4 @@ public class GroupWithPermissionDto { this.description = description; return this; } - - public GroupWithPermission toGroupWithPermission() { - return new GroupWithPermission() - .setId(id) - .setName(name) - .setDescription(description) - .hasPermission(permission != null); - } } diff --git a/sonar-db/src/main/java/org/sonar/db/permission/UserWithPermissionDto.java b/sonar-db/src/main/java/org/sonar/db/permission/UserWithPermissionDto.java index efa0d17c07c..15ed345eb11 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/UserWithPermissionDto.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/UserWithPermissionDto.java @@ -22,7 +22,6 @@ package org.sonar.db.permission; import javax.annotation.CheckForNull; import javax.annotation.Nullable; -import org.sonar.core.permission.UserWithPermission; public class UserWithPermissionDto { @@ -67,12 +66,4 @@ public class UserWithPermissionDto { this.permission = permission; return this; } - - public UserWithPermission toUserWithPermission() { - return new UserWithPermission() - .setLogin(login) - .setName(name) - .setEmail(email) - .hasPermission(permission != null); - } } diff --git a/sonar-db/src/test/java/org/sonar/db/permission/GroupWithPermissionDtoTest.java b/sonar-db/src/test/java/org/sonar/db/permission/GroupWithPermissionDtoTest.java deleted file mode 100644 index 04101abeb04..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/permission/GroupWithPermissionDtoTest.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.db.permission; - -import org.junit.Test; -import org.sonar.core.permission.GroupWithPermission; - -import static org.assertj.core.api.Assertions.assertThat; - -public class GroupWithPermissionDtoTest { - - @Test - public void to_group_with_permission_having_permission() { - GroupWithPermission group = new GroupWithPermissionDto() - .setName("users") - .setDescription("desc") - .setPermission("user") - .toGroupWithPermission(); - - assertThat(group.name()).isEqualTo("users"); - assertThat(group.description()).isEqualTo("desc"); - assertThat(group.hasPermission()).isTrue(); - } - - @Test - public void to_group_with_permission_not_having_permission() { - GroupWithPermission group = new GroupWithPermissionDto() - .setName("users") - .setPermission(null) - .toGroupWithPermission(); - - assertThat(group.name()).isEqualTo("users"); - assertThat(group.description()).isNull(); - assertThat(group.hasPermission()).isFalse(); - } -} diff --git a/sonar-db/src/test/java/org/sonar/db/permission/GroupWithPermissionTest.java b/sonar-db/src/test/java/org/sonar/db/permission/GroupWithPermissionTest.java deleted file mode 100644 index 7ddc78d1308..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/permission/GroupWithPermissionTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.db.permission; - -import org.junit.Test; -import org.sonar.core.permission.GroupWithPermission; - -import static org.assertj.core.api.Assertions.assertThat; - -public class GroupWithPermissionTest { - - @Test - public void test_setters_and_getters() throws Exception { - GroupWithPermission user = new GroupWithPermission() - .setName("users") - .hasPermission(true); - - assertThat(user.name()).isEqualTo("users"); - assertThat(user.hasPermission()).isTrue(); - } - - @Test - public void test_equals() throws Exception { - assertThat(new GroupWithPermission().setName("users")).isEqualTo(new GroupWithPermission().setName("users")); - assertThat(new GroupWithPermission().setName("users")).isNotEqualTo(new GroupWithPermission().setName("reviewers")); - - GroupWithPermission group = new GroupWithPermission() - .setName("users") - .hasPermission(true); - assertThat(group).isEqualTo(group); - } - - @Test - public void test_hashcode() throws Exception { - assertThat(new GroupWithPermission().setName("users").hashCode()).isEqualTo(new GroupWithPermission().setName("users").hashCode()); - assertThat(new GroupWithPermission().setName("users").hashCode()).isNotEqualTo(new GroupWithPermission().setName("reviewers").hashCode()); - } - -} diff --git a/sonar-db/src/test/java/org/sonar/db/permission/UserWithPermissionDtoTest.java b/sonar-db/src/test/java/org/sonar/db/permission/UserWithPermissionDtoTest.java deleted file mode 100644 index 56d951ebb2d..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/permission/UserWithPermissionDtoTest.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.db.permission; - -import org.junit.Test; -import org.sonar.core.permission.UserWithPermission; - -import static org.assertj.core.api.Assertions.assertThat; - -public class UserWithPermissionDtoTest { - - @Test - public void to_user_with_permission_having_permission() { - UserWithPermission user = new UserWithPermissionDto() - .setName("Arthur") - .setLogin("arthur") - .setPermission("user") - .toUserWithPermission(); - - assertThat(user.name()).isEqualTo("Arthur"); - assertThat(user.login()).isEqualTo("arthur"); - assertThat(user.hasPermission()).isTrue(); - } - - @Test - public void to_user_with_permission_not_having_permission() { - UserWithPermission user = new UserWithPermissionDto() - .setName("Arthur") - .setLogin("arthur") - .setPermission(null) - .toUserWithPermission(); - - assertThat(user.name()).isEqualTo("Arthur"); - assertThat(user.login()).isEqualTo("arthur"); - assertThat(user.hasPermission()).isFalse(); - } -} diff --git a/sonar-db/src/test/java/org/sonar/db/permission/UserWithPermissionTest.java b/sonar-db/src/test/java/org/sonar/db/permission/UserWithPermissionTest.java deleted file mode 100644 index baada745f8c..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/permission/UserWithPermissionTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.db.permission; - -import org.junit.Test; -import org.sonar.core.permission.UserWithPermission; - -import static org.assertj.core.api.Assertions.assertThat; - -public class UserWithPermissionTest { - - @Test - public void test_setters_and_getters() throws Exception { - UserWithPermission user = new UserWithPermission() - .setName("Arthur") - .setLogin("arthur") - .hasPermission(true); - - assertThat(user.name()).isEqualTo("Arthur"); - assertThat(user.login()).isEqualTo("arthur"); - assertThat(user.hasPermission()).isTrue(); - } - - @Test - public void test_equals() throws Exception { - assertThat(new UserWithPermission().setLogin("arthur")).isEqualTo(new UserWithPermission().setLogin("arthur")); - assertThat(new UserWithPermission().setLogin("arthur")).isNotEqualTo(new UserWithPermission().setLogin("john")); - - UserWithPermission user = new UserWithPermission() - .setName("Arthur") - .setLogin("arthur") - .hasPermission(true); - assertThat(user).isEqualTo(user); - } - - @Test - public void test_hashcode() throws Exception { - assertThat(new UserWithPermission().setLogin("arthur").hashCode()).isEqualTo(new UserWithPermission().setLogin("arthur").hashCode()); - assertThat(new UserWithPermission().setLogin("arthur").hashCode()).isNotEqualTo(new UserWithPermission().setLogin("john").hashCode()); - } - -} |