james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [james-project] 04/06: JAMES-2708 Putting cleanup strategy to linshare extension
Date Thu, 11 Apr 2019 10:15:43 GMT
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 356146a2766febd191841bae7dfedbb110fcc8dd
Author: Tran Tien Duc <dtran@linagora.com>
AuthorDate: Mon Apr 8 12:00:49 2019 +0700

    JAMES-2708 Putting cleanup strategy to linshare extension
---
 third-party/linshare/pom.xml                       |  45 ++++++++-
 .../org/apache/james/linshare/client/User.java     | 102 +++++++++++++++++++++
 .../java/org/apache/james/linshare/Linshare.java   |   4 +
 .../apache/james/linshare/LinshareExtension.java   |  80 +++++++++++++++-
 .../org/apache/james/linshare/LinshareFixture.java |  83 +++++++++++++++++
 .../UserTest.java}                                 |  25 ++---
 6 files changed, 317 insertions(+), 22 deletions(-)

diff --git a/third-party/linshare/pom.xml b/third-party/linshare/pom.xml
index 5fc4a83..576c3ee 100644
--- a/third-party/linshare/pom.xml
+++ b/third-party/linshare/pom.xml
@@ -30,14 +30,37 @@
 
     <dependencies>
         <dependency>
+            <groupId>${james.groupId}</groupId>
+            <artifactId>james-core</artifactId>
+        </dependency>
+        <dependency>
             <groupId>ch.qos.logback</groupId>
             <artifactId>logback-classic</artifactId>
             <scope>test</scope>
         </dependency>
         <dependency>
-            <groupId>org.slf4j</groupId>
-            <artifactId>jcl-over-slf4j</artifactId>
-            <scope>test</scope>
+            <groupId>com.github.steveash.guavate</groupId>
+            <artifactId>guavate</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>io.github.openfeign</groupId>
+            <artifactId>feign-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>io.github.openfeign</groupId>
+            <artifactId>feign-jackson</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>io.github.openfeign</groupId>
+            <artifactId>feign-slf4j</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>io.github.openfeign.form</groupId>
+            <artifactId>feign-form</artifactId>
         </dependency>
         <dependency>
             <groupId>io.rest-assured</groupId>
@@ -45,6 +68,16 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>nl.jqno.equalsverifier</groupId>
+            <artifactId>equalsverifier</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.assertj</groupId>
+            <artifactId>assertj-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.junit.jupiter</groupId>
             <artifactId>junit-jupiter-engine</artifactId>
             <scope>test</scope>
@@ -56,6 +89,11 @@
         </dependency>
         <dependency>
             <groupId>org.slf4j</groupId>
+            <artifactId>jcl-over-slf4j</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
         </dependency>
         <dependency>
@@ -64,5 +102,4 @@
             <scope>test</scope>
         </dependency>
     </dependencies>
-
 </project>
diff --git a/third-party/linshare/src/main/java/org/apache/james/linshare/client/User.java
b/third-party/linshare/src/main/java/org/apache/james/linshare/client/User.java
new file mode 100644
index 0000000..357523d
--- /dev/null
+++ b/third-party/linshare/src/main/java/org/apache/james/linshare/client/User.java
@@ -0,0 +1,102 @@
+/****************************************************************
+ * 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;
+
+public class User {
+
+    private final String uuid;
+    private final String domain;
+    private final String firstName;
+    private final String lastName;
+    private final String mail;
+    private final String accountType;
+    private final boolean external;
+
+    @VisibleForTesting
+    User(@JsonProperty("uuid") String uuid,
+         @JsonProperty("domain") String domain,
+         @JsonProperty("firstName") String firstName,
+         @JsonProperty("lastName") String lastName,
+         @JsonProperty("mail") String mail,
+         @JsonProperty("accountType") String accountType,
+         @JsonProperty("external") boolean external) {
+        this.uuid = uuid;
+        this.domain = domain;
+        this.firstName = firstName;
+        this.lastName = lastName;
+        this.mail = mail;
+        this.accountType = accountType;
+        this.external = external;
+    }
+
+    public String getUuid() {
+        return uuid;
+    }
+
+    public String getDomain() {
+        return domain;
+    }
+
+    public String getFirstName() {
+        return firstName;
+    }
+
+    public String getLastName() {
+        return lastName;
+    }
+
+    public String getMail() {
+        return mail;
+    }
+
+    public String getAccountType() {
+        return accountType;
+    }
+
+    public boolean isExternal() {
+        return external;
+    }
+
+    @Override
+    public final boolean equals(Object o) {
+        if (o instanceof User) {
+            User user = (User) o;
+
+            return Objects.equals(this.external, user.external)
+                && Objects.equals(this.uuid, user.uuid)
+                && Objects.equals(this.domain, user.domain)
+                && Objects.equals(this.firstName, user.firstName)
+                && Objects.equals(this.lastName, user.lastName)
+                && Objects.equals(this.mail, user.mail)
+                && Objects.equals(this.accountType, user.accountType);
+        }
+        return false;
+    }
+
+    @Override
+    public final int hashCode() {
+        return Objects.hash(uuid, domain, firstName, lastName, mail, accountType, external);
+    }
+}
\ No newline at end of file
diff --git a/third-party/linshare/src/test/java/org/apache/james/linshare/Linshare.java b/third-party/linshare/src/test/java/org/apache/james/linshare/Linshare.java
index 3bd4f47..4112f75 100644
--- a/third-party/linshare/src/test/java/org/apache/james/linshare/Linshare.java
+++ b/third-party/linshare/src/test/java/org/apache/james/linshare/Linshare.java
@@ -75,6 +75,10 @@ public class Linshare {
         return linshareBackend.getContainerIpAddress();
     }
 
+    public String getUrl() {
+        return "http://" + getIp() + ":" + getPort();
+    }
+
     private GenericContainer createDockerDatabase() {
         return new GenericContainer<>("linagora/linshare-database:2.2")
             .withNetworkAliases("database", "linshare_database")
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 557b29d..e1527f2 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
@@ -18,11 +18,56 @@
  ****************************************************************/
 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 java.util.List;
+import java.util.Optional;
+
+import org.apache.james.linshare.client.LinshareAPI;
+import org.apache.james.linshare.client.User;
 import org.junit.jupiter.api.extension.AfterAllCallback;
+import org.junit.jupiter.api.extension.AfterEachCallback;
 import org.junit.jupiter.api.extension.BeforeAllCallback;
 import org.junit.jupiter.api.extension.ExtensionContext;
 
-public class LinshareExtension implements BeforeAllCallback, AfterAllCallback {
+import com.github.fge.lambdas.Throwing;
+
+import feign.Feign;
+import feign.Headers;
+import feign.Logger;
+import feign.RequestLine;
+import feign.auth.BasicAuthRequestInterceptor;
+import feign.form.FormEncoder;
+import feign.jackson.JacksonDecoder;
+import feign.jackson.JacksonEncoder;
+import feign.slf4j.Slf4jLogger;
+
+public class LinshareExtension implements BeforeAllCallback, AfterEachCallback, AfterAllCallback
{
+
+    private interface LinshareAPIForTesting {
+
+        static LinshareAPIForTesting from(LinshareFixture.Credential credential, Linshare
linshare) {
+
+            return Feign.builder()
+                .requestInterceptor(new BasicAuthRequestInterceptor(credential.getUsername(),
credential.getPassword()))
+                .logger(new Slf4jLogger(LinshareAPIForTesting.class))
+                .logLevel(Logger.Level.FULL)
+                .encoder(new FormEncoder(new JacksonEncoder()))
+                .decoder(new JacksonDecoder())
+                .target(LinshareAPIForTesting.class, linshare.getUrl());
+        }
+
+        @RequestLine("GET /linshare/webservice/rest/user/v2/authentication/jwt")
+        @Headers(ACCEPT_APPLICATION_JSON)
+        AuthorizationToken jwt();
+
+        @RequestLine("GET /linshare/webservice/rest/user/v2/users")
+        @Headers(ACCEPT_APPLICATION_JSON)
+        List<User> allUsers();
+    }
+
     private Linshare linshare;
 
     @Override
@@ -32,6 +77,11 @@ public class LinshareExtension implements BeforeAllCallback, AfterAllCallback
{
     }
 
     @Override
+    public void afterEach(ExtensionContext context) throws Exception {
+        deleteAllUsersDocuments();
+    }
+
+    @Override
     public void afterAll(ExtensionContext extensionContext) {
         linshare.stop();
     }
@@ -39,4 +89,32 @@ public class LinshareExtension implements BeforeAllCallback, AfterAllCallback
{
     public Linshare getLinshare() {
         return linshare;
     }
+
+    public LinshareAPI getAPIFor(LinshareFixture.Credential credential) throws Exception
{
+        return LinshareAPI.from(configurationWithJwtFor(credential));
+    }
+
+    private void deleteAllUsersDocuments() {
+        LinshareAPIForTesting.from(USER_1, linshare)
+            .allUsers()
+            .stream()
+            .map(this::getUsernamePassword)
+            .map(Throwing.function(this::configurationWithJwtFor))
+            .map(LinshareAPI::from)
+            .forEach(LinshareAPI::deleteAllDocuments);
+    }
+
+    private LinshareFixture.Credential getUsernamePassword(User user) {
+        return Optional.ofNullable(USER_CREDENTIAL_MAP.get(user.getMail()))
+            .orElseThrow(() -> new RuntimeException("cannot get token of user " + user.getMail()));
+    }
+
+    private LinshareConfiguration configurationWithJwtFor(LinshareFixture.Credential credential)
throws Exception {
+        AuthorizationToken token = LinshareAPIForTesting.from(credential, linshare).jwt();
+
+        return LinshareConfiguration.builder()
+            .urlAsString(linshare.getUrl())
+            .authorizationToken(token)
+            .build();
+    }
 }
diff --git a/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareFixture.java
b/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareFixture.java
new file mode 100644
index 0000000..9d53133
--- /dev/null
+++ b/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareFixture.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;
+
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+
+import org.testcontainers.shaded.com.google.common.collect.ImmutableList;
+
+import com.github.steveash.guavate.Guavate;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+
+public interface LinshareFixture {
+
+    class Credential {
+
+        private final String username;
+        private final String password;
+
+        @VisibleForTesting
+        public Credential(String username, String password) {
+            Preconditions.checkNotNull(username);
+            Preconditions.checkNotNull(password);
+
+            this.username = username;
+            this.password = password;
+        }
+
+        public String getUsername() {
+            return username;
+        }
+
+        public String getPassword() {
+            return password;
+        }
+    }
+
+    Credential USER_1 = new Credential("user1@linshare.org", "password1");
+    Credential USER_2 = new Credential("user2@linshare.org", "password2");
+    Credential USER_3 = new Credential("user3@linshare.org", "password3");
+    Credential USER_4 = new Credential("user4@linshare.org", "password4");
+    Credential USER_5 = new Credential("user5@linshare.org", "password5");
+    Credential USER_6 = new Credential("user6@linshare.org", "password6");
+    Credential USER_7 = new Credential("user7@linshare.org", "password7");
+    Credential LINAGORA = new Credential("linagora@linshare.org", "linagora");
+    Credential EXTERNAL_1 = new Credential("external1@linshare.org", "password1");
+    Credential EXTERNAL_2 = new Credential("external2@linshare.org", "password2");
+
+    List<Credential> USER_CREDENTIALS = ImmutableList.of(
+        USER_1,
+        USER_2,
+        USER_3,
+        USER_4,
+        USER_5,
+        USER_6,
+        USER_7,
+        LINAGORA,
+        EXTERNAL_1,
+        EXTERNAL_2);
+
+    Map<String, Credential> USER_CREDENTIAL_MAP = USER_CREDENTIALS.stream()
+        .collect(Guavate.toImmutableMap(Credential::getUsername, Function.identity()));
+
+}
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/client/UserTest.java
similarity index 65%
copy from third-party/linshare/src/test/java/org/apache/james/linshare/LinshareExtension.java
copy to third-party/linshare/src/test/java/org/apache/james/linshare/client/UserTest.java
index 557b29d..9d05129 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/client/UserTest.java
@@ -16,27 +16,18 @@
  * specific language governing permissions and limitations      *
  * under the License.                                           *
  ****************************************************************/
-package org.apache.james.linshare;
 
-import org.junit.jupiter.api.extension.AfterAllCallback;
-import org.junit.jupiter.api.extension.BeforeAllCallback;
-import org.junit.jupiter.api.extension.ExtensionContext;
+package org.apache.james.linshare.client;
 
-public class LinshareExtension implements BeforeAllCallback, AfterAllCallback {
-    private Linshare linshare;
+import org.junit.jupiter.api.Test;
 
-    @Override
-    public void beforeAll(ExtensionContext context) {
-        linshare = new Linshare();
-        linshare.start();
-    }
+import nl.jqno.equalsverifier.EqualsVerifier;
 
-    @Override
-    public void afterAll(ExtensionContext extensionContext) {
-        linshare.stop();
-    }
+class UserTest {
 
-    public Linshare getLinshare() {
-        return linshare;
+    @Test
+    void shouldMatchBeanContract() {
+        EqualsVerifier.forClass(User.class)
+            .verify();
     }
 }


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