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 rc...@apache.org on 2019/11/29 02:00:31 UTC

[james-project] 05/06: JAMES-2989 Move the hardcoded "(Empty)" to the presentation layer

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 abcb395a8255842e65d0b2ef14442d598308cedc
Author: Tran Tien Duc <dt...@linagora.com>
AuthorDate: Wed Nov 27 10:47:46 2019 +0700

    JAMES-2989 Move the hardcoded "(Empty)" to the presentation layer
    
    To avoid storing hardcoded string in to the database for improving locale
    friendly in the future. Conversation is at:
    
    https://github.com/linagora/james-project/pull/2918#discussion_r350829600
---
 .../org/apache/james/jmap/api/preview/Preview.java | 11 +---
 .../apache/james/jmap/api/preview/PreviewTest.java | 22 +++----
 .../apache/james/jmap/draft/model/PreviewDTO.java  | 14 ++++-
 .../draft/model/message/view/MessageFullView.java  |  7 ++-
 .../model/message/view/MessageFullViewFactory.java |  3 +-
 .../james/jmap/draft/model/PreviewDTOTest.java     | 71 ++++++++++++++++++++++
 .../message/view/MessageFullViewFactoryTest.java   | 10 +--
 .../model/message/view/MessageFullViewTest.java    |  2 +-
 8 files changed, 109 insertions(+), 31 deletions(-)

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 8fb0b66..42c3db3 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,7 +20,6 @@
 package org.apache.james.jmap.api.preview;
 
 import java.util.Objects;
-import java.util.Optional;
 
 import org.apache.commons.lang3.StringUtils;
 
@@ -30,19 +29,15 @@ 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);
+        return Preview.from(
+            truncateToMaxLength(
+                StringUtils.normalizeSpace(textBody)));
     }
 
     private static String truncateToMaxLength(String body) {
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 33b5ee4..8a897b2 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
@@ -34,7 +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)");
+    private static final Preview EMPTY_STRING_PREVIEW = Preview.from("");
 
     @Test
     void shouldMatchBeanContract() {
@@ -78,33 +78,33 @@ class PreviewTest {
     class ComputeTest {
         
         @Test
-        void computeShouldReturnStringEmptyWhenStringEmptyTextBody() throws Exception {
+        void computeShouldReturnEmptyStringPreviewWhenStringEmptyTextBody() throws Exception {
             assertThat(Preview.compute(""))
-                .isEqualTo(NO_BODY);
+                .isEqualTo(EMPTY_STRING_PREVIEW);
         }
 
         @Test
-        void computeShouldReturnStringEmptyWhenOnlySpaceTabAndBreakLines() throws Exception {
+        void computeShouldReturnEmptyStringPreviewWhenOnlySpaceTabAndBreakLines() throws Exception {
             assertThat(Preview.compute(" \n\t "))
-                .isEqualTo(NO_BODY);
+                .isEqualTo(EMPTY_STRING_PREVIEW);
         }
 
         @Test
-        void computeShouldReturnStringEmptyWhenOnlySpace() throws Exception {
+        void computeShouldReturnEmptyStringPreviewWhenOnlySpace() throws Exception {
             assertThat(Preview.compute(" "))
-                .isEqualTo(NO_BODY);
+                .isEqualTo(EMPTY_STRING_PREVIEW);
         }
 
         @Test
-        void computeShouldReturnStringEmptyWhenOnlyTab() throws Exception {
+        void computeShouldReturnEmptyStringPreviewWhenOnlyTab() throws Exception {
             assertThat(Preview.compute("\t"))
-                .isEqualTo(NO_BODY);
+                .isEqualTo(EMPTY_STRING_PREVIEW);
         }
 
         @Test
-        void computeShouldReturnStringEmptyWhenOnlyBreakLines() throws Exception {
+        void computeShouldReturnEmptyStringPreviewWhenOnlyBreakLines() throws Exception {
             assertThat(Preview.compute("\n"))
-                .isEqualTo(NO_BODY);
+                .isEqualTo(EMPTY_STRING_PREVIEW);
         }
 
         @Test
diff --git a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/PreviewDTO.java b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/PreviewDTO.java
index 6754db7..6b2457c 100644
--- a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/PreviewDTO.java
+++ b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/PreviewDTO.java
@@ -20,6 +20,7 @@
 package org.apache.james.jmap.draft.model;
 
 import java.util.Objects;
+import java.util.Optional;
 
 import org.apache.james.jmap.api.preview.Preview;
 
@@ -29,8 +30,13 @@ import com.google.common.base.Preconditions;
 
 public class PreviewDTO {
 
-    public static PreviewDTO from(Preview preview) {
-        return new PreviewDTO(preview.getValue());
+    private static final String NO_BODY_AS_STRING = "(Empty)";
+    private static final PreviewDTO NO_BODY = PreviewDTO.of(NO_BODY_AS_STRING);
+
+    public static PreviewDTO from(Optional<Preview> preview) {
+        return preview.map(Preview::getValue)
+            .map(PreviewDTO::of)
+            .orElse(NO_BODY);
     }
 
     @VisibleForTesting
@@ -43,7 +49,9 @@ public class PreviewDTO {
     private PreviewDTO(String value) {
         Preconditions.checkNotNull(value);
 
-        this.value = value;
+        this.value = Optional.of(value)
+            .filter(previewValue -> !previewValue.isEmpty())
+            .orElse(NO_BODY_AS_STRING);
     }
 
     @JsonValue
diff --git a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/message/view/MessageFullView.java b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/message/view/MessageFullView.java
index 00d8005..40399c9 100644
--- a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/message/view/MessageFullView.java
+++ b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/message/view/MessageFullView.java
@@ -54,7 +54,7 @@ public class MessageFullView extends MessageHeaderView {
 
     @JsonPOJOBuilder(withPrefix = "")
     public static class Builder extends MessageHeaderView.Builder<MessageFullView.Builder> {
-        private Preview preview;
+        private Optional<Preview> preview;
         private Optional<String> textBody = Optional.empty();
         private Optional<String> htmlBody = Optional.empty();
         private final ImmutableList.Builder<Attachment> attachments;
@@ -67,6 +67,11 @@ public class MessageFullView extends MessageHeaderView {
         }
 
         public Builder preview(Preview preview) {
+            this.preview = Optional.of(preview);
+            return this;
+        }
+
+        public Builder preview(Optional<Preview> preview) {
             this.preview = preview;
             return this;
         }
diff --git a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewFactory.java b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewFactory.java
index c246ea5..bc17419 100644
--- a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewFactory.java
+++ b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewFactory.java
@@ -78,8 +78,7 @@ public class MessageFullViewFactory implements MessageViewFactory<MessageFullVie
         Optional<String> mainTextContent = mainTextContent(messageContent);
         Optional<String> textBody = computeTextBodyIfNeeded(messageContent, mainTextContent);
 
-        Preview preview = mainTextContent.map(Preview::compute)
-            .orElse(Preview.NO_BODY);
+        Optional<Preview> preview = mainTextContent.map(Preview::compute);
 
         return MessageFullView.builder()
                 .id(message.getMessageId())
diff --git a/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/PreviewDTOTest.java b/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/PreviewDTOTest.java
new file mode 100644
index 0000000..95b1a79
--- /dev/null
+++ b/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/PreviewDTOTest.java
@@ -0,0 +1,71 @@
+/****************************************************************
+ * 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.draft.model;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.Optional;
+
+import org.apache.james.jmap.api.preview.Preview;
+import org.junit.jupiter.api.Test;
+
+import nl.jqno.equalsverifier.EqualsVerifier;
+
+class PreviewDTOTest {
+
+    private static final String EMPTY_PREVIEW = "(Empty)";
+    private static final String SAMPLE_PREVIEW_VALUE = "hello bob!";
+
+    @Test
+    void shouldMatchBeanContract() {
+        EqualsVerifier.forClass(PreviewDTO.class)
+            .verify();
+    }
+
+    @Test
+    void fromShouldReturnPreviewWithTheValue() {
+        assertThat(PreviewDTO.from(Optional.of(Preview.from(SAMPLE_PREVIEW_VALUE))))
+            .isEqualTo(PreviewDTO.of(SAMPLE_PREVIEW_VALUE));
+    }
+
+    @Test
+    void fromShouldReturnNoBodyWhenNoPreview() {
+        assertThat(PreviewDTO.from(Optional.empty()))
+            .isEqualTo(PreviewDTO.of(EMPTY_PREVIEW));
+    }
+
+    @Test
+    void fromShouldReturnNoBodyWhenPreviewOfEmptyString() {
+        assertThat(PreviewDTO.from(Optional.of(Preview.from(""))))
+            .isEqualTo(PreviewDTO.of(EMPTY_PREVIEW));
+    }
+
+    @Test
+    void ofShouldReturnPreviewWithTheValue() {
+        assertThat(PreviewDTO.of(SAMPLE_PREVIEW_VALUE))
+            .isEqualTo(PreviewDTO.of(SAMPLE_PREVIEW_VALUE));
+    }
+
+    @Test
+    void ofShouldReturnNoBodyWhenPreviewOfEmptyString() {
+        assertThat(PreviewDTO.of(""))
+            .isEqualTo(PreviewDTO.of(EMPTY_PREVIEW));
+    }
+}
\ No newline at end of file
diff --git a/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewFactoryTest.java b/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewFactoryTest.java
index 60aefee..e0c3ff9 100644
--- a/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewFactoryTest.java
+++ b/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewFactoryTest.java
@@ -171,7 +171,7 @@ class MessageFullViewFactoryTest {
         MessageFullView testee = messageFullViewFactory.fromMetaDataWithContent(testMail);
         assertThat(testee)
             .extracting(MessageFullView::getPreview, MessageFullView::getSize, MessageFullView::getSubject, MessageFullView::getHeaders, MessageFullView::getDate)
-            .containsExactly(PreviewDTO.of("(Empty)"), Number.ZERO, "", ImmutableMap.of("MIME-Version", "1.0"), INTERNAL_DATE);
+            .containsExactly(PreviewDTO.of(""), Number.ZERO, "", ImmutableMap.of("MIME-Version", "1.0"), INTERNAL_DATE);
     }
 
     @Test
@@ -639,7 +639,7 @@ class MessageFullViewFactoryTest {
         MessageFullView testee = messageFullViewFactory.fromMetaDataWithContent(testMail);
         assertThat(testee)
             .extracting(MessageFullView::getPreview, MessageFullView::getSize, MessageFullView::getSubject, MessageFullView::getHeaders, MessageFullView::getDate)
-            .containsExactly(PreviewDTO.of("(Empty)"), Number.fromLong(1010L), "", ImmutableMap.of("MIME-Version", "1.0"), INTERNAL_DATE);
+            .containsExactly(PreviewDTO.of(""), Number.fromLong(1010L), "", ImmutableMap.of("MIME-Version", "1.0"), INTERNAL_DATE);
     }
 
     @Test
@@ -682,7 +682,7 @@ class MessageFullViewFactoryTest {
             .build();
         MessageFullView testee = messageFullViewFactory.fromMetaDataWithContent(testMail);
 
-        assertThat(testee.getPreview()).isEqualTo(PreviewDTO.of("(Empty)"));
+        assertThat(testee.getPreview()).isEqualTo(PreviewDTO.of(""));
         assertThat(testee.getHtmlBody()).contains("");
         assertThat(testee.getTextBody()).isEmpty();
     }
@@ -759,7 +759,7 @@ class MessageFullViewFactoryTest {
 
         assertThat(testee)
             .extracting(MessageFullView::getPreview, MessageFullView::getHtmlBody, MessageFullView::getTextBody)
-            .containsExactly(PreviewDTO.of("(Empty)"), Optional.empty(), Optional.of(""));
+            .containsExactly(PreviewDTO.of(""), Optional.empty(), Optional.of(""));
     }
 
     @Test
@@ -783,7 +783,7 @@ class MessageFullViewFactoryTest {
 
         assertThat(testee)
             .extracting(MessageFullView::getPreview, MessageFullView::getHtmlBody, MessageFullView::getTextBody)
-            .containsExactly(PreviewDTO.of("(Empty)"), Optional.of("<html><body></body></html>"), Optional.empty());
+            .containsExactly(PreviewDTO.of(""), Optional.of("<html><body></body></html>"), Optional.empty());
     }
 
     @Test
diff --git a/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewTest.java b/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewTest.java
index ff503b6..81c64e6 100644
--- a/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewTest.java
+++ b/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewTest.java
@@ -42,7 +42,7 @@ import com.google.common.collect.ImmutableMap;
 class MessageFullViewTest {
 
     private static final Preview PREVIEW = Preview.from("preview");
-    private static final PreviewDTO PREVIEW_DTO = PreviewDTO.from(PREVIEW);
+    private static final PreviewDTO PREVIEW_DTO = PreviewDTO.of(PREVIEW.getValue());
 
     @Test
     void buildShouldThrowWhenIdIsNull() {


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