JAMES-1773 Add tests for JamesMailetContext. Required to test RecipientIsLocal.
Are tested : isLocalServer, isLocalUser, isLocalEmail Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/992ab012 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/992ab012 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/992ab012 Branch: refs/heads/master Commit: 992ab012b2b969fa3445dc33de28590c28621f55 Parents: d0114b5 Author: Benoit Tellier <[email protected]> Authored: Wed Aug 10 10:06:08 2016 +0700 Committer: Benoit Tellier <[email protected]> Committed: Thu Aug 18 17:18:15 2016 +0700 ---------------------------------------------------------------------- server/mailet/mailetcontainer-camel/pom.xml | 11 ++ .../impl/JamesMailetContextTest.java | 142 +++++++++++++++++++ 2 files changed, 153 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/992ab012/server/mailet/mailetcontainer-camel/pom.xml ---------------------------------------------------------------------- diff --git a/server/mailet/mailetcontainer-camel/pom.xml b/server/mailet/mailetcontainer-camel/pom.xml index 6de3849..c3a591c 100644 --- a/server/mailet/mailetcontainer-camel/pom.xml +++ b/server/mailet/mailetcontainer-camel/pom.xml @@ -113,6 +113,11 @@ </dependency> <dependency> <groupId>org.apache.james</groupId> + <artifactId>james-server-data-memory</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.james</groupId> <artifactId>apache-mailet-base</artifactId> <classifier>tests</classifier> <scope>test</scope> @@ -129,6 +134,12 @@ <scope>test</scope> </dependency> <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <version>${assertj-1.version}</version> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <scope>test</scope> http://git-wip-us.apache.org/repos/asf/james-project/blob/992ab012/server/mailet/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/impl/JamesMailetContextTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/impl/JamesMailetContextTest.java b/server/mailet/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/impl/JamesMailetContextTest.java new file mode 100644 index 0000000..a70d8a9 --- /dev/null +++ b/server/mailet/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/impl/JamesMailetContextTest.java @@ -0,0 +1,142 @@ +/**************************************************************** + * 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.mailetcontainer.impl; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.apache.commons.configuration.HierarchicalConfiguration; +import org.apache.james.domainlist.memory.MemoryDomainList; +import org.apache.james.user.memory.MemoryUsersRepository; +import org.apache.mailet.MailAddress; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class JamesMailetContextTest { + private static final Logger LOGGER = LoggerFactory.getLogger(JamesMailetContextTest.class); + + public static final String DOMAIN_COM = "domain.com"; + public static final String USERNAME = "user"; + public static final String USERMAIL = USERNAME + "@" + DOMAIN_COM; + public static final String PASSWORD = "password"; + + private MemoryDomainList domainList; + private MemoryUsersRepository usersRepository; + private JamesMailetContext testee; + private MailAddress mailAddress; + + @Before + public void setUp() throws Exception { + domainList = new MemoryDomainList(); + domainList.setLog(LOGGER); + usersRepository = MemoryUsersRepository.withVirtualHosting(); + usersRepository.setDomainList(domainList); + testee = new JamesMailetContext(); + testee.setDomainList(domainList); + testee.setUsersRepository(usersRepository); + mailAddress = new MailAddress(USERMAIL); + } + + @Test + public void isLocalUserShouldBeFalseOnNullUser() { + assertThat(testee.isLocalUser(null)).isFalse(); + } + + @Test + public void isLocalServerShouldBeFalseWhenDomainDoNotExist() { + assertThat(testee.isLocalServer(DOMAIN_COM)).isFalse(); + } + + @Test + public void isLocalServerShouldBeTrueWhenDomainExist() throws Exception { + domainList.addDomain(DOMAIN_COM); + + assertThat(testee.isLocalServer(DOMAIN_COM)).isTrue(); + } + + @Test + public void isLocalUserShouldBeTrueWhenUsernameExist() throws Exception { + domainList.addDomain(DOMAIN_COM); + usersRepository.addUser(USERMAIL, PASSWORD); + + assertThat(testee.isLocalUser(USERMAIL)).isTrue(); + } + + @Test + public void isLocalUserShouldReturnTrueWhenUsedWithLocalPartAndUserExistOnDefaultDomain() throws Exception { + HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class); + when(configuration.getString(eq("defaultDomain"), any(String.class))) + .thenReturn(DOMAIN_COM); + + domainList.configure(configuration); + domainList.addDomain(DOMAIN_COM); + usersRepository.addUser(USERMAIL, PASSWORD); + + assertThat(testee.isLocalUser(USERNAME)).isTrue(); + } + + @Test + public void isLocalUserShouldReturnFalseWhenUsedWithLocalPartAndUserDoNotExistOnDefaultDomain() throws Exception { + HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class); + when(configuration.getString(eq("defaultDomain"), any(String.class))) + .thenReturn("any"); + + domainList.configure(configuration); + domainList.addDomain(DOMAIN_COM); + usersRepository.addUser(USERMAIL, PASSWORD); + + assertThat(testee.isLocalUser(USERNAME)).isFalse(); + } + + @Test + public void isLocalUserShouldBeFalseWhenUsernameDoNotExist() throws Exception { + assertThat(testee.isLocalUser(USERMAIL)).isFalse(); + } + + @Test + public void isLocalEmailShouldBeFalseWhenUsernameDoNotExist() throws Exception { + assertThat(testee.isLocalEmail(mailAddress)).isFalse(); + } + + @Test + public void isLocalEmailShouldBeFalseWhenUsernameDoNotExistButDomainExists() throws Exception { + domainList.addDomain(DOMAIN_COM); + + assertThat(testee.isLocalEmail(mailAddress)).isFalse(); + } + + @Test + public void isLocalEmailShouldBeTrueWhenUsernameExists() throws Exception { + domainList.addDomain(DOMAIN_COM); + usersRepository.addUser(USERMAIL, PASSWORD); + + assertThat(testee.isLocalEmail(mailAddress)).isTrue(); + } + + @Test + public void isLocalEmailShouldBeFalseWhenMailIsNull() throws Exception { + assertThat(testee.isLocalEmail(null)).isFalse(); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
