From 5bf97e8c831e996b97a6a3559f9a7be78e663970 Mon Sep 17 00:00:00 2001 From: Klaudio Sinani Date: Fri, 3 Jun 2022 13:06:36 +0200 Subject: [PATCH] SONAR-16316 Minor refactoring on quality profile findings writer --- .../db/qualityprofile/ActiveRuleDto.java | 10 +++++++++ .../db/qualityprofile/QualityProfileDao.java | 4 ++++ .../qualityprofile/QualityProfileMapper.java | 3 ++- .../db/qualityprofile/ActiveRuleMapper.xml | 3 ++- .../qualityprofile/QualityProfileMapper.xml | 10 +++++++++ .../db/qualityprofile/ActiveRuleDaoTest.java | 2 ++ .../qualityprofile/QualityProfileDaoTest.java | 21 +++++++++++++++++++ 7 files changed, 51 insertions(+), 2 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java index c867aa778dd..f96e21bad3a 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java @@ -50,6 +50,7 @@ public class ActiveRuleDto { private String ruleField; private String ruleProfileUuid; private String securityStandards; + private boolean isExternal; public ActiveRuleDto() { // nothing to do here @@ -160,6 +161,15 @@ public class ActiveRuleDto { return this; } + public boolean isExternal() { + return this.isExternal; + } + + public ActiveRuleDto setIsExternal(boolean isExternal) { + this.isExternal = isExternal; + return this; + } + public static ActiveRuleDto createFor(QProfileDto profile, RuleDto ruleDto) { requireNonNull(profile.getRulesProfileUuid(), "Profile is not persisted"); requireNonNull(ruleDto.getUuid(), "Rule is not persisted"); 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..7d2d4c1838b 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 @@ -171,6 +171,10 @@ public class QualityProfileDao implements Dao { return mapper(dbSession).selectQProfileUuidsByProjectUuid(projectUuid); } + public List selectQProfilesByProjectUuid(DbSession dbSession, String projectUuid) { + return mapper(dbSession).selectQProfilesByProjectUuid(projectUuid); + } + public List selectByLanguage(DbSession dbSession, String language) { return mapper(dbSession).selectByLanguage(language); } 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 cf5e8480f94..27fc6f28def 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,7 +24,6 @@ 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 { @@ -94,6 +93,8 @@ public interface QualityProfileMapper { List selectQProfileUuidsByProjectUuid(@Param("projectUuid") String projectUuid); + List selectQProfilesByProjectUuid(@Param("projectUuid") String projectUuid); + void insertProjectProfileAssociation( @Param("uuid") String uuid, @Param("projectUuid") String projectUuid, diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml index 8d05ec5155b..84a298f7dcc 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml @@ -28,7 +28,8 @@ rp.uuid as "ruleProfileUuid", a.created_at as "createdAt", a.updated_at as "updatedAt", - oqp.uuid as "orgProfileUuid" + oqp.uuid as "orgProfileUuid", + r.is_external as "isExternal" 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..fb610cac517 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 @@ -287,6 +287,16 @@ + + insert into project_qprofiles ( uuid, diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java index 9a83f626dca..30781d2d34d 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java @@ -259,6 +259,7 @@ public class ActiveRuleDaoTest { ActiveRuleDto activeRule = createFor(profile1, rule1) .setSeverity(BLOCKER) .setInheritance(INHERITED) + .setIsExternal(false) .setCreatedAt(1000L) .setUpdatedAt(2000L); underTest.insert(dbSession, activeRule); @@ -271,6 +272,7 @@ public class ActiveRuleDaoTest { assertThat(result.getProfileUuid()).isEqualTo(profile1.getRulesProfileUuid()); assertThat(result.getSeverityString()).isEqualTo(BLOCKER); assertThat(result.getInheritance()).isEqualTo(INHERITED); + assertThat(result.isExternal()).isFalse(); assertThat(result.getCreatedAt()).isEqualTo(1000L); assertThat(result.getUpdatedAt()).isEqualTo(2000L); } 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..f2a77a607e9 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 @@ -693,6 +693,27 @@ public class QualityProfileDaoTest { .isEmpty(); } + @Test + public void test_selectQProfilesByProjectUuid() { + ProjectDto project1 = db.components().insertPublicProjectDto(); + ProjectDto project2 = db.components().insertPublicProjectDto(); + ProjectDto project3 = db.components().insertPublicProjectDto(); + QProfileDto javaProfile = db.qualityProfiles().insert(p -> p.setLanguage("java")); + QProfileDto jsProfile = db.qualityProfiles().insert(p -> p.setLanguage("js")); + QProfileDto cProfile = db.qualityProfiles().insert(p -> p.setLanguage("c")); + db.qualityProfiles().associateWithProject(project1, javaProfile, cProfile); + db.qualityProfiles().associateWithProject(project2, jsProfile); + + assertThat(underTest.selectQProfilesByProjectUuid(dbSession, project1.getUuid())) + .containsExactly(javaProfile, cProfile); + + assertThat(underTest.selectQProfilesByProjectUuid(dbSession, project2.getUuid())) + .containsExactly(jsProfile); + + assertThat(underTest.selectQProfilesByProjectUuid(dbSession, project3.getUuid())) + .isEmpty(); + } + @Test public void test_updateProjectProfileAssociation() { ProjectDto project = db.components().insertPrivateProjectDto(); -- 2.39.5