james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [03/24] james-project git commit: JAMES-2366 Improve XMLRecipientRewriteTableTest
Date Fri, 06 Apr 2018 13:20:22 GMT
JAMES-2366 Improve XMLRecipientRewriteTableTest


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

Branch: refs/heads/master
Commit: 57c417c612def7ce832ca712eb6bccdc03e37db7
Parents: c9078f2
Author: benwa <btellier@linagora.com>
Authored: Fri Mar 30 09:37:14 2018 +0700
Committer: Antoine Duprat <aduprat@linagora.com>
Committed: Fri Apr 6 14:39:17 2018 +0200

----------------------------------------------------------------------
 .../rrt/file/XMLRecipientRewriteTableTest.java  | 68 ++++++--------------
 .../org/apache/james/rrt/lib/MappingImpl.java   |  4 ++
 2 files changed, 24 insertions(+), 48 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/57c417c6/server/data/data-file/src/test/java/org/apache/james/rrt/file/XMLRecipientRewriteTableTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-file/src/test/java/org/apache/james/rrt/file/XMLRecipientRewriteTableTest.java
b/server/data/data-file/src/test/java/org/apache/james/rrt/file/XMLRecipientRewriteTableTest.java
index f5b7712..5f45be3 100644
--- a/server/data/data-file/src/test/java/org/apache/james/rrt/file/XMLRecipientRewriteTableTest.java
+++ b/server/data/data-file/src/test/java/org/apache/james/rrt/file/XMLRecipientRewriteTableTest.java
@@ -31,7 +31,6 @@ import org.apache.james.rrt.lib.Mapping.Type;
 import org.apache.james.rrt.lib.MappingImpl;
 import org.apache.james.rrt.lib.Mappings;
 import org.apache.james.rrt.lib.MappingsImpl;
-import org.apache.james.rrt.lib.MappingsImpl.Builder;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Ignore;
@@ -59,7 +58,7 @@ public class XMLRecipientRewriteTableTest extends AbstractRecipientRewriteTableT
 
 
     @Override
-    protected AbstractRecipientRewriteTable getRecipientRewriteTable() throws Exception {
+    protected AbstractRecipientRewriteTable getRecipientRewriteTable() {
         return new XMLRecipientRewriteTable();
     }
 
@@ -70,67 +69,40 @@ public class XMLRecipientRewriteTableTest extends AbstractRecipientRewriteTableT
     }
 
     @Override
-    protected void addMapping(String user, Domain domain, String mapping, Type type) throws
-            RecipientRewriteTableException {
+    protected void addMapping(String user, Domain domain, String mapping, Type type) throws
RecipientRewriteTableException {
+        Mappings mappings = Optional.ofNullable(virtualUserTable.getUserDomainMappings(user,
domain))
+            .orElse(MappingsImpl.empty());
 
-        Mappings mappings = virtualUserTable.getUserDomainMappings(user, domain);
-
-        if (mappings != null) {
-            removeMappingsFromConfig(user, domain, mappings);
-        }
-
-        Builder builder = MappingsImpl.from(Optional.ofNullable(mappings).orElse(MappingsImpl.empty()));
-
-        builder.add(type.asPrefix() + mapping);
-
-        Mappings updatedMappings = builder.build();
-        
-        if (!updatedMappings.isEmpty()) {
-            defaultConfiguration.addProperty("mapping", user + "@" + domain.asString() +
"=" + updatedMappings.serialize());
-        }
-
-        try {
-            virtualUserTable.configure(defaultConfiguration);
-        } catch (Exception e) {
-            if (updatedMappings.size() > 0) {
-                throw new RecipientRewriteTableException("Error update mapping", e);
-            }
-        }
+        Mappings updatedMappings = MappingsImpl.from(mappings)
+            .add(MappingImpl.of(type, mapping))
+            .build();
 
+        updateConfiguration(user, domain, mappings, updatedMappings);
     }
 
     @Override
-    protected void removeMapping(String user, Domain domain, String mapping, Type type) throws
-            RecipientRewriteTableException {
+    protected void removeMapping(String user, Domain domain, String mapping, Type type) throws
RecipientRewriteTableException {
+        Mappings oldMappings = Optional.ofNullable(virtualUserTable.getUserDomainMappings(user,
domain))
+            .orElseThrow(() -> new RecipientRewriteTableException("Cannot remove from
null mappings"));
 
-        Mappings mappings = virtualUserTable.getUserDomainMappings(user, domain);
+        Mappings updatedMappings = oldMappings.remove(MappingImpl.of(type, mapping));
 
-        if (mappings == null) {
-            throw new RecipientRewriteTableException("Cannot remove from null mappings");
-        }
+        updateConfiguration(user, domain, oldMappings, updatedMappings);
+    }
 
-        removeMappingsFromConfig(user, domain, mappings);
-
-        if (type == Type.Error) {
-            mappings = mappings.remove(MappingImpl.error(mapping));
-        } else if (type == Type.Regex) {
-            mappings = mappings.remove(MappingImpl.regex(mapping));
-        } else if (type == Type.Address) {
-            mappings = mappings.remove(MappingImpl.address(mapping));
-        } else if (type == Type.Domain) {
-            mappings = mappings.remove(MappingImpl.domain(Domain.of(mapping)));
-        } else if (type == Type.Forward) {
-            mappings = mappings.remove(MappingImpl.forward(mapping));
+    private void updateConfiguration(String user, Domain domain, Mappings oldMappings, Mappings
updatedMappings) throws RecipientRewriteTableException {
+        if (oldMappings != null) {
+            removeMappingsFromConfig(user, domain, oldMappings);
         }
 
-        if (mappings.size() > 0) {
-            defaultConfiguration.addProperty("mapping", user + "@" + domain.asString() +
"=" + mappings.serialize());
+        if (!updatedMappings.isEmpty()) {
+            defaultConfiguration.addProperty("mapping", user + "@" + domain.asString() +
"=" + updatedMappings.serialize());
         }
 
         try {
             virtualUserTable.configure(defaultConfiguration);
         } catch (Exception e) {
-            if (mappings.size() > 0) {
+            if (updatedMappings.size() > 0) {
                 throw new RecipientRewriteTableException("Error update mapping", e);
             }
         }

http://git-wip-us.apache.org/repos/asf/james-project/blob/57c417c6/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingImpl.java
----------------------------------------------------------------------
diff --git a/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingImpl.java
b/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingImpl.java
index 1dc0c92..0c663a9 100644
--- a/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingImpl.java
+++ b/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingImpl.java
@@ -37,6 +37,10 @@ public class MappingImpl implements Mapping, Serializable {
         Type type = Mapping.detectType(mapping);
         return new MappingImpl(type, type.withoutPrefix(mapping));
     }
+
+    public static MappingImpl of(Type type, String mapping) {
+        return new MappingImpl(type, mapping);
+    }
     
     public static MappingImpl address(String mapping) {
         return new MappingImpl(Type.Address, mapping);


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