]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-17437 - Improve project search for quality profile by searching for the key
authorAntoine Vinot <antoine.vinot@sonarsource.com>
Wed, 12 Oct 2022 15:50:35 +0000 (17:50 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 13 Oct 2022 20:03:18 +0000 (20:03 +0000)
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/ws/ProjectsActionTest.java

index ec6a84fd1f2ba4af8714cb6fd5cd1db2c01d795b..8a27afb1aada9d04dd022f022fcb40cae885fa49 100644 (file)
@@ -114,15 +114,15 @@ public interface QualityProfileMapper {
 
   List<ProjectQprofileAssociationDto> selectSelectedProjects(
     @Param("profileUuid") String profileUuid,
-    @Param("nameQuery") String nameQuery);
+    @Param("nameOrKeyQuery") String nameOrKeyQuery);
 
   List<ProjectQprofileAssociationDto> selectDeselectedProjects(
     @Param("profileUuid") String profileUuid,
-    @Param("nameQuery") String nameQuery);
+    @Param("nameOrKeyQuery") String nameOrKeyQuery);
 
   List<ProjectQprofileAssociationDto> selectProjectAssociations(
     @Param("profileUuid") String profileUuid,
-    @Param("nameQuery") String nameQuery);
+    @Param("nameOrKeyQuery") String nameOrKeyQuery);
 
   List<String> selectUuidsOfCustomRuleProfiles(@Param("language") String language, @Param("name") String name);
 
index a48cbe39e7e3c35c00076402940612365432687b..b3f7fef3f3979ddb322f92c618b91796c660d507 100644 (file)
       pj.scope = 'PRJ'
       and pj.qualifier = 'TRK'
       and pj.main_branch_project_uuid is null
-      and upper(pj.name) like #{nameQuery, jdbcType=VARCHAR}
+      and (upper(pj.name) like #{nameOrKeyQuery, jdbcType=VARCHAR} or upper(pj.kee) like #{nameOrKeyQuery, jdbcType=VARCHAR})
     order by pj.name ASC
   </select>
 
   <select id="selectDeselectedProjects" resultType="org.sonar.db.qualityprofile.ProjectQprofileAssociationDto">
-    SELECT pj.uuid as projectUuid, pj.kee as projectKey, pj.name as projectName, pp.profile_key as profileKey
+    SELECT
+      pj.uuid as projectUuid,
+      pj.kee as projectKey,
+      pj.name as projectName,
+      pp.profile_key as profileKey
     FROM components pj
-    LEFT JOIN project_qprofiles pp ON pp.project_uuid = pj.uuid
-    AND pp.profile_key = #{profileUuid, jdbcType=VARCHAR}
-    WHERE pj.scope='PRJ' AND pj.qualifier='TRK' AND pj.main_branch_project_uuid is null
-      AND UPPER(pj.name) LIKE #{nameQuery, jdbcType=VARCHAR}
+    LEFT JOIN project_qprofiles pp ON pp.project_uuid = pj.uuid AND pp.profile_key = #{profileUuid, jdbcType=VARCHAR}
+    WHERE
+      pj.scope='PRJ'
+      AND pj.qualifier='TRK'
+      AND pj.main_branch_project_uuid is null
+      AND (upper(pj.name) like #{nameOrKeyQuery, jdbcType=VARCHAR} or upper(pj.kee) like #{nameOrKeyQuery, jdbcType=VARCHAR})
       AND pp.profile_key IS NULL
     ORDER BY pj.name ASC
   </select>
 
   <select id="selectProjectAssociations" resultType="org.sonar.db.qualityprofile.ProjectQprofileAssociationDto">
-    SELECT pj.uuid as projectUuid, pj.kee as projectKey, pj.name as projectName, pp.profile_key as profileKey
+    SELECT
+      pj.uuid as projectUuid,
+      pj.kee as projectKey,
+      pj.name as projectName,
+      pp.profile_key as profileKey
     FROM components pj
     LEFT JOIN project_qprofiles pp ON pp.project_uuid = pj.uuid
     AND pp.profile_key = #{profileUuid, jdbcType=VARCHAR}
-    WHERE pj.scope='PRJ' AND pj.qualifier='TRK' AND pj.main_branch_project_uuid is null
-      AND UPPER(pj.name) LIKE #{nameQuery, jdbcType=VARCHAR}
+    WHERE
+      pj.scope='PRJ'
+      AND pj.qualifier='TRK'
+      AND pj.main_branch_project_uuid is null
+      AND (upper(pj.name) like #{nameOrKeyQuery, jdbcType=VARCHAR} or upper(pj.kee) like #{nameOrKeyQuery, jdbcType=VARCHAR})
     ORDER BY pj.name ASC
   </select>
 
index 0631c5d3fc3a46de71f8acf5c7155b5e057ad66d..1e0f71be9543ecf86499dcbc337c8d9dc2837d62 100644 (file)
@@ -185,7 +185,7 @@ public class QualityProfileDaoTest {
   public void selectRuleProfile() {
     RulesProfileDto rp = insertRulesProfile();
 
-    assertThat(underTest.selectRuleProfile(dbSession, rp.getUuid()).getName()).isEqualTo(rp.getName());
+    assertThat(underTest.selectRuleProfile(dbSession, rp.getUuid())).extracting(RulesProfileDto::getName).isEqualTo(rp.getName());
     assertThat(underTest.selectRuleProfile(dbSession, "missing")).isNull();
   }
 
@@ -237,8 +237,6 @@ public class QualityProfileDaoTest {
     ProjectDto project2 = db.components().insertPrivateProjectDto();
     ProjectDto project3 = db.components().insertPrivateProjectDto();
 
-    db.getDbClient().projectDao().selectByUuid(dbSession, project1.getUuid()).get();
-
     db.qualityProfiles().associateWithProject(project1, profile1);
     db.qualityProfiles().associateWithProject(project2, profile1);
     db.qualityProfiles().associateWithProject(project3, profile2);
@@ -365,7 +363,7 @@ public class QualityProfileDaoTest {
 
   @Test
   public void selectByNameAndLanguage() {
-    List<QProfileDto> sharedData = createSharedData();
+    createSharedData();
 
     QProfileDto dto = underTest.selectByNameAndLanguage(dbSession, "Sonar Way", "java");
     assertThat(dto).isNotNull();
@@ -647,14 +645,10 @@ public class QualityProfileDaoTest {
     QProfileDto jsProfile = db.qualityProfiles().insert(p -> p.setLanguage("js"));
     db.qualityProfiles().associateWithProject(project1, javaProfile, jsProfile);
 
-    assertThat(underTest.selectAssociatedToProjectAndLanguage(dbSession, project1, "java").getKee())
-      .isEqualTo(javaProfile.getKee());
-    assertThat(underTest.selectAssociatedToProjectAndLanguage(dbSession, project1, "js").getKee())
-      .isEqualTo(jsProfile.getKee());
-    assertThat(underTest.selectAssociatedToProjectAndLanguage(dbSession, project1, "cobol"))
-      .isNull();
-    assertThat(underTest.selectAssociatedToProjectAndLanguage(dbSession, project2, "java"))
-      .isNull();
+    assertThat(underTest.selectAssociatedToProjectAndLanguage(dbSession, project1, "java")).extracting(QProfileDto::getKee).isEqualTo(javaProfile.getKee());
+    assertThat(underTest.selectAssociatedToProjectAndLanguage(dbSession, project1, "js")).extracting(QProfileDto::getKee).isEqualTo(jsProfile.getKee());
+    assertThat(underTest.selectAssociatedToProjectAndLanguage(dbSession, project1, "cobol")).isNull();
+    assertThat(underTest.selectAssociatedToProjectAndLanguage(dbSession, project2, "java")).isNull();
   }
 
   @Test
@@ -733,8 +727,8 @@ public class QualityProfileDaoTest {
 
     underTest.updateProjectProfileAssociation(dbSession, project, javaProfile2.getKee(), javaProfile1.getKee());
 
-    assertThat(underTest.selectAssociatedToProjectAndLanguage(dbSession, project, "java").getKee()).isEqualTo(javaProfile2.getKee());
-    assertThat(underTest.selectAssociatedToProjectAndLanguage(dbSession, project, "js").getKee()).isEqualTo(jsProfile.getKee());
+    assertThat(underTest.selectAssociatedToProjectAndLanguage(dbSession, project, "java")).extracting(QProfileDto::getKee).isEqualTo(javaProfile2.getKee());
+    assertThat(underTest.selectAssociatedToProjectAndLanguage(dbSession, project, "js")).extracting(QProfileDto::getKee).isEqualTo(jsProfile.getKee());
   }
 
   @Test
@@ -755,8 +749,8 @@ public class QualityProfileDaoTest {
     ComponentDto project1 = db.components().insertPrivateProject(t -> t.setName("Project1 name"));
     ComponentDto project2 = db.components().insertPrivateProject(t -> t.setName("Project2 name"));
     ComponentDto project3 = db.components().insertPrivateProject(t -> t.setName("Project3 name"));
-    ComponentDto project4 = db.components().insertPrivateProject(t -> t.setName("Project4 name"));
-    ComponentDto branch = db.components().insertProjectBranch(project1, t -> t.setKey("branch"));
+    db.components().insertPrivateProject(t -> t.setName("Project4 name"));
+    db.components().insertProjectBranch(project1, t -> t.setKey("branch"));
 
     QProfileDto profile1 = newQualityProfileDto();
     db.qualityProfiles().insert(profile1);
@@ -808,7 +802,7 @@ public class QualityProfileDaoTest {
     ComponentDto project1 = db.components().insertPrivateProject(t -> t.setName("Project1 name"));
     ComponentDto project2 = db.components().insertPrivateProject(t -> t.setName("Project2 name"));
     ComponentDto project3 = db.components().insertPrivateProject(t -> t.setName("Project3 name"));
-    ComponentDto branch = db.components().insertProjectBranch(project1, t -> t.setKey("branch"));
+    db.components().insertProjectBranch(project1, t -> t.setKey("branch"));
 
     QProfileDto profile1 = newQualityProfileDto();
     db.qualityProfiles().insert(profile1);
@@ -833,8 +827,8 @@ public class QualityProfileDaoTest {
   @Test
   public void selectUuidsOfCustomRulesProfiles_returns_the_custom_profiles_with_specified_name() {
     QProfileDto outdatedProfile1 = db.qualityProfiles().insert(p -> p.setIsBuiltIn(false).setLanguage("java").setName("foo"));
-    QProfileDto differentLanguage = db.qualityProfiles().insert(p -> p.setIsBuiltIn(false).setLanguage("cobol").setName("foo"));
-    QProfileDto differentName = db.qualityProfiles().insert(p -> p.setIsBuiltIn(false).setLanguage("java").setName("bar"));
+    db.qualityProfiles().insert(p -> p.setIsBuiltIn(false).setLanguage("cobol").setName("foo"));
+    db.qualityProfiles().insert(p -> p.setIsBuiltIn(false).setLanguage("java").setName("bar"));
 
     Collection<String> keys = underTest.selectUuidsOfCustomRulesProfiles(dbSession, "java", "foo");
     assertThat(keys).containsOnly(outdatedProfile1.getRulesProfileUuid());
@@ -883,6 +877,37 @@ public class QualityProfileDaoTest {
     assertThat(result).isEmpty();
   }
 
+  @Test
+  public void selectProjectAssociations_shouldFindResult_whenQueryMatchingKey() {
+    ComponentDto privateProject = db.components().insertPrivateProject(project -> project.setName("project name"), project -> project.setKey("project_key"));
+    QProfileDto qProfileDto = db.qualityProfiles().insert();
+
+    List<ProjectQprofileAssociationDto> results = underTest.selectProjectAssociations(dbSession, qProfileDto, "key");
+
+    assertThat(results).extracting(ProjectQprofileAssociationDto::getProjectUuid).containsOnly(privateProject.uuid());
+  }
+
+  @Test
+  public void selectSelectedProjects_shouldFindResult_whenQueryMatchingKey() {
+    ComponentDto privateProject = db.components().insertPrivateProject(project -> project.setName("project name"), project -> project.setKey("project_key"));
+    QProfileDto qProfileDto = db.qualityProfiles().insert();
+    db.qualityProfiles().associateWithProject(db.components().getProjectDto(privateProject), qProfileDto);
+
+    List<ProjectQprofileAssociationDto> results = underTest.selectSelectedProjects(dbSession, qProfileDto, "key");
+
+    assertThat(results).extracting(ProjectQprofileAssociationDto::getProjectUuid).containsOnly(privateProject.uuid());
+  }
+
+  @Test
+  public void selectDeselectedProjects_shouldFindResult_whenQueryMatchingKey() {
+    ComponentDto privateProject = db.components().insertPrivateProject(project -> project.setName("project name"), project -> project.setKey("project_key"));
+    QProfileDto qProfileDto = db.qualityProfiles().insert();
+
+    List<ProjectQprofileAssociationDto> results = underTest.selectDeselectedProjects(dbSession, qProfileDto, "key");
+
+    assertThat(results).extracting(ProjectQprofileAssociationDto::getProjectUuid).containsOnly(privateProject.uuid());
+  }
+
   private List<QProfileDto> createSharedData() {
     QProfileDto dto1 = new QProfileDto()
       .setKee("java_sonar_way")
index f179570d0c24a4b7613fbad784061a71643bbbcf..6dde162782bb490f6a652ea041a6639ae66483e5 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.db.qualityprofile.QProfileDto;
 import org.sonar.db.user.UserDto;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.TestRequest;
 import org.sonar.server.ws.WsActionTester;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -241,11 +242,13 @@ public class ProjectsActionTest {
   }
 
   @Test
-  public void filter_on_name() {
+  public void filter_on_name_and_key() {
     ProjectDto project1 = db.components().insertPublicProjectDto(p -> p.setName("Project One"));
     ProjectDto project2 = db.components().insertPublicProjectDto(p -> p.setName("Project Two"));
     ProjectDto project3 = db.components().insertPublicProjectDto(p -> p.setName("Project Three"));
-    ProjectDto project4 = db.components().insertPublicProjectDto(p -> p.setName("Project Four"));
+    db.components().insertPublicProjectDto(p -> p.setName("Project Four"));
+    ProjectDto project5 = db.components().insertPublicProjectDto(p -> p.setDbKey("Project the fifth"));
+
     QProfileDto qualityProfile = db.qualityProfiles().insert();
     associateProjectsWithProfile(qualityProfile, project1, project2);
 
@@ -266,6 +269,11 @@ public class ProjectsActionTest {
         "      \"key\": \"" + project2.getKey() + "\",\n" +
         "      \"name\": \"" + project2.getName() + "\",\n" +
         "      \"selected\": true\n" +
+        "    },\n" +
+        "    {\n" +
+        "      \"key\": \"" + project5.getKey() + "\",\n" +
+        "      \"name\": \"" + project5.getName() + "\",\n" +
+        "      \"selected\": false\n" +
         "    }\n" +
         "  ]}\n");
   }
@@ -290,12 +298,8 @@ public class ProjectsActionTest {
 
   @Test
   public void fail_on_nonexistent_profile() {
-    assertThatThrownBy(() -> {
-      ws.newRequest()
-        .setParam(PARAM_KEY, "unknown")
-        .execute();
-    })
-      .isInstanceOf(NotFoundException.class);
+    TestRequest testRequest = ws.newRequest().setParam(PARAM_KEY, "unknown");
+    assertThatThrownBy(testRequest::execute).isInstanceOf(NotFoundException.class);
   }
 
   @Test
@@ -306,10 +310,11 @@ public class ProjectsActionTest {
     assertThat(definition.responseExampleAsString()).isNotEmpty();
     assertThat(definition.params()).extracting(Param::key).containsExactlyInAnyOrder("key", "p", "ps", "q", "selected");
     Param profile = definition.param("key");
+    assertThat(profile).isNotNull();
     assertThat(profile.deprecatedKey()).isNullOrEmpty();
     assertThat(definition.param("p")).isNotNull();
     assertThat(definition.param("ps")).isNotNull();
-    Param query = definition.param("q");
+    assertThat(definition.param("q")).isNotNull();
   }
 
   private void associateProjectsWithProfile(QProfileDto profile, ProjectDto... projects) {