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 e08c7b93fad158dbf6bb3646cc785970821dd364 Author: Tran Tien Duc <[email protected]> AuthorDate: Mon Apr 8 14:59:55 2019 +0700 JAMES-2708 Linshare document APIs & related POJO --- .../apache/james/linshare/AuthorizationToken.java | 3 +- .../org/apache/james/linshare/client/Document.java | 209 +++++++++++++++++++++ .../apache/james/linshare/client/LinshareAPI.java | 115 ++++++++++++ .../james/linshare/client/ReceivedShare.java | 83 ++++++++ .../apache/james/linshare/client/ShareRequest.java | 135 +++++++++++++ .../apache/james/linshare/client/ShareResult.java | 118 ++++++++++++ .../james/linshare/AuthorizationTokenTest.java | 9 + .../james/linshare/LinshareConfigurationTest.java | 2 +- .../apache/james/linshare/LinshareExtension.java | 2 +- .../DocumentTest.java} | 35 ++-- .../james/linshare/client/LinshareAPITest.java | 205 ++++++++++++++++++++ .../ReceivedShareTest.java} | 22 +-- .../james/linshare/client/ShareRequestTest.java | 95 ++++++++++ .../ShareResultTest.java} | 22 +-- 14 files changed, 1000 insertions(+), 55 deletions(-) diff --git a/third-party/linshare/src/main/java/org/apache/james/linshare/AuthorizationToken.java b/third-party/linshare/src/main/java/org/apache/james/linshare/AuthorizationToken.java index 34fe45f..9150a0a 100644 --- a/third-party/linshare/src/main/java/org/apache/james/linshare/AuthorizationToken.java +++ b/third-party/linshare/src/main/java/org/apache/james/linshare/AuthorizationToken.java @@ -23,6 +23,7 @@ import java.util.Objects; import org.apache.commons.lang3.StringUtils; +import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; @@ -31,7 +32,7 @@ public class AuthorizationToken { private final String value; @VisibleForTesting - public AuthorizationToken(String value) { + public AuthorizationToken(@JsonProperty("token") String value) { Preconditions.checkArgument(StringUtils.isNotBlank(value), "token value cannot be null or blank"); this.value = value; } diff --git a/third-party/linshare/src/main/java/org/apache/james/linshare/client/Document.java b/third-party/linshare/src/main/java/org/apache/james/linshare/client/Document.java new file mode 100644 index 0000000..ef87ed6 --- /dev/null +++ b/third-party/linshare/src/main/java/org/apache/james/linshare/client/Document.java @@ -0,0 +1,209 @@ +/**************************************************************** + * 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.linshare.client; + +import java.util.Objects; +import java.util.UUID; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.base.MoreObjects; +import com.google.common.base.Preconditions; + +public class Document { + + static class DocumentId { + private final UUID id; + + DocumentId(UUID id) { + Preconditions.checkNotNull(id); + this.id = id; + } + + public UUID getId() { + return id; + } + + public String asString() { + return id.toString(); + } + + @Override + public final boolean equals(Object o) { + if (o instanceof DocumentId) { + DocumentId that = (DocumentId) o; + + return Objects.equals(this.id, that.id); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(id); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("id", id) + .toString(); + } + } + + private final DocumentId id; + private final String name; + private final String description; + private final long creationDate; + private final long modificationDate; + private final long expirationDate; + private final boolean ciphered; + private final String type; + private final long size; + private final String metaData; + private final String sha256sum; + private final boolean hasThumbnail; + private final int shared; + + public Document(@JsonProperty("uuid") String uuid, + @JsonProperty("name") String name, + @JsonProperty("description") String description, + @JsonProperty("creationDate") long creationDate, + @JsonProperty("modificationDate") long modificationDate, + @JsonProperty("expirationDate") long expirationDate, + @JsonProperty("ciphered") boolean ciphered, + @JsonProperty("type") String type, + @JsonProperty("size") long size, + @JsonProperty("metaData") String metaData, + @JsonProperty("sha256sum") String sha256sum, + @JsonProperty("hasThumbnail") boolean hasThumbnail, + @JsonProperty("shared") int shared) { + this.id = new DocumentId(UUID.fromString(uuid)); + this.name = name; + this.description = description; + this.creationDate = creationDate; + this.modificationDate = modificationDate; + this.expirationDate = expirationDate; + this.ciphered = ciphered; + this.type = type; + this.size = size; + this.metaData = metaData; + this.sha256sum = sha256sum; + this.hasThumbnail = hasThumbnail; + this.shared = shared; + } + + public DocumentId getId() { + return id; + } + + public String getName() { + return name; + } + + public String getDescription() { + return description; + } + + public long getCreationDate() { + return creationDate; + } + + public long getModificationDate() { + return modificationDate; + } + + public long getExpirationDate() { + return expirationDate; + } + + public boolean isCiphered() { + return ciphered; + } + + public String getType() { + return type; + } + + public long getSize() { + return size; + } + + public String getMetaData() { + return metaData; + } + + public String getSha256sum() { + return sha256sum; + } + + public boolean isHasThumbnail() { + return hasThumbnail; + } + + public int getShared() { + return shared; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof Document) { + Document document = (Document) o; + + return Objects.equals(this.creationDate, document.creationDate) + && Objects.equals(this.modificationDate, document.modificationDate) + && Objects.equals(this.expirationDate, document.expirationDate) + && Objects.equals(this.ciphered, document.ciphered) + && Objects.equals(this.size, document.size) + && Objects.equals(this.hasThumbnail, document.hasThumbnail) + && Objects.equals(this.shared, document.shared) + && Objects.equals(this.id, document.id) + && Objects.equals(this.name, document.name) + && Objects.equals(this.description, document.description) + && Objects.equals(this.type, document.type) + && Objects.equals(this.metaData, document.metaData) + && Objects.equals(this.sha256sum, document.sha256sum); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(id, name, description, creationDate, modificationDate, expirationDate, ciphered, type, size, metaData, sha256sum, hasThumbnail, shared); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("id", id) + .add("name", name) + .add("description", description) + .add("creationDate", creationDate) + .add("modificationDate", modificationDate) + .add("expirationDate", expirationDate) + .add("ciphered", ciphered) + .add("type", type) + .add("size", size) + .add("metaData", metaData) + .add("sha256sum", sha256sum) + .add("hasThumbnail", hasThumbnail) + .add("shared", shared) + .toString(); + } +} diff --git a/third-party/linshare/src/main/java/org/apache/james/linshare/client/LinshareAPI.java b/third-party/linshare/src/main/java/org/apache/james/linshare/client/LinshareAPI.java new file mode 100644 index 0000000..400c9cd --- /dev/null +++ b/third-party/linshare/src/main/java/org/apache/james/linshare/client/LinshareAPI.java @@ -0,0 +1,115 @@ +/**************************************************************** + * 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.linshare.client; + +import static org.apache.james.linshare.client.LinshareAPI.Headers.ACCEPT_APPLICATION_JSON; +import static org.apache.james.linshare.client.LinshareAPI.Headers.CONTENT_TYPE_APPLICATION_JSON; +import static org.apache.james.linshare.client.LinshareAPI.Headers.CONTENT_TYPE_MULTIPART; + +import java.io.File; +import java.util.List; + +import org.apache.james.linshare.AuthorizationToken; +import org.apache.james.linshare.LinshareConfiguration; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; + +import feign.Feign; +import feign.Logger; +import feign.Param; +import feign.RequestInterceptor; +import feign.RequestLine; +import feign.RequestTemplate; +import feign.form.FormEncoder; +import feign.jackson.JacksonDecoder; +import feign.jackson.JacksonEncoder; +import feign.slf4j.Slf4jLogger; + +public interface LinshareAPI { + + interface Headers { + String ACCEPT_APPLICATION_JSON = "Accept: application/json"; + String CONTENT_TYPE_MULTIPART = "Content-Type: multipart/form-data"; + String CONTENT_TYPE_APPLICATION_JSON = "Content-Type: application/json"; + } + + class AuthorizationInterceptor implements RequestInterceptor { + + private final AuthorizationToken authorizationToken; + + AuthorizationInterceptor(AuthorizationToken authorizationToken) { + this.authorizationToken = authorizationToken; + } + + @Override + public void apply(RequestTemplate template) { + template.header("Authorization", authorizationToken.asBearerHeader()); + } + } + + @VisibleForTesting + static LinshareAPI from(LinshareConfiguration configuration) { + return Feign.builder() + .requestInterceptor(new AuthorizationInterceptor(configuration.getToken())) + .logger(new Slf4jLogger(LinshareAPI.class)) + .logLevel(Logger.Level.FULL) + .encoder(new FormEncoder(new JacksonEncoder())) + .decoder(new JacksonDecoder()) + .target(LinshareAPI.class, configuration.getUrl().toString()); + } + + @RequestLine("GET /linshare/webservice/rest/user/v2/documents") + @feign.Headers(ACCEPT_APPLICATION_JSON) + List<Document> listAllDocuments(); + + @RequestLine("POST /linshare/webservice/rest/user/v2/documents") + @feign.Headers({ ACCEPT_APPLICATION_JSON, CONTENT_TYPE_MULTIPART }) + Document uploadDocument(@Param("file") File file, @Param("filesize") long fileSize); + + default Document uploadDocument(File file) { + Preconditions.checkNotNull(file); + Preconditions.checkArgument(file.exists(), "File to upload does not exist: " + file.getAbsolutePath()); + + return uploadDocument(file, file.length()); + } + + @RequestLine("POST /linshare/webservice/rest/user/v2/shares") + @feign.Headers({ ACCEPT_APPLICATION_JSON, CONTENT_TYPE_APPLICATION_JSON }) + List<ShareResult> share(ShareRequest request); + + @RequestLine("DELETE /linshare/webservice/rest/user/v2/documents/{documentId}") + @feign.Headers({ ACCEPT_APPLICATION_JSON, CONTENT_TYPE_APPLICATION_JSON }) + Document delete(@Param("documentId") String documentId); + + default Document delete(Document.DocumentId documentId) { + return delete(documentId.asString()); + } + + @VisibleForTesting + default void deleteAllDocuments() { + listAllDocuments() + .forEach(document -> delete(document.getId())); + } + + @RequestLine("GET /linshare/webservice/rest/user/v2/received_shares") + @feign.Headers({ ACCEPT_APPLICATION_JSON, CONTENT_TYPE_APPLICATION_JSON }) + List<ReceivedShare> receivedShares(); +} diff --git a/third-party/linshare/src/main/java/org/apache/james/linshare/client/ReceivedShare.java b/third-party/linshare/src/main/java/org/apache/james/linshare/client/ReceivedShare.java new file mode 100644 index 0000000..7d21962 --- /dev/null +++ b/third-party/linshare/src/main/java/org/apache/james/linshare/client/ReceivedShare.java @@ -0,0 +1,83 @@ +/**************************************************************** + * 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.linshare.client; + +import java.util.Objects; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.annotations.VisibleForTesting; + +class ReceivedShare { + + private final User sender; + private final Document document; + private final int downloaded; + + @VisibleForTesting + ReceivedShare(@JsonProperty("sender") User sender, + @JsonProperty("downloaded") int downloaded, + @JsonProperty("uuid") String uuid, + @JsonProperty("name") String name, + @JsonProperty("description") String description, + @JsonProperty("creationDate") long creationDate, + @JsonProperty("modificationDate") long modificationDate, + @JsonProperty("expirationDate") long expirationDate, + @JsonProperty("ciphered") boolean ciphered, + @JsonProperty("type") String type, + @JsonProperty("size") long size, + @JsonProperty("metaData") String metaData, + @JsonProperty("sha256sum") String sha256sum, + @JsonProperty("hasThumbnail") boolean hasThumbnail, + @JsonProperty("shared") int shared) { + this.sender = sender; + this.downloaded = downloaded; + this.document = new Document(uuid, name, description, creationDate, modificationDate, expirationDate, + ciphered, type, size, metaData, sha256sum, hasThumbnail, shared); + } + + public User getSender() { + return sender; + } + + public Document getDocument() { + return document; + } + + public int getDownloaded() { + return downloaded; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof ReceivedShare) { + ReceivedShare that = (ReceivedShare) o; + + return Objects.equals(this.sender, that.sender) + && Objects.equals(this.document, that.document) + && Objects.equals(this.downloaded, that.downloaded); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(sender, document, downloaded); + } +} 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 new file mode 100644 index 0000000..c0a95bb --- /dev/null +++ b/third-party/linshare/src/main/java/org/apache/james/linshare/client/ShareRequest.java @@ -0,0 +1,135 @@ +/**************************************************************** + * 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.linshare.client; + +import java.util.List; +import java.util.Objects; +import java.util.UUID; + +import org.apache.james.core.MailAddress; +import org.apache.james.linshare.client.Document.DocumentId; + +import com.github.steveash.guavate.Guavate; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; + +class ShareRequest { + + static class Recipient { + private final MailAddress mail; + + @VisibleForTesting + Recipient(MailAddress mail) { + Preconditions.checkNotNull(mail); + Preconditions.checkArgument(!MailAddress.nullSender().equals(mail), "nullSender is not allowed"); + + this.mail = mail; + } + + public String getMail() { + return mail.asString(); + } + + @Override + public final boolean equals(Object o) { + if (o instanceof Recipient) { + Recipient recipient = (Recipient) o; + + return Objects.equals(this.mail, recipient.mail); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(mail); + } + } + + static class Builder { + private final ImmutableList.Builder<Recipient> recipientsBuilder; + private final ImmutableList.Builder<DocumentId> documentIdsBuilder; + + Builder() { + this.recipientsBuilder = new ImmutableList.Builder<>(); + this.documentIdsBuilder = new ImmutableList.Builder<>(); + } + + Builder addRecipient(MailAddress recipient) { + recipientsBuilder.add(new Recipient(recipient)); + return this; + } + + Builder addDocumentId(DocumentId documentId) { + documentIdsBuilder.add(documentId); + return this; + } + + ShareRequest build() { + return new ShareRequest(recipientsBuilder.build(), documentIdsBuilder.build()); + } + } + + static Builder builder() { + return new Builder(); + } + + private final List<Recipient> recipients; + private final List<DocumentId> documentIds; + + private ShareRequest(List<Recipient> recipients, List<DocumentId> documentIds) { + Preconditions.checkNotNull(recipients); + Preconditions.checkNotNull(documentIds); + Preconditions.checkArgument(!recipients.isEmpty(), "recipients cannot be empty"); + Preconditions.checkArgument(!documentIds.isEmpty(), "documents cannot be empty"); + + this.recipients = recipients; + this.documentIds = documentIds; + } + + public List<Recipient> getRecipients() { + return recipients; + } + + public List<String> getDocuments() { + return documentIds + .stream() + .map(DocumentId::getId) + .map(UUID::toString) + .collect(Guavate.toImmutableList()); + } + + @Override + public final boolean equals(Object o) { + if (o instanceof ShareRequest) { + ShareRequest that = (ShareRequest) o; + + return Objects.equals(this.recipients, that.recipients) + && Objects.equals(this.documentIds, that.documentIds); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(recipients, documentIds); + } +} diff --git a/third-party/linshare/src/main/java/org/apache/james/linshare/client/ShareResult.java b/third-party/linshare/src/main/java/org/apache/james/linshare/client/ShareResult.java new file mode 100644 index 0000000..49bdaeb --- /dev/null +++ b/third-party/linshare/src/main/java/org/apache/james/linshare/client/ShareResult.java @@ -0,0 +1,118 @@ +/**************************************************************** + * 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.linshare.client; + +import java.util.Objects; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.annotations.VisibleForTesting; + +class ShareResult { + + private final String uuid; + private final String name; + private final long creationDate; + private final long modificationDate; + private final long expirationDate; + private final int downloaded; + private final String description; + private final Document document; + private final User recipient; + + @VisibleForTesting + ShareResult(@JsonProperty("uuid") String uuid, + @JsonProperty("name") String name, + @JsonProperty("creationDate") long creationDate, + @JsonProperty("modificationDate") long modificationDate, + @JsonProperty("expirationDate") long expirationDate, + @JsonProperty("downloaded") int downloaded, + @JsonProperty("description") String description, + @JsonProperty("document") Document document, + @JsonProperty("recipient") User recipient) { + this.uuid = uuid; + this.name = name; + this.creationDate = creationDate; + this.modificationDate = modificationDate; + this.expirationDate = expirationDate; + this.downloaded = downloaded; + this.description = description; + this.document = document; + this.recipient = recipient; + } + + public String getUuid() { + return uuid; + } + + public String getName() { + return name; + } + + public long getCreationDate() { + return creationDate; + } + + public long getModificationDate() { + return modificationDate; + } + + public long getExpirationDate() { + return expirationDate; + } + + public int getDownloaded() { + return downloaded; + } + + public String getDescription() { + return description; + } + + public Document getDocument() { + return document; + } + + public User getRecipient() { + return recipient; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof ShareResult) { + ShareResult that = (ShareResult) o; + + return Objects.equals(this.creationDate, that.creationDate) + && Objects.equals(this.modificationDate, that.modificationDate) + && Objects.equals(this.expirationDate, that.expirationDate) + && Objects.equals(this.downloaded, that.downloaded) + && Objects.equals(this.uuid, that.uuid) + && Objects.equals(this.name, that.name) + && Objects.equals(this.description, that.description) + && Objects.equals(this.document, that.document) + && Objects.equals(this.recipient, that.recipient); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(uuid, name, creationDate, modificationDate, expirationDate, downloaded, description, document, recipient); + } +} diff --git a/third-party/linshare/src/test/java/org/apache/james/linshare/AuthorizationTokenTest.java b/third-party/linshare/src/test/java/org/apache/james/linshare/AuthorizationTokenTest.java index 626bb1d..bfcbe1f 100644 --- a/third-party/linshare/src/test/java/org/apache/james/linshare/AuthorizationTokenTest.java +++ b/third-party/linshare/src/test/java/org/apache/james/linshare/AuthorizationTokenTest.java @@ -19,6 +19,7 @@ package org.apache.james.linshare; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import org.junit.jupiter.api.Test; @@ -46,4 +47,12 @@ class AuthorizationTokenTest { assertThatThrownBy(() -> new AuthorizationToken(blankToken)) .isInstanceOf(IllegalArgumentException.class); } + + @Test + void asBearerHeaderShouldReturnBearerWithASpaceAtBeginning() { + AuthorizationToken token = new AuthorizationToken("token-suffix"); + + assertThat(token.asBearerHeader()) + .isEqualTo("Bearer token-suffix"); + } } diff --git a/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareConfigurationTest.java b/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareConfigurationTest.java index 8090d11..6b9316b 100644 --- a/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareConfigurationTest.java +++ b/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareConfigurationTest.java @@ -44,7 +44,7 @@ class LinshareConfigurationTest { @Test void constructorShouldThrowWhenPassingNullUrl() { - AuthorizationToken token = new AuthorizationToken("Bearer jwt-token-at-here"); + AuthorizationToken token = new AuthorizationToken("jwt-token-at-here"); URL nullUrl = null; assertThatThrownBy(() -> new LinshareConfiguration(nullUrl, token)) .isInstanceOf(NullPointerException.class); diff --git a/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareExtension.java b/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareExtension.java index e1527f2..2a3fbad 100644 --- a/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareExtension.java +++ b/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareExtension.java @@ -20,7 +20,7 @@ package org.apache.james.linshare; import static org.apache.james.linshare.LinshareFixture.USER_1; import static org.apache.james.linshare.LinshareFixture.USER_CREDENTIAL_MAP; -import static org.apache.james.linshare.client.LinshareAPI.Header.ACCEPT_APPLICATION_JSON; +import static org.apache.james.linshare.client.LinshareAPI.Headers.ACCEPT_APPLICATION_JSON; import java.util.List; import java.util.Optional; diff --git a/third-party/linshare/src/test/java/org/apache/james/linshare/AuthorizationTokenTest.java b/third-party/linshare/src/test/java/org/apache/james/linshare/client/DocumentTest.java similarity index 69% copy from third-party/linshare/src/test/java/org/apache/james/linshare/AuthorizationTokenTest.java copy to third-party/linshare/src/test/java/org/apache/james/linshare/client/DocumentTest.java index 626bb1d..3274bf0 100644 --- a/third-party/linshare/src/test/java/org/apache/james/linshare/AuthorizationTokenTest.java +++ b/third-party/linshare/src/test/java/org/apache/james/linshare/client/DocumentTest.java @@ -17,33 +17,40 @@ * under the License. * ****************************************************************/ -package org.apache.james.linshare; +package org.apache.james.linshare.client; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import java.util.UUID; + +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import nl.jqno.equalsverifier.EqualsVerifier; -class AuthorizationTokenTest { +class DocumentTest { @Test void shouldMatchBeanContract() { - EqualsVerifier.forClass(AuthorizationToken.class) + EqualsVerifier.forClass(Document.class) .verify(); } - @Test - void constructorShouldThrowWhenTokenIsNull() { - String nullToken = null; - assertThatThrownBy(() -> new AuthorizationToken(nullToken)) - .isInstanceOf(IllegalArgumentException.class); - } + @Nested + class DocumentIdTest { - @Test - void constructorShouldThrowWhenTokenIsBlank() { - String blankToken = " "; - assertThatThrownBy(() -> new AuthorizationToken(blankToken)) - .isInstanceOf(IllegalArgumentException.class); + @Test + void shouldMatchBeanContract() { + EqualsVerifier.forClass(Document.DocumentId.class) + .verify(); + } + + @Test + void constructorShouldThrowWhenPassingNullUUID() { + UUID nullUUId = null; + + assertThatThrownBy(() -> new Document.DocumentId(nullUUId)) + .isInstanceOf(NullPointerException.class); + } } } 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 new file mode 100644 index 0000000..1991514 --- /dev/null +++ b/third-party/linshare/src/test/java/org/apache/james/linshare/client/LinshareAPITest.java @@ -0,0 +1,205 @@ +/**************************************************************** + * 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.linshare.client; + +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 java.io.File; +import java.nio.file.Files; +import java.util.Collection; +import java.util.List; + +import org.apache.james.core.MailAddress; +import org.apache.james.linshare.LinshareExtension; +import org.assertj.core.api.SoftAssertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.testcontainers.shaded.com.google.common.collect.ImmutableList; + +import com.github.steveash.guavate.Guavate; + +import feign.FeignException; + +class LinshareAPITest { + + @RegisterExtension + static LinshareExtension linshareExtension = new LinshareExtension(); + + private LinshareAPI user1LinshareAPI; + private LinshareAPI user2LinshareAPI; + private LinshareAPI user3LinshareAPI; + private LinshareAPI user4LinshareAPI; + + @BeforeEach + void setup() throws Exception { + user1LinshareAPI = linshareExtension.getAPIFor(USER_1); + user2LinshareAPI = linshareExtension.getAPIFor(USER_2); + user3LinshareAPI = linshareExtension.getAPIFor(USER_3); + user4LinshareAPI = linshareExtension.getAPIFor(USER_4); + } + + @Test + void uploadDocumentShouldReturnUploaded() throws Exception { + File uploadFile = templateFile(); + Document uploadedDocument = user1LinshareAPI.uploadDocument(uploadFile); + + SoftAssertions.assertSoftly(softly -> { + softly.assertThat(uploadedDocument.getName()).isEqualTo(uploadFile.getName()); + softly.assertThat(uploadedDocument.getSize()).isEqualTo(uploadFile.length()); + }); + } + + @Test + void uploadDocumentShouldMakeListingReturnUploaded() throws Exception { + Document uploadedDocument = user1LinshareAPI.uploadDocument(templateFile()); + + assertThat(user1LinshareAPI.listAllDocuments()) + .hasSize(1) + .containsExactly(uploadedDocument); + } + + @Test + void listAllShouldReturnEmptyWhenNoUpload() { + assertThat(user1LinshareAPI.listAllDocuments()) + .isEmpty(); + } + + @Test + void listAllShouldReturnAllUploadedDocuments() throws Exception { + Document firstDocument = user1LinshareAPI.uploadDocument(templateFile()); + Document secondDocument = user1LinshareAPI.uploadDocument(templateFile()); + + assertThat(user1LinshareAPI.listAllDocuments()) + .containsExactly(firstDocument, secondDocument); + } + + @Test + void listAllShouldNotReturnDocumentsOfOtherUsers() throws Exception { + Document firstDocument = user1LinshareAPI.uploadDocument(templateFile()); + Document secondDocument = user1LinshareAPI.uploadDocument(templateFile()); + + Document user2Document = user2LinshareAPI.uploadDocument(templateFile()); + + assertThat(user1LinshareAPI.listAllDocuments()) + .containsExactly(firstDocument, secondDocument); + } + + @Test + void deleteShouldDeleteUploadedDocument() throws Exception { + Document firstDocument = user1LinshareAPI.uploadDocument(templateFile()); + user1LinshareAPI.delete(firstDocument.getId()); + + assertThat(user1LinshareAPI.listAllDocuments()) + .isEmpty(); + } + + @Test + void deleteShouldNotDeleteOtherUserDocuments() throws Exception { + Document user1Document = user1LinshareAPI.uploadDocument(templateFile()); + Document user2Document = user2LinshareAPI.uploadDocument(templateFile()); + user1LinshareAPI.delete(user1Document.getId()); + + assertThat(user2LinshareAPI.listAllDocuments()) + .containsExactly(user2Document); + } + + @Test + void deleteShouldReturnErrorWhenDeleteOtherUserDocuments() throws Exception { + Document user1Document = user1LinshareAPI.uploadDocument(templateFile()); + + assertThatThrownBy(() -> user2LinshareAPI.delete(user1Document.getId())) + .isInstanceOf(FeignException.Forbidden.class); + } + + @Test + void deleteAllShouldClearAllDocumentsOfAnUser() throws Exception { + Document user1Document1 = user1LinshareAPI.uploadDocument(templateFile()); + Document user1Document2 = user1LinshareAPI.uploadDocument(templateFile()); + + user1LinshareAPI.deleteAllDocuments(); + + assertThat(user1LinshareAPI.listAllDocuments()) + .isEmpty(); + } + + @Test + void shareShouldShareToTargetedRecipient() throws Exception { + Document user1Document = user1LinshareAPI.uploadDocument(templateFile()); + + ShareRequest shareRequest = ShareRequest.builder() + .addDocumentId(user1Document.getId()) + .addRecipient(new MailAddress(USER_2.getUsername())) + .build(); + + user1LinshareAPI.share(shareRequest); + assertThat(user2LinshareAPI.receivedShares()) + .hasSize(1) + .allSatisfy(shareReceived -> { + Document sharedDoc = shareReceived.getDocument(); + assertThat(sharedDoc.getName()).isEqualTo(user1Document.getName()); + assertThat(sharedDoc.getSize()).isEqualTo(user1Document.getSize()); + }); + } + + @Test + void shareShouldWorkWithMultipleRecipients() throws Exception { + Document user1Document = user1LinshareAPI.uploadDocument(templateFile()); + + ShareRequest shareRequest = ShareRequest.builder() + .addDocumentId(user1Document.getId()) + .addRecipient(new MailAddress(USER_2.getUsername())) + .addRecipient(new MailAddress(USER_3.getUsername())) + .addRecipient(new MailAddress(USER_4.getUsername())) + .build(); + + user1LinshareAPI.share(shareRequest); + List<ReceivedShare> user2Shares = user2LinshareAPI.receivedShares(); + List<ReceivedShare> user3Shares = user3LinshareAPI.receivedShares(); + List<ReceivedShare> user4Shares = user4LinshareAPI.receivedShares(); + + assertThat(user2Shares) + .hasSameSizeAs(user3Shares) + .hasSameSizeAs(user4Shares) + .hasSize(1); + + assertThat(sharedDocs(user2Shares, user3Shares, user4Shares)) + .allSatisfy(sharedDoc -> { + assertThat(sharedDoc.getName()).isEqualTo(user1Document.getName()); + assertThat(sharedDoc.getSize()).isEqualTo(user1Document.getSize()); + }); + } + + private File templateFile() throws Exception { + return Files.createTempFile("linshare-api-test", ".temp").toFile(); + } + + private List<Document> sharedDocs(List<ReceivedShare>...shares) { + return ImmutableList.copyOf(shares).stream() + .flatMap(Collection::stream) + .map(ReceivedShare::getDocument) + .collect(Guavate.toImmutableList()); + } +} diff --git a/third-party/linshare/src/test/java/org/apache/james/linshare/AuthorizationTokenTest.java b/third-party/linshare/src/test/java/org/apache/james/linshare/client/ReceivedShareTest.java similarity index 67% copy from third-party/linshare/src/test/java/org/apache/james/linshare/AuthorizationTokenTest.java copy to third-party/linshare/src/test/java/org/apache/james/linshare/client/ReceivedShareTest.java index 626bb1d..8a97468 100644 --- a/third-party/linshare/src/test/java/org/apache/james/linshare/AuthorizationTokenTest.java +++ b/third-party/linshare/src/test/java/org/apache/james/linshare/client/ReceivedShareTest.java @@ -17,33 +17,17 @@ * under the License. * ****************************************************************/ -package org.apache.james.linshare; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; +package org.apache.james.linshare.client; import org.junit.jupiter.api.Test; import nl.jqno.equalsverifier.EqualsVerifier; -class AuthorizationTokenTest { +class ReceivedShareTest { @Test void shouldMatchBeanContract() { - EqualsVerifier.forClass(AuthorizationToken.class) + EqualsVerifier.forClass(ReceivedShare.class) .verify(); } - - @Test - void constructorShouldThrowWhenTokenIsNull() { - String nullToken = null; - assertThatThrownBy(() -> new AuthorizationToken(nullToken)) - .isInstanceOf(IllegalArgumentException.class); - } - - @Test - void constructorShouldThrowWhenTokenIsBlank() { - String blankToken = " "; - assertThatThrownBy(() -> new AuthorizationToken(blankToken)) - .isInstanceOf(IllegalArgumentException.class); - } } 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 new file mode 100644 index 0000000..7e648bd --- /dev/null +++ b/third-party/linshare/src/test/java/org/apache/james/linshare/client/ShareRequestTest.java @@ -0,0 +1,95 @@ +/**************************************************************** + * 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.linshare.client; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.UUID; + +import org.apache.james.core.MailAddress; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import nl.jqno.equalsverifier.EqualsVerifier; + +class ShareRequestTest { + + @Test + void shouldMatchBeanContract() { + EqualsVerifier.forClass(ShareRequest.class) + .verify(); + } + + @Test + void builderShouldThrowWhenPassingNullRecipient() { + assertThatThrownBy(() -> ShareRequest.builder() + .addDocumentId(new Document.DocumentId(UUID.fromString("89bc2e3b-e07e-405f-9520-2de33a0a836c"))) + .addRecipient(null) + .build()) + .isInstanceOf(NullPointerException.class); + } + + @Test + void builderShouldThrowWhenPassingNullDocumentId() { + assertThatThrownBy(() -> ShareRequest.builder() + .addDocumentId(null) + .addRecipient(new MailAddress("[email protected]")) + .build()) + .isInstanceOf(NullPointerException.class); + } + + @Test + void builderShouldThrowWhenNoRecipient() { + assertThatThrownBy(() -> ShareRequest.builder() + .addRecipient(new MailAddress("[email protected]")) + .build()) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + void builderShouldThrowWhenNoDocumentId() { + assertThatThrownBy(() -> ShareRequest.builder() + .addDocumentId(new Document.DocumentId(UUID.fromString("89bc2e3b-e07e-405f-9520-2de33a0a836c"))) + .build()) + .isInstanceOf(IllegalArgumentException.class); + } + + @Nested + class RecipientTest { + + @Test + void shouldMatchBeanContract() { + EqualsVerifier.forClass(ShareRequest.Recipient.class) + .verify(); + } + + @Test + void constructorShouldThrowWhenPassingNullMailAddress() { + assertThatThrownBy(() -> new ShareRequest.Recipient(null)) + .isInstanceOf(NullPointerException.class); + } + + @Test + void constructorShouldThrowWhenPassingNullSenderMailAddress() { + assertThatThrownBy(() -> new ShareRequest.Recipient(MailAddress.nullSender())) + .isInstanceOf(IllegalArgumentException.class); + } + } +} diff --git a/third-party/linshare/src/test/java/org/apache/james/linshare/AuthorizationTokenTest.java b/third-party/linshare/src/test/java/org/apache/james/linshare/client/ShareResultTest.java similarity index 67% copy from third-party/linshare/src/test/java/org/apache/james/linshare/AuthorizationTokenTest.java copy to third-party/linshare/src/test/java/org/apache/james/linshare/client/ShareResultTest.java index 626bb1d..a14ace5 100644 --- a/third-party/linshare/src/test/java/org/apache/james/linshare/AuthorizationTokenTest.java +++ b/third-party/linshare/src/test/java/org/apache/james/linshare/client/ShareResultTest.java @@ -17,33 +17,17 @@ * under the License. * ****************************************************************/ -package org.apache.james.linshare; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; +package org.apache.james.linshare.client; import org.junit.jupiter.api.Test; import nl.jqno.equalsverifier.EqualsVerifier; -class AuthorizationTokenTest { +class ShareResultTest { @Test void shouldMatchBeanContract() { - EqualsVerifier.forClass(AuthorizationToken.class) + EqualsVerifier.forClass(ShareResult.class) .verify(); } - - @Test - void constructorShouldThrowWhenTokenIsNull() { - String nullToken = null; - assertThatThrownBy(() -> new AuthorizationToken(nullToken)) - .isInstanceOf(IllegalArgumentException.class); - } - - @Test - void constructorShouldThrowWhenTokenIsBlank() { - String blankToken = " "; - assertThatThrownBy(() -> new AuthorizationToken(blankToken)) - .isInstanceOf(IllegalArgumentException.class); - } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
