diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-07-23 10:14:08 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-07-23 11:09:45 +0200 |
commit | d8e2d42e70807608e015381b55d2431db73bc574 (patch) | |
tree | c6699cb9534859ecf386b82f1a8fbfe782e10570 /sonar-db | |
parent | 28230d418e9efe480e7140dba2764e45927ee4a9 (diff) | |
download | sonarqube-d8e2d42e70807608e015381b55d2431db73bc574.tar.gz sonarqube-d8e2d42e70807608e015381b55d2431db73bc574.zip |
Move GroupDao to sonar-db
Diffstat (limited to 'sonar-db')
10 files changed, 356 insertions, 0 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/DbClient.java b/sonar-db/src/main/java/org/sonar/db/DbClient.java index fba496c2f90..6ff2e99fdd6 100644 --- a/sonar-db/src/main/java/org/sonar/db/DbClient.java +++ b/sonar-db/src/main/java/org/sonar/db/DbClient.java @@ -60,6 +60,7 @@ import org.sonar.db.qualityprofile.QualityProfileDao; import org.sonar.db.source.FileSourceDao; import org.sonar.db.user.AuthorDao; import org.sonar.db.user.AuthorizationDao; +import org.sonar.db.user.GroupDao; import org.sonar.db.user.GroupMembershipDao; import org.sonar.db.user.RoleDao; import org.sonar.db.user.UserDao; @@ -111,6 +112,7 @@ public class DbClient { private final NotificationQueueDao notificationQueueDao; private final CustomMeasureDao customMeasureDao; private final MetricDao metricDao; + private final GroupDao groupDao; public DbClient(Database database, MyBatis myBatis, Dao... daos) { this.database = database; @@ -162,6 +164,7 @@ public class DbClient { notificationQueueDao = getDao(map, NotificationQueueDao.class); customMeasureDao = getDao(map, CustomMeasureDao.class); metricDao = getDao(map, MetricDao.class); + groupDao = getDao(map, GroupDao.class); doOnLoad(map); } @@ -350,6 +353,10 @@ public class DbClient { return metricDao; } + public GroupDao groupDao() { + return groupDao; + } + protected <K extends Dao> K getDao(Map<Class, Dao> map, Class<K> clazz) { return (K) map.get(clazz); } diff --git a/sonar-db/src/main/java/org/sonar/db/user/GroupDao.java b/sonar-db/src/main/java/org/sonar/db/user/GroupDao.java new file mode 100644 index 00000000000..a642b77c6b5 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/user/GroupDao.java @@ -0,0 +1,112 @@ +/* + * 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.user; + +import java.util.Date; +import java.util.List; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; +import org.apache.commons.lang.StringUtils; +import org.apache.ibatis.session.RowBounds; +import org.sonar.api.utils.System2; +import org.sonar.db.Dao; +import org.sonar.db.DbSession; +import org.sonar.db.util.RowNotFoundException; + +public class GroupDao implements Dao { + + private static final String SQL_WILDCARD = "%"; + private System2 system; + + public GroupDao(System2 system) { + this.system = system; + } + + public GroupDto selectByKey(DbSession session, String key) { + GroupDto group = selectNullableByKey(session, key); + if (group == null) { + throw new RowNotFoundException(String.format("Could not find a group with name '%s'", key)); + } + return group; + } + + @CheckForNull + public GroupDto selectNullableByKey(DbSession session, String key) { + return mapper(session).selectByKey(key); + } + + public GroupDto selectById(DbSession dbSession, long groupId) { + GroupDto group = selectNullableById(dbSession, groupId); + if (group == null) { + throw new RowNotFoundException(String.format("Could not find a group with id '%d'", groupId)); + } + return group; + } + + @CheckForNull + public GroupDto selectNullableById(DbSession dbSession, long groupId) { + return mapper(dbSession).selectById(groupId); + } + + public void deleteById(DbSession dbSession, long groupId) { + mapper(dbSession).deleteById(groupId); + } + + public int countByQuery(DbSession session, @Nullable String query) { + return mapper(session).countByQuery(groupSearchToSql(query)); + } + + public List<GroupDto> selectByQuery(DbSession session, @Nullable String query, int offset, int limit) { + return mapper(session).selectByQuery(groupSearchToSql(query), new RowBounds(offset, limit)); + } + + public GroupDto insert(DbSession session, GroupDto item) { + Date createdAt = new Date(system.now()); + item.setCreatedAt(createdAt) + .setUpdatedAt(createdAt); + mapper(session).insert(item); + return item; + } + + public GroupDto update(DbSession session, GroupDto item) { + item.setUpdatedAt(new Date(system.now())); + mapper(session).update(item); + return item; + } + + public List<GroupDto> findByUserLogin(DbSession session, String login){ + return mapper(session).selectByUserLogin(login); + } + + private GroupMapper mapper(DbSession session) { + return session.getMapper(GroupMapper.class); + } + + private String groupSearchToSql(@Nullable String query) { + String sql = SQL_WILDCARD; + if (query != null) { + sql = StringUtils.replace(StringUtils.upperCase(query), SQL_WILDCARD, "/%"); + sql = StringUtils.replace(sql, "_", "/_"); + sql = SQL_WILDCARD + sql + SQL_WILDCARD; + } + return sql; + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/user/GroupDaoTest.java b/sonar-db/src/test/java/org/sonar/db/user/GroupDaoTest.java new file mode 100644 index 00000000000..4389dc9a5d6 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/user/GroupDaoTest.java @@ -0,0 +1,188 @@ +/* + * 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.user; + +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.sonar.api.utils.DateUtils; +import org.sonar.api.utils.System2; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; +import org.sonar.test.DbTests; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@Category(DbTests.class) +public class GroupDaoTest { + + @Rule + public DbTester dbTester = DbTester.create(System2.INSTANCE); + + GroupDao dao; + DbSession session; + System2 system2; + + @Before + public void setUp() { + dbTester.truncateTables(); + this.session = dbTester.myBatis().openSession(false); + this.system2 = mock(System2.class); + this.dao = new GroupDao(system2); + } + + @After + public void tearDown() { + session.close(); + } + + @Test + public void select_by_key() { + dbTester.prepareDbUnit(getClass(), "select_by_key.xml"); + + GroupDto group = new GroupDao(system2).selectByKey(session, "sonar-users"); + assertThat(group).isNotNull(); + assertThat(group.getId()).isEqualTo(1L); + assertThat(group.getName()).isEqualTo("sonar-users"); + assertThat(group.getDescription()).isEqualTo("Sonar Users"); + assertThat(group.getCreatedAt()).isEqualTo(DateUtils.parseDate("2014-09-07")); + assertThat(group.getUpdatedAt()).isEqualTo(DateUtils.parseDate("2014-09-08")); + } + + @Test + public void select_by_id() { + dbTester.prepareDbUnit(getClass(), "select_by_key.xml"); + + GroupDto group = new GroupDao(system2).selectById(session, 1L); + assertThat(group).isNotNull(); + assertThat(group.getId()).isEqualTo(1L); + assertThat(group.getName()).isEqualTo("sonar-users"); + assertThat(group.getDescription()).isEqualTo("Sonar Users"); + assertThat(group.getCreatedAt()).isEqualTo(DateUtils.parseDate("2014-09-07")); + assertThat(group.getUpdatedAt()).isEqualTo(DateUtils.parseDate("2014-09-08")); + } + + @Test + public void find_by_user_login() { + dbTester.prepareDbUnit(getClass(), "find_by_user_login.xml"); + + assertThat(dao.findByUserLogin(session, "john")).hasSize(2); + assertThat(dao.findByUserLogin(session, "max")).isEmpty(); + } + + @Test + public void insert() { + when(system2.now()).thenReturn(DateUtils.parseDate("2014-09-08").getTime()); + + dbTester.prepareDbUnit(getClass(), "empty.xml"); + + GroupDto dto = new GroupDto() + .setId(1L) + .setName("sonar-users") + .setDescription("Sonar Users"); + + dao.insert(session, dto); + session.commit(); + + dbTester.assertDbUnit(getClass(), "insert-result.xml", "groups"); + } + + @Test + public void update() { + when(system2.now()).thenReturn(DateUtils.parseDate("2013-07-25").getTime()); + + dbTester.prepareDbUnit(getClass(), "update.xml"); + + GroupDto dto = new GroupDto() + .setId(1L) + .setName("new-name") + .setDescription("New Description"); + + dao.update(session, dto); + session.commit(); + + dbTester.assertDbUnit(getClass(), "update-result.xml", "groups"); + } + + @Test + public void select_by_query() { + dbTester.prepareDbUnit(getClass(), "select_by_query.xml"); + + /* + * Ordering and paging are not fully tested, case insensitive sort is broken on MySQL + */ + + // Null query + assertThat(new GroupDao(system2).selectByQuery(session, null, 0, 10)) + .hasSize(5) + .extracting("name").containsOnly("customers-group1", "customers-group2", "customers-group3", "SONAR-ADMINS", "sonar-users"); + + // Empty query + assertThat(new GroupDao(system2).selectByQuery(session, "", 0, 10)) + .hasSize(5) + .extracting("name").containsOnly("customers-group1", "customers-group2", "customers-group3", "SONAR-ADMINS", "sonar-users"); + + // Filter on name + assertThat(new GroupDao(system2).selectByQuery(session, "sonar", 0, 10)) + .hasSize(2) + .extracting("name").containsOnly("SONAR-ADMINS", "sonar-users"); + + // Pagination + assertThat(new GroupDao(system2).selectByQuery(session, null, 0, 3)) + .hasSize(3); + assertThat(new GroupDao(system2).selectByQuery(session, null, 3, 3)) + .hasSize(2); + assertThat(new GroupDao(system2).selectByQuery(session, null, 6, 3)).isEmpty(); + assertThat(new GroupDao(system2).selectByQuery(session, null, 0, 5)) + .hasSize(5); + assertThat(new GroupDao(system2).selectByQuery(session, null, 5, 5)).isEmpty(); + } + + @Test + public void count_by_query() { + dbTester.prepareDbUnit(getClass(), "select_by_query.xml"); + + // Null query + assertThat(new GroupDao(system2).countByQuery(session, null)).isEqualTo(5); + + // Empty query + assertThat(new GroupDao(system2).countByQuery(session, "")).isEqualTo(5); + + // Filter on name + assertThat(new GroupDao(system2).countByQuery(session, "sonar")).isEqualTo(2); + } + + @Test + public void delete_by_id() { + dbTester.prepareDbUnit(getClass(), "select_by_key.xml"); + + GroupDao groupDao = new GroupDao(system2); + groupDao.deleteById(session, 1L); + session.commit(); + + assertThat(groupDao.countByQuery(session, null)).isZero(); + } + +} diff --git a/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/empty.xml b/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/empty.xml new file mode 100644 index 00000000000..a1c54e4625a --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/empty.xml @@ -0,0 +1,4 @@ +<dataset> + + +</dataset> diff --git a/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/find_by_user_login.xml b/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/find_by_user_login.xml new file mode 100644 index 00000000000..da498e192bd --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/find_by_user_login.xml @@ -0,0 +1,12 @@ +<dataset> + + <groups id="1" name="sonar-users" description="Sonar Users" created_at="2014-09-07" updated_at="2014-09-08"/> + <groups id="2" name="sonar-admins" description="Sonar Admins" created_at="2014-09-07" updated_at="2014-09-08"/> + <groups id="3" name="sonar-reviewers" description="Sonar Reviewers" created_at="2014-09-07" updated_at="2014-09-08"/> + + <groups_users user_id="100" group_id="1"/> + <groups_users user_id="100" group_id="2"/> + + <users id="100" login="john" name="John" email="jo@hn.com" created_at="1418215735482" updated_at="1418215735482" active="[true]"/> + +</dataset> diff --git a/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/insert-result.xml b/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/insert-result.xml new file mode 100644 index 00000000000..ebbd59a6b8d --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/insert-result.xml @@ -0,0 +1,5 @@ +<dataset> + + <groups id="1" name="sonar-users" description="Sonar Users" created_at="2014-09-08" updated_at="2014-09-08"/> + +</dataset> diff --git a/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/select_by_key.xml b/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/select_by_key.xml new file mode 100644 index 00000000000..e3ec0111eea --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/select_by_key.xml @@ -0,0 +1,5 @@ +<dataset> + + <groups id="1" name="sonar-users" description="Sonar Users" created_at="2014-09-07" updated_at="2014-09-08"/> + +</dataset> diff --git a/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/select_by_query.xml b/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/select_by_query.xml new file mode 100644 index 00000000000..983f6ad9980 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/select_by_query.xml @@ -0,0 +1,9 @@ +<dataset> + + <groups id="1" name="sonar-users" description="Sonar Users" created_at="2014-09-07" updated_at="2014-09-08"/> + <groups id="2" name="SONAR-ADMINS" description="Sonar Admins" created_at="2014-09-07" updated_at="2014-09-08"/> + <groups id="3" name="customers-group1" description="Group 1" created_at="2014-09-07" updated_at="2014-09-08"/> + <groups id="4" name="customers-group2" description="Group 2" created_at="2014-09-07" updated_at="2014-09-08"/> + <groups id="5" name="customers-group3" description="Group 3" created_at="2014-09-07" updated_at="2014-09-08"/> + +</dataset> diff --git a/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/update-result.xml b/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/update-result.xml new file mode 100644 index 00000000000..7fdf00ca9d0 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/update-result.xml @@ -0,0 +1,7 @@ +<dataset> + + <groups id="1" name="new-name" description="New Description" created_at="2011-04-25" updated_at="2013-07-25"/> + + <groups id="2" name="untouched" description="Untouched" created_at="2003-03-23" updated_at="2006-09-09"/> + +</dataset> diff --git a/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/update.xml b/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/update.xml new file mode 100644 index 00000000000..df225b534d4 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/user/GroupDaoTest/update.xml @@ -0,0 +1,7 @@ +<dataset> + + <groups id="1" name="old-name" description="Old Description" created_at="2011-04-25" updated_at="2011-04-25"/> + + <groups id="2" name="untouched" description="Untouched" created_at="2003-03-23" updated_at="2006-09-09"/> + +</dataset> |