diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2015-11-19 08:43:10 +0100 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2015-11-19 14:56:30 +0100 |
commit | 25d1773a05c754cda5f69e3133cbcaaab819d572 (patch) | |
tree | 554e60f9dba7a7249f17aad5c9f0c381e3ad8e83 /sonar-db | |
parent | 240cf9f64ff0d7aa1e94f4fdeeb449f48141bcb0 (diff) | |
download | sonarqube-25d1773a05c754cda5f69e3133cbcaaab819d572.tar.gz sonarqube-25d1773a05c754cda5f69e3133cbcaaab819d572.zip |
SONAR-7027 clean up AuthorDao
take session as parameter
use DbSession instead of SqlSession
remove unused methods
Diffstat (limited to 'sonar-db')
-rw-r--r-- | sonar-db/src/main/java/org/sonar/db/user/AuthorDao.java | 72 | ||||
-rw-r--r-- | sonar-db/src/test/java/org/sonar/db/user/AuthorDaoTest.java | 86 |
2 files changed, 40 insertions, 118 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/user/AuthorDao.java b/sonar-db/src/main/java/org/sonar/db/user/AuthorDao.java index 63c9ac78167..5c9df9864e7 100644 --- a/sonar-db/src/main/java/org/sonar/db/user/AuthorDao.java +++ b/sonar-db/src/main/java/org/sonar/db/user/AuthorDao.java @@ -20,78 +20,28 @@ package org.sonar.db.user; import com.google.common.base.Function; -import com.google.common.base.Strings; import java.util.Collection; import java.util.Date; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.sonar.db.Dao; import org.sonar.db.DatabaseUtils; -import org.sonar.db.MyBatis; -import org.sonar.db.component.ComponentDto; -import org.sonar.db.component.ResourceDao; -import org.sonar.db.component.ResourceDto; +import org.sonar.db.DbSession; /** * Be careful when updating this class because it's used by the Dev Cockpit plugin. */ public class AuthorDao implements Dao { - private final MyBatis mybatis; - private final ResourceDao resourceDao; - - public AuthorDao(MyBatis mybatis, ResourceDao resourceDao) { - this.mybatis = mybatis; - this.resourceDao = resourceDao; - } - - public AuthorDto selectByLogin(String login) { - SqlSession session = mybatis.openSession(false); - try { - AuthorMapper mapper = session.getMapper(AuthorMapper.class); - return mapper.selectByLogin(login); - } finally { - MyBatis.closeQuietly(session); - } - } - - public int countDeveloperLogins(long developerId) { - SqlSession session = mybatis.openSession(false); - try { - AuthorMapper mapper = session.getMapper(AuthorMapper.class); - return mapper.countDeveloperLogins(developerId); - } finally { - MyBatis.closeQuietly(session); - } + public AuthorDto selectByLogin(DbSession session, String login) { + return getMapper(session).selectByLogin(login); } - public void insertAuthor(String login, long personId) { - SqlSession session = mybatis.openSession(false); - try { - insertAuthor(login, personId, session); - session.commit(); - } finally { - MyBatis.closeQuietly(session); - } + public int countDeveloperLogins(DbSession session, long developerId) { + return getMapper(session).countDeveloperLogins(developerId); } - public void insertAuthorAndDeveloper(String login, ResourceDto resourceDto) { - SqlSession session = mybatis.openSession(false); - try { - // Hack in order to set the right module uuid path on DEVs - if (Strings.isNullOrEmpty(resourceDto.getModuleUuidPath())) { - resourceDto.setModuleUuidPath(ComponentDto.MODULE_UUID_PATH_SEP + resourceDto.getUuid() + ComponentDto.MODULE_UUID_PATH_SEP); - } - resourceDao.insertUsingExistingSession(resourceDto, session); - insertAuthor(login, resourceDto.getId(), session); - session.commit(); - } finally { - MyBatis.closeQuietly(session); - } - } - - private void insertAuthor(String login, long personId, SqlSession session) { - AuthorMapper authorMapper = session.getMapper(AuthorMapper.class); + public void insertAuthor(DbSession session, String login, long personId) { Date now = new Date(); AuthorDto authorDto = new AuthorDto() .setLogin(login) @@ -99,15 +49,19 @@ public class AuthorDao implements Dao { .setCreatedAt(now) .setUpdatedAt(now); - authorMapper.insert(authorDto); + getMapper(session).insert(authorDto); } - public List<String> selectScmAccountsByDeveloperUuids(final SqlSession session, Collection<String> developerUuids) { + public List<String> selectScmAccountsByDeveloperUuids(final DbSession session, Collection<String> developerUuids) { return DatabaseUtils.executeLargeInputs(developerUuids, new Function<List<String>, List<String>>() { @Override public List<String> apply(List<String> partition) { - return session.getMapper(AuthorMapper.class).selectScmAccountsByDeveloperUuids(partition); + return getMapper(session).selectScmAccountsByDeveloperUuids(partition); } }); } + + private AuthorMapper getMapper(SqlSession session) { + return session.getMapper(AuthorMapper.class); + } } diff --git a/sonar-db/src/test/java/org/sonar/db/user/AuthorDaoTest.java b/sonar-db/src/test/java/org/sonar/db/user/AuthorDaoTest.java index c3ec351c257..8aa790e4bc6 100644 --- a/sonar-db/src/test/java/org/sonar/db/user/AuthorDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/user/AuthorDaoTest.java @@ -19,42 +19,54 @@ */ package org.sonar.db.user; +import org.junit.After; import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; +import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; +import org.sonar.db.DbSession; import org.sonar.db.DbTester; -import org.sonar.db.component.ResourceDto; import org.sonar.test.DbTests; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.fail; @Category(DbTests.class) public class AuthorDaoTest { @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + DbSession dbSession = dbTester.getSession(); AuthorDao dao = dbTester.getDbClient().authorDao(); + @After + public void tearDown() throws Exception { + dbSession.close(); + } + @Test public void shouldSelectByLogin() { dbTester.prepareDbUnit(getClass(), "shouldSelectByLogin.xml"); + dbSession.commit(); - AuthorDto authorDto = dao.selectByLogin("godin"); + AuthorDto authorDto = dao.selectByLogin(dbSession, "godin"); assertThat(authorDto.getId()).isEqualTo(1L); assertThat(authorDto.getPersonId()).isEqualTo(13L); assertThat(authorDto.getLogin()).isEqualTo("godin"); - assertThat(dao.selectByLogin("simon")).isNull(); + assertThat(dao.selectByLogin(dbSession, "simon")).isNull(); } @Test public void shouldInsertAuthor() { dbTester.prepareDbUnit(getClass(), "shouldInsertAuthor.xml"); + dbSession.commit(); - dao.insertAuthor("godin", 13L); + dao.insertAuthor(dbSession, "godin", 13L); + dbSession.commit(); dbTester.assertDbUnit(getClass(), "shouldInsertAuthor-result.xml", new String[] {"created_at", "updated_at"}, "authors"); } @@ -62,70 +74,26 @@ public class AuthorDaoTest { @Test public void countDeveloperLogins() { dbTester.prepareDbUnit(getClass(), "countDeveloperLogins.xml"); + dbSession.commit(); - assertThat(dao.countDeveloperLogins(1L)).isEqualTo(2); - assertThat(dao.countDeveloperLogins(98765L)).isEqualTo(0); - } - - @Test - public void shouldInsertAuthorAndDeveloper() { - dbTester.prepareDbUnit(getClass(), "shouldInsertAuthorAndDeveloper.xml"); - - String login = "developer@company.net"; - ResourceDto resourceDto = new ResourceDto().setName(login).setQualifier("DEV").setUuid("ABCD").setProjectUuid("ABCD").setModuleUuidPath("."); - dao.insertAuthorAndDeveloper(login, resourceDto); - - dbTester.assertDbUnit(getClass(), "shouldInsertAuthorAndDeveloper-result.xml", - new String[] {"created_at", "updated_at", "copy_resource_id", "description", "enabled", "kee", "deprecated_kee", "path", "language", "long_name", "person_id", "root_id", - "scope", "authorization_updated_at"}, - "authors", "projects"); - } - - @Test - public void add_missing_module_uuid_path() { - dbTester.prepareDbUnit(getClass(), "add_missing_module_uuid_path.xml"); - - dao.insertAuthorAndDeveloper("developer@company.net", new ResourceDto().setKey("developer").setName("developer@company.net").setQualifier("DEV").setUuid("ABCD") - .setProjectUuid("ABCD") - .setModuleUuidPath("")); - dao.insertAuthorAndDeveloper("developer2@company.net", new ResourceDto().setKey("developer2").setName("developer2@company.net").setQualifier("DEV").setUuid("BCDE") - .setProjectUuid("BCDE")); - - dbTester.assertDbUnit(getClass(), "add_missing_module_uuid_path-result.xml", - new String[] {"created_at", "updated_at", "copy_resource_id", "description", "enabled", "kee", "deprecated_kee", "path", "language", "long_name", "person_id", "root_id", - "scope", "authorization_updated_at"}, - "authors", "projects"); + assertThat(dao.countDeveloperLogins(dbSession, 1L)).isEqualTo(2); + assertThat(dao.countDeveloperLogins(dbSession, 98765L)).isEqualTo(0); } @Test public void shouldPreventAuthorsDuplication() { dbTester.prepareDbUnit(getClass(), "shouldPreventAuthorsDuplication.xml"); + dbSession.commit(); - try { - dao.insertAuthor("godin", 20L); - fail(); - } catch (RuntimeException ex) { - } - - dbTester.assertDbUnit(getClass(), "shouldPreventAuthorsDuplication-result.xml", new String[] {"created_at", "updated_at"}, "authors"); - } - - @Test - public void shouldPreventAuthorsAndDevelopersDuplication() { - dbTester.prepareDbUnit(getClass(), "shouldPreventAuthorsAndDevelopersDuplication.xml"); - - String login = "developer@company.net"; - ResourceDto resourceDto = new ResourceDto().setName(login).setQualifier("DEV"); + expectedException.expect(RuntimeException.class); try { - dao.insertAuthorAndDeveloper("developer@company.net", resourceDto); - fail(); + dao.insertAuthor(dbSession, "godin", 20L); } catch (RuntimeException ex) { + dbSession.commit(); + dbTester.assertDbUnit(getClass(), "shouldPreventAuthorsDuplication-result.xml", new String[] {"created_at", "updated_at"}, "authors"); + throw ex; } - - dbTester.assertDbUnit(getClass(), "shouldPreventAuthorsAndDevelopersDuplication-result.xml", - new String[] {"created_at", "updated_at", "copy_resource_id", "description", "enabled", "kee", "deprecated_kee", "path", "language", "long_name", "person_id", "root_id", - "scope", "authorization_updated_at"}, - "authors", "projects"); } + } |