From c19f9df029dc1df988a91472ba688688ce8061b7 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Mon, 15 Jul 2019 12:53:18 +0100 Subject: [PATCH] Added ability for observation data to be sent by email --- backend/pom.xml | 27 +++++++++-- .../uk/co/neviyn/observationdatabase/Email.kt | 48 +++++++++++++++++++ .../src/main/resources/application.properties | 8 +++- .../observationdatabase/RepositoryTest.kt | 6 ++- .../controller/GroupSessionControllerTest.kt | 9 +++- .../controller/ObservationsControllerTest.kt | 15 ++++-- 6 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Email.kt diff --git a/backend/pom.xml b/backend/pom.xml index c361fa1..9299d2d 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -21,9 +21,9 @@ UTF-8 UTF-8 1.8 - 1.2.71 + 1.3.40 1.8 - 2.0.6.RELEASE + 2.1.6.RELEASE @@ -67,12 +67,23 @@ com.fasterxml.jackson.module jackson-module-kotlin + 2.9.9 org.jetbrains.kotlin kotlin-stdlib-jdk8 ${kotlin.version} + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-stdlib-jdk7 + ${kotlin.version} + org.jetbrains.kotlin kotlin-reflect @@ -96,7 +107,7 @@ com.fasterxml.jackson.datatype jackson-datatype-joda - 2.9.5 + 2.9.9 org.jadira.usertype @@ -108,6 +119,16 @@ caffeine 2.6.2 + + org.simplejavamail + simple-java-mail + 5.2.0 + + + org.jetbrains.kotlinx + kotlinx-coroutines-core + 1.3.0-M2 + org.springframework.boot diff --git a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Email.kt b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Email.kt new file mode 100644 index 0000000..293bd4f --- /dev/null +++ b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Email.kt @@ -0,0 +1,48 @@ +package uk.co.neviyn.observationdatabase + +import org.joda.time.DateTime +import org.simplejavamail.email.EmailBuilder +import org.simplejavamail.mailer.Mailer +import org.simplejavamail.mailer.MailerBuilder +import org.slf4j.Logger +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.core.env.Environment +import org.springframework.stereotype.Service + +@Service +class Email { + @Autowired + lateinit var environment: Environment + + private val logger: Logger = LoggerFactory.getLogger(javaClass)!! + + fun sendObservationData(observations: List) { + if (observations.isEmpty()) { + logger.error("Cannot send observations email containing no observations!") + return + } + if (environment.getProperty("smtp.enabled")!!.toBoolean()) { + logger.debug("SMTP is enables") + logger.debug("Building CSV file content") + logger.debug("Setting up mailer environment") + val mailer = createMailer() + logger.debug("Constructing email") + val email = EmailBuilder.startingBlank() + .to("Simulator Team", environment.getProperty("smtp.toaddress")) + .withSubject("Observation Session Data") + .withPlainText("Attached is the observation data for the session completed ${DateTime.now()}.") + .withAttachment("observations.csv", observationsToCSV(observations).toByteArray(), "text/csv") + .buildEmail() + logger.info("Sending mail to ${email.recipients}") + mailer.sendMail(email) + logger.debug("Email sent") + } else { + logger.info("SMTP is not enabled in the configuration, no mail will be sent.") + } + } + + private fun createMailer(): Mailer = MailerBuilder.withSMTPServer(environment.getProperty("smtp.host"), + environment.getProperty("smtp.port")!!.toInt(), environment.getProperty("smtp.username"), + environment.getProperty("smtp.password")).buildMailer() +} \ No newline at end of file diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index 34c3abb..34d00d7 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -5,4 +5,10 @@ obsdb.password=observation spring.cache.type=caffeine spring.cache.cache-names=observations,charts spring.cache.caffeine.spec=maximumSize=500,expireAfterAccess=600s -management.endpoints.web.exposure.include=health,info,metrics \ No newline at end of file +management.endpoints.web.exposure.include=health,info,metrics +smtp.enabled=false +smtp.server=x +smtp.port=587 +smtp.username=none +smtp.password=none +smtp.toaddress=none \ No newline at end of file diff --git a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/RepositoryTest.kt b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/RepositoryTest.kt index cd236db..a36457d 100644 --- a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/RepositoryTest.kt +++ b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/RepositoryTest.kt @@ -1,7 +1,11 @@ package uk.co.neviyn.observationdatabase -import junit.framework.TestCase.* import org.joda.time.LocalDate +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNotSame +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue import org.junit.Test import org.junit.runner.RunWith import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest diff --git a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionControllerTest.kt b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionControllerTest.kt index 98b1f42..feeea64 100644 --- a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionControllerTest.kt +++ b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionControllerTest.kt @@ -2,7 +2,9 @@ package uk.co.neviyn.observationdatabase.controller import org.joda.time.LocalDate import org.junit.After -import org.junit.Assert.* +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertTrue import org.junit.Test import org.junit.runner.RunWith import org.mockito.ArgumentMatchers.any @@ -14,6 +16,7 @@ import org.mockito.Mockito.verify import org.mockito.junit.MockitoJUnitRunner import org.springframework.messaging.simp.SimpMessagingTemplate import org.springframework.web.server.ResponseStatusException +import uk.co.neviyn.observationdatabase.Email import uk.co.neviyn.observationdatabase.GroupObservation import uk.co.neviyn.observationdatabase.GroupObservationInit import uk.co.neviyn.observationdatabase.GroupSessionManager @@ -28,7 +31,7 @@ import uk.co.neviyn.observationdatabase.SiteRepository import uk.co.neviyn.observationdatabase.TrainingType import uk.co.neviyn.observationdatabase.Tutor import uk.co.neviyn.observationdatabase.TutorRepository -import java.util.* +import java.util.Optional @RunWith(MockitoJUnitRunner::class) class GroupSessionControllerTest { @@ -46,6 +49,8 @@ class GroupSessionControllerTest { lateinit var observationRepository: ObservationRepository @Mock lateinit var personRepository: PersonRepository + @Mock + lateinit var mailer: Email @After fun tearDown() { diff --git a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsControllerTest.kt b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsControllerTest.kt index 47bb365..375aeed 100644 --- a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsControllerTest.kt +++ b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsControllerTest.kt @@ -1,11 +1,18 @@ package uk.co.neviyn.observationdatabase.controller -import junit.framework.TestCase.* import org.joda.time.LocalDate +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue import org.junit.Test import org.junit.runner.RunWith -import org.mockito.* -import org.mockito.Mockito.* +import org.mockito.ArgumentMatchers +import org.mockito.InjectMocks +import org.mockito.Mock +import org.mockito.Mockito.doReturn +import org.mockito.Mockito.times +import org.mockito.Mockito.verify import org.mockito.junit.MockitoJUnitRunner import org.springframework.web.server.ResponseStatusException import uk.co.neviyn.observationdatabase.NameValue @@ -24,7 +31,7 @@ import uk.co.neviyn.observationdatabase.SiteRepository import uk.co.neviyn.observationdatabase.TrainingType import uk.co.neviyn.observationdatabase.Tutor import uk.co.neviyn.observationdatabase.TutorRepository -import java.util.* +import java.util.Optional @RunWith(MockitoJUnitRunner::class) class ObservationsControllerTest {