aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2015-05-05 15:38:14 +0200
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2015-05-07 16:40:09 +0200
commit912e9886ae4a982781c99afd7881dd9b0e934565 (patch)
tree020d8a2efeb1ccc2a73253e5886bb6a755f96399 /sonar-core
parentf7c5f42cf3ca81ff948ae08afe50d15c09412bfc (diff)
downloadsonarqube-912e9886ae4a982781c99afd7881dd9b0e934565.tar.gz
sonarqube-912e9886ae4a982781c99afd7881dd9b0e934565.zip
SONAR-6470 New Java WS to show a user's groups
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/user/GroupMembershipDao.java17
-rw-r--r--sonar-core/src/main/resources/org/sonar/core/user/GroupMembershipMapper.xml13
-rw-r--r--sonar-core/src/test/java/org/sonar/core/user/GroupMembershipDaoTest.java50
3 files changed, 64 insertions, 16 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/user/GroupMembershipDao.java b/sonar-core/src/main/java/org/sonar/core/user/GroupMembershipDao.java
index 6cc67f26e95..145bef37faa 100644
--- a/sonar-core/src/main/java/org/sonar/core/user/GroupMembershipDao.java
+++ b/sonar-core/src/main/java/org/sonar/core/user/GroupMembershipDao.java
@@ -24,12 +24,13 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
+import org.sonar.core.persistence.DaoComponent;
import org.sonar.core.persistence.MyBatis;
import java.util.List;
import java.util.Map;
-public class GroupMembershipDao {
+public class GroupMembershipDao implements DaoComponent {
private final MyBatis mybatis;
@@ -37,16 +38,26 @@ public class GroupMembershipDao {
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 {
- Map<String, Object> params = ImmutableMap.of("query", query, "userId", userId);
- return session.selectList("org.sonar.core.user.GroupMembershipMapper.selectGroups", params, new RowBounds(offset, limit));
+ 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 session.selectList("org.sonar.core.user.GroupMembershipMapper.selectGroups", params, new RowBounds(offset, limit));
+ }
+
+ public int countGroups(SqlSession session, GroupMembershipQuery query, Long userId) {
+ Map<String, Object> params = ImmutableMap.of("query", query, "userId", userId);
+ return session.selectOne("org.sonar.core.user.GroupMembershipMapper.countGroups", params);
+ }
+
@VisibleForTesting
List<GroupMembershipDto> selectGroups(GroupMembershipQuery query, Long userId) {
return selectGroups(query, userId, 0, Integer.MAX_VALUE);
diff --git a/sonar-core/src/main/resources/org/sonar/core/user/GroupMembershipMapper.xml b/sonar-core/src/main/resources/org/sonar/core/user/GroupMembershipMapper.xml
index 5a7c4dd214d..5c7654c0cb9 100644
--- a/sonar-core/src/main/resources/org/sonar/core/user/GroupMembershipMapper.xml
+++ b/sonar-core/src/main/resources/org/sonar/core/user/GroupMembershipMapper.xml
@@ -3,8 +3,7 @@
<mapper namespace="org.sonar.core.user.GroupMembershipMapper">
- <select id="selectGroups" parameterType="map" resultType="GroupMembership">
- SELECT g.id as id, g.name as name, g.description as description, gu.user_id as userId
+ <sql id="commonClauses">
FROM groups g
LEFT JOIN groups_users gu ON gu.group_id=g.id AND gu.user_id=#{userId}
<where>
@@ -20,7 +19,17 @@
AND (UPPER(g.name) LIKE #{query.groupSearchSql} ESCAPE '/')
</if>
</where>
+ </sql>
+
+ <select id="selectGroups" parameterType="map" resultType="GroupMembership">
+ SELECT g.id as id, g.name as name, g.description as description, gu.user_id as userId
+ <include refid="commonClauses" />
ORDER BY g.name
</select>
+ <select id="countGroups" parameterType="map" resultType="int">
+ SELECT COUNT(g.id)
+ <include refid="commonClauses" />
+ </select>
+
</mapper>
diff --git a/sonar-core/src/test/java/org/sonar/core/user/GroupMembershipDaoTest.java b/sonar-core/src/test/java/org/sonar/core/user/GroupMembershipDaoTest.java
index bad880abb04..ea1fd3bdd5a 100644
--- a/sonar-core/src/test/java/org/sonar/core/user/GroupMembershipDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/user/GroupMembershipDaoTest.java
@@ -21,25 +21,34 @@
package org.sonar.core.user;
import org.junit.Before;
+import org.junit.ClassRule;
import org.junit.Test;
-import org.sonar.core.persistence.AbstractDaoTestCase;
+import org.junit.experimental.categories.Category;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.persistence.DbTester;
+import org.sonar.test.DbTests;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
-public class GroupMembershipDaoTest extends AbstractDaoTestCase {
+@Category(DbTests.class)
+public class GroupMembershipDaoTest {
+
+ @ClassRule
+ public static final DbTester dbTester = new DbTester();
private GroupMembershipDao dao;
@Before
public void setUp() {
- dao = new GroupMembershipDao(getMyBatis());
+ dbTester.truncateTables();
+ dao = new GroupMembershipDao(dbTester.myBatis());
}
@Test
public void select_all_groups_by_query() throws Exception {
- setupData("shared");
+ dbTester.prepareDbUnit(getClass(), "shared.xml");
GroupMembershipQuery query = GroupMembershipQuery.builder().login("arthur").build();
List<GroupMembershipDto> result = dao.selectGroups(query, 200L);
@@ -48,7 +57,7 @@ public class GroupMembershipDaoTest extends AbstractDaoTestCase {
@Test
public void select_user_group() throws Exception {
- setupData("select_user_group");
+ dbTester.prepareDbUnit(getClass(), "select_user_group.xml");
GroupMembershipQuery query = GroupMembershipQuery.builder().login("arthur").build();
List<GroupMembershipDto> result = dao.selectGroups(query, 201L);
@@ -63,7 +72,7 @@ public class GroupMembershipDaoTest extends AbstractDaoTestCase {
@Test
public void select_user_groups_by_query() throws Exception {
- setupData("shared");
+ 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);
@@ -75,7 +84,7 @@ public class GroupMembershipDaoTest extends AbstractDaoTestCase {
@Test
public void select_groups_not_affected_to_a_user_by_query() throws Exception {
- setupData("shared");
+ dbTester.prepareDbUnit(getClass(), "shared.xml");
// 200 is member of 3 groups
assertThat(dao.selectGroups(GroupMembershipQuery.builder().login("arthur").membership(GroupMembershipQuery.OUT).build(), 200L)).isEmpty();
@@ -87,7 +96,7 @@ public class GroupMembershipDaoTest extends AbstractDaoTestCase {
@Test
public void search_by_group_name() throws Exception {
- setupData("shared");
+ dbTester.prepareDbUnit(getClass(), "shared.xml");
List<GroupMembershipDto> result = dao.selectGroups(GroupMembershipQuery.builder().login("arthur").groupSearch("user").build(), 200L);
assertThat(result).hasSize(1);
@@ -100,7 +109,7 @@ public class GroupMembershipDaoTest extends AbstractDaoTestCase {
@Test
public void search_by_group_name_with_capitalization() throws Exception {
- setupData("shared");
+ dbTester.prepareDbUnit(getClass(), "shared.xml");
List<GroupMembershipDto> result = dao.selectGroups(GroupMembershipQuery.builder().login("arthur").groupSearch("UsER").build(), 200L);
assertThat(result).hasSize(1);
@@ -113,7 +122,7 @@ public class GroupMembershipDaoTest extends AbstractDaoTestCase {
@Test
public void should_be_sorted_by_group_name() throws Exception {
- setupData("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);
@@ -124,7 +133,7 @@ public class GroupMembershipDaoTest extends AbstractDaoTestCase {
@Test
public void should_be_paginated() throws Exception {
- setupData("shared");
+ dbTester.prepareDbUnit(getClass(), "shared.xml");
List<GroupMembershipDto> result = dao.selectGroups(GroupMembershipQuery.builder().login("arthur").build(), 200L, 0, 2);
assertThat(result).hasSize(2);
@@ -141,4 +150,23 @@ public class GroupMembershipDaoTest extends AbstractDaoTestCase {
assertThat(result.get(0).getName()).isEqualTo("sonar-users");
}
+ @Test
+ public void count_groups() throws Exception {
+ dbTester.prepareDbUnit(getClass(), "shared.xml");
+ DbSession session = dbTester.myBatis().openSession(false);
+
+ try {
+ // 200 is member of 3 groups
+ assertThat(dao.countGroups(session, GroupMembershipQuery.builder().login("arthur").membership(GroupMembershipQuery.IN).build(), 200L)).isEqualTo(3);
+ assertThat(dao.countGroups(session, GroupMembershipQuery.builder().login("arthur").membership(GroupMembershipQuery.OUT).build(), 200L)).isZero();
+ // 201 is member of 1 group on 3
+ assertThat(dao.countGroups(session, GroupMembershipQuery.builder().login("arthur").membership(GroupMembershipQuery.IN).build(), 201L)).isEqualTo(1);
+ assertThat(dao.countGroups(session, GroupMembershipQuery.builder().login("arthur").membership(GroupMembershipQuery.OUT).build(), 201L)).isEqualTo(2);
+ // 999 is member of 0 group
+ assertThat(dao.countGroups(session, GroupMembershipQuery.builder().login("arthur").membership(GroupMembershipQuery.IN).build(), 999L)).isZero();
+ assertThat(dao.countGroups(session, GroupMembershipQuery.builder().login("arthur").membership(GroupMembershipQuery.OUT).build(), 2999L)).isEqualTo(3);
+ } finally {
+ session.close();
+ }
+ }
}