RepositorySearchContribution extends DefaultSearchContribution { @Override public void contribute(final BoolQueryBuilder query, final String type, final String value) { if (value == null) { return; } Repository repository = repositoryManager.get(value); if (repository != null) { Optional<GroupFacet> groupFacet = repository.optionalFacet(GroupFacet.class); if (groupFacet.isPresent()) { List<Repository> members = groupFacet.get().leafMembers(); query.must(QueryBuilders.termsQuery(type, members.stream().map(Repository::getName).toArray(String[]::new))); return; } } super.contribute(query, type, value); } @Inject  RepositorySearchContribution(final RepositoryManager repositoryManager); @Override void contribute(final BoolQueryBuilder query, final String type, final String value); static final String NAME; }
@Test public void testContribute() { mockRepo("repo"); BoolQueryBuilder query = QueryBuilders.boolQuery(); underTest.contribute(query, REPOSITORY_NAME, "repo"); assertThat(query.toString(), Matchers.containsString("\"query\" : \"repo\"")); assertThat(query.toString(), Matchers.containsString("\"fields\" : [ \"repository_name\" ]")); }
@Test public void testContribute_noRepoMatch() { when(repositoryManager.get("repo")).thenReturn(null); BoolQueryBuilder query = QueryBuilders.boolQuery(); underTest.contribute(query, REPOSITORY_NAME, "repo"); assertThat(query.toString(), Matchers.containsString("\"query\" : \"repo\"")); assertThat(query.toString(), Matchers.containsString("\"fields\" : [ \"repository_name\" ]")); }
@Test public void testContribute_withGroup() { Repository repository = mockRepo("repo"); Repository repository2 = mockRepo("repo2"); mockRepo("group", repository, repository2); BoolQueryBuilder query = QueryBuilders.boolQuery(); underTest.contribute(query, REPOSITORY_NAME, "group"); assertThat(query.toString(), Matchers.containsString("\"repository_name\" : [ \"repo\", \"repo2\" ]")); }