@Test @RunWithCustomExecutor public void should_not_update_when_update_with_no_changes() throws Exception { LogbookOperationsClient logbookOperationsclient = mock(LogbookOperationsClient.class); when(logbookOperationsClientFactory.getClient()).thenReturn(logbookOperationsclient); when(logbookOperationsclient.selectOperation(any())) .thenReturn(getJsonResult(StatusCode.OK.name(), TENANT_ID)); Path reportPath = Paths.get(tempFolder.newFolder().getAbsolutePath(), "report_agencies.json"); doAnswer(arguments -> { Files.copy(arguments.<InputStream>getArgument(0), reportPath); return null; }).when(functionalBackupService).saveFile(any(InputStream.class), any(GUID.class), eq(AGENCIES_REPORT), eq(DataCategory.REPORT), endsWith(".json")); RequestResponse<AgenciesModel> response = agencyService .importAgencies(new FileInputStream(getResourceFile("agencies_no_changes.csv")), "MY-FILE-NAME"); JsonNode report = getFromFile(reportPath.toFile()); assertNotNull(report); assertThat(report.has("InsertAgencies")).isFalse(); assertThat(report.has("UpdatedAgencies")).isFalse(); }
public RequestResponse<AgenciesModel> importAgencies(InputStream stream, String filename) throws VitamException, IOException { manager.logStarted(AGENCIES_IMPORT_EVENT); InputStream reportStream; File file = null; InputStream csvFileInputStream = null; try { file = convertInputStreamToFile(stream); try (FileInputStream inputStream = new FileInputStream(file)) { checkFile(inputStream); } checkAgenciesInDb(); createInsertUpdateDeleteList(); checkAgenciesDeletion(); findAllAgenciesUsedByAccessContracts(); findAllAgenciesUsedByUnits(); commitAgencies(); reportStream = generateReportOK(); backupService.saveFile(reportStream, eip, AGENCIES_REPORT_EVENT, DataCategory.REPORT, eip + ".json"); csvFileInputStream = new FileInputStream(file); backupService.saveFile(csvFileInputStream, eip, IMPORT_AGENCIES_BACKUP_CSV, DataCategory.REPORT, eip + ".csv"); backupService.saveCollectionAndSequence(eip, AGENCIES_BACKUP_EVENT, FunctionalAdminCollections.AGENCIES, eip.toString()); manager.logFinish(filename); } catch (final AgencyImportDeletionException e) { LOGGER.error(MESSAGE_ERROR, e); InputStream errorStream = generateErrorReport(); backupService.saveFile(errorStream, eip, AGENCIES_REPORT_EVENT, DataCategory.REPORT, eip + ".json"); errorStream.close(); ObjectNode errorMessage = JsonHandler.createObjectNode(); String listAgencies = agenciesToDelete.stream().map(AgenciesModel::getIdentifier) .collect(Collectors.joining(",")); errorMessage.put("Agencies ", listAgencies); return generateVitamBadRequestError(errorMessage.toString()); } catch (SchemaValidationException | BadRequestException e) { LOGGER.error(MESSAGE_ERROR, e); InputStream errorStream = generateErrorReport(); backupService.saveFile(errorStream, eip, AGENCIES_REPORT_EVENT, DataCategory.REPORT, eip + ".json"); errorStream.close(); return getVitamError(VitamCode.AGENCIES_VALIDATION_ERROR.getItem(), e.getMessage() ).setHttpCode(Response.Status.BAD_REQUEST.getStatusCode()); } catch (final Exception e) { LOGGER.error(MESSAGE_ERROR, e); InputStream errorStream = generateErrorReport(); backupService.saveFile(errorStream, eip, AGENCIES_REPORT_EVENT, DataCategory.REPORT, eip + ".json"); errorStream.close(); return generateVitamError(MESSAGE_ERROR + e.getMessage()); } finally { IOUtils.closeQuietly(csvFileInputStream); if (file != null && !file.delete()) { LOGGER.warn("Failed to delete file"); } } return new RequestResponseOK<AgenciesModel>().setHttpCode(Response.Status.CREATED.getStatusCode()); }
AgenciesService implements VitamAutoCloseable { public RequestResponse<AgenciesModel> importAgencies(InputStream stream, String filename) throws VitamException, IOException { manager.logStarted(AGENCIES_IMPORT_EVENT); InputStream reportStream; File file = null; InputStream csvFileInputStream = null; try { file = convertInputStreamToFile(stream); try (FileInputStream inputStream = new FileInputStream(file)) { checkFile(inputStream); } checkAgenciesInDb(); createInsertUpdateDeleteList(); checkAgenciesDeletion(); findAllAgenciesUsedByAccessContracts(); findAllAgenciesUsedByUnits(); commitAgencies(); reportStream = generateReportOK(); backupService.saveFile(reportStream, eip, AGENCIES_REPORT_EVENT, DataCategory.REPORT, eip + ".json"); csvFileInputStream = new FileInputStream(file); backupService.saveFile(csvFileInputStream, eip, IMPORT_AGENCIES_BACKUP_CSV, DataCategory.REPORT, eip + ".csv"); backupService.saveCollectionAndSequence(eip, AGENCIES_BACKUP_EVENT, FunctionalAdminCollections.AGENCIES, eip.toString()); manager.logFinish(filename); } catch (final AgencyImportDeletionException e) { LOGGER.error(MESSAGE_ERROR, e); InputStream errorStream = generateErrorReport(); backupService.saveFile(errorStream, eip, AGENCIES_REPORT_EVENT, DataCategory.REPORT, eip + ".json"); errorStream.close(); ObjectNode errorMessage = JsonHandler.createObjectNode(); String listAgencies = agenciesToDelete.stream().map(AgenciesModel::getIdentifier) .collect(Collectors.joining(",")); errorMessage.put("Agencies ", listAgencies); return generateVitamBadRequestError(errorMessage.toString()); } catch (SchemaValidationException | BadRequestException e) { LOGGER.error(MESSAGE_ERROR, e); InputStream errorStream = generateErrorReport(); backupService.saveFile(errorStream, eip, AGENCIES_REPORT_EVENT, DataCategory.REPORT, eip + ".json"); errorStream.close(); return getVitamError(VitamCode.AGENCIES_VALIDATION_ERROR.getItem(), e.getMessage() ).setHttpCode(Response.Status.BAD_REQUEST.getStatusCode()); } catch (final Exception e) { LOGGER.error(MESSAGE_ERROR, e); InputStream errorStream = generateErrorReport(); backupService.saveFile(errorStream, eip, AGENCIES_REPORT_EVENT, DataCategory.REPORT, eip + ".json"); errorStream.close(); return generateVitamError(MESSAGE_ERROR + e.getMessage()); } finally { IOUtils.closeQuietly(csvFileInputStream); if (file != null && !file.delete()) { LOGGER.warn("Failed to delete file"); } } return new RequestResponseOK<AgenciesModel>().setHttpCode(Response.Status.CREATED.getStatusCode()); } }
AgenciesService implements VitamAutoCloseable { public RequestResponse<AgenciesModel> importAgencies(InputStream stream, String filename) throws VitamException, IOException { manager.logStarted(AGENCIES_IMPORT_EVENT); InputStream reportStream; File file = null; InputStream csvFileInputStream = null; try { file = convertInputStreamToFile(stream); try (FileInputStream inputStream = new FileInputStream(file)) { checkFile(inputStream); } checkAgenciesInDb(); createInsertUpdateDeleteList(); checkAgenciesDeletion(); findAllAgenciesUsedByAccessContracts(); findAllAgenciesUsedByUnits(); commitAgencies(); reportStream = generateReportOK(); backupService.saveFile(reportStream, eip, AGENCIES_REPORT_EVENT, DataCategory.REPORT, eip + ".json"); csvFileInputStream = new FileInputStream(file); backupService.saveFile(csvFileInputStream, eip, IMPORT_AGENCIES_BACKUP_CSV, DataCategory.REPORT, eip + ".csv"); backupService.saveCollectionAndSequence(eip, AGENCIES_BACKUP_EVENT, FunctionalAdminCollections.AGENCIES, eip.toString()); manager.logFinish(filename); } catch (final AgencyImportDeletionException e) { LOGGER.error(MESSAGE_ERROR, e); InputStream errorStream = generateErrorReport(); backupService.saveFile(errorStream, eip, AGENCIES_REPORT_EVENT, DataCategory.REPORT, eip + ".json"); errorStream.close(); ObjectNode errorMessage = JsonHandler.createObjectNode(); String listAgencies = agenciesToDelete.stream().map(AgenciesModel::getIdentifier) .collect(Collectors.joining(",")); errorMessage.put("Agencies ", listAgencies); return generateVitamBadRequestError(errorMessage.toString()); } catch (SchemaValidationException | BadRequestException e) { LOGGER.error(MESSAGE_ERROR, e); InputStream errorStream = generateErrorReport(); backupService.saveFile(errorStream, eip, AGENCIES_REPORT_EVENT, DataCategory.REPORT, eip + ".json"); errorStream.close(); return getVitamError(VitamCode.AGENCIES_VALIDATION_ERROR.getItem(), e.getMessage() ).setHttpCode(Response.Status.BAD_REQUEST.getStatusCode()); } catch (final Exception e) { LOGGER.error(MESSAGE_ERROR, e); InputStream errorStream = generateErrorReport(); backupService.saveFile(errorStream, eip, AGENCIES_REPORT_EVENT, DataCategory.REPORT, eip + ".json"); errorStream.close(); return generateVitamError(MESSAGE_ERROR + e.getMessage()); } finally { IOUtils.closeQuietly(csvFileInputStream); if (file != null && !file.delete()) { LOGGER.warn("Failed to delete file"); } } return new RequestResponseOK<AgenciesModel>().setHttpCode(Response.Status.CREATED.getStatusCode()); } 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 RequestResponse<AgenciesModel> importAgencies(InputStream stream, String filename) throws VitamException, IOException { manager.logStarted(AGENCIES_IMPORT_EVENT); InputStream reportStream; File file = null; InputStream csvFileInputStream = null; try { file = convertInputStreamToFile(stream); try (FileInputStream inputStream = new FileInputStream(file)) { checkFile(inputStream); } checkAgenciesInDb(); createInsertUpdateDeleteList(); checkAgenciesDeletion(); findAllAgenciesUsedByAccessContracts(); findAllAgenciesUsedByUnits(); commitAgencies(); reportStream = generateReportOK(); backupService.saveFile(reportStream, eip, AGENCIES_REPORT_EVENT, DataCategory.REPORT, eip + ".json"); csvFileInputStream = new FileInputStream(file); backupService.saveFile(csvFileInputStream, eip, IMPORT_AGENCIES_BACKUP_CSV, DataCategory.REPORT, eip + ".csv"); backupService.saveCollectionAndSequence(eip, AGENCIES_BACKUP_EVENT, FunctionalAdminCollections.AGENCIES, eip.toString()); manager.logFinish(filename); } catch (final AgencyImportDeletionException e) { LOGGER.error(MESSAGE_ERROR, e); InputStream errorStream = generateErrorReport(); backupService.saveFile(errorStream, eip, AGENCIES_REPORT_EVENT, DataCategory.REPORT, eip + ".json"); errorStream.close(); ObjectNode errorMessage = JsonHandler.createObjectNode(); String listAgencies = agenciesToDelete.stream().map(AgenciesModel::getIdentifier) .collect(Collectors.joining(",")); errorMessage.put("Agencies ", listAgencies); return generateVitamBadRequestError(errorMessage.toString()); } catch (SchemaValidationException | BadRequestException e) { LOGGER.error(MESSAGE_ERROR, e); InputStream errorStream = generateErrorReport(); backupService.saveFile(errorStream, eip, AGENCIES_REPORT_EVENT, DataCategory.REPORT, eip + ".json"); errorStream.close(); return getVitamError(VitamCode.AGENCIES_VALIDATION_ERROR.getItem(), e.getMessage() ).setHttpCode(Response.Status.BAD_REQUEST.getStatusCode()); } catch (final Exception e) { LOGGER.error(MESSAGE_ERROR, e); InputStream errorStream = generateErrorReport(); backupService.saveFile(errorStream, eip, AGENCIES_REPORT_EVENT, DataCategory.REPORT, eip + ".json"); errorStream.close(); return generateVitamError(MESSAGE_ERROR + e.getMessage()); } finally { IOUtils.closeQuietly(csvFileInputStream); if (file != null && !file.delete()) { LOGGER.warn("Failed to delete file"); } } return new RequestResponseOK<AgenciesModel>().setHttpCode(Response.Status.CREATED.getStatusCode()); } 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 RequestResponse<AgenciesModel> importAgencies(InputStream stream, String filename) throws VitamException, IOException { manager.logStarted(AGENCIES_IMPORT_EVENT); InputStream reportStream; File file = null; InputStream csvFileInputStream = null; try { file = convertInputStreamToFile(stream); try (FileInputStream inputStream = new FileInputStream(file)) { checkFile(inputStream); } checkAgenciesInDb(); createInsertUpdateDeleteList(); checkAgenciesDeletion(); findAllAgenciesUsedByAccessContracts(); findAllAgenciesUsedByUnits(); commitAgencies(); reportStream = generateReportOK(); backupService.saveFile(reportStream, eip, AGENCIES_REPORT_EVENT, DataCategory.REPORT, eip + ".json"); csvFileInputStream = new FileInputStream(file); backupService.saveFile(csvFileInputStream, eip, IMPORT_AGENCIES_BACKUP_CSV, DataCategory.REPORT, eip + ".csv"); backupService.saveCollectionAndSequence(eip, AGENCIES_BACKUP_EVENT, FunctionalAdminCollections.AGENCIES, eip.toString()); manager.logFinish(filename); } catch (final AgencyImportDeletionException e) { LOGGER.error(MESSAGE_ERROR, e); InputStream errorStream = generateErrorReport(); backupService.saveFile(errorStream, eip, AGENCIES_REPORT_EVENT, DataCategory.REPORT, eip + ".json"); errorStream.close(); ObjectNode errorMessage = JsonHandler.createObjectNode(); String listAgencies = agenciesToDelete.stream().map(AgenciesModel::getIdentifier) .collect(Collectors.joining(",")); errorMessage.put("Agencies ", listAgencies); return generateVitamBadRequestError(errorMessage.toString()); } catch (SchemaValidationException | BadRequestException e) { LOGGER.error(MESSAGE_ERROR, e); InputStream errorStream = generateErrorReport(); backupService.saveFile(errorStream, eip, AGENCIES_REPORT_EVENT, DataCategory.REPORT, eip + ".json"); errorStream.close(); return getVitamError(VitamCode.AGENCIES_VALIDATION_ERROR.getItem(), e.getMessage() ).setHttpCode(Response.Status.BAD_REQUEST.getStatusCode()); } catch (final Exception e) { LOGGER.error(MESSAGE_ERROR, e); InputStream errorStream = generateErrorReport(); backupService.saveFile(errorStream, eip, AGENCIES_REPORT_EVENT, DataCategory.REPORT, eip + ".json"); errorStream.close(); return generateVitamError(MESSAGE_ERROR + e.getMessage()); } finally { IOUtils.closeQuietly(csvFileInputStream); if (file != null && !file.delete()) { LOGGER.warn("Failed to delete file"); } } return new RequestResponseOK<AgenciesModel>().setHttpCode(Response.Status.CREATED.getStatusCode()); } 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; }