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 3e94093464f787d2b3fa3c8775c10fb705319887
Author: Tran Tien Duc <[email protected]>
AuthorDate: Fri Nov 22 10:02:01 2019 +0700

    JAMES-2989 Preview factory method with the business from PreviewGenerator
    
    Because the business to create a preview is simple and doesn't depend on
    other external states. I decided to move it into the POJO
---
 .../org/apache/james/jmap/api/preview/Preview.java | 17 ++++++
 .../apache/james/jmap/api/preview/PreviewTest.java | 66 ++++++++++++++++++++++
 2 files changed, 83 insertions(+)

diff --git 
a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/preview/Preview.java
 
b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/preview/Preview.java
index 0d4a208..8fb0b66 100644
--- 
a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/preview/Preview.java
+++ 
b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/preview/Preview.java
@@ -20,6 +20,9 @@
 package org.apache.james.jmap.api.preview;
 
 import java.util.Objects;
+import java.util.Optional;
+
+import org.apache.commons.lang3.StringUtils;
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
@@ -27,11 +30,25 @@ import com.google.common.base.Preconditions;
 public class Preview {
 
     private static final int MAX_LENGTH = 256;
+    public static final Preview NO_BODY = new Preview("(Empty)");
 
     public static Preview from(String value) {
         return new Preview(value);
     }
 
+    public static Preview compute(String textBody) {
+        return Optional.of(textBody)
+            .map(StringUtils::normalizeSpace)
+            .filter(text -> !text.isEmpty())
+            .map(Preview::truncateToMaxLength)
+            .map(Preview::from)
+            .orElse(NO_BODY);
+    }
+
+    private static String truncateToMaxLength(String body) {
+        return StringUtils.left(body, MAX_LENGTH);
+    }
+
     private final String value;
 
     @VisibleForTesting
diff --git 
a/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/preview/PreviewTest.java
 
b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/preview/PreviewTest.java
index 8298f5d..33b5ee4 100644
--- 
a/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/preview/PreviewTest.java
+++ 
b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/preview/PreviewTest.java
@@ -23,6 +23,8 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatCode;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
+import org.apache.commons.lang3.StringUtils;
+import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
 
 import com.google.common.base.Strings;
@@ -32,6 +34,7 @@ import nl.jqno.equalsverifier.EqualsVerifier;
 class PreviewTest {
 
     private static final String PREVIEW_RAW_VALUE = "Hello James!";
+    private static final Preview NO_BODY = new Preview("(Empty)");
 
     @Test
     void shouldMatchBeanContract() {
@@ -70,4 +73,67 @@ class PreviewTest {
         assertThatCode(() -> Preview.from(Strings.repeat("a", 256)))
             .doesNotThrowAnyException();
     }
+
+    @Nested
+    class ComputeTest {
+        
+        @Test
+        void computeShouldReturnStringEmptyWhenStringEmptyTextBody() throws 
Exception {
+            assertThat(Preview.compute(""))
+                .isEqualTo(NO_BODY);
+        }
+
+        @Test
+        void computeShouldReturnStringEmptyWhenOnlySpaceTabAndBreakLines() 
throws Exception {
+            assertThat(Preview.compute(" \n\t "))
+                .isEqualTo(NO_BODY);
+        }
+
+        @Test
+        void computeShouldReturnStringEmptyWhenOnlySpace() throws Exception {
+            assertThat(Preview.compute(" "))
+                .isEqualTo(NO_BODY);
+        }
+
+        @Test
+        void computeShouldReturnStringEmptyWhenOnlyTab() throws Exception {
+            assertThat(Preview.compute("\t"))
+                .isEqualTo(NO_BODY);
+        }
+
+        @Test
+        void computeShouldReturnStringEmptyWhenOnlyBreakLines() throws 
Exception {
+            assertThat(Preview.compute("\n"))
+                .isEqualTo(NO_BODY);
+        }
+
+        @Test
+        void computeShouldReturnStringWithoutTruncation() throws Exception {
+            String body = StringUtils.leftPad("a", 100, "b");
+
+            assertThat(Preview.compute(body)
+                    .getValue())
+                .hasSize(100)
+                .isEqualTo(body);
+        }
+
+        @Test
+        void computeShouldReturnStringIsLimitedTo256Length() throws Exception {
+            String body = StringUtils.leftPad("a", 300, "b");
+            String expected = StringUtils.leftPad("b", 256, "b");
+
+            assertThat(Preview.compute(body)
+                    .getValue())
+                .hasSize(256)
+                .isEqualTo(expected);
+        }
+
+        @Test
+        void computeShouldReturnNormalizeSpaceString() throws Exception {
+            String body = "    this      is\n      the\r           preview\t   
      content\n\n         ";
+
+            assertThat(Preview.compute(body))
+                .isEqualTo(Preview.from("this is the preview content"));
+        }
+    }
 }
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to