RetentionConfiguration { public static RetentionConfiguration from(Configuration propertiesConfiguration) { return Optional.ofNullable(propertiesConfiguration.getString("retentionPeriod")) .map(string -> DurationParser.parse(string, ChronoUnit.DAYS)) .map(RetentionConfiguration::new) .orElse(DEFAULT); } RetentionConfiguration(Duration retentionPeriod); static RetentionConfiguration from(Configuration propertiesConfiguration); Duration getRetentionPeriod(); @Override final boolean equals(Object o); @Override final int hashCode(); static final RetentionConfiguration DEFAULT; }
@Test void fromShouldThrowWhenNull() { assertThatThrownBy(() -> RetentionConfiguration.from(null)) .isInstanceOf(NullPointerException.class); }
@Test void fromShouldReturnConfiguredRetentionTime() { PropertiesConfiguration configuration = new PropertiesConfiguration(); configuration.addProperty("retentionPeriod", "15d"); assertThat(RetentionConfiguration.from(configuration)).isEqualTo(new RetentionConfiguration(Duration.ofDays(15))); }
@Test void fromShouldHandleHours() { PropertiesConfiguration configuration = new PropertiesConfiguration(); configuration.addProperty("retentionPeriod", "15h"); assertThat(RetentionConfiguration.from(configuration)).isEqualTo(new RetentionConfiguration(Duration.ofHours(15))); }
@Test void fromShouldUseDaysAsADefaultUnit() { PropertiesConfiguration configuration = new PropertiesConfiguration(); configuration.addProperty("retentionPeriod", "15"); assertThat(RetentionConfiguration.from(configuration)).isEqualTo(new RetentionConfiguration(Duration.ofDays(15))); }
@Test void fromShouldReturnDefaultWhenNoRetentionTime() { PropertiesConfiguration configuration = new PropertiesConfiguration(); assertThat(RetentionConfiguration.from(configuration)).isEqualTo(RetentionConfiguration.DEFAULT); }