diff options
author | Benjamin Campomenosi <109955405+benjamin-campomenosi-sonarsource@users.noreply.github.com> | 2022-08-11 09:12:25 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-08-11 20:03:47 +0000 |
commit | ad505ffd8bb057b425064a0f580fbed25e683502 (patch) | |
tree | c6d59a81a7b171d773a0a51f84eabbc9133c6347 /server/sonar-db-dao | |
parent | 2ae7aacc4fca167a3edef4e3dcb1ee657c8ca81b (diff) | |
download | sonarqube-ad505ffd8bb057b425064a0f580fbed25e683502.tar.gz sonarqube-ad505ffd8bb057b425064a0f580fbed25e683502.zip |
SONAR-17150 fix SSF-39
Diffstat (limited to 'server/sonar-db-dao')
4 files changed, 66 insertions, 18 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleRepositoryDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleRepositoryDao.java index e08d821f929..2c6136a94ac 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleRepositoryDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleRepositoryDao.java @@ -21,16 +21,21 @@ package org.sonar.db.rule; import java.util.Collection; import java.util.List; +import java.util.Locale; import java.util.Set; +import javax.annotation.Nullable; import org.sonar.api.utils.System2; import org.sonar.db.Dao; import org.sonar.db.DatabaseUtils; import org.sonar.db.DbSession; import static com.google.common.base.Preconditions.checkArgument; +import static org.apache.commons.lang.StringUtils.isBlank; public class RuleRepositoryDao implements Dao { + private static final String PERCENT_SIGN = "%"; + private final System2 system2; public RuleRepositoryDao(System2 system2) { @@ -49,12 +54,9 @@ public class RuleRepositoryDao implements Dao { return dbSession.getMapper(RuleRepositoryMapper.class).selectAllKeys(); } - /** - * @return a non-null list ordered by key (as implemented by database, order may - * depend on case sensitivity) - */ - public List<RuleRepositoryDto> selectByLanguage(DbSession dbSession, String language) { - return dbSession.getMapper(RuleRepositoryMapper.class).selectByLanguage(language); + public List<RuleRepositoryDto> selectByQueryAndLanguage(DbSession dbSession, @Nullable String query, @Nullable String language){ + String queryUpgraded = toLowerCaseAndSurroundWithPercentSigns(query); + return dbSession.getMapper(RuleRepositoryMapper.class).selectByQueryAndLanguage(queryUpgraded,language); } public void insert(DbSession dbSession, Collection<RuleRepositoryDto> dtos) { @@ -76,4 +78,9 @@ public class RuleRepositoryDao implements Dao { checkArgument(keys.size() < DatabaseUtils.PARTITION_SIZE_FOR_ORACLE, "too many rule repositories: %s", keys.size()); dbSession.getMapper(RuleRepositoryMapper.class).deleteIfKeyNotIn(keys); } + + private static String toLowerCaseAndSurroundWithPercentSigns(@Nullable String query) { + return isBlank(query) ? PERCENT_SIGN : (PERCENT_SIGN + query.toLowerCase(Locale.ENGLISH) + PERCENT_SIGN); + } + } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleRepositoryMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleRepositoryMapper.java index 2f0aeb989b1..6a2c9d2c119 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleRepositoryMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleRepositoryMapper.java @@ -23,6 +23,7 @@ import java.util.Collection; import java.util.List; import java.util.Set; import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import org.apache.ibatis.annotations.Param; public interface RuleRepositoryMapper { @@ -31,7 +32,7 @@ public interface RuleRepositoryMapper { Set<String> selectAllKeys(); - List<RuleRepositoryDto> selectByLanguage(@Param("language") String language); + List<RuleRepositoryDto> selectByQueryAndLanguage(@Param("query") String query,@Param("language") @Nullable String language); @CheckForNull RuleRepositoryDto selectByKey(@Param("key") String key); diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleRepositoryMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleRepositoryMapper.xml index 962dd8a1a1a..da15614c902 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleRepositoryMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleRepositoryMapper.xml @@ -18,10 +18,15 @@ from rule_repositories </select> - <select id="selectByLanguage" parameterType="String" resultType="org.sonar.db.rule.RuleRepositoryDto"> + <select id="selectByQueryAndLanguage" parameterType="String" resultType="org.sonar.db.rule.RuleRepositoryDto"> select <include refid="sqlColumns"/> from rule_repositories - where language = #{language} + where + ( lower(kee) like #{query} + or lower(name) like #{query} ) + <if test="language != null and language != ''"> + and language = #{language} + </if> order by kee </select> diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleRepositoryDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleRepositoryDaoTest.java index b75e26632cc..66b76b8d32d 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleRepositoryDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleRepositoryDaoTest.java @@ -123,19 +123,35 @@ public class RuleRepositoryDaoTest { } @Test - public void selectByLanguage() { + public void selectByQueryAndLanguage_shouldMatchOnlyOnKeeOrName(){ DbSession dbSession = dbTester.getSession(); - RuleRepositoryDto dto1 = new RuleRepositoryDto("findbugs", "java", "Findbugs"); - RuleRepositoryDto dto2 = new RuleRepositoryDto("java", "java", "Java"); + RuleRepositoryDto dto1 = new RuleRepositoryDto("a_findbugs", "java", "Findbugs"); + RuleRepositoryDto dto2 = new RuleRepositoryDto("jdk", "java", "Java"); RuleRepositoryDto dto3 = new RuleRepositoryDto("cobol-lint", "cobol", "Cobol Lint"); - underTest.insert(dbSession, asList(dto1, dto2, dto3)); + underTest.insert(dbSession, asList(dto1,dto2,dto3)); - assertThat(underTest.selectByLanguage(dbSession, "java")).extracting(RuleRepositoryDto::getKey) - // ordered by key - .containsExactly("findbugs", "java"); + List<RuleRepositoryDto> ruleRepositoryDtos = underTest.selectByQueryAndLanguage(dbSession, "%a%",null); + + assertThat(ruleRepositoryDtos).extracting(RuleRepositoryDto::getName) + .containsExactlyInAnyOrder("Java","Findbugs"); } @Test + public void selectByQueryAndLanguage_whenSeveralRepoMatchingForDifferentLanguages_matchOnlyTheRepoOfTheChosenLanguage(){ + DbSession dbSession = dbTester.getSession(); + RuleRepositoryDto dto1 = new RuleRepositoryDto("findbugs", "java", "Findbugsa"); + RuleRepositoryDto dto2 = new RuleRepositoryDto("java", "java", "Java"); + RuleRepositoryDto dto3 = new RuleRepositoryDto("cobol-bug", "cobol", "Cobol Lint"); + underTest.insert(dbSession, asList(dto1,dto2,dto3)); + + List<RuleRepositoryDto> ruleRepositoryDtos = underTest.selectByQueryAndLanguage(dbSession, "%bug%", "java"); + + assertThat(ruleRepositoryDtos).extracting(RuleRepositoryDto::getKey) + .containsExactly("findbugs"); + } + + + @Test public void selectAllKeys() { DbSession dbSession = dbTester.getSession(); RuleRepositoryDto dto1 = new RuleRepositoryDto("findbugs", "java", "Findbugs"); @@ -147,14 +163,33 @@ public class RuleRepositoryDaoTest { } @Test - public void selectByLanguage_returns_empty_list_if_no_results() { + public void selectByQueryAndLanguage_returnsEmptyList_when_thereIsNoResults() { DbSession dbSession = dbTester.getSession(); RuleRepositoryDto dto1 = new RuleRepositoryDto("findbugs", "java", "Findbugs"); underTest.insert(dbSession, asList(dto1)); - assertThat(underTest.selectByLanguage(dbSession, "missing")).isEmpty(); + assertThat(underTest.selectByQueryAndLanguage(dbSession, "missing", null)).isEmpty(); } + @Test + public void selectByQueryAndLanguage_shouldBeCaseInsensitive(){ + DbSession dbSession = dbTester.getSession(); + RuleRepositoryDto dto1 = new RuleRepositoryDto("FINDBUGS", "java", "repoFB"); + RuleRepositoryDto dto2 = new RuleRepositoryDto("cobol-lint", "cobol", "Cobol Lint"); + RuleRepositoryDto dto3 = new RuleRepositoryDto("openjdk", "java", "JaVa"); + underTest.insert(dbSession, asList(dto1,dto2,dto3)); + + assertThat(underTest.selectByQueryAndLanguage(dbSession,"bug", null)) + .extracting(RuleRepositoryDto::getKey).contains("FINDBUGS"); + assertThat(underTest.selectByQueryAndLanguage(dbSession,"COBOL", null)) + .extracting(RuleRepositoryDto::getKey).contains("cobol-lint"); + assertThat(underTest.selectByQueryAndLanguage(dbSession,"jAvA", null)) + .extracting(RuleRepositoryDto::getKey).contains("openjdk"); + + } + + + private long selectCreatedAtByKey(DbSession dbSession, String key) { return (long) dbTester.selectFirst(dbSession, "select created_at as \"created_at\" from rule_repositories where kee='" + key + "'") .get("created_at"); |