@Test void testHandleRequestNoWritablePackageException() { HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); when(httpServletRequest.getParameter("data-option")).thenReturn("add"); BindingResult bindingResult = mock(BindingResult.class); ImportWizard wizard = mock(ImportWizard.class); File file = mock(File.class); when(file.getName()).thenReturn("fileName"); when(wizard.getFile()).thenReturn(file); FileRepositoryCollection fileRepositoryCollection = mock(FileRepositoryCollection.class); when(fileRepositoryCollectionFactory.createFileRepositoryCollection(file)) .thenReturn(fileRepositoryCollection); ImportService importService = mock(ImportService.class); EntitiesValidationReport entitiesValidationReport = mock(EntitiesValidationReport.class); when(entitiesValidationReport.getSheetsImportable()) .thenReturn(singletonMap("MyEntityType", true)); when(importService.validateImport(fileRepositoryCollection)) .thenReturn(entitiesValidationReport); when(importServiceFactory.getImportService(file, fileRepositoryCollection)) .thenReturn(importService); MetaDataService metaDataService = mock(MetaDataService.class); Package package0 = mock(Package.class); when(package0.getId()).thenReturn("packageId"); when(metaDataService.getPackages()).thenReturn(singletonList(package0)); when(dataService.getMeta()).thenReturn(metaDataService); assertNull(optionsWizardPage.handleRequest(httpServletRequest, bindingResult, wizard)); verify(bindingResult) .addError( new ObjectError( "wizard", "<b>Your import failed:</b><br />FAILED TO FORMAT LOCALIZED MESSAGE FOR ERROR CODE DS07.%nFallback message: no writable package")); }
@SuppressWarnings("javasecurity:S2083") @Override public String handleRequest(HttpServletRequest request, BindingResult result, Wizard wizard) { ImportWizardUtil.validateImportWizard(wizard); ImportWizard importWizard = (ImportWizard) wizard; String dataImportOption = setImportOption(request, importWizard); setMetadataImportOption(request, importWizard); if (importWizard.getMustChangeEntityName()) { @SuppressWarnings("javasecurity:S2083") String userGivenName = request.getParameter("name"); if (StringUtils.isEmpty(userGivenName)) { result.addError(new ObjectError("wizard", "Please enter an entity name")); return null; } try { NameValidator.validateEntityName(userGivenName); if (getDataService().hasRepository(userGivenName)) { result.addError(new ObjectError("wizard", "An entity with this name already exists.")); return null; } } catch (MolgenisDataException e) { ImportWizardUtil.handleException(e, importWizard, result, LOG, dataImportOption); return null; } File tmpFile = importWizard.getFile(); String tmpFilename = tmpFile.getName(); String extension = FileExtensionUtils.findExtensionFromPossibilities( tmpFilename, getFileRepositoryCollectionFactory() .createFileRepositoryCollection(tmpFile) .getFileNameExtensions()); File file = new File(tmpFile.getParent(), userGivenName + "." + extension); if (!tmpFile.renameTo(file)) { String filename = file.getName() == null ? file.getName().replaceAll("[\n|\r|\t]", "_") : null; LOG.error("Failed to rename '{}' to '{}'", tmpFilename, filename); } importWizard.setFile(file); } try { return validateInput(importWizard.getFile(), importWizard); } catch (Exception e) { ImportWizardUtil.handleException(e, importWizard, result, LOG, dataImportOption); } return null; }
OptionsWizardPage extends AbstractWizardPage { @SuppressWarnings("javasecurity:S2083") @Override public String handleRequest(HttpServletRequest request, BindingResult result, Wizard wizard) { ImportWizardUtil.validateImportWizard(wizard); ImportWizard importWizard = (ImportWizard) wizard; String dataImportOption = setImportOption(request, importWizard); setMetadataImportOption(request, importWizard); if (importWizard.getMustChangeEntityName()) { @SuppressWarnings("javasecurity:S2083") String userGivenName = request.getParameter("name"); if (StringUtils.isEmpty(userGivenName)) { result.addError(new ObjectError("wizard", "Please enter an entity name")); return null; } try { NameValidator.validateEntityName(userGivenName); if (getDataService().hasRepository(userGivenName)) { result.addError(new ObjectError("wizard", "An entity with this name already exists.")); return null; } } catch (MolgenisDataException e) { ImportWizardUtil.handleException(e, importWizard, result, LOG, dataImportOption); return null; } File tmpFile = importWizard.getFile(); String tmpFilename = tmpFile.getName(); String extension = FileExtensionUtils.findExtensionFromPossibilities( tmpFilename, getFileRepositoryCollectionFactory() .createFileRepositoryCollection(tmpFile) .getFileNameExtensions()); File file = new File(tmpFile.getParent(), userGivenName + "." + extension); if (!tmpFile.renameTo(file)) { String filename = file.getName() == null ? file.getName().replaceAll("[\n|\r|\t]", "_") : null; LOG.error("Failed to rename '{}' to '{}'", tmpFilename, filename); } importWizard.setFile(file); } try { return validateInput(importWizard.getFile(), importWizard); } catch (Exception e) { ImportWizardUtil.handleException(e, importWizard, result, LOG, dataImportOption); } return null; } }
OptionsWizardPage extends AbstractWizardPage { @SuppressWarnings("javasecurity:S2083") @Override public String handleRequest(HttpServletRequest request, BindingResult result, Wizard wizard) { ImportWizardUtil.validateImportWizard(wizard); ImportWizard importWizard = (ImportWizard) wizard; String dataImportOption = setImportOption(request, importWizard); setMetadataImportOption(request, importWizard); if (importWizard.getMustChangeEntityName()) { @SuppressWarnings("javasecurity:S2083") String userGivenName = request.getParameter("name"); if (StringUtils.isEmpty(userGivenName)) { result.addError(new ObjectError("wizard", "Please enter an entity name")); return null; } try { NameValidator.validateEntityName(userGivenName); if (getDataService().hasRepository(userGivenName)) { result.addError(new ObjectError("wizard", "An entity with this name already exists.")); return null; } } catch (MolgenisDataException e) { ImportWizardUtil.handleException(e, importWizard, result, LOG, dataImportOption); return null; } File tmpFile = importWizard.getFile(); String tmpFilename = tmpFile.getName(); String extension = FileExtensionUtils.findExtensionFromPossibilities( tmpFilename, getFileRepositoryCollectionFactory() .createFileRepositoryCollection(tmpFile) .getFileNameExtensions()); File file = new File(tmpFile.getParent(), userGivenName + "." + extension); if (!tmpFile.renameTo(file)) { String filename = file.getName() == null ? file.getName().replaceAll("[\n|\r|\t]", "_") : null; LOG.error("Failed to rename '{}' to '{}'", tmpFilename, filename); } importWizard.setFile(file); } try { return validateInput(importWizard.getFile(), importWizard); } catch (Exception e) { ImportWizardUtil.handleException(e, importWizard, result, LOG, dataImportOption); } return null; } OptionsWizardPage(
      FileRepositoryCollectionFactory fileRepositoryCollectionFactory,
      ImportServiceFactory importServiceFactory,
      DataService dataService,
      UserPermissionEvaluator userPermissionEvaluator); }
OptionsWizardPage extends AbstractWizardPage { @SuppressWarnings("javasecurity:S2083") @Override public String handleRequest(HttpServletRequest request, BindingResult result, Wizard wizard) { ImportWizardUtil.validateImportWizard(wizard); ImportWizard importWizard = (ImportWizard) wizard; String dataImportOption = setImportOption(request, importWizard); setMetadataImportOption(request, importWizard); if (importWizard.getMustChangeEntityName()) { @SuppressWarnings("javasecurity:S2083") String userGivenName = request.getParameter("name"); if (StringUtils.isEmpty(userGivenName)) { result.addError(new ObjectError("wizard", "Please enter an entity name")); return null; } try { NameValidator.validateEntityName(userGivenName); if (getDataService().hasRepository(userGivenName)) { result.addError(new ObjectError("wizard", "An entity with this name already exists.")); return null; } } catch (MolgenisDataException e) { ImportWizardUtil.handleException(e, importWizard, result, LOG, dataImportOption); return null; } File tmpFile = importWizard.getFile(); String tmpFilename = tmpFile.getName(); String extension = FileExtensionUtils.findExtensionFromPossibilities( tmpFilename, getFileRepositoryCollectionFactory() .createFileRepositoryCollection(tmpFile) .getFileNameExtensions()); File file = new File(tmpFile.getParent(), userGivenName + "." + extension); if (!tmpFile.renameTo(file)) { String filename = file.getName() == null ? file.getName().replaceAll("[\n|\r|\t]", "_") : null; LOG.error("Failed to rename '{}' to '{}'", tmpFilename, filename); } importWizard.setFile(file); } try { return validateInput(importWizard.getFile(), importWizard); } catch (Exception e) { ImportWizardUtil.handleException(e, importWizard, result, LOG, dataImportOption); } return null; } OptionsWizardPage(
      FileRepositoryCollectionFactory fileRepositoryCollectionFactory,
      ImportServiceFactory importServiceFactory,
      DataService dataService,
      UserPermissionEvaluator userPermissionEvaluator); @Override String getTitle(); @SuppressWarnings("javasecurity:S2083") @Override String handleRequest(HttpServletRequest request, BindingResult result, Wizard wizard); }
OptionsWizardPage extends AbstractWizardPage { @SuppressWarnings("javasecurity:S2083") @Override public String handleRequest(HttpServletRequest request, BindingResult result, Wizard wizard) { ImportWizardUtil.validateImportWizard(wizard); ImportWizard importWizard = (ImportWizard) wizard; String dataImportOption = setImportOption(request, importWizard); setMetadataImportOption(request, importWizard); if (importWizard.getMustChangeEntityName()) { @SuppressWarnings("javasecurity:S2083") String userGivenName = request.getParameter("name"); if (StringUtils.isEmpty(userGivenName)) { result.addError(new ObjectError("wizard", "Please enter an entity name")); return null; } try { NameValidator.validateEntityName(userGivenName); if (getDataService().hasRepository(userGivenName)) { result.addError(new ObjectError("wizard", "An entity with this name already exists.")); return null; } } catch (MolgenisDataException e) { ImportWizardUtil.handleException(e, importWizard, result, LOG, dataImportOption); return null; } File tmpFile = importWizard.getFile(); String tmpFilename = tmpFile.getName(); String extension = FileExtensionUtils.findExtensionFromPossibilities( tmpFilename, getFileRepositoryCollectionFactory() .createFileRepositoryCollection(tmpFile) .getFileNameExtensions()); File file = new File(tmpFile.getParent(), userGivenName + "." + extension); if (!tmpFile.renameTo(file)) { String filename = file.getName() == null ? file.getName().replaceAll("[\n|\r|\t]", "_") : null; LOG.error("Failed to rename '{}' to '{}'", tmpFilename, filename); } importWizard.setFile(file); } try { return validateInput(importWizard.getFile(), importWizard); } catch (Exception e) { ImportWizardUtil.handleException(e, importWizard, result, LOG, dataImportOption); } return null; } OptionsWizardPage(
      FileRepositoryCollectionFactory fileRepositoryCollectionFactory,
      ImportServiceFactory importServiceFactory,
      DataService dataService,
      UserPermissionEvaluator userPermissionEvaluator); @Override String getTitle(); @SuppressWarnings("javasecurity:S2083") @Override String handleRequest(HttpServletRequest request, BindingResult result, Wizard wizard);  }