From 80fce319736825fa478988ae8d20c4de39c7cbf5 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Tue, 24 Oct 2023 12:32:42 +0800 Subject: [PATCH] [Backport 2.x] Replace the TestMailServer to GreenMail server (#807) * Replace the TestMailServer to GreenMail server (#801) * Add 2.11 release notes (#774) Signed-off-by: yuye-aws Signed-off-by: rdani * Fix integration test failure by allowing direct access to system index warning (#784) * Fix integration test failure by allowing direct access to system index warning Signed-off-by: gaobinlong * Fix bwc test failure of throwing direct access to system index when getting mapping Signed-off-by: gaobinlong --------- Signed-off-by: gaobinlong Signed-off-by: rdani * Replace the TestMailServer to GreenMail server Signed-off-by: rdani * bump bwc version to 2.12 (#793) Signed-off-by: Hailong Cui Signed-off-by: rdani * Update dependency org.json:json to v20231013 (#795) Signed-off-by: gaobinlong Signed-off-by: rdani * Re-enable detekt (#796) Bumped version of `io.gitlab.arturbosch.detekt:detekt-gradle-plugin` to `1.23.0` Signed-off-by: Aniruddh <63553175+Noir01@users.noreply.github.com> Co-authored-by: Hailong Cui Signed-off-by: rdani * Add assertion for retrieval of notification Signed-off-by: rdani * Update to stable version Signed-off-by: rdani * Update to stable version Signed-off-by: rdani * Update to suggested version Signed-off-by: rdani --------- Signed-off-by: yuye-aws Signed-off-by: rdani Signed-off-by: gaobinlong Signed-off-by: Hailong Cui Signed-off-by: Aniruddh <63553175+Noir01@users.noreply.github.com> Co-authored-by: Yuye Zhu Co-authored-by: gaobinlong Co-authored-by: rdani Co-authored-by: Hailong Cui Co-authored-by: Aniruddh <63553175+Noir01@users.noreply.github.com> (cherry picked from commit 76ddcd47820df976121dadeb8323632a546271b5) Signed-off-by: github-actions[bot] * Add slf4j-api for greenmail Signed-off-by: Hailong Cui --------- Signed-off-by: yuye-aws Signed-off-by: rdani Signed-off-by: gaobinlong Signed-off-by: Hailong Cui Signed-off-by: Aniruddh <63553175+Noir01@users.noreply.github.com> Signed-off-by: github-actions[bot] Co-authored-by: github-actions[bot] Co-authored-by: Yuye Zhu Co-authored-by: gaobinlong Co-authored-by: rdani Co-authored-by: Hailong Cui Co-authored-by: Aniruddh <63553175+Noir01@users.noreply.github.com> --- notifications/core/build.gradle | 3 +++ .../notifications/core/smtp/SmtpEmailTests.kt | 27 +++++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) 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)