]> source.dussan.org Git - sonarqube.git/blob
d7af68df6e379a9701fff0f50619815df076511d
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.component.index;
21
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;
40
41 import static java.util.Collections.singleton;
42 import static org.assertj.core.api.Assertions.assertThat;
43
44 public class ComponentIndexSearchTest {
45   @Rule
46   public EsTester es = EsTester.create();
47   @Rule
48   public DbTester db = DbTester.create(System2.INSTANCE);
49   @Rule
50   public UserSessionRule userSession = UserSessionRule.standalone();
51   @Rule
52   public ComponentTextSearchFeatureRule features = new ComponentTextSearchFeatureRule();
53
54   private ComponentIndexer indexer = new ComponentIndexer(db.getDbClient(), es.client());
55   private PermissionIndexerTester authorizationIndexerTester = new PermissionIndexerTester(es, indexer);
56
57   private ComponentIndex underTest = new ComponentIndex(es.client(), new WebAuthorizationTypeSupport(userSession), System2.INSTANCE);
58
59   @Test
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);
64
65     SearchIdResult<String> result = underTest.search(ComponentQuery.builder().setQuery("shiny").build(), new SearchOptions());
66
67     assertThat(result.getUuids()).containsExactlyInAnyOrder(project.uuid());
68   }
69
70   @Test
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);
76
77     SearchIdResult<String> result = underTest.search(ComponentQuery.builder().setQuery("shiny-project").build(), new SearchOptions());
78
79     assertThat(result.getUuids()).containsExactlyInAnyOrder(project.uuid());
80   }
81
82   @Test
83   public void filter_by_qualifier() {
84     ComponentDto project = db.components().insertPrivateProject();
85     ComponentDto portfolio = db.components().insertPrivatePortfolio();
86     index(project);
87     index(portfolio);
88
89     SearchIdResult<String> result = underTest.search(ComponentQuery.builder().setQualifiers(singleton(Qualifiers.PROJECT)).build(), new SearchOptions());
90
91     assertThat(result.getUuids()).containsExactlyInAnyOrder(project.uuid());
92   }
93
94   @Test
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);
101
102     SearchIdResult<String> result = underTest.search(ComponentQuery.builder().setOrganization(organization.getUuid()).build(), new SearchOptions());
103
104     assertThat(result.getUuids()).containsExactlyInAnyOrder(project.uuid());
105   }
106
107   @Test
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);
113
114     SearchIdResult<String> result = underTest.search(ComponentQuery.builder().build(), new SearchOptions());
115
116     assertThat(result.getUuids()).containsExactly(project1.uuid(), project2.uuid(), project3.uuid());
117   }
118
119   @Test
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]));
125
126     SearchIdResult<String> result = underTest.search(ComponentQuery.builder().build(), new SearchOptions().setPage(2, 3));
127
128     assertThat(result.getUuids()).containsExactlyInAnyOrder(projects.get(3).uuid(), projects.get(4).uuid(), projects.get(5).uuid());
129   }
130
131   @Test
132   public void filter_unauthorized_components() {
133     ComponentDto unauthorizedProject = db.components().insertPrivateProject();
134     ComponentDto project1 = db.components().insertPrivateProject();
135     ComponentDto project2 = db.components().insertPrivateProject();
136     indexer.indexAll();
137     authorizationIndexerTester.allowOnlyAnyone(project1);
138     authorizationIndexerTester.allowOnlyAnyone(project2);
139
140     SearchIdResult<String> result = underTest.search(ComponentQuery.builder().build(), new SearchOptions());
141
142     assertThat(result.getUuids()).containsExactlyInAnyOrder(project1.uuid(), project2.uuid())
143       .doesNotContain(unauthorizedProject.uuid());
144   }
145
146   private void index(ComponentDto... components) {
147     indexer.indexAll();
148     Arrays.stream(components).forEach(c -> authorizationIndexerTester.allowOnlyAnyone(c));
149   }
150 }