You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/03/28 03:22:03 UTC
[james-project] 19/23: MAILBOX-388 Add a VaultConfiguration object
This is an automated email from the ASF dual-hosted git repository.
btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 948f85b23dee285728101d80a9c1a2fda3d99488
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Thu Mar 21 12:01:31 2019 +0700
MAILBOX-388 Add a VaultConfiguration object
---
.../apache/james/vault/RetentionConfiguration.java | 68 ++++++++++++++++++
.../james/vault/RetentionConfigurationTest.java | 81 ++++++++++++++++++++++
2 files changed, 149 insertions(+)
diff --git a/mailbox/plugin/deleted-messages-vault/src/main/java/org/apache/james/vault/RetentionConfiguration.java b/mailbox/plugin/deleted-messages-vault/src/main/java/org/apache/james/vault/RetentionConfiguration.java
new file mode 100644
index 0000000..93dd6b6
--- /dev/null
+++ b/mailbox/plugin/deleted-messages-vault/src/main/java/org/apache/james/vault/RetentionConfiguration.java
@@ -0,0 +1,68 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.vault;
+
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
+import java.util.Objects;
+import java.util.Optional;
+
+import org.apache.commons.configuration.Configuration;
+import org.apache.james.util.DurationParser;
+
+import com.google.common.base.Preconditions;
+
+public class RetentionConfiguration {
+ public static final RetentionConfiguration DEFAULT = new RetentionConfiguration(ChronoUnit.YEARS.getDuration());
+
+ public static RetentionConfiguration from(Configuration propertiesConfiguration) {
+ return Optional.ofNullable(propertiesConfiguration.getString("retentionPeriod"))
+ .map(string -> DurationParser.parse(string, ChronoUnit.DAYS))
+ .map(RetentionConfiguration::new)
+ .orElse(DEFAULT);
+ }
+
+ private final Duration retentionPeriod;
+
+ RetentionConfiguration(Duration retentionPeriod) {
+ Preconditions.checkNotNull(retentionPeriod);
+
+ this.retentionPeriod = retentionPeriod;
+ }
+
+ public Duration getRetentionPeriod() {
+ return retentionPeriod;
+ }
+
+ @Override
+ public final boolean equals(Object o) {
+ if (o instanceof RetentionConfiguration) {
+ RetentionConfiguration that = (RetentionConfiguration) o;
+
+ return Objects.equals(this.retentionPeriod, that.retentionPeriod);
+ }
+ return false;
+ }
+
+ @Override
+ public final int hashCode() {
+ return Objects.hash(retentionPeriod);
+ }
+}
diff --git a/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/RetentionConfigurationTest.java b/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/RetentionConfigurationTest.java
new file mode 100644
index 0000000..7a15011
--- /dev/null
+++ b/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/RetentionConfigurationTest.java
@@ -0,0 +1,81 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.vault;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.time.Duration;
+
+import org.apache.commons.configuration.PropertiesConfiguration;
+import org.junit.jupiter.api.Test;
+
+import nl.jqno.equalsverifier.EqualsVerifier;
+
+class RetentionConfigurationTest {
+ @Test
+ void ShouldMatchBeanContract() {
+ EqualsVerifier.forClass(RetentionConfiguration.class)
+ .verify();
+ }
+
+ @Test
+ void constructorShouldThrowWhenNull() {
+ assertThatThrownBy(() -> new RetentionConfiguration(null))
+ .isInstanceOf(NullPointerException.class);
+ }
+
+ @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);
+ }
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org