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

Fix jpa element collection type #20

Merged
merged 8 commits into from
Aug 11, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import gdsc.konkuk.platformcore.controller.email.dto.EmailSendRequest;
import gdsc.konkuk.platformcore.domain.email.entity.EmailTask;
import gdsc.konkuk.platformcore.domain.email.repository.EmailTaskRepository;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
Expand Down Expand Up @@ -44,8 +46,10 @@ public EmailTask update(Long emailId, EmailSendRequest request) {
validateEmailTaskAlreadySent(task);

task.changeEmailDetails(request.toEmailDetails());
task.changeEmailReceivers(request.toEmailReceivers());
task.changeSendAt(request.getSendAt());

Set<String> updatedReceivers = mergeReceivers(task, request.getReceivers());
task.changeEmailReceivers(updatedReceivers);
return task;
}

Expand All @@ -66,4 +70,12 @@ private void validateEmailTaskAlreadySent(EmailTask emailTask) {
throw EmailAlreadyProcessedException.of(EmailErrorCode.EMAIL_ALREADY_PROCESSED);
}
}

private Set<String> mergeReceivers(EmailTask emailTask, Set<String> updatedReceivers) {
List<String> receiversInPrevSet = emailTask.filterReceiversInPrevSet(updatedReceivers);
List<String> receiversInNewSet = emailTask.filterReceiversNotInPrevSet(updatedReceivers);
Set<String> mergedReceiver = new HashSet<>(receiversInPrevSet);
mergedReceiver.addAll(receiversInNewSet);
return mergedReceiver;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -20,12 +20,12 @@ public class EmailSendRequest {
@NotEmpty
private String content;
@NotNull
private List<@Email String> receivers;
private Set<@Email String> receivers;
@NotNull
private LocalDateTime sendAt;

@Builder
public EmailSendRequest(String subject, String content, List<String> receivers, LocalDateTime sendAt) {
public EmailSendRequest(String subject, String content, Set<String> receivers, LocalDateTime sendAt) {
this.subject = subject;
this.content = content;
this.receivers = receivers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ public class EmailTaskDetailsResponse {
public EmailTaskDetailsResponse(EmailDetails emailDetails, EmailReceivers emailReceivers, LocalDateTime sendAt) {
this.subject = emailDetails.getSubject();
this.content = emailDetails.getContent();
this.receivers = emailReceivers.getReceivers();
this.receivers = emailReceivers
.getReceivers()
.stream()
.toList();
this.sendAt = sendAt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import jakarta.persistence.CollectionTable;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Objects;

import jakarta.persistence.Embeddable;
import java.util.Set;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -20,14 +19,11 @@
public class EmailReceivers {

@ElementCollection
@CollectionTable(
name = "email_receivers",
joinColumns = @JoinColumn(name = "task_id")
)
@CollectionTable(name = "email_receivers", joinColumns = @JoinColumn(name = "task_id"))
@Column(name = "receiver_email")
List<String> receivers = new ArrayList<>();
Set<String> receivers = new HashSet<>();

public EmailReceivers(List<String> receivers) {
public EmailReceivers(Set<String> receivers) {
this.receivers.addAll(receivers);
}

Expand All @@ -43,4 +39,12 @@ public boolean equals(Object o) {
EmailReceivers that = (EmailReceivers) o;
return Objects.equals(receivers, that.receivers);
}

public void removeAll() {
this.receivers.clear();
}

public void insertAll(Set<String> set) {
this.receivers.addAll(set);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import java.util.List;
import java.util.Set;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -48,8 +50,9 @@ public void changeEmailDetails(final EmailDetails newEmailDetails) {
emailDetails = newEmailDetails;
}

public void changeEmailReceivers(final EmailReceivers newEmailReceivers) {
emailReceivers = newEmailReceivers;
public void changeEmailReceivers(final Set<String> set) {
emailReceivers.removeAll();
emailReceivers.insertAll(set);
}

public void changeSendAt(final LocalDateTime newSendAt) {
Expand All @@ -59,4 +62,18 @@ public void changeSendAt(final LocalDateTime newSendAt) {
public void markAsSent() {
isSent = true;
}

public List<String> filterReceiversInPrevSet(Set<String> set) {
return this.getEmailReceivers().getReceivers()
.stream()
.filter(set::contains)
.toList();
}

public List<String> filterReceiversNotInPrevSet(Set<String> set) {
return set
.stream()
.filter((e) -> !emailReceivers.getReceivers().contains(e))
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gdsc.konkuk.platformcore.external.email;

import java.util.List;

import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
Expand All @@ -27,7 +26,7 @@ public class EmailClient {

public void sendEmailToReceivers(EmailTask email) {
EmailDetails emailDetails = email.getEmailDetails();
List<String> receivers = email.getEmailReceivers().getReceivers();
Set<String> receivers = email.getEmailReceivers().getReceivers();
receivers.forEach(receiver -> sendEmail(receiver, emailDetails));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package gdsc.konkuk.platformcore.annotation;

import gdsc.konkuk.platformcore.domain.member.entity.MemberRole;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import org.springframework.security.test.context.support.WithSecurityContext;

import gdsc.konkuk.platformcore.domain.member.entity.MemberRole;

@Retention(RetentionPolicy.RUNTIME)
@WithSecurityContext(factory = WithMyCustomUserSecurityContextFactory.class)
public @interface CustomMockUser {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gdsc.konkuk.platformcore.annotation;

import gdsc.konkuk.platformcore.domain.member.entity.MemberRole;
import java.util.List;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
Expand All @@ -10,8 +10,6 @@
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.test.context.support.WithSecurityContextFactory;

import gdsc.konkuk.platformcore.domain.member.entity.MemberRole;

public class WithMyCustomUserSecurityContextFactory implements WithSecurityContextFactory<CustomMockUser> {
@Override
public SecurityContext createSecurityContext(CustomMockUser annotation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import gdsc.konkuk.platformcore.global.exceptions.TaskNotFoundException;
import gdsc.konkuk.platformcore.global.scheduler.TaskInMemoryRepository;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -72,7 +72,7 @@ void should_save_task_at_InMemoryTaskRepository() {
EmailSendRequest.builder()
.subject("subject")
.content("content")
.receivers(List.of("example1.com", "example2.com"))
.receivers(Set.of("example1.com", "example2.com"))
.sendAt(LocalDateTime.now().plusHours(1))
.build();
// when
Expand All @@ -92,7 +92,7 @@ void should_send_task_when_time_is_up() throws InterruptedException {
EmailSendRequest.builder()
.subject("subject")
.content("content")
.receivers(List.of("[email protected]", "[email protected]"))
.receivers(Set.of("[email protected]", "[email protected]"))
.sendAt(LocalDateTime.now().plusSeconds(5L))
.build();
// when
Expand Down Expand Up @@ -120,7 +120,7 @@ void should_cancel_and_schedule_new_when_update() {
EmailSendRequest.builder()
.subject("subject")
.content("content")
.receivers(List.of("example1.com", "example2.com"))
.receivers(Set.of("example1.com", "example2.com"))
.sendAt(LocalDateTime.now().plusHours(1))
.build();
EmailTask emailTask = emailTaskFacade.register(emailRequest);
Expand All @@ -130,7 +130,7 @@ void should_cancel_and_schedule_new_when_update() {
EmailSendRequest.builder()
.subject("subject")
.content("content")
.receivers(List.of("example1.com", "example2.com"))
.receivers(Set.of("example1.com", "example2.com"))
.sendAt(LocalDateTime.now().plusHours(2))
.build();
// when
Expand All @@ -156,7 +156,7 @@ void should_cancel_task() {
EmailSendRequest.builder()
.subject("subject")
.content("content")
.receivers(List.of("example1.com", "example2.com"))
.receivers(Set.of("example1.com", "example2.com"))
.sendAt(LocalDateTime.now().plusHours(1))
.build();
EmailTask emailTask = emailTaskFacade.register(emailRequest);
Expand All @@ -182,7 +182,7 @@ void should_fail_when_cancel_already_processed_task() throws Exception {
EmailSendRequest.builder()
.subject("subject")
.content("content")
.receivers(List.of("example1.com", "example2.com"))
.receivers(Set.of("example1.com", "example2.com"))
.sendAt(LocalDateTime.now().plusSeconds(1L))
.build();
//when
Expand All @@ -202,7 +202,7 @@ void should_send_discord_message_when_email_sending_error() throws InterruptedEx
EmailSendRequest.builder()
.subject("subject")
.content("content")
.receivers(List.of("example1.com", "example2.com"))
.receivers(Set.of("example1.com", "example2.com"))
.sendAt(LocalDateTime.now().plusSeconds(1L))
.build();
doThrow(EmailSendingException.of(GlobalErrorCode.INTERNAL_SERVER_ERROR))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import gdsc.konkuk.platformcore.domain.email.entity.EmailTask;
import gdsc.konkuk.platformcore.domain.email.repository.EmailTaskRepository;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
Expand All @@ -25,7 +25,7 @@ class EmailServiceHelperTest {
EmailTask.builder()
.id(1L)
.emailDetails(new EmailDetails("subject", "content"))
.receivers(new EmailReceivers(List.of("example1.com", "example2.com")))
.receivers(new EmailReceivers(Set.of("example1.com", "example2.com")))
.sendAt(LocalDateTime.of(2021, 1, 1, 1, 1))
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -29,23 +30,23 @@ class EmailServiceTest {
EmailTask.builder()
.id(1L)
.emailDetails(new EmailDetails("subject", "content"))
.receivers(new EmailReceivers(List.of("example1.com", "example2.com")))
.receivers(new EmailReceivers(Set.of("example1.com", "example2.com")))
.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(List.of("example1.com", "example2.com")))
.receivers(new EmailReceivers(Set.of("example1.com", "example2.com")))
.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(List.of("example1.com", "example2.com")))
.receivers(new EmailReceivers(Set.of("example1.com", "example2.com")))
.sendAt(LocalDateTime.now())
.build();

Expand Down Expand Up @@ -93,7 +94,7 @@ void should_success_when_register_task() {
EmailSendRequest.builder()
.subject("subject")
.content("content")
.receivers(List.of("example1.com", "example2.com"))
.receivers(Set.of("example1.com", "example2.com"))
.sendAt(LocalDateTime.of(2021, 1, 1, 1, 1))
.build();
given(emailTaskRepository.save(any(EmailTask.class))).willReturn(mock1);
Expand All @@ -114,7 +115,7 @@ void should_success_when_update_task() {
EmailSendRequest.builder()
.subject("subject2")
.content("content2")
.receivers(List.of("example2.com", "example4.com"))
.receivers(Set.of("example2.com", "example4.com"))
.sendAt(LocalDateTime.of(2021, 1, 1, 1, 1))
.build();
given(emailTaskRepository.findById(1L)).willReturn(java.util.Optional.of(mock1));
Expand All @@ -134,7 +135,7 @@ void should_fail_when_update_task_already_sent() {
EmailSendRequest.builder()
.subject("subject2")
.content("content2")
.receivers(List.of("example2.com", "example4.com"))
.receivers(Set.of("example2.com", "example4.com"))
.sendAt(LocalDateTime.of(2021, 1, 1, 1, 1))
.build();
// when
Expand All @@ -151,7 +152,7 @@ void should_fail_when_update_task_not_found() {
EmailSendRequest.builder()
.subject("subject2")
.content("content2")
.receivers(List.of("example2.com", "example4.com"))
.receivers(Set.of("example2.com", "example4.com"))
.sendAt(LocalDateTime.of(2021, 1, 1, 1, 1))
.build();
// when
Expand Down
Loading