public List<String> getIds(IndexType indexType) { return getDocuments(indexType).stream().map(SearchHit::getId).collect(Collectors.toList()); }
/** * 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 assertThatIndexHasOnly(IssueDto... expectedIssues) { assertThat(es.getDocuments(INDEX_TYPE_ISSUE)) .extracting(SearchHit::getId) .containsExactlyInAnyOrder(Arrays.stream(expectedIssues).map(IssueDto::getKey).toArray(String[]::new)); }
/** * Get all the indexed documents (no paginated results). Results are converted to BaseDoc objects. * Results are not sorted. */ public <E extends BaseDoc> List<E> getDocuments(IndexType indexType, final Class<E> docClass) { List<SearchHit> hits = getDocuments(indexType); return new ArrayList<>(Collections2.transform(hits, input -> { try { return (E) ConstructorUtils.invokeConstructor(docClass, input.getSource()); } catch (Exception e) { throw Throwables.propagate(e); } })); }
private void assertThatIndexHasOnly(String... expectedKeys) { List<IssueDoc> issues = es.getDocuments(INDEX_TYPE_ISSUE, IssueDoc.class); assertThat(issues).extracting(IssueDoc::key).containsOnly(expectedKeys); }
@Test public void index_on_startup() { db.prepareDbUnit(getClass(), "index.xml"); underTest.indexOnStartup(emptySet()); List<ViewDoc> docs = es.getDocuments(ViewIndexDefinition.INDEX_TYPE_VIEW, ViewDoc.class); assertThat(docs).hasSize(4); Map<String, ViewDoc> viewsByUuid = Maps.uniqueIndex(docs, ViewDoc::uuid); assertThat(viewsByUuid.get("ABCD").projects()).containsOnly("JKLM"); assertThat(viewsByUuid.get("EFGH").projects()).containsOnly("KLMN", "JKLM"); assertThat(viewsByUuid.get("FGHI").projects()).containsOnly("JKLM"); assertThat(viewsByUuid.get("IJKL").projects()).isEmpty(); }
@Test public void index_root_view() { db.prepareDbUnit(getClass(), "index.xml"); underTest.index("EFGH"); List<ViewDoc> docs = es.getDocuments(ViewIndexDefinition.INDEX_TYPE_VIEW, ViewDoc.class); assertThat(docs).hasSize(2); Map<String, ViewDoc> viewsByUuid = Maps.uniqueIndex(docs, ViewDoc::uuid); assertThat(viewsByUuid.get("EFGH").projects()).containsOnly("KLMN", "JKLM"); assertThat(viewsByUuid.get("FGHI").projects()).containsOnly("JKLM"); }
@Test public void index_view_doc() { underTest.index(new ViewDoc().setUuid("EFGH").setProjects(newArrayList("KLMN", "JKLM"))); List<ViewDoc> result = es.getDocuments(ViewIndexDefinition.INDEX_TYPE_VIEW, ViewDoc.class); assertThat(result).hasSize(1); ViewDoc view = result.get(0); assertThat(view.uuid()).isEqualTo("EFGH"); assertThat(view.projects()).containsOnly("KLMN", "JKLM"); }
@Test public void indexOnStartup_indexes_all_data() { ActiveRuleDto activeRule = db.qualityProfiles().activateRule(profile1, rule1); underTest.indexOnStartup(emptySet()); List<ActiveRuleDoc> docs = es.getDocuments(INDEX_TYPE_ACTIVE_RULE, ActiveRuleDoc.class); assertThat(docs).hasSize(1); verify(docs.get(0), profile1, activeRule); assertThatEsQueueTableIsEmpty(); }
@Test public void indexOnStartup_adds_all_users_to_index() { UserDto user = db.users().insertUser(u -> u.setScmAccounts(asList("user_1", "u1"))); underTest.indexOnStartup(new HashSet<>()); List<UserDoc> docs = es.getDocuments(UserIndexDefinition.INDEX_TYPE_USER, UserDoc.class); assertThat(docs).hasSize(1); UserDoc doc = docs.get(0); assertThat(doc.uuid()).isEqualTo(user.getUuid()); assertThat(doc.login()).isEqualTo(user.getLogin()); assertThat(doc.name()).isEqualTo(user.getName()); assertThat(doc.email()).isEqualTo(user.getEmail()); assertThat(doc.active()).isEqualTo(user.isActive()); assertThat(doc.scmAccounts()).isEqualTo(user.getScmAccountsAsList()); assertThat(doc.organizationUuids()).isEmpty(); }
@Test public void map_fields() { OrganizationDto organization = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organization, p -> p.setLanguage("java")); underTest.indexOnStartup(emptySet()); assertThatIndexContainsOnly(project); ComponentDoc doc = es.getDocuments(INDEX_TYPE_COMPONENT, ComponentDoc.class).get(0); assertThat(doc.getId()).isEqualTo(project.uuid()); assertThat(doc.getKey()).isEqualTo(project.getDbKey()); assertThat(doc.getProjectUuid()).isEqualTo(project.projectUuid()); assertThat(doc.getName()).isEqualTo(project.name()); assertThat(doc.getLanguage()).isEqualTo(project.language()); assertThat(doc.getOrganization()).isEqualTo(project.getOrganizationUuid()); }
@Test public void index_application_on_startup() { ComponentDto application = db.components().insertApplication(db.getDefaultOrganization()); ComponentDto project = db.components().insertPrivateProject(); db.components().insertComponent(newProjectCopy("PC1", project, application)); underTest.indexOnStartup(emptySet()); List<ViewDoc> result = es.getDocuments(ViewIndexDefinition.INDEX_TYPE_VIEW, ViewDoc.class); assertThat(result).hasSize(1); ViewDoc resultApp = result.get(0); assertThat(resultApp.uuid()).isEqualTo(application.uuid()); assertThat(resultApp.projects()).containsExactlyInAnyOrder(project.uuid()); }
@Test public void update_index_when_updating_user_login() { UserDto oldUser = db.users().insertUser(); createDefaultGroup(); userIndexer.indexOnStartup(null); underTest.updateAndCommit(session, oldUser, new UpdateUser() .setLogin("new_login"), u -> { }); List<SearchHit> indexUsers = es.getDocuments(UserIndexDefinition.INDEX_TYPE_USER); assertThat(indexUsers).hasSize(1); assertThat(indexUsers.get(0).getSource()) .contains(entry("login", "new_login")); }
@Test public void index_application() { ComponentDto application = db.components().insertApplication(db.getDefaultOrganization()); ComponentDto project = db.components().insertPrivateProject(); db.components().insertComponent(newProjectCopy("PC1", project, application)); underTest.index(application.uuid()); List<ViewDoc> result = es.getDocuments(ViewIndexDefinition.INDEX_TYPE_VIEW, ViewDoc.class); assertThat(result).hasSize(1); ViewDoc resultApp = result.get(0); assertThat(resultApp.uuid()).isEqualTo(application.uuid()); assertThat(resultApp.projects()).containsExactlyInAnyOrder(project.uuid()); }
@Test public void indexOnStartup_adds_all_users_with_organizations() { OrganizationDto organization1 = db.organizations().insert(); OrganizationDto organization2 = db.organizations().insert(); UserDto user = db.users().insertUser(); db.organizations().addMember(organization1, user); db.organizations().addMember(organization2, user); underTest.indexOnStartup(new HashSet<>()); List<UserDoc> docs = es.getDocuments(UserIndexDefinition.INDEX_TYPE_USER, UserDoc.class); assertThat(docs).hasSize(1); UserDoc doc = docs.get(0); assertThat(doc.uuid()).isEqualTo(user.getUuid()); assertThat(doc.login()).isEqualTo(user.getLogin()); assertThat(doc.organizationUuids()).containsExactlyInAnyOrder(organization1.getUuid(), organization2.getUuid()); }
@Test public void index_issue_in_non_main_branch() { RuleDefinitionDto rule = db.rules().insert(); ComponentDto project = db.components().insertPrivateProject(organization); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("feature/foo")); ComponentDto dir = db.components().insertComponent(ComponentTesting.newDirectory(branch, "src/main/java/foo")); ComponentDto file = db.components().insertComponent(newFileDto(branch, dir, "F1")); IssueDto issue = db.issues().insertIssue(IssueTesting.newIssue(rule, branch, file)); underTest.indexOnStartup(emptySet()); IssueDoc doc = es.getDocuments(INDEX_TYPE_ISSUE, IssueDoc.class).get(0); assertThat(doc.getId()).isEqualTo(issue.getKey()); assertThat(doc.organizationUuid()).isEqualTo(organization.getUuid()); assertThat(doc.componentUuid()).isEqualTo(file.uuid()); assertThat(doc.projectUuid()).isEqualTo(branch.getMainBranchProjectUuid()); assertThat(doc.branchUuid()).isEqualTo(branch.uuid()); assertThat(doc.isMainBranch()).isFalse(); }
@Test public void verify_security_standards_indexation() { RuleDefinitionDto rule = db.rules().insert(r -> r.setSecurityStandards(new HashSet<>(Arrays.asList("cwe:123", "owaspTop10:a3", "cwe:863")))); ComponentDto project = db.components().insertPrivateProject(organization); ComponentDto dir = db.components().insertComponent(ComponentTesting.newDirectory(project, "src/main/java/foo")); ComponentDto file = db.components().insertComponent(newFileDto(project, dir, "F1")); IssueDto issue = db.issues().insertIssue(IssueTesting.newIssue(rule, project, file)); underTest.indexOnStartup(emptySet()); IssueDoc doc = es.getDocuments(INDEX_TYPE_ISSUE, IssueDoc.class).get(0); assertThat(doc.getCwe()).containsExactlyInAnyOrder("123", "863"); assertThat(doc.getOwaspTop10()).containsExactlyInAnyOrder("a3"); assertThat(doc.getSansTop25()).containsExactlyInAnyOrder(SANS_TOP_25_POROUS_DEFENSES); }
@Test public void commitAndIndex_single_user() { UserDto user = db.users().insertUser(); UserDto anotherUser = db.users().insertUser(); underTest.commitAndIndex(db.getSession(), user); List<UserDoc> docs = es.getDocuments(UserIndexDefinition.INDEX_TYPE_USER, UserDoc.class); assertThat(docs) .extracting(UserDoc::uuid) .containsExactlyInAnyOrder(user.getUuid()) .doesNotContain(anotherUser.getUuid()); }
@Test public void indexOnAnalysis_does_not_delete_orphan_docs() { RuleDefinitionDto rule = db.rules().insert(); ComponentDto project = db.components().insertPrivateProject(organization); ComponentDto file = db.components().insertComponent(newFileDto(project)); IssueDto issue = db.issues().insertIssue(IssueTesting.newIssue(rule, project, file)); // orphan in the project addIssueToIndex(project.uuid(), "orphan"); underTest.indexOnAnalysis(project.uuid()); assertThat(es.getDocuments(INDEX_TYPE_ISSUE)) .extracting(SearchHit::getId) .containsExactlyInAnyOrder(issue.getKey(), "orphan"); }
@Test public void commitAndIndex_single_user_belonging_to_organizations() { OrganizationDto organization1 = db.organizations().insert(); OrganizationDto organization2 = db.organizations().insert(); UserDto user = db.users().insertUser(); db.organizations().addMember(organization1, user); db.organizations().addMember(organization2, user); UserDto anotherUser = db.users().insertUser(); db.organizations().addMember(organization1, anotherUser); underTest.commitAndIndex(db.getSession(), user); List<UserDoc> docs = es.getDocuments(UserIndexDefinition.INDEX_TYPE_USER, UserDoc.class); assertThat(docs) .extracting(UserDoc::uuid, UserDoc::organizationUuids) .containsExactlyInAnyOrder(tuple(user.getUuid(), asList(organization1.getUuid(), organization2.getUuid()))); }