Search in sources :

Example 1 with ElasticsearchUtils

use of org.apache.syncope.ext.elasticsearch.client.ElasticsearchUtils in project syncope by apache.

the class ElasticsearchAnySearchDAOTest method searchRequest_groupOwner.

@Test
public void searchRequest_groupOwner() throws IOException {
    // 1. mock
    AnyUtils anyUtils = mock(AnyUtils.class);
    when(anyUtils.getField("id")).thenReturn(ReflectionUtils.findField(JPAUser.class, "id"));
    when(anyUtils.newPlainAttrValue()).thenReturn(new JPAUPlainAttrValue());
    when(anyUtilsFactory.getInstance(AnyTypeKind.USER)).thenReturn(anyUtils);
    when(entityFactory.newEntity(PlainSchema.class)).thenReturn(new JPAPlainSchema());
    when(groupDAO.findKey("groupKey")).thenReturn("groupKey");
    try (MockedStatic<ElasticsearchUtils> utils = Mockito.mockStatic(ElasticsearchUtils.class)) {
        utils.when(() -> ElasticsearchUtils.getContextDomainName(SyncopeConstants.MASTER_DOMAIN, AnyTypeKind.USER)).thenReturn("master_user");
        // 2. test
        Set<String> adminRealms = Set.of(RealmUtils.getGroupOwnerRealm("/any", "groupKey"));
        AnyCond anyCond = new AnyCond(AttrCond.Type.ISNOTNULL);
        anyCond.setSchema("id");
        SearchRequest request = new SearchRequest.Builder().index(ElasticsearchUtils.getContextDomainName(AuthContextUtils.getDomain(), AnyTypeKind.USER)).searchType(SearchType.QueryThenFetch).query(searchDAO.getQuery(adminRealms, SearchCond.getLeaf(anyCond), AnyTypeKind.USER)).from(1).size(10).build();
        assertThat(new Query.Builder().bool(QueryBuilders.bool().must(new Query.Builder().exists(QueryBuilders.exists().field("id").build()).build()).must(new Query.Builder().term(QueryBuilders.term().field("memberships").value(FieldValue.of("groupKey")).build()).build()).build()).build()).usingRecursiveComparison().isEqualTo(request.query());
    }
}
Also used : SearchRequest(co.elastic.clients.elasticsearch.core.SearchRequest) JPAUPlainAttrValue(org.apache.syncope.core.persistence.jpa.entity.user.JPAUPlainAttrValue) ElasticsearchUtils(org.apache.syncope.ext.elasticsearch.client.ElasticsearchUtils) Query(co.elastic.clients.elasticsearch._types.query_dsl.Query) JPAPlainSchema(org.apache.syncope.core.persistence.jpa.entity.JPAPlainSchema) JPAUser(org.apache.syncope.core.persistence.jpa.entity.user.JPAUser) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils) AnyCond(org.apache.syncope.core.persistence.api.dao.search.AnyCond) Test(org.junit.jupiter.api.Test)

Aggregations

Query (co.elastic.clients.elasticsearch._types.query_dsl.Query)1 SearchRequest (co.elastic.clients.elasticsearch.core.SearchRequest)1 AnyCond (org.apache.syncope.core.persistence.api.dao.search.AnyCond)1 AnyUtils (org.apache.syncope.core.persistence.api.entity.AnyUtils)1 JPAPlainSchema (org.apache.syncope.core.persistence.jpa.entity.JPAPlainSchema)1 JPAUPlainAttrValue (org.apache.syncope.core.persistence.jpa.entity.user.JPAUPlainAttrValue)1 JPAUser (org.apache.syncope.core.persistence.jpa.entity.user.JPAUser)1 ElasticsearchUtils (org.apache.syncope.ext.elasticsearch.client.ElasticsearchUtils)1 Test (org.junit.jupiter.api.Test)1