diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2016-07-29 11:08:56 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2016-07-29 14:53:13 +0200 |
commit | 3e3e4b1e16c0e5ebf8a485d3014cb7bd2577e192 (patch) | |
tree | 6cccc368df16e99ca836a7626762f75de5a62b4d /sonar-db | |
parent | 88c26b4030c29372e57847eed22646b259d1a9c1 (diff) | |
download | sonarqube-3e3e4b1e16c0e5ebf8a485d3014cb7bd2577e192.tar.gz sonarqube-3e3e4b1e16c0e5ebf8a485d3014cb7bd2577e192.zip |
SONAR-7923 Create WS api/qualitygates/get_by_project
Diffstat (limited to 'sonar-db')
6 files changed, 79 insertions, 20 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/qualitygate/ProjectQgateAssociationDao.java b/sonar-db/src/main/java/org/sonar/db/qualitygate/ProjectQgateAssociationDao.java index c984fbe315c..74544f980ec 100644 --- a/sonar-db/src/main/java/org/sonar/db/qualitygate/ProjectQgateAssociationDao.java +++ b/sonar-db/src/main/java/org/sonar/db/qualitygate/ProjectQgateAssociationDao.java @@ -20,6 +20,7 @@ package org.sonar.db.qualitygate; import java.util.List; +import java.util.Optional; import org.apache.ibatis.session.SqlSession; import org.sonar.db.Dao; import org.sonar.db.DbSession; @@ -30,6 +31,16 @@ public class ProjectQgateAssociationDao implements Dao { return mapper(dbSession).selectProjects(query); } + /** + * @return quality gate id if a specific Quality Gate has been defined for the given component id. <br> + * Returns <code>{@link Optional#empty()}</code> otherwise (ex: default quality gate applies) + */ + public Optional<Long> selectQGateIdByComponentId(DbSession dbSession, long componentId) { + String id = mapper(dbSession).selectQGateIdByComponentId(componentId); + + return id == null ? Optional.empty() : Optional.of(Long.valueOf(id)); + } + private static ProjectQgateAssociationMapper mapper(SqlSession session) { return session.getMapper(ProjectQgateAssociationMapper.class); } diff --git a/sonar-db/src/main/java/org/sonar/db/qualitygate/ProjectQgateAssociationMapper.java b/sonar-db/src/main/java/org/sonar/db/qualitygate/ProjectQgateAssociationMapper.java index 5b9d9462d2a..c4d058c69f7 100644 --- a/sonar-db/src/main/java/org/sonar/db/qualitygate/ProjectQgateAssociationMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/qualitygate/ProjectQgateAssociationMapper.java @@ -20,10 +20,13 @@ package org.sonar.db.qualitygate; import java.util.List; +import javax.annotation.CheckForNull; import org.apache.ibatis.annotations.Param; public interface ProjectQgateAssociationMapper { List<ProjectQgateAssociationDto> selectProjects(@Param("query") ProjectQgateAssociationQuery query); + @CheckForNull + String selectQGateIdByComponentId(long componentId); } diff --git a/sonar-db/src/main/java/org/sonar/db/qualitygate/QualityGateDao.java b/sonar-db/src/main/java/org/sonar/db/qualitygate/QualityGateDao.java index 41cc46124ce..49982d7ed20 100644 --- a/sonar-db/src/main/java/org/sonar/db/qualitygate/QualityGateDao.java +++ b/sonar-db/src/main/java/org/sonar/db/qualitygate/QualityGateDao.java @@ -24,6 +24,7 @@ import java.util.Date; import javax.annotation.CheckForNull; import org.apache.ibatis.session.SqlSession; import org.sonar.db.Dao; +import org.sonar.db.DbSession; import org.sonar.db.MyBatis; public class QualityGateDao implements Dao{ @@ -35,17 +36,19 @@ public class QualityGateDao implements Dao{ } public void insert(QualityGateDto newQualityGate) { - SqlSession session = myBatis.openSession(false); + DbSession session = myBatis.openSession(false); try { - insert(newQualityGate, session); + insert(session, newQualityGate); session.commit(); } finally { MyBatis.closeQuietly(session); } } - public void insert(QualityGateDto newQualityGate, SqlSession session) { + public QualityGateDto insert(DbSession session, QualityGateDto newQualityGate) { mapper(session).insert(newQualityGate.setCreatedAt(new Date())); + + return newQualityGate; } public Collection<QualityGateDto> selectAll() { diff --git a/sonar-db/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java b/sonar-db/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java index 9a1d8e60720..f7fa21e4d0c 100644 --- a/sonar-db/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java @@ -42,7 +42,7 @@ import java.util.List; */ public interface QualityGateMapper { - void insert(QualityGateDto newQualityGate); + void insert(QualityGateDto qualityGate); List<QualityGateDto> selectAll(); diff --git a/sonar-db/src/main/resources/org/sonar/db/qualitygate/ProjectQgateAssociationMapper.xml b/sonar-db/src/main/resources/org/sonar/db/qualitygate/ProjectQgateAssociationMapper.xml index fe6683b58db..01203faf49a 100644 --- a/sonar-db/src/main/resources/org/sonar/db/qualitygate/ProjectQgateAssociationMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/qualitygate/ProjectQgateAssociationMapper.xml @@ -32,4 +32,13 @@ ORDER BY proj.name </select> + <select id="selectQGateIdByComponentId" parameterType="long" resultType="string"> + SELECT text_value + FROM properties + <where> + AND resource_id=#{componentId} + AND prop_key='sonar.qualitygate' + </where> + </select> + </mapper> diff --git a/sonar-db/src/test/java/org/sonar/db/qualitygate/ProjectQgateAssociationDaoTest.java b/sonar-db/src/test/java/org/sonar/db/qualitygate/ProjectQgateAssociationDaoTest.java index fbf6e2812b0..5673fe776a3 100644 --- a/sonar-db/src/test/java/org/sonar/db/qualitygate/ProjectQgateAssociationDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/qualitygate/ProjectQgateAssociationDaoTest.java @@ -20,61 +20,67 @@ package org.sonar.db.qualitygate; import java.util.List; +import java.util.Optional; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.System2; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.component.ComponentDbTester; +import org.sonar.db.component.ComponentDto; +import org.sonar.db.property.PropertyDto; import static org.assertj.core.api.Assertions.assertThat; - +import static org.sonar.db.component.ComponentTesting.newProjectDto; public class ProjectQgateAssociationDaoTest { @Rule - public DbTester dbTester = DbTester.create(System2.INSTANCE); - - DbSession dbSession = dbTester.getSession(); + public DbTester db = DbTester.create(System2.INSTANCE); + ComponentDbTester componentDb = new ComponentDbTester(db); + DbClient dbClient = db.getDbClient(); + DbSession dbSession = db.getSession(); - ProjectQgateAssociationDao dao = dbTester.getDbClient().projectQgateAssociationDao(); + ProjectQgateAssociationDao underTest = db.getDbClient().projectQgateAssociationDao(); @Test public void select_all_projects_by_query() { - dbTester.prepareDbUnit(getClass(), "shared.xml"); + db.prepareDbUnit(getClass(), "shared.xml"); ProjectQgateAssociationQuery query = ProjectQgateAssociationQuery.builder().gateId("42").build(); - List<ProjectQgateAssociationDto> result = dao.selectProjects(dbSession, query); + List<ProjectQgateAssociationDto> result = underTest.selectProjects(dbSession, query); assertThat(result).hasSize(5); } @Test public void select_projects_by_query() { - dbTester.prepareDbUnit(getClass(), "shared.xml"); + db.prepareDbUnit(getClass(), "shared.xml"); - assertThat(dao.selectProjects(dbSession, ProjectQgateAssociationQuery.builder().gateId("42").membership(ProjectQgateAssociationQuery.IN).build())).hasSize(3); - assertThat(dao.selectProjects(dbSession, ProjectQgateAssociationQuery.builder().gateId("42").membership(ProjectQgateAssociationQuery.OUT).build())).hasSize(2); + assertThat(underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder().gateId("42").membership(ProjectQgateAssociationQuery.IN).build())).hasSize(3); + assertThat(underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder().gateId("42").membership(ProjectQgateAssociationQuery.OUT).build())).hasSize(2); } @Test public void search_by_project_name() { - dbTester.prepareDbUnit(getClass(), "shared.xml"); + db.prepareDbUnit(getClass(), "shared.xml"); - List<ProjectQgateAssociationDto> result = dao.selectProjects(dbSession, ProjectQgateAssociationQuery.builder().gateId("42").projectSearch("one").build()); + List<ProjectQgateAssociationDto> result = underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder().gateId("42").projectSearch("one").build()); assertThat(result).hasSize(1); assertThat(result.get(0).getName()).isEqualTo("Project One"); - result = dao.selectProjects(dbSession, ProjectQgateAssociationQuery.builder().gateId("42").projectSearch("one").build()); + result = underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder().gateId("42").projectSearch("one").build()); assertThat(result).hasSize(1); - result = dao.selectProjects(dbSession, ProjectQgateAssociationQuery.builder().gateId("42").projectSearch("project").build()); + result = underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder().gateId("42").projectSearch("project").build()); assertThat(result).hasSize(2); } @Test public void should_be_sorted_by_project_name() { - dbTester.prepareDbUnit(getClass(), "shared.xml"); + db.prepareDbUnit(getClass(), "shared.xml"); - List<ProjectQgateAssociationDto> result = dao.selectProjects(dbSession, ProjectQgateAssociationQuery.builder().gateId("42").build()); + List<ProjectQgateAssociationDto> result = underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder().gateId("42").build()); assertThat(result).hasSize(5); assertThat(result.get(0).getName()).isEqualTo("Project Five"); assertThat(result.get(1).getName()).isEqualTo("Project Four"); @@ -82,4 +88,31 @@ public class ProjectQgateAssociationDaoTest { assertThat(result.get(3).getName()).isEqualTo("Project Three"); assertThat(result.get(4).getName()).isEqualTo("Project Two"); } + + @Test + public void select_qgate_id_is_absent() { + ComponentDto project = componentDb.insertComponent(newProjectDto()); + + Optional<Long> result = underTest.selectQGateIdByComponentId(dbSession, project.getId()); + + assertThat(result.isPresent()).isFalse(); + } + + @Test + public void select_qgate_id() { + associateProjectToQualityGate(10L, 1L); + associateProjectToQualityGate(11L, 2L); + + Optional<Long> result = underTest.selectQGateIdByComponentId(dbSession, 10L); + + assertThat(result).contains(1L); + } + + private void associateProjectToQualityGate(long componentId, long qualityGateId) { + dbClient.propertiesDao().insertProperty(dbSession, new PropertyDto() + .setKey("sonar.qualitygate") + .setResourceId(componentId) + .setValue(String.valueOf(qualityGateId))); + db.commit(); + } } |