Skip to content

Commit

Permalink
refactor: delete factory method from MailMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
woowabrie committed Oct 9, 2023
1 parent a869835 commit 00ee05f
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 117 deletions.
7 changes: 1 addition & 6 deletions src/main/kotlin/apply/application/mail/MailData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ data class MailData(

fun toMailMessage(): MailMessage {
// TODO: 작성자 ID 바인딩
return MailMessage.of(subject, body, sender, recipients, 1L)
}

fun toReservationMailMessage(): MailMessage {
// TODO: 작성자 ID 바인딩
return MailMessage.withReservation(subject, body, sender, recipients, sentTime, 1L)
return MailMessage(subject, body, sender, recipients, 1L)
}
}
10 changes: 8 additions & 2 deletions src/main/kotlin/apply/application/mail/MailMessageService.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package apply.application.mail

import apply.domain.mail.MailMessageRepository
import apply.domain.mail.MailReservation
import apply.domain.mail.MailReservationRepository
import apply.domain.mail.getOrThrow
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Transactional
@Service
class MailMessageService(
private val mailMessageRepository: MailMessageRepository
private val mailMessageRepository: MailMessageRepository,
private val mailReservationRepository: MailReservationRepository,
) {
fun reserve(request: MailData): MailMessageResponse {
val mailMessage = mailMessageRepository.save(request.toReservationMailMessage())
val mailMessage = mailMessageRepository.save(request.toMailMessage())
val mailReservation = mailReservationRepository.save(
MailReservation(mailMessage, reservationTime = request.sentTime)
)
return MailMessageResponse(mailMessage)
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/apply/config/DatabaseInitializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ class DatabaseInitializer(
}

private fun populateMailHistories() {
val mailMessage = MailMessage.of(
val mailMessage = MailMessage(
subject = "[우아한테크코스] 프리코스를 진행하는 목적과 사전 준비",
body = "안녕하세요.",
sender = "[email protected]",
Expand Down
41 changes: 1 addition & 40 deletions src/main/kotlin/apply/domain/mail/MailMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import javax.persistence.Lob
import javax.persistence.OneToMany

@Entity
class MailMessage private constructor(
class MailMessage(
@Column(nullable = false)
val subject: String,

Expand Down Expand Up @@ -54,43 +54,4 @@ class MailMessage private constructor(
private fun hasReservation(): Boolean {
return reservations.isNotEmpty()
}

companion object {
fun of(
subject: String,
body: String,
sender: String,
recipients: List<String>,
creatorId: Long
): MailMessage {
return MailMessage(
subject = subject,
body = body,
sender = sender,
recipients = recipients,
creatorId = creatorId
)
}

fun withReservation(
subject: String,
body: String,
sender: String,
recipients: List<String>,
reservationTime: LocalDateTime,
creatorId: Long
): MailMessage {
return MailMessage(
subject = subject,
body = body,
sender = sender,
recipients = recipients,
creatorId = creatorId
).apply {
reservations.add(
MailReservation(this, reservationTime = reservationTime)
)
}
}
}
}
25 changes: 3 additions & 22 deletions src/test/kotlin/apply/MailHistoryFixtures.kt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,15 @@ fun createMailMessage(
id: Long = 0L,
creatorId: Long = 0L
): MailMessage {
return MailMessage.of(subject, body, sender, recipients, creatorId)
}

fun createReservationMailMessage(
subject: String = SUBJECT,
body: String = BODY,
sender: String = SENDER,
recipients: List<String> = RECIPIENTS,
reservationTime: LocalDateTime = RESERVATION_TIME,
id: Long = 0L,
creatorId: Long = 0L
): MailMessage {
return MailMessage.withReservation(subject, body, sender, recipients, reservationTime, creatorId)
return MailMessage(subject, body, sender, recipients, creatorId, id = id)
}

fun createMailReservation(
subject: String = SUBJECT,
body: String = BODY,
sender: String = SENDER,
recipients: List<String> = RECIPIENTS,
mailMessage: MailMessage = createMailMessage(),
reservationTime: LocalDateTime = RESERVATION_TIME,
id: Long = 0L,
creatorId: Long = 0L
): MailReservation {
return MailReservation(
createMailMessage(subject, body, sender, recipients, id, creatorId),
reservationTime = reservationTime
)
return MailReservation(mailMessage, reservationTime = reservationTime, id = id)
}

fun createSuccessMailHistory(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package apply.application.mail

import apply.createMailData
import apply.createReservationMailMessage
import apply.createMailMessage
import apply.createMailReservation
import apply.domain.mail.MailMessageRepository
import apply.domain.mail.MailReservationRepository
import apply.domain.mail.getOrThrow
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.BehaviorSpec
Expand All @@ -20,7 +22,7 @@ import java.time.LocalDateTime.now
class MailMessageIntegrationTest(
private val mailMessageService: MailMessageService,
private val mailMessageRepository: MailMessageRepository,
private val mailReservationRepository: MailMessageRepository
private val mailReservationRepository: MailReservationRepository
) : BehaviorSpec({
extensions(SpringTestExtension(SpringTestLifecycleMode.Root))

Expand All @@ -42,48 +44,49 @@ class MailMessageIntegrationTest(
}

Given("취소하고 싶은 예약 메일이 있는 경우") {
val mailMessage = mailMessageRepository.save(createReservationMailMessage())
val mailMessage = mailMessageRepository.save(createMailMessage())
val mailReservation = mailReservationRepository.save(createMailReservation(mailMessage))

When("메일 예약을 취소하면") {
mailMessageService.cancelReservation(mailMessage.id)

Then("메일 메시지와 예약이 삭제된다") {
mailMessageRepository.findById(mailMessage.id).isEmpty shouldBe true
mailReservationRepository.findAll() shouldBe emptyList()
mailReservationRepository.findById(mailReservation.id).isEmpty shouldNotBe true
}
}
}

Given("취소하고 싶은 예약 메일이 처리중인 경우") {
val mailMessage = mailMessageRepository.save(createReservationMailMessage())
mailMessage.reservation()?.send()
val mailMessage = mailMessageRepository.save(createMailMessage())
val mailReservation = mailReservationRepository.save(createMailReservation(mailMessage))
mailReservation.send()

When("메일 예약을 취소하면") {
Then("에러가 발생하고 메일 메시지와 예약은 남아있다") {
shouldThrow<IllegalStateException> {
mailMessageService.cancelReservation(mailMessage.id)
}

val actual = mailMessageRepository.getOrThrow(mailMessage.id)
actual shouldNotBe null
actual.reservation() shouldNotBe null
mailMessageRepository.findById(mailMessage.id).isPresent shouldNotBe true
mailReservationRepository.findById(mailReservation.id).isPresent shouldNotBe true
}
}
}

Given("취소하고 싶은 예약 메일이 발송 완료된 경우") {
val mailMessage = mailMessageRepository.save(createReservationMailMessage())
mailMessage.reservation()?.finish()
val mailMessage = mailMessageRepository.save(createMailMessage())
val mailReservation = mailReservationRepository.save(createMailReservation(mailMessage))
mailReservation.finish()

When("메일 예약을 취소하면") {
Then("에러가 발생하고 메일 메시지와 예약은 남아있다") {
shouldThrow<IllegalStateException> {
mailMessageService.cancelReservation(mailMessage.id)
}

val actual = mailMessageRepository.getOrThrow(mailMessage.id)
actual shouldNotBe null
actual.reservation() shouldNotBe null
mailMessageRepository.findById(mailMessage.id).isPresent shouldNotBe true
mailReservationRepository.findById(mailReservation.id).isPresent shouldNotBe true
}
}
}
Expand Down
22 changes: 0 additions & 22 deletions src/test/kotlin/apply/domain/mail/MailMessageRepositoryTest.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package apply.domain.mail

import apply.createMailMessage
import apply.createReservationMailMessage
import io.kotest.core.spec.style.ExpectSpec
import io.kotest.extensions.spring.SpringExtension
import io.kotest.matchers.longs.shouldNotBeZero
import io.kotest.matchers.nulls.shouldBeNull
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager
import support.test.RepositoryTest
import support.test.spec.afterRootTest
import java.time.LocalDateTime.now

@RepositoryTest
class MailMessageRepositoryTest(
Expand All @@ -25,13 +22,6 @@ class MailMessageRepositoryTest(
val actual = mailMessageRepository.save(createMailMessage())
actual.id.shouldNotBeZero()
}

expect("메일 메시지와 함께 메일 예약을 저장한다") {
val actual = mailMessageRepository.save(createReservationMailMessage())
actual.id.shouldNotBeZero()
actual.reservation().shouldNotBeNull()
actual.reservation()!!.id.shouldNotBeZero()
}
}

context("즉시 발송 메일 메시지 조회") {
Expand All @@ -44,18 +34,6 @@ class MailMessageRepositoryTest(
}
}

context("예약 메일 메시지 조회") {
val reservationTime = now().plusHours(3).withMinute(10)
val mailMessage = mailMessageRepository.save(createReservationMailMessage(reservationTime = reservationTime))

expect("메일 메시지와 함께 메일 예약을 조회한다") {
val actual = mailMessageRepository.findById(mailMessage.id).get()
actual.shouldNotBeNull()
actual.reservation().shouldNotBeNull()
actual.reservation()!!.reservationTime shouldBe reservationTime
}
}

afterEach {
entityManager.flush()
entityManager.clear()
Expand Down
10 changes: 0 additions & 10 deletions src/test/kotlin/apply/domain/mail/MailMessageTest.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package apply.domain.mail

import apply.createMailMessage
import apply.createReservationMailMessage
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe

class MailMessageTest : StringSpec({
"즉시 발송하는 메일 메시지를 생성한다" {
Expand All @@ -14,12 +12,4 @@ class MailMessageTest : StringSpec({
mailMessage.body shouldBe "내용"
mailMessage.reservation() shouldBe null
}

"예약 메일 메시지를 생성한다" {
val mailMessage = createReservationMailMessage(subject = "제목", body = "내용")

mailMessage.subject shouldBe "제목"
mailMessage.body shouldBe "내용"
mailMessage.reservation() shouldNotBe null
}
})

0 comments on commit 00ee05f

Please sign in to comment.