james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [james-project] 10/12: JAMES-2709 Position explanation on the share
Date Fri, 19 Apr 2019 07:43:03 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit 86159d10c3656a636b0080b78c1019e67675af6f
Author: Benoit Tellier <btellier@linagora.com>
AuthorDate: Thu Apr 11 15:50:15 2019 +0700

    JAMES-2709 Position explanation on the share
---
 .../linshare/LinshareBlobExportMechanism.java      |  9 +++--
 .../apache/james/linshare/client/ShareRequest.java | 29 ++++++++++----
 .../linshare/LinshareBlobExportMechanismTest.java  | 36 +++++++++++++++++
 .../james/linshare/client/LinshareAPITest.java     | 46 ++++++++++++++++++++--
 .../james/linshare/client/ShareRequestTest.java    | 20 +++++++++-
 5 files changed, 124 insertions(+), 16 deletions(-)

diff --git a/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareBlobExportMechanism.java
b/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareBlobExportMechanism.java
index 042c966..3af2261 100644
--- a/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareBlobExportMechanism.java
+++ b/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareBlobExportMechanism.java
@@ -49,26 +49,27 @@ public class LinshareBlobExportMechanism implements BlobExportMechanism
{
     public ShareeStage blobId(BlobId blobId) {
         return mailAddress -> explanation -> fileExtension -> () ->  {
             try {
-                exportBlob(blobId, mailAddress, fileExtension);
+                exportBlob(blobId, mailAddress, fileExtension, explanation);
             } catch (Exception e) {
                 throw new BlobExportException("Error while exporting blob " + blobId.asString()
+ " to " + mailAddress.asString(), e);
             }
         };
     }
 
-    private void exportBlob(BlobId blobId, MailAddress mailAddress, Optional<FileExtension>
fileExtension) throws IOException {
+    private void exportBlob(BlobId blobId, MailAddress mailAddress, Optional<FileExtension>
fileExtension, String explanation) throws IOException {
         File tempFile = createTempFile(blobId, fileExtension);
         try {
             FileUtils.copyInputStreamToFile(blobStore.read(blobId), tempFile);
-            uploadAndShare(mailAddress, tempFile);
+            uploadAndShare(mailAddress, tempFile, explanation);
         } finally {
             FileUtils.forceDelete(tempFile);
         }
     }
 
-    private void uploadAndShare(MailAddress mailAddress, File tempFile) {
+    private void uploadAndShare(MailAddress mailAddress, File tempFile, String explanation)
{
         Document document = linshareAPI.uploadDocument(tempFile);
         linshareAPI.share(ShareRequest.builder()
+            .message(explanation)
             .addDocumentId(document.getId())
             .addRecipient(mailAddress)
             .build());
diff --git a/third-party/linshare/src/main/java/org/apache/james/linshare/client/ShareRequest.java
b/third-party/linshare/src/main/java/org/apache/james/linshare/client/ShareRequest.java
index 61a2d8b..beb6892 100644
--- a/third-party/linshare/src/main/java/org/apache/james/linshare/client/ShareRequest.java
+++ b/third-party/linshare/src/main/java/org/apache/james/linshare/client/ShareRequest.java
@@ -63,12 +63,19 @@ public class ShareRequest {
             return Objects.hash(mail);
         }
     }
+    
+    @FunctionalInterface
+    public interface RequireMessage {
+        Builder message(String message);
+    }
 
     public static class Builder {
         private final ImmutableList.Builder<Recipient> recipientsBuilder;
         private final ImmutableList.Builder<DocumentId> documentIdsBuilder;
+        private final String message;
 
-        Builder() {
+        Builder(String message) {
+            this.message = message;
             this.recipientsBuilder = new ImmutableList.Builder<>();
             this.documentIdsBuilder = new ImmutableList.Builder<>();
         }
@@ -84,27 +91,34 @@ public class ShareRequest {
         }
 
         public ShareRequest build() {
-            return new ShareRequest(recipientsBuilder.build(), documentIdsBuilder.build());
+            return new ShareRequest(recipientsBuilder.build(), documentIdsBuilder.build(),
message);
         }
     }
 
-    public static Builder builder() {
-        return new Builder();
+    public static RequireMessage builder() {
+        return Builder::new;
     }
 
     private final List<Recipient> recipients;
     private final List<DocumentId> documentIds;
+    private final String message;
 
-    private ShareRequest(List<Recipient> recipients, List<DocumentId> documentIds)
{
+    private ShareRequest(List<Recipient> recipients, List<DocumentId> documentIds,
String message) {
+        Preconditions.checkNotNull(message);
         Preconditions.checkNotNull(recipients);
         Preconditions.checkNotNull(documentIds);
         Preconditions.checkArgument(!recipients.isEmpty(), "recipients cannot be empty");
         Preconditions.checkArgument(!documentIds.isEmpty(), "documents cannot be empty");
 
+        this.message = message;
         this.recipients = recipients;
         this.documentIds = documentIds;
     }
 
+    public String getMessage() {
+        return message;
+    }
+
     public List<Recipient> getRecipients() {
         return recipients;
     }
@@ -123,13 +137,14 @@ public class ShareRequest {
             ShareRequest that = (ShareRequest) o;
 
             return Objects.equals(this.recipients, that.recipients)
-                && Objects.equals(this.documentIds, that.documentIds);
+                && Objects.equals(this.documentIds, that.documentIds)
+                && Objects.equals(this.message, that.message);
         }
         return false;
     }
 
     @Override
     public final int hashCode() {
-        return Objects.hash(recipients, documentIds);
+        return Objects.hash(recipients, documentIds, message);
     }
 }
diff --git a/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareBlobExportMechanismTest.java
b/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareBlobExportMechanismTest.java
index e693cd5..569cde9 100644
--- a/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareBlobExportMechanismTest.java
+++ b/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareBlobExportMechanismTest.java
@@ -19,10 +19,14 @@
 
 package org.apache.james.linshare;
 
+import static io.restassured.RestAssured.given;
 import static org.apache.james.linshare.LinshareFixture.USER_1;
 import static org.apache.james.linshare.LinshareFixture.USER_2;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.hasSize;
 
 import java.nio.charset.StandardCharsets;
 
@@ -33,10 +37,14 @@ import org.apache.james.blob.export.api.FileExtension;
 import org.apache.james.blob.memory.MemoryBlobStore;
 import org.apache.james.core.MailAddress;
 import org.apache.james.linshare.client.LinshareAPI;
+import org.awaitility.Awaitility;
+import org.awaitility.Duration;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
 
+import io.restassured.specification.RequestSpecification;
+
 class LinshareBlobExportMechanismTest {
     private static final String EXPLANATION = "Explanation about the file being shared";
 
@@ -77,6 +85,34 @@ class LinshareBlobExportMechanismTest {
     }
 
     @Test
+    void exportShouldSendAnEmailToSharee() throws Exception {
+        BlobId blobId = blobStore.save("content".getBytes(StandardCharsets.UTF_8)).block();
+
+        testee.blobId(blobId)
+            .with(new MailAddress(USER_2.getUsername()))
+            .explanation(EXPLANATION)
+            .fileExtension(FileExtension.of("txt"))
+            .export();
+
+        RequestSpecification request = given(linshareExtension.getLinshare().fakeSmtpRequestSpecification());
+
+        Awaitility.waitAtMost(Duration.TEN_SECONDS)
+            .pollInterval(Duration.ONE_SECOND)
+            .untilAsserted(
+                () -> request
+                    .get("/api/email")
+                .then()
+                    .body("", hasSize(2)));
+
+        request
+            .get("/api/email")
+        .then()
+            .body("[1].subject", containsString("John Doe has shared a file with you"))
+            .body("[1].to", hasItem(USER_2.getUsername()))
+            .body("[1].html", containsString(EXPLANATION));
+    }
+
+    @Test
     void exportShouldFailWhenBlobDoesNotExist() {
         BlobId blobId = blobIdFactory.randomId();
 
diff --git a/third-party/linshare/src/test/java/org/apache/james/linshare/client/LinshareAPITest.java
b/third-party/linshare/src/test/java/org/apache/james/linshare/client/LinshareAPITest.java
index 1991514..d765379 100644
--- a/third-party/linshare/src/test/java/org/apache/james/linshare/client/LinshareAPITest.java
+++ b/third-party/linshare/src/test/java/org/apache/james/linshare/client/LinshareAPITest.java
@@ -19,12 +19,15 @@
 
 package org.apache.james.linshare.client;
 
+import static io.restassured.RestAssured.given;
 import static org.apache.james.linshare.LinshareFixture.USER_1;
 import static org.apache.james.linshare.LinshareFixture.USER_2;
 import static org.apache.james.linshare.LinshareFixture.USER_3;
 import static org.apache.james.linshare.LinshareFixture.USER_4;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.hasSize;
 
 import java.io.File;
 import java.nio.file.Files;
@@ -34,6 +37,8 @@ import java.util.List;
 import org.apache.james.core.MailAddress;
 import org.apache.james.linshare.LinshareExtension;
 import org.assertj.core.api.SoftAssertions;
+import org.awaitility.Awaitility;
+import org.awaitility.Duration;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
@@ -42,8 +47,10 @@ import org.testcontainers.shaded.com.google.common.collect.ImmutableList;
 import com.github.steveash.guavate.Guavate;
 
 import feign.FeignException;
+import io.restassured.specification.RequestSpecification;
 
 class LinshareAPITest {
+    private static final String MESSAGE = "message";
 
     @RegisterExtension
     static LinshareExtension linshareExtension = new LinshareExtension();
@@ -101,7 +108,7 @@ class LinshareAPITest {
         Document firstDocument = user1LinshareAPI.uploadDocument(templateFile());
         Document secondDocument = user1LinshareAPI.uploadDocument(templateFile());
 
-        Document user2Document = user2LinshareAPI.uploadDocument(templateFile());
+        user2LinshareAPI.uploadDocument(templateFile());
 
         assertThat(user1LinshareAPI.listAllDocuments())
             .containsExactly(firstDocument, secondDocument);
@@ -136,8 +143,8 @@ class LinshareAPITest {
 
     @Test
     void deleteAllShouldClearAllDocumentsOfAnUser() throws Exception {
-        Document user1Document1 = user1LinshareAPI.uploadDocument(templateFile());
-        Document user1Document2 = user1LinshareAPI.uploadDocument(templateFile());
+        user1LinshareAPI.uploadDocument(templateFile());
+        user1LinshareAPI.uploadDocument(templateFile());
 
         user1LinshareAPI.deleteAllDocuments();
 
@@ -146,10 +153,42 @@ class LinshareAPITest {
     }
 
     @Test
+    void shareShouldTriggerAnEmail() throws Exception {
+        Document user1Document = user1LinshareAPI.uploadDocument(templateFile());
+
+        String message = "Very specific message";
+        ShareRequest shareRequest = ShareRequest.builder()
+            .message(message)
+            .addDocumentId(user1Document.getId())
+            .addRecipient(new MailAddress(USER_2.getUsername()))
+            .build();
+
+        user1LinshareAPI.share(shareRequest);
+
+        RequestSpecification request = given(linshareExtension.getLinshare().fakeSmtpRequestSpecification());
+
+        Awaitility.waitAtMost(Duration.TEN_SECONDS)
+            .pollInterval(Duration.ONE_SECOND)
+            .untilAsserted(
+                () -> request
+                    .get("/api/email")
+                .then()
+                    .body("", hasSize(2)));
+
+        request
+            .get("/api/email")
+        .then()
+            .body("[1].subject", containsString("John Doe has shared a file with you"))
+            .body("[1].html", containsString(message));
+    }
+
+
+    @Test
     void shareShouldShareToTargetedRecipient() throws Exception {
         Document user1Document = user1LinshareAPI.uploadDocument(templateFile());
 
         ShareRequest shareRequest = ShareRequest.builder()
+            .message(MESSAGE)
             .addDocumentId(user1Document.getId())
             .addRecipient(new MailAddress(USER_2.getUsername()))
             .build();
@@ -169,6 +208,7 @@ class LinshareAPITest {
         Document user1Document = user1LinshareAPI.uploadDocument(templateFile());
 
         ShareRequest shareRequest = ShareRequest.builder()
+            .message(MESSAGE)
             .addDocumentId(user1Document.getId())
             .addRecipient(new MailAddress(USER_2.getUsername()))
             .addRecipient(new MailAddress(USER_3.getUsername()))
diff --git a/third-party/linshare/src/test/java/org/apache/james/linshare/client/ShareRequestTest.java
b/third-party/linshare/src/test/java/org/apache/james/linshare/client/ShareRequestTest.java
index 7e648bd..bb6567f 100644
--- a/third-party/linshare/src/test/java/org/apache/james/linshare/client/ShareRequestTest.java
+++ b/third-party/linshare/src/test/java/org/apache/james/linshare/client/ShareRequestTest.java
@@ -31,6 +31,8 @@ import nl.jqno.equalsverifier.EqualsVerifier;
 
 class ShareRequestTest {
 
+    private static final String MESSAGE = "message";
+
     @Test
     void shouldMatchBeanContract() {
         EqualsVerifier.forClass(ShareRequest.class)
@@ -40,6 +42,7 @@ class ShareRequestTest {
     @Test
     void builderShouldThrowWhenPassingNullRecipient() {
         assertThatThrownBy(() -> ShareRequest.builder()
+                .message(MESSAGE)
                 .addDocumentId(new Document.DocumentId(UUID.fromString("89bc2e3b-e07e-405f-9520-2de33a0a836c")))
                 .addRecipient(null)
                 .build())
@@ -47,8 +50,19 @@ class ShareRequestTest {
     }
 
     @Test
+    void builderShouldThrowWhenPassingNullMessage() {
+        assertThatThrownBy(() -> ShareRequest.builder()
+                .message(null)
+                .addDocumentId(new Document.DocumentId(UUID.fromString("89bc2e3b-e07e-405f-9520-2de33a0a836c")))
+                .addRecipient(new MailAddress("user@james.org"))
+                .build())
+            .isInstanceOf(NullPointerException.class);
+    }
+
+    @Test
     void builderShouldThrowWhenPassingNullDocumentId() {
         assertThatThrownBy(() -> ShareRequest.builder()
+                .message(MESSAGE)
                 .addDocumentId(null)
                 .addRecipient(new MailAddress("user@james.org"))
                 .build())
@@ -56,16 +70,18 @@ class ShareRequestTest {
     }
 
     @Test
-    void builderShouldThrowWhenNoRecipient() {
+    void builderShouldThrowWhenNoDocumentId() {
         assertThatThrownBy(() -> ShareRequest.builder()
+                .message(MESSAGE)
                 .addRecipient(new MailAddress("user@james.org"))
                 .build())
             .isInstanceOf(IllegalArgumentException.class);
     }
 
     @Test
-    void builderShouldThrowWhenNoDocumentId() {
+    void builderShouldThrowWhenNoRecipient() {
         assertThatThrownBy(() -> ShareRequest.builder()
+                .message(MESSAGE)
                 .addDocumentId(new Document.DocumentId(UUID.fromString("89bc2e3b-e07e-405f-9520-2de33a0a836c")))
                 .build())
             .isInstanceOf(IllegalArgumentException.class);


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


Mime
View raw message