Search in sources :

Example 1 with AggregateQuery

use of org.molgenis.data.aggregation.AggregateQuery in project molgenis by molgenis.

the class TransactionalRepositoryDecoratorTest method aggregate.

@Test
public void aggregate() throws Exception {
    AggregateQuery aggregateQuery = mock(AggregateQuery.class);
    transactionalRepo.aggregate(aggregateQuery);
    verify(transactionManager).getTransaction(any(TransactionDefinition.class));
    verify(delegateRepository).aggregate(aggregateQuery);
}
Also used : TransactionDefinition(org.springframework.transaction.TransactionDefinition) AggregateQuery(org.molgenis.data.aggregation.AggregateQuery) Test(org.testng.annotations.Test)

Example 2 with AggregateQuery

use of org.molgenis.data.aggregation.AggregateQuery in project molgenis by molgenis.

the class EntityTypeRepositorySecurityDecoratorTest method aggregateUser.

@WithMockUser(username = USERNAME)
@Test(expectedExceptions = UnsupportedOperationException.class)
public void aggregateUser() {
    AggregateQuery aggregateQuery = mock(AggregateQuery.class);
    repo.aggregate(aggregateQuery);
}
Also used : AggregateQuery(org.molgenis.data.aggregation.AggregateQuery) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.testng.annotations.Test)

Example 3 with AggregateQuery

use of org.molgenis.data.aggregation.AggregateQuery in project molgenis by molgenis.

the class RowLevelSecurityRepositoryDecoratorTest method testAggregate.

@WithMockUser(username = USERNAME, roles = "SU")
@Test
public void testAggregate() {
    AggregateQuery aggregateQuery = mock(AggregateQuery.class);
    AggregateResult aggregateResponse = mock(AggregateResult.class);
    when(delegateRepository.aggregate(aggregateQuery)).thenReturn(aggregateResponse);
    assertEquals(rowLevelSecurityRepositoryDecorator.aggregate(aggregateQuery), aggregateResponse);
}
Also used : AggregateResult(org.molgenis.data.aggregation.AggregateResult) AggregateQuery(org.molgenis.data.aggregation.AggregateQuery) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.testng.annotations.Test)

Example 4 with AggregateQuery

use of org.molgenis.data.aggregation.AggregateQuery in project molgenis by molgenis.

the class RowLevelSecurityRepositoryDecoratorTest method testAggregatePermissionDenied.

@WithMockUser(username = USERNAME)
@Test(expectedExceptions = UnsupportedOperationException.class)
public void testAggregatePermissionDenied() {
    AggregateQuery aggregateQuery = mock(AggregateQuery.class);
    rowLevelSecurityRepositoryDecorator.aggregate(aggregateQuery);
}
Also used : AggregateQuery(org.molgenis.data.aggregation.AggregateQuery) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.testng.annotations.Test)

Example 5 with AggregateQuery

use of org.molgenis.data.aggregation.AggregateQuery in project molgenis by molgenis.

the class RestControllerV2 method createEntityCollectionResponse.

private EntityCollectionResponseV2 createEntityCollectionResponse(String entityTypeId, EntityCollectionRequestV2 request, HttpServletRequest httpRequest, boolean includeCategories) {
    EntityType meta = dataService.getEntityType(entityTypeId);
    Query<Entity> q = request.getQ() != null ? request.getQ().createQuery(meta) : new QueryImpl<>();
    q.pageSize(request.getNum()).offset(request.getStart()).sort(request.getSort());
    Fetch fetch = AttributeFilterToFetchConverter.convert(request.getAttrs(), meta, LocaleContextHolder.getLocale().getLanguage());
    if (fetch != null) {
        q.fetch(fetch);
    }
    if (request.getAggs() != null) {
        // return aggregates for aggregate query
        AggregateQuery aggsQ = request.getAggs().createAggregateQuery(meta, q);
        Attribute xAttr = aggsQ.getAttributeX();
        Attribute yAttr = aggsQ.getAttributeY();
        if (xAttr == null && yAttr == null) {
            throw new MolgenisQueryException("Aggregate query is missing 'x' or 'y' attribute");
        }
        AggregateResult aggs = dataService.aggregate(entityTypeId, aggsQ);
        AttributeResponseV2 xAttrResponse = xAttr != null ? new AttributeResponseV2(entityTypeId, meta, xAttr, fetch, permissionService, dataService) : null;
        AttributeResponseV2 yAttrResponse = yAttr != null ? new AttributeResponseV2(entityTypeId, meta, yAttr, fetch, permissionService, dataService) : null;
        return new EntityAggregatesResponse(aggs, xAttrResponse, yAttrResponse, BASE_URI + '/' + entityTypeId);
    } else {
        Long count = dataService.count(entityTypeId, new QueryImpl<>(q).setOffset(0).setPageSize(0));
        Iterable<Entity> it;
        if (count > 0 && q.getPageSize() > 0) {
            it = () -> dataService.findAll(entityTypeId, q).iterator();
        } else {
            it = Collections.emptyList();
        }
        EntityPager pager = new EntityPager(request.getStart(), request.getNum(), count, it);
        List<Map<String, Object>> entities = new ArrayList<>();
        for (Entity entity : it) {
            Map<String, Object> responseData = new LinkedHashMap<>();
            createEntityValuesResponse(entity, fetch, responseData);
            entities.add(responseData);
        }
        UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(getFullURL(httpRequest));
        String prevHref = null;
        if (pager.getPrevStart() != null) {
            builder.replaceQueryParam("start", pager.getPrevStart());
            prevHref = builder.build(false).toUriString();
        }
        String nextHref = null;
        if (pager.getNextStart() != null) {
            builder.replaceQueryParam("start", pager.getNextStart());
            nextHref = builder.build(false).toUriString();
        }
        return new EntityCollectionResponseV2(pager, entities, fetch, BASE_URI + '/' + entityTypeId, meta, permissionService, dataService, prevHref, nextHref, includeCategories);
    }
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute) AggregateQuery(org.molgenis.data.aggregation.AggregateQuery) EntityType(org.molgenis.data.meta.model.EntityType) AttributeFilterToFetchConverter.createDefaultAttributeFetch(org.molgenis.data.rest.v2.AttributeFilterToFetchConverter.createDefaultAttributeFetch) EntityPager(org.molgenis.data.rest.EntityPager) AggregateResult(org.molgenis.data.aggregation.AggregateResult) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder)

Aggregations

AggregateQuery (org.molgenis.data.aggregation.AggregateQuery)23 Test (org.testng.annotations.Test)19 AggregateResult (org.molgenis.data.aggregation.AggregateResult)13 WithMockUser (org.springframework.security.test.context.support.WithMockUser)10 AggregateQueryImpl (org.molgenis.data.support.AggregateQueryImpl)8 QueryImpl (org.molgenis.data.support.QueryImpl)4 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)3 UnknownIndexException (org.molgenis.data.index.exception.UnknownIndexException)2 Attribute (org.molgenis.data.meta.model.Attribute)2 EntityType (org.molgenis.data.meta.model.EntityType)1 EntityPager (org.molgenis.data.rest.EntityPager)1 AttributeFilterToFetchConverter.createDefaultAttributeFetch (org.molgenis.data.rest.v2.AttributeFilterToFetchConverter.createDefaultAttributeFetch)1 TransactionDefinition (org.springframework.transaction.TransactionDefinition)1 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)1