3 * Copyright (C) 2009-2020 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 java.util.Arrays;
23 import java.util.List;
24 import java.util.stream.Collectors;
25 import java.util.stream.IntStream;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.api.resources.Qualifiers;
29 import org.sonar.api.utils.System2;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.db.organization.OrganizationDto;
33 import org.sonar.server.es.EsTester;
34 import org.sonar.server.es.SearchIdResult;
35 import org.sonar.server.es.SearchOptions;
36 import org.sonar.server.es.textsearch.ComponentTextSearchFeatureRule;
37 import org.sonar.server.permission.index.PermissionIndexerTester;
38 import org.sonar.server.permission.index.WebAuthorizationTypeSupport;
39 import org.sonar.server.tester.UserSessionRule;
41 import static java.util.Collections.singleton;
42 import static org.assertj.core.api.Assertions.assertThat;
44 public class ComponentIndexSearchTest {
46 public EsTester es = EsTester.create();
48 public DbTester db = DbTester.create(System2.INSTANCE);
50 public UserSessionRule userSession = UserSessionRule.standalone();
52 public ComponentTextSearchFeatureRule features = new ComponentTextSearchFeatureRule();
54 private ComponentIndexer indexer = new ComponentIndexer(db.getDbClient(), es.client());
55 private PermissionIndexerTester authorizationIndexerTester = new PermissionIndexerTester(es, indexer);
57 private ComponentIndex underTest = new ComponentIndex(es.client(), new WebAuthorizationTypeSupport(userSession), System2.INSTANCE);
60 public void filter_by_name() {
61 ComponentDto ignoredProject = db.components().insertPrivateProject(p -> p.setName("ignored project"));
62 ComponentDto project = db.components().insertPrivateProject(p -> p.setName("Project Shiny name"));
63 index(ignoredProject, project);
65 SearchIdResult<String> result = underTest.search(ComponentQuery.builder().setQuery("shiny").build(), new SearchOptions());
67 assertThat(result.getUuids()).containsExactlyInAnyOrder(project.uuid());
71 public void filter_by_key_with_exact_match() {
72 ComponentDto ignoredProject = db.components().insertPrivateProject(p -> p.setDbKey("ignored-project"));
73 ComponentDto project = db.components().insertPrivateProject(p -> p.setDbKey("shiny-project"));
74 db.components().insertPrivateProject(p -> p.setDbKey("another-shiny-project"));
75 index(ignoredProject, project);
77 SearchIdResult<String> result = underTest.search(ComponentQuery.builder().setQuery("shiny-project").build(), new SearchOptions());
79 assertThat(result.getUuids()).containsExactlyInAnyOrder(project.uuid());
83 public void filter_by_qualifier() {
84 ComponentDto project = db.components().insertPrivateProject();
85 ComponentDto portfolio = db.components().insertPrivatePortfolio();
89 SearchIdResult<String> result = underTest.search(ComponentQuery.builder().setQualifiers(singleton(Qualifiers.PROJECT)).build(), new SearchOptions());
91 assertThat(result.getUuids()).containsExactlyInAnyOrder(project.uuid());
95 public void filter_by_organization() {
96 OrganizationDto organization = db.organizations().insert();
97 OrganizationDto anotherOrganization = db.organizations().insert();
98 ComponentDto project = db.components().insertPrivateProject(organization);
99 ComponentDto anotherProject = db.components().insertPrivateProject(anotherOrganization);
100 index(project, anotherProject);
102 SearchIdResult<String> result = underTest.search(ComponentQuery.builder().setOrganization(organization.getUuid()).build(), new SearchOptions());
104 assertThat(result.getUuids()).containsExactlyInAnyOrder(project.uuid());
108 public void order_by_name_case_insensitive() {
109 ComponentDto project2 = db.components().insertPrivateProject(p -> p.setName("PROJECT 2"));
110 ComponentDto project3 = db.components().insertPrivateProject(p -> p.setName("project 3"));
111 ComponentDto project1 = db.components().insertPrivateProject(p -> p.setName("Project 1"));
112 index(project1, project2, project3);
114 SearchIdResult<String> result = underTest.search(ComponentQuery.builder().build(), new SearchOptions());
116 assertThat(result.getUuids()).containsExactly(project1.uuid(), project2.uuid(), project3.uuid());
120 public void paginate_results() {
121 List<ComponentDto> projects = IntStream.range(0, 9)
122 .mapToObj(i -> db.components().insertPrivateProject(p -> p.setName("project " + i)))
123 .collect(Collectors.toList());
124 index(projects.toArray(new ComponentDto[0]));
126 SearchIdResult<String> result = underTest.search(ComponentQuery.builder().build(), new SearchOptions().setPage(2, 3));
128 assertThat(result.getUuids()).containsExactlyInAnyOrder(projects.get(3).uuid(), projects.get(4).uuid(), projects.get(5).uuid());
132 public void filter_unauthorized_components() {
133 ComponentDto unauthorizedProject = db.components().insertPrivateProject();
134 ComponentDto project1 = db.components().insertPrivateProject();
135 ComponentDto project2 = db.components().insertPrivateProject();
137 authorizationIndexerTester.allowOnlyAnyone(project1);
138 authorizationIndexerTester.allowOnlyAnyone(project2);
140 SearchIdResult<String> result = underTest.search(ComponentQuery.builder().build(), new SearchOptions());
142 assertThat(result.getUuids()).containsExactlyInAnyOrder(project1.uuid(), project2.uuid())
143 .doesNotContain(unauthorizedProject.uuid());
146 private void index(ComponentDto... components) {
148 Arrays.stream(components).forEach(c -> authorizationIndexerTester.allowOnlyAnyone(c));