@Test 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.getFileUnchecked(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"); }
@Transactional public List<EntityType> getEntityType(Progress progress, String filename) throws UnknownFileTypeException, IOException, InvalidFormatException, EmptySheetException { File file = fileStore.getFileUnchecked(filename); String fileExtension = findExtensionFromPossibilities(filename, newHashSet("csv", "xlsx", "zip", "xls")); progress.status("Preparing import"); List<DataCollection> dataCollections = newArrayList(); if (fileExtension == null) { throw new UnknownFileTypeException( String.format( "File [%s] does not have a valid extension, supported: [csv, xlsx, zip, xls]", filename)); } else if (fileExtension.equals("xls") || fileExtension.equals("xlsx")) { List<Sheet> sheets = excelService.buildExcelSheetsFromFile(file); dataCollections.addAll(oneClickImporterService.buildDataCollectionsFromExcel(sheets)); } else if (fileExtension.equals("csv")) { List<String[]> lines = csvService.buildLinesFromFile(file); dataCollections.add( oneClickImporterService.buildDataCollectionFromCsv( oneClickImporterNamingService.createValidIdFromFileName(filename), lines)); } else if (fileExtension.equals("zip")) { List<File> filesInZip; try { filesInZip = ZipFileUtil.unzipSkipHidden(file); } catch (UnzipException zipException) { throw new CodedUnzipException(file.getName(), zipException); } for (File fileInZip : filesInZip) { String fileInZipExtension = findExtensionFromPossibilities(fileInZip.getName(), newHashSet("csv")); if (fileInZipExtension != null) { List<String[]> lines = csvService.buildLinesFromFile(fileInZip); dataCollections.add( oneClickImporterService.buildDataCollectionFromCsv( oneClickImporterNamingService.createValidIdFromFileName(fileInZip.getName()), lines)); } else { throw new UnknownFileTypeException("Zip file contains files which are not of type CSV"); } } } List<EntityType> entityTypes = newArrayList(); String packageName = oneClickImporterNamingService.createValidIdFromFileName(filename); dataCollections.forEach( dataCollection -> { progress.status( "Importing [" + dataCollection.getName() + "] into package [" + packageName + "]"); entityTypes.add(entityService.createEntityType(dataCollection, packageName)); }); return entityTypes; }
OneClickImportJob { @Transactional public List<EntityType> getEntityType(Progress progress, String filename) throws UnknownFileTypeException, IOException, InvalidFormatException, EmptySheetException { File file = fileStore.getFileUnchecked(filename); String fileExtension = findExtensionFromPossibilities(filename, newHashSet("csv", "xlsx", "zip", "xls")); progress.status("Preparing import"); List<DataCollection> dataCollections = newArrayList(); if (fileExtension == null) { throw new UnknownFileTypeException( String.format( "File [%s] does not have a valid extension, supported: [csv, xlsx, zip, xls]", filename)); } else if (fileExtension.equals("xls") || fileExtension.equals("xlsx")) { List<Sheet> sheets = excelService.buildExcelSheetsFromFile(file); dataCollections.addAll(oneClickImporterService.buildDataCollectionsFromExcel(sheets)); } else if (fileExtension.equals("csv")) { List<String[]> lines = csvService.buildLinesFromFile(file); dataCollections.add( oneClickImporterService.buildDataCollectionFromCsv( oneClickImporterNamingService.createValidIdFromFileName(filename), lines)); } else if (fileExtension.equals("zip")) { List<File> filesInZip; try { filesInZip = ZipFileUtil.unzipSkipHidden(file); } catch (UnzipException zipException) { throw new CodedUnzipException(file.getName(), zipException); } for (File fileInZip : filesInZip) { String fileInZipExtension = findExtensionFromPossibilities(fileInZip.getName(), newHashSet("csv")); if (fileInZipExtension != null) { List<String[]> lines = csvService.buildLinesFromFile(fileInZip); dataCollections.add( oneClickImporterService.buildDataCollectionFromCsv( oneClickImporterNamingService.createValidIdFromFileName(fileInZip.getName()), lines)); } else { throw new UnknownFileTypeException("Zip file contains files which are not of type CSV"); } } } List<EntityType> entityTypes = newArrayList(); String packageName = oneClickImporterNamingService.createValidIdFromFileName(filename); dataCollections.forEach( dataCollection -> { progress.status( "Importing [" + dataCollection.getName() + "] into package [" + packageName + "]"); entityTypes.add(entityService.createEntityType(dataCollection, packageName)); }); return entityTypes; } }
OneClickImportJob { @Transactional public List<EntityType> getEntityType(Progress progress, String filename) throws UnknownFileTypeException, IOException, InvalidFormatException, EmptySheetException { File file = fileStore.getFileUnchecked(filename); String fileExtension = findExtensionFromPossibilities(filename, newHashSet("csv", "xlsx", "zip", "xls")); progress.status("Preparing import"); List<DataCollection> dataCollections = newArrayList(); if (fileExtension == null) { throw new UnknownFileTypeException( String.format( "File [%s] does not have a valid extension, supported: [csv, xlsx, zip, xls]", filename)); } else if (fileExtension.equals("xls") || fileExtension.equals("xlsx")) { List<Sheet> sheets = excelService.buildExcelSheetsFromFile(file); dataCollections.addAll(oneClickImporterService.buildDataCollectionsFromExcel(sheets)); } else if (fileExtension.equals("csv")) { List<String[]> lines = csvService.buildLinesFromFile(file); dataCollections.add( oneClickImporterService.buildDataCollectionFromCsv( oneClickImporterNamingService.createValidIdFromFileName(filename), lines)); } else if (fileExtension.equals("zip")) { List<File> filesInZip; try { filesInZip = ZipFileUtil.unzipSkipHidden(file); } catch (UnzipException zipException) { throw new CodedUnzipException(file.getName(), zipException); } for (File fileInZip : filesInZip) { String fileInZipExtension = findExtensionFromPossibilities(fileInZip.getName(), newHashSet("csv")); if (fileInZipExtension != null) { List<String[]> lines = csvService.buildLinesFromFile(fileInZip); dataCollections.add( oneClickImporterService.buildDataCollectionFromCsv( oneClickImporterNamingService.createValidIdFromFileName(fileInZip.getName()), lines)); } else { throw new UnknownFileTypeException("Zip file contains files which are not of type CSV"); } } } List<EntityType> entityTypes = newArrayList(); String packageName = oneClickImporterNamingService.createValidIdFromFileName(filename); dataCollections.forEach( dataCollection -> { progress.status( "Importing [" + dataCollection.getName() + "] into package [" + packageName + "]"); entityTypes.add(entityService.createEntityType(dataCollection, packageName)); }); return entityTypes; } OneClickImportJob(
      ExcelService excelService,
      CsvService csvService,
      OneClickImporterService oneClickImporterService,
      OneClickImporterNamingService oneClickImporterNamingService,
      EntityService entityService,
      FileStore fileStore); }
OneClickImportJob { @Transactional public List<EntityType> getEntityType(Progress progress, String filename) throws UnknownFileTypeException, IOException, InvalidFormatException, EmptySheetException { File file = fileStore.getFileUnchecked(filename); String fileExtension = findExtensionFromPossibilities(filename, newHashSet("csv", "xlsx", "zip", "xls")); progress.status("Preparing import"); List<DataCollection> dataCollections = newArrayList(); if (fileExtension == null) { throw new UnknownFileTypeException( String.format( "File [%s] does not have a valid extension, supported: [csv, xlsx, zip, xls]", filename)); } else if (fileExtension.equals("xls") || fileExtension.equals("xlsx")) { List<Sheet> sheets = excelService.buildExcelSheetsFromFile(file); dataCollections.addAll(oneClickImporterService.buildDataCollectionsFromExcel(sheets)); } else if (fileExtension.equals("csv")) { List<String[]> lines = csvService.buildLinesFromFile(file); dataCollections.add( oneClickImporterService.buildDataCollectionFromCsv( oneClickImporterNamingService.createValidIdFromFileName(filename), lines)); } else if (fileExtension.equals("zip")) { List<File> filesInZip; try { filesInZip = ZipFileUtil.unzipSkipHidden(file); } catch (UnzipException zipException) { throw new CodedUnzipException(file.getName(), zipException); } for (File fileInZip : filesInZip) { String fileInZipExtension = findExtensionFromPossibilities(fileInZip.getName(), newHashSet("csv")); if (fileInZipExtension != null) { List<String[]> lines = csvService.buildLinesFromFile(fileInZip); dataCollections.add( oneClickImporterService.buildDataCollectionFromCsv( oneClickImporterNamingService.createValidIdFromFileName(fileInZip.getName()), lines)); } else { throw new UnknownFileTypeException("Zip file contains files which are not of type CSV"); } } } List<EntityType> entityTypes = newArrayList(); String packageName = oneClickImporterNamingService.createValidIdFromFileName(filename); dataCollections.forEach( dataCollection -> { progress.status( "Importing [" + dataCollection.getName() + "] into package [" + packageName + "]"); entityTypes.add(entityService.createEntityType(dataCollection, packageName)); }); return entityTypes; } OneClickImportJob(
      ExcelService excelService,
      CsvService csvService,
      OneClickImporterService oneClickImporterService,
      OneClickImporterNamingService oneClickImporterNamingService,
      EntityService entityService,
      FileStore fileStore); @Transactional List<EntityType> getEntityType(Progress progress, String filename); }
OneClickImportJob { @Transactional public List<EntityType> getEntityType(Progress progress, String filename) throws UnknownFileTypeException, IOException, InvalidFormatException, EmptySheetException { File file = fileStore.getFileUnchecked(filename); String fileExtension = findExtensionFromPossibilities(filename, newHashSet("csv", "xlsx", "zip", "xls")); progress.status("Preparing import"); List<DataCollection> dataCollections = newArrayList(); if (fileExtension == null) { throw new UnknownFileTypeException( String.format( "File [%s] does not have a valid extension, supported: [csv, xlsx, zip, xls]", filename)); } else if (fileExtension.equals("xls") || fileExtension.equals("xlsx")) { List<Sheet> sheets = excelService.buildExcelSheetsFromFile(file); dataCollections.addAll(oneClickImporterService.buildDataCollectionsFromExcel(sheets)); } else if (fileExtension.equals("csv")) { List<String[]> lines = csvService.buildLinesFromFile(file); dataCollections.add( oneClickImporterService.buildDataCollectionFromCsv( oneClickImporterNamingService.createValidIdFromFileName(filename), lines)); } else if (fileExtension.equals("zip")) { List<File> filesInZip; try { filesInZip = ZipFileUtil.unzipSkipHidden(file); } catch (UnzipException zipException) { throw new CodedUnzipException(file.getName(), zipException); } for (File fileInZip : filesInZip) { String fileInZipExtension = findExtensionFromPossibilities(fileInZip.getName(), newHashSet("csv")); if (fileInZipExtension != null) { List<String[]> lines = csvService.buildLinesFromFile(fileInZip); dataCollections.add( oneClickImporterService.buildDataCollectionFromCsv( oneClickImporterNamingService.createValidIdFromFileName(fileInZip.getName()), lines)); } else { throw new UnknownFileTypeException("Zip file contains files which are not of type CSV"); } } } List<EntityType> entityTypes = newArrayList(); String packageName = oneClickImporterNamingService.createValidIdFromFileName(filename); dataCollections.forEach( dataCollection -> { progress.status( "Importing [" + dataCollection.getName() + "] into package [" + packageName + "]"); entityTypes.add(entityService.createEntityType(dataCollection, packageName)); }); return entityTypes; } OneClickImportJob(
      ExcelService excelService,
      CsvService csvService,
      OneClickImporterService oneClickImporterService,
      OneClickImporterNamingService oneClickImporterNamingService,
      EntityService entityService,
      FileStore fileStore); @Transactional List<EntityType> getEntityType(Progress progress, String filename);  }