JAMES-1589 Add tests for auto-detected domain upon "contains"
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/ff51cee2 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/ff51cee2 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/ff51cee2 Branch: refs/heads/master Commit: ff51cee2435a144024c45f23b2f86f867b06cb96 Parents: f375965 Author: benwa <btell...@linagora.com> Authored: Fri Jul 28 17:03:09 2017 +0700 Committer: benwa <btell...@linagora.com> Committed: Tue Aug 8 17:08:50 2017 +0700 ---------------------------------------------------------------------- .../cassandra/CassandraDomainList.java | 2 +- .../james/domainlist/xml/XMLDomainList.java | 2 +- .../james/domainlist/hbase/HBaseDomainList.java | 2 +- .../james/domainlist/jpa/JPADomainList.java | 2 +- .../domainlist/lib/AbstractDomainList.java | 10 ++- .../AbstractDomainListPrivateMethodsTest.java | 82 +++++++++++++++++++- .../domainlist/memory/MemoryDomainList.java | 2 +- 7 files changed, 94 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/ff51cee2/server/data/data-cassandra/src/main/java/org/apache/james/domainlist/cassandra/CassandraDomainList.java ---------------------------------------------------------------------- diff --git a/server/data/data-cassandra/src/main/java/org/apache/james/domainlist/cassandra/CassandraDomainList.java b/server/data/data-cassandra/src/main/java/org/apache/james/domainlist/cassandra/CassandraDomainList.java index efbc554..ae889aa 100644 --- a/server/data/data-cassandra/src/main/java/org/apache/james/domainlist/cassandra/CassandraDomainList.java +++ b/server/data/data-cassandra/src/main/java/org/apache/james/domainlist/cassandra/CassandraDomainList.java @@ -100,7 +100,7 @@ public class CassandraDomainList extends AbstractDomainList { } @Override - public boolean containsDomain(String domain) throws DomainListException { + protected boolean containsDomainInternal(String domain) throws DomainListException { return executor.executeSingleRow(readStatement.bind() .setString(DOMAIN, domain.toLowerCase(Locale.US))) .join() http://git-wip-us.apache.org/repos/asf/james-project/blob/ff51cee2/server/data/data-file/src/main/java/org/apache/james/domainlist/xml/XMLDomainList.java ---------------------------------------------------------------------- diff --git a/server/data/data-file/src/main/java/org/apache/james/domainlist/xml/XMLDomainList.java b/server/data/data-file/src/main/java/org/apache/james/domainlist/xml/XMLDomainList.java index 34376a9..6848ab5 100644 --- a/server/data/data-file/src/main/java/org/apache/james/domainlist/xml/XMLDomainList.java +++ b/server/data/data-file/src/main/java/org/apache/james/domainlist/xml/XMLDomainList.java @@ -59,7 +59,7 @@ public class XMLDomainList extends AbstractDomainList implements Configurable { } @Override - public boolean containsDomain(String domains) throws DomainListException { + protected boolean containsDomainInternal(String domains) throws DomainListException { return domainNames.contains(domains.toLowerCase(Locale.US)); } http://git-wip-us.apache.org/repos/asf/james-project/blob/ff51cee2/server/data/data-hbase/src/main/java/org/apache/james/domainlist/hbase/HBaseDomainList.java ---------------------------------------------------------------------- diff --git a/server/data/data-hbase/src/main/java/org/apache/james/domainlist/hbase/HBaseDomainList.java b/server/data/data-hbase/src/main/java/org/apache/james/domainlist/hbase/HBaseDomainList.java index 6342e01..dbeb21b 100644 --- a/server/data/data-hbase/src/main/java/org/apache/james/domainlist/hbase/HBaseDomainList.java +++ b/server/data/data-hbase/src/main/java/org/apache/james/domainlist/hbase/HBaseDomainList.java @@ -52,7 +52,7 @@ public class HBaseDomainList extends AbstractDomainList { * @see org.apache.james.domainlist.api.DomainList#containsDomain(String) */ @Override - public boolean containsDomain(String domain) throws DomainListException { + protected boolean containsDomainInternal(String domain) throws DomainListException { HTableInterface table = null; try { table = TablePool.getInstance().getDomainlistTable(); http://git-wip-us.apache.org/repos/asf/james-project/blob/ff51cee2/server/data/data-jpa/src/main/java/org/apache/james/domainlist/jpa/JPADomainList.java ---------------------------------------------------------------------- diff --git a/server/data/data-jpa/src/main/java/org/apache/james/domainlist/jpa/JPADomainList.java b/server/data/data-jpa/src/main/java/org/apache/james/domainlist/jpa/JPADomainList.java index 14f57fe..af9ea44 100644 --- a/server/data/data-jpa/src/main/java/org/apache/james/domainlist/jpa/JPADomainList.java +++ b/server/data/data-jpa/src/main/java/org/apache/james/domainlist/jpa/JPADomainList.java @@ -86,7 +86,7 @@ public class JPADomainList extends AbstractDomainList { } @Override - public boolean containsDomain(String domain) throws DomainListException { + protected boolean containsDomainInternal(String domain) throws DomainListException { String lowerCasedDomain = domain.toLowerCase(Locale.US); EntityManager entityManager = entityManagerFactory.createEntityManager(); final EntityTransaction transaction = entityManager.getTransaction(); http://git-wip-us.apache.org/repos/asf/james-project/blob/ff51cee2/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java ---------------------------------------------------------------------- diff --git a/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java b/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java index afc8a1d..d2b564c 100644 --- a/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java +++ b/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java @@ -118,6 +118,12 @@ public abstract class AbstractDomainList implements DomainList, LogEnabled, Conf } @Override + public boolean containsDomain(String domain) throws DomainListException { + boolean internalAnswer = containsDomainInternal(domain); + return internalAnswer || getDomains().contains(domain); + } + + @Override public List<String> getDomains() throws DomainListException { List<String> domains = getDomainListInternal(); @@ -153,7 +159,7 @@ public abstract class AbstractDomainList implements DomainList, LogEnabled, Conf } getLogger().info("Local host is: " + hostName); - if (!hostName.equals("localhost")) { + if (hostName != null && !hostName.equals("localhost")) { return ImmutableList.of(hostName.toLowerCase(Locale.US)); } } @@ -242,4 +248,6 @@ public abstract class AbstractDomainList implements DomainList, LogEnabled, Conf */ protected abstract List<String> getDomainListInternal() throws DomainListException; + protected abstract boolean containsDomainInternal(String domain) throws DomainListException; + } http://git-wip-us.apache.org/repos/asf/james-project/blob/ff51cee2/server/data/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListPrivateMethodsTest.java ---------------------------------------------------------------------- diff --git a/server/data/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListPrivateMethodsTest.java b/server/data/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListPrivateMethodsTest.java index 9255dfd..5098c08 100644 --- a/server/data/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListPrivateMethodsTest.java +++ b/server/data/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListPrivateMethodsTest.java @@ -63,7 +63,7 @@ public class AbstractDomainListPrivateMethodsTest { } @Override - public boolean containsDomain(String domain) throws DomainListException { + protected boolean containsDomainInternal(String domain) throws DomainListException { return domains.contains(domain); } @@ -145,7 +145,7 @@ public class AbstractDomainListPrivateMethodsTest { } @Test - public void setDefaultDomainNotFailWhenDomainContained() throws Exception { + public void setDefaultDomainShouldNotFailWhenDomainContained() throws Exception { HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class); String expectedDefaultDomain = "myDomain.org"; when(configuration.getString(AbstractDomainList.CONFIGURE_DEFAULT_DOMAIN, AbstractDomainList.LOCALHOST)) @@ -238,4 +238,82 @@ public class AbstractDomainListPrivateMethodsTest { assertThat(domainList.getDomains()).containsOnly(detected, detectedIp1, added, detectedIp2); } + + @Test + public void getDomainsShouldListAddedDomain() throws Exception { + HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class); + + when(configuration.getBoolean(AbstractDomainList.CONFIGURE_AUTODETECT, true)).thenReturn(false); + when(configuration.getBoolean(AbstractDomainList.CONFIGURE_AUTODETECT_IP, true)).thenReturn(false); + String domain = "added.tld"; + domainList.addDomain(domain); + domainList.configure(configuration); + + assertThat(domainList.getDomains()).containsOnly(domain); + } + + @Test + public void containsDomainShouldReturnDetectedIp() throws Exception { + String detected = "detected.tld"; + String detectedIp = "148.25.32.1"; + when(dnsService.getHostName(any(InetAddress.class))).thenReturn(detected); + InetAddress detectedAddress = mock(InetAddress.class); + when(detectedAddress.getHostAddress()).thenReturn(detectedIp); + when(dnsService.getAllByName(detected)).thenReturn(ImmutableList.of(detectedAddress)); + + assertThat(domainList.containsDomain(detectedIp)).isTrue(); + } + + @Test + public void containsDomainShouldReturnTrueWhenDomainIsContained() throws Exception { + HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class); + + when(configuration.getBoolean(AbstractDomainList.CONFIGURE_AUTODETECT, true)).thenReturn(false); + when(configuration.getBoolean(AbstractDomainList.CONFIGURE_AUTODETECT_IP, true)).thenReturn(false); + String domain = "added.tld"; + domainList.addDomain(domain); + domainList.configure(configuration); + + assertThat(domainList.containsDomain(domain)).isTrue(); + } + + @Test + public void containsDomainShouldReturnFalseWhenDomainIsNotContained() throws Exception { + HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class); + + when(configuration.getBoolean(AbstractDomainList.CONFIGURE_AUTODETECT, true)).thenReturn(false); + when(configuration.getBoolean(AbstractDomainList.CONFIGURE_AUTODETECT_IP, true)).thenReturn(false); + String domain = "added.tld"; + domainList.configure(configuration); + + assertThat(domainList.containsDomain(domain)).isFalse(); + } + + @Test + public void containsDomainShouldNotInteractWithDNSWhenDisabled () throws Exception { + HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class); + + when(configuration.getBoolean(AbstractDomainList.CONFIGURE_AUTODETECT, true)).thenReturn(false); + when(configuration.getBoolean(AbstractDomainList.CONFIGURE_AUTODETECT_IP, true)).thenReturn(false); + domainList.configure(configuration); + domainList.containsDomain("added.tld"); + + verifyZeroInteractions(dnsService); + } + + @Test + public void containsDomainShouldReturnDetectedDomains() throws Exception { + HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class); + + when(configuration.getBoolean(AbstractDomainList.CONFIGURE_AUTODETECT, true)).thenReturn(true); + when(configuration.getBoolean(AbstractDomainList.CONFIGURE_AUTODETECT_IP, true)).thenReturn(false); + domainList.setLog(LOGGER); + domainList.configure(configuration); + + String detected = "detected.tld"; + when(dnsService.getHostName(any(InetAddress.class))).thenReturn(detected); + + assertThat(domainList.containsDomain(detected)).isTrue(); + } + } http://git-wip-us.apache.org/repos/asf/james-project/blob/ff51cee2/server/data/data-memory/src/main/java/org/apache/james/domainlist/memory/MemoryDomainList.java ---------------------------------------------------------------------- diff --git a/server/data/data-memory/src/main/java/org/apache/james/domainlist/memory/MemoryDomainList.java b/server/data/data-memory/src/main/java/org/apache/james/domainlist/memory/MemoryDomainList.java index d890c33..5e135f7 100644 --- a/server/data/data-memory/src/main/java/org/apache/james/domainlist/memory/MemoryDomainList.java +++ b/server/data/data-memory/src/main/java/org/apache/james/domainlist/memory/MemoryDomainList.java @@ -42,7 +42,7 @@ public class MemoryDomainList extends AbstractDomainList { } @Override - public boolean containsDomain(String domain) throws DomainListException { + protected boolean containsDomainInternal(String domain) throws DomainListException { return domains.contains(domain.toLowerCase(Locale.US)); } --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org