Search in sources :

Example 1 with DataCollection

use of org.molgenis.oneclickimporter.model.DataCollection in project molgenis by molgenis.

the class OneClickImporterServiceImpl method buildDataCollectionsFromExcel.

@Override
public List<DataCollection> buildDataCollectionsFromExcel(List<Sheet> sheets) {
    List<DataCollection> dataCollections = newArrayList();
    sheets.forEach(sheet -> {
        List<Column> columns = newArrayList();
        Row headerRow = sheet.getRow(0);
        headerRow.cellIterator().forEachRemaining(cell -> columns.add(createColumnFromCell(sheet, cell)));
        dataCollections.add(DataCollection.create(sheet.getSheetName(), columns));
    });
    return dataCollections;
}
Also used : DataCollection(org.molgenis.oneclickimporter.model.DataCollection) Column(org.molgenis.oneclickimporter.model.Column) Row(org.apache.poi.ss.usermodel.Row)

Example 2 with DataCollection

use of org.molgenis.oneclickimporter.model.DataCollection in project molgenis by molgenis.

the class OneClickImportJobTest method testGetEntityTypeWithZip.

@Test
public void testGetEntityTypeWithZip() throws InvalidFormatException, IOException, URISyntaxException, UnknownFileTypeException, EmptySheetException {
    Progress progress = mock(Progress.class);
    String filename = "simple-valid.zip";
    when(oneClickImporterNamingService.createValidIdFromFileName(filename)).thenReturn("simple_valid");
    File file = loadFile(OneClickImportJobTest.class, "/" + filename);
    when(fileStore.getFile(filename)).thenReturn(file);
    File zipFile1 = loadFile(OneClickImportJobTest.class, "/zip_file_1.csv");
    when(oneClickImporterNamingService.createValidIdFromFileName("zip_file_1.csv")).thenReturn("zip_file_1");
    File zipFile2 = loadFile(OneClickImportJobTest.class, "/zip_file_2.csv");
    when(oneClickImporterNamingService.createValidIdFromFileName("zip_file_2.csv")).thenReturn("zip_file_2");
    File zipFile3 = loadFile(OneClickImportJobTest.class, "/zip_file_3.csv");
    when(oneClickImporterNamingService.createValidIdFromFileName("zip_file_3.csv")).thenReturn("zip_file_3");
    File zipFile4 = loadFile(OneClickImportJobTest.class, "/zip_file_4.csv");
    when(oneClickImporterNamingService.createValidIdFromFileName("zip_file_4.csv")).thenReturn("zip_file_4");
    List<String[]> lines1 = new ArrayList<>();
    lines1.add(new String[] { "name,age", "piet,25" });
    when(csvService.buildLinesFromFile(zipFile1)).thenReturn(lines1);
    List<String[]> lines2 = new ArrayList<>();
    lines2.add(new String[] { "name,age", "klaas,30" });
    when(csvService.buildLinesFromFile(zipFile2)).thenReturn(lines2);
    List<String[]> lines3 = new ArrayList<>();
    lines3.add(new String[] { "name,age", "Jan,35" });
    when(csvService.buildLinesFromFile(zipFile3)).thenReturn(lines3);
    List<String[]> lines4 = new ArrayList<>();
    lines4.add(new String[] { "name,age", "Henk,40" });
    when(csvService.buildLinesFromFile(zipFile4)).thenReturn(lines4);
    DataCollection dataCollection1 = mock(DataCollection.class);
    when(dataCollection1.getName()).thenReturn("zip_file_1");
    when(oneClickImporterService.buildDataCollectionFromCsv("zip_file_1", lines1)).thenReturn(dataCollection1);
    DataCollection dataCollection2 = mock(DataCollection.class);
    when(dataCollection2.getName()).thenReturn("zip_file_2");
    when(oneClickImporterService.buildDataCollectionFromCsv("zip_file_2", lines2)).thenReturn(dataCollection2);
    DataCollection dataCollection3 = mock(DataCollection.class);
    when(dataCollection3.getName()).thenReturn("zip_file_3");
    when(oneClickImporterService.buildDataCollectionFromCsv("zip_file_3", lines3)).thenReturn(dataCollection3);
    DataCollection dataCollection4 = mock(DataCollection.class);
    when(dataCollection4.getName()).thenReturn("zip_file_4");
    when(oneClickImporterService.buildDataCollectionFromCsv("zip_file_4", lines4)).thenReturn(dataCollection4);
    EntityType entityType1 = mock(EntityType.class);
    when(entityService.createEntityType(dataCollection1, "simple_valid")).thenReturn(entityType1);
    EntityType entityType2 = mock(EntityType.class);
    when(entityService.createEntityType(dataCollection2, "simple_valid")).thenReturn(entityType2);
    EntityType entityType3 = mock(EntityType.class);
    when(entityService.createEntityType(dataCollection3, "simple_valid")).thenReturn(entityType3);
    EntityType entityType4 = mock(EntityType.class);
    when(entityService.createEntityType(dataCollection4, "simple_valid")).thenReturn(entityType4);
    oneClickImporterJob = new OneClickImportJob(excelService, csvService, oneClickImporterService, oneClickImporterNamingService, entityService, fileStore);
    oneClickImporterJob.getEntityType(progress, filename);
    verify(progress).status("Preparing import");
    verify(csvService).buildLinesFromFile(zipFile1);
    verify(oneClickImporterService).buildDataCollectionFromCsv("zip_file_1", lines1);
    verify(csvService).buildLinesFromFile(zipFile2);
    verify(oneClickImporterService).buildDataCollectionFromCsv("zip_file_2", lines2);
    verify(csvService).buildLinesFromFile(zipFile3);
    verify(oneClickImporterService).buildDataCollectionFromCsv("zip_file_3", lines3);
    verify(csvService).buildLinesFromFile(zipFile4);
    verify(oneClickImporterService).buildDataCollectionFromCsv("zip_file_4", lines4);
    verify(progress).status("Importing [zip_file_1] into package [simple_valid]");
    verify(entityService).createEntityType(dataCollection1, "simple_valid");
    verify(progress).status("Importing [zip_file_2] into package [simple_valid]");
    verify(entityService).createEntityType(dataCollection2, "simple_valid");
    verify(progress).status("Importing [zip_file_3] into package [simple_valid]");
    verify(entityService).createEntityType(dataCollection3, "simple_valid");
    verify(progress).status("Importing [zip_file_4] into package [simple_valid]");
    verify(entityService).createEntityType(dataCollection4, "simple_valid");
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Progress(org.molgenis.jobs.Progress) DataCollection(org.molgenis.oneclickimporter.model.DataCollection) ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) File(java.io.File) OneClickImporterTestUtils.loadFile(org.molgenis.oneclickimporter.service.utils.OneClickImporterTestUtils.loadFile) Test(org.testng.annotations.Test)

Example 3 with DataCollection

use of org.molgenis.oneclickimporter.model.DataCollection in project molgenis by molgenis.

the class OneClickImportJobTest method testGetEntityTypeWithExcel.

@Test
public void testGetEntityTypeWithExcel() throws InvalidFormatException, IOException, URISyntaxException, UnknownFileTypeException, EmptySheetException {
    Progress progress = mock(Progress.class);
    String filename = "simple-valid.xlsx";
    when(oneClickImporterNamingService.createValidIdFromFileName(filename)).thenReturn("simple_valid");
    File file = loadFile(OneClickImportJobTest.class, "/" + filename);
    when(fileStore.getFile(filename)).thenReturn(file);
    List<Sheet> sheets = new ArrayList<>();
    when(excelService.buildExcelSheetsFromFile(file)).thenReturn(sheets);
    DataCollection dataCollection = mock(DataCollection.class);
    when(dataCollection.getName()).thenReturn("Sheet1");
    when(oneClickImporterService.buildDataCollectionsFromExcel(sheets)).thenReturn(newArrayList(dataCollection));
    EntityType entityType = mock(EntityType.class);
    when(entityService.createEntityType(dataCollection, "simple_valid")).thenReturn(entityType);
    oneClickImporterJob = new OneClickImportJob(excelService, csvService, oneClickImporterService, oneClickImporterNamingService, entityService, fileStore);
    oneClickImporterJob.getEntityType(progress, filename);
    verify(progress).status("Preparing import");
    verify(excelService).buildExcelSheetsFromFile(file);
    verify(oneClickImporterService).buildDataCollectionsFromExcel(sheets);
    verify(progress).status("Importing [Sheet1] into package [simple_valid]");
    verify(entityService).createEntityType(dataCollection, "simple_valid");
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Progress(org.molgenis.jobs.Progress) DataCollection(org.molgenis.oneclickimporter.model.DataCollection) ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) File(java.io.File) OneClickImporterTestUtils.loadFile(org.molgenis.oneclickimporter.service.utils.OneClickImporterTestUtils.loadFile) Sheet(org.apache.poi.ss.usermodel.Sheet) Test(org.testng.annotations.Test)

Example 4 with DataCollection

use of org.molgenis.oneclickimporter.model.DataCollection in project molgenis by molgenis.

the class OneClickImporterServiceTest method testBuildDataSheetBuildsColumnsOfEqualLength.

@Test
public void testBuildDataSheetBuildsColumnsOfEqualLength() throws IOException, InvalidFormatException, URISyntaxException, EmptySheetException {
    List<Sheet> sheets = loadSheetFromFile(OneClickImporterServiceTest.class, "/valid-with-blank-values.xlsx");
    DataCollection actual = oneClickImporterService.buildDataCollectionsFromExcel(sheets).get(0);
    Column c1 = Column.create("name", 0, newArrayList("Mark", "Bart", "Tommy", "Sido", "Connor", null));
    Column c2 = Column.create("favorite food", 1, newArrayList("Fries", null, "Vegan food", "Pizza", null, "Spinache"));
    DataCollection expected = DataCollection.create("Sheet1", newArrayList(c1, c2));
    assertEquals(actual, expected);
    assertEquals(6, c1.getDataValues().size());
    assertEquals(6, c2.getDataValues().size());
}
Also used : DataCollection(org.molgenis.oneclickimporter.model.DataCollection) Column(org.molgenis.oneclickimporter.model.Column) Sheet(org.apache.poi.ss.usermodel.Sheet) Test(org.testng.annotations.Test)

Example 5 with DataCollection

use of org.molgenis.oneclickimporter.model.DataCollection in project molgenis by molgenis.

the class OneClickImporterServiceTest method testBuildDataSheetWithDates.

@Test
public void testBuildDataSheetWithDates() throws IOException, InvalidFormatException, URISyntaxException, EmptySheetException {
    List<Sheet> sheets = loadSheetFromFile(OneClickImporterServiceTest.class, "/valid-with-dates.xlsx");
    DataCollection actual = oneClickImporterService.buildDataCollectionsFromExcel(sheets).get(0);
    Column c1 = Column.create("dates", 0, newArrayList("2018-01-03T00:00", "2018-01-04T00:00", "2018-01-05T00:00", "2018-01-06T00:00", "2018-01-07T00:00"));
    Column c2 = Column.create("event", 1, newArrayList("being cool day", "bike day", "sleep day", "bye bye day", "work day"));
    DataCollection expected = DataCollection.create("Sheet1", newArrayList(c1, c2));
    assertEquals(actual, expected);
}
Also used : DataCollection(org.molgenis.oneclickimporter.model.DataCollection) Column(org.molgenis.oneclickimporter.model.Column) Sheet(org.apache.poi.ss.usermodel.Sheet) Test(org.testng.annotations.Test)

Aggregations

DataCollection (org.molgenis.oneclickimporter.model.DataCollection)13 Test (org.testng.annotations.Test)11 Column (org.molgenis.oneclickimporter.model.Column)9 Sheet (org.apache.poi.ss.usermodel.Sheet)6 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)4 File (java.io.File)4 EntityType (org.molgenis.data.meta.model.EntityType)4 ArrayList (java.util.ArrayList)3 Progress (org.molgenis.jobs.Progress)3 OneClickImporterTestUtils.loadFile (org.molgenis.oneclickimporter.service.utils.OneClickImporterTestUtils.loadFile)3 OneClickImporterServiceImpl (org.molgenis.oneclickimporter.service.impl.OneClickImporterServiceImpl)2 List (java.util.List)1 Row (org.apache.poi.ss.usermodel.Row)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 Entity (org.molgenis.data.Entity)1 Package (org.molgenis.data.meta.model.Package)1 UnknownFileTypeException (org.molgenis.oneclickimporter.exceptions.UnknownFileTypeException)1 EntityServiceImpl (org.molgenis.oneclickimporter.service.impl.EntityServiceImpl)1 Transactional (org.springframework.transaction.annotation.Transactional)1