@Test @RunWithCustomExecutor public void testimportRulesFileWithEmptyLine() throws Exception { int tenantId = 12; VitamThreadUtils.getVitamSession().setTenantId(tenantId); when(logbookOperationsClient.selectOperation(any())) .thenReturn(getJsonResult(STP_IMPORT_RULES, tenantId)); Map<Integer, List<ErrorReport>> errors = new HashMap<>(); List<FileRulesModel> usedDeletedRules = new ArrayList<>(); List<FileRulesModel> usedUpdatedRules = new ArrayList<>(); List<FileRulesModel> usedUpdateRulesForUpdateUnit = new ArrayList<>(); List<FileRulesModel> insertRules = new ArrayList<>(); Set<String> notUsedDeletedRules = new HashSet<>(); Set<String> notUsedUpdatedRules = new HashSet<>(); assertThatThrownBy(() -> rulesFileManager.checkFile( new FileInputStream(PropertiesUtils.findFile(FILE_TO_TEST_KO_EMPTY_LINE)), errors, usedDeletedRules, usedUpdatedRules, usedUpdateRulesForUpdateUnit, insertRules, notUsedDeletedRules, notUsedUpdatedRules)) .isInstanceOf(FileRulesCsvException.class) .hasMessageContaining("Index for header 'RuleType' is 1 but CSVRecord only has 1 values!"); }
public ArrayNode checkFile(InputStream rulesFileStream, Map<Integer, List<ErrorReport>> errorsMap, List<FileRulesModel> usedDeletedRules, List<FileRulesModel> usedUpdatedRules, List<FileRulesModel> usedUpdateRulesForUpdateUnit, List<FileRulesModel> insertRules, Set<String> notUsedDeletedRules, Set<String> notUsedUpdatedRules) throws IOException, ReferentialException, InvalidParseOperationException { ParametersChecker.checkParameter(RULES_FILE_STREAM_IS_A_MANDATORY_PARAMETER, rulesFileStream); File csvFileReader = convertInputStreamToFile(rulesFileStream, TXT); int lineNumber = 1; try (FileReader reader = new FileReader(csvFileReader)) { @SuppressWarnings("resource") final CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader().withTrim().withIgnoreEmptyLines(false)); final HashSet<String> ruleIdSet = new HashSet<>(); try { for (final CSVRecord record : parser) { lineNumber++; List<ErrorReport> errors = new ArrayList<>(); if (checkRecords(record)) { final String ruleId = record.get(FileRulesModel.TAG_RULE_ID); final String ruleType = record.get(FileRulesModel.TAG_RULE_TYPE); final String ruleValue = record.get(FileRulesModel.TAG_RULE_VALUE); final String ruleDuration = record.get(FileRulesModel.TAG_RULE_DURATION); final String ruleMeasurementValue = record.get(FileRulesModel.TAG_RULE_MEASUREMENT); final FileRulesModel fileRulesModel = new FileRulesModel(ruleId, ruleType, ruleValue, null, ruleDuration, ruleMeasurementValue); checkParametersNotEmpty(ruleId, ruleType, ruleValue, ruleDuration, ruleMeasurementValue, errors, lineNumber); checkRuleDuration(fileRulesModel, errors, lineNumber); if (ruleIdSet.contains(ruleId)) { errors .add(new ErrorReport(FileRulesErrorCode.STP_IMPORT_RULES_RULEID_DUPLICATION, lineNumber, fileRulesModel)); } ruleIdSet.add(ruleId); if (!containsRuleMeasurement(ruleMeasurementValue)) { errors.add(new ErrorReport(FileRulesErrorCode.STP_IMPORT_RULES_WRONG_RULEMEASUREMENT, lineNumber, fileRulesModel)); } if (!containsRuleType(ruleType)) { errors.add(new ErrorReport(FileRulesErrorCode.STP_IMPORT_RULES_WRONG_RULETYPE_UNKNOW, lineNumber, fileRulesModel)); } if (checkAssociationRuleDurationRuleMeasurementLimit(record, errors, lineNumber, fileRulesModel)) { checkRuleDurationWithConfiguration(record, errors, lineNumber, fileRulesModel); } if (errors.size() > 0) { errorsMap.put(lineNumber, errors); } } } } catch (IllegalArgumentException e) { String message = e.getMessage(); if (message.contains(FileRulesModel.TAG_RULE_ID + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_ID; } else if (message.contains(FileRulesModel.TAG_RULE_TYPE + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_TYPE; } else if (message.contains(FileRulesModel.TAG_RULE_VALUE + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_VALUE; } else if (message.contains(FileRulesModel.TAG_RULE_DURATION + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_DURATION; } else if (message.contains(FileRulesModel.TAG_RULE_DESCRIPTION + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_DESCRIPTION; } else if (message.contains(FileRulesModel.TAG_RULE_MEASUREMENT + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_MEASUREMENT; } List<ErrorReport> errors = new ArrayList<>(); errors .add(new ErrorReport(FileRulesErrorCode.STP_IMPORT_RULES_NOT_CSV_FORMAT, lineNumber, message)); errorsMap.put(lineNumber, errors); throw new FileRulesCsvException(message); } catch (Exception e) { throw new ReferentialException(e); } } if (csvFileReader != null) { final ArrayNode readRulesAsJson = RulesManagerParser.readObjectsFromCsvWriteAsArrayNode(csvFileReader); checkRulesLinkedToAu(readRulesAsJson, usedDeletedRules, usedUpdatedRules, usedUpdateRulesForUpdateUnit, insertRules, notUsedDeletedRules, notUsedUpdatedRules); if (errorsMap.size() > 0) { for (List<ErrorReport> map : errorsMap.values()) { for (ErrorReport error : map) { if (error.getCode().equals(FileRulesErrorCode.STP_IMPORT_RULES_RULEDURATION_EXCEED)) { throw new FileRulesDurationException(RULE_DURATION_EXCEED); } } } throw new FileRulesCsvException(INVALID_CSV_FILE); } if (!usedDeletedRules.isEmpty()) { throw new FileRulesDeleteException("used Rules want to be deleted"); } if (!usedUpdateRulesForUpdateUnit.isEmpty()) { throw new FileRulesUpdateException("used Rules want to be updated"); } Files.delete(csvFileReader.toPath()); return readRulesAsJson; } throw new FileRulesException(INVALID_CSV_FILE); }
RulesManagerFileImpl implements ReferentialFile<FileRules> { public ArrayNode checkFile(InputStream rulesFileStream, Map<Integer, List<ErrorReport>> errorsMap, List<FileRulesModel> usedDeletedRules, List<FileRulesModel> usedUpdatedRules, List<FileRulesModel> usedUpdateRulesForUpdateUnit, List<FileRulesModel> insertRules, Set<String> notUsedDeletedRules, Set<String> notUsedUpdatedRules) throws IOException, ReferentialException, InvalidParseOperationException { ParametersChecker.checkParameter(RULES_FILE_STREAM_IS_A_MANDATORY_PARAMETER, rulesFileStream); File csvFileReader = convertInputStreamToFile(rulesFileStream, TXT); int lineNumber = 1; try (FileReader reader = new FileReader(csvFileReader)) { @SuppressWarnings("resource") final CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader().withTrim().withIgnoreEmptyLines(false)); final HashSet<String> ruleIdSet = new HashSet<>(); try { for (final CSVRecord record : parser) { lineNumber++; List<ErrorReport> errors = new ArrayList<>(); if (checkRecords(record)) { final String ruleId = record.get(FileRulesModel.TAG_RULE_ID); final String ruleType = record.get(FileRulesModel.TAG_RULE_TYPE); final String ruleValue = record.get(FileRulesModel.TAG_RULE_VALUE); final String ruleDuration = record.get(FileRulesModel.TAG_RULE_DURATION); final String ruleMeasurementValue = record.get(FileRulesModel.TAG_RULE_MEASUREMENT); final FileRulesModel fileRulesModel = new FileRulesModel(ruleId, ruleType, ruleValue, null, ruleDuration, ruleMeasurementValue); checkParametersNotEmpty(ruleId, ruleType, ruleValue, ruleDuration, ruleMeasurementValue, errors, lineNumber); checkRuleDuration(fileRulesModel, errors, lineNumber); if (ruleIdSet.contains(ruleId)) { errors .add(new ErrorReport(FileRulesErrorCode.STP_IMPORT_RULES_RULEID_DUPLICATION, lineNumber, fileRulesModel)); } ruleIdSet.add(ruleId); if (!containsRuleMeasurement(ruleMeasurementValue)) { errors.add(new ErrorReport(FileRulesErrorCode.STP_IMPORT_RULES_WRONG_RULEMEASUREMENT, lineNumber, fileRulesModel)); } if (!containsRuleType(ruleType)) { errors.add(new ErrorReport(FileRulesErrorCode.STP_IMPORT_RULES_WRONG_RULETYPE_UNKNOW, lineNumber, fileRulesModel)); } if (checkAssociationRuleDurationRuleMeasurementLimit(record, errors, lineNumber, fileRulesModel)) { checkRuleDurationWithConfiguration(record, errors, lineNumber, fileRulesModel); } if (errors.size() > 0) { errorsMap.put(lineNumber, errors); } } } } catch (IllegalArgumentException e) { String message = e.getMessage(); if (message.contains(FileRulesModel.TAG_RULE_ID + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_ID; } else if (message.contains(FileRulesModel.TAG_RULE_TYPE + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_TYPE; } else if (message.contains(FileRulesModel.TAG_RULE_VALUE + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_VALUE; } else if (message.contains(FileRulesModel.TAG_RULE_DURATION + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_DURATION; } else if (message.contains(FileRulesModel.TAG_RULE_DESCRIPTION + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_DESCRIPTION; } else if (message.contains(FileRulesModel.TAG_RULE_MEASUREMENT + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_MEASUREMENT; } List<ErrorReport> errors = new ArrayList<>(); errors .add(new ErrorReport(FileRulesErrorCode.STP_IMPORT_RULES_NOT_CSV_FORMAT, lineNumber, message)); errorsMap.put(lineNumber, errors); throw new FileRulesCsvException(message); } catch (Exception e) { throw new ReferentialException(e); } } if (csvFileReader != null) { final ArrayNode readRulesAsJson = RulesManagerParser.readObjectsFromCsvWriteAsArrayNode(csvFileReader); checkRulesLinkedToAu(readRulesAsJson, usedDeletedRules, usedUpdatedRules, usedUpdateRulesForUpdateUnit, insertRules, notUsedDeletedRules, notUsedUpdatedRules); if (errorsMap.size() > 0) { for (List<ErrorReport> map : errorsMap.values()) { for (ErrorReport error : map) { if (error.getCode().equals(FileRulesErrorCode.STP_IMPORT_RULES_RULEDURATION_EXCEED)) { throw new FileRulesDurationException(RULE_DURATION_EXCEED); } } } throw new FileRulesCsvException(INVALID_CSV_FILE); } if (!usedDeletedRules.isEmpty()) { throw new FileRulesDeleteException("used Rules want to be deleted"); } if (!usedUpdateRulesForUpdateUnit.isEmpty()) { throw new FileRulesUpdateException("used Rules want to be updated"); } Files.delete(csvFileReader.toPath()); return readRulesAsJson; } throw new FileRulesException(INVALID_CSV_FILE); } }
RulesManagerFileImpl implements ReferentialFile<FileRules> { public ArrayNode checkFile(InputStream rulesFileStream, Map<Integer, List<ErrorReport>> errorsMap, List<FileRulesModel> usedDeletedRules, List<FileRulesModel> usedUpdatedRules, List<FileRulesModel> usedUpdateRulesForUpdateUnit, List<FileRulesModel> insertRules, Set<String> notUsedDeletedRules, Set<String> notUsedUpdatedRules) throws IOException, ReferentialException, InvalidParseOperationException { ParametersChecker.checkParameter(RULES_FILE_STREAM_IS_A_MANDATORY_PARAMETER, rulesFileStream); File csvFileReader = convertInputStreamToFile(rulesFileStream, TXT); int lineNumber = 1; try (FileReader reader = new FileReader(csvFileReader)) { @SuppressWarnings("resource") final CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader().withTrim().withIgnoreEmptyLines(false)); final HashSet<String> ruleIdSet = new HashSet<>(); try { for (final CSVRecord record : parser) { lineNumber++; List<ErrorReport> errors = new ArrayList<>(); if (checkRecords(record)) { final String ruleId = record.get(FileRulesModel.TAG_RULE_ID); final String ruleType = record.get(FileRulesModel.TAG_RULE_TYPE); final String ruleValue = record.get(FileRulesModel.TAG_RULE_VALUE); final String ruleDuration = record.get(FileRulesModel.TAG_RULE_DURATION); final String ruleMeasurementValue = record.get(FileRulesModel.TAG_RULE_MEASUREMENT); final FileRulesModel fileRulesModel = new FileRulesModel(ruleId, ruleType, ruleValue, null, ruleDuration, ruleMeasurementValue); checkParametersNotEmpty(ruleId, ruleType, ruleValue, ruleDuration, ruleMeasurementValue, errors, lineNumber); checkRuleDuration(fileRulesModel, errors, lineNumber); if (ruleIdSet.contains(ruleId)) { errors .add(new ErrorReport(FileRulesErrorCode.STP_IMPORT_RULES_RULEID_DUPLICATION, lineNumber, fileRulesModel)); } ruleIdSet.add(ruleId); if (!containsRuleMeasurement(ruleMeasurementValue)) { errors.add(new ErrorReport(FileRulesErrorCode.STP_IMPORT_RULES_WRONG_RULEMEASUREMENT, lineNumber, fileRulesModel)); } if (!containsRuleType(ruleType)) { errors.add(new ErrorReport(FileRulesErrorCode.STP_IMPORT_RULES_WRONG_RULETYPE_UNKNOW, lineNumber, fileRulesModel)); } if (checkAssociationRuleDurationRuleMeasurementLimit(record, errors, lineNumber, fileRulesModel)) { checkRuleDurationWithConfiguration(record, errors, lineNumber, fileRulesModel); } if (errors.size() > 0) { errorsMap.put(lineNumber, errors); } } } } catch (IllegalArgumentException e) { String message = e.getMessage(); if (message.contains(FileRulesModel.TAG_RULE_ID + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_ID; } else if (message.contains(FileRulesModel.TAG_RULE_TYPE + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_TYPE; } else if (message.contains(FileRulesModel.TAG_RULE_VALUE + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_VALUE; } else if (message.contains(FileRulesModel.TAG_RULE_DURATION + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_DURATION; } else if (message.contains(FileRulesModel.TAG_RULE_DESCRIPTION + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_DESCRIPTION; } else if (message.contains(FileRulesModel.TAG_RULE_MEASUREMENT + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_MEASUREMENT; } List<ErrorReport> errors = new ArrayList<>(); errors .add(new ErrorReport(FileRulesErrorCode.STP_IMPORT_RULES_NOT_CSV_FORMAT, lineNumber, message)); errorsMap.put(lineNumber, errors); throw new FileRulesCsvException(message); } catch (Exception e) { throw new ReferentialException(e); } } if (csvFileReader != null) { final ArrayNode readRulesAsJson = RulesManagerParser.readObjectsFromCsvWriteAsArrayNode(csvFileReader); checkRulesLinkedToAu(readRulesAsJson, usedDeletedRules, usedUpdatedRules, usedUpdateRulesForUpdateUnit, insertRules, notUsedDeletedRules, notUsedUpdatedRules); if (errorsMap.size() > 0) { for (List<ErrorReport> map : errorsMap.values()) { for (ErrorReport error : map) { if (error.getCode().equals(FileRulesErrorCode.STP_IMPORT_RULES_RULEDURATION_EXCEED)) { throw new FileRulesDurationException(RULE_DURATION_EXCEED); } } } throw new FileRulesCsvException(INVALID_CSV_FILE); } if (!usedDeletedRules.isEmpty()) { throw new FileRulesDeleteException("used Rules want to be deleted"); } if (!usedUpdateRulesForUpdateUnit.isEmpty()) { throw new FileRulesUpdateException("used Rules want to be updated"); } Files.delete(csvFileReader.toPath()); return readRulesAsJson; } throw new FileRulesException(INVALID_CSV_FILE); } RulesManagerFileImpl(MongoDbAccessAdminImpl dbConfiguration, VitamCounterService vitamCounterService,
        OntologyLoader ontologyLoader, VitamRuleService vitamRuleService); @VisibleForTesting  RulesManagerFileImpl(MongoDbAccessAdminImpl dbConfiguration,
        VitamCounterService vitamCounterService,
        FunctionalBackupService backupService, LogbookOperationsClientFactory logbookOperationsClientFactory,
        MetaDataClientFactory metaDataClientFactory,
        ProcessingManagementClientFactory processingManagementClientFactory,
        WorkspaceClientFactory workspaceClientFactory,
        OntologyLoader ontologyLoader,
        VitamRuleService vitamRuleService); }
RulesManagerFileImpl implements ReferentialFile<FileRules> { public ArrayNode checkFile(InputStream rulesFileStream, Map<Integer, List<ErrorReport>> errorsMap, List<FileRulesModel> usedDeletedRules, List<FileRulesModel> usedUpdatedRules, List<FileRulesModel> usedUpdateRulesForUpdateUnit, List<FileRulesModel> insertRules, Set<String> notUsedDeletedRules, Set<String> notUsedUpdatedRules) throws IOException, ReferentialException, InvalidParseOperationException { ParametersChecker.checkParameter(RULES_FILE_STREAM_IS_A_MANDATORY_PARAMETER, rulesFileStream); File csvFileReader = convertInputStreamToFile(rulesFileStream, TXT); int lineNumber = 1; try (FileReader reader = new FileReader(csvFileReader)) { @SuppressWarnings("resource") final CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader().withTrim().withIgnoreEmptyLines(false)); final HashSet<String> ruleIdSet = new HashSet<>(); try { for (final CSVRecord record : parser) { lineNumber++; List<ErrorReport> errors = new ArrayList<>(); if (checkRecords(record)) { final String ruleId = record.get(FileRulesModel.TAG_RULE_ID); final String ruleType = record.get(FileRulesModel.TAG_RULE_TYPE); final String ruleValue = record.get(FileRulesModel.TAG_RULE_VALUE); final String ruleDuration = record.get(FileRulesModel.TAG_RULE_DURATION); final String ruleMeasurementValue = record.get(FileRulesModel.TAG_RULE_MEASUREMENT); final FileRulesModel fileRulesModel = new FileRulesModel(ruleId, ruleType, ruleValue, null, ruleDuration, ruleMeasurementValue); checkParametersNotEmpty(ruleId, ruleType, ruleValue, ruleDuration, ruleMeasurementValue, errors, lineNumber); checkRuleDuration(fileRulesModel, errors, lineNumber); if (ruleIdSet.contains(ruleId)) { errors .add(new ErrorReport(FileRulesErrorCode.STP_IMPORT_RULES_RULEID_DUPLICATION, lineNumber, fileRulesModel)); } ruleIdSet.add(ruleId); if (!containsRuleMeasurement(ruleMeasurementValue)) { errors.add(new ErrorReport(FileRulesErrorCode.STP_IMPORT_RULES_WRONG_RULEMEASUREMENT, lineNumber, fileRulesModel)); } if (!containsRuleType(ruleType)) { errors.add(new ErrorReport(FileRulesErrorCode.STP_IMPORT_RULES_WRONG_RULETYPE_UNKNOW, lineNumber, fileRulesModel)); } if (checkAssociationRuleDurationRuleMeasurementLimit(record, errors, lineNumber, fileRulesModel)) { checkRuleDurationWithConfiguration(record, errors, lineNumber, fileRulesModel); } if (errors.size() > 0) { errorsMap.put(lineNumber, errors); } } } } catch (IllegalArgumentException e) { String message = e.getMessage(); if (message.contains(FileRulesModel.TAG_RULE_ID + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_ID; } else if (message.contains(FileRulesModel.TAG_RULE_TYPE + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_TYPE; } else if (message.contains(FileRulesModel.TAG_RULE_VALUE + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_VALUE; } else if (message.contains(FileRulesModel.TAG_RULE_DURATION + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_DURATION; } else if (message.contains(FileRulesModel.TAG_RULE_DESCRIPTION + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_DESCRIPTION; } else if (message.contains(FileRulesModel.TAG_RULE_MEASUREMENT + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_MEASUREMENT; } List<ErrorReport> errors = new ArrayList<>(); errors .add(new ErrorReport(FileRulesErrorCode.STP_IMPORT_RULES_NOT_CSV_FORMAT, lineNumber, message)); errorsMap.put(lineNumber, errors); throw new FileRulesCsvException(message); } catch (Exception e) { throw new ReferentialException(e); } } if (csvFileReader != null) { final ArrayNode readRulesAsJson = RulesManagerParser.readObjectsFromCsvWriteAsArrayNode(csvFileReader); checkRulesLinkedToAu(readRulesAsJson, usedDeletedRules, usedUpdatedRules, usedUpdateRulesForUpdateUnit, insertRules, notUsedDeletedRules, notUsedUpdatedRules); if (errorsMap.size() > 0) { for (List<ErrorReport> map : errorsMap.values()) { for (ErrorReport error : map) { if (error.getCode().equals(FileRulesErrorCode.STP_IMPORT_RULES_RULEDURATION_EXCEED)) { throw new FileRulesDurationException(RULE_DURATION_EXCEED); } } } throw new FileRulesCsvException(INVALID_CSV_FILE); } if (!usedDeletedRules.isEmpty()) { throw new FileRulesDeleteException("used Rules want to be deleted"); } if (!usedUpdateRulesForUpdateUnit.isEmpty()) { throw new FileRulesUpdateException("used Rules want to be updated"); } Files.delete(csvFileReader.toPath()); return readRulesAsJson; } throw new FileRulesException(INVALID_CSV_FILE); } RulesManagerFileImpl(MongoDbAccessAdminImpl dbConfiguration, VitamCounterService vitamCounterService,
        OntologyLoader ontologyLoader, VitamRuleService vitamRuleService); @VisibleForTesting  RulesManagerFileImpl(MongoDbAccessAdminImpl dbConfiguration,
        VitamCounterService vitamCounterService,
        FunctionalBackupService backupService, LogbookOperationsClientFactory logbookOperationsClientFactory,
        MetaDataClientFactory metaDataClientFactory,
        ProcessingManagementClientFactory processingManagementClientFactory,
        WorkspaceClientFactory workspaceClientFactory,
        OntologyLoader ontologyLoader,
        VitamRuleService vitamRuleService); @Override void importFile(InputStream rulesFileStream, String filename); ArrayNode checkFile(InputStream rulesFileStream, Map<Integer, List<ErrorReport>> errorsMap,
        List<FileRulesModel> usedDeletedRules, List<FileRulesModel> usedUpdatedRules,
        List<FileRulesModel> usedUpdateRulesForUpdateUnit, List<FileRulesModel> insertRules,
        Set<String> notUsedDeletedRules, Set<String> notUsedUpdatedRules); @Override FileRules findDocumentById(String id); @Override RequestResponseOK<FileRules> findDocuments(JsonNode select); InputStream generateErrorReport(Map<Integer, List<ErrorReport>> errors,
        List<FileRulesModel> usedDeletedRules, List<FileRulesModel> usedUpdatedRules,
        StatusCode status, GUID eipMaster); ArrayNode getRuleFromCollection(int tenant); ArrayNode getRuleFromOffer(int tenant); boolean checkRuleConformity(ArrayNode array1, ArrayNode array2, int tenant); }
RulesManagerFileImpl implements ReferentialFile<FileRules> { public ArrayNode checkFile(InputStream rulesFileStream, Map<Integer, List<ErrorReport>> errorsMap, List<FileRulesModel> usedDeletedRules, List<FileRulesModel> usedUpdatedRules, List<FileRulesModel> usedUpdateRulesForUpdateUnit, List<FileRulesModel> insertRules, Set<String> notUsedDeletedRules, Set<String> notUsedUpdatedRules) throws IOException, ReferentialException, InvalidParseOperationException { ParametersChecker.checkParameter(RULES_FILE_STREAM_IS_A_MANDATORY_PARAMETER, rulesFileStream); File csvFileReader = convertInputStreamToFile(rulesFileStream, TXT); int lineNumber = 1; try (FileReader reader = new FileReader(csvFileReader)) { @SuppressWarnings("resource") final CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader().withTrim().withIgnoreEmptyLines(false)); final HashSet<String> ruleIdSet = new HashSet<>(); try { for (final CSVRecord record : parser) { lineNumber++; List<ErrorReport> errors = new ArrayList<>(); if (checkRecords(record)) { final String ruleId = record.get(FileRulesModel.TAG_RULE_ID); final String ruleType = record.get(FileRulesModel.TAG_RULE_TYPE); final String ruleValue = record.get(FileRulesModel.TAG_RULE_VALUE); final String ruleDuration = record.get(FileRulesModel.TAG_RULE_DURATION); final String ruleMeasurementValue = record.get(FileRulesModel.TAG_RULE_MEASUREMENT); final FileRulesModel fileRulesModel = new FileRulesModel(ruleId, ruleType, ruleValue, null, ruleDuration, ruleMeasurementValue); checkParametersNotEmpty(ruleId, ruleType, ruleValue, ruleDuration, ruleMeasurementValue, errors, lineNumber); checkRuleDuration(fileRulesModel, errors, lineNumber); if (ruleIdSet.contains(ruleId)) { errors .add(new ErrorReport(FileRulesErrorCode.STP_IMPORT_RULES_RULEID_DUPLICATION, lineNumber, fileRulesModel)); } ruleIdSet.add(ruleId); if (!containsRuleMeasurement(ruleMeasurementValue)) { errors.add(new ErrorReport(FileRulesErrorCode.STP_IMPORT_RULES_WRONG_RULEMEASUREMENT, lineNumber, fileRulesModel)); } if (!containsRuleType(ruleType)) { errors.add(new ErrorReport(FileRulesErrorCode.STP_IMPORT_RULES_WRONG_RULETYPE_UNKNOW, lineNumber, fileRulesModel)); } if (checkAssociationRuleDurationRuleMeasurementLimit(record, errors, lineNumber, fileRulesModel)) { checkRuleDurationWithConfiguration(record, errors, lineNumber, fileRulesModel); } if (errors.size() > 0) { errorsMap.put(lineNumber, errors); } } } } catch (IllegalArgumentException e) { String message = e.getMessage(); if (message.contains(FileRulesModel.TAG_RULE_ID + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_ID; } else if (message.contains(FileRulesModel.TAG_RULE_TYPE + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_TYPE; } else if (message.contains(FileRulesModel.TAG_RULE_VALUE + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_VALUE; } else if (message.contains(FileRulesModel.TAG_RULE_DURATION + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_DURATION; } else if (message.contains(FileRulesModel.TAG_RULE_DESCRIPTION + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_DESCRIPTION; } else if (message.contains(FileRulesModel.TAG_RULE_MEASUREMENT + " not found")) { message = ReportConstants.FILE_INVALID + FileRulesModel.TAG_RULE_MEASUREMENT; } List<ErrorReport> errors = new ArrayList<>(); errors .add(new ErrorReport(FileRulesErrorCode.STP_IMPORT_RULES_NOT_CSV_FORMAT, lineNumber, message)); errorsMap.put(lineNumber, errors); throw new FileRulesCsvException(message); } catch (Exception e) { throw new ReferentialException(e); } } if (csvFileReader != null) { final ArrayNode readRulesAsJson = RulesManagerParser.readObjectsFromCsvWriteAsArrayNode(csvFileReader); checkRulesLinkedToAu(readRulesAsJson, usedDeletedRules, usedUpdatedRules, usedUpdateRulesForUpdateUnit, insertRules, notUsedDeletedRules, notUsedUpdatedRules); if (errorsMap.size() > 0) { for (List<ErrorReport> map : errorsMap.values()) { for (ErrorReport error : map) { if (error.getCode().equals(FileRulesErrorCode.STP_IMPORT_RULES_RULEDURATION_EXCEED)) { throw new FileRulesDurationException(RULE_DURATION_EXCEED); } } } throw new FileRulesCsvException(INVALID_CSV_FILE); } if (!usedDeletedRules.isEmpty()) { throw new FileRulesDeleteException("used Rules want to be deleted"); } if (!usedUpdateRulesForUpdateUnit.isEmpty()) { throw new FileRulesUpdateException("used Rules want to be updated"); } Files.delete(csvFileReader.toPath()); return readRulesAsJson; } throw new FileRulesException(INVALID_CSV_FILE); } RulesManagerFileImpl(MongoDbAccessAdminImpl dbConfiguration, VitamCounterService vitamCounterService,
        OntologyLoader ontologyLoader, VitamRuleService vitamRuleService); @VisibleForTesting  RulesManagerFileImpl(MongoDbAccessAdminImpl dbConfiguration,
        VitamCounterService vitamCounterService,
        FunctionalBackupService backupService, LogbookOperationsClientFactory logbookOperationsClientFactory,
        MetaDataClientFactory metaDataClientFactory,
        ProcessingManagementClientFactory processingManagementClientFactory,
        WorkspaceClientFactory workspaceClientFactory,
        OntologyLoader ontologyLoader,
        VitamRuleService vitamRuleService); @Override void importFile(InputStream rulesFileStream, String filename); ArrayNode checkFile(InputStream rulesFileStream, Map<Integer, List<ErrorReport>> errorsMap,
        List<FileRulesModel> usedDeletedRules, List<FileRulesModel> usedUpdatedRules,
        List<FileRulesModel> usedUpdateRulesForUpdateUnit, List<FileRulesModel> insertRules,
        Set<String> notUsedDeletedRules, Set<String> notUsedUpdatedRules); @Override FileRules findDocumentById(String id); @Override RequestResponseOK<FileRules> findDocuments(JsonNode select); InputStream generateErrorReport(Map<Integer, List<ErrorReport>> errors,
        List<FileRulesModel> usedDeletedRules, List<FileRulesModel> usedUpdatedRules,
        StatusCode status, GUID eipMaster); ArrayNode getRuleFromCollection(int tenant); ArrayNode getRuleFromOffer(int tenant); boolean checkRuleConformity(ArrayNode array1, ArrayNode array2, int tenant);  }