You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by rc...@apache.org on 2021/08/06 03:26:59 UTC

[james-project] 03/05: JAMES-3544 UploadBucketName POJO

This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 4e13700282d732c2aad2b12167eb2237ef1f3d60
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Wed Aug 4 17:15:01 2021 +0700

    JAMES-3544 UploadBucketName POJO
    
    Holds formatting rules of the BucketNames we will be using.
---
 .../jmap/cassandra/upload/UploadBucketName.java    | 88 ++++++++++++++++++++++
 .../cassandra/upload/UploadBucketNameTest.java     | 76 +++++++++++++++++++
 2 files changed, 164 insertions(+)

diff --git a/server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/upload/UploadBucketName.java b/server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/upload/UploadBucketName.java
new file mode 100644
index 0000000..2e5e9d6
--- /dev/null
+++ b/server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/upload/UploadBucketName.java
@@ -0,0 +1,88 @@
+/****************************************************************
+ * 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.jmap.cassandra.upload;
+
+import java.util.Optional;
+
+import org.apache.james.blob.api.BucketName;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+import com.google.common.base.Preconditions;
+
+public class UploadBucketName {
+    private static final String PREFIX = "uploads-";
+
+    public static Optional<UploadBucketName> ofBucket(BucketName bucketName) {
+        String bucketNameString = bucketName.asString();
+        if (!bucketNameString.startsWith(PREFIX)) {
+            return Optional.empty();
+        }
+        String weekPart = bucketNameString.substring(PREFIX.length());
+        try {
+            int weekNumber = Integer.parseInt(weekPart);
+            if (weekNumber >= 0) {
+                return Optional.of(new UploadBucketName(weekNumber));
+            } else {
+                return Optional.empty();
+            }
+        } catch (Exception e) {
+            return Optional.empty();
+        }
+    }
+
+    private final int weekNumber;
+
+    public UploadBucketName(int weekNumber) {
+        Preconditions.checkArgument(weekNumber >= 0, "'weekNumber' should be strictly positive");
+
+        this.weekNumber = weekNumber;
+    }
+
+    public BucketName asBucketName() {
+        return BucketName.of(String.format(PREFIX + "%d", weekNumber));
+    }
+
+    public int getWeekNumber() {
+        return weekNumber;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (obj instanceof UploadBucketName) {
+            UploadBucketName other = (UploadBucketName) obj;
+            return Objects.equal(weekNumber, other.weekNumber);
+        }
+        return false;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(weekNumber);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects
+            .toStringHelper(this)
+            .add("weekNumber", weekNumber)
+            .toString();
+    }
+}
diff --git a/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/upload/UploadBucketNameTest.java b/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/upload/UploadBucketNameTest.java
new file mode 100644
index 0000000..5a9c789
--- /dev/null
+++ b/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/upload/UploadBucketNameTest.java
@@ -0,0 +1,76 @@
+/****************************************************************
+ * 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.jmap.cassandra.upload;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.apache.james.blob.api.BucketName;
+import org.junit.jupiter.api.Test;
+
+class UploadBucketNameTest {
+    @Test
+    void shouldThrowOnNegativeWeekCount() {
+        assertThatThrownBy(() -> new UploadBucketName(-1))
+            .isInstanceOf(IllegalArgumentException.class);
+    }
+
+    @Test
+    void zeroShouldBeValid() {
+        assertThat(new UploadBucketName(0).getWeekNumber())
+            .isEqualTo(0);
+    }
+
+    @Test
+    void asBucketNameShouldConvertToABucketName() {
+        assertThat(new UploadBucketName(36).asBucketName())
+            .isEqualTo(BucketName.of("uploads-36"));
+    }
+
+    @Test
+    void ofBucketShouldFilterUnrelatedBuckets() {
+        assertThat(UploadBucketName.ofBucket(BucketName.of("bad")))
+            .isEmpty();
+    }
+
+    @Test
+    void ofBucketShouldFilterEmptyWeekCount() {
+        assertThat(UploadBucketName.ofBucket(BucketName.of("uploads-")))
+            .isEmpty();
+    }
+
+    @Test
+    void ofBucketShouldFilterInvalidWeekCount() {
+        assertThat(UploadBucketName.ofBucket(BucketName.of("uploads-invalid")))
+            .isEmpty();
+    }
+
+    @Test
+    void ofBucketShouldFilterNegativeWeekCount() {
+        assertThat(UploadBucketName.ofBucket(BucketName.of("uploads--1")))
+            .isEmpty();
+    }
+
+    @Test
+    void ofBucketShouldParseValidValues() {
+        assertThat(UploadBucketName.ofBucket(BucketName.of("uploads-36")))
+            .contains(new UploadBucketName(36));
+    }
+}
\ No newline at end of file

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org