@Test @RunWithCustomExecutor public void should_not_throw_exception_check_file() throws Exception { VitamThreadUtils.getVitamSession().setTenantId(TENANT_ID); reset(functionalBackupService); LogbookOperationsClient logbookOperationsclient = mock(LogbookOperationsClient.class); when(logbookOperationsClientFactory.getClient()).thenReturn(logbookOperationsclient); when(logbookOperationsclient.selectOperation(any())) .thenReturn(getJsonResult(StatusCode.OK.name(), TENANT_ID)); File file = getResourceFile("agencies_delete.csv"); agencyService.checkFile(new FileInputStream(file)); }
public void checkFile(InputStream stream) throws ReferentialException, IOException { int tenantId = VitamThreadUtils.getVitamSession().getTenantId(); int lineNumber = 1; File csvFileReader = convertInputStreamToFile(stream); InputStream csvFileInputStream = null; try (FileReader reader = new FileReader(csvFileReader)) { final CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader().withTrim().withIgnoreEmptyLines(false)); final HashSet<String> idsset = new HashSet<>(); try { for (final CSVRecord record : parser) { List<ErrorReportAgencies> errors = new ArrayList<>(); lineNumber++; if (checkRecords(record)) { final String identifier = record.get(AgenciesModel.TAG_IDENTIFIER); final String name = record.get(AgenciesModel.TAG_NAME); final String description = record.get(AgenciesModel.TAG_DESCRIPTION); final AgenciesModel agenciesModel = new AgenciesModel(identifier, name, description, tenantId); checkParametersNotEmpty(identifier, name, description, errors, lineNumber); if (idsset.contains(identifier)) { errors .add(new ErrorReportAgencies(FileAgenciesErrorCode.STP_IMPORT_AGENCIES_ID_DUPLICATION, lineNumber, agenciesModel)); } idsset.add(identifier); if (!errors.isEmpty()) { errorsMap.put(lineNumber, errors); } } } } catch (IllegalArgumentException e) { String message = e.getMessage(); if (message.contains("Name not found")) { message = ReportConstants.FILE_INVALID + "Name"; } if (message.contains("Identifier not found")) { message = ReportConstants.FILE_INVALID + "Identifier"; } if (message.contains("Description not found")) { message = ReportConstants.FILE_INVALID + "Description"; } List<ErrorReportAgencies> errors = new ArrayList<>(); errors .add(new ErrorReportAgencies(FileAgenciesErrorCode.STP_IMPORT_AGENCIES_NOT_CSV_FORMAT, lineNumber, message)); errorsMap.put(lineNumber, errors); throw new ReferentialException(message); } catch (Exception e) { throw new ReferentialException(e); } csvFileInputStream = new FileInputStream(csvFileReader); agenciesToImport = new HashSet<>(AgenciesParser.readFromCsv(csvFileInputStream)); if (errorsMap.size() > 0) { throw new ReferentialException(INVALID_CSV_FILE); } } finally { IOUtils.closeQuietly(csvFileInputStream); if (csvFileReader != null && !csvFileReader.delete()) { LOGGER.warn("Failed to delete file"); } } }
AgenciesService implements VitamAutoCloseable { public void checkFile(InputStream stream) throws ReferentialException, IOException { int tenantId = VitamThreadUtils.getVitamSession().getTenantId(); int lineNumber = 1; File csvFileReader = convertInputStreamToFile(stream); InputStream csvFileInputStream = null; try (FileReader reader = new FileReader(csvFileReader)) { final CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader().withTrim().withIgnoreEmptyLines(false)); final HashSet<String> idsset = new HashSet<>(); try { for (final CSVRecord record : parser) { List<ErrorReportAgencies> errors = new ArrayList<>(); lineNumber++; if (checkRecords(record)) { final String identifier = record.get(AgenciesModel.TAG_IDENTIFIER); final String name = record.get(AgenciesModel.TAG_NAME); final String description = record.get(AgenciesModel.TAG_DESCRIPTION); final AgenciesModel agenciesModel = new AgenciesModel(identifier, name, description, tenantId); checkParametersNotEmpty(identifier, name, description, errors, lineNumber); if (idsset.contains(identifier)) { errors .add(new ErrorReportAgencies(FileAgenciesErrorCode.STP_IMPORT_AGENCIES_ID_DUPLICATION, lineNumber, agenciesModel)); } idsset.add(identifier); if (!errors.isEmpty()) { errorsMap.put(lineNumber, errors); } } } } catch (IllegalArgumentException e) { String message = e.getMessage(); if (message.contains("Name not found")) { message = ReportConstants.FILE_INVALID + "Name"; } if (message.contains("Identifier not found")) { message = ReportConstants.FILE_INVALID + "Identifier"; } if (message.contains("Description not found")) { message = ReportConstants.FILE_INVALID + "Description"; } List<ErrorReportAgencies> errors = new ArrayList<>(); errors .add(new ErrorReportAgencies(FileAgenciesErrorCode.STP_IMPORT_AGENCIES_NOT_CSV_FORMAT, lineNumber, message)); errorsMap.put(lineNumber, errors); throw new ReferentialException(message); } catch (Exception e) { throw new ReferentialException(e); } csvFileInputStream = new FileInputStream(csvFileReader); agenciesToImport = new HashSet<>(AgenciesParser.readFromCsv(csvFileInputStream)); if (errorsMap.size() > 0) { throw new ReferentialException(INVALID_CSV_FILE); } } finally { IOUtils.closeQuietly(csvFileInputStream); if (csvFileReader != null && !csvFileReader.delete()) { LOGGER.warn("Failed to delete file"); } } } }
AgenciesService implements VitamAutoCloseable { public void checkFile(InputStream stream) throws ReferentialException, IOException { int tenantId = VitamThreadUtils.getVitamSession().getTenantId(); int lineNumber = 1; File csvFileReader = convertInputStreamToFile(stream); InputStream csvFileInputStream = null; try (FileReader reader = new FileReader(csvFileReader)) { final CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader().withTrim().withIgnoreEmptyLines(false)); final HashSet<String> idsset = new HashSet<>(); try { for (final CSVRecord record : parser) { List<ErrorReportAgencies> errors = new ArrayList<>(); lineNumber++; if (checkRecords(record)) { final String identifier = record.get(AgenciesModel.TAG_IDENTIFIER); final String name = record.get(AgenciesModel.TAG_NAME); final String description = record.get(AgenciesModel.TAG_DESCRIPTION); final AgenciesModel agenciesModel = new AgenciesModel(identifier, name, description, tenantId); checkParametersNotEmpty(identifier, name, description, errors, lineNumber); if (idsset.contains(identifier)) { errors .add(new ErrorReportAgencies(FileAgenciesErrorCode.STP_IMPORT_AGENCIES_ID_DUPLICATION, lineNumber, agenciesModel)); } idsset.add(identifier); if (!errors.isEmpty()) { errorsMap.put(lineNumber, errors); } } } } catch (IllegalArgumentException e) { String message = e.getMessage(); if (message.contains("Name not found")) { message = ReportConstants.FILE_INVALID + "Name"; } if (message.contains("Identifier not found")) { message = ReportConstants.FILE_INVALID + "Identifier"; } if (message.contains("Description not found")) { message = ReportConstants.FILE_INVALID + "Description"; } List<ErrorReportAgencies> errors = new ArrayList<>(); errors .add(new ErrorReportAgencies(FileAgenciesErrorCode.STP_IMPORT_AGENCIES_NOT_CSV_FORMAT, lineNumber, message)); errorsMap.put(lineNumber, errors); throw new ReferentialException(message); } catch (Exception e) { throw new ReferentialException(e); } csvFileInputStream = new FileInputStream(csvFileReader); agenciesToImport = new HashSet<>(AgenciesParser.readFromCsv(csvFileInputStream)); if (errorsMap.size() > 0) { throw new ReferentialException(INVALID_CSV_FILE); } } finally { IOUtils.closeQuietly(csvFileInputStream); if (csvFileReader != null && !csvFileReader.delete()) { LOGGER.warn("Failed to delete file"); } } } AgenciesService(MongoDbAccessAdminImpl mongoAccess,
        VitamCounterService vitamCounterService, FunctionalBackupService backupService, OntologyLoader ontologyLoader); @VisibleForTesting  AgenciesService(
        MongoDbAccessAdminImpl mongoAccess,
        VitamCounterService vitamCounterService,
        FunctionalBackupService backupService,
        LogbookOperationsClientFactory logbookOperationsClientFactory,
        AgenciesManager manager,
        Set<AgenciesModel> agenciesInDb,
        Set<AgenciesModel> agenciesToDelete,
        Set<AgenciesModel> agenciesToInsert,
        Set<AgenciesModel> agenciesToUpdate,
        Set<AgenciesModel> usedAgenciesByAU,
        Set<AgenciesModel> usedAgenciesByContracts,
        Set <AgenciesModel> unusedAgenciesToDelete,
        OntologyLoader ontologyLoader); }
AgenciesService implements VitamAutoCloseable { public void checkFile(InputStream stream) throws ReferentialException, IOException { int tenantId = VitamThreadUtils.getVitamSession().getTenantId(); int lineNumber = 1; File csvFileReader = convertInputStreamToFile(stream); InputStream csvFileInputStream = null; try (FileReader reader = new FileReader(csvFileReader)) { final CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader().withTrim().withIgnoreEmptyLines(false)); final HashSet<String> idsset = new HashSet<>(); try { for (final CSVRecord record : parser) { List<ErrorReportAgencies> errors = new ArrayList<>(); lineNumber++; if (checkRecords(record)) { final String identifier = record.get(AgenciesModel.TAG_IDENTIFIER); final String name = record.get(AgenciesModel.TAG_NAME); final String description = record.get(AgenciesModel.TAG_DESCRIPTION); final AgenciesModel agenciesModel = new AgenciesModel(identifier, name, description, tenantId); checkParametersNotEmpty(identifier, name, description, errors, lineNumber); if (idsset.contains(identifier)) { errors .add(new ErrorReportAgencies(FileAgenciesErrorCode.STP_IMPORT_AGENCIES_ID_DUPLICATION, lineNumber, agenciesModel)); } idsset.add(identifier); if (!errors.isEmpty()) { errorsMap.put(lineNumber, errors); } } } } catch (IllegalArgumentException e) { String message = e.getMessage(); if (message.contains("Name not found")) { message = ReportConstants.FILE_INVALID + "Name"; } if (message.contains("Identifier not found")) { message = ReportConstants.FILE_INVALID + "Identifier"; } if (message.contains("Description not found")) { message = ReportConstants.FILE_INVALID + "Description"; } List<ErrorReportAgencies> errors = new ArrayList<>(); errors .add(new ErrorReportAgencies(FileAgenciesErrorCode.STP_IMPORT_AGENCIES_NOT_CSV_FORMAT, lineNumber, message)); errorsMap.put(lineNumber, errors); throw new ReferentialException(message); } catch (Exception e) { throw new ReferentialException(e); } csvFileInputStream = new FileInputStream(csvFileReader); agenciesToImport = new HashSet<>(AgenciesParser.readFromCsv(csvFileInputStream)); if (errorsMap.size() > 0) { throw new ReferentialException(INVALID_CSV_FILE); } } finally { IOUtils.closeQuietly(csvFileInputStream); if (csvFileReader != null && !csvFileReader.delete()) { LOGGER.warn("Failed to delete file"); } } } AgenciesService(MongoDbAccessAdminImpl mongoAccess,
        VitamCounterService vitamCounterService, FunctionalBackupService backupService, OntologyLoader ontologyLoader); @VisibleForTesting  AgenciesService(
        MongoDbAccessAdminImpl mongoAccess,
        VitamCounterService vitamCounterService,
        FunctionalBackupService backupService,
        LogbookOperationsClientFactory logbookOperationsClientFactory,
        AgenciesManager manager,
        Set<AgenciesModel> agenciesInDb,
        Set<AgenciesModel> agenciesToDelete,
        Set<AgenciesModel> agenciesToInsert,
        Set<AgenciesModel> agenciesToUpdate,
        Set<AgenciesModel> usedAgenciesByAU,
        Set<AgenciesModel> usedAgenciesByContracts,
        Set <AgenciesModel> unusedAgenciesToDelete,
        OntologyLoader ontologyLoader); RequestResponseOK findDocuments(JsonNode select); void findAllAgenciesUsedByUnits(); void findAllAgenciesUsedByAccessContracts(); void checkFile(InputStream stream); void checkAgenciesInDb(); RequestResponse<AgenciesModel> importAgencies(InputStream stream, String filename); DbRequestResult findAgencies(JsonNode queryDsl); InputStream generateErrorReport(); @Override void close(); }
AgenciesService implements VitamAutoCloseable { public void checkFile(InputStream stream) throws ReferentialException, IOException { int tenantId = VitamThreadUtils.getVitamSession().getTenantId(); int lineNumber = 1; File csvFileReader = convertInputStreamToFile(stream); InputStream csvFileInputStream = null; try (FileReader reader = new FileReader(csvFileReader)) { final CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader().withTrim().withIgnoreEmptyLines(false)); final HashSet<String> idsset = new HashSet<>(); try { for (final CSVRecord record : parser) { List<ErrorReportAgencies> errors = new ArrayList<>(); lineNumber++; if (checkRecords(record)) { final String identifier = record.get(AgenciesModel.TAG_IDENTIFIER); final String name = record.get(AgenciesModel.TAG_NAME); final String description = record.get(AgenciesModel.TAG_DESCRIPTION); final AgenciesModel agenciesModel = new AgenciesModel(identifier, name, description, tenantId); checkParametersNotEmpty(identifier, name, description, errors, lineNumber); if (idsset.contains(identifier)) { errors .add(new ErrorReportAgencies(FileAgenciesErrorCode.STP_IMPORT_AGENCIES_ID_DUPLICATION, lineNumber, agenciesModel)); } idsset.add(identifier); if (!errors.isEmpty()) { errorsMap.put(lineNumber, errors); } } } } catch (IllegalArgumentException e) { String message = e.getMessage(); if (message.contains("Name not found")) { message = ReportConstants.FILE_INVALID + "Name"; } if (message.contains("Identifier not found")) { message = ReportConstants.FILE_INVALID + "Identifier"; } if (message.contains("Description not found")) { message = ReportConstants.FILE_INVALID + "Description"; } List<ErrorReportAgencies> errors = new ArrayList<>(); errors .add(new ErrorReportAgencies(FileAgenciesErrorCode.STP_IMPORT_AGENCIES_NOT_CSV_FORMAT, lineNumber, message)); errorsMap.put(lineNumber, errors); throw new ReferentialException(message); } catch (Exception e) { throw new ReferentialException(e); } csvFileInputStream = new FileInputStream(csvFileReader); agenciesToImport = new HashSet<>(AgenciesParser.readFromCsv(csvFileInputStream)); if (errorsMap.size() > 0) { throw new ReferentialException(INVALID_CSV_FILE); } } finally { IOUtils.closeQuietly(csvFileInputStream); if (csvFileReader != null && !csvFileReader.delete()) { LOGGER.warn("Failed to delete file"); } } } AgenciesService(MongoDbAccessAdminImpl mongoAccess,
        VitamCounterService vitamCounterService, FunctionalBackupService backupService, OntologyLoader ontologyLoader); @VisibleForTesting  AgenciesService(
        MongoDbAccessAdminImpl mongoAccess,
        VitamCounterService vitamCounterService,
        FunctionalBackupService backupService,
        LogbookOperationsClientFactory logbookOperationsClientFactory,
        AgenciesManager manager,
        Set<AgenciesModel> agenciesInDb,
        Set<AgenciesModel> agenciesToDelete,
        Set<AgenciesModel> agenciesToInsert,
        Set<AgenciesModel> agenciesToUpdate,
        Set<AgenciesModel> usedAgenciesByAU,
        Set<AgenciesModel> usedAgenciesByContracts,
        Set <AgenciesModel> unusedAgenciesToDelete,
        OntologyLoader ontologyLoader); RequestResponseOK findDocuments(JsonNode select); void findAllAgenciesUsedByUnits(); void findAllAgenciesUsedByAccessContracts(); void checkFile(InputStream stream); void checkAgenciesInDb(); RequestResponse<AgenciesModel> importAgencies(InputStream stream, String filename); DbRequestResult findAgencies(JsonNode queryDsl); InputStream generateErrorReport(); @Override void close(); static final VitamLogger LOGGER; }