diff --git a/notifications/core/build.gradle b/notifications/core/build.gradle index 10abf4a6..79893d4e 100644 --- a/notifications/core/build.gradle +++ b/notifications/core/build.gradle @@ -159,6 +159,9 @@ dependencies { 'io.mockk:mockk-agent-jvm:1.11.0', ) testImplementation 'org.springframework.integration:spring-integration-mail:5.5.0' + // https://mvnrepository.com/artifact/com.icegreen/greenmail + testImplementation group: 'com.icegreen', name: 'greenmail', version: '1.6.14' + testImplementation "org.slf4j:slf4j-api:${versions.slf4j}" testImplementation 'org.springframework.integration:spring-integration-test-support:5.5.0' testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine:5.6.2') testImplementation "org.jetbrains.kotlin:kotlin-test:${kotlin_version}" diff --git a/notifications/core/src/test/kotlin/org/opensearch/notifications/core/smtp/SmtpEmailTests.kt b/notifications/core/src/test/kotlin/org/opensearch/notifications/core/smtp/SmtpEmailTests.kt index c3bd0e91..66f400c6 100644 --- a/notifications/core/src/test/kotlin/org/opensearch/notifications/core/smtp/SmtpEmailTests.kt +++ b/notifications/core/src/test/kotlin/org/opensearch/notifications/core/smtp/SmtpEmailTests.kt @@ -5,7 +5,10 @@ package org.opensearch.notifications.core.smtp -import org.junit.After +import com.icegreen.greenmail.util.GreenMail +import com.icegreen.greenmail.util.ServerSetupTest +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.opensearch.core.rest.RestStatus import org.opensearch.notifications.core.NotificationCoreImpl @@ -14,20 +17,21 @@ import org.opensearch.notifications.core.transport.SmtpDestinationTransport import org.opensearch.notifications.spi.model.MessageContent import org.opensearch.notifications.spi.model.destination.DestinationType import org.opensearch.notifications.spi.model.destination.SmtpDestination -import org.springframework.integration.test.mail.TestMailServer import kotlin.test.assertEquals class SmtpEmailTests { - internal companion object { - private const val smtpPort = 10255 // use non-standard port > 1024 to avoid permission issue - private val smtpServer = TestMailServer.smtp(smtpPort) + private lateinit var greenMail: GreenMail + + @BeforeEach + fun setUpServer() { + greenMail = GreenMail(ServerSetupTest.SMTP) + greenMail.start() } - @After + @AfterEach fun tearDownServer() { - smtpServer.stop() - smtpServer.resetServer() + greenMail.stop() } @Test @@ -35,7 +39,7 @@ class SmtpEmailTests { val smtpDestination = SmtpDestination( "testAccountName", "localhost", - smtpPort, + ServerSetupTest.SMTP.port, "none", "from@email.com", "test@localhost.com" @@ -53,6 +57,7 @@ class SmtpEmailTests { val response = NotificationCoreImpl.sendMessage(smtpDestination, message, "ref") assertEquals("Success", response.statusText) assertEquals(RestStatus.OK.status, response.statusCode) + assertEquals(1, greenMail.receivedMessages.size) // Indicates retrieval of notification. } @Test @@ -60,7 +65,7 @@ class SmtpEmailTests { val smtpDestination = SmtpDestination( "testAccountName", "invalidHost", - smtpPort, + ServerSetupTest.SMTP.port, "none", "from@email.com", "test@localhost.com" @@ -77,7 +82,7 @@ class SmtpEmailTests { DestinationTransportProvider.destinationTransportMap = mapOf(DestinationType.SMTP to SmtpDestinationTransport()) val response = NotificationCoreImpl.sendMessage(smtpDestination, message, "ref") assertEquals( - "sendEmail Error, status:Couldn't connect to host, port: invalidHost, $smtpPort; timeout -1", + "sendEmail Error, status:Couldn't connect to host, port: invalidHost, ${ServerSetupTest.SMTP.port}; timeout -1", response.statusText ) assertEquals(RestStatus.SERVICE_UNAVAILABLE.status, response.statusCode)