You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ComponentIndexTest.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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. import java.util.Arrays;
  22. import java.util.Comparator;
  23. import java.util.List;
  24. import org.assertj.core.api.ListAssert;
  25. import org.junit.Rule;
  26. import org.sonar.api.utils.System2;
  27. import org.sonar.db.DbTester;
  28. import org.sonar.db.component.ComponentDto;
  29. import org.sonar.db.component.ComponentTesting;
  30. import org.sonar.server.es.EsTester;
  31. import org.sonar.server.es.textsearch.ComponentTextSearchFeatureRule;
  32. import org.sonar.server.permission.index.PermissionIndexerTester;
  33. import org.sonar.server.permission.index.WebAuthorizationTypeSupport;
  34. import org.sonar.server.tester.UserSessionRule;
  35. import static java.util.Arrays.asList;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. import static org.sonar.api.resources.Qualifiers.FILE;
  38. import static org.sonar.api.resources.Qualifiers.PROJECT;
  39. public abstract class ComponentIndexTest {
  40. @Rule
  41. public EsTester es = EsTester.create();
  42. @Rule
  43. public DbTester db = DbTester.create(System2.INSTANCE);
  44. @Rule
  45. public UserSessionRule userSession = UserSessionRule.standalone();
  46. @Rule
  47. public ComponentTextSearchFeatureRule features = new ComponentTextSearchFeatureRule();
  48. protected ComponentIndexer indexer = new ComponentIndexer(db.getDbClient(), es.client());
  49. protected ComponentIndex index = new ComponentIndex(es.client(), new WebAuthorizationTypeSupport(userSession), System2.INSTANCE);
  50. protected PermissionIndexerTester authorizationIndexerTester = new PermissionIndexerTester(es, indexer);
  51. protected void assertFileMatches(String query, String... fileNames) {
  52. ComponentDto[] files = Arrays.stream(fileNames)
  53. .map(this::indexFile)
  54. .toArray(ComponentDto[]::new);
  55. assertSearch(query).containsExactlyInAnyOrder(uuids(files));
  56. }
  57. protected void assertNoFileMatches(String query, String... fileNames) {
  58. Arrays.stream(fileNames)
  59. .forEach(this::indexFile);
  60. assertSearch(query).isEmpty();
  61. }
  62. protected void assertResultOrder(String query, String... resultsInOrder) {
  63. ComponentDto project = indexProject("key-1", "Quality Product");
  64. List<ComponentDto> files = Arrays.stream(resultsInOrder)
  65. .map(r -> ComponentTesting.newFileDto(project).setName(r))
  66. .peek(f -> f.setUuid(f.uuid() + "_" + f.name().replaceAll("[^a-zA-Z0-9]", "")))
  67. .toList();
  68. // index them, but not in the expected order
  69. files.stream()
  70. .sorted(Comparator.comparing(ComponentDto::uuid).reversed())
  71. .forEach(this::index);
  72. assertExactResults(query, files.toArray(new ComponentDto[0]));
  73. }
  74. protected ListAssert<String> assertSearch(String query) {
  75. return assertSearch(SuggestionQuery.builder().setQuery(query).setQualifiers(asList(PROJECT, FILE)).build());
  76. }
  77. protected ListAssert<String> assertSearch(SuggestionQuery query) {
  78. return (ListAssert<String>) assertThat(index.searchSuggestions(query, features.get()).getQualifiers())
  79. .flatExtracting(ComponentHitsPerQualifier::getHits)
  80. .extracting(ComponentHit::getUuid);
  81. }
  82. protected void assertSearchResults(String query, ComponentDto... expectedComponents) {
  83. assertSearchResults(SuggestionQuery.builder().setQuery(query).setQualifiers(asList(PROJECT, FILE)).build(), expectedComponents);
  84. }
  85. protected void assertSearchResults(SuggestionQuery query, ComponentDto... expectedComponents) {
  86. assertSearch(query).containsOnly(uuids(expectedComponents));
  87. }
  88. protected void assertExactResults(String query, ComponentDto... expectedComponents) {
  89. assertSearch(query).containsExactly(uuids(expectedComponents));
  90. }
  91. protected void assertNoSearchResults(String query) {
  92. assertSearchResults(query);
  93. }
  94. protected ComponentDto indexProject(String key, String name) {
  95. return index(
  96. ComponentTesting.newPrivateProjectDto("UUID_" + key)
  97. .setKey(key)
  98. .setName(name));
  99. }
  100. protected ComponentDto newProject(String key, String name) {
  101. return ComponentTesting.newPrivateProjectDto("UUID_" + key)
  102. .setKey(key)
  103. .setName(name);
  104. }
  105. protected ComponentDto indexFile(String fileName) {
  106. ComponentDto project = indexProject("key-1", "SonarQube");
  107. return indexFile(project, "src/main/java/" + fileName, fileName);
  108. }
  109. protected ComponentDto indexFile(ComponentDto project, String fileKey, String fileName) {
  110. return index(
  111. ComponentTesting.newFileDto(project)
  112. .setKey(fileKey)
  113. .setName(fileName));
  114. }
  115. protected ComponentDto index(ComponentDto dto) {
  116. indexer.index(dto);
  117. authorizationIndexerTester.allowOnlyAnyone(dto);
  118. return dto;
  119. }
  120. protected static String[] uuids(ComponentDto... expectedComponents) {
  121. return Arrays.stream(expectedComponents).map(ComponentDto::uuid).toArray(String[]::new);
  122. }
  123. }