]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11370 Add new SQL request to retrieve rules by languages and type
authorEric Hartmann <hartmann.eric@gmail.com>
Fri, 19 Oct 2018 08:46:37 +0000 (10:46 +0200)
committerSonarTech <sonartech@sonarsource.com>
Thu, 25 Oct 2018 18:21:00 +0000 (20:21 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java

index ebf033b83166ac50cd2052bb73028479b9dfe3ac..7b8fe189c2bc7bc1fce8281fc1f23b0873cae814 100644 (file)
@@ -141,6 +141,11 @@ public class RuleDao implements Dao {
     return mapper(session).selectAllDefinitions();
   }
 
+  public List<RuleDto> selectByTypeAndLanguages(DbSession session, String organizationUuid, List<Integer> types, List<String> languages) {
+    return ensureOrganizationIsSet(organizationUuid,
+      executeLargeInputs(languages, chunk -> mapper(session).selectByTypeAndLanguages(organizationUuid, types, chunk)));
+  }
+
   public List<RuleDto> selectByQuery(DbSession session, String organizationUuid, RuleQuery ruleQuery) {
     return ensureOrganizationIsSet(organizationUuid, mapper(session).selectByQuery(organizationUuid, ruleQuery));
   }
index f6271b0e723e355f0b184cb407c9d5fea6f11077..e3754a7d2a1cdd5c156690089c1db50fd5781461 100644 (file)
@@ -63,6 +63,8 @@ public interface RuleMapper {
 
   List<RuleDto> selectByQuery(@Param("organizationUuid") String organizationUuid, @Param("query") RuleQuery ruleQuery);
 
+  List<RuleDto> selectByTypeAndLanguages(@Param("organizationUuid") String organizationUuid, @Param("types") List<Integer> types, @Param("languages") List<String> languages);
+
   void insertDefinition(RuleDefinitionDto ruleDefinitionDto);
 
   void updateDefinition(RuleDefinitionDto ruleDefinitionDto);
index b0ed27b6d1eabd8a5d3f1f15d87ae24b3c9d56ae..60d92465953268e2596d2ac648897cdb616d9b1e 100644 (file)
       r.updated_at desc
   </select>
 
+  <select id="selectByTypeAndLanguages" parameterType="map" resultType="Rule">
+    select
+    <include refid="selectJoinedTablesColumns"/>
+    from
+    rules r
+    <include refid="outerJoinRulesMetadata"/>
+    where
+      r.status != 'REMOVED'
+      and r.rule_type in <foreach collection="types" item="type" separator="," open="(" close=")">#{type, jdbcType=INTEGER}</foreach>
+      and r.language in <foreach collection="languages" item="language" separator="," open="(" close=")">#{language, jdbcType=VARCHAR}</foreach>
+  </select>
+
   <insert id="insertDefinition" parameterType="org.sonar.db.rule.RuleDefinitionDto" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
     insert into rules (
       plugin_key,
index 399dda82f172fe0ca65da5aa0e1cc71f3eff5f5e..946ca37331facfe16fc3c3dd84bf89f32c1b9bfb 100644 (file)
@@ -435,6 +435,96 @@ public class RuleDaoTest {
     assertThat(ruleDto.getId()).isEqualTo(rule.getId());
   }
 
+  @Test
+  public void selectByTypeAndLanguages() {
+    OrganizationDto organization = db.organizations().insert();
+    OrganizationDto organization2 = db.organizations().insert();
+
+    RuleDefinitionDto rule1 = db.rules().insert(
+      r -> r.setKey(RuleKey.of("java", "S001"))
+            .setConfigKey("S1")
+            .setType(RuleType.VULNERABILITY)
+            .setLanguage("java"));
+    db.rules().insertOrUpdateMetadata(rule1, organization);
+
+    RuleDefinitionDto rule2 = db.rules().insert(
+      r -> r.setKey(RuleKey.of("js", "S002"))
+            .setType(RuleType.SECURITY_HOTSPOT)
+            .setLanguage("js"));
+    db.rules().insertOrUpdateMetadata(rule2, organization);
+
+    assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization.getUuid(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("java")))
+      .extracting(RuleDto::getOrganizationUuid, RuleDto::getId, RuleDto::getLanguage, RuleDto::getType)
+      .containsExactly(tuple(organization.getUuid(), rule1.getId(), "java", RuleType.VULNERABILITY.getDbConstant()));
+
+    // Rule available also on organization2
+    assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization2.getUuid(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("java")))
+      .extracting(RuleDto::getOrganizationUuid, RuleDto::getId, RuleDto::getLanguage, RuleDto::getType)
+      .containsExactly(tuple(organization2.getUuid(), rule1.getId(), "java", RuleType.VULNERABILITY.getDbConstant()));
+
+    assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization.getUuid(), singletonList(RuleType.SECURITY_HOTSPOT.getDbConstant()), singletonList("js")))
+      .extracting(RuleDto::getOrganizationUuid, RuleDto::getId, RuleDto::getLanguage, RuleDto::getType)
+      .containsExactly(tuple(organization.getUuid(), rule2.getId(), "js", RuleType.SECURITY_HOTSPOT.getDbConstant()));
+
+    // Rule available also on organization2
+    assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization2.getUuid(), singletonList(RuleType.SECURITY_HOTSPOT.getDbConstant()), singletonList("js")))
+      .extracting(RuleDto::getOrganizationUuid, RuleDto::getId, RuleDto::getLanguage, RuleDto::getType)
+      .containsExactly(tuple(organization2.getUuid(), rule2.getId(), "js", RuleType.SECURITY_HOTSPOT.getDbConstant()));
+
+    assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization.getUuid(), singletonList(RuleType.SECURITY_HOTSPOT.getDbConstant()), singletonList("java")))
+      .isEmpty();
+    assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization.getUuid(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("js")))
+      .isEmpty();
+  }
+
+  @Test
+  public void selectByTypeAndLanguages_return_nothing_when_no_rule_on_languages() {
+    OrganizationDto organization = db.organizations().insert();
+
+    RuleDefinitionDto rule1 = db.rules().insert(
+      r -> r.setKey(RuleKey.of("java", "S001"))
+        .setConfigKey("S1")
+        .setType(RuleType.VULNERABILITY)
+        .setLanguage("java"));
+    db.rules().insertOrUpdateMetadata(rule1, organization);
+
+    RuleDefinitionDto rule2 = db.rules().insert(
+      r -> r.setKey(RuleKey.of("js", "S002"))
+        .setType(RuleType.VULNERABILITY)
+        .setLanguage("js"));
+    db.rules().insertOrUpdateMetadata(rule2, organization);
+
+    assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization.getUuid(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("cpp")))
+      .isEmpty();
+  }
+
+  @Test
+  public void selectByTypeAndLanguages_return_nothing_when_no_rule_with_type() {
+    OrganizationDto organization = db.organizations().insert();
+
+    RuleDefinitionDto rule1 = db.rules().insert(
+      r -> r.setKey(RuleKey.of("java", "S001"))
+        .setConfigKey("S1")
+        .setType(RuleType.VULNERABILITY)
+        .setLanguage("java"));
+    db.rules().insertOrUpdateMetadata(rule1, organization);
+
+    RuleDefinitionDto rule2 = db.rules().insert(
+      r -> r.setKey(RuleKey.of("java", "S002"))
+        .setType(RuleType.SECURITY_HOTSPOT)
+        .setLanguage("java"));
+    db.rules().insertOrUpdateMetadata(rule2, organization);
+
+    RuleDefinitionDto rule3 = db.rules().insert(
+      r -> r.setKey(RuleKey.of("java", "S003"))
+        .setType(RuleType.CODE_SMELL)
+        .setLanguage("java"));
+    db.rules().insertOrUpdateMetadata(rule3, organization);
+
+    assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization.getUuid(), singletonList(RuleType.BUG.getDbConstant()), singletonList("java")))
+      .isEmpty();
+  }
+
   @Test
   public void select_by_query() {
     OrganizationDto organization = db.organizations().insert();