3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.component.index;
22 import com.google.common.collect.ImmutableSet;
23 import org.junit.Test;
24 import org.sonar.db.component.ComponentDto;
25 import org.sonar.db.component.ComponentTesting;
26 import org.sonar.server.es.textsearch.ComponentTextSearchFeatureRepertoire;
28 import static java.util.Arrays.asList;
29 import static org.sonar.api.resources.Qualifiers.FILE;
30 import static org.sonar.api.resources.Qualifiers.MODULE;
31 import static org.sonar.api.resources.Qualifiers.PROJECT;
33 public class ComponentIndexScoreTest extends ComponentIndexTest {
36 public void should_prefer_components_without_prefix() {
37 assertResultOrder("File.java",
43 public void should_prefer_components_without_suffix() {
44 assertResultOrder("File",
50 public void should_prefer_key_matching_over_name_matching() {
51 ComponentDto project1 = indexProject("quality", "SonarQube");
52 ComponentDto project2 = indexProject("sonarqube", "Quality Product");
54 assertExactResults("sonarqube", project2, project1);
58 public void should_prefer_prefix_matching_over_partial_matching() {
59 assertResultOrder("corem",
65 public void should_prefer_case_sensitive_prefix() {
66 assertResultOrder("caSe",
72 public void scoring_prefix_with_multiple_words() {
73 assertResultOrder("index java",
74 "IndexSomething.java",
79 public void scoring_prefix_with_multiple_words_and_case() {
80 assertResultOrder("Index JAVA",
81 "IndexSomething.java",
86 public void scoring_long_items() {
87 assertResultOrder("ThisIsAVeryLongNameToSearchForAndItExceeds15Characters.java",
88 "ThisIsAVeryLongNameToSearchForAndItExceeds15Characters.java",
89 "ThisIsAVeryLongNameToSearchForAndItEndsDifferently.java");
93 public void scoring_perfect_match() {
94 assertResultOrder("SonarQube",
100 public void scoring_perfect_match_dispite_case_changes() {
101 assertResultOrder("sonarqube",
103 "SonarQube SCM Git");
107 public void scoring_perfect_match_with_matching_case_higher_than_without_matching_case() {
108 assertResultOrder("sonarqube",
114 public void should_prefer_favorite_over_recently_browsed() {
115 ComponentDto file1 = db.components().insertPrivateProject(c -> c.setName("File1"));
118 ComponentDto file2 = db.components().insertPrivateProject(c -> c.setName("File2"));
121 assertSearch(SuggestionQuery.builder()
123 .setQualifiers(asList(PROJECT, MODULE, FILE))
124 .setRecentlyBrowsedKeys(ImmutableSet.of(file1.getDbKey()))
125 .setFavoriteKeys(ImmutableSet.of(file2.getDbKey()))
126 .build()).containsExactly(uuids(file2, file1));
128 assertSearch(SuggestionQuery.builder()
130 .setQualifiers(asList(PROJECT, MODULE, FILE))
131 .setRecentlyBrowsedKeys(ImmutableSet.of(file2.getDbKey()))
132 .setFavoriteKeys(ImmutableSet.of(file1.getDbKey()))
133 .build()).containsExactly(uuids(file1, file2));
137 public void do_not_match_wrong_file_extension() {
138 ComponentDto file1 = indexFile("MyClass.java");
139 ComponentDto file2 = indexFile("ClassExample.java");
140 ComponentDto file3 = indexFile("Class.java");
141 indexFile("Class.cs");
142 indexFile("Class.js");
143 indexFile("Class.rb");
145 assertExactResults("Class java", file3, file2, file1);
149 public void if_relevancy_is_equal_fall_back_to_alphabetical_ordering() {
150 assertResultOrder("sonarqube",
156 public void scoring_test_DbTester() {
157 features.set(ComponentTextSearchFeatureRepertoire.PARTIAL);
159 ComponentDto project = indexProject("key-1", "Quality Product");
161 index(ComponentTesting.newFileDto(project)
162 .setName("DbTester.java")
163 .setDbKey("java/org/example/DbTester.java")
164 .setUuid("UUID-DbTester"));
166 index(ComponentTesting.newFileDto(project)
167 .setName("WebhookDbTesting.java")
168 .setDbKey("java/org/example/WebhookDbTesting.java")
169 .setUuid("UUID-WebhookDbTesting"));
171 assertSearch("dbt").containsExactly(
174 "UUID-WebhookDbTesting"