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

Pivotal ID # 186547510: Configurable Notifications E-Mail Subject #800

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ebi.ac.uk.notifications.integration.templates

import ebi.ac.uk.notifications.integration.model.NotificationTemplate
import ebi.ac.uk.notifications.integration.model.NotificationTemplateModel

internal class NotificationSubjectTemplate(content: String) : NotificationTemplate<NotificationSubjectModel>(content)

internal class NotificationSubjectModel(
private val accNo: String
) : NotificationTemplateModel {
override fun getParams(): List<Pair<String, String>> = listOf(
"ACC_NO" to accNo,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package ebi.ac.uk.notifications.service
import ebi.ac.uk.base.EMPTY
import ebi.ac.uk.extended.model.ExtSubmission
import ebi.ac.uk.extended.model.computedTitle
import ebi.ac.uk.notifications.integration.templates.NotificationSubjectModel
import ebi.ac.uk.notifications.integration.templates.NotificationSubjectTemplate
import ebi.ac.uk.notifications.integration.templates.SubmissionReleaseModel
import ebi.ac.uk.notifications.integration.templates.SubmissionReleaseTemplate
import ebi.ac.uk.notifications.integration.templates.SuccessfulSubmissionModel
Expand All @@ -11,6 +13,7 @@ import ebi.ac.uk.notifications.util.TemplateLoader
import ebi.ac.uk.util.date.toStringDate

internal const val FROM = "biostudies@ebi.ac.uk"
internal const val EMAIL_SUBJECT_TEMPLATE = "subject/%s.txt"
internal const val SUBMISSION_RELEASE_TEMPLATE = "release/%s.txt"
internal const val SUCCESSFUL_SUBMISSION_TEMPLATE = "submission/%s.txt"
internal const val SUCCESSFUL_RESUBMISSION_TEMPLATE = "resubmission/%s.txt"
Expand All @@ -20,7 +23,7 @@ class RtNotificationService(
private val rtTicketService: RtTicketService
) {
fun notifySuccessfulSubmission(sub: ExtSubmission, ownerFullName: String, uiUrl: String, stUrl: String) {
val subject = "BioStudies Submission - ${sub.accNo}"
val subject = notificationSubject(sub)
val template = if (sub.version == 1) SUCCESSFUL_SUBMISSION_TEMPLATE else SUCCESSFUL_RESUBMISSION_TEMPLATE
val model = successfulSubmissionModel(sub, uiUrl, stUrl, ownerFullName)
val content = SuccessfulSubmissionTemplate(templateLoader.loadTemplateOrDefault(sub, template)).render(model)
Expand All @@ -29,14 +32,21 @@ class RtNotificationService(
}

fun notifySubmissionRelease(sub: ExtSubmission, ownerFullName: String, uiUrl: String, stUrl: String) {
val subject = "BioStudies Submission - ${sub.accNo}"
val subject = notificationSubject(sub)
val model = submissionReleaseModel(sub, uiUrl, stUrl, ownerFullName)
val template = templateLoader.loadTemplateOrDefault(sub, SUBMISSION_RELEASE_TEMPLATE)
val content = SubmissionReleaseTemplate(template).render(model)

rtTicketService.saveRtTicket(sub.accNo, subject, sub.owner, content)
}

private fun notificationSubject(sub: ExtSubmission): String {
val model = NotificationSubjectModel(sub.accNo)
val template = templateLoader.loadTemplateOrDefault(sub, EMAIL_SUBJECT_TEMPLATE)

return NotificationSubjectTemplate(template).render(model)
}

companion object {
private fun submissionReleaseModel(
submission: ExtSubmission,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BioImage Archive Submission - ${ACC_NO}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BioStudies Submission - ${ACC_NO}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal class RtNotificationServiceTest(
@Test
fun `when no released but release date`() {
testNotification(
submission = testSubmission(
submission = testSubmission.copy(
releaseTime = OffsetDateTime.of(2019, 9, 21, 10, 30, 34, 15, ZoneOffset.UTC),
released = false
),
Expand All @@ -62,7 +62,7 @@ internal class RtNotificationServiceTest(
@Test
fun `when no released no release date`() {
testNotification(
submission = testSubmission(releaseTime = null, released = false),
submission = testSubmission.copy(releaseTime = null, released = false),
"""
Dear owner@mail.org,

Expand All @@ -83,7 +83,7 @@ internal class RtNotificationServiceTest(
@Test
fun `when released`() {
testNotification(
submission = testSubmission(released = true),
submission = testSubmission.copy(released = true),
"""
Dear owner@mail.org,

Expand All @@ -104,7 +104,7 @@ internal class RtNotificationServiceTest(
@Test
fun `when submission title`() {
testNotification(
submission = testSubmission(title = "Sub Title", version = 1),
submission = testSubmission.copy(title = "Sub Title"),
"""
Dear owner@mail.org,

Expand All @@ -125,7 +125,7 @@ internal class RtNotificationServiceTest(
@Test
fun `when section title`() {
testNotification(
submission = testSubmission(title = "Sect Title", version = 1),
submission = testSubmission.copy(title = "Sect Title"),
"""
Dear owner@mail.org,

Expand All @@ -149,10 +149,15 @@ internal class RtNotificationServiceTest(
every {
loader.loadTemplateOrDefault(submission, SUCCESSFUL_SUBMISSION_TEMPLATE)
} returns asText("/templates/submission/Default.txt")

every {
loader.loadTemplateOrDefault(submission, EMAIL_SUBJECT_TEMPLATE)
} returns asText("/templates/subject/BioImages.txt")

every {
ticketService.saveRtTicket(
"S-TEST1",
"BioStudies Submission - S-TEST1",
"BioImage Archive Submission - S-TEST1",
"owner@mail.org",
capture(slot)
)
Expand All @@ -169,7 +174,7 @@ internal class RtNotificationServiceTest(
@Test
fun `when released`() {
testNotification(
submission = testSubmission(released = true, version = 2),
submission = testSubmission.copy(released = true, version = 2, collections = listOf()),
"""
Dear owner@mail.org,

Expand All @@ -191,6 +196,11 @@ internal class RtNotificationServiceTest(
every {
loader.loadTemplateOrDefault(submission, SUCCESSFUL_RESUBMISSION_TEMPLATE)
} returns asText("/templates/resubmission/Default.txt")

every {
loader.loadTemplateOrDefault(submission, EMAIL_SUBJECT_TEMPLATE)
} returns asText("/templates/subject/Default.txt")

every {
ticketService.saveRtTicket(
"S-TEST1",
Expand All @@ -212,7 +222,7 @@ internal class RtNotificationServiceTest(
@Test
fun `when no title`() {
testNotification(
submission = testSubmission(released = true),
submission = testSubmission.copy(released = true),
"""
Dear owner@mail.org,

Expand All @@ -233,7 +243,7 @@ internal class RtNotificationServiceTest(
@Test
fun `when submission title`() {
testNotification(
submission = testSubmission(title = "Sub Title", version = 1),
submission = testSubmission.copy(title = "Sub Title"),
"""
Dear owner@mail.org,

Expand All @@ -254,7 +264,7 @@ internal class RtNotificationServiceTest(
@Test
fun `when section title`() {
testNotification(
submission = testSubmission(title = "Sect Title", version = 1),
submission = testSubmission.copy(title = "Sect Title"),
"""
Dear owner@mail.org,

Expand All @@ -278,10 +288,15 @@ internal class RtNotificationServiceTest(
every {
loader.loadTemplateOrDefault(submission, SUBMISSION_RELEASE_TEMPLATE)
} returns asText("/templates/release/Default.txt")

every {
loader.loadTemplateOrDefault(submission, EMAIL_SUBJECT_TEMPLATE)
} returns asText("/templates/subject/BioImages.txt")

every {
ticketService.saveRtTicket(
"S-TEST1",
"BioStudies Submission - S-TEST1",
"BioImage Archive Submission - S-TEST1",
"owner@mail.org",
capture(slot)
)
Expand All @@ -296,35 +311,28 @@ internal class RtNotificationServiceTest(
private fun asText(path: String): String =
object {}.javaClass.getResource(path)!!.readText()

private fun testSubmission(
title: String? = null,
secTitle: String? = null,
version: Int = 1,
released: Boolean = false,
releaseTime: OffsetDateTime? = OffsetDateTime.of(2019, 9, 21, 10, 30, 34, 15, ZoneOffset.UTC)
): ExtSubmission = ExtSubmission(
accNo = "S-TEST1",
version = version,
schemaVersion = "1.0",
owner = "owner@mail.org",
submitter = "submitter@mail.org",
title = title,
doi = "10.983/S-TEST1",
method = ExtSubmissionMethod.PAGE_TAB,
relPath = "/a/rel/path",
rootPath = "/a/root/path",
released = released,
secretKey = "a-secret-key",
releaseTime = releaseTime,
modificationTime = OffsetDateTime.of(2019, 9, 20, 10, 30, 34, 15, ZoneOffset.UTC),
creationTime = OffsetDateTime.of(2019, 9, 19, 10, 30, 34, 15, ZoneOffset.UTC),
attributes = listOf(ExtAttribute("AttachTo", "BioImages")),
tags = listOf(ExtTag("component", "web")),
collections = listOf(ExtCollection("BioImages")),
section = ExtSection(
type = "Study",
attributes = secTitle?.let { listOf(ExtAttribute(name = "Title", value = it)) } ?: listOf()
),
storageMode = StorageMode.NFS
)
companion object {
val testSubmission = ExtSubmission(
accNo = "S-TEST1",
version = 1,
schemaVersion = "1.0",
owner = "owner@mail.org",
submitter = "submitter@mail.org",
title = null,
doi = "10.983/S-TEST1",
method = ExtSubmissionMethod.PAGE_TAB,
relPath = "/a/rel/path",
rootPath = "/a/root/path",
released = false,
secretKey = "a-secret-key",
releaseTime = OffsetDateTime.of(2019, 9, 21, 10, 30, 34, 15, ZoneOffset.UTC),
modificationTime = OffsetDateTime.of(2019, 9, 20, 10, 30, 34, 15, ZoneOffset.UTC),
creationTime = OffsetDateTime.of(2019, 9, 19, 10, 30, 34, 15, ZoneOffset.UTC),
attributes = listOf(ExtAttribute("AttachTo", "BioImages")),
tags = listOf(ExtTag("component", "web")),
collections = listOf(ExtCollection("BioImages")),
section = ExtSection(type = "Study"),
storageMode = StorageMode.NFS,
)
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package ac.uk.ebi.biostd.common.properties

import org.springframework.boot.context.properties.NestedConfigurationProperty
import java.util.Properties

class NotificationProperties {
lateinit var smtp: String
lateinit var uiUrl: String
lateinit var stUrl: String
lateinit var slackUrl: String

@NestedConfigurationProperty
var rt: RtConfig = RtConfig()

data class NotificationProperties(
val smtp: String,
val uiUrl: String,
val stUrl: String,
val slackUrl: String,
val rt: RtConfig,
) {
fun asProperties(): Properties = Properties().apply { setProperty("mail.host", smtp) }
}

class RtConfig {
lateinit var host: String
lateinit var queue: String
lateinit var user: String
lateinit var password: String
}
data class RtConfig(
val host: String,
val queue: String,
val user: String,
val password: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.springframework.amqp.rabbit.core.RabbitTemplate
import org.springframework.amqp.rabbit.listener.ConditionalRejectingErrorHandler
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
Expand All @@ -27,6 +28,7 @@ import uk.ac.ebi.extended.serialization.service.ExtSerializationService

@Configuration
@Import(NotificationPersistenceConfig::class)
@EnableConfigurationProperties(ApplicationProperties::class)
class Listeners {
@Bean
fun logListener(): LogSubmissionListener = LogSubmissionListener()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ package ac.uk.ebi.biostd.handlers.config

import ac.uk.ebi.biostd.common.properties.NotificationProperties
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.NestedConfigurationProperty
import org.springframework.stereotype.Component
import org.springframework.boot.context.properties.ConstructorBinding

@Component
@ConfigurationProperties(prefix = "app")
class ApplicationProperties {
lateinit var baseInstanceUrl: String

@NestedConfigurationProperty
var notifications: NotificationProperties = NotificationProperties()
}
@ConstructorBinding
data class ApplicationProperties(
val notifications: NotificationProperties,
)