@Test public void should_save_collection_and_sequence() throws Exception { final FunctionalAdminCollections agencies = FunctionalAdminCollections.AGENCIES; List<String> savedDocCapture = new ArrayList<>(); doAnswer(invocation -> { savedDocCapture .add(IOUtils.toString(((InputStream) invocation.getArguments()[0]), StandardCharsets.UTF_8)); return null; }).when(backupService).backup(any(), any(), anyString()); VitamThreadUtils.getVitamSession().setTenantId(0); GUID guid = newEventGUID(0); functionalBackupService.saveCollectionAndSequence(guid, "STP_TEST", agencies, guid.toString()); ArgumentCaptor<String> hashArgCaptor = ArgumentCaptor.forClass(String.class); verify(backupLogbookManager) .logEventSuccess(eq(guid), eq("STP_TEST"), hashArgCaptor.capture(), eq("0_" + PREFIX + "Agencies_10.json"), any()); String expectedDump = "{\"collection\":[" + DOC1_TENANT0 + "],\"sequence\":" + SEQUENCE_DOC + ",\"backup_sequence\":" + BACKUP_SEQUENCE_DOC + "}"; String expectedDigest = new Digest(VitamConfiguration.getDefaultDigestType()).update(expectedDump).digestHex(); assertThat(savedDocCapture).hasSize(1); assertThat(savedDocCapture.get(0)).isEqualTo(expectedDump); assertThat(hashArgCaptor.getValue()).isEqualTo(expectedDigest); }
public void saveCollectionAndSequence(GUID eipMaster, String eventCode, FunctionalAdminCollections collection, String objectIdentifier) throws VitamException { try { int initialTenant = ParameterHelper.getTenantParameter(); int storageTenant = getStorageTenant(collection, initialTenant); VitamSequence backupSequence = vitamCounterService .getNextBackupSequenceDocument(storageTenant, fromFunctionalAdminCollections(collection)); String fileName = getBackupFileName(collection, storageTenant, backupSequence.getCounter()); String digestStr; File file = null; try { VitamThreadUtils.getVitamSession().setTenantId(storageTenant); file = saveFunctionalCollectionToTempFile(collection, storageTenant, backupSequence); digestStr = storeBackupFileInStorage(fileName, file); } finally { VitamThreadUtils.getVitamSession().setTenantId(initialTenant); deleteTmpFile(file); } backupLogbookManager.logEventSuccess(eipMaster, eventCode, digestStr, fileName, objectIdentifier); } catch (ReferentialException | IOException | BackupServiceException e) { try { backupLogbookManager.logError(eipMaster, eventCode, e.getMessage()); } catch (VitamException | RuntimeException logbookErrorException) { LOGGER.error("Could not persist backup error in logbook operation", logbookErrorException); } throw new FunctionalBackupServiceException("Could not backup collection " + collection.toString(), e); } catch (Exception e) { try { backupLogbookManager.logError(eipMaster, eventCode, e.getMessage()); } catch (VitamException | RuntimeException logbookErrorException) { LOGGER.error("Could not persist backup error in logbook operation", logbookErrorException); } throw new FunctionalBackupServiceException("Could not backup collection " + collection.toString(), e); } }
FunctionalBackupService { public void saveCollectionAndSequence(GUID eipMaster, String eventCode, FunctionalAdminCollections collection, String objectIdentifier) throws VitamException { try { int initialTenant = ParameterHelper.getTenantParameter(); int storageTenant = getStorageTenant(collection, initialTenant); VitamSequence backupSequence = vitamCounterService .getNextBackupSequenceDocument(storageTenant, fromFunctionalAdminCollections(collection)); String fileName = getBackupFileName(collection, storageTenant, backupSequence.getCounter()); String digestStr; File file = null; try { VitamThreadUtils.getVitamSession().setTenantId(storageTenant); file = saveFunctionalCollectionToTempFile(collection, storageTenant, backupSequence); digestStr = storeBackupFileInStorage(fileName, file); } finally { VitamThreadUtils.getVitamSession().setTenantId(initialTenant); deleteTmpFile(file); } backupLogbookManager.logEventSuccess(eipMaster, eventCode, digestStr, fileName, objectIdentifier); } catch (ReferentialException | IOException | BackupServiceException e) { try { backupLogbookManager.logError(eipMaster, eventCode, e.getMessage()); } catch (VitamException | RuntimeException logbookErrorException) { LOGGER.error("Could not persist backup error in logbook operation", logbookErrorException); } throw new FunctionalBackupServiceException("Could not backup collection " + collection.toString(), e); } catch (Exception e) { try { backupLogbookManager.logError(eipMaster, eventCode, e.getMessage()); } catch (VitamException | RuntimeException logbookErrorException) { LOGGER.error("Could not persist backup error in logbook operation", logbookErrorException); } throw new FunctionalBackupServiceException("Could not backup collection " + collection.toString(), e); } } }
FunctionalBackupService { public void saveCollectionAndSequence(GUID eipMaster, String eventCode, FunctionalAdminCollections collection, String objectIdentifier) throws VitamException { try { int initialTenant = ParameterHelper.getTenantParameter(); int storageTenant = getStorageTenant(collection, initialTenant); VitamSequence backupSequence = vitamCounterService .getNextBackupSequenceDocument(storageTenant, fromFunctionalAdminCollections(collection)); String fileName = getBackupFileName(collection, storageTenant, backupSequence.getCounter()); String digestStr; File file = null; try { VitamThreadUtils.getVitamSession().setTenantId(storageTenant); file = saveFunctionalCollectionToTempFile(collection, storageTenant, backupSequence); digestStr = storeBackupFileInStorage(fileName, file); } finally { VitamThreadUtils.getVitamSession().setTenantId(initialTenant); deleteTmpFile(file); } backupLogbookManager.logEventSuccess(eipMaster, eventCode, digestStr, fileName, objectIdentifier); } catch (ReferentialException | IOException | BackupServiceException e) { try { backupLogbookManager.logError(eipMaster, eventCode, e.getMessage()); } catch (VitamException | RuntimeException logbookErrorException) { LOGGER.error("Could not persist backup error in logbook operation", logbookErrorException); } throw new FunctionalBackupServiceException("Could not backup collection " + collection.toString(), e); } catch (Exception e) { try { backupLogbookManager.logError(eipMaster, eventCode, e.getMessage()); } catch (VitamException | RuntimeException logbookErrorException) { LOGGER.error("Could not persist backup error in logbook operation", logbookErrorException); } throw new FunctionalBackupServiceException("Could not backup collection " + collection.toString(), e); } } FunctionalBackupService(
        VitamCounterService vitamCounterService); @VisibleForTesting  FunctionalBackupService(BackupService backupService,
        VitamCounterService vitamCounterService, BackupLogbookManager backupLogbookManager); }
FunctionalBackupService { public void saveCollectionAndSequence(GUID eipMaster, String eventCode, FunctionalAdminCollections collection, String objectIdentifier) throws VitamException { try { int initialTenant = ParameterHelper.getTenantParameter(); int storageTenant = getStorageTenant(collection, initialTenant); VitamSequence backupSequence = vitamCounterService .getNextBackupSequenceDocument(storageTenant, fromFunctionalAdminCollections(collection)); String fileName = getBackupFileName(collection, storageTenant, backupSequence.getCounter()); String digestStr; File file = null; try { VitamThreadUtils.getVitamSession().setTenantId(storageTenant); file = saveFunctionalCollectionToTempFile(collection, storageTenant, backupSequence); digestStr = storeBackupFileInStorage(fileName, file); } finally { VitamThreadUtils.getVitamSession().setTenantId(initialTenant); deleteTmpFile(file); } backupLogbookManager.logEventSuccess(eipMaster, eventCode, digestStr, fileName, objectIdentifier); } catch (ReferentialException | IOException | BackupServiceException e) { try { backupLogbookManager.logError(eipMaster, eventCode, e.getMessage()); } catch (VitamException | RuntimeException logbookErrorException) { LOGGER.error("Could not persist backup error in logbook operation", logbookErrorException); } throw new FunctionalBackupServiceException("Could not backup collection " + collection.toString(), e); } catch (Exception e) { try { backupLogbookManager.logError(eipMaster, eventCode, e.getMessage()); } catch (VitamException | RuntimeException logbookErrorException) { LOGGER.error("Could not persist backup error in logbook operation", logbookErrorException); } throw new FunctionalBackupServiceException("Could not backup collection " + collection.toString(), e); } } FunctionalBackupService(
        VitamCounterService vitamCounterService); @VisibleForTesting  FunctionalBackupService(BackupService backupService,
        VitamCounterService vitamCounterService, BackupLogbookManager backupLogbookManager); void saveCollectionAndSequence(GUID eipMaster, String eventCode,
        FunctionalAdminCollections collection, String objectIdentifier); void saveDocument(FunctionalAdminCollections collection, Document document); static String getBackupFileName(FunctionalAdminCollections functionalAdminCollections, int tenant,
        Integer sequence); void saveFile(InputStream inputStream, GUID eipMaster, String eventCode,
        DataCategory dataCategory, String fileName); String getName(DataCategory dataCategory, int tenant, String fileName); MongoCursor getCurrentCollection(FunctionalAdminCollections collections, int tenant); ArrayNode getCollectionInJson(MongoCursor mongoCursor); }
FunctionalBackupService { public void saveCollectionAndSequence(GUID eipMaster, String eventCode, FunctionalAdminCollections collection, String objectIdentifier) throws VitamException { try { int initialTenant = ParameterHelper.getTenantParameter(); int storageTenant = getStorageTenant(collection, initialTenant); VitamSequence backupSequence = vitamCounterService .getNextBackupSequenceDocument(storageTenant, fromFunctionalAdminCollections(collection)); String fileName = getBackupFileName(collection, storageTenant, backupSequence.getCounter()); String digestStr; File file = null; try { VitamThreadUtils.getVitamSession().setTenantId(storageTenant); file = saveFunctionalCollectionToTempFile(collection, storageTenant, backupSequence); digestStr = storeBackupFileInStorage(fileName, file); } finally { VitamThreadUtils.getVitamSession().setTenantId(initialTenant); deleteTmpFile(file); } backupLogbookManager.logEventSuccess(eipMaster, eventCode, digestStr, fileName, objectIdentifier); } catch (ReferentialException | IOException | BackupServiceException e) { try { backupLogbookManager.logError(eipMaster, eventCode, e.getMessage()); } catch (VitamException | RuntimeException logbookErrorException) { LOGGER.error("Could not persist backup error in logbook operation", logbookErrorException); } throw new FunctionalBackupServiceException("Could not backup collection " + collection.toString(), e); } catch (Exception e) { try { backupLogbookManager.logError(eipMaster, eventCode, e.getMessage()); } catch (VitamException | RuntimeException logbookErrorException) { LOGGER.error("Could not persist backup error in logbook operation", logbookErrorException); } throw new FunctionalBackupServiceException("Could not backup collection " + collection.toString(), e); } } FunctionalBackupService(
        VitamCounterService vitamCounterService); @VisibleForTesting  FunctionalBackupService(BackupService backupService,
        VitamCounterService vitamCounterService, BackupLogbookManager backupLogbookManager); void saveCollectionAndSequence(GUID eipMaster, String eventCode,
        FunctionalAdminCollections collection, String objectIdentifier); void saveDocument(FunctionalAdminCollections collection, Document document); static String getBackupFileName(FunctionalAdminCollections functionalAdminCollections, int tenant,
        Integer sequence); void saveFile(InputStream inputStream, GUID eipMaster, String eventCode,
        DataCategory dataCategory, String fileName); String getName(DataCategory dataCategory, int tenant, String fileName); MongoCursor getCurrentCollection(FunctionalAdminCollections collections, int tenant); ArrayNode getCollectionInJson(MongoCursor mongoCursor); static final String FIELD_COLLECTION; static final String FIELD_SEQUENCE; static final String FIELD_BACKUP_SEQUENCE; static final String DEFAULT_EXTENSION; }