JAMES-2262 Provide a FakeSmtp wrapper class
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/1d9f9dfb Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/1d9f9dfb Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/1d9f9dfb Branch: refs/heads/improve-mailet-testing-experience-v1 Commit: 1d9f9dfb2e33a2c0c95743b40ef9233a15340515 Parents: b5efd2f Author: benwa <[email protected]> Authored: Mon Dec 25 11:11:08 2017 +0700 Committer: benwa <[email protected]> Committed: Fri Jan 5 09:34:39 2018 +0700 ---------------------------------------------------------------------- .../apache/james/mpt/smtp/ForwardSmtpTest.java | 25 ++---- .../GatewayRemoteDeliveryIntegrationTest.java | 52 ++++------- .../james/smtp/SmtpAuthorizedAddressesTest.java | 27 ++---- .../transport/mailets/GroupMappingTest.java | 40 ++------- .../jmap/VacationRelayIntegrationTest.java | 20 ++--- server/testing/pom.xml | 9 ++ .../java/org/apache/james/utils/FakeSmtp.java | 91 ++++++++++++++++++++ .../org/apache/james/utils/FakeSmtpHelper.java | 56 ------------ 8 files changed, 144 insertions(+), 176 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/1d9f9dfb/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/ForwardSmtpTest.java ---------------------------------------------------------------------- diff --git a/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/ForwardSmtpTest.java b/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/ForwardSmtpTest.java index bcfdd5d..37b691a 100644 --- a/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/ForwardSmtpTest.java +++ b/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/ForwardSmtpTest.java @@ -25,20 +25,14 @@ import static org.hamcrest.Matchers.equalTo; import java.util.Locale; import org.apache.james.mpt.script.SimpleScriptedTestProtocol; -import org.apache.james.util.docker.Images; -import org.apache.james.util.docker.SwarmGenericContainer; -import org.apache.james.utils.FakeSmtpHelper; +import org.apache.james.utils.FakeSmtp; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.RuleChain; -import org.junit.rules.TemporaryFolder; -import org.testcontainers.containers.wait.HostPortWaitStrategy; import com.jayway.awaitility.Awaitility; import com.jayway.awaitility.Duration; import com.jayway.awaitility.core.ConditionFactory; -import com.jayway.restassured.RestAssured; public abstract class ForwardSmtpTest { @@ -47,14 +41,9 @@ public abstract class ForwardSmtpTest { public static final String USER_AT_DOMAIN = USER + "@" + DOMAIN; public static final String PASSWORD = "secret"; - private final TemporaryFolder folder = new TemporaryFolder(); - private final SwarmGenericContainer fakeSmtp = new SwarmGenericContainer(Images.FAKE_SMTP) - .withExposedPorts(25) - .withAffinityToContainer() - .waitingFor(new HostPortWaitStrategy()); - @Rule - public final RuleChain chain = RuleChain.outerRule(folder).around(fakeSmtp); + public FakeSmtp fakeSmtp = new FakeSmtp(); + private ConditionFactory calmlyAwait; protected abstract SmtpHostSystem createSmtpHostSystem(); @@ -71,11 +60,9 @@ public abstract class ForwardSmtpTest { .withUser(USER_AT_DOMAIN, PASSWORD); hostSystem.getInMemoryDnsService() - .registerMxRecord("yopmail.com", fakeSmtp.getContainerIp()); + .registerMxRecord("yopmail.com", fakeSmtp.getContainer().getContainerIp()); hostSystem.addAddressMapping(USER, DOMAIN, "[email protected]"); - RestAssured.requestSpecification = FakeSmtpHelper.requestSpecification(fakeSmtp.getContainerIp()); - Duration slowPacedPollInterval = FIVE_HUNDRED_MILLISECONDS; calmlyAwait = Awaitility.with() .pollInterval(slowPacedPollInterval) @@ -84,7 +71,7 @@ public abstract class ForwardSmtpTest { .pollDelay(slowPacedPollInterval) .await(); - calmlyAwait.atMost(ONE_MINUTE).until(() -> fakeSmtp.tryConnect(25)); + fakeSmtp.awaitStarted(calmlyAwait.atMost(ONE_MINUTE)); } @Test @@ -92,7 +79,7 @@ public abstract class ForwardSmtpTest { scriptedTest.run("helo"); calmlyAwait.atMost(ONE_MINUTE).until(() -> - FakeSmtpHelper.isReceived(response -> response + fakeSmtp.isReceived(response -> response .body("[0].from", equalTo("[email protected]")) .body("[0].subject", equalTo("test")) .body("[0].text", equalTo("content")))); http://git-wip-us.apache.org/repos/asf/james-project/blob/1d9f9dfb/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/GatewayRemoteDeliveryIntegrationTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/GatewayRemoteDeliveryIntegrationTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/GatewayRemoteDeliveryIntegrationTest.java index ff55d89..2eb4c77 100644 --- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/GatewayRemoteDeliveryIntegrationTest.java +++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/GatewayRemoteDeliveryIntegrationTest.java @@ -19,7 +19,6 @@ package org.apache.james.mailets; -import static com.jayway.restassured.RestAssured.when; import static org.apache.james.MemoryJamesServerMain.SMTP_AND_IMAP_MODULE; import static org.apache.james.MemoryJamesServerMain.SMTP_ONLY_MODULE; import static org.apache.james.mailets.configuration.Constants.DEFAULT_DOMAIN; @@ -28,11 +27,10 @@ import static org.apache.james.mailets.configuration.Constants.LOCALHOST_IP; import static org.apache.james.mailets.configuration.Constants.PASSWORD; import static org.apache.james.mailets.configuration.Constants.SMTP_PORT; import static org.apache.james.mailets.configuration.Constants.awaitOneMinute; +import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; -import java.util.concurrent.TimeUnit; - import org.apache.james.dnsservice.api.DNSService; import org.apache.james.dnsservice.api.InMemoryDNSService; import org.apache.james.mailets.configuration.CommonProcessors; @@ -41,21 +39,15 @@ import org.apache.james.mailets.configuration.MailetContainer; import org.apache.james.mailets.configuration.ProcessorConfiguration; import org.apache.james.probe.DataProbe; import org.apache.james.transport.matchers.All; -import org.apache.james.util.docker.Images; -import org.apache.james.util.docker.SwarmGenericContainer; import org.apache.james.utils.DataProbeImpl; -import org.apache.james.utils.FakeSmtpHelper; +import org.apache.james.utils.FakeSmtp; import org.apache.james.utils.IMAPMessageReader; import org.apache.james.utils.SMTPMessageSender; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.RuleChain; import org.junit.rules.TemporaryFolder; -import org.testcontainers.containers.wait.HostPortWaitStrategy; - -import com.jayway.restassured.RestAssured; public class GatewayRemoteDeliveryIntegrationTest { private static final String JAMES_ANOTHER_DOMAIN = "james.com"; @@ -69,15 +61,8 @@ public class GatewayRemoteDeliveryIntegrationTest { public IMAPMessageReader imapMessageReader = new IMAPMessageReader(); @Rule public SMTPMessageSender messageSender = new SMTPMessageSender(DEFAULT_DOMAIN); - - private final TemporaryFolder smtpFolder = new TemporaryFolder(); - private final SwarmGenericContainer fakeSmtp = new SwarmGenericContainer(Images.FAKE_SMTP) - .withExposedPorts(25) - .withAffinityToContainer() - .waitingFor(new HostPortWaitStrategy()); - @Rule - public final RuleChain chain = RuleChain.outerRule(smtpFolder).around(fakeSmtp); + public FakeSmtp fakeSmtp = new FakeSmtp(); private TemporaryJamesServer jamesServer; private DataProbe dataProbe; @@ -85,12 +70,10 @@ public class GatewayRemoteDeliveryIntegrationTest { @Before public void setup() throws Exception { - awaitOneMinute.until(() -> fakeSmtp.tryConnect(25)); - - RestAssured.requestSpecification = FakeSmtpHelper.requestSpecification(fakeSmtp.getContainerIp()); + fakeSmtp.awaitStarted(awaitOneMinute); inMemoryDNSService = new InMemoryDNSService() - .registerMxRecord(JAMES_ANOTHER_DOMAIN, fakeSmtp.getContainerIp()); + .registerMxRecord(JAMES_ANOTHER_DOMAIN, fakeSmtp.getContainer().getContainerIp()); } @After @@ -102,7 +85,7 @@ public class GatewayRemoteDeliveryIntegrationTest { @Test public void outgoingMailShouldTransitThroughGatewayWhenNoPort() throws Exception { - String gatewayProperty = fakeSmtp.getContainerIp(); + String gatewayProperty = fakeSmtp.getContainer().getContainerIp(); jamesServer = TemporaryJamesServer.builder() .withBase(SMTP_ONLY_MODULE) @@ -121,7 +104,7 @@ public class GatewayRemoteDeliveryIntegrationTest { @Test public void outgoingMailShouldTransitThroughGatewayWhenPort() throws Exception { - String gatewayProperty = fakeSmtp.getContainerIp() + ":25"; + String gatewayProperty = fakeSmtp.getContainer().getContainerIp() + ":25"; jamesServer = TemporaryJamesServer.builder() .withBase(SMTP_ONLY_MODULE) @@ -140,7 +123,7 @@ public class GatewayRemoteDeliveryIntegrationTest { @Test public void outgoingMailShouldTransitThroughGatewayWhenSeveralIps() throws Exception { - String gatewayProperty = fakeSmtp.getContainerIp() + ",invalid.domain"; + String gatewayProperty = fakeSmtp.getContainer().getContainerIp() + ",invalid.domain"; jamesServer = TemporaryJamesServer.builder() .withBase(SMTP_ONLY_MODULE) @@ -159,7 +142,7 @@ public class GatewayRemoteDeliveryIntegrationTest { @Test public void outgoingMailShouldFallbackToSecondGatewayWhenFirstInvalid() throws Exception { - String gatewayProperty = "invalid.domain," + fakeSmtp.getContainerIp(); + String gatewayProperty = "invalid.domain," + fakeSmtp.getContainer().getContainerIp(); jamesServer = TemporaryJamesServer.builder() .withBase(SMTP_ONLY_MODULE) @@ -181,7 +164,7 @@ public class GatewayRemoteDeliveryIntegrationTest { String gatewayProperty = "invalid.domain"; jamesServer = TemporaryJamesServer.builder() - .withBase(SMTP_ONLY_MODULE) + .withBase(SMTP_AND_IMAP_MODULE) .withOverrides(binder -> binder.bind(DNSService.class).toInstance(inMemoryDNSService)) .withMailetContainer(generateMailetContainerConfiguration(gatewayProperty)) .build(temporaryFolder); @@ -193,12 +176,13 @@ public class GatewayRemoteDeliveryIntegrationTest { messageSender.connect(LOCALHOST_IP, SMTP_PORT) .sendMessage(FROM, RECIPIENT); - Thread.sleep(TimeUnit.SECONDS.toMillis(5)); - when() - .get("/api/email") - .then() - .statusCode(200) - .body("", hasSize(0)); + // Wait for bounce being sent before checking no email is sent + imapMessageReader.connect(LOCALHOST_IP, IMAP_PORT) + .login(FROM, PASSWORD) + .select(IMAPMessageReader.INBOX) + .awaitMessage(awaitOneMinute); + assertThat(fakeSmtp.isReceived(response -> response.body("", hasSize(0)))) + .isTrue(); } @Test @@ -276,7 +260,7 @@ public class GatewayRemoteDeliveryIntegrationTest { } private boolean messageIsReceivedByTheSmtpServer() { - return FakeSmtpHelper.isReceived(response -> response + return fakeSmtp.isReceived(response -> response .body("", hasSize(1)) .body("[0].from", equalTo(FROM)) .body("[0].subject", equalTo("test"))); http://git-wip-us.apache.org/repos/asf/james-project/blob/1d9f9dfb/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpAuthorizedAddressesTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpAuthorizedAddressesTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpAuthorizedAddressesTest.java index 81b3b1c..5fef6bb 100644 --- a/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpAuthorizedAddressesTest.java +++ b/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpAuthorizedAddressesTest.java @@ -37,39 +37,26 @@ import org.apache.james.mailets.configuration.ProcessorConfiguration; import org.apache.james.mailets.configuration.SmtpConfiguration; import org.apache.james.probe.DataProbe; import org.apache.james.transport.matchers.SMTPIsAuthNetwork; -import org.apache.james.util.docker.Images; -import org.apache.james.util.docker.SwarmGenericContainer; import org.apache.james.utils.DataProbeImpl; -import org.apache.james.utils.FakeSmtpHelper; +import org.apache.james.utils.FakeSmtp; import org.apache.james.utils.IMAPMessageReader; import org.apache.james.utils.SMTPMessageSender; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.RuleChain; import org.junit.rules.TemporaryFolder; -import org.testcontainers.containers.wait.HostPortWaitStrategy; - -import com.jayway.restassured.RestAssured; public class SmtpAuthorizedAddressesTest { private static final String FROM = "fromuser@" + DEFAULT_DOMAIN; private static final String TO = "[email protected]"; - private final TemporaryFolder smtpFolder = new TemporaryFolder(); - private final SwarmGenericContainer fakeSmtp = new SwarmGenericContainer(Images.FAKE_SMTP) - .withExposedPorts(25) - .withAffinityToContainer() - .waitingFor(new HostPortWaitStrategy()); - @Rule - public RuleChain chain = RuleChain.outerRule(smtpFolder).around(fakeSmtp); + public FakeSmtp fakeSmtp = new FakeSmtp(); @Rule public IMAPMessageReader imapMessageReader = new IMAPMessageReader(); @Rule public SMTPMessageSender messageSender = new SMTPMessageSender(DEFAULT_DOMAIN); - @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); @@ -77,9 +64,7 @@ public class SmtpAuthorizedAddressesTest { @Before public void setup() throws Exception { - awaitOneMinute.until(() -> fakeSmtp.tryConnect(25)); - - RestAssured.requestSpecification = FakeSmtpHelper.requestSpecification(fakeSmtp.getContainerIp()); + fakeSmtp.awaitStarted(awaitOneMinute); } private void createJamesServer(SmtpConfiguration.Builder smtpConfiguration) throws Exception { @@ -88,7 +73,7 @@ public class SmtpAuthorizedAddressesTest { .addMailetsFrom(CommonProcessors.deliverOnlyTransport()) .addMailet(MailetConfiguration.remoteDeliveryBuilder() .matcher(SMTPIsAuthNetwork.class) - .addProperty("gateway", fakeSmtp.getContainerIp())) + .addProperty("gateway", fakeSmtp.getContainer().getContainerIp())) .addMailet(MailetConfiguration.TO_BOUNCE)); jamesServer = TemporaryJamesServer.builder() @@ -119,7 +104,7 @@ public class SmtpAuthorizedAddressesTest { .sendMessage(FROM, TO) .awaitSent(awaitOneMinute); - awaitOneMinute.until(() -> FakeSmtpHelper.isReceived(response -> response + awaitOneMinute.until(() -> fakeSmtp.isReceived(response -> response .body("", hasSize(1)) .body("[0].from", equalTo(FROM)) .body("[0].subject", equalTo("test")))); @@ -147,7 +132,7 @@ public class SmtpAuthorizedAddressesTest { .sendMessage(FROM, TO) .awaitSent(awaitOneMinute); - awaitOneMinute.until(() -> FakeSmtpHelper.isReceived(response -> response + awaitOneMinute.until(() -> fakeSmtp.isReceived(response -> response .body("", hasSize(1)) .body("[0].from", equalTo(FROM)) .body("[0].subject", equalTo("test")))); http://git-wip-us.apache.org/repos/asf/james-project/blob/1d9f9dfb/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/GroupMappingTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/GroupMappingTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/GroupMappingTest.java index 123560f..36dc533 100644 --- a/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/GroupMappingTest.java +++ b/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/GroupMappingTest.java @@ -19,19 +19,15 @@ package org.apache.james.transport.mailets; -import static com.jayway.restassured.RestAssured.with; import static org.apache.james.mailets.configuration.Constants.DEFAULT_DOMAIN; import static org.apache.james.mailets.configuration.Constants.IMAP_PORT; import static org.apache.james.mailets.configuration.Constants.LOCALHOST_IP; import static org.apache.james.mailets.configuration.Constants.PASSWORD; import static org.apache.james.mailets.configuration.Constants.SMTP_PORT; import static org.apache.james.mailets.configuration.Constants.awaitOneMinute; -import static org.apache.james.mailets.configuration.Constants.calmlyAwait; import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.equalTo; -import java.util.concurrent.TimeUnit; - import javax.mail.internet.MimeMessage; import org.apache.james.jmap.mailet.VacationMailet; @@ -45,9 +41,8 @@ import org.apache.james.modules.MailboxProbeImpl; import org.apache.james.probe.DataProbe; import org.apache.james.transport.matchers.All; import org.apache.james.transport.matchers.RecipientIsLocal; -import org.apache.james.util.docker.Images; -import org.apache.james.util.docker.SwarmGenericContainer; import org.apache.james.utils.DataProbeImpl; +import org.apache.james.utils.FakeSmtp; import org.apache.james.utils.IMAPMessageReader; import org.apache.james.utils.SMTPMessageSender; import org.apache.james.utils.WebAdminGuiceProbe; @@ -59,7 +54,6 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import org.testcontainers.containers.wait.HostPortWaitStrategy; import com.jayway.restassured.RestAssured; import com.jayway.restassured.specification.RequestSpecification; @@ -82,11 +76,7 @@ public class GroupMappingTest { private RequestSpecification restApiRequest; @Rule - public final SwarmGenericContainer fakeSmtp = new SwarmGenericContainer(Images.FAKE_SMTP) - .withExposedPorts(25) - .withAffinityToContainer() - .waitingFor(new HostPortWaitStrategy()); - + public final FakeSmtp fakeSmtp = new FakeSmtp(); @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); @Rule @@ -107,13 +97,13 @@ public class GroupMappingTest { .addMailetsFrom(CommonProcessors.deliverOnlyTransport()) .addMailet(MailetConfiguration.remoteDeliveryBuilder() .matcher(All.class) - .addProperty("gateway", fakeSmtp.getContainerIp()))); + .addProperty("gateway", fakeSmtp.getContainer().getContainerIp()))); jamesServer = TemporaryJamesServer.builder() .withMailetContainer(mailetContainer) .build(temporaryFolder); - awaitOneMinute.until(() -> fakeSmtp.tryConnect(25)); + fakeSmtp.awaitStarted(awaitOneMinute); dataProbe = jamesServer.getProbe(DataProbeImpl.class); dataProbe.addDomain(DOMAIN1); @@ -441,23 +431,9 @@ public class GroupMappingTest { .recipient(GROUP_ON_DOMAIN1)) .awaitSent(awaitOneMinute); - calmlyAwait.atMost(1, TimeUnit.MINUTES) - .until(() -> { - try { - with() - .baseUri("http://" + fakeSmtp.getContainerIp()) - .port(80) - .get("/api/email") - .then() - .statusCode(200) - .body("[0].from", equalTo(SENDER)) - .body("[0].to[0]", equalTo(externalMail)) - .body("[0].text", equalTo(MESSAGE_CONTENT)); - - return true; - } catch(AssertionError e) { - return false; - } - }); + fakeSmtp.isReceived(response -> response + .body("[0].from", equalTo(SENDER)) + .body("[0].to[0]", equalTo(externalMail)) + .body("[0].text", equalTo(MESSAGE_CONTENT))); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/1d9f9dfb/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/VacationRelayIntegrationTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/VacationRelayIntegrationTest.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/VacationRelayIntegrationTest.java index dda4ddd..eb1c812 100644 --- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/VacationRelayIntegrationTest.java +++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/VacationRelayIntegrationTest.java @@ -19,6 +19,7 @@ package org.apache.james.jmap; +import static com.jayway.awaitility.Duration.ONE_MINUTE; import static org.hamcrest.Matchers.equalTo; import java.util.concurrent.TimeUnit; @@ -32,21 +33,17 @@ import org.apache.james.mailbox.model.MailboxConstants; import org.apache.james.mailbox.store.probe.MailboxProbe; import org.apache.james.modules.MailboxProbeImpl; import org.apache.james.probe.DataProbe; -import org.apache.james.util.docker.Images; -import org.apache.james.util.docker.SwarmGenericContainer; import org.apache.james.utils.DataProbeImpl; -import org.apache.james.utils.FakeSmtpHelper; +import org.apache.james.utils.FakeSmtp; import org.apache.james.utils.JmapGuiceProbe; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.testcontainers.containers.wait.HostPortWaitStrategy; import com.jayway.awaitility.Awaitility; import com.jayway.awaitility.Duration; import com.jayway.awaitility.core.ConditionFactory; -import com.jayway.restassured.RestAssured; public abstract class VacationRelayIntegrationTest { @@ -58,12 +55,9 @@ public abstract class VacationRelayIntegrationTest { private static final String LOCALHOST_IP = "127.0.0.1"; private static final int SMTP_PORT = 1025; - private static final int REST_SMTP_SINK_PORT = 25; @Rule - public final SwarmGenericContainer fakeSmtp = new SwarmGenericContainer(Images.FAKE_SMTP) - .withExposedPorts(REST_SMTP_SINK_PORT) - .waitingFor(new HostPortWaitStrategy()); + public FakeSmtp fakeSmtp = new FakeSmtp(); private ConditionFactory calmlyAwait; @@ -79,7 +73,7 @@ public abstract class VacationRelayIntegrationTest { @Before public void setUp() throws Exception { getInMemoryDns() - .registerMxRecord("yopmail.com", fakeSmtp.getContainerIp()); + .registerMxRecord("yopmail.com", fakeSmtp.getContainer().getContainerIp()); guiceJamesServer = getJmapServer(); guiceJamesServer.start(); @@ -94,8 +88,6 @@ public abstract class VacationRelayIntegrationTest { jmapGuiceProbe = guiceJamesServer.getProbe(JmapGuiceProbe.class); - RestAssured.requestSpecification = FakeSmtpHelper.requestSpecification(fakeSmtp.getContainerIp()); - Duration slowPacedPollInterval = Duration.FIVE_HUNDRED_MILLISECONDS; calmlyAwait = Awaitility .with() @@ -103,7 +95,7 @@ public abstract class VacationRelayIntegrationTest { .and() .pollDelay(slowPacedPollInterval).await(); - calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> fakeSmtp.tryConnect(25)); + fakeSmtp.awaitStarted(calmlyAwait.atMost(ONE_MINUTE)); } @After @@ -130,7 +122,7 @@ public abstract class VacationRelayIntegrationTest { calmlyAwait.atMost(1, TimeUnit.MINUTES) .until(() -> - FakeSmtpHelper.isReceived(response -> response + fakeSmtp.isReceived(response -> response .body("[0].from", equalTo(USER_WITH_DOMAIN)) .body("[0].to[0]", equalTo(externalMail)) .body("[0].text", equalTo(REASON)))); http://git-wip-us.apache.org/repos/asf/james-project/blob/1d9f9dfb/server/testing/pom.xml ---------------------------------------------------------------------- diff --git a/server/testing/pom.xml b/server/testing/pom.xml index f989249..c8db6e3 100644 --- a/server/testing/pom.xml +++ b/server/testing/pom.xml @@ -41,6 +41,11 @@ <type>test-jar</type> </dependency> <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>james-server-util-java8</artifactId> + <type>test-jar</type> + </dependency> + <dependency> <groupId>com.jayway.awaitility</groupId> <artifactId>awaitility</artifactId> </dependency> @@ -70,6 +75,10 @@ <artifactId>mockito-core</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>testcontainers</artifactId> + </dependency> </dependencies> </project> http://git-wip-us.apache.org/repos/asf/james-project/blob/1d9f9dfb/server/testing/src/main/java/org/apache/james/utils/FakeSmtp.java ---------------------------------------------------------------------- diff --git a/server/testing/src/main/java/org/apache/james/utils/FakeSmtp.java b/server/testing/src/main/java/org/apache/james/utils/FakeSmtp.java new file mode 100644 index 0000000..b84ac9f --- /dev/null +++ b/server/testing/src/main/java/org/apache/james/utils/FakeSmtp.java @@ -0,0 +1,91 @@ +/**************************************************************** + * 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.utils; + +import static com.jayway.restassured.RestAssured.given; +import static com.jayway.restassured.config.EncoderConfig.encoderConfig; +import static com.jayway.restassured.config.RestAssuredConfig.newConfig; + +import java.util.function.Function; + +import org.apache.james.util.docker.Images; +import org.apache.james.util.docker.SwarmGenericContainer; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; +import org.testcontainers.containers.wait.HostPortWaitStrategy; + +import com.google.common.base.Charsets; +import com.jayway.awaitility.core.ConditionFactory; +import com.jayway.restassured.builder.RequestSpecBuilder; +import com.jayway.restassured.builder.ResponseSpecBuilder; +import com.jayway.restassured.http.ContentType; +import com.jayway.restassured.response.ValidatableResponse; +import com.jayway.restassured.specification.RequestSpecification; +import com.jayway.restassured.specification.ResponseSpecification; + +public class FakeSmtp implements TestRule { + private static final int SMTP_PORT = 25; + private static final ResponseSpecification RESPONSE_SPECIFICATION = new ResponseSpecBuilder().build(); + private final SwarmGenericContainer container; + + public FakeSmtp() { + container = new SwarmGenericContainer(Images.FAKE_SMTP) + .withExposedPorts(SMTP_PORT) + .withAffinityToContainer() + .waitingFor(new HostPortWaitStrategy()); + } + + @Override + public Statement apply(Statement statement, Description description) { + return container.apply(statement, description); + } + + public void awaitStarted(ConditionFactory calmyAwait) { + calmyAwait.until(() -> container.tryConnect(SMTP_PORT)); + } + + public boolean isReceived(Function<ValidatableResponse, ValidatableResponse> expectations) { + try { + expectations.apply( + given(requestSpecification(), RESPONSE_SPECIFICATION) + .get("/api/email") + .then() + .statusCode(200)); + return true; + } catch (Exception e) { + return false; + } + } + + private RequestSpecification requestSpecification() { + return new RequestSpecBuilder() + .setContentType(ContentType.JSON) + .setAccept(ContentType.JSON) + .setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8))) + .setPort(80) + .setBaseUri("http://" + container.getContainerIp()) + .build(); + } + + public SwarmGenericContainer getContainer() { + return container; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/1d9f9dfb/server/testing/src/main/java/org/apache/james/utils/FakeSmtpHelper.java ---------------------------------------------------------------------- diff --git a/server/testing/src/main/java/org/apache/james/utils/FakeSmtpHelper.java b/server/testing/src/main/java/org/apache/james/utils/FakeSmtpHelper.java deleted file mode 100644 index 7129f60..0000000 --- a/server/testing/src/main/java/org/apache/james/utils/FakeSmtpHelper.java +++ /dev/null @@ -1,56 +0,0 @@ -/**************************************************************** - * 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.utils; - -import static com.jayway.restassured.RestAssured.when; -import static com.jayway.restassured.config.EncoderConfig.encoderConfig; -import static com.jayway.restassured.config.RestAssuredConfig.newConfig; - -import java.util.function.Function; - -import com.google.common.base.Charsets; -import com.jayway.restassured.builder.RequestSpecBuilder; -import com.jayway.restassured.http.ContentType; -import com.jayway.restassured.response.ValidatableResponse; -import com.jayway.restassured.specification.RequestSpecification; - -public class FakeSmtpHelper { - public static RequestSpecification requestSpecification(String ip) { - return new RequestSpecBuilder() - .setContentType(ContentType.JSON) - .setAccept(ContentType.JSON) - .setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8))) - .setPort(80) - .setBaseUri("http://" + ip) - .build(); - } - - public static boolean isReceived(Function<ValidatableResponse, ValidatableResponse> expectations) { - try { - expectations.apply(when() - .get("/api/email") - .then() - .statusCode(200)); - return true; - } catch (Exception e) { - return false; - } - } -} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
