Search in sources :

Example 1 with AggregateResult

use of org.molgenis.data.aggregation.AggregateResult 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 2 with AggregateResult

use of org.molgenis.data.aggregation.AggregateResult 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)

Example 3 with AggregateResult

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

the class AggregateAnonymizerImplTest method anonymize.

@Test
public void anonymize() {
    int threshold = 10;
    List<List<Long>> matrix = Lists.newArrayList();
    matrix.add(Arrays.asList(11L, 10L, 90L, 0L, 7L));
    matrix.add(Arrays.asList(null, 1L, 90L, 100L, 17L));
    matrix.add(Arrays.asList(11L, null, 90L, 5L, 10L));
    List<Object> xLabels = Arrays.asList("x1", "x2", "x3");
    List<Object> yLabels = Arrays.asList("y1", "y2", "y3");
    AnonymizedAggregateResult result = new AggregateAnonymizerImpl().anonymize(new AggregateResult(matrix, xLabels, yLabels), threshold);
    List<List<Long>> expectedMatrix = Lists.newArrayList();
    expectedMatrix.add(Arrays.asList(11L, AGGREGATE_ANONYMIZATION_VALUE, 90L, AGGREGATE_ANONYMIZATION_VALUE, AGGREGATE_ANONYMIZATION_VALUE));
    expectedMatrix.add(Arrays.asList(null, AGGREGATE_ANONYMIZATION_VALUE, 90L, 100L, 17L));
    expectedMatrix.add(Arrays.asList(11L, null, 90L, AGGREGATE_ANONYMIZATION_VALUE, AGGREGATE_ANONYMIZATION_VALUE));
    assertEquals(result, new AnonymizedAggregateResult(expectedMatrix, xLabels, yLabels, threshold));
}
Also used : AggregateResult(org.molgenis.data.aggregation.AggregateResult) List(java.util.List) Test(org.testng.annotations.Test)

Example 4 with AggregateResult

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

the class AttributeRepositorySecurityDecoratorTest method aggregateSuOrSystem.

private void aggregateSuOrSystem() {
    AggregateQuery aggregateQuery = mock(AggregateQuery.class);
    AggregateResult aggregateResult = mock(AggregateResult.class);
    when(delegateRepository.aggregate(aggregateQuery)).thenReturn(aggregateResult);
    assertEquals(repo.aggregate(aggregateQuery), aggregateResult);
}
Also used : AggregateResult(org.molgenis.data.aggregation.AggregateResult) AggregateQuery(org.molgenis.data.aggregation.AggregateQuery)

Example 5 with AggregateResult

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

the class AggregateAnonymizerRepositoryDecoratorTest method aggregateNoThreshold.

@Test
public void aggregateNoThreshold() throws Exception {
    when(appSettings.getAggregateThreshold()).thenReturn(null);
    AggregateQuery aggregateQuery = mock(AggregateQuery.class);
    AggregateResult aggregateResult = mock(AggregateResult.class);
    when(delegateRepository.aggregate(aggregateQuery)).thenReturn(aggregateResult);
    assertEquals(aggregateResult, aggregateAnonymizerRepoDecorator.aggregate(aggregateQuery));
    verifyZeroInteractions(aggregateAnonymizer);
    verifyZeroInteractions(aggregateResult);
}
Also used : AggregateResult(org.molgenis.data.aggregation.AggregateResult) AggregateQuery(org.molgenis.data.aggregation.AggregateQuery) Test(org.testng.annotations.Test)

Aggregations

AggregateResult (org.molgenis.data.aggregation.AggregateResult)19 AggregateQuery (org.molgenis.data.aggregation.AggregateQuery)13 Test (org.testng.annotations.Test)12 WithMockUser (org.springframework.security.test.context.support.WithMockUser)9 AggregateQueryImpl (org.molgenis.data.support.AggregateQueryImpl)7 QueryImpl (org.molgenis.data.support.QueryImpl)5 Attribute (org.molgenis.data.meta.model.Attribute)3 List (java.util.List)2 EntityType (org.molgenis.data.meta.model.EntityType)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 URISyntaxException (java.net.URISyntaxException)1 Entity (org.molgenis.data.Entity)1 UnknownIndexException (org.molgenis.data.index.exception.UnknownIndexException)1 EntityPager (org.molgenis.data.rest.EntityPager)1 AttributeFilterToFetchConverter.createDefaultAttributeFetch (org.molgenis.data.rest.v2.AttributeFilterToFetchConverter.createDefaultAttributeFetch)1 AggregateResultResponse (org.molgenis.data.rest.v2.EntityAggregatesResponse.AggregateResultResponse)1 ExplainedAttribute (org.molgenis.semanticsearch.explain.bean.ExplainedAttribute)1 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)1 ResponseEntity (org.springframework.http.ResponseEntity)1 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)1