james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [james-project] 04/12: JAMES-2709 Parse LinShareConfiguration from .properties
Date Fri, 19 Apr 2019 07:42:57 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 82305ca960e11608a384e341d6553c7b6519f5bd
Author: Benoit Tellier <btellier@linagora.com>
AuthorDate: Thu Apr 11 11:07:10 2019 +0700

    JAMES-2709 Parse LinShareConfiguration from .properties
---
 third-party/linshare/pom.xml                       |  4 ++
 .../james/linshare/LinshareConfiguration.java      | 17 +++++--
 .../james/linshare/LinshareConfigurationTest.java  | 58 ++++++++++++++++++----
 3 files changed, 66 insertions(+), 13 deletions(-)

diff --git a/third-party/linshare/pom.xml b/third-party/linshare/pom.xml
index 8a7917a..fe07bbd 100644
--- a/third-party/linshare/pom.xml
+++ b/third-party/linshare/pom.xml
@@ -56,6 +56,10 @@
             <artifactId>guava</artifactId>
         </dependency>
         <dependency>
+            <groupId>commons-configuration</groupId>
+            <artifactId>commons-configuration</artifactId>
+        </dependency>
+        <dependency>
             <groupId>io.github.openfeign</groupId>
             <artifactId>feign-core</artifactId>
         </dependency>
diff --git a/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareConfiguration.java
b/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareConfiguration.java
index d2ca31b..6957bb3 100644
--- a/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareConfiguration.java
+++ b/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareConfiguration.java
@@ -23,13 +23,17 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Objects;
 
+import org.apache.commons.configuration.Configuration;
+
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 
 public class LinshareConfiguration {
 
-    public static class Builder {
+    public static final String URL = "blob.export.linshare.url";
+    public static final String TOKEN = "blob.export.linshare.token";
 
+    public static class Builder {
         @FunctionalInterface
         public interface RequireUrl {
             RequireAuthorizationToken url(URL url);
@@ -62,13 +66,20 @@ public class LinshareConfiguration {
         return url -> credential -> new Builder.ReadyToBuild(url, credential);
     }
 
+    public static LinshareConfiguration from(Configuration configuration) throws MalformedURLException
{
+        return builder()
+            .urlAsString(configuration.getString(URL, null))
+            .authorizationToken(new AuthorizationToken(configuration.getString(TOKEN, null)))
+            .build();
+    }
+
     private final URL url;
     private final AuthorizationToken token;
 
     @VisibleForTesting
     LinshareConfiguration(URL url, AuthorizationToken token) {
-        Preconditions.checkNotNull(url);
-        Preconditions.checkNotNull(token);
+        Preconditions.checkNotNull(url, "'" + URL + "' can not be null");
+        Preconditions.checkNotNull(token, "'" + TOKEN + "' can not be null");
 
         this.url = url;
         this.token = token;
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 6b9316b..59d237e 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
@@ -19,16 +19,18 @@
 
 package org.apache.james.linshare;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
+import java.net.MalformedURLException;
 import java.net.URL;
 
+import org.apache.commons.configuration.PropertiesConfiguration;
 import org.junit.jupiter.api.Test;
 
 import nl.jqno.equalsverifier.EqualsVerifier;
 
 class LinshareConfigurationTest {
-
     @Test
     void shouldMatchBeanContract() {
         EqualsVerifier.forClass(LinshareConfiguration.class)
@@ -36,17 +38,53 @@ class LinshareConfigurationTest {
     }
 
     @Test
-    void constructorShouldThrowWhenPassingNullCredential() {
-        AuthorizationToken nullToken = null;
-        assertThatThrownBy(() -> new LinshareConfiguration(new URL("https://linshare.linagora.com"),
nullToken))
-            .isInstanceOf(NullPointerException.class);
+    void fromShouldThrowWhenUrlIsNull() {
+        PropertiesConfiguration configuration = new PropertiesConfiguration();
+        configuration.addProperty("blob.export.linshare.token", "token");
+        configuration.addProperty("blob.export.linshare.url", null);
+
+        assertThatThrownBy(() -> LinshareConfiguration.from(configuration)).isInstanceOf(MalformedURLException.class);
+    }
+
+    @Test
+    void fromShouldThrowWhenTokenIsNull() {
+        PropertiesConfiguration configuration = new PropertiesConfiguration();
+        configuration.addProperty("blob.export.linshare.token", null);
+        configuration.addProperty("blob.export.linshare.url", "http://127.0.0.1:8080");
+
+        assertThatThrownBy(() -> LinshareConfiguration.from(configuration)).isInstanceOf(IllegalArgumentException.class);
+    }
+
+    @Test
+    void fromShouldThrowWhenURLIsInvalid() {
+        PropertiesConfiguration configuration = new PropertiesConfiguration();
+        configuration.addProperty("blob.export.linshare.token", "token");
+        configuration.addProperty("blob.export.linshare.url", "invalid");
+
+        assertThatThrownBy(() -> LinshareConfiguration.from(configuration)).isInstanceOf(MalformedURLException.class);
+    }
+
+    @Test
+    void fromShouldThrowWhenTokenIsEmpty() {
+        PropertiesConfiguration configuration = new PropertiesConfiguration();
+        configuration.addProperty("blob.export.linshare.token", "");
+        configuration.addProperty("blob.export.linshare.url", "http://127.0.0.1:8080");
+
+        assertThatThrownBy(() -> LinshareConfiguration.from(configuration)).isInstanceOf(IllegalArgumentException.class);
     }
 
     @Test
-    void constructorShouldThrowWhenPassingNullUrl() {
-        AuthorizationToken token = new AuthorizationToken("jwt-token-at-here");
-        URL nullUrl = null;
-        assertThatThrownBy(() -> new LinshareConfiguration(nullUrl, token))
-            .isInstanceOf(NullPointerException.class);
+    void fromShouldReturnProvidedConfiguration() throws Exception {
+        String token = "token";
+        String url = "http://127.0.0.1:8080";
+
+        PropertiesConfiguration configuration = new PropertiesConfiguration();
+        configuration.addProperty("blob.export.linshare.token", token);
+        configuration.addProperty("blob.export.linshare.url", url);
+
+        assertThat(LinshareConfiguration.from(configuration)).isEqualTo(LinshareConfiguration.builder()
+            .url(new URL(url))
+            .authorizationToken(new AuthorizationToken(token))
+            .build());
     }
 }


---------------------------------------------------------------------
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