aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKlaudio Sinani <klaudio.sinani@sonarsource.com>2022-05-24 09:52:38 +0200
committersonartech <sonartech@sonarsource.com>2022-05-25 20:03:16 +0000
commit477470a1db1057fb60630185f89c170b9783323e (patch)
tree6f1f4018d020fffaa7912ba60209d92070f8215e
parent6895f3582df8ae0b30bbc16b6a00e67ff4a1acac (diff)
downloadsonarqube-477470a1db1057fb60630185f89c170b9783323e.tar.gz
sonarqube-477470a1db1057fb60630185f89c170b9783323e.zip
SONAR-16316 Migrate Quality Profile & Quality Gate queries to Quality Gate & Quality Profile daos
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateDao.java5
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateFindingDto.java (renamed from server/sonar-db-dao/src/main/java/org/sonar/db/report/QualityGateFindingDto.java)2
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java3
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java5
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileFindingDto.java (renamed from server/sonar-db-dao/src/main/java/org/sonar/db/report/QualityProfileFindingDto.java)2
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java5
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/report/RegulatoryReportDao.java9
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/report/RegulatoryReportMapper.java4
-rw-r--r--server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateMapper.xml23
-rw-r--r--server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml20
-rw-r--r--server/sonar-db-dao/src/main/resources/org/sonar/db/report/RegulatoryReportMapper.xml43
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java47
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java56
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/report/RegulatoryReportDaoTest.java104
14 files changed, 165 insertions, 163 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateDao.java
index d05ce597cb0..3b1426d8191 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateDao.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateDao.java
@@ -22,6 +22,7 @@ package org.sonar.db.qualitygate;
import java.util.Collection;
import java.util.Date;
import javax.annotation.CheckForNull;
+import org.apache.ibatis.session.ResultHandler;
import org.sonar.core.util.UuidFactory;
import org.sonar.db.Dao;
import org.sonar.db.DatabaseUtils;
@@ -78,6 +79,10 @@ public class QualityGateDao implements Dao {
mapper(dbSession).ensureOneBuiltInQualityGate(builtInName);
}
+ public void selectQualityGateFindings(DbSession dbSession, String qualityGateUuid, ResultHandler<QualityGateFindingDto> handler) {
+ mapper(dbSession).selectQualityGateFindings(qualityGateUuid, handler);
+ }
+
public QualityGateDto selectBuiltIn(DbSession dbSession) {
return mapper(dbSession).selectBuiltIn();
}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/report/QualityGateFindingDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateFindingDto.java
index 1a21276e026..171215f93a0 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/report/QualityGateFindingDto.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateFindingDto.java
@@ -17,7 +17,7 @@
* 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.report;
+package org.sonar.db.qualitygate;
import javax.annotation.CheckForNull;
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java
index ec4901e102b..75f99bf0f4c 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java
@@ -22,6 +22,7 @@ package org.sonar.db.qualitygate;
import java.util.Collection;
import java.util.List;
import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.session.ResultHandler;
public interface QualityGateMapper {
@@ -41,6 +42,8 @@ public interface QualityGateMapper {
void ensureOneBuiltInQualityGate(String builtInQualityName);
+ void selectQualityGateFindings(String qualityGateUuid, ResultHandler<QualityGateFindingDto> handler);
+
QualityGateDto selectByUuid(String uuid);
QualityGateDto selectDefault();
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java
index 2948705b2a9..eab5232a9c2 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java
@@ -28,6 +28,7 @@ import java.util.Map;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+import org.apache.ibatis.session.ResultHandler;
import org.sonar.api.utils.System2;
import org.sonar.core.util.UuidFactory;
import org.sonar.core.util.stream.MoreCollectors;
@@ -207,6 +208,10 @@ public class QualityProfileDao implements Dao {
return mapper(dbSession).selectByNameAndLanguages(name, languages);
}
+ public void selectQualityProfileFindings(DbSession dbSession, String qualityProfileUuid, ResultHandler<QualityProfileFindingDto> handler) {
+ mapper(dbSession).selectQualityProfileFindings(qualityProfileUuid, handler);
+ }
+
public Map<String, Long> countProjectsByProfiles(DbSession dbSession, List<QProfileDto> profiles) {
List<String> profileUuids = profiles.stream().map(QProfileDto::getKee).collect(MoreCollectors.toList());
return KeyLongValue.toMap(executeLargeInputs(profileUuids, partition -> mapper(dbSession).countProjectsByProfiles(partition)));
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/report/QualityProfileFindingDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileFindingDto.java
index 43017f1c9c2..64af7bd6605 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/report/QualityProfileFindingDto.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileFindingDto.java
@@ -17,7 +17,7 @@
* 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.report;
+package org.sonar.db.qualityprofile;
import javax.annotation.CheckForNull;
import org.sonar.api.rule.RuleKey;
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java
index 96bbb6a7efb..3b3333f4894 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java
@@ -24,6 +24,7 @@ import java.util.Date;
import java.util.List;
import javax.annotation.CheckForNull;
import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.session.ResultHandler;
import org.sonar.db.KeyLongValue;
public interface QualityProfileMapper {
@@ -93,6 +94,10 @@ public interface QualityProfileMapper {
List<String> selectQProfileUuidsByProjectUuid(@Param("projectUuid") String projectUuid);
+ void selectQualityProfileFindings(
+ @Param("qualityProfileUuid") String qualityProfileUuid,
+ @Param("handler") ResultHandler<QualityProfileFindingDto> handler);
+
void insertProjectProfileAssociation(
@Param("uuid") String uuid,
@Param("projectUuid") String projectUuid,
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/report/RegulatoryReportDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/report/RegulatoryReportDao.java
index e7eb4291de1..8c6ca026ef0 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/report/RegulatoryReportDao.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/report/RegulatoryReportDao.java
@@ -28,16 +28,7 @@ public class RegulatoryReportDao implements Dao {
mapper(dbSession).scrollIssues(branchUuid, handler);
}
- public void getQualityGateFindings(DbSession dbSession, String qualityGateUuid, ResultHandler<QualityGateFindingDto> handler) {
- mapper(dbSession).getQualityGateFindings(qualityGateUuid, handler);
- }
-
- public void getQualityProfileFindings(DbSession dbSession, String qualityProfileUuid, ResultHandler<QualityProfileFindingDto> handler) {
- mapper(dbSession).getQualityProfileFindings(qualityProfileUuid, handler);
- }
-
private static RegulatoryReportMapper mapper(DbSession dbSession) {
return dbSession.getMapper(RegulatoryReportMapper.class);
}
-
}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/report/RegulatoryReportMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/report/RegulatoryReportMapper.java
index a73fddc21dd..1a75e22bdcb 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/report/RegulatoryReportMapper.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/report/RegulatoryReportMapper.java
@@ -23,8 +23,4 @@ import org.apache.ibatis.session.ResultHandler;
public interface RegulatoryReportMapper {
void scrollIssues(String branchUuid, ResultHandler<IssueFindingDto> handler);
-
- void getQualityGateFindings(String qualityGateUuid, ResultHandler<QualityGateFindingDto> handler);
-
- void getQualityProfileFindings(String qualityProfileUuid, ResultHandler<QualityProfileFindingDto> handler);
}
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateMapper.xml
index 0b52c10da77..2488f9adcfb 100644
--- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateMapper.xml
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateMapper.xml
@@ -2,11 +2,23 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd">
<mapper namespace="org.sonar.db.qualitygate.QualityGateMapper">
-
<sql id="gateColumns">
qg.uuid, qg.name, qg.is_built_in as isBuiltIn, qg.created_at as createdAt, qg.updated_at as updatedAt
</sql>
+ <sql id="qualityGateFindingColumns">
+ m.description as description,
+ qgc.operator as operator,
+ m.name as kee,
+ m.enabled as isEnabled,
+ m.val_type as valueType,
+ m.best_value as bestValue,
+ m.worst_value as worstValue,
+ m.optimized_best_value as optimizedBestValue,
+ qgc.value_error as errorThreshold,
+ m.decimal_scale as decimalScale
+ </sql>
+
<insert id="insertQualityGate" parameterType="QualityGate" useGeneratedKeys="false">
insert into quality_gates (uuid, name, is_built_in, created_at, updated_at)
values (#{uuid, jdbcType=VARCHAR}, #{name, jdbcType=VARCHAR}, #{isBuiltIn, jdbcType=BOOLEAN}, #{createdAt, jdbcType=TIMESTAMP}, #{updatedAt, jdbcType=TIMESTAMP})
@@ -60,6 +72,15 @@
and p.user_uuid is null
</select>
+ <select id="selectQualityGateFindings" parameterType="String" resultType="org.sonar.db.qualitygate.QualityGateFindingDto">
+ select
+ <include refid="qualityGateFindingColumns"/>
+ from quality_gates qg
+ left join quality_gate_conditions qgc on qgc.qgate_uuid = qg.uuid
+ left join metrics m on m.uuid = qgc.metric_uuid
+ where qg.uuid=#{qualityGateUuid,jdbcType=VARCHAR}
+ </select>
+
<update id="delete" parameterType="String">
delete from quality_gates where uuid=#{uuid}
</update>
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml
index db706ae4d65..c5ca6a87473 100644
--- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml
@@ -23,6 +23,17 @@
rp.is_built_in as isBuiltIn
</sql>
+ <sql id="qualityProfileFindingColumns">
+ r.language as language,
+ r.name as title,
+ r.plugin_name as repository,
+ r.plugin_rule_key as ruleKey,
+ r.status as status,
+ r.rule_type as type,
+ r.priority as defaultSeverity,
+ ar.failure_level as severity
+ </sql>
+
<insert id="insertRuleProfile" parameterType="map" useGeneratedKeys="false">
insert into rules_profiles (
uuid,
@@ -370,6 +381,15 @@
and rp.is_built_in = ${_false}
</select>
+ <select id="selectQualityProfileFindings" parameterType="String" resultType="org.sonar.db.qualityprofile.QualityProfileFindingDto" fetchSize="${_scrollFetchSize}" resultSetType="FORWARD_ONLY">
+ select
+ <include refid="qualityProfileFindingColumns"/>
+ from rules r
+ left join active_rules ar on ar.rule_uuid = r.uuid
+ inner join org_qprofiles oqp on oqp.rules_profile_uuid = ar.profile_uuid
+ where oqp.uuid=#{qualityProfileUuid,jdbcType=VARCHAR}
+ </select>
+
<update id="renameRuleProfiles" parameterType="map">
update rules_profiles
set
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/report/RegulatoryReportMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/report/RegulatoryReportMapper.xml
index 50111cac536..ea6b2bb051f 100644
--- a/server/sonar-db-dao/src/main/resources/org/sonar/db/report/RegulatoryReportMapper.xml
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/report/RegulatoryReportMapper.xml
@@ -10,30 +10,6 @@
</collection>
</resultMap>
- <sql id="qualityGateFindingColumns">
- m.description as description,
- qgc.operator as operator,
- m.name as kee,
- m.enabled as isEnabled,
- m.val_type as valueType,
- m.best_value as bestValue,
- m.worst_value as worstValue,
- m.optimized_best_value as optimizedBestValue,
- qgc.value_error as errorThreshold,
- m.decimal_scale as decimalScale
- </sql>
-
- <sql id="qualityProfileFindingColumns">
- r.language as language,
- r.name as title,
- r.plugin_name as repository,
- r.plugin_rule_key as ruleKey,
- r.status as status,
- r.rule_type as type,
- r.priority as defaultSeverity,
- ar.failure_level as severity
- </sql>
-
<sql id="issueColumns">
i.kee as kee,
i.severity as severity,
@@ -50,28 +26,9 @@
r.security_standards as securityStandards,
r.name as ruleName,
i.issue_creation_date as creationDate,
-
<include refid="org.sonar.db.issue.IssueMapper.isNewCodeReferenceIssue"/>
</sql>
- <select id="getQualityGateFindings" parameterType="String" resultType="org.sonar.db.report.QualityGateFindingDto">
- select
- <include refid="qualityGateFindingColumns"/>
- from quality_gates qg
- left join quality_gate_conditions qgc on qgc.qgate_uuid = qg.uuid
- left join metrics m on m.uuid = qgc.metric_uuid
- where qg.uuid=#{qualityGateUuid,jdbcType=VARCHAR}
- </select>
-
- <select id="getQualityProfileFindings" parameterType="String" resultType="org.sonar.db.report.QualityProfileFindingDto" fetchSize="${_scrollFetchSize}" resultSetType="FORWARD_ONLY">
- select
- <include refid="qualityProfileFindingColumns"/>
- from rules r
- left join active_rules ar on ar.rule_uuid = r.uuid
- inner join org_qprofiles oqp on oqp.rules_profile_uuid = ar.profile_uuid
- where oqp.uuid=#{qualityProfileUuid,jdbcType=VARCHAR}
- </select>
-
<select id="scrollIssues" parameterType="String" resultMap="issueResultMap" resultOrdered="true" fetchSize="${_scrollFetchSize}" resultSetType="FORWARD_ONLY">
select
<include refid="issueColumns"/>,
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java
index 4adca22600f..4225abb4778 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java
@@ -19,18 +19,26 @@
*/
package org.sonar.db.qualitygate;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
+import java.util.List;
+import java.util.stream.Collectors;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.utils.System2;
import org.sonar.core.util.Uuids;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.component.BranchDto;
+import org.sonar.db.component.BranchType;
+import org.sonar.db.metric.MetricDto;
import org.sonar.db.project.ProjectDto;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.AssertionsForClassTypes.within;
+import static org.sonar.db.qualitygate.QualityGateFindingDto.NEW_CODE_METRIC_PREFIX;
public class QualityGateDaoTest {
@@ -117,6 +125,45 @@ public class QualityGateDaoTest {
}
@Test
+ public void selectQualityGateFindings_returns_all_quality_gate_details_for_project() {
+ ProjectDto project = db.components().insertPublicProjectDto();
+ BranchDto branch = db.components().insertProjectBranch(project).setBranchType(BranchType.BRANCH);
+ QualityGateDto gate = db.qualityGates().insertQualityGate();
+ db.qualityGates().setDefaultQualityGate(gate);
+
+ MetricDto metric1 = db.measures().insertMetric(m -> m.setDescription("metric 1 description").setDecimalScale(0));
+ QualityGateConditionDto condition1 = db.qualityGates().addCondition(gate, metric1);
+
+ MetricDto metric2 = db.measures().insertMetric(m -> m.setDescription("metric 2 description").setDecimalScale(1));
+ QualityGateConditionDto condition2 = db.qualityGates().addCondition(gate, metric2);
+
+ MetricDto metric3 = db.measures().insertMetric(m -> m.setDescription("metric 3 description").setDecimalScale(0));
+ QualityGateConditionDto condition3 = db.qualityGates().addCondition(gate, metric3);
+
+ db.qualityGates().associateProjectToQualityGate(project, gate);
+ db.commit();
+
+ List<QualityGateFindingDto> findings = new ArrayList<>();
+ underTest.selectQualityGateFindings(db.getSession(), gate.getUuid(), result -> findings.add(result.getResultObject()));
+
+ QualityGateFindingDto finding = findings.stream().filter(f -> f.getDescription().equals("metric 1 description")).findFirst().get();
+
+ // check fields
+ assertThat(findings).hasSize(3);
+ assertThat(findings.stream().map(f -> f.getDescription()).collect(Collectors.toSet())).containsExactlyInAnyOrder("metric 1 description", "metric 2 description", "metric 3 description");
+ assertThat(finding.getDescription()).isEqualTo(metric1.getDescription());
+ assertThat(finding.getOperatorDescription()).isEqualTo(QualityGateFindingDto.OperatorDescription.valueOf(condition1.getOperator()).getDescription());
+ assertThat(finding.getErrorThreshold()).isEqualTo(condition1.getErrorThreshold());
+ assertThat(finding.getValueType()).isEqualTo(metric1.getValueType());
+ assertThat(finding.isNewCodeMetric()).isEqualTo(metric1.getKey().startsWith(NEW_CODE_METRIC_PREFIX));
+ assertThat(finding.isEnabled()).isEqualTo(metric1.isEnabled());
+ assertThat(finding.getBestValue()).isEqualTo(metric1.getBestValue(), within(0.00001));
+ assertThat(finding.getWorstValue()).isEqualTo(metric1.getWorstValue(), within(0.00001));
+ assertThat(finding.isOptimizedBestValue()).isEqualTo(metric1.isOptimizedBestValue());
+ assertThat(finding.getDecimalScale()).isEqualTo(metric1.getDecimalScale());
+ }
+
+ @Test
public void delete() {
QualityGateDto qualityGate = qualityGateDbTester.insertQualityGate();
QualityGateDto otherQualityGate = qualityGateDbTester.insertQualityGate();
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java
index 2ea5e44292c..c4b3fc210ca 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java
@@ -20,25 +20,32 @@
package org.sonar.db.qualityprofile;
import com.google.common.collect.Sets;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
+import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.assertj.core.data.MapEntry;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rules.RuleType;
import org.sonar.api.utils.System2;
import org.sonar.core.util.UtcDateUtils;
import org.sonar.core.util.Uuids;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.component.BranchDto;
+import org.sonar.db.component.BranchType;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.project.ProjectDto;
import org.sonar.db.rule.RuleDto;
+import org.sonar.db.rule.SeverityUtil;
import static com.google.common.collect.ImmutableList.of;
import static com.google.common.collect.Lists.newArrayList;
@@ -400,6 +407,55 @@ public class QualityProfileDaoTest {
}
@Test
+ public void selectQualityProfileFindings_returns_all_quality_profile_details_for_project() {
+ String projectUuid = "project_uuid";
+ String projectKey = "project_key";
+ String branchUuid = "branch_uuid";
+ String branchName = "branch";
+
+ BranchDto branch = new BranchDto()
+ .setBranchType(BranchType.BRANCH)
+ .setKey(branchName)
+ .setUuid(branchUuid)
+ .setProjectUuid(projectUuid);
+
+ db.getDbClient().branchDao().insert(db.getSession(), branch);
+
+ ProjectDto project = db.components().insertPublicProjectDto(t -> t.setProjectUuid(projectUuid)
+ .setUuid(projectUuid)
+ .setDbKey(projectKey)
+ .setMainBranchProjectUuid(branchUuid));
+
+ QProfileDto cppQPWithoutActiveRules = db.qualityProfiles().insert(qp -> qp.setIsBuiltIn(true).setLanguage("cpp"));
+ db.qualityProfiles().setAsDefault(cppQPWithoutActiveRules);
+
+ QProfileDto javaBuiltInQPWithActiveRules = db.qualityProfiles().insert(qp -> qp.setIsBuiltIn(true).setLanguage("java"));
+ RuleDto rule1 = db.rules().insert(r -> r.setName("rule 1 title"));
+ ActiveRuleDto activeRule1 = db.qualityProfiles().activateRule(javaBuiltInQPWithActiveRules, rule1);
+ RuleDto rule2 = db.rules().insert(r -> r.setName("rule 2 title"));
+ ActiveRuleDto activeRule2 = db.qualityProfiles().activateRule(javaBuiltInQPWithActiveRules, rule2);
+ RuleDto rule3 = db.rules().insert(r -> r.setName("rule 3 title"));
+ ActiveRuleDto activeRule3 = db.qualityProfiles().activateRule(javaBuiltInQPWithActiveRules, rule3);
+
+ db.qualityProfiles().associateWithProject(project, cppQPWithoutActiveRules, javaBuiltInQPWithActiveRules);
+ db.getSession().commit();
+
+ List<QualityProfileFindingDto> findings = new ArrayList<>();
+ underTest.selectQualityProfileFindings(db.getSession(), javaBuiltInQPWithActiveRules.getKee(), result -> findings.add(result.getResultObject()));
+
+ QualityProfileFindingDto finding = findings.stream().filter(f -> f.getTitle().equals("rule 1 title")).findFirst().get();
+
+ assertThat(findings).hasSize(3);
+ assertThat(findings.stream().map(f -> f.getTitle()).collect(Collectors.toSet())).containsExactlyInAnyOrder("rule 1 title", "rule 2 title", "rule 3 title");
+ assertThat(finding.getLanguage()).isEqualTo(rule1.getLanguage());
+ assertThat(finding.getTitle()).isEqualTo(rule1.getName());
+ assertThat(finding.getReferenceKey()).isEqualTo(RuleKey.of(rule1.getRepositoryKey(), rule1.getRuleKey()));
+ assertThat(finding.getStatus()).isEqualTo(rule1.getStatus());
+ assertThat(finding.getType()).isEqualTo(RuleType.valueOf(rule1.getType()));
+ assertThat(finding.getSeverity()).isEqualTo(SeverityUtil.getSeverityFromOrdinal(activeRule1.getSeverity()));
+ }
+
+ @Test
public void should_not_selectByLanguage_with_wrong_language() {
QProfileDto profile = QualityProfileTesting.newQualityProfileDto();
underTest.insert(dbSession, profile);
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/report/RegulatoryReportDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/report/RegulatoryReportDaoTest.java
index e2e96733c50..e500e8bfa13 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/report/RegulatoryReportDaoTest.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/report/RegulatoryReportDaoTest.java
@@ -21,34 +21,18 @@ package org.sonar.db.report;
import java.util.ArrayList;
import java.util.List;
-import java.util.stream.Collectors;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rules.RuleType;
import org.sonar.api.utils.System2;
-import org.sonar.core.util.UuidFactoryFast;
import org.sonar.db.DbTester;
-import org.sonar.db.component.BranchDto;
-import org.sonar.db.component.BranchType;
import org.sonar.db.component.ComponentDto;
-import org.sonar.db.issue.IssueChangeDto;
import org.sonar.db.issue.IssueDto;
-import org.sonar.db.metric.MetricDto;
-import org.sonar.db.project.ProjectDto;
-import org.sonar.db.qualitygate.QualityGateConditionDto;
-import org.sonar.db.qualitygate.QualityGateDto;
-import org.sonar.db.qualityprofile.ActiveRuleDto;
-import org.sonar.db.qualityprofile.QProfileDto;
import org.sonar.db.rule.RuleDto;
-import org.sonar.db.rule.SeverityUtil;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.AssertionsForClassTypes.within;
import static org.sonar.api.issue.Issue.RESOLUTION_WONT_FIX;
import static org.sonar.db.component.ComponentTesting.newFileDto;
-import static org.sonar.db.report.QualityGateFindingDto.NEW_CODE_METRIC_PREFIX;
public class RegulatoryReportDaoTest {
private static final String PROJECT_UUID = "prj_uuid";
@@ -74,94 +58,6 @@ public class RegulatoryReportDaoTest {
}
@Test
- public void getQualityGateFindings_returns_all_quality_gate_details_for_project() {
- ProjectDto project = db.components().insertPublicProjectDto();
- BranchDto branch = db.components().insertProjectBranch(project).setBranchType(BranchType.BRANCH);
- QualityGateDto gate = db.qualityGates().insertQualityGate();
- db.qualityGates().setDefaultQualityGate(gate);
-
- MetricDto metric1 = db.measures().insertMetric(m -> m.setDescription("metric 1 description").setDecimalScale(0));
- QualityGateConditionDto condition1 = db.qualityGates().addCondition(gate, metric1);
-
- MetricDto metric2 = db.measures().insertMetric(m -> m.setDescription("metric 2 description").setDecimalScale(1));
- QualityGateConditionDto condition2 = db.qualityGates().addCondition(gate, metric2);
-
- MetricDto metric3 = db.measures().insertMetric(m -> m.setDescription("metric 3 description").setDecimalScale(0));
- QualityGateConditionDto condition3 = db.qualityGates().addCondition(gate, metric3);
-
- db.qualityGates().associateProjectToQualityGate(project, gate);
- db.commit();
-
- List<QualityGateFindingDto> findings = new ArrayList<>();
- underTest.getQualityGateFindings(db.getSession(), gate.getUuid(), result -> findings.add(result.getResultObject()));
-
- QualityGateFindingDto finding = findings.stream().filter(f -> f.getDescription().equals("metric 1 description")).findFirst().get();
-
- // check fields
- assertThat(findings).hasSize(3);
- assertThat(findings.stream().map(f -> f.getDescription()).collect(Collectors.toSet())).containsExactlyInAnyOrder("metric 1 description", "metric 2 description", "metric 3 description");
- assertThat(finding.getDescription()).isEqualTo(metric1.getDescription());
- assertThat(finding.getOperatorDescription()).isEqualTo(QualityGateFindingDto.OperatorDescription.valueOf(condition1.getOperator()).getDescription());
- assertThat(finding.getErrorThreshold()).isEqualTo(condition1.getErrorThreshold());
- assertThat(finding.getValueType()).isEqualTo(metric1.getValueType());
- assertThat(finding.isNewCodeMetric()).isEqualTo(metric1.getKey().startsWith(NEW_CODE_METRIC_PREFIX));
- assertThat(finding.isEnabled()).isEqualTo(metric1.isEnabled());
- assertThat(finding.getBestValue()).isEqualTo(metric1.getBestValue(), within(0.00001));
- assertThat(finding.getWorstValue()).isEqualTo(metric1.getWorstValue(), within(0.00001));
- assertThat(finding.isOptimizedBestValue()).isEqualTo(metric1.isOptimizedBestValue());
- assertThat(finding.getDecimalScale()).isEqualTo(metric1.getDecimalScale());
- }
-
- @Test
- public void getQualityProfileFindings_returns_all_quality_profile_details_for_project() {
- String projectUuid = "project_uuid";
- String projectKey = "project_key";
- String branchUuid = "branch_uuid";
- String branchName = "branch";
-
- BranchDto branch = new BranchDto()
- .setBranchType(BranchType.BRANCH)
- .setKey(branchName)
- .setUuid(branchUuid)
- .setProjectUuid(projectUuid);
-
- db.getDbClient().branchDao().insert(db.getSession(), branch);
-
- ProjectDto project = db.components().insertPublicProjectDto(t -> t.setProjectUuid(projectUuid)
- .setUuid(projectUuid)
- .setDbKey(projectKey)
- .setMainBranchProjectUuid(branchUuid));
-
- QProfileDto cppQPWithoutActiveRules = db.qualityProfiles().insert(qp -> qp.setIsBuiltIn(true).setLanguage("cpp"));
- db.qualityProfiles().setAsDefault(cppQPWithoutActiveRules);
-
- QProfileDto javaBuiltInQPWithActiveRules = db.qualityProfiles().insert(qp -> qp.setIsBuiltIn(true).setLanguage("java"));
- RuleDto rule1 = db.rules().insert(r -> r.setName("rule 1 title"));
- ActiveRuleDto activeRule1 = db.qualityProfiles().activateRule(javaBuiltInQPWithActiveRules, rule1);
- RuleDto rule2 = db.rules().insert(r -> r.setName("rule 2 title"));
- ActiveRuleDto activeRule2 = db.qualityProfiles().activateRule(javaBuiltInQPWithActiveRules, rule2);
- RuleDto rule3 = db.rules().insert(r -> r.setName("rule 3 title"));
- ActiveRuleDto activeRule3 = db.qualityProfiles().activateRule(javaBuiltInQPWithActiveRules, rule3);
-
- db.qualityProfiles().associateWithProject(project, cppQPWithoutActiveRules, javaBuiltInQPWithActiveRules);
- db.getSession().commit();
-
- List<QualityProfileFindingDto> findings = new ArrayList<>();
- underTest.getQualityProfileFindings(db.getSession(), javaBuiltInQPWithActiveRules.getKee(), result -> findings.add(result.getResultObject()));
-
- QualityProfileFindingDto finding = findings.stream().filter(f -> f.getTitle().equals("rule 1 title")).findFirst().get();
-
- assertThat(findings).hasSize(3);
- assertThat(findings.stream().map(f -> f.getTitle()).collect(Collectors.toSet())).containsExactlyInAnyOrder("rule 1 title", "rule 2 title", "rule 3 title");
- assertThat(finding.getLanguage()).isEqualTo(rule1.getLanguage());
- assertThat(finding.getTitle()).isEqualTo(rule1.getName());
- assertThat(finding.getReferenceKey()).isEqualTo(RuleKey.of(rule1.getRepositoryKey(), rule1.getRuleKey()));
- assertThat(finding.getStatus()).isEqualTo(rule1.getStatus());
- assertThat(finding.getType()).isEqualTo(RuleType.valueOf(rule1.getType()));
- assertThat(finding.getSeverity()).isEqualTo(SeverityUtil.getSeverityFromOrdinal(activeRule1.getSeverity()));
- }
-
- @Test
public void scrollIssues_returns_all_non_closed_issues_for_project() {
IssueDto issue1 = db.issues().insertIssue(rule, project, file, i -> i.setStatus("OPEN").setResolution(null));
IssueDto issue2 = db.issues().insertIssue(rule, project, file, i -> i.setStatus("CONFIRMED").setResolution(null));