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);
}
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);
}
}
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));
}
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);
}
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);
}
Aggregations