blob: c8e9d8c07151f3854b63b39c58c79bb4857883eb [file] [log] [blame]
/****************************************************************
* 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.jmap;
import java.util.UUID;
import org.apache.james.mock.smtp.server.ConfigurationClient;
import org.apache.james.util.Host;
import org.apache.james.util.docker.DockerContainer;
import org.apache.james.util.docker.Images;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.wait.strategy.Wait;
public class MockSmtpTestRule implements TestRule {
public static class DockerMockSmtp {
private static final Logger LOGGER = LoggerFactory.getLogger(DockerMockSmtp.class);
private final DockerContainer mockSmtpServer;
DockerMockSmtp() {
mockSmtpServer = DockerContainer.fromName(Images.MOCK_SMTP_SERVER)
.withLogConsumer(outputFrame -> LOGGER.debug("MockSMTP: " + outputFrame.getUtf8String()))
.withExposedPorts(25, 8000)
.waitingFor(Wait.forLogMessage(".*Mock SMTP server started.*", 1))
.withName("james-testing-mock-smtp-server-" + UUID.randomUUID());
}
public ConfigurationClient getConfigurationClient() {
return ConfigurationClient.from(Host.from(
mockSmtpServer.getHostIp(),
mockSmtpServer.getMappedPort(8000)));
}
public String getIPAddress() {
return mockSmtpServer.getContainerIp();
}
}
private final DockerMockSmtp dockerMockSmtp;
public MockSmtpTestRule() {
this.dockerMockSmtp = new DockerMockSmtp();
}
@Override
public Statement apply(Statement statement, Description description) {
return dockerMockSmtp.mockSmtpServer.apply(statement, description);
}
public DockerMockSmtp getDockerMockSmtp() {
return dockerMockSmtp;
}
}