Skip to content

Commit

Permalink
test: 수정 사항 반영 (이메일 수신자에 이름 속성 추가)
Browse files Browse the repository at this point in the history
  • Loading branch information
goldentrash committed Aug 19, 2024
1 parent 91f694a commit 160b364
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static org.springframework.test.util.AssertionErrors.fail;

import gdsc.konkuk.platformcore.application.email.exceptions.EmailAlreadyProcessedException;
import gdsc.konkuk.platformcore.controller.email.dtos.EmailReceiverInfo;
import gdsc.konkuk.platformcore.controller.email.dtos.EmailSendRequest;
import gdsc.konkuk.platformcore.domain.email.entity.EmailTask;
import gdsc.konkuk.platformcore.domain.email.repository.EmailTaskRepository;
Expand Down Expand Up @@ -74,11 +75,15 @@ void should_save_task_at_InMemoryTaskRepository() {
EmailSendRequest.builder()
.subject("subject")
.content("content")
.receivers(Set.of("example1.com", "example2.com"))
.receiverInfos(Set.of(
EmailReceiverInfo.builder().email("example1.com").name("guest1").build(),
EmailReceiverInfo.builder().email("example2.com").name("guest2").build()))
.sendAt(LocalDateTime.now().plusHours(1))
.build();

// when
EmailTask emailTask = emailTaskFacade.register(emailRequest);

// then
assertNotNull("Task should not be null", emailTask);
assertNotNull(
Expand All @@ -94,15 +99,18 @@ void should_send_task_when_time_is_up() throws InterruptedException {
EmailSendRequest.builder()
.subject("subject")
.content("content")
.receivers(Set.of("[email protected]", "[email protected]"))
.receiverInfos(Set.of(
EmailReceiverInfo.builder().email("[email protected]").name("guest1").build(),
EmailReceiverInfo.builder().email("[email protected]").name("guest2").build()))
.sendAt(LocalDateTime.now().plusSeconds(5L))
.build();

// when
emailTaskFacade.register(emailRequest);
sleep(10000);

// then
verify(emailClient).sendEmailToReceivers(any(EmailTask.class));

assertEquals(0, executor.getQueue().size());
assertEquals(0, taskInMemoryRepository.size());
}
Expand All @@ -113,7 +121,6 @@ void should_send_task_when_time_is_up() throws InterruptedException {
* 2. 작업 수정 요청
* 3. 기존의 작업 취소, 새로운 작업 예약
* */

@Test
@DisplayName("작업 수정 시 스케줄된 작업 취소 후 다시 스케줄")
void should_cancel_and_schedule_new_when_update() {
Expand All @@ -122,21 +129,29 @@ void should_cancel_and_schedule_new_when_update() {
EmailSendRequest.builder()
.subject("subject")
.content("content")
.receivers(Set.of("example1.com", "example2.com"))
.receiverInfos(Set.of(
EmailReceiverInfo.builder().email("example1.com").name("guest1").build(),
EmailReceiverInfo.builder().email("example2.com").name("guest2").build()))
.sendAt(LocalDateTime.now().plusHours(1))
.build();
EmailTask emailTask = emailTaskFacade.register(emailRequest);

assertEquals(1, executor.getQueue().size());
assertEquals(1, taskInMemoryRepository.size());

EmailSendRequest updatedRequest =
EmailSendRequest.builder()
.subject("subject")
.content("content")
.receivers(Set.of("example1.com", "example2.com"))
.receiverInfos(Set.of(
EmailReceiverInfo.builder().email("example1.com").name("guest1").build(),
EmailReceiverInfo.builder().email("example2.com").name("guest2").build()))
.sendAt(LocalDateTime.now().plusHours(2))
.build();

// when
emailTaskFacade.update(emailTask.getId(), updatedRequest);

// then
assertEquals(1, executor.getQueue().size());
assertNotNull(
Expand All @@ -158,7 +173,9 @@ void should_cancel_task() {
EmailSendRequest.builder()
.subject("subject")
.content("content")
.receivers(Set.of("example1.com", "example2.com"))
.receiverInfos(Set.of(
EmailReceiverInfo.builder().email("example1.com").name("guest1").build(),
EmailReceiverInfo.builder().email("example2.com").name("guest2").build()))
.sendAt(LocalDateTime.now().plusHours(1))
.build();
EmailTask emailTask = emailTaskFacade.register(emailRequest);
Expand All @@ -167,7 +184,6 @@ void should_cancel_task() {
// when
emailTaskFacade.cancel(emailTask.getId());


// then
assertEquals(0, executor.getQueue().size());
assertTrue(emailTaskRepository.findById(emailTask.getId()).isEmpty());
Expand All @@ -184,7 +200,9 @@ void should_fail_when_cancel_already_processed_task() throws Exception {
EmailSendRequest.builder()
.subject("subject")
.content("content")
.receivers(Set.of("example1.com", "example2.com"))
.receiverInfos(Set.of(
EmailReceiverInfo.builder().email("example1.com").name("guest1").build(),
EmailReceiverInfo.builder().email("example2.com").name("guest2").build()))
.sendAt(LocalDateTime.now().plusSeconds(1L))
.build();

Expand All @@ -208,11 +226,14 @@ void should_send_discord_message_when_email_sending_error() throws InterruptedEx
EmailSendRequest.builder()
.subject("subject")
.content("content")
.receivers(Set.of("example1.com", "example2.com"))
.receiverInfos(Set.of(
EmailReceiverInfo.builder().email("example1.com").name("guest1").build(),
EmailReceiverInfo.builder().email("example2.com").name("guest2").build()))
.sendAt(LocalDateTime.now().plusSeconds(1L))
.build();
doThrow(EmailSendingException.of(GlobalErrorCode.INTERNAL_SERVER_ERROR))
.when(emailClient).sendEmailToReceivers(any());

//when
EmailTask scheduledTask = emailTaskFacade.register(emailRequest);
sleep(2000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import gdsc.konkuk.platformcore.application.email.exceptions.EmailNotFoundException;
import gdsc.konkuk.platformcore.domain.email.entity.EmailDetails;
import gdsc.konkuk.platformcore.domain.email.entity.EmailReceiver;
import gdsc.konkuk.platformcore.domain.email.entity.EmailReceivers;
import gdsc.konkuk.platformcore.domain.email.entity.EmailTask;
import gdsc.konkuk.platformcore.domain.email.repository.EmailTaskRepository;
Expand All @@ -25,7 +26,10 @@ class EmailServiceHelperTest {
EmailTask.builder()
.id(1L)
.emailDetails(new EmailDetails("subject", "content"))
.receivers(new EmailReceivers(Set.of("example1.com", "example2.com")))
.receivers(new EmailReceivers(
Set.of(
EmailReceiver.builder().email("example1.com").name("guest1").build(),
EmailReceiver.builder().email("example2.com").name("guest2").build())))
.sendAt(LocalDateTime.of(2021, 1, 1, 1, 1))
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

import gdsc.konkuk.platformcore.application.email.exceptions.EmailAlreadyProcessedException;
import gdsc.konkuk.platformcore.application.email.exceptions.EmailNotFoundException;
import gdsc.konkuk.platformcore.controller.email.dtos.EmailReceiverInfo;
import gdsc.konkuk.platformcore.controller.email.dtos.EmailSendRequest;
import gdsc.konkuk.platformcore.domain.email.entity.EmailDetails;
import gdsc.konkuk.platformcore.domain.email.entity.EmailReceiver;
import gdsc.konkuk.platformcore.domain.email.entity.EmailReceivers;
import gdsc.konkuk.platformcore.domain.email.entity.EmailTask;
import gdsc.konkuk.platformcore.domain.email.repository.EmailTaskRepository;
Expand All @@ -30,23 +32,34 @@ class EmailServiceTest {
EmailTask.builder()
.id(1L)
.emailDetails(new EmailDetails("subject", "content"))
.receivers(new EmailReceivers(Set.of("example1.com", "example2.com")))
.receivers(new EmailReceivers(
Set.of(
EmailReceiver.builder().email("example1.com").name("guest1").build(),
EmailReceiver.builder().email("example2.com").name("guest2").build())))
.sendAt(LocalDateTime.of(2021, 1, 1, 1, 1))
.build();

private final EmailTask mock2 =
EmailTask.builder()
.id(2L)
.emailDetails(new EmailDetails("subject2", "content2"))
.receivers(new EmailReceivers(Set.of("example1.com", "example2.com")))
.receivers(new EmailReceivers(
Set.of(
EmailReceiver.builder().email("example1.com").name("guest1").build(),
EmailReceiver.builder().email("example2.com").name("guest2").build())))
.sendAt(LocalDateTime.now())
.build();

List<EmailTask> mockEmailTaskList = List.of(mock1, mock2);

private final EmailTask mockAlreadySent =
EmailTask.builder()
.id(3L)
.emailDetails(new EmailDetails("subject3", "content3"))
.receivers(new EmailReceivers(Set.of("example1.com", "example2.com")))
.receivers(new EmailReceivers(
Set.of(
EmailReceiver.builder().email("example1.com").name("guest1").build(),
EmailReceiver.builder().email("example2.com").name("guest2").build())))
.sendAt(LocalDateTime.now())
.build();

Expand All @@ -65,6 +78,7 @@ void should_success_when_getAllTaskAsList() {

// when
List<EmailTask> actual = subject.getAllTaskAsList();

// then
assertEquals(mockEmailTaskList.size(), actual.size());
assertEquals(mockEmailTaskList.get(0).getId(), actual.get(0).getId());
Expand All @@ -76,8 +90,10 @@ void should_success_when_getAllTaskAsList() {
void should_success_when_getTaskDetails() {
// given
given(emailTaskRepository.findById(1L)).willReturn(java.util.Optional.of(mock1));

// when
EmailTask actual = subject.getTaskDetails(1L);

// then
assertEquals(mock1.getId(), actual.getId());
assertEquals(mock1.getEmailDetails().getSubject(), actual.getEmailDetails().getSubject());
Expand All @@ -89,15 +105,18 @@ void should_success_when_getTaskDetails() {
@DisplayName("registerTask : 이메일 전송 작업 등록 성공")
void should_success_when_register_task() {
// given

EmailSendRequest emailRequest =
EmailSendRequest.builder()
.subject("subject")
.content("content")
.receivers(Set.of("example1.com", "example2.com"))
.receiverInfos(
Set.of(
EmailReceiverInfo.builder().email("example1.com").name("guest1").build(),
EmailReceiverInfo.builder().email("example2.com").name("guest2").build()))
.sendAt(LocalDateTime.of(2021, 1, 1, 1, 1))
.build();
given(emailTaskRepository.save(any(EmailTask.class))).willReturn(mock1);

// when
EmailTask expected = EmailSendRequest.toEntity(emailRequest);
EmailTask actual = subject.registerTask(EmailSendRequest.toEntity(emailRequest));
Expand All @@ -115,13 +134,18 @@ void should_success_when_update_task() {
EmailSendRequest.builder()
.subject("subject2")
.content("content2")
.receivers(Set.of("example2.com", "example4.com"))
.receiverInfos(
Set.of(
EmailReceiverInfo.builder().email("example2.com").name("guest2").build(),
EmailReceiverInfo.builder().email("example4.com").name("guest4").build()))
.sendAt(LocalDateTime.of(2021, 1, 1, 1, 1))
.build();
given(emailTaskRepository.findById(1L)).willReturn(java.util.Optional.of(mock1));

// when
EmailTask expected = EmailSendRequest.toEntity(emailRequest);
EmailTask actual = subject.update(1L, emailRequest);

// then
assertEquals(expected.getEmailDetails(), actual.getEmailDetails());
assertEquals(expected.getEmailReceivers(), actual.getEmailReceivers());
Expand All @@ -135,11 +159,16 @@ void should_fail_when_update_task_already_sent() {
EmailSendRequest.builder()
.subject("subject2")
.content("content2")
.receivers(Set.of("example2.com", "example4.com"))
.receiverInfos(
Set.of(
EmailReceiverInfo.builder().email("example2.com").name("guest2").build(),
EmailReceiverInfo.builder().email("example4.com").name("guest4").build()))
.sendAt(LocalDateTime.of(2021, 1, 1, 1, 1))
.build();

// when
when(emailTaskRepository.findById(1L)).thenReturn(Optional.of(mockAlreadySent));

// then
assertThrows(EmailAlreadyProcessedException.class, () -> subject.update(1L, emailRequest));
}
Expand All @@ -152,11 +181,16 @@ void should_fail_when_update_task_not_found() {
EmailSendRequest.builder()
.subject("subject2")
.content("content2")
.receivers(Set.of("example2.com", "example4.com"))
.receiverInfos(
Set.of(
EmailReceiverInfo.builder().email("example2.com").name("guest2").build(),
EmailReceiverInfo.builder().email("example4.com").name("guest4").build()))
.sendAt(LocalDateTime.of(2021, 1, 1, 1, 1))
.build();

// when
when(emailTaskRepository.findById(1L)).thenReturn(Optional.empty());

// then
assertThrows(EmailNotFoundException.class, () -> subject.update(1L, emailRequest));
}
Expand All @@ -166,9 +200,11 @@ void should_fail_when_update_task_not_found() {
void should_success_when_delete_task() {
// given
given(emailTaskRepository.findById(1L)).willReturn(Optional.of(mock1));

// when
doNothing().when(emailTaskRepository).delete(mock1);
subject.delete(1L);

// then
verify(emailTaskRepository).delete(mock1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.openMocks;

import gdsc.konkuk.platformcore.controller.email.dtos.EmailReceiverInfo;
import gdsc.konkuk.platformcore.controller.email.dtos.EmailSendRequest;
import gdsc.konkuk.platformcore.domain.email.entity.EmailDetails;
import gdsc.konkuk.platformcore.domain.email.entity.EmailReceiver;
import gdsc.konkuk.platformcore.domain.email.entity.EmailReceivers;
import gdsc.konkuk.platformcore.domain.email.entity.EmailTask;
import gdsc.konkuk.platformcore.global.scheduler.TaskScheduler;
Expand All @@ -31,7 +33,10 @@ class EmailTaskFacadeTest {
EmailTask.builder()
.id(1L)
.emailDetails(new EmailDetails("subject", "content"))
.receivers(new EmailReceivers(Set.of("example1.com", "example2.com")))
.receivers(new EmailReceivers(
Set.of(
EmailReceiver.builder().email("example1.com").name("guest1").build(),
EmailReceiver.builder().email("example2.com").name("guest2").build())))
.sendAt(LocalDateTime.of(2021, 1, 1, 1, 1))
.build();

Expand All @@ -49,11 +54,16 @@ void should_success_when_reschedule_task() {
EmailSendRequest.builder()
.subject("subject")
.content("content")
.receivers(Set.of("example1.com", "example2.com"))
.receiverInfos(
Set.of(
EmailReceiverInfo.builder().email("example1.com").name("guest1").build(),
EmailReceiverInfo.builder().email("example2.com").name("guest2").build()))
.sendAt(LocalDateTime.of(2021, 1, 1, 1, 1))
.build();

//when
when(emailService.update(1L, emailRequest)).thenReturn(mock1);

//then
subject.update(1L, emailRequest);
verify(emailTaskScheduler).cancelTask("1");
Expand All @@ -64,6 +74,7 @@ void should_success_when_reschedule_task() {
void should_success_when_cancel_task() {
//given
Long emailId = 1L;

//when
subject.cancel(1L);

Expand Down
Loading

0 comments on commit 160b364

Please sign in to comment.