@Test public void cleansUpIfExceptionThrown() { final Config config = mock(Config.class); final KeyConfiguration keyConfiguration = mock(KeyConfiguration.class); when(config.getKeys()).thenReturn(keyConfiguration); when(keyConfiguration.getKeyData()).thenReturn(new ArrayList<>()); when(filesDelegate.newOutputStream(any(), any())).thenThrow(new RuntimeException()); final Path configDest = mock(Path.class); final Throwable ex = catchThrowable(() -> writer.updateAndWrite(new ArrayList<>(), null, config, configDest)); assertThat(ex).isNotNull(); verify(filesDelegate).deleteIfExists(configDest); }
public void updateAndWrite( List<KeyData> newKeys, KeyVaultConfig keyVaultConfig, Config config, Path configDest) throws IOException { LOGGER.info("Writing updated config to {}", configDest); update(newKeys, keyVaultConfig, config); try (OutputStream out = filesDelegate.newOutputStream(configDest, CREATE_NEW)) { JaxbUtil.marshal(config, out); LOGGER.info("Updated config written to {}", configDest); } catch (Exception e) { LOGGER.info("Writing updated config failed, cleaning up: deleting {}", configDest); filesDelegate.deleteIfExists(configDest); throw e; } }
ConfigFileUpdaterWriter { public void updateAndWrite( List<KeyData> newKeys, KeyVaultConfig keyVaultConfig, Config config, Path configDest) throws IOException { LOGGER.info("Writing updated config to {}", configDest); update(newKeys, keyVaultConfig, config); try (OutputStream out = filesDelegate.newOutputStream(configDest, CREATE_NEW)) { JaxbUtil.marshal(config, out); LOGGER.info("Updated config written to {}", configDest); } catch (Exception e) { LOGGER.info("Writing updated config failed, cleaning up: deleting {}", configDest); filesDelegate.deleteIfExists(configDest); throw e; } } }
ConfigFileUpdaterWriter { public void updateAndWrite( List<KeyData> newKeys, KeyVaultConfig keyVaultConfig, Config config, Path configDest) throws IOException { LOGGER.info("Writing updated config to {}", configDest); update(newKeys, keyVaultConfig, config); try (OutputStream out = filesDelegate.newOutputStream(configDest, CREATE_NEW)) { JaxbUtil.marshal(config, out); LOGGER.info("Updated config written to {}", configDest); } catch (Exception e) { LOGGER.info("Writing updated config failed, cleaning up: deleting {}", configDest); filesDelegate.deleteIfExists(configDest); throw e; } } ConfigFileUpdaterWriter(FilesDelegate filesDelegate); }
ConfigFileUpdaterWriter { public void updateAndWrite( List<KeyData> newKeys, KeyVaultConfig keyVaultConfig, Config config, Path configDest) throws IOException { LOGGER.info("Writing updated config to {}", configDest); update(newKeys, keyVaultConfig, config); try (OutputStream out = filesDelegate.newOutputStream(configDest, CREATE_NEW)) { JaxbUtil.marshal(config, out); LOGGER.info("Updated config written to {}", configDest); } catch (Exception e) { LOGGER.info("Writing updated config failed, cleaning up: deleting {}", configDest); filesDelegate.deleteIfExists(configDest); throw e; } } ConfigFileUpdaterWriter(FilesDelegate filesDelegate); void updateAndWrite(
        List<KeyData> newKeys, KeyVaultConfig keyVaultConfig, Config config, Path configDest); void updateAndWriteToCLI(List<KeyData> newKeys, KeyVaultConfig keyVaultConfig, Config config); }
ConfigFileUpdaterWriter { public void updateAndWrite( List<KeyData> newKeys, KeyVaultConfig keyVaultConfig, Config config, Path configDest) throws IOException { LOGGER.info("Writing updated config to {}", configDest); update(newKeys, keyVaultConfig, config); try (OutputStream out = filesDelegate.newOutputStream(configDest, CREATE_NEW)) { JaxbUtil.marshal(config, out); LOGGER.info("Updated config written to {}", configDest); } catch (Exception e) { LOGGER.info("Writing updated config failed, cleaning up: deleting {}", configDest); filesDelegate.deleteIfExists(configDest); throw e; } } ConfigFileUpdaterWriter(FilesDelegate filesDelegate); void updateAndWrite(
        List<KeyData> newKeys, KeyVaultConfig keyVaultConfig, Config config, Path configDest); void updateAndWriteToCLI(List<KeyData> newKeys, KeyVaultConfig keyVaultConfig, Config config);  }