private void assertThatIndexHasSize(long expectedSize) { assertThat(es.countDocuments(INDEX_TYPE_ISSUE)).isEqualTo(expectedSize); }
public List<String> getIds(IndexType indexType) { return getDocuments(indexType).stream().map(SearchHit::getId).collect(Collectors.toList()); }
/** * New instance which contains the specified indices. Note that * core indices may exist. */ public static EsTester createCustom(IndexDefinition... definitions) { createIndices(definitions); return new EsTester(true); }
@Test public void clear_indexes() { es.putDocuments(IssueIndexDefinition.INDEX_TYPE_ISSUE, IssueDocTesting.newDoc()); es.putDocuments(RuleIndexDefinition.INDEX_TYPE_RULE, newRuleDoc()); es.putDocuments(ComponentIndexDefinition.INDEX_TYPE_COMPONENT, newComponentDoc()); underTest.clearIndexes(); assertThat(es.countDocuments(IssueIndexDefinition.INDEX_TYPE_ISSUE)).isEqualTo(0); assertThat(es.countDocuments(ComponentIndexDefinition.INDEX_TYPE_COMPONENT)).isEqualTo(0); }
@Test public void deleteByKeys_does_not_recover_from_errors() { addIssueToIndex("P1", "Issue1"); es.lockWrites(INDEX_TYPE_ISSUE); try { // FIXME : test also message expectedException.expect(IllegalStateException.class); underTest.deleteByKeys("P1", asList("Issue1")); } finally { assertThatIndexHasOnly("Issue1"); assertThatEsQueueTableHasSize(0); es.unlockWrites(INDEX_TYPE_ISSUE); } }
@Test public void errors_during_indexing_are_recovered() { ComponentDto project = db.components().insertPrivateProject(); es.lockWrites(INDEX_TYPE_PROJECT_MEASURES); IndexingResult result = indexProject(project, PROJECT_CREATION); assertThat(result.getTotal()).isEqualTo(1L); assertThat(result.getFailures()).isEqualTo(1L); // index is still read-only, fail to recover result = recover(); assertThat(result.getTotal()).isEqualTo(1L); assertThat(result.getFailures()).isEqualTo(1L); assertThat(es.countDocuments(INDEX_TYPE_PROJECT_MEASURES)).isEqualTo(0); assertThatEsQueueTableHasSize(1); es.unlockWrites(INDEX_TYPE_PROJECT_MEASURES); result = recover(); assertThat(result.getTotal()).isEqualTo(1L); assertThat(result.getFailures()).isEqualTo(0L); assertThatEsQueueTableHasSize(0); assertThatIndexContainsOnly(project); }
@Test public void getAtMostThreeActiveUsersForScmAccount_is_case_sensitive_for_login() { UserDoc user = newUser("the_login", singletonList("John.Smith")); es.putDocuments(INDEX_TYPE_USER, user); assertThat(underTest.getAtMostThreeActiveUsersForScmAccount("the_login", ORGANIZATION_UUID)).hasSize(1); assertThat(underTest.getAtMostThreeActiveUsersForScmAccount("the_Login", ORGANIZATION_UUID)).isEmpty(); }
/** * This is a technical constraint, to ensure, that the indexers can be called in any order, during startup. */ @Test public void parent_child_relationship_does_not_require_ordering_of_index_requests() { IssueDoc issueDoc = new IssueDoc(); issueDoc.setKey("key"); issueDoc.setProjectUuid("parent-does-not-exist"); new IssueIndexer(es.client(), db.getDbClient(), new IssueIteratorFactory(db.getDbClient())) .index(asList(issueDoc).iterator()); assertThat(es.countDocuments(INDEX_TYPE_ISSUE)).isEqualTo(1L); }
private void assertThatIndexContainsOnly(ComponentDto... expectedComponents) { assertThat(es.getIds(INDEX_TYPE_COMPONENT)).containsExactlyInAnyOrder( Arrays.stream(expectedComponents).map(ComponentDto::uuid).toArray(String[]::new)); }
@Test public void delete_should_delete_the_view() { ViewDoc view1 = new ViewDoc().setUuid("UUID1").setProjects(asList("P1")); ViewDoc view2 = new ViewDoc().setUuid("UUID2").setProjects(asList("P2", "P3", "P4")); ViewDoc view3 = new ViewDoc().setUuid("UUID3").setProjects(asList("P2", "P3", "P4")); es.putDocuments(INDEX_TYPE_VIEW, view1); es.putDocuments(INDEX_TYPE_VIEW, view2); es.putDocuments(INDEX_TYPE_VIEW, view3); assertThat(es.getDocumentFieldValues(INDEX_TYPE_VIEW, ViewIndexDefinition.FIELD_UUID)) .containsOnly(view1.uuid(), view2.uuid(), view3.uuid()); underTest.delete(dbSession, asList(view1.uuid(), view2.uuid())); assertThat(es.getDocumentFieldValues(INDEX_TYPE_VIEW, ViewIndexDefinition.FIELD_UUID)) .containsOnly(view3.uuid()); }
@Test public void indexOnStartup_does_not_fail_on_errors_and_does_enable_recovery_mode() { es.lockWrites(INDEX_TYPE_ISSUE); db.issues().insertIssue(organization); try { // FIXME : test also message expectedException.expect(IllegalStateException.class); underTest.indexOnStartup(emptySet()); } finally { assertThatIndexHasSize(0); assertThatEsQueueTableHasSize(0); es.unlockWrites(INDEX_TYPE_ISSUE); } }
@Test public void errors_during_indexing_are_recovered() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); es.lockWrites(INDEX_TYPE_COMPONENT); IndexingResult result = indexProject(project, PROJECT_CREATION); assertThat(result.getTotal()).isEqualTo(2L); assertThat(result.getFailures()).isEqualTo(2L); // index is still read-only, fail to recover result = recover(); assertThat(result.getTotal()).isEqualTo(2L); assertThat(result.getFailures()).isEqualTo(2L); assertThat(es.countDocuments(INDEX_TYPE_COMPONENT)).isEqualTo(0); es.unlockWrites(INDEX_TYPE_COMPONENT); result = recover(); assertThat(result.getTotal()).isEqualTo(2L); assertThat(result.getFailures()).isEqualTo(0L); assertThatIndexContainsOnly(project, file); }
@Test public void getAtMostThreeActiveUsersForScmAccount_is_case_insensitive_for_email() { UserDoc user = newUser("the_login", "the_EMAIL@corp.com", singletonList("John.Smith")); es.putDocuments(INDEX_TYPE_USER, user); assertThat(underTest.getAtMostThreeActiveUsersForScmAccount("the_EMAIL@corp.com", ORGANIZATION_UUID)).hasSize(1); assertThat(underTest.getAtMostThreeActiveUsersForScmAccount("the_email@corp.com", ORGANIZATION_UUID)).hasSize(1); assertThat(underTest.getAtMostThreeActiveUsersForScmAccount("email", ORGANIZATION_UUID)).isEmpty(); }
private void assertThatIndexContainsOnly(SnapshotDto... expectedProjects) { assertThat(es.getIds(INDEX_TYPE_PROJECT_MEASURES)).containsExactlyInAnyOrder( Arrays.stream(expectedProjects).map(SnapshotDto::getComponentUuid).toArray(String[]::new)); }
/** * New instance which contains the core indices (rules, issues, ...). */ public static EsTester create() { if (!CORE_INDICES_CREATED.get()) { Configuration config = new MapSettings().asConfig(); List<IndexDefinition.Index> createdIndices = createIndices( new ComponentIndexDefinition(config), IssueIndexDefinition.createForTest(), new ProjectMeasuresIndexDefinition(config), RuleIndexDefinition.createForTest(), new TestIndexDefinition(config), new UserIndexDefinition(config), new ViewIndexDefinition(config)); CORE_INDICES_CREATED.set(true); createdIndices.stream().map(IndexDefinition.Index::getName).forEach(CORE_INDICES_NAMES::add); } return new EsTester(false); }
/** * Get a list of a specific field from all indexed documents. */ public <T> List<T> getDocumentFieldValues(IndexType indexType, final String fieldNameToReturn) { return getDocuments(indexType) .stream() .map(input -> (T) input.getSourceAsMap().get(fieldNameToReturn)) .collect(Collectors.toList()); }
private void assertThatAuthIndexHasSize(int expectedSize) { IndexType authIndexType = underTest.getIndexTypes().iterator().next(); assertThat(es.countDocuments(authIndexType)).isEqualTo(expectedSize); }
@Test public void errors_during_project_deletion_are_recovered() { addIssueToIndex("P1", "I1"); assertThatIndexHasSize(1); es.lockWrites(INDEX_TYPE_ISSUE); IndexingResult result = indexProject("P1", ProjectIndexer.Cause.PROJECT_DELETION); assertThat(result.getTotal()).isEqualTo(1L); assertThat(result.getFailures()).isEqualTo(1L); // index is still read-only, fail to recover result = recover(); assertThat(result.getTotal()).isEqualTo(1L); assertThat(result.getFailures()).isEqualTo(1L); assertThatIndexHasSize(1); es.unlockWrites(INDEX_TYPE_ISSUE); result = recover(); assertThat(result.getTotal()).isEqualTo(1L); assertThat(result.getFailures()).isEqualTo(0L); assertThatIndexHasSize(0); }