Remove the unused Ruby WS /user_groups/search and the related Java code.tags/6.2-RC1
@@ -52,7 +52,7 @@ public class PermissionDependenciesFinder { | |||
/** | |||
* @throws org.sonar.server.exceptions.NotFoundException if a project identifier is provided but it's not found | |||
*/ | |||
public Optional<ComponentDto> searchProject(DbSession dbSession, Optional<WsProjectRef> optionalWsProjectRef) { | |||
Optional<ComponentDto> searchProject(DbSession dbSession, Optional<WsProjectRef> optionalWsProjectRef) { | |||
if (!optionalWsProjectRef.isPresent()) { | |||
return Optional.absent(); | |||
} |
@@ -1,42 +0,0 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2016 SonarSource SA | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* This program 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. | |||
* | |||
* This program 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.ws; | |||
import org.sonar.api.server.ws.WebService.SelectionMode; | |||
import org.sonar.db.user.GroupMembershipQuery; | |||
class PermissionQueryParser { | |||
private PermissionQueryParser() { | |||
// Utility class | |||
} | |||
public static String fromSelectionModeToMembership(String selectionModeString) { | |||
SelectionMode selectionMode = SelectionMode.fromParam(selectionModeString); | |||
if (SelectionMode.SELECTED == selectionMode) { | |||
return GroupMembershipQuery.IN; | |||
} else if (SelectionMode.DESELECTED == selectionMode) { | |||
return GroupMembershipQuery.OUT; | |||
} else { | |||
return GroupMembershipQuery.ANY; | |||
} | |||
} | |||
} |
@@ -243,8 +243,6 @@ import org.sonar.server.updatecenter.UpdateCenterModule; | |||
import org.sonar.server.user.DefaultUserFinder; | |||
import org.sonar.server.user.DefaultUserService; | |||
import org.sonar.server.user.DeprecatedUserFinder; | |||
import org.sonar.server.user.GroupMembershipFinder; | |||
import org.sonar.server.user.GroupMembershipService; | |||
import org.sonar.server.user.NewUserNotifier; | |||
import org.sonar.server.user.SecurityRealmFactory; | |||
import org.sonar.server.user.UserUpdater; | |||
@@ -428,8 +426,6 @@ public class PlatformLevel4 extends PlatformLevel { | |||
UserTokenModule.class, | |||
// groups | |||
GroupMembershipService.class, | |||
GroupMembershipFinder.class, | |||
UserGroupsModule.class, | |||
// permissions |
@@ -1,93 +0,0 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2016 SonarSource SA | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* This program 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. | |||
* | |||
* This program 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.user; | |||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
import org.apache.commons.lang.builder.ToStringBuilder; | |||
import org.apache.commons.lang.builder.ToStringStyle; | |||
public class GroupMembership { | |||
private Long id; | |||
private String name; | |||
private String description; | |||
private boolean isMember; | |||
public Long id() { | |||
return id; | |||
} | |||
public GroupMembership setId(Long id) { | |||
this.id = id; | |||
return this; | |||
} | |||
public String name() { | |||
return name; | |||
} | |||
public GroupMembership setName(String name) { | |||
this.name = name; | |||
return this; | |||
} | |||
@CheckForNull | |||
public String description() { | |||
return description; | |||
} | |||
public GroupMembership setDescription(@Nullable String description) { | |||
this.description = description; | |||
return this; | |||
} | |||
public boolean isMember() { | |||
return isMember; | |||
} | |||
public GroupMembership setMember(boolean isMember) { | |||
this.isMember = isMember; | |||
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; | |||
} | |||
GroupMembership that = (GroupMembership) o; | |||
return name.equals(that.name); | |||
} | |||
@Override | |||
public int hashCode() { | |||
return name.hashCode(); | |||
} | |||
} |
@@ -1,99 +0,0 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2016 SonarSource SA | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* This program 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. | |||
* | |||
* This program 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.user; | |||
import java.util.List; | |||
import org.sonar.api.server.ServerSide; | |||
import org.sonar.db.user.GroupMembershipDao; | |||
import org.sonar.db.user.GroupMembershipDto; | |||
import org.sonar.db.user.GroupMembershipQuery; | |||
import org.sonar.db.user.UserDao; | |||
import org.sonar.db.user.UserDto; | |||
import org.sonar.server.exceptions.NotFoundException; | |||
import static com.google.common.collect.Lists.newArrayList; | |||
@ServerSide | |||
public class GroupMembershipFinder { | |||
public static class Membership { | |||
private List<GroupMembership> groups; | |||
private boolean hasMoreResults; | |||
private Membership(List<GroupMembership> groups, boolean hasMoreResults) { | |||
this.groups = groups; | |||
this.hasMoreResults = hasMoreResults; | |||
} | |||
public List<GroupMembership> groups() { | |||
return groups; | |||
} | |||
public boolean hasMoreResults() { | |||
return hasMoreResults; | |||
} | |||
} | |||
private final UserDao userDao; | |||
private final GroupMembershipDao groupMembershipDao; | |||
public GroupMembershipFinder(UserDao userDao, GroupMembershipDao groupMembershipDao) { | |||
this.userDao = userDao; | |||
this.groupMembershipDao = groupMembershipDao; | |||
} | |||
public Membership find(GroupMembershipQuery query) { | |||
Long userId = userId(query.login()); | |||
int pageSize = query.pageSize(); | |||
int pageIndex = query.pageIndex(); | |||
int offset = (pageIndex - 1) * pageSize; | |||
// Add one to page size in order to be able to know if there's more results or not | |||
int limit = pageSize + 1; | |||
List<GroupMembershipDto> dtos = groupMembershipDao.selectGroups(query, userId, offset, limit); | |||
boolean hasMoreResults = false; | |||
if (dtos.size() == limit) { | |||
hasMoreResults = true; | |||
// Removed last entry as it's only need to know if there more results or not | |||
dtos.remove(dtos.size() - 1); | |||
} | |||
return new Membership(toGroupMembership(dtos), hasMoreResults); | |||
} | |||
private Long userId(String login) { | |||
UserDto userDto = userDao.selectActiveUserByLogin(login); | |||
if (userDto == null) { | |||
throw new NotFoundException("User '" + login + "' does not exists."); | |||
} | |||
return userDto.getId(); | |||
} | |||
private static List<GroupMembership> toGroupMembership(List<GroupMembershipDto> dtos) { | |||
List<GroupMembership> groups = newArrayList(); | |||
for (GroupMembershipDto dto : dtos) { | |||
groups.add(new GroupMembership() | |||
.setId(dto.getId()) | |||
.setName(dto.getName()) | |||
.setDescription(dto.getDescription()) | |||
.setMember(dto.getUserId() != null)); | |||
} | |||
return groups; | |||
} | |||
} |
@@ -1,66 +0,0 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2016 SonarSource SA | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* This program 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. | |||
* | |||
* This program 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.user; | |||
import java.util.Map; | |||
import org.sonar.api.server.ServerSide; | |||
import org.sonar.db.user.GroupMembershipQuery; | |||
import org.sonar.server.util.RubyUtils; | |||
/** | |||
* Used by ruby code <pre>Internal.group_membership</pre> | |||
*/ | |||
@ServerSide | |||
public class GroupMembershipService { | |||
private static final String SELECTED_MEMBERSHIP = "selected"; | |||
private static final String DESELECTED_MEMBERSHIP = "deselected"; | |||
private final GroupMembershipFinder finder; | |||
public GroupMembershipService(GroupMembershipFinder finder) { | |||
this.finder = finder; | |||
} | |||
public GroupMembershipFinder.Membership find(Map<String, Object> params) { | |||
return finder.find(parseQuery(params)); | |||
} | |||
private static GroupMembershipQuery parseQuery(Map<String, Object> params) { | |||
GroupMembershipQuery.Builder builder = GroupMembershipQuery.builder(); | |||
builder.membership(membership(params)); | |||
builder.groupSearch((String) params.get("query")); | |||
builder.pageIndex(RubyUtils.toInteger(params.get("page"))); | |||
builder.pageSize(RubyUtils.toInteger(params.get("pageSize"))); | |||
builder.login((String) params.get("user")); | |||
return builder.build(); | |||
} | |||
private static String membership(Map<String, Object> params) { | |||
String selected = (String) params.get("selected"); | |||
if (SELECTED_MEMBERSHIP.equals(selected)) { | |||
return GroupMembershipQuery.IN; | |||
} | |||
if (DESELECTED_MEMBERSHIP.equals(selected)) { | |||
return GroupMembershipQuery.OUT; | |||
} | |||
return GroupMembershipQuery.ANY; | |||
} | |||
} |
@@ -1,121 +0,0 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2016 SonarSource SA | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* This program 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. | |||
* | |||
* This program 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.user; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
import org.mockito.ArgumentCaptor; | |||
import org.sonar.db.user.GroupMembershipDao; | |||
import org.sonar.db.user.GroupMembershipDto; | |||
import org.sonar.db.user.GroupMembershipQuery; | |||
import org.sonar.db.user.UserDao; | |||
import org.sonar.db.user.UserDto; | |||
import static com.google.common.collect.Lists.newArrayList; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.mockito.Mockito.anyInt; | |||
import static org.mockito.Mockito.anyLong; | |||
import static org.mockito.Mockito.eq; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.verify; | |||
import static org.mockito.Mockito.when; | |||
public class GroupMembershipFinderTest { | |||
UserDao userDao = mock(UserDao.class); | |||
GroupMembershipDao groupMembershipDao = mock(GroupMembershipDao.class); | |||
GroupMembershipFinder finder; | |||
@Before | |||
public void setUp() { | |||
when(userDao.selectActiveUserByLogin("arthur")).thenReturn(new UserDto().setId(100L).setName("arthur")); | |||
finder = new GroupMembershipFinder(userDao, groupMembershipDao); | |||
} | |||
@Test | |||
public void find() { | |||
GroupMembershipQuery query = GroupMembershipQuery.builder().login("arthur").build(); | |||
when(groupMembershipDao.selectGroups(eq(query), anyLong(), anyInt(), anyInt())).thenReturn( | |||
newArrayList(new GroupMembershipDto().setId(1L).setName("users").setDescription("Users group").setUserId(100L)) | |||
); | |||
GroupMembershipFinder.Membership result = finder.find(query); | |||
assertThat(result.groups()).hasSize(1); | |||
assertThat(result.hasMoreResults()).isFalse(); | |||
GroupMembership group = result.groups().get(0); | |||
assertThat(group.id()).isEqualTo(1); | |||
assertThat(group.name()).isEqualTo("users"); | |||
assertThat(group.description()).isEqualTo("Users group"); | |||
assertThat(group.isMember()).isTrue(); | |||
} | |||
@Test | |||
public void find_with_paging() { | |||
GroupMembershipQuery query = GroupMembershipQuery.builder().login("arthur").pageIndex(3).pageSize(10).build(); | |||
finder.find(query); | |||
ArgumentCaptor<Integer> argumentOffset = ArgumentCaptor.forClass(Integer.class); | |||
ArgumentCaptor<Integer> argumentLimit = ArgumentCaptor.forClass(Integer.class); | |||
verify(groupMembershipDao).selectGroups(eq(query), anyLong(), argumentOffset.capture(), argumentLimit.capture()); | |||
assertThat(argumentOffset.getValue()).isEqualTo(20); | |||
assertThat(argumentLimit.getValue()).isEqualTo(11); | |||
} | |||
@Test | |||
public void find_with_paging_having_more_results() { | |||
GroupMembershipQuery query = GroupMembershipQuery.builder().login("arthur").pageIndex(1).pageSize(2).build(); | |||
when(groupMembershipDao.selectGroups(eq(query), anyLong(), anyInt(), anyInt())).thenReturn(newArrayList( | |||
new GroupMembershipDto().setId(1L).setName("group1"), | |||
new GroupMembershipDto().setId(2L).setName("group2"), | |||
new GroupMembershipDto().setId(3L).setName("group3")) | |||
); | |||
GroupMembershipFinder.Membership result = finder.find(query); | |||
ArgumentCaptor<Integer> argumentOffset = ArgumentCaptor.forClass(Integer.class); | |||
ArgumentCaptor<Integer> argumentLimit = ArgumentCaptor.forClass(Integer.class); | |||
verify(groupMembershipDao).selectGroups(eq(query), anyLong(), argumentOffset.capture(), argumentLimit.capture()); | |||
assertThat(argumentOffset.getValue()).isEqualTo(0); | |||
assertThat(argumentLimit.getValue()).isEqualTo(3); | |||
assertThat(result.hasMoreResults()).isTrue(); | |||
} | |||
@Test | |||
public void find_with_paging_having_no_more_results() { | |||
GroupMembershipQuery query = GroupMembershipQuery.builder().login("arthur").pageIndex(1).pageSize(10).build(); | |||
when(groupMembershipDao.selectGroups(eq(query), anyLong(), anyInt(), anyInt())).thenReturn(newArrayList( | |||
new GroupMembershipDto().setId(1L).setName("group1"), | |||
new GroupMembershipDto().setId(2L).setName("group2"), | |||
new GroupMembershipDto().setId(3L).setName("group3"), | |||
new GroupMembershipDto().setId(4L).setName("group4")) | |||
); | |||
GroupMembershipFinder.Membership result = finder.find(query); | |||
ArgumentCaptor<Integer> argumentOffset = ArgumentCaptor.forClass(Integer.class); | |||
ArgumentCaptor<Integer> argumentLimit = ArgumentCaptor.forClass(Integer.class); | |||
verify(groupMembershipDao).selectGroups(eq(query), anyLong(), argumentOffset.capture(), argumentLimit.capture()); | |||
assertThat(argumentOffset.getValue()).isEqualTo(0); | |||
assertThat(argumentLimit.getValue()).isEqualTo(11); | |||
assertThat(result.hasMoreResults()).isFalse(); | |||
} | |||
} |
@@ -1,164 +0,0 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2016 SonarSource SA | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* This program 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. | |||
* | |||
* This program 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.user; | |||
import com.google.common.collect.ImmutableMap; | |||
import java.util.List; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.sonar.api.utils.System2; | |||
import org.sonar.db.DbTester; | |||
import org.sonar.server.exceptions.NotFoundException; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.junit.Assert.fail; | |||
/** | |||
* Use BbUnit tests because there's no IT on this feature for the moment | |||
*/ | |||
public class GroupMembershipServiceTest { | |||
@Rule | |||
public DbTester dbTester = DbTester.create(System2.INSTANCE); | |||
GroupMembershipService service = new GroupMembershipService(new GroupMembershipFinder(dbTester.getDbClient().userDao(), dbTester.getDbClient().groupMembershipDao())); | |||
@Test | |||
public void find_all_member_groups() { | |||
dbTester.prepareDbUnit(getClass(), "shared.xml"); | |||
GroupMembershipFinder.Membership queryResult = service.find(ImmutableMap.<String, Object>of( | |||
"user", "user1", | |||
"selected", "all")); | |||
List<GroupMembership> result = queryResult.groups(); | |||
assertThat(result).hasSize(3); | |||
check(result.get(0), "sonar-administrators", false); | |||
check(result.get(1), "sonar-reviewers", false); | |||
check(result.get(2), "sonar-users", true); | |||
} | |||
@Test | |||
public void find_all_member_groups_when_no_selected_parameter() { | |||
dbTester.prepareDbUnit(getClass(), "shared.xml"); | |||
GroupMembershipFinder.Membership queryResult = service.find(ImmutableMap.<String, Object>of( | |||
"user", "user1")); | |||
List<GroupMembership> result = queryResult.groups(); | |||
assertThat(result).hasSize(3); | |||
check(result.get(0), "sonar-administrators", false); | |||
check(result.get(1), "sonar-reviewers", false); | |||
check(result.get(2), "sonar-users", true); | |||
} | |||
@Test | |||
public void find_member_groups() { | |||
dbTester.prepareDbUnit(getClass(), "shared.xml"); | |||
GroupMembershipFinder.Membership queryResult = service.find(ImmutableMap.<String, Object>of( | |||
"user", "user1", | |||
"selected", "selected")); | |||
List<GroupMembership> result = queryResult.groups(); | |||
assertThat(result).hasSize(1); | |||
check(result.get(0), "sonar-users", true); | |||
} | |||
@Test | |||
public void find_not_member_groups() { | |||
dbTester.prepareDbUnit(getClass(), "shared.xml"); | |||
GroupMembershipFinder.Membership queryResult = service.find(ImmutableMap.<String, Object>of( | |||
"user", "user1", | |||
"selected", "deselected")); | |||
List<GroupMembership> result = queryResult.groups(); | |||
assertThat(result).hasSize(2); | |||
check(result.get(0), "sonar-administrators", false); | |||
check(result.get(1), "sonar-reviewers", false); | |||
} | |||
@Test | |||
public void find_with_paging_with_more_results() { | |||
dbTester.prepareDbUnit(getClass(), "shared.xml"); | |||
GroupMembershipFinder.Membership queryResult = service.find(ImmutableMap.<String, Object>of( | |||
"user", "user1", | |||
"selected", "all", | |||
"page", 1, | |||
"pageSize", 2 | |||
)); | |||
List<GroupMembership> result = queryResult.groups(); | |||
assertThat(result).hasSize(2); | |||
assertThat(queryResult.hasMoreResults()).isTrue(); | |||
} | |||
@Test | |||
public void find_with_paging_with_no_more_results() { | |||
dbTester.prepareDbUnit(getClass(), "shared.xml"); | |||
GroupMembershipFinder.Membership queryResult = service.find(ImmutableMap.<String, Object>of( | |||
"user", "user1", | |||
"selected", "all", | |||
"page", 3, | |||
"pageSize", 1 | |||
)); | |||
List<GroupMembership> result = queryResult.groups(); | |||
assertThat(result).hasSize(1); | |||
assertThat(queryResult.hasMoreResults()).isFalse(); | |||
} | |||
@Test | |||
public void fail_if_user_not_found() { | |||
dbTester.prepareDbUnit(getClass(), "shared.xml"); | |||
try { | |||
service.find(ImmutableMap.<String, Object>of( | |||
"user", "user_not_existing", | |||
"selected", "all")); | |||
fail(); | |||
} catch (Exception e) { | |||
assertThat(e).isInstanceOf(NotFoundException.class).hasMessage("User 'user_not_existing' does not exists."); | |||
} | |||
} | |||
@Test | |||
public void find_matched_groups_name() { | |||
dbTester.prepareDbUnit(getClass(), "shared.xml"); | |||
GroupMembershipFinder.Membership queryResult = service.find(ImmutableMap.<String, Object>of( | |||
"user", "user1", | |||
"selected", "all", | |||
"query", "user")); | |||
List<GroupMembership> result = queryResult.groups(); | |||
assertThat(result).hasSize(1); | |||
check(result.get(0), "sonar-users", true); | |||
queryResult = service.find(ImmutableMap.<String, Object>of( | |||
"user", "user1", | |||
"selected", "all", | |||
"query", "sonar")); | |||
result = queryResult.groups(); | |||
assertThat(result).hasSize(3); | |||
} | |||
private void check(GroupMembership groupMembership, String expectedName, boolean isMember) { | |||
assertThat(groupMembership.name()).isEqualTo(expectedName); | |||
assertThat(groupMembership.isMember()).isEqualTo(isMember); | |||
} | |||
} |
@@ -1,52 +0,0 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2016 SonarSource SA | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* This program 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. | |||
* | |||
* This program 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.user; | |||
import org.junit.Test; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
public class GroupMembershipTest { | |||
@Test | |||
public void test_setters_and_getters() throws Exception { | |||
GroupMembership group = new GroupMembership() | |||
.setId(1L) | |||
.setName("users") | |||
.setMember(true); | |||
assertThat(group.id()).isEqualTo(1L); | |||
assertThat(group.name()).isEqualTo("users"); | |||
assertThat(group.isMember()).isTrue(); | |||
} | |||
@Test | |||
public void test_equals() throws Exception { | |||
assertThat(new GroupMembership().setName("users")).isEqualTo(new GroupMembership().setName("users")); | |||
assertThat(new GroupMembership().setName("users")).isNotEqualTo(new GroupMembership().setName("reviewers")); | |||
GroupMembership group = new GroupMembership() | |||
.setId(1L) | |||
.setName("users") | |||
.setMember(true); | |||
assertThat(group).isEqualTo(group); | |||
} | |||
} |
@@ -21,6 +21,7 @@ package org.sonar.server.user; | |||
import com.google.common.base.Strings; | |||
import com.google.common.collect.ImmutableList; | |||
import com.google.common.collect.Multimap; | |||
import java.util.List; | |||
import org.elasticsearch.search.SearchHit; | |||
import org.junit.Before; | |||
@@ -36,7 +37,6 @@ import org.sonar.db.DbSession; | |||
import org.sonar.db.DbTester; | |||
import org.sonar.db.user.GroupDao; | |||
import org.sonar.db.user.GroupDto; | |||
import org.sonar.db.user.GroupMembershipQuery; | |||
import org.sonar.db.user.UserDao; | |||
import org.sonar.db.user.UserDto; | |||
import org.sonar.db.user.UserTesting; | |||
@@ -49,6 +49,7 @@ import org.sonar.server.user.index.UserIndexer; | |||
import org.sonar.server.util.Validation; | |||
import static com.google.common.collect.Lists.newArrayList; | |||
import static java.util.Arrays.asList; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.assertj.core.data.MapEntry.entry; | |||
import static org.junit.Assert.fail; | |||
@@ -83,7 +84,6 @@ public class UserUpdaterTest { | |||
Settings settings = new MapSettings(); | |||
UserDao userDao = dbClient.userDao(); | |||
GroupDao groupDao = dbClient.groupDao(); | |||
GroupMembershipFinder groupMembershipFinder = new GroupMembershipFinder(userDao, dbClient.groupMembershipDao()); | |||
DbSession session = db.getSession(); | |||
UserIndexer userIndexer; | |||
@@ -442,10 +442,8 @@ public class UserUpdaterTest { | |||
.setPassword("password") | |||
.setScmAccounts(newArrayList("u1", "u_1"))); | |||
GroupMembershipFinder.Membership membership = groupMembershipFinder.find(GroupMembershipQuery.builder().login("user").build()); | |||
assertThat(membership.groups()).hasSize(1); | |||
assertThat(membership.groups().get(0).name()).isEqualTo("sonar-users"); | |||
assertThat(membership.groups().get(0).isMember()).isTrue(); | |||
Multimap<String, String> groups = dbClient.groupMembershipDao().selectGroupsByLogins(session, asList("user")); | |||
assertThat(groups.get("user")).containsOnly("sonar-users"); | |||
} | |||
@Test | |||
@@ -486,8 +484,7 @@ public class UserUpdaterTest { | |||
addUser(newDisabledUser(DEFAULT_LOGIN) | |||
.setLocal(false) | |||
.setCreatedAt(PAST) | |||
.setUpdatedAt(PAST) | |||
); | |||
.setUpdatedAt(PAST)); | |||
createDefaultGroup(); | |||
boolean result = userUpdater.create(NewUser.create() | |||
@@ -543,8 +540,7 @@ public class UserUpdaterTest { | |||
addUser(newDisabledUser(DEFAULT_LOGIN) | |||
.setLocal(true) | |||
.setCreatedAt(PAST) | |||
.setUpdatedAt(PAST) | |||
); | |||
.setUpdatedAt(PAST)); | |||
createDefaultGroup(); | |||
userUpdater.create(NewUser.create() | |||
@@ -589,10 +585,8 @@ public class UserUpdaterTest { | |||
.setPassword("password2")); | |||
session.commit(); | |||
GroupMembershipFinder.Membership membership = groupMembershipFinder.find(GroupMembershipQuery.builder().login(DEFAULT_LOGIN).groupSearch("sonar-users").build()); | |||
assertThat(membership.groups()).hasSize(1); | |||
assertThat(membership.groups().get(0).name()).isEqualTo("sonar-users"); | |||
assertThat(membership.groups().get(0).isMember()).isTrue(); | |||
Multimap<String, String> groups = dbClient.groupMembershipDao().selectGroupsByLogins(session, asList(DEFAULT_LOGIN)); | |||
assertThat(groups.get(DEFAULT_LOGIN).stream().anyMatch(g -> g.equals("sonar-users"))).isTrue(); | |||
} | |||
@Test | |||
@@ -633,8 +627,7 @@ public class UserUpdaterTest { | |||
public void update_user_external_identity_when_user_was_not_local() { | |||
addUser(UserTesting.newExternalUser(DEFAULT_LOGIN, "Marius", "marius@email.com") | |||
.setCreatedAt(PAST) | |||
.setUpdatedAt(PAST) | |||
); | |||
.setUpdatedAt(PAST)); | |||
createDefaultGroup(); | |||
userUpdater.update(UpdateUser.create(DEFAULT_LOGIN) | |||
@@ -655,8 +648,7 @@ public class UserUpdaterTest { | |||
public void update_user_external_identity_when_user_was_local() { | |||
addUser(UserTesting.newLocalUser(DEFAULT_LOGIN, "Marius", "marius@email.com") | |||
.setCreatedAt(PAST) | |||
.setUpdatedAt(PAST) | |||
); | |||
.setUpdatedAt(PAST)); | |||
createDefaultGroup(); | |||
userUpdater.update(UpdateUser.create(DEFAULT_LOGIN) | |||
@@ -878,10 +870,8 @@ public class UserUpdaterTest { | |||
.setScmAccounts(newArrayList("ma2"))); | |||
session.commit(); | |||
GroupMembershipFinder.Membership membership = groupMembershipFinder.find(GroupMembershipQuery.builder().login(DEFAULT_LOGIN).groupSearch("sonar-users").build()); | |||
assertThat(membership.groups()).hasSize(1); | |||
assertThat(membership.groups().get(0).name()).isEqualTo("sonar-users"); | |||
assertThat(membership.groups().get(0).isMember()).isFalse(); | |||
Multimap<String, String> groups = dbClient.groupMembershipDao().selectGroupsByLogins(session, asList(DEFAULT_LOGIN)); | |||
assertThat(groups.get(DEFAULT_LOGIN).stream().anyMatch(g -> g.equals("sonar-users"))).isFalse(); | |||
} | |||
@Test | |||
@@ -891,10 +881,8 @@ public class UserUpdaterTest { | |||
session.commit(); | |||
// User is already associate to the default group | |||
GroupMembershipFinder.Membership membership = groupMembershipFinder.find(GroupMembershipQuery.builder().login(DEFAULT_LOGIN).groupSearch("sonar-users").build()); | |||
assertThat(membership.groups()).hasSize(1); | |||
assertThat(membership.groups().get(0).name()).isEqualTo("sonar-users"); | |||
assertThat(membership.groups().get(0).isMember()).isTrue(); | |||
Multimap<String, String> groups = dbClient.groupMembershipDao().selectGroupsByLogins(session, asList(DEFAULT_LOGIN)); | |||
assertThat(groups.get(DEFAULT_LOGIN).stream().anyMatch(g -> g.equals("sonar-users"))).isTrue(); | |||
userUpdater.update(UpdateUser.create(DEFAULT_LOGIN) | |||
.setName("Marius2") | |||
@@ -904,10 +892,8 @@ public class UserUpdaterTest { | |||
session.commit(); | |||
// Nothing as changed | |||
membership = groupMembershipFinder.find(GroupMembershipQuery.builder().login(DEFAULT_LOGIN).groupSearch("sonar-users").build()); | |||
assertThat(membership.groups()).hasSize(1); | |||
assertThat(membership.groups().get(0).name()).isEqualTo("sonar-users"); | |||
assertThat(membership.groups().get(0).isMember()).isTrue(); | |||
groups = dbClient.groupMembershipDao().selectGroupsByLogins(session, asList(DEFAULT_LOGIN)); | |||
assertThat(groups.get(DEFAULT_LOGIN).stream().anyMatch(g -> g.equals("sonar-users"))).isTrue(); | |||
} | |||
@Test |
@@ -76,7 +76,7 @@ public class DeactivateActionTest { | |||
public void setUp() { | |||
System2 system2 = new System2(); | |||
UserDao userDao = new UserDao(db.myBatis(), system2); | |||
dbClient = new DbClient(db.database(), db.myBatis(), userDao, new GroupMembershipDao(db.myBatis()), new UserTokenDao()); | |||
dbClient = new DbClient(db.database(), db.myBatis(), userDao, new GroupMembershipDao(), new UserTokenDao()); | |||
dbSession = db.getSession(); | |||
dbSession.commit(); | |||
@@ -61,7 +61,7 @@ public class GroupsActionTest { | |||
UserDao userDao = new UserDao(dbTester.myBatis(), system2); | |||
GroupDao groupDao = new GroupDao(system2); | |||
UserGroupDao userGroupDao = new UserGroupDao(); | |||
GroupMembershipDao groupMembershipDao = new GroupMembershipDao(dbTester.myBatis()); | |||
GroupMembershipDao groupMembershipDao = new GroupMembershipDao(); | |||
dbClient = new DbClient(dbTester.database(), dbTester.myBatis(), userDao, groupDao, userGroupDao, groupMembershipDao); | |||
session = dbClient.openSession(false); |
@@ -1,22 +0,0 @@ | |||
<dataset> | |||
<groups id="100" name="sonar-administrators"/> | |||
<groups id="101" name="sonar-users"/> | |||
<groups id="102" name="sonar-reviewers"/> | |||
<!-- user 1 is in users group --> | |||
<groups_users user_id="200" group_id="101"/> | |||
<!-- user 2 is in all groups --> | |||
<groups_users user_id="201" group_id="100"/> | |||
<groups_users user_id="201" group_id="101"/> | |||
<groups_users user_id="201" group_id="102"/> | |||
<!-- user 3 is in no group --> | |||
<users id="200" login="user1" name="user1"/> | |||
<users id="201" login="user2" name="user2"/> | |||
<users id="202" login="user3" name="user3"/> | |||
</dataset> |
@@ -1,50 +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. | |||
# | |||
class UserGroupsController < ApplicationController | |||
# | |||
# GET /user_groups/search?user=<login>&selected=<selected>&page=3&pageSize=10&query=<query> | |||
# | |||
# Possible value of 'selected' are 'selected', 'deselected' and 'all' () | |||
# | |||
def search | |||
result = Internal.group_membership.find(params) | |||
groups = result.groups() | |||
more = result.hasMoreResults() | |||
respond_to do |format| | |||
format.json { | |||
render :json => { | |||
:more => more, | |||
:results => groups.map { |group| | |||
hash = { | |||
:id => group.id(), | |||
:name => group.name(), | |||
:selected => group.isMember() | |||
} | |||
hash[:description] = group.description() if group.description() && !group.description().blank? | |||
hash | |||
} | |||
} | |||
} | |||
end | |||
end | |||
end |
@@ -51,10 +51,6 @@ class Internal | |||
component(Java::OrgSonarServerDebt::DebtModelService.java_class) | |||
end | |||
def self.group_membership | |||
component(Java::OrgSonarServerUser::GroupMembershipService.java_class) | |||
end | |||
def self.quality_profiles | |||
component(Java::OrgSonarServerQualityprofile::QProfiles.java_class) | |||
end |
@@ -19,7 +19,6 @@ | |||
*/ | |||
package org.sonar.db.user; | |||
import com.google.common.annotations.VisibleForTesting; | |||
import com.google.common.collect.ArrayListMultimap; | |||
import com.google.common.collect.ImmutableMap; | |||
import com.google.common.collect.Maps; | |||
@@ -31,28 +30,11 @@ import org.apache.ibatis.session.RowBounds; | |||
import org.apache.ibatis.session.SqlSession; | |||
import org.sonar.db.Dao; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.MyBatis; | |||
import static org.sonar.db.DatabaseUtils.executeLargeInputs; | |||
public class GroupMembershipDao implements Dao { | |||
private final MyBatis mybatis; | |||
public GroupMembershipDao(MyBatis mybatis) { | |||
this.mybatis = mybatis; | |||
} | |||
// TODO Remove this method and associated client code when the UI is migrated to Backbone | |||
public List<GroupMembershipDto> selectGroups(GroupMembershipQuery query, Long userId, int offset, int limit) { | |||
SqlSession session = mybatis.openSession(false); | |||
try { | |||
return selectGroups(session, query, userId, offset, limit); | |||
} finally { | |||
MyBatis.closeQuietly(session); | |||
} | |||
} | |||
public List<GroupMembershipDto> selectGroups(SqlSession session, GroupMembershipQuery query, Long userId, int offset, int limit) { | |||
Map<String, Object> params = ImmutableMap.of("query", query, "userId", userId); | |||
return mapper(session).selectGroups(params, new RowBounds(offset, limit)); | |||
@@ -103,11 +85,6 @@ public class GroupMembershipDao implements Dao { | |||
return result; | |||
} | |||
@VisibleForTesting | |||
List<GroupMembershipDto> selectGroups(GroupMembershipQuery query, Long userId) { | |||
return selectGroups(query, userId, 0, Integer.MAX_VALUE); | |||
} | |||
private static GroupMembershipMapper mapper(SqlSession session) { | |||
return session.getMapper(GroupMembershipMapper.class); | |||
} |
@@ -26,8 +26,6 @@ import org.apache.ibatis.session.RowBounds; | |||
public interface GroupMembershipMapper { | |||
List<GroupMembershipDto> selectGroups(GroupMembershipQuery query); | |||
List<GroupMembershipDto> selectGroups(Map<String, Object> params, RowBounds rowBounds); | |||
int countGroups(Map<String, Object> params); |
@@ -38,110 +38,6 @@ public class GroupMembershipDaoTest { | |||
GroupMembershipDao dao = dbTester.getDbClient().groupMembershipDao(); | |||
@Test | |||
public void select_all_groups_by_query() { | |||
dbTester.prepareDbUnit(getClass(), "shared.xml"); | |||
GroupMembershipQuery query = GroupMembershipQuery.builder().login("arthur").build(); | |||
List<GroupMembershipDto> result = dao.selectGroups(query, 200L); | |||
assertThat(result).hasSize(3); | |||
} | |||
@Test | |||
public void select_user_group() { | |||
dbTester.prepareDbUnit(getClass(), "select_user_group.xml"); | |||
GroupMembershipQuery query = GroupMembershipQuery.builder().login("arthur").build(); | |||
List<GroupMembershipDto> result = dao.selectGroups(query, 201L); | |||
assertThat(result).hasSize(1); | |||
GroupMembershipDto dto = result.get(0); | |||
assertThat(dto.getId()).isEqualTo(101L); | |||
assertThat(dto.getName()).isEqualTo("sonar-users"); | |||
assertThat(dto.getDescription()).isEqualTo("Any new users created will automatically join this group"); | |||
assertThat(dto.getUserId()).isEqualTo(201L); | |||
} | |||
@Test | |||
public void select_user_groups_by_query() { | |||
dbTester.prepareDbUnit(getClass(), "shared.xml"); | |||
// 200 is member of 3 groups | |||
assertThat(dao.selectGroups(GroupMembershipQuery.builder().login("arthur").membership(GroupMembershipQuery.IN).build(), 200L)).hasSize(3); | |||
// 201 is member of 1 group on 3 | |||
assertThat(dao.selectGroups(GroupMembershipQuery.builder().login("arthur").membership(GroupMembershipQuery.IN).build(), 201L)).hasSize(1); | |||
// 999 is member of 0 group | |||
assertThat(dao.selectGroups(GroupMembershipQuery.builder().login("arthur").membership(GroupMembershipQuery.IN).build(), 999L)).isEmpty(); | |||
} | |||
@Test | |||
public void select_groups_not_affected_to_a_user_by_query() { | |||
dbTester.prepareDbUnit(getClass(), "shared.xml"); | |||
// 200 is member of 3 groups | |||
assertThat(dao.selectGroups(GroupMembershipQuery.builder().login("arthur").membership(GroupMembershipQuery.OUT).build(), 200L)).isEmpty(); | |||
// 201 is member of 1 group on 3 | |||
assertThat(dao.selectGroups(GroupMembershipQuery.builder().login("arthur").membership(GroupMembershipQuery.OUT).build(), 201L)).hasSize(2); | |||
// 999 is member of 0 group | |||
assertThat(dao.selectGroups(GroupMembershipQuery.builder().login("arthur").membership(GroupMembershipQuery.OUT).build(), 2999L)).hasSize(3); | |||
} | |||
@Test | |||
public void search_by_group_name() { | |||
dbTester.prepareDbUnit(getClass(), "shared.xml"); | |||
List<GroupMembershipDto> result = dao.selectGroups(GroupMembershipQuery.builder().login("arthur").groupSearch("user").build(), 200L); | |||
assertThat(result).hasSize(1); | |||
assertThat(result.get(0).getName()).isEqualTo("sonar-users"); | |||
result = dao.selectGroups(GroupMembershipQuery.builder().login("arthur").groupSearch("sonar").build(), 200L); | |||
assertThat(result).hasSize(3); | |||
} | |||
@Test | |||
public void search_by_group_name_with_capitalization() { | |||
dbTester.prepareDbUnit(getClass(), "shared.xml"); | |||
List<GroupMembershipDto> result = dao.selectGroups(GroupMembershipQuery.builder().login("arthur").groupSearch("UsER").build(), 200L); | |||
assertThat(result).hasSize(1); | |||
assertThat(result.get(0).getName()).isEqualTo("sonar-users"); | |||
result = dao.selectGroups(GroupMembershipQuery.builder().login("arthur").groupSearch("sonar").build(), 200L); | |||
assertThat(result).hasSize(3); | |||
} | |||
@Test | |||
public void should_be_sorted_by_group_name() { | |||
dbTester.prepareDbUnit(getClass(), "should_be_sorted_by_group_name.xml"); | |||
List<GroupMembershipDto> result = dao.selectGroups(GroupMembershipQuery.builder().login("arthur").build(), 200L); | |||
assertThat(result).hasSize(3); | |||
assertThat(result.get(0).getName()).isEqualTo("sonar-administrators"); | |||
assertThat(result.get(1).getName()).isEqualTo("sonar-reviewers"); | |||
assertThat(result.get(2).getName()).isEqualTo("sonar-users"); | |||
} | |||
@Test | |||
public void should_be_paginated() { | |||
dbTester.prepareDbUnit(getClass(), "shared.xml"); | |||
List<GroupMembershipDto> result = dao.selectGroups(GroupMembershipQuery.builder().login("arthur").build(), 200L, 0, 2); | |||
assertThat(result).hasSize(2); | |||
assertThat(result.get(0).getName()).isEqualTo("sonar-administrators"); | |||
assertThat(result.get(1).getName()).isEqualTo("sonar-reviewers"); | |||
result = dao.selectGroups(GroupMembershipQuery.builder().login("arthur").build(), 200L, 1, 2); | |||
assertThat(result).hasSize(2); | |||
assertThat(result.get(0).getName()).isEqualTo("sonar-reviewers"); | |||
assertThat(result.get(1).getName()).isEqualTo("sonar-users"); | |||
result = dao.selectGroups(GroupMembershipQuery.builder().login("arthur").build(), 200L, 2, 1); | |||
assertThat(result).hasSize(1); | |||
assertThat(result.get(0).getName()).isEqualTo("sonar-users"); | |||
} | |||
@Test | |||
public void count_groups() { | |||
dbTester.prepareDbUnit(getClass(), "shared.xml"); |