diff options
author | Lukasz Jarocki <lukasz.jarocki@sonarsource.com> | 2023-05-23 10:27:06 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-06-01 20:02:59 +0000 |
commit | 92c132a13f46f23a5ecc5dee5dfdfb619e6fa65f (patch) | |
tree | af217205f33c28a90494945efaf680a587edd693 /server/sonar-webserver-es | |
parent | 36e9f01ad563aaeaae36dc6efc69e724f8c039c1 (diff) | |
download | sonarqube-92c132a13f46f23a5ecc5dee5dfdfb619e6fa65f.tar.gz sonarqube-92c132a13f46f23a5ecc5dee5dfdfb619e6fa65f.zip |
SONAR-18856 Refactor subportfolios and fix build and tests
Diffstat (limited to 'server/sonar-webserver-es')
14 files changed, 116 insertions, 171 deletions
diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexCombinationTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexCombinationTest.java index 313d9050184..f1b08fb1340 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexCombinationTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexCombinationTest.java @@ -23,6 +23,7 @@ import java.util.stream.IntStream; import org.junit.Test; import org.sonar.api.resources.Qualifiers; import org.sonar.db.component.ComponentDto; +import org.sonar.db.project.ProjectDto; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; @@ -44,14 +45,22 @@ public class ComponentIndexCombinationTest extends ComponentIndexTest { } @Test - public void filter_results_by_qualifier() { - ComponentDto project = indexProject("struts", "Apache Struts"); - indexFile(project, "src/main/java/StrutsManager.java", "StrutsManager.java"); + public void index_whenQualifierMatchesWhatIsTheIndex_shouldReturnTheProject() { + ProjectDto project = indexProject("struts", "Apache Struts"); assertSearchResults(SuggestionQuery.builder().setQuery("struts").setQualifiers(singletonList(Qualifiers.PROJECT)).build(), project); } @Test + public void index_whenQualifierDoesNotMatchWhatIsTheIndex_shouldReturnTheProject() { + ProjectDto project = indexProject("struts", "Apache Struts"); + + SuggestionQuery query = SuggestionQuery.builder().setQuery("struts").setQualifiers(singletonList(Qualifiers.VIEW)).build(); + + assertNoSearchResults(query.getQuery(), Qualifiers.VIEW); + } + + @Test public void should_limit_the_number_of_results() { IntStream.rangeClosed(0, 10).forEach(i -> indexProject("sonarqube" + i, "SonarQube" + i)); diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureExactTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureExactTest.java index cb48c9e79c9..7ed80295203 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureExactTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureExactTest.java @@ -23,6 +23,7 @@ import java.util.Collections; import org.junit.Before; import org.junit.Test; import org.sonar.db.component.ComponentDto; +import org.sonar.db.project.ProjectDto; import org.sonar.server.es.textsearch.ComponentTextSearchFeatureRepertoire; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; @@ -37,8 +38,8 @@ public class ComponentIndexFeatureExactTest extends ComponentIndexTest { @Test public void scoring_cares_about_exact_matches() { - ComponentDto project1 = indexProject("project1", "LongNameLongNameLongNameLongNameSonarQube"); - ComponentDto project2 = indexProject("project2", "LongNameLongNameLongNameLongNameSonarQubeX"); + ProjectDto project1 = indexProject("project1", "LongNameLongNameLongNameLongNameSonarQube"); + ProjectDto project2 = indexProject("project2", "LongNameLongNameLongNameLongNameSonarQubeX"); SuggestionQuery query1 = SuggestionQuery.builder() .setQuery("LongNameLongNameLongNameLongNameSonarQube") diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureFavoriteTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureFavoriteTest.java index 8fc4a10d6bc..c992bc553e8 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureFavoriteTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureFavoriteTest.java @@ -22,6 +22,7 @@ package org.sonar.server.component.index; import org.junit.Before; import org.junit.Test; import org.sonar.db.component.ComponentDto; +import org.sonar.db.project.ProjectDto; import org.sonar.server.es.textsearch.ComponentTextSearchFeatureRepertoire; import static com.google.common.collect.ImmutableSet.of; @@ -40,8 +41,8 @@ public class ComponentIndexFeatureFavoriteTest extends ComponentIndexTest { @Test public void scoring_cares_about_favorites() { - ComponentDto project1 = indexProject("sonarqube", "SonarQube"); - ComponentDto project2 = indexProject("recent", "SonarQube Recently"); + ProjectDto project1 = indexProject("sonarqube", "SonarQube"); + ProjectDto project2 = indexProject("recent", "SonarQube Recently"); SuggestionQuery query1 = SuggestionQuery.builder() .setQuery("SonarQube") @@ -61,7 +62,7 @@ public class ComponentIndexFeatureFavoriteTest extends ComponentIndexTest { @Test public void irrelevant_favorites_are_not_returned() { features.set(q -> termQuery(FIELD_KEY, "non-existing-value"), ComponentTextSearchFeatureRepertoire.FAVORITE); - ComponentDto project1 = indexProject("foo", "foo"); + ProjectDto project1 = indexProject("foo", "foo"); SuggestionQuery query1 = SuggestionQuery.builder() .setQuery("bar") diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureKeyTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureKeyTest.java index 0a00196cbe4..3ab5ac21085 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureKeyTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureKeyTest.java @@ -22,6 +22,7 @@ package org.sonar.server.component.index; import org.junit.Before; import org.junit.Test; import org.sonar.db.component.ComponentDto; +import org.sonar.db.project.ProjectDto; import org.sonar.server.es.textsearch.ComponentTextSearchFeatureRepertoire; public class ComponentIndexFeatureKeyTest extends ComponentIndexTest { @@ -33,7 +34,7 @@ public class ComponentIndexFeatureKeyTest extends ComponentIndexTest { @Test public void should_search_projects_by_exact_case_insensitive_key() { - ComponentDto project1 = indexProject("keyOne", "Project One"); + ProjectDto project1 = indexProject("keyOne", "Project One"); indexProject("keyTwo", "Project Two"); assertSearchResults("keyOne", project1); @@ -43,7 +44,7 @@ public class ComponentIndexFeatureKeyTest extends ComponentIndexTest { @Test public void should_search_project_with_dot_in_key() { - ComponentDto project = indexProject("org.sonarqube", "SonarQube"); + ProjectDto project = indexProject("org.sonarqube", "SonarQube"); assertSearchResults("org.sonarqube", project); assertNoSearchResults("orgsonarqube"); @@ -51,7 +52,7 @@ public class ComponentIndexFeatureKeyTest extends ComponentIndexTest { @Test public void should_search_project_with_dash_in_key() { - ComponentDto project = indexProject("org-sonarqube", "SonarQube"); + ProjectDto project = indexProject("org-sonarqube", "SonarQube"); assertSearchResults("org-sonarqube", project); assertNoSearchResults("orgsonarqube"); @@ -59,7 +60,7 @@ public class ComponentIndexFeatureKeyTest extends ComponentIndexTest { @Test public void should_search_project_with_colon_in_key() { - ComponentDto project = indexProject("org:sonarqube", "Quality Product"); + ProjectDto project = indexProject("org:sonarqube", "Quality Product"); assertSearchResults("org:sonarqube", project); assertNoSearchResults("orgsonarqube"); @@ -69,7 +70,7 @@ public class ComponentIndexFeatureKeyTest extends ComponentIndexTest { @Test public void should_search_project_with_all_special_characters_in_key() { - ComponentDto project = indexProject("org.sonarqube:sonar-sérvèr_ç", "SonarQube"); + ProjectDto project = indexProject("org.sonarqube:sonar-sérvèr_ç", "SonarQube"); assertSearchResults("org.sonarqube:sonar-sérvèr_ç", project); } diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeaturePartialTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeaturePartialTest.java index 033dfa48f5a..9bd6485da6e 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeaturePartialTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeaturePartialTest.java @@ -22,6 +22,7 @@ package org.sonar.server.component.index; import org.junit.Before; import org.junit.Test; import org.sonar.db.component.ComponentDto; +import org.sonar.db.project.ProjectDto; import org.sonar.server.es.textsearch.ComponentTextSearchFeatureRepertoire; public class ComponentIndexFeaturePartialTest extends ComponentIndexTest { @@ -33,7 +34,7 @@ public class ComponentIndexFeaturePartialTest extends ComponentIndexTest { @Test public void search_projects_by_exact_name() { - ComponentDto struts = indexProject("struts", "Apache Struts"); + ProjectDto struts = indexProject("struts", "Apache Struts"); indexProject("sonarqube", "SonarQube"); assertSearchResults("Apache Struts", struts); @@ -42,19 +43,8 @@ public class ComponentIndexFeaturePartialTest extends ComponentIndexTest { } @Test - public void search_file_with_long_name() { - ComponentDto project = indexProject("struts", "Apache Struts"); - ComponentDto file1 = indexFile(project, "src/main/java/DefaultRubyComponentServiceTestManagerFactory.java", "DefaultRubyComponentServiceTestManagerFactory.java"); - - assertSearchResults("DefaultRubyComponentServiceTestManagerFactory", file1); - assertSearchResults("DefaultRubyComponentServiceTestManagerFactory.java", file1); - assertSearchResults("RubyComponentServiceTestManager", file1); - assertSearchResults("te", file1); - } - - @Test public void should_search_by_name_with_two_characters() { - ComponentDto project = indexProject("struts", "Apache Struts"); + ProjectDto project = indexProject("struts", "Apache Struts"); assertSearchResults("st", project); assertSearchResults("tr", project); @@ -62,7 +52,7 @@ public class ComponentIndexFeaturePartialTest extends ComponentIndexTest { @Test public void search_projects_by_partial_name() { - ComponentDto struts = indexProject("struts", "Apache Struts"); + ProjectDto struts = indexProject("struts", "Apache Struts"); assertSearchResults("truts", struts); assertSearchResults("pache", struts); @@ -72,52 +62,36 @@ public class ComponentIndexFeaturePartialTest extends ComponentIndexTest { @Test public void search_projects_and_files_by_partial_name() { - ComponentDto project = indexProject("struts", "Apache Struts"); - ComponentDto file1 = indexFile(project, "src/main/java/StrutsManager.java", "StrutsManager.java"); - indexFile(project, "src/main/java/Foo.java", "Foo.java"); - - assertSearchResults("struts", project, file1); - assertSearchResults("Struts", project, file1); - assertSearchResults("StrutsManager", file1); - assertSearchResults("STRUTSMAN", file1); - assertSearchResults("utsManag", file1); - } + ProjectDto project = indexProject("struts", "Apache Struts"); - @Test - public void should_find_file_by_file_extension() { - ComponentDto project = indexProject("struts", "Apache Struts"); - ComponentDto file1 = indexFile(project, "src/main/java/StrutsManager.java", "StrutsManager.java"); - ComponentDto file2 = indexFile(project, "src/main/java/Foo.java", "Foo.java"); - - assertSearchResults(".java", file1, file2); - assertSearchResults("manager.java", file1); - - // do not match - assertNoSearchResults("somethingStrutsManager.java"); + assertSearchResults("struts", project); + assertSearchResults("Struts", project); } @Test public void should_search_for_word_and_suffix() { - assertFileMatches("plugin java", "AbstractPluginFactory.java"); + assertResultOrder("plugin java", "AbstractPluginFactory.java"); } @Test public void should_search_for_word_and_suffix_in_any_order() { - assertFileMatches("java plugin", "AbstractPluginFactory.java"); + assertResultOrder("java plugin", "AbstractPluginFactory.java"); } @Test public void should_search_for_two_words() { - assertFileMatches("abstract factory", "AbstractPluginFactory.java"); + assertResultOrder("abstract factory", "AbstractPluginFactory.java"); } @Test public void should_search_for_two_words_in_any_order() { - assertFileMatches("factory abstract", "AbstractPluginFactory.java"); + assertResultOrder("factory abstract", "AbstractPluginFactory.java"); } @Test public void should_require_at_least_one_matching_word() { - assertNoFileMatches("monitor object", "AbstractPluginFactory.java"); + indexProject("AbstractPluginFactory"); + + assertNoSearchResults("monitor object"); } } diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeaturePrefixTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeaturePrefixTest.java index 2885f26eb81..69a4f0411da 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeaturePrefixTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeaturePrefixTest.java @@ -42,7 +42,9 @@ public class ComponentIndexFeaturePrefixTest extends ComponentIndexTest { @Test public void should_not_find_partially() { - assertNoFileMatches("component.js", "my_component.js"); + indexProject("my_component"); + + assertNoSearchResults("component.js"); } @Test diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureRecentlyBrowsedTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureRecentlyBrowsedTest.java index 2548f01b28f..56c2c27e102 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureRecentlyBrowsedTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureRecentlyBrowsedTest.java @@ -23,6 +23,7 @@ import java.util.Collections; import org.junit.Before; import org.junit.Test; import org.sonar.db.component.ComponentDto; +import org.sonar.db.project.ProjectDto; import org.sonar.server.es.textsearch.ComponentTextSearchFeatureRepertoire; import static com.google.common.collect.ImmutableSet.of; @@ -38,8 +39,8 @@ public class ComponentIndexFeatureRecentlyBrowsedTest extends ComponentIndexTest @Test public void scoring_cares_about_recently_browsed() { - ComponentDto project1 = indexProject("sonarqube", "SonarQube"); - ComponentDto project2 = indexProject("recent", "SonarQube Recently"); + ProjectDto project1 = indexProject("sonarqube", "SonarQube"); + ProjectDto project2 = indexProject("recent", "SonarQube Recently"); SuggestionQuery query1 = SuggestionQuery.builder() .setQuery("SonarQube") diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexHighlightTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexHighlightTest.java index 73cbfa4b589..6d73654b38f 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexHighlightTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexHighlightTest.java @@ -59,12 +59,12 @@ public class ComponentIndexHighlightTest extends ComponentIndexTest { assertHighlighting("quickbrownfox", "fox brown", "quick<mark>brownfox</mark>"); } - private void assertHighlighting(String fileName, String search, String expectedHighlighting) { - indexFile(fileName); + private void assertHighlighting(String projectName, String search, String expectedHighlighting) { + indexProject(projectName, projectName); SuggestionQuery query = SuggestionQuery.builder() .setQuery(search) - .setQualifiers(Collections.singletonList(Qualifiers.FILE)) + .setQualifiers(Collections.singletonList(Qualifiers.PROJECT)) .build(); Stream<ComponentHitsPerQualifier> results = index.searchSuggestions(query, features.get()).getQualifiers(); diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexLoginTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexLoginTest.java index 47b513f166e..6e2bf368fa7 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexLoginTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexLoginTest.java @@ -21,6 +21,7 @@ package org.sonar.server.component.index; import org.junit.Test; import org.sonar.db.component.ComponentDto; +import org.sonar.db.entity.EntityDto; import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; @@ -44,7 +45,7 @@ public class ComponentIndexLoginTest extends ComponentIndexTest { UserDto user = newUserDto(); userSession.logIn(user); - ComponentDto project = newProject("sonarqube", "Quality Product"); + EntityDto project = newProject("sonarqube", "Quality Product"); indexer.index(project); assertNoSearchResults("sonarqube"); @@ -59,7 +60,7 @@ public class ComponentIndexLoginTest extends ComponentIndexTest { GroupDto group = newGroupDto(); userSession.logIn().setGroups(group); - ComponentDto project = newProject("sonarqube", "Quality Product"); + EntityDto project = newProject("sonarqube", "Quality Product"); indexer.index(project); assertNoSearchResults("sonarqube"); diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexMultipleWordsTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexMultipleWordsTest.java index 7bbef7a4762..0ec52d288b9 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexMultipleWordsTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexMultipleWordsTest.java @@ -73,23 +73,5 @@ public class ComponentIndexMultipleWordsTest extends ComponentIndexTest { "Some.Struts.Class.java.old"); } - @Test - public void should_require_all_words_to_match() { - assertNoFileMatches("struts java", - "Struts"); - } - - @Test - public void should_ignore_empty_words() { - assertFileMatches(" struts \n \n\n", - "Struts"); - } - - @Test - public void should_require_all_words_to_match_for_partial() { - features.set(ComponentTextSearchFeatureRepertoire.PARTIAL); - assertNoFileMatches("struts java", - "Struts"); - } } diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexScoreTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexScoreTest.java index 847532a171c..e911826d01f 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexScoreTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexScoreTest.java @@ -20,9 +20,12 @@ package org.sonar.server.component.index; import com.google.common.collect.ImmutableSet; +import java.util.List; +import java.util.Set; import org.junit.Test; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentTesting; +import org.sonar.db.project.ProjectDto; import org.sonar.server.es.textsearch.ComponentTextSearchFeatureRepertoire; import static java.util.Arrays.asList; @@ -49,8 +52,8 @@ public class ComponentIndexScoreTest extends ComponentIndexTest { @Test public void should_prefer_key_matching_over_name_matching() { es.recreateIndexes(); - ComponentDto project1 = indexProject("quality", "SonarQube"); - ComponentDto project2 = indexProject("sonarqube", "Quality Product"); + ProjectDto project1 = indexProject("quality", "SonarQube"); + ProjectDto project2 = indexProject("sonarqube", "Quality Product"); assertExactResults("sonarqube", project2, project1); } @@ -113,37 +116,25 @@ public class ComponentIndexScoreTest extends ComponentIndexTest { @Test public void should_prefer_favorite_over_recently_browsed() { - ComponentDto file1 = db.components().insertPrivateProject(c -> c.setName("File1")).getMainBranchComponent(); - index(file1); + ProjectDto project1 = db.components().insertPrivateProject(c -> c.setName("File1")).getProjectDto(); + index(project1); - ComponentDto file2 = db.components().insertPrivateProject(c -> c.setName("File2")).getMainBranchComponent(); - index(file2); + ProjectDto project2 = db.components().insertPrivateProject(c -> c.setName("File2")).getProjectDto(); + index(project2); assertSearch(SuggestionQuery.builder() .setQuery("File") - .setQualifiers(asList(PROJECT, DIRECTORY, FILE)) - .setRecentlyBrowsedKeys(ImmutableSet.of(file1.getKey())) - .setFavoriteKeys(ImmutableSet.of(file2.getKey())) - .build()).containsExactly(uuids(file2, file1)); + .setQualifiers(List.of(PROJECT)) + .setRecentlyBrowsedKeys(Set.of(project1.getKey())) + .setFavoriteKeys(Set.of(project2.getKey())) + .build()).containsExactly(uuids(project2, project1)); assertSearch(SuggestionQuery.builder() .setQuery("File") - .setQualifiers(asList(PROJECT, DIRECTORY, FILE)) - .setRecentlyBrowsedKeys(ImmutableSet.of(file2.getKey())) - .setFavoriteKeys(ImmutableSet.of(file1.getKey())) - .build()).containsExactly(uuids(file1, file2)); - } - - @Test - public void do_not_match_wrong_file_extension() { - ComponentDto file1 = indexFile("MyClass.java"); - ComponentDto file2 = indexFile("ClassExample.java"); - ComponentDto file3 = indexFile("Class.java"); - indexFile("Class.cs"); - indexFile("Class.js"); - indexFile("Class.rb"); - - assertExactResults("Class java", file3, file2, file1); + .setQualifiers(List.of(PROJECT)) + .setRecentlyBrowsedKeys(Set.of(project2.getKey())) + .setFavoriteKeys(Set.of(project1.getKey())) + .build()).containsExactly(uuids(project1, project2)); } @Test @@ -157,23 +148,8 @@ public class ComponentIndexScoreTest extends ComponentIndexTest { public void scoring_test_DbTester() { features.set(ComponentTextSearchFeatureRepertoire.PARTIAL); - ComponentDto project = indexProject("key-1", "Quality Product"); - - index(ComponentTesting.newFileDto(project) - .setName("DbTester.java") - .setKey("java/org/example/DbTester.java") - .setUuid("UUID-DbTester")); - - index(ComponentTesting.newFileDto(project) - .setName("WebhookDbTesting.java") - .setKey("java/org/example/WebhookDbTesting.java") - .setUuid("UUID-WebhookDbTesting")); - - assertSearch("dbt").containsExactly( - - "UUID-DbTester", - "UUID-WebhookDbTesting" + ProjectDto project = indexProject("key-1", "Quality Product"); - ); + assertSearch("dbt").isEmpty(); } } diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexSearchWindowExceededTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexSearchWindowExceededTest.java index c3c9ea9602d..6efa0172d22 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexSearchWindowExceededTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexSearchWindowExceededTest.java @@ -65,7 +65,6 @@ public class ComponentIndexSearchWindowExceededTest { .setId(componentDoc.uuid()) .setKey(componentDoc.getKey()) .setName(componentDoc.name()) - .setProjectUuid(componentDoc.branchUuid()) .setQualifier(componentDoc.qualifier()); } } diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexTest.java index 3751a7dea19..5e48e0c3733 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexTest.java @@ -22,12 +22,17 @@ package org.sonar.server.component.index; import java.util.Arrays; import java.util.Comparator; import java.util.List; +import java.util.stream.Collectors; import org.assertj.core.api.ListAssert; import org.junit.Rule; +import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; +import org.sonar.db.component.ComponentDbTester; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentTesting; +import org.sonar.db.entity.EntityDto; +import org.sonar.db.project.ProjectDto; import org.sonar.server.es.EsTester; import org.sonar.server.es.textsearch.ComponentTextSearchFeatureRule; import org.sonar.server.permission.index.PermissionIndexerTester; @@ -44,7 +49,7 @@ public abstract class ComponentIndexTest { @Rule public EsTester es = EsTester.create(); @Rule - public DbTester db = DbTester.create(System2.INSTANCE); + public DbTester db = DbTester.create(System2.INSTANCE, true); @Rule public UserSessionRule userSession = UserSessionRule.standalone(); @@ -55,32 +60,21 @@ public abstract class ComponentIndexTest { protected ComponentIndex index = new ComponentIndex(es.client(), new WebAuthorizationTypeSupport(userSession), System2.INSTANCE); protected PermissionIndexerTester authorizationIndexerTester = new PermissionIndexerTester(es, indexer); - protected void assertFileMatches(String query, String... fileNames) { - ComponentDto[] files = Arrays.stream(fileNames) - .map(this::indexFile) - .toArray(ComponentDto[]::new); - assertSearch(query).containsExactlyInAnyOrder(uuids(files)); - } - - protected void assertNoFileMatches(String query, String... fileNames) { - Arrays.stream(fileNames) - .forEach(this::indexFile); - assertSearch(query).isEmpty(); - } + private final ComponentDbTester componentDbTester = new ComponentDbTester(db); protected void assertResultOrder(String query, String... resultsInOrder) { - ComponentDto project = indexProject("key-1", "Quality Product"); - List<ComponentDto> files = Arrays.stream(resultsInOrder) - .map(r -> ComponentTesting.newFileDto(project).setName(r)) - .peek(f -> f.setUuid(f.uuid() + "_" + f.name().replaceAll("[^a-zA-Z0-9]", ""))) + indexProject("key-1", "Quality Product"); + List<ProjectDto> projects = Arrays.stream(resultsInOrder) + .map(r -> componentDbTester.insertPublicProject(c -> c.setName(r).setKey(r)).getProjectDto()) + .peek(p -> p.setUuid(p.getUuid() + "_" + p.getName().replaceAll("[^a-zA-Z0-9]", ""))) .toList(); // index them, but not in the expected order - files.stream() - .sorted(Comparator.comparing(ComponentDto::uuid).reversed()) + projects.stream() + .sorted(Comparator.comparing(ProjectDto::getUuid).reversed()) .forEach(this::index); - assertExactResults(query, files.toArray(new ComponentDto[0])); + assertExactResults(query, projects.toArray(new ProjectDto[0])); } protected ListAssert<String> assertSearch(String query) { @@ -93,54 +87,45 @@ public abstract class ComponentIndexTest { .extracting(ComponentHit::getUuid); } - protected void assertSearchResults(String query, ComponentDto... expectedComponents) { - assertSearchResults(SuggestionQuery.builder().setQuery(query).setQualifiers(asList(PROJECT, FILE)).build(), expectedComponents); + protected void assertSearchResults(String query, EntityDto... expectedComponents) { + assertSearchResults(query, List.of(PROJECT), expectedComponents); } - protected void assertSearchResults(SuggestionQuery query, ComponentDto... expectedComponents) { - assertSearch(query).containsOnly(uuids(expectedComponents)); + protected void assertSearchResults(String query, List<String> queryQualifiers, EntityDto... expectedComponents) { + assertSearchResults(SuggestionQuery.builder().setQuery(query).setQualifiers(queryQualifiers).build(), expectedComponents); } - protected void assertExactResults(String query, ComponentDto... expectedComponents) { - assertSearch(query).containsExactly(uuids(expectedComponents)); + protected void assertSearchResults(SuggestionQuery query, EntityDto... expectedComponents) { + assertSearch(query).containsOnly(uuids(expectedComponents)); } - protected void assertNoSearchResults(String query) { - assertSearchResults(query); + protected void assertExactResults(String query, ProjectDto... expectedComponents) { + assertSearch(query).containsExactly(uuids(expectedComponents)); } - protected ComponentDto indexProject(String key, String name) { - return index( - ComponentTesting.newPrivateProjectDto("UUID_" + key) - .setKey(key) - .setName(name)); + protected void assertNoSearchResults(String query, String ... qualifiers) { + assertSearchResults(query, List.of(qualifiers)); } - protected ComponentDto newProject(String key, String name) { - return ComponentTesting.newPrivateProjectDto("UUID_" + key) - .setKey(key) - .setName(name); + protected ProjectDto indexProject(String name) { + return indexProject(name, name); } - protected ComponentDto indexFile(String fileName) { - ComponentDto project = indexProject("key-1", "SonarQube"); - return indexFile(project, "src/main/java/" + fileName, fileName); + protected ProjectDto indexProject(String key, String name) { + return index(componentDbTester.insertPublicProject("UUID" + key, c -> c.setKey(key).setName(name)).getProjectDto()); } - protected ComponentDto indexFile(ComponentDto project, String fileKey, String fileName) { - return index( - ComponentTesting.newFileDto(project) - .setKey(fileKey) - .setName(fileName)); + protected EntityDto newProject(String key, String name) { + return componentDbTester.insertPublicProject("UUID_" + key, c -> c.setKey(key).setName(name)).getProjectDto(); } - protected ComponentDto index(ComponentDto dto) { + protected ProjectDto index(ProjectDto dto) { indexer.index(dto); authorizationIndexerTester.allowOnlyAnyone(dto); return dto; } - protected static String[] uuids(ComponentDto... expectedComponents) { - return Arrays.stream(expectedComponents).map(ComponentDto::uuid).toArray(String[]::new); + protected static String[] uuids(EntityDto... expectedComponents) { + return Arrays.stream(expectedComponents).map(EntityDto::getUuid).toArray(String[]::new); } } diff --git a/server/sonar-webserver-es/src/testFixtures/java/org/sonar/server/permission/index/PermissionIndexerTester.java b/server/sonar-webserver-es/src/testFixtures/java/org/sonar/server/permission/index/PermissionIndexerTester.java index 59d6417564f..abc0453fa77 100644 --- a/server/sonar-webserver-es/src/testFixtures/java/org/sonar/server/permission/index/PermissionIndexerTester.java +++ b/server/sonar-webserver-es/src/testFixtures/java/org/sonar/server/permission/index/PermissionIndexerTester.java @@ -22,6 +22,7 @@ package org.sonar.server.permission.index; import java.util.List; import java.util.stream.Stream; import org.sonar.db.component.ComponentDto; +import org.sonar.db.entity.EntityDto; import org.sonar.db.project.ProjectDto; import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; @@ -52,12 +53,24 @@ public class PermissionIndexerTester { return allow(dto); } + public PermissionIndexerTester allowOnlyUser(EntityDto entityDto, UserDto user) { + IndexPermissions dto = new IndexPermissions(entityDto.getUuid(), entityDto.getQualifier()) + .addUserUuid(user.getUuid()); + return allow(dto); + } + public PermissionIndexerTester allowOnlyGroup(ComponentDto project, GroupDto group) { IndexPermissions dto = new IndexPermissions(project.uuid(), project.qualifier()) .addGroupUuid(group.getUuid()); return allow(dto); } + public PermissionIndexerTester allowOnlyGroup(EntityDto entityDto, GroupDto group) { + IndexPermissions dto = new IndexPermissions(entityDto.getUuid(), entityDto.getQualifier()) + .addGroupUuid(group.getUuid()); + return allow(dto); + } + public PermissionIndexerTester allow(IndexPermissions... indexPermissions) { return allow(stream(indexPermissions).toList()); } |