/** * Execute the search request and return the document ids of results. */ private List<String> searchAndReturnKeys(IssueQuery.Builder query) { return Arrays.stream(underTest.search(query.build(), new SearchOptions()).getHits().getHits()) .map(SearchHit::getId) .collect(Collectors.toList()); }
@Test public void page_starts_at_one() { SearchOptions options = new SearchOptions().setPage(1, 10); assertThat(options.getLimit()).isEqualTo(10); assertThat(options.getOffset()).isEqualTo(0); assertThat(options.getPage()).isEqualTo(1); }
@Test public void page_must_be_strictly_positive() { try { new SearchOptions().setPage(0, 10); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage("Page must be greater or equal to 1 (got 0)"); } }
@Test public void max_limit() { SearchOptions options = new SearchOptions().setLimit(42); assertThat(options.getLimit()).isEqualTo(42); options.setLimit(SearchOptions.MAX_LIMIT + 10); assertThat(options.getLimit()).isEqualTo(SearchOptions.MAX_LIMIT); }
private void verifySearch(RuleQuery query, RuleDefinitionDto... expectedRules) { SearchIdResult<Integer> result = underTest.search(query, new SearchOptions()); assertThat(result.getTotal()).isEqualTo((long) expectedRules.length); assertThat(result.getIds()).hasSize(expectedRules.length); for (RuleDefinitionDto expectedRule : expectedRules) { assertThat(result.getIds()).contains(expectedRule.getId()); } }
private static SearchOptions buildSearchOptions(SearchRequest request) { SearchOptions context = loadCommonContext(request); SearchOptions searchOptions = new SearchOptions() .setLimit(context.getLimit()) .setOffset(context.getOffset()); if (context.getFacets().contains(RuleIndex.FACET_OLD_DEFAULT)) { searchOptions.addFacets(DEFAULT_FACETS); } else { searchOptions.addFacets(context.getFacets()); } return searchOptions; }
@Test public void searchUsers() { es.putDocuments(INDEX_TYPE_USER, newUser(USER1_LOGIN, asList("user_1", "u1")).setEmail("email1")); es.putDocuments(INDEX_TYPE_USER, newUser(USER2_LOGIN, Collections.emptyList()).setEmail("email2")); assertThat(underTest.search(userQuery.build(), new SearchOptions()).getDocs()).hasSize(2); assertThat(underTest.search(userQuery.setTextQuery("user").build(), new SearchOptions()).getDocs()).hasSize(2); assertThat(underTest.search(userQuery.setTextQuery("ser").build(), new SearchOptions()).getDocs()).hasSize(2); assertThat(underTest.search(userQuery.setTextQuery(USER1_LOGIN).build(), new SearchOptions()).getDocs()).hasSize(1); assertThat(underTest.search(userQuery.setTextQuery(USER2_LOGIN).build(), new SearchOptions()).getDocs()).hasSize(1); assertThat(underTest.search(userQuery.setTextQuery("mail").build(), new SearchOptions()).getDocs()).hasSize(2); assertThat(underTest.search(userQuery.setTextQuery("EMAIL1").build(), new SearchOptions()).getDocs()).hasSize(1); }
@Test public void languages_facet_should_return_top_100_items() { rangeClosed(1, 101).forEach(i -> db.rules().insert(r -> r.setLanguage("lang" + i))); index(); SearchIdResult result = underTest.search(new RuleQuery(), new SearchOptions().addFacets(singletonList(FACET_LANGUAGES))); assertThat(result.getFacets().get(FACET_LANGUAGES).size()).isEqualTo(100); }
@Test public void repositories_facet_should_return_top_10_items() { rangeClosed(1, 11).forEach(i -> db.rules().insert(r -> r.setRepositoryKey("repo" + i))); index(); SearchIdResult result = underTest.search(new RuleQuery(), new SearchOptions().addFacets(singletonList(FACET_REPOSITORIES))); assertThat(result.getFacets().get(FACET_REPOSITORIES).size()).isEqualTo(10); }
@Test public void paginate_results() { List<ComponentDto> projects = IntStream.range(0, 9) .mapToObj(i -> db.components().insertPrivateProject(p -> p.setName("project " + i))) .collect(Collectors.toList()); index(projects.toArray(new ComponentDto[0])); SearchIdResult<String> result = underTest.search(ComponentQuery.builder().build(), new SearchOptions().setPage(2, 3)); assertThat(result.getIds()).containsExactlyInAnyOrder(projects.get(3).uuid(), projects.get(4).uuid(), projects.get(5).uuid()); }
@Test public void search_all_rules() { createRule(); createRule(); index(); SearchIdResult results = underTest.search(new RuleQuery(), new SearchOptions()); assertThat(results.getTotal()).isEqualTo(2); assertThat(results.getIds()).hasSize(2); }
@Test public void search_users_filter_by_organization_uuid() { es.putDocuments(INDEX_TYPE_USER, newUser(USER1_LOGIN, asList("user_1", "u1")).setEmail("email1") .setOrganizationUuids(newArrayList("O1", "O2"))); es.putDocuments(INDEX_TYPE_USER, newUser(USER2_LOGIN, emptyList()).setEmail("email2") .setOrganizationUuids(newArrayList("O2"))); assertThat(underTest.search(userQuery.setOrganizationUuid("O42").build(), new SearchOptions()).getDocs()).isEmpty(); assertThat(underTest.search(userQuery.setOrganizationUuid("O2").build(), new SearchOptions()).getDocs()).extracting(UserDoc::login).containsOnly(USER1_LOGIN, USER2_LOGIN); assertThat(underTest.search(userQuery.setOrganizationUuid("O1").build(), new SearchOptions()).getDocs()).extracting(UserDoc::login).containsOnly(USER1_LOGIN); }
@Test public void writeJson() { SearchOptions options = new SearchOptions().setPage(3, 10); StringWriter json = new StringWriter(); JsonWriter jsonWriter = JsonWriter.of(json).beginObject(); options.writeJson(jsonWriter, 42L); jsonWriter.endObject().close(); JsonAssert.assertJson(json.toString()).isSimilarTo("{\"total\": 42, \"p\": 3, \"ps\": 10}"); }
@Test public void facet_tags_returns_more_than_10_tags_when_tags_filter_contains_value_not_in_top_10() { index( newDoc().setTags(newArrayList("finance1", "finance2", "finance3", "finance4", "finance5", "finance6", "finance7", "finance8", "finance9", "finance10")), newDoc().setTags(newArrayList("finance1", "finance2", "finance3", "finance4", "finance5", "finance6", "finance7", "finance8", "finance9", "finance10")), newDoc().setTags(newArrayList("solo", "solo2"))); Map<String, Long> result = underTest.search(new ProjectMeasuresQuery().setTags(ImmutableSet.of("solo", "solo2")), new SearchOptions().addFacets(FIELD_TAGS)).getFacets() .get(FIELD_TAGS); assertThat(result).hasSize(12).containsOnlyKeys("finance1", "finance2", "finance3", "finance4", "finance5", "finance6", "finance7", "finance8", "finance9", "finance10", "solo", "solo2"); }
@Test public void tags_facet_should_be_available_if_organization_is_specified() { OrganizationDto organization = db.organizations().insert(); RuleQuery query = new RuleQuery().setOrganization(organization); SearchOptions options = new SearchOptions().addFacets(singletonList(FACET_TAGS)); SearchIdResult result = underTest.search(query, options); assertThat(result.getFacets().get(FACET_TAGS)).isNotNull(); }
@Test public void facet_tags_returns_10_elements_by_default() { index( newDoc().setTags(newArrayList("finance1", "finance2", "finance3", "finance4", "finance5", "finance6", "finance7", "finance8", "finance9", "finance10")), newDoc().setTags(newArrayList("finance1", "finance2", "finance3", "finance4", "finance5", "finance6", "finance7", "finance8", "finance9", "finance10")), newDoc().setTags(newArrayList("solo"))); Map<String, Long> result = underTest.search(new ProjectMeasuresQuery(), new SearchOptions().addFacets(FIELD_TAGS)).getFacets().get(FIELD_TAGS); assertThat(result).hasSize(10).containsOnlyKeys("finance1", "finance2", "finance3", "finance4", "finance5", "finance6", "finance7", "finance8", "finance9", "finance10"); }
@Test public void filter_unauthorized_components() { ComponentDto unauthorizedProject = db.components().insertPrivateProject(); ComponentDto project1 = db.components().insertPrivateProject(); ComponentDto project2 = db.components().insertPrivateProject(); indexer.indexOnStartup(emptySet()); authorizationIndexerTester.allowOnlyAnyone(project1); authorizationIndexerTester.allowOnlyAnyone(project2); SearchIdResult<String> result = underTest.search(ComponentQuery.builder().build(), new SearchOptions()); assertThat(result.getIds()).containsExactlyInAnyOrder(project1.uuid(), project2.uuid()) .doesNotContain(unauthorizedProject.uuid()); }
@Test public void fail_if_created_before_equals_created_after() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Start bound cannot be larger or equal to end bound"); underTest.search(IssueQuery.builder().createdAfter(parseDate("2014-09-20")).createdBefore(parseDate("2014-09-20")).build(), new SearchOptions()); }
@Test public void filter_by_name() { ComponentDto ignoredProject = db.components().insertPrivateProject(p -> p.setName("ignored project")); ComponentDto project = db.components().insertPrivateProject(p -> p.setName("Project Shiny name")); index(ignoredProject, project); SearchIdResult<String> result = underTest.search(ComponentQuery.builder().setQuery("shiny").build(), new SearchOptions()); assertThat(result.getIds()).containsExactlyInAnyOrder(project.uuid()); }
@Test public void filter_by_language() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto javaFile = db.components().insertComponent(newFileDto(project).setLanguage("java")); ComponentDto jsFile1 = db.components().insertComponent(newFileDto(project).setLanguage("js")); ComponentDto jsFile2 = db.components().insertComponent(newFileDto(project).setLanguage("js")); index(project); SearchIdResult<String> result = underTest.search(ComponentQuery.builder().setLanguage("js").build(), new SearchOptions()); assertThat(result.getIds()).containsExactlyInAnyOrder(jsFile1.uuid(), jsFile2.uuid()); }