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 4678063b9f7ccccf01aa64abcc5e470ea0acf350 Author: Tran Tien Duc <[email protected]> AuthorDate: Thu Aug 22 14:31:04 2019 +0700 JAMES-2865 Implement received mail memory storage for MockSMTPServer --- server/mailet/mock-smtp-server/pom.xml | 19 ++++ .../mock/smtp/server/ReceivedMailRepository.java | 47 +++++++++ .../org/apache/james/mock/smtp/server/Fixture.java | 5 + .../smtp/server/ReceivedMailRepositoryTest.java | 105 +++++++++++++++++++++ .../james/mock/smtp/server/SMTPCommandTest.java | 1 + 5 files changed, 177 insertions(+) diff --git a/server/mailet/mock-smtp-server/pom.xml b/server/mailet/mock-smtp-server/pom.xml index afc5a9c..b1aa69e 100644 --- a/server/mailet/mock-smtp-server/pom.xml +++ b/server/mailet/mock-smtp-server/pom.xml @@ -46,6 +46,15 @@ <artifactId>james-server-jetty</artifactId> </dependency> <dependency> + <groupId>${james.groupId}</groupId> + <artifactId>james-server-util</artifactId> + </dependency> + <dependency> + <groupId>${james.groupId}</groupId> + <artifactId>james-server-testing</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> @@ -92,6 +101,11 @@ <scope>test</scope> </dependency> <dependency> + <groupId>org.awaitility</groupId> + <artifactId>awaitility</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> </dependency> @@ -99,5 +113,10 @@ <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> + <dependency> + <groupId>org.subethamail</groupId> + <artifactId>subethasmtp</artifactId> + <version>3.1.7</version> + </dependency> </dependencies> </project> diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/ReceivedMailRepository.java b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/ReceivedMailRepository.java new file mode 100644 index 0000000..b2faec2 --- /dev/null +++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/ReceivedMailRepository.java @@ -0,0 +1,47 @@ +/**************************************************************** + * 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.mock.smtp.server; + +import java.util.List; +import java.util.concurrent.ConcurrentLinkedQueue; + +import org.apache.james.mock.smtp.server.model.Mail; + +import com.google.common.collect.ImmutableList; + +public class ReceivedMailRepository { + private final ConcurrentLinkedQueue<Mail> mails; + + public ReceivedMailRepository() { + mails = new ConcurrentLinkedQueue<>(); + } + + public void store(Mail mail) { + mails.add(mail); + } + + public List<Mail> list() { + return ImmutableList.copyOf(mails); + } + + public void clear() { + mails.clear(); + } +} diff --git a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/Fixture.java b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/Fixture.java index 34cf335..ac7b69e 100644 --- a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/Fixture.java +++ b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/Fixture.java @@ -31,6 +31,11 @@ import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; import com.google.common.collect.ImmutableList; public interface Fixture { + String DOMAIN = "james.org"; + String BOB = "bob@" + DOMAIN; + String ALICE = "alice@" + DOMAIN; + String JACK = "jack@" + DOMAIN; + ObjectMapper OBJECT_MAPPER = new ObjectMapper() .registerModule(new Jdk8Module()) .registerModule(new GuavaModule()); diff --git a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/ReceivedMailRepositoryTest.java b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/ReceivedMailRepositoryTest.java new file mode 100644 index 0000000..28cecad --- /dev/null +++ b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/ReceivedMailRepositoryTest.java @@ -0,0 +1,105 @@ +/**************************************************************** + * 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.mock.smtp.server; + +import static org.apache.james.mock.smtp.server.Fixture.ALICE; +import static org.apache.james.mock.smtp.server.Fixture.BOB; +import static org.apache.james.mock.smtp.server.Fixture.JACK; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; + +import javax.mail.internet.MimeMessage; + +import org.apache.james.core.MailAddress; +import org.apache.james.core.builder.MimeMessageBuilder; +import org.apache.james.mock.smtp.server.model.Mail; +import org.apache.james.util.MimeMessageUtil; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.google.common.collect.ImmutableList; + +class ReceivedMailRepositoryTest { + private ReceivedMailRepository testee; + private Mail mail; + private Mail mail2; + + @BeforeEach + void setUp() throws Exception { + testee = new ReceivedMailRepository(); + MimeMessage message = MimeMessageBuilder.mimeMessageBuilder() + .setSubject("test") + .setText("any text") + .build(); + mail = new Mail( + new Mail.Envelope( + new MailAddress(BOB), + ImmutableList.of(new MailAddress(ALICE), new MailAddress(JACK))), + MimeMessageUtil.asString(message)); + mail2 = new Mail( + new Mail.Envelope( + new MailAddress(ALICE), + ImmutableList.of(new MailAddress(BOB), new MailAddress(JACK))), + MimeMessageUtil.asString(message)); + } + + @Test + void listShouldBeEmptyWhenNoMailStored() { + assertThat(testee.list()).isEmpty(); + } + + @Test + void listShouldReturnStoredMail() { + testee.store(mail); + + assertThat(testee.list()).containsExactly(mail); + } + + @Test + void listShouldReturnStoredMails() { + testee.store(mail); + testee.store(mail2); + + assertThat(testee.list()).containsExactly(mail, mail2); + } + + @Test + void listShouldPreserveDuplicates() { + testee.store(mail); + testee.store(mail); + + assertThat(testee.list()).containsExactly(mail, mail); + } + + @Test + void listShouldNotReturnClearedMails() { + testee.store(mail); + + testee.clear(); + + assertThat(testee.list()).isEmpty(); + } + + @Test + void clearShouldNotFailWhenNoElementStored() { + assertThatCode(() -> testee.clear()) + .doesNotThrowAnyException(); + } +} \ No newline at end of file diff --git a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/SMTPCommandTest.java b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/SMTPCommandTest.java index 95abb58..12704ba 100644 --- a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/SMTPCommandTest.java +++ b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/SMTPCommandTest.java @@ -25,6 +25,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.io.IOException; +import org.apache.james.mock.smtp.server.model.SMTPCommand; import org.junit.jupiter.api.Test; class SMTPCommandTest { --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
