Search in sources :

Example 1 with DataRequest

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));
}
Also used : DataRequest(org.molgenis.dataexplorer.controller.DataRequest) Test(org.testng.annotations.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 2 with DataRequest

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);
}
Also used : DataRequest(org.molgenis.dataexplorer.controller.DataRequest) Test(org.testng.annotations.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 3 with DataRequest

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());
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) OutputStream(java.io.OutputStream) AttributeFactory(org.molgenis.data.meta.model.AttributeFactory) EntityWriteMode(org.molgenis.data.support.AbstractWritable.EntityWriteMode) Set(java.util.Set) AttributeWriteMode(org.molgenis.data.support.AbstractWritable.AttributeWriteMode) IOException(java.io.IOException) Streams(com.google.common.collect.Streams) Attribute(org.molgenis.data.meta.model.Attribute) EntityType(org.molgenis.data.meta.model.EntityType) FileFormat(org.molgenis.data.excel.ExcelWriter.FileFormat) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Objects.requireNonNull(java.util.Objects.requireNonNull) DataService(org.molgenis.data.DataService) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) ExcelWriter(org.molgenis.data.excel.ExcelWriter) ExcelSheetWriter(org.molgenis.data.excel.ExcelSheetWriter) MolgenisDataException(org.molgenis.data.MolgenisDataException) DataRequest(org.molgenis.dataexplorer.controller.DataRequest) CsvWriter(org.molgenis.data.csv.CsvWriter)

Example 4 with DataRequest

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);
}
Also used : DataRequest(org.molgenis.dataexplorer.controller.DataRequest) Test(org.testng.annotations.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Aggregations

DataRequest (org.molgenis.dataexplorer.controller.DataRequest)4 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)3 Test (org.testng.annotations.Test)3 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)1 Streams (com.google.common.collect.Streams)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 List (java.util.List)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Set (java.util.Set)1 Collectors.toList (java.util.stream.Collectors.toList)1 DataService (org.molgenis.data.DataService)1 MolgenisDataException (org.molgenis.data.MolgenisDataException)1 CsvWriter (org.molgenis.data.csv.CsvWriter)1 ExcelSheetWriter (org.molgenis.data.excel.ExcelSheetWriter)1 ExcelWriter (org.molgenis.data.excel.ExcelWriter)1 FileFormat (org.molgenis.data.excel.ExcelWriter.FileFormat)1 Attribute (org.molgenis.data.meta.model.Attribute)1 AttributeFactory (org.molgenis.data.meta.model.AttributeFactory)1 EntityType (org.molgenis.data.meta.model.EntityType)1