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/07/11 12:46:45 UTC

[james-project] 01/04: JAMES-2811 Retrieve the last instant a bucket is responsible of

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 a4c3423866a9fafce5b023b8b4e07003d4f94b7b
Author: Tran Tien Duc <dt...@linagora.com>
AuthorDate: Wed Jul 10 11:54:07 2019 +0700

    JAMES-2811 Retrieve the last instant a bucket is responsible of
    
    Because this component is the place contains a logic to generate bucket names.
    It should be able to extract the date time information from bucket names
    which are made by its self
---
 mailbox/plugin/deleted-messages-vault/pom.xml      |  5 +++
 .../james/vault/blob/BucketNameGenerator.java      | 31 +++++++++++++++--
 .../james/vault/blob/BucketNameGeneratorTest.java  | 39 ++++++++++++++++++++--
 3 files changed, 69 insertions(+), 6 deletions(-)

diff --git a/mailbox/plugin/deleted-messages-vault/pom.xml b/mailbox/plugin/deleted-messages-vault/pom.xml
index 43828e4..1d37854 100644
--- a/mailbox/plugin/deleted-messages-vault/pom.xml
+++ b/mailbox/plugin/deleted-messages-vault/pom.xml
@@ -123,6 +123,11 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter-params</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.mockito</groupId>
             <artifactId>mockito-core</artifactId>
             <scope>test</scope>
diff --git a/mailbox/plugin/deleted-messages-vault/src/main/java/org/apache/james/vault/blob/BucketNameGenerator.java b/mailbox/plugin/deleted-messages-vault/src/main/java/org/apache/james/vault/blob/BucketNameGenerator.java
index 37931a2..38fd25e 100644
--- a/mailbox/plugin/deleted-messages-vault/src/main/java/org/apache/james/vault/blob/BucketNameGenerator.java
+++ b/mailbox/plugin/deleted-messages-vault/src/main/java/org/apache/james/vault/blob/BucketNameGenerator.java
@@ -21,20 +21,45 @@ package org.apache.james.vault.blob;
 
 import java.time.Clock;
 import java.time.ZonedDateTime;
+import java.time.temporal.ChronoField;
+import java.util.Optional;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.james.blob.api.BucketName;
 
 public class BucketNameGenerator {
+    private static final Pattern BUCKET_NAME_PATTERN = Pattern.compile("deleted-messages-([0-9]{4})-([0-9]{2})-(01)");
+    private static final String BUCKET_NAME_GENRATING_FORMAT = "deleted-messages-%d-%02d-01";
+    private static final int FIRST_DAY_OF_MONTH = 1;
+    private static final int ZERO_HOUR = 0;
+    private static final int ZERO_MINUTE = 0;
+    private static final int ZERO_SECOND = 0;
+    private static final int ZERO_NANOSECOND = 0;
+
     private final Clock clock;
 
     public BucketNameGenerator(Clock clock) {
         this.clock = clock;
     }
 
-    public BucketName currentBucket() {
+    BucketName currentBucket() {
         ZonedDateTime now = ZonedDateTime.now(clock);
-        int month = now.getMonthValue();
+        int month = now.getMonth().get(ChronoField.MONTH_OF_YEAR);
         int year = now.getYear();
-        return BucketName.of(String.format("deletedMessages-%d-%02d-01", year, month));
+        return BucketName.of(String.format(BUCKET_NAME_GENRATING_FORMAT, year, month));
+    }
+
+    Optional<ZonedDateTime> bucketEndTime(BucketName bucketName) {
+        return Optional.of(BUCKET_NAME_PATTERN.matcher(bucketName.asString()))
+            .filter(Matcher::matches)
+            .map(matcher -> {
+                int year = Integer.parseInt(matcher.group(1));
+                int month = Integer.parseInt(matcher.group(2));
+                ZonedDateTime firstDayOfMonth = ZonedDateTime.of(year, month, FIRST_DAY_OF_MONTH, ZERO_HOUR, ZERO_MINUTE,
+                    ZERO_SECOND, ZERO_NANOSECOND, clock.getZone());
+                ZonedDateTime lastInstantOfMonth = firstDayOfMonth.plusMonths(1).minusNanos(1);
+                return lastInstantOfMonth;
+            });
     }
 }
diff --git a/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/blob/BucketNameGeneratorTest.java b/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/blob/BucketNameGeneratorTest.java
index b2a2c6a..2f1e013 100644
--- a/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/blob/BucketNameGeneratorTest.java
+++ b/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/blob/BucketNameGeneratorTest.java
@@ -24,25 +24,58 @@ import static org.assertj.core.api.Assertions.assertThat;
 import java.time.Clock;
 import java.time.Instant;
 import java.time.ZoneId;
+import java.time.ZonedDateTime;
 
 import org.apache.james.blob.api.BucketName;
+import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 
 class BucketNameGeneratorTest {
     private static final Instant NOW = Instant.parse("2007-12-03T10:15:30.00Z");
     private static final Instant DATE_2 = Instant.parse("2007-07-03T10:15:30.00Z");
     private static final Clock CLOCK = Clock.fixed(NOW, ZoneId.of("UTC"));
+    private static final BucketNameGenerator DEFAULT_GENERATOR = new BucketNameGenerator(CLOCK);
     private static final Clock CLOCK_2 = Clock.fixed(DATE_2, ZoneId.of("UTC"));
 
     @Test
     void currentBucketShouldReturnBucketFormattedOnFirstDayOfMonth() {
-        assertThat(new BucketNameGenerator(CLOCK).currentBucket())
-            .isEqualTo(BucketName.of("deletedMessages-2007-12-01"));
+        assertThat(DEFAULT_GENERATOR.currentBucket())
+            .isEqualTo(BucketName.of("deleted-messages-2007-12-01"));
     }
 
     @Test
     void monthShouldBeFormattedWithTwoDigits() {
         assertThat(new BucketNameGenerator(CLOCK_2).currentBucket())
-            .isEqualTo(BucketName.of("deletedMessages-2007-07-01"));
+            .isEqualTo(BucketName.of("deleted-messages-2007-07-01"));
+    }
+
+    @Nested
+    class IsBucketRangeBefore {
+        @ParameterizedTest
+        @ValueSource(strings = {
+            "deleted-Messages-2018-07-01",
+            "deleted-Messages-2018-07-02",
+            "deletedMessages-2018-07-01",
+            "deletedMessages-2018-007-01",
+            "deletedMessages-2018-07-001",
+            "2018-07-01",
+            "2018-01-07",
+            "01-07-2018",
+        })
+        void shouldBeEmptyWhenPassingNonWellFormattedBucketNames(String bucketNameAsString) {
+            BucketName bucketName = BucketName.of(bucketNameAsString);
+
+            assertThat(DEFAULT_GENERATOR.bucketEndTime(bucketName)).isEmpty();
+        }
+
+        @Test
+        void shouldBeFalseWhenBucketNameIsInSameMonthWithPassedTime() {
+            BucketName bucketName = BucketName.of("deleted-messages-2019-07-01");
+
+            assertThat(DEFAULT_GENERATOR.bucketEndTime(bucketName))
+                .contains(ZonedDateTime.parse("2019-07-31T23:59:59.999999999Z[UTC]"));
+        }
     }
 }
\ 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