JAMES-2366 Storage of group prefixed mappings

Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/7208b3ce
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/7208b3ce
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/7208b3ce

Branch: refs/heads/master
Commit: 7208b3ced11625d3c79ddc5677faa9c1e660fde8
Parents: 43a6882
Author: benwa <btell...@linagora.com>
Authored: Fri Apr 6 11:28:06 2018 +0700
Committer: Matthieu Baechler <matth...@apache.org>
Committed: Tue Apr 24 14:45:26 2018 +0200

----------------------------------------------------------------------
 .../james/rrt/api/RecipientRewriteTable.java    |  4 +++
 .../rrt/lib/AbstractRecipientRewriteTable.java  | 21 ++++++++++++++
 .../user/lib/AbstractJamesUsersRepository.java  | 10 +++++++
 .../lib/AbstractRecipientRewriteTableTest.java  | 30 ++++++++++++++++++++
 .../james/rrt/lib/RewriteTablesStepdefs.java    | 10 +++++++
 .../resources/cucumber/rewrite_tables.feature   | 24 +++++++++++++++-
 6 files changed, 98 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/7208b3ce/server/data/data-api/src/main/java/org/apache/james/rrt/api/RecipientRewriteTable.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-api/src/main/java/org/apache/james/rrt/api/RecipientRewriteTable.java
 
b/server/data/data-api/src/main/java/org/apache/james/rrt/api/RecipientRewriteTable.java
index 92c803d..e2e7315 100644
--- 
a/server/data/data-api/src/main/java/org/apache/james/rrt/api/RecipientRewriteTable.java
+++ 
b/server/data/data-api/src/main/java/org/apache/james/rrt/api/RecipientRewriteTable.java
@@ -212,6 +212,10 @@ public interface RecipientRewriteTable {
 
     void removeForwardMapping(String user, Domain domain, String address) 
throws RecipientRewriteTableException;
 
+    void addGroupMapping(String user, Domain domain, String address) throws 
RecipientRewriteTableException;
+
+    void removeGroupMapping(String user, Domain domain, String address) throws 
RecipientRewriteTableException;
+
     class ErrorMappingException extends Exception {
 
         private static final long serialVersionUID = 2348752938798L;

http://git-wip-us.apache.org/repos/asf/james-project/blob/7208b3ce/server/data/data-library/src/main/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTable.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-library/src/main/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTable.java
 
b/server/data/data-library/src/main/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTable.java
index 2586f37..134f57c 100644
--- 
a/server/data/data-library/src/main/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTable.java
+++ 
b/server/data/data-library/src/main/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTable.java
@@ -308,6 +308,27 @@ public abstract class AbstractRecipientRewriteTable 
implements RecipientRewriteT
         removeMapping(user, domain, mapping);
     }
 
+    @Override
+    public void addGroupMapping(String user, Domain domain, String address) 
throws RecipientRewriteTableException {
+        Mapping mapping = MappingImpl.group(address)
+            .appendDomainFromThrowingSupplierIfNone(this::defaultDomain);
+
+        checkHasValidAddress(mapping);
+        checkMapping(user, domain, mapping);
+
+        LOGGER.info("Add forward mapping => {} for user: {} domain: {}", 
mapping, user, domain.name());
+        addMapping(user, domain, mapping);
+    }
+
+    @Override
+    public void removeGroupMapping(String user, Domain domain, String address) 
throws RecipientRewriteTableException {
+        Mapping mapping = MappingImpl.group(address)
+            .appendDomainFromThrowingSupplierIfNone(this::defaultDomain);
+
+        LOGGER.info("Remove forward mapping => {} for user: {} domain: {}", 
mapping, user, domain.name());
+        removeMapping(user, domain, mapping);
+    }
+
     /**
      * Return a Map which holds all Mappings
      * 

http://git-wip-us.apache.org/repos/asf/james-project/blob/7208b3ce/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractJamesUsersRepository.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractJamesUsersRepository.java
 
b/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractJamesUsersRepository.java
index 6328ed3..3f9c4d3 100644
--- 
a/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractJamesUsersRepository.java
+++ 
b/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractJamesUsersRepository.java
@@ -276,4 +276,14 @@ public abstract class AbstractJamesUsersRepository extends 
AbstractUsersReposito
     public void removeForwardMapping(String user, Domain domain, String 
address) throws RecipientRewriteTableException {
         throw new RecipientRewriteTableException("Read-Only 
RecipientRewriteTable");
     }
+
+    @Override
+    public void addGroupMapping(String user, Domain domain, String address) 
throws RecipientRewriteTableException {
+        throw new RecipientRewriteTableException("Read-Only 
RecipientRewriteTable");
+    }
+
+    @Override
+    public void removeGroupMapping(String user, Domain domain, String address) 
throws RecipientRewriteTableException {
+        throw new RecipientRewriteTableException("Read-Only 
RecipientRewriteTable");
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/7208b3ce/server/data/data-library/src/test/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTableTest.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-library/src/test/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTableTest.java
 
b/server/data/data-library/src/test/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTableTest.java
index 6ce90e4..84f204d 100644
--- 
a/server/data/data-library/src/test/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTableTest.java
+++ 
b/server/data/data-library/src/test/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTableTest.java
@@ -311,4 +311,34 @@ public abstract class AbstractRecipientRewriteTableTest {
         assertThat(virtualUserTable.getMappings(user, domain))
             .isEqualTo(MappingsImpl.empty());
     }
+
+    @Test
+    public void addGroupMappingShouldStore() throws ErrorMappingException, 
RecipientRewriteTableException {
+        String user = "test";
+        Domain domain = Domain.LOCALHOST;
+        String address = "test@localhost2";
+        String address2 = "test@james";
+
+        virtualUserTable.addMapping(user, domain, MappingImpl.group(address));
+        virtualUserTable.addMapping(user, domain, MappingImpl.group(address2));
+
+        assertThat(virtualUserTable.getMappings(user, domain)).hasSize(2);
+    }
+
+    @Test
+    public void removeGroupMappingShouldDelete() throws ErrorMappingException, 
RecipientRewriteTableException {
+        String user = "test";
+        Domain domain = Domain.LOCALHOST;
+        String address = "test@localhost2";
+        String address2 = "test@james";
+
+        virtualUserTable.addMapping(user, domain, MappingImpl.group(address));
+        virtualUserTable.addMapping(user, domain, MappingImpl.group(address2));
+
+        virtualUserTable.removeMapping(user, domain, 
MappingImpl.group(address));
+        virtualUserTable.removeMapping(user, domain, 
MappingImpl.group(address2));
+
+        assertThat(virtualUserTable.getMappings(user, domain))
+            .isEqualTo(MappingsImpl.empty());
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/7208b3ce/server/data/data-library/src/test/java/org/apache/james/rrt/lib/RewriteTablesStepdefs.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-library/src/test/java/org/apache/james/rrt/lib/RewriteTablesStepdefs.java
 
b/server/data/data-library/src/test/java/org/apache/james/rrt/lib/RewriteTablesStepdefs.java
index c274bc1..bd888ca 100644
--- 
a/server/data/data-library/src/test/java/org/apache/james/rrt/lib/RewriteTablesStepdefs.java
+++ 
b/server/data/data-library/src/test/java/org/apache/james/rrt/lib/RewriteTablesStepdefs.java
@@ -76,6 +76,11 @@ public class RewriteTablesStepdefs {
         rewriteTable.addForwardMapping(user, Domain.of(domain), address);
     }
 
+    @Given("store \"([^\"]*)\" group mapping for user \"([^\"]*)\" at domain 
\"([^\"]*)\"")
+    public void storeGroupMappingForUserAtDomain(String address, String user, 
String domain) throws Throwable {
+        rewriteTable.addGroupMapping(user, Domain.of(domain), address);
+    }
+
     @Given("recursive mapping is disable")
     public void disableRecursiveMapping() {
         rewriteTable.setRecursiveMapping(false);
@@ -106,6 +111,11 @@ public class RewriteTablesStepdefs {
         rewriteTable.removeForwardMapping(user, Domain.of(domain), address);
     }
 
+    @When("user \"([^\"]*)\" at domain \"([^\"]*)\" removes a group mapping 
\"([^\"]*)\"")
+    public void userAtDomainRemovesGroupMapping(String user, String domain, 
String address) throws Throwable {
+        rewriteTable.removeGroupMapping(user, Domain.of(domain), address);
+    }
+
     @When("wildcard address mapping \"([^\"]*)\" at domain \"([^\"]*)\" is 
removed")
     public void removeWildcardAddressMappingAtDomain(String address, String 
domain) throws Throwable {
         userAtDomainRemovesAddressMapping(RecipientRewriteTable.WILDCARD, 
domain, address);

http://git-wip-us.apache.org/repos/asf/james-project/blob/7208b3ce/server/data/data-library/src/test/resources/cucumber/rewrite_tables.feature
----------------------------------------------------------------------
diff --git 
a/server/data/data-library/src/test/resources/cucumber/rewrite_tables.feature 
b/server/data/data-library/src/test/resources/cucumber/rewrite_tables.feature
index 03d4c55..16be5f1 100644
--- 
a/server/data/data-library/src/test/resources/cucumber/rewrite_tables.feature
+++ 
b/server/data/data-library/src/test/resources/cucumber/rewrite_tables.feature
@@ -240,4 +240,26 @@ Feature: Rewrite Tables tests
     Given store "test@localhost2" forward mapping for user "test" at domain 
"localhost"
     And store "test@james" forward mapping for user "test" at domain 
"localhost"
     When user "test" at domain "localhost" removes a forward mapping 
"test@james"
-    Then mappings for user "test" at domain "localhost" should contain only 
"forward:test@localhost2"
\ No newline at end of file
+    Then mappings for user "test" at domain "localhost" should contain only 
"forward:test@localhost2"
+
+# Group mapping
+
+  Scenario: stored group mapping should be retrieved when one mapping is 
matching
+    Given store "test@localhost2" group mapping for user "test" at domain 
"localhost"
+    Then mappings for user "test" at domain "localhost" should contain only 
"group:test@localhost2"
+
+  Scenario: stored group mapping should be retrieved when two mappings are 
matching
+    Given store "test@localhost2" group mapping for user "test" at domain 
"localhost"
+    And store "test@james" group mapping for user "test" at domain "localhost"
+    Then mappings for user "test" at domain "localhost" should contain only 
"group:test@localhost2, group:test@james"
+
+  Scenario: stored group mapping should not be retrieved by another user
+    Given store "test@localhost2" group mapping for user "test" at domain 
"localhost"
+    And store "test@james" group mapping for user "test" at domain "localhost"
+    Then mappings for user "test2" at domain "localhost" should be empty
+
+  Scenario: removing a stored group mapping should work
+    Given store "test@localhost2" group mapping for user "test" at domain 
"localhost"
+    And store "test@james" group mapping for user "test" at domain "localhost"
+    When user "test" at domain "localhost" removes a group mapping "test@james"
+    Then mappings for user "test" at domain "localhost" should contain only 
"group:test@localhost2"
\ No newline at end of file


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

Reply via email to