Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace the TestMailServer to GreenMail server #801

Merged
merged 12 commits into from
Oct 23, 2023
2 changes: 2 additions & 0 deletions notifications/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ 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.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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,28 +17,29 @@ 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)
Hailong-am marked this conversation as resolved.
Show resolved Hide resolved
greenMail.start()
}

@After
@AfterEach
fun tearDownServer() {
smtpServer.stop()
smtpServer.resetServer()
greenMail.stop()
}

@Test
fun `test send email to one recipient over smtp server`() {
val smtpDestination = SmtpDestination(
"testAccountName",
"localhost",
smtpPort,
ServerSetupTest.SMTP.port,
"none",
"[email protected]",
"[email protected]"
Expand All @@ -53,14 +57,15 @@ 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
fun `test send email with non-available host`() {
val smtpDestination = SmtpDestination(
"testAccountName",
"invalidHost",
smtpPort,
ServerSetupTest.SMTP.port,
"none",
"[email protected]",
"[email protected]"
Expand All @@ -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)
Expand Down
Loading