diff options
Diffstat (limited to 'server')
21 files changed, 195 insertions, 207 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/entity/EntityDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/entity/EntityDto.java index 11aa6ef3d37..03e0b564dc3 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/entity/EntityDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/entity/EntityDto.java @@ -32,6 +32,16 @@ public class EntityDto { protected String qualifier; protected boolean isPrivate; + // This field should be null for anything that is not subportfolio + protected String authUuid; + + public String getAuthUuid() { + if (qualifier.equals("SVW")) { + return authUuid; + } + return uuid; + } + public String getKey() { return kee; } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml index 0264a211032..d48462256c2 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml @@ -14,7 +14,9 @@ p.selection_mode as selectionMode, p.selection_expression as selectionExpression, p.created_at as createdAt, - p.updated_at as updatedAt + p.updated_at as updatedAt, + case when p.parent_uuid is null then 'VW' else 'SVW' end as qualifier, + case when p.root_uuid != p.uuid then p.root_uuid else null end as authUuid </sql> <sql id="projectColumns"> diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml index 11332464dcd..e623ae82be7 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml @@ -203,12 +203,13 @@ </select> <sql id="entityProjectColumns"> - p.uuid as uuid, p.kee as kee, p.name as name, p.private as isPrivate, p.qualifier as qualifier + p.uuid as uuid, p.kee as kee, p.name as name, p.private as isPrivate, p.qualifier as qualifier, null as authUuid </sql> <sql id="entityPortfolioColumns"> p.uuid as uuid, p.kee as kee, p.name as name, p.private as isPrivate, - case when p.parent_uuid is null then 'VW' else 'SVW' end as qualifier + case when p.parent_uuid is null then 'VW' else 'SVW' end as qualifier, + case when p.root_uuid != p.uuid then p.root_uuid else null end as authUuid </sql> <select id="selectEntityByUuid" parameterType="string" resultType="Entity"> diff --git a/server/sonar-server-common/src/it/java/org/sonar/server/component/index/ComponentIndexerIT.java b/server/sonar-server-common/src/it/java/org/sonar/server/component/index/ComponentIndexerIT.java index 4835bc85c94..28bc6d98112 100644 --- a/server/sonar-server-common/src/it/java/org/sonar/server/component/index/ComponentIndexerIT.java +++ b/server/sonar-server-common/src/it/java/org/sonar/server/component/index/ComponentIndexerIT.java @@ -32,6 +32,7 @@ import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentUpdateDto; import org.sonar.db.es.EsQueueDto; +import org.sonar.db.project.ProjectDto; import org.sonar.server.es.EsClient; import org.sonar.server.es.EsTester; import org.sonar.server.es.IndexingResult; @@ -136,22 +137,6 @@ public class ComponentIndexerIT { } @Test - public void indexOnAnalysis_updates_index_on_changes() { - ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); - underTest.indexOnAnalysis(project.uuid()); - assertThatComponentHasName(project, project.name()); - - // modify - project.setName("NewName"); - updateDb(project); - - // verify that index is updated - underTest.indexOnAnalysis(project.uuid()); - assertThatIndexContainsOnly(project); - assertThatComponentHasName(project, "NewName"); - } - - @Test public void indexOnAnalysis_does_not_index_non_main_branches() { ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("feature/foo")); @@ -264,7 +249,11 @@ public class ComponentIndexerIT { Arrays.stream(expectedComponents).map(ComponentDto::uuid).toArray(String[]::new)); } - private void assertThatComponentHasName(ComponentDto component, String expectedName) { + private void assertThatIndexContainsOnly(String uuid) { + assertThat(es.getIds(TYPE_COMPONENT)).containsExactlyInAnyOrder(uuid); + } + + private void assertThatComponentHasName(String uuid, String expectedName) { SearchHit[] hits = es.client() .search(EsClient.prepareSearch(TYPE_COMPONENT.getMainType()) .source(new SearchSourceBuilder() @@ -273,6 +262,6 @@ public class ComponentIndexerIT { .getHits(); assertThat(hits) .extracting(SearchHit::getId) - .contains(component.uuid()); + .contains(uuid); } } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentDoc.java b/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentDoc.java index d2fff419631..a05706d4f3e 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentDoc.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentDoc.java @@ -20,7 +20,9 @@ package org.sonar.server.component.index; import java.util.HashMap; +import java.util.Map; import org.sonar.server.es.BaseDoc; +import org.sonar.server.permission.index.AuthorizationDoc; import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_KEY; import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_NAME; @@ -34,6 +36,13 @@ public class ComponentDoc extends BaseDoc { super(TYPE_COMPONENT, new HashMap<>(6)); } + /** + * Needed for reflection. Do not remove. + */ + public ComponentDoc(Map<String, Object> fields) { + super(TYPE_COMPONENT, fields); + } + @Override public String getId() { return getField(FIELD_UUID); @@ -41,6 +50,7 @@ public class ComponentDoc extends BaseDoc { public ComponentDoc setId(String s) { setField(FIELD_UUID, s); + setParent(AuthorizationDoc.idOf(s)); return this; } @@ -70,4 +80,9 @@ public class ComponentDoc extends BaseDoc { setField(FIELD_QUALIFIER, s); return this; } + + public ComponentDoc setAuthUuid(String s) { + setParent(AuthorizationDoc.idOf(s)); + return this; + } } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java b/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java index 8c8d295d0a0..3c0802f194f 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java @@ -56,7 +56,7 @@ public class ComponentIndexDefinition implements IndexDefinition { private ComponentIndexDefinition(Configuration config, boolean enableSource) { this.config = config; - this.enableSource = enableSource; + this.enableSource = true; } @Inject diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexer.java b/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexer.java index c3287289257..5051ed8284c 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexer.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexer.java @@ -25,7 +25,9 @@ import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.List; +import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; import javax.annotation.Nullable; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.index.query.QueryBuilders; @@ -35,6 +37,7 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.entity.EntityDto; import org.sonar.db.es.EsQueueDto; +import org.sonar.db.portfolio.PortfolioDto; import org.sonar.server.es.BaseDoc; import org.sonar.server.es.BulkIndexer; import org.sonar.server.es.BulkIndexer.Size; @@ -70,11 +73,11 @@ public class ComponentIndexer implements ProjectIndexer, NeedAuthorizationIndexe @Override public void indexOnStartup(Set<IndexType> uninitializedIndexTypes) { - doIndexByProjectUuid(null, Size.LARGE); + doIndexByProjectUuid(Size.LARGE); } public void indexAll() { - doIndexByProjectUuid(null, Size.REGULAR); + doIndexByProjectUuid(Size.REGULAR); } @Override @@ -83,8 +86,8 @@ public class ComponentIndexer implements ProjectIndexer, NeedAuthorizationIndexe } @Override - public void indexOnAnalysis(String branchUuid, Set<String> unchangedComponentUuids) { - doIndexByProjectUuid(branchUuid, Size.REGULAR); + public void indexOnAnalysis(String entityUuid, Set<String> unchangedComponentUuids) { + doIndexByProjectUuid(entityUuid); } @Override @@ -124,8 +127,8 @@ public class ComponentIndexer implements ProjectIndexer, NeedAuthorizationIndexe for (String entityUuid : entityUuids) { dbClient.projectDao().scrollEntitiesForIndexing(dbSession, entityUuid, context -> { EntityDto dto = context.getResultObject(); - bulkIndexer.add(toDocument(dto).toIndexRequest()); remaining.remove(dto.getUuid()); + bulkIndexer.add(toDocument(dto).toIndexRequest()); }); } @@ -137,20 +140,42 @@ public class ComponentIndexer implements ProjectIndexer, NeedAuthorizationIndexe } /** - * @param projectUuid the uuid of the project to analyze, or {@code null} if all content should be indexed.<br/> - * <b>Warning:</b> only use {@code null} during startup. + * @param entityUuid the uuid of the project to analyze, or {@code null} if all content should be indexed.<br/> + * <b>Warning:</b> only use {@code null} during startup. */ - private void doIndexByProjectUuid(@Nullable String projectUuid, Size bulkSize) { - BulkIndexer bulk = new BulkIndexer(esClient, TYPE_COMPONENT, bulkSize); + private void doIndexByProjectUuid(String entityUuid) { + BulkIndexer bulk = new BulkIndexer(esClient, TYPE_COMPONENT, Size.REGULAR); + bulk.start(); + + try (DbSession dbSession = dbClient.openSession(false)) { + Optional<EntityDto> entityDto = dbClient.projectDao().selectEntityByUuid(dbSession, entityUuid); + + if (entityDto.isEmpty()) { + return; + } + EntityDto entity = entityDto.get(); + + bulk.add(toDocument(entity).toIndexRequest()); + if (entity.getQualifier().equals("VW")) { + dbClient.portfolioDao().selectTree(dbSession, entityUuid).forEach(sub -> + bulk.add(toDocument(sub).toIndexRequest())); + } + } + + bulk.stop(); + } + + private void doIndexByProjectUuid(Size bulkSize) { + BulkIndexer bulk = new BulkIndexer(esClient, TYPE_COMPONENT, bulkSize); bulk.start(); try (DbSession dbSession = dbClient.openSession(false)) { - dbClient.projectDao() - .scrollEntitiesForIndexing(dbSession, projectUuid, context -> { - EntityDto dto = context.getResultObject(); - bulk.add(toDocument(dto).toIndexRequest()); - }); + dbClient.projectDao().scrollEntitiesForIndexing(dbSession, null, context -> { + EntityDto dto = context.getResultObject(); + bulk.add(toDocument(dto).toIndexRequest()); + }); } + bulk.stop(); } @@ -182,6 +207,7 @@ public class ComponentIndexer implements ProjectIndexer, NeedAuthorizationIndexe public static ComponentDoc toDocument(EntityDto component) { return new ComponentDoc() .setId(component.getUuid()) + .setAuthUuid(component.getAuthUuid()) .setName(component.getName()) .setKey(component.getKey()) .setQualifier(component.getQualifier()); 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()); } |