Skip to content

Commit

Permalink
test(mail): refactor IT for mail sessions to use @JvmSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
poikilotherm committed Oct 5, 2023
1 parent bc55587 commit c367e09
Showing 1 changed file with 15 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import edu.harvard.iq.dataverse.branding.BrandingUtil;
import edu.harvard.iq.dataverse.settings.JvmSettings;
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
import edu.harvard.iq.dataverse.util.testing.JvmSetting;
import edu.harvard.iq.dataverse.util.testing.LocalJvmSettings;
import io.restassured.RestAssured;
import jakarta.mail.Session;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -32,14 +34,14 @@
*/
@Testcontainers
@ExtendWith(MockitoExtension.class)
@LocalJvmSettings
@JvmSetting(key = JvmSettings.MAIL_MTA_HOST, method = "tcSmtpHost")
@JvmSetting(key = JvmSettings.MAIL_MTA_SETTING, method = "tcSmtpPort", varArgs = "port")
class MailSessionProducerIT {

private static final Integer PORT_SMTP = 1025;
private static final Integer PORT_HTTP = 1080;

Integer smtpPort;
String smtpHost;

@Mock
SettingsServiceBean settingsServiceBean;
@Mock
Expand All @@ -49,30 +51,23 @@ class MailSessionProducerIT {
static GenericContainer<?> maildev = new GenericContainer<>("maildev/maildev:2.1.0")
.withExposedPorts(PORT_HTTP, PORT_SMTP)
.waitingFor(Wait.forHttp("/"));

static String tcSmtpHost() {
return maildev.getHost();
}

static String tcSmtpPort() {
return maildev.getMappedPort(PORT_SMTP).toString();
}

@BeforeEach
void setUp() {
smtpHost = maildev.getHost();
smtpPort = maildev.getMappedPort(PORT_SMTP);
Integer httpPort = maildev.getMappedPort(PORT_HTTP);

RestAssured.baseURI = "http://" + smtpHost;
RestAssured.port = httpPort;
RestAssured.baseURI = "http://" + tcSmtpHost();
RestAssured.port = maildev.getMappedPort(PORT_HTTP);;

// Setup mocks
Mockito.when(settingsServiceBean.getValueForKey(SettingsServiceBean.Key.SystemEmail)).thenReturn("[email protected]");
BrandingUtil.injectServices(dataverseServiceBean, settingsServiceBean);

// TODO: Once we merge PR 9273 (https://github.com/IQSS/dataverse/pull/9273),
// we can use methods to inject the settings in @JvmSetting
System.setProperty(JvmSettings.MAIL_MTA_HOST.getScopedKey(), smtpHost);
System.setProperty(JvmSettings.MAIL_MTA_SETTING.insert("port"), smtpPort.toString());
}

@AfterEach
void tearDown() {
System.clearProperty(JvmSettings.MAIL_MTA_HOST.getScopedKey());
System.clearProperty(JvmSettings.MAIL_MTA_SETTING.insert("port"));
}

@Test
Expand Down

0 comments on commit c367e09

Please sign in to comment.