@Test 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.getFileUnchecked(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"); }
@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);  }