Added ability for observation data to be sent by email

This commit is contained in:
neviyn 2019-07-15 12:53:18 +01:00
parent 0778586e51
commit c19f9df029
6 changed files with 102 additions and 11 deletions

View File

@ -21,9 +21,9 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<kotlin.version>1.2.71</kotlin.version>
<kotlin.version>1.3.40</kotlin.version>
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
<spring.version>2.0.6.RELEASE</spring.version>
<spring.version>2.1.6.RELEASE</spring.version>
</properties>
<dependencyManagement>
@ -67,12 +67,23 @@
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk7</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
@ -96,7 +107,7 @@
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.9.5</version>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>org.jadira.usertype</groupId>
@ -108,6 +119,16 @@
<artifactId>caffeine</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
<version>1.3.0-M2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -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<Observation>) {
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()
}

View File

@ -6,3 +6,9 @@ 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
smtp.enabled=false
smtp.server=x
smtp.port=587
smtp.username=none
smtp.password=none
smtp.toaddress=none

View File

@ -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

View File

@ -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() {

View File

@ -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 {