use of org.molgenis.dataexplorer.controller.DataRequest in project molgenis by molgenis.
the class DataExplorerDownloadHandlerTest method testWriteToExcelTooManyCells.
@Test(expectedExceptions = MolgenisDataException.class, expectedExceptionsMessageRegExp = "Total number of cells for this download exceeds the maximum of 500000 for .xlsx downloads, please use .csv instead")
public void testWriteToExcelTooManyCells() throws Exception {
when(dataService.count("sys_set_thousandgenomes", query)).thenReturn(2500001L);
when(dataService.getEntityType("sys_set_thousandgenomes")).thenReturn(entityType);
when(entityType.getAtomicAttributes()).thenReturn(asList(attribute1, attribute2, attribute3));
when(attribute1.getName()).thenReturn("attr1");
when(attribute2.getName()).thenReturn("attr2");
when(attribute3.getName()).thenReturn("attr3");
DataRequest dataRequest = new DataRequest();
dataRequest.setEntityName("sys_set_thousandgenomes");
dataRequest.setQuery(query);
dataRequest.setAttributeNames(asList("attr1", "attr2"));
dataRequest.setColNames(DataRequest.ColNames.ATTRIBUTE_NAMES);
dataRequest.setEntityValues(DataRequest.EntityValues.ENTITY_LABELS);
dataExplorerDownloadHandler.writeToExcel(dataRequest, mock(OutputStream.class));
}
use of org.molgenis.dataexplorer.controller.DataRequest in project molgenis by molgenis.
the class DataExplorerDownloadHandlerTest method testWriteToCSV.
@Test(dataProvider = "writeToExcelDataProvider")
public void testWriteToCSV(DataRequest.ColNames colNames, DataRequest.EntityValues entityValues, Map<String, List<List<String>>> expected) throws Exception {
String entityTypeId = "sys_set_thousandgenomes";
when(dataService.getEntityType(entityTypeId)).thenReturn(entityType);
when(entityType.getAtomicAttributes()).thenReturn(asList(attribute1, attribute2, attribute3));
if (colNames == DataRequest.ColNames.ATTRIBUTE_LABELS) {
when(attribute1.getLabel()).thenReturn("attr1Label");
when(attribute2.getLabel()).thenReturn("attr2Label");
}
when(attribute1.getName()).thenReturn("attr1");
when(attribute2.getName()).thenReturn("attr2");
when(attribute3.getName()).thenReturn("attr3");
DataRequest dataRequest = new DataRequest();
dataRequest.setEntityName(entityTypeId);
dataRequest.setQuery(query);
dataRequest.setAttributeNames(asList("attr1", "attr2"));
dataRequest.setColNames(colNames);
dataRequest.setEntityValues(entityValues);
when(dataService.findAll(entityTypeId, query)).thenReturn(Stream.of(entity1, entity2));
doReturn("entity1attr1").when(entity1).get("attr1");
doReturn(refEntity1).when(entity1).get("attr2");
doReturn("entity2attr1").when(entity2).get("attr1");
doReturn(refEntity2).when(entity2).get("attr2");
if (entityValues == DataRequest.EntityValues.ENTITY_LABELS) {
when(refEntity1.getLabelValue()).thenReturn("refEntity1Label");
when(refEntity2.getLabelValue()).thenReturn("refEntity2Label");
} else {
when(refEntity1.getIdValue()).thenReturn("refEntity1Id");
when(refEntity2.getIdValue()).thenReturn("refEntity2Id");
}
File tmpFile = File.createTempFile("download", ".csv");
FileOutputStream fos = new FileOutputStream(tmpFile);
dataExplorerDownloadHandler.writeToCsv(dataRequest, fos, ',');
assertEquals(readCsv(tmpFile), expected.get(entityTypeId), "entities should get exported");
assertTrue(tmpFile.delete());
verifyNoMoreInteractions(refEntity1, refEntity2, attribute1, attribute2);
}
use of org.molgenis.dataexplorer.controller.DataRequest in project molgenis by molgenis.
the class DataExplorerDownloadHandler method filterAttributes.
private List<Attribute> filterAttributes(DataRequest dataRequest) {
EntityType entityType = dataService.getEntityType(dataRequest.getEntityName());
final Set<String> attributeNames = newHashSet(dataRequest.getAttributeNames());
return Streams.stream(entityType.getAtomicAttributes()).filter(attribute -> attributeNames.contains(attribute.getName())).collect(toList());
}
use of org.molgenis.dataexplorer.controller.DataRequest in project molgenis by molgenis.
the class DataExplorerDownloadHandlerTest method testWriteToExcel.
@Test(dataProvider = "writeToExcelDataProvider")
public void testWriteToExcel(DataRequest.ColNames colNames, DataRequest.EntityValues entityValues, Map<String, List<List<String>>> expected) throws Exception {
String entityTypeId = "sys_set_thousandgenomes";
when(dataService.count(entityTypeId, query)).thenReturn(2L);
when(dataService.getEntityType(entityTypeId)).thenReturn(entityType);
when(entityType.getAtomicAttributes()).thenReturn(asList(attribute1, attribute2, attribute3));
if (colNames == DataRequest.ColNames.ATTRIBUTE_LABELS) {
when(attribute1.getLabel()).thenReturn("attr1Label");
when(attribute2.getLabel()).thenReturn("attr2Label");
}
when(attribute1.getName()).thenReturn("attr1");
when(attribute2.getName()).thenReturn("attr2");
when(attribute3.getName()).thenReturn("attr3");
DataRequest dataRequest = new DataRequest();
dataRequest.setEntityName(entityTypeId);
dataRequest.setQuery(query);
dataRequest.setAttributeNames(asList("attr1", "attr2"));
dataRequest.setColNames(colNames);
dataRequest.setEntityValues(entityValues);
when(dataService.findAll(entityTypeId, query)).thenReturn(Stream.of(entity1, entity2));
doReturn("entity1attr1").when(entity1).get("attr1");
doReturn(refEntity1).when(entity1).get("attr2");
doReturn("entity2attr1").when(entity2).get("attr1");
doReturn(refEntity2).when(entity2).get("attr2");
if (entityValues == DataRequest.EntityValues.ENTITY_LABELS) {
when(refEntity1.getLabelValue()).thenReturn("refEntity1Label");
when(refEntity2.getLabelValue()).thenReturn("refEntity2Label");
} else {
when(refEntity1.getIdValue()).thenReturn("refEntity1Id");
when(refEntity2.getIdValue()).thenReturn("refEntity2Id");
}
File tmpFile = File.createTempFile("download", ".xlsx");
FileOutputStream fos = new FileOutputStream(tmpFile);
dataExplorerDownloadHandler.writeToExcel(dataRequest, fos);
assertEquals(readExcel(tmpFile), expected, "entities should get exported");
assertTrue(tmpFile.delete());
verifyNoMoreInteractions(refEntity1, refEntity2, attribute1, attribute2);
}
Aggregations