3 * Copyright (C) 2009-2023 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.stream.IntStream;
23 import org.elasticsearch.index.query.QueryBuilders;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.utils.System2;
27 import org.sonar.db.component.ComponentDto;
28 import org.sonar.db.component.ComponentTesting;
29 import org.sonar.server.es.EsTester;
30 import org.sonar.server.es.SearchIdResult;
31 import org.sonar.server.es.SearchOptions;
32 import org.sonar.server.permission.index.WebAuthorizationTypeSupport;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.when;
37 import static org.sonar.server.component.index.ComponentIndexDefinition.TYPE_COMPONENT;
39 public class ComponentIndexSearchWindowExceededTest {
41 public EsTester es = EsTester.create();
43 private final WebAuthorizationTypeSupport authorizationTypeSupport = mock(WebAuthorizationTypeSupport.class);
44 private final ComponentIndex underTest = new ComponentIndex(es.client(), authorizationTypeSupport, System2.INSTANCE);
47 public void returns_correct_total_number_if_default_index_window_exceeded() {
48 // bypassing the permission check, to have easily 12_000 elements searcheable without having to inserting them + permission.
49 when(authorizationTypeSupport.createQueryFilter()).thenReturn(QueryBuilders.matchAllQuery());
51 index(IntStream.range(0, 12_000)
52 .mapToObj(i -> newDoc(ComponentTesting.newPublicProjectDto()))
53 .toArray(ComponentDoc[]::new));
55 SearchIdResult<String> result = underTest.search(ComponentQuery.builder().build(), new SearchOptions().setPage(2, 3));
56 assertThat(result.getTotal()).isEqualTo(12_000);
59 private void index(ComponentDoc... componentDocs) {
60 es.putDocuments(TYPE_COMPONENT.getMainType(), componentDocs);
63 private ComponentDoc newDoc(ComponentDto componentDoc) {
64 return new ComponentDoc()
65 .setId(componentDoc.uuid())
66 .setKey(componentDoc.getKey())
67 .setName(componentDoc.name())
68 .setQualifier(componentDoc.qualifier());