### Question:
CausalClusteringBackupStrategy extends LifecycleAdapter implements BackupStrategy { @Override public Fallible<BackupStageOutcome> performFullBackup( DatabaseLayout targetDatabaseLayout, Config config, OptionalHostnamePort userProvidedAddress ) { AdvertisedSocketAddress fromAddress = addressResolver.resolveCorrectCCAddress( config, userProvidedAddress ); log.info( "Resolved address for catchup protocol is " + fromAddress ); StoreId storeId; try { storeId = backupDelegator.fetchStoreId( fromAddress ); log.info( "Remote store id is " + storeId ); } catch ( StoreIdDownloadFailedException e ) { return new Fallible<>( BackupStageOutcome.WRONG_PROTOCOL, e ); } Optional<StoreId> expectedStoreId = readLocalStoreId( targetDatabaseLayout ); if ( expectedStoreId.isPresent() ) { return new Fallible<>( BackupStageOutcome.FAILURE, new StoreIdDownloadFailedException( format( "Cannot perform a full backup onto preexisting backup. Remote store id was %s but local is %s", storeId, expectedStoreId ) ) ); } try { backupDelegator.copy( fromAddress, storeId, targetDatabaseLayout ); return new Fallible<>( BackupStageOutcome.SUCCESS, null ); } catch ( StoreCopyFailedException e ) { return new Fallible<>( BackupStageOutcome.FAILURE, e ); } } CausalClusteringBackupStrategy( BackupDelegator backupDelegator, AddressResolver addressResolver, LogProvider logProvider, StoreFiles storeFiles ); @Override Fallible<BackupStageOutcome> performFullBackup( DatabaseLayout targetDatabaseLayout, Config config,
                                                           OptionalHostnamePort userProvidedAddress ); @Override Fallible<BackupStageOutcome> performIncrementalBackup( DatabaseLayout databaseLayout, Config config,
                                                                  OptionalHostnamePort userProvidedAddress ); @Override void start(); @Override void stop();  }### Answer:
@Test public void fullBackupUsesCorrectResolvedAddress() throws StoreIdDownloadFailedException { AdvertisedSocketAddress expectedAddress = new AdvertisedSocketAddress( "expected-host", 1578 ); when( addressResolver.resolveCorrectCCAddress( any(), any() ) ).thenReturn( expectedAddress ); subject.performFullBackup( desiredBackupLayout, config, userProvidedAddress ); verify( backupDelegator ).fetchStoreId( expectedAddress ); }
@Test public void fullRunsRetrieveStoreWithTargetsStoreId() throws StoreIdDownloadFailedException, StoreCopyFailedException, IOException { when( storeFiles.readStoreId( any() ) ).thenThrow( IOException.class ); subject.performFullBackup( desiredBackupLayout, config, userProvidedAddress ); verify( backupDelegator ).fetchStoreId( resolvedFromAddress ); verify( backupDelegator ).copy( resolvedFromAddress, expectedStoreId, desiredBackupLayout ); }
@Test public void failingToRetrieveStoreIdCausesFailWithStatus_fullBackup() throws StoreIdDownloadFailedException { StoreIdDownloadFailedException storeIdDownloadFailedException = new StoreIdDownloadFailedException( "Expected description" ); when( backupDelegator.fetchStoreId( any() ) ).thenThrow( storeIdDownloadFailedException ); Fallible state = subject.performFullBackup( desiredBackupLayout, config, userProvidedAddress ); assertEquals( BackupStageOutcome.WRONG_PROTOCOL, state.getState() ); assertEquals( storeIdDownloadFailedException, state.getCause().get() ); }
@Test public void failingToCopyStoresCausesFailWithStatus_fullBackup() throws StoreCopyFailedException, IOException { doThrow( StoreCopyFailedException.class ).when( backupDelegator ).copy( any(), any(), any() ); when( storeFiles.readStoreId( any() ) ).thenThrow( IOException.class ); Fallible state = subject.performFullBackup( desiredBackupLayout, config, userProvidedAddress ); assertEquals( BackupStageOutcome.FAILURE, state.getState() ); System.out.println( state.getCause() ); assertEquals( StoreCopyFailedException.class, state.getCause().get().getClass() ); }
@Test public void fullBackupFailsWhenTargetHasStoreId() throws IOException { when( storeFiles.readStoreId( any() ) ).thenReturn( expectedStoreId ); Fallible<BackupStageOutcome> state = subject.performFullBackup( desiredBackupLayout, config, userProvidedAddress ); assertEquals( StoreIdDownloadFailedException.class, state.getCause().get().getClass() ); assertEquals( BackupStageOutcome.FAILURE, state.getState() ); }