Skip to content

Commit

Permalink
refactor/#22: EventPublisher 를 사용한 메일 메서드 호출
Browse files Browse the repository at this point in the history
  • Loading branch information
pushedrumex committed Feb 21, 2024
1 parent bdb367f commit 5897274
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.verby.indp.domain.contact.service;

import com.verby.indp.domain.common.event.mail.MailSendEvent;
import com.verby.indp.domain.common.notification.mail.Mail;
import com.verby.indp.domain.common.notification.mail.MailService;
import com.verby.indp.domain.contact.Contact;
import com.verby.indp.domain.contact.dto.request.RegisterContactRequest;
import com.verby.indp.domain.contact.repository.ContactRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -19,7 +21,7 @@ public class ContactService {
private String to;

private final ContactRepository contactRepository;
private final MailService mailService;
private final ApplicationEventPublisher applicationEventPublisher;

@Transactional
public long registerContact(RegisterContactRequest request) {
Expand All @@ -30,7 +32,7 @@ public long registerContact(RegisterContactRequest request) {
"문의 내용: " + request.content() + "\n" +
"문의자 성함: " + request.userName() + "\n" +
"문의자 연락처: " + request.phoneNumber() + "\n");
mailService.sendMail(mail);
applicationEventPublisher.publishEvent(new MailSendEvent(mail));

return persistContact.getContactId();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.verby.indp.domain.recommendation.service;

import com.verby.indp.domain.common.event.mail.MailSendEvent;
import com.verby.indp.domain.common.notification.mail.Mail;
import com.verby.indp.domain.common.notification.mail.MailService;
import com.verby.indp.domain.recommendation.Recommendation;
Expand All @@ -9,6 +10,7 @@
import com.verby.indp.domain.store.repository.StoreRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -22,7 +24,7 @@ public class RecommendationService {

private final RecommendationRepository recommendationRepository;
private final StoreRepository storeRepository;
private final MailService mailService;
private final ApplicationEventPublisher applicationEventPublisher;

@Transactional
public long registerRecommendation(RegisterRecommendationRequest request) {
Expand All @@ -37,7 +39,7 @@ public long registerRecommendation(RegisterRecommendationRequest request) {
"추천인 연락처: " + request.phoneNumber() + "\n" +
"매장 이름: " + store.getName() + "\n" +
"매장 주소: " + store.getAddress() + "\n");
mailService.sendMail(mail);
applicationEventPublisher.publishEvent(new MailSendEvent(mail));

return persistRecommendation.getRecommendationId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.verby.indp.domain.common.notification.mail.Mail;
import com.verby.indp.domain.common.notification.mail.MailService;
import com.verby.indp.domain.common.event.mail.MailSendEvent;
import com.verby.indp.domain.contact.Contact;
import com.verby.indp.domain.contact.dto.request.RegisterContactRequest;
import com.verby.indp.domain.contact.repository.ContactRepository;
Expand All @@ -18,6 +17,7 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ApplicationEventPublisher;

@ExtendWith(MockitoExtension.class)
class ContactServiceTest {
Expand All @@ -29,7 +29,7 @@ class ContactServiceTest {
private ContactRepository contactRepository;

@Mock
private MailService mailService;
private ApplicationEventPublisher applicationEventPublisher;

@Nested
@DisplayName("registerContact 메소드 호출 시")
Expand All @@ -51,7 +51,7 @@ void registerContact() {

// then
verify(contactRepository, times(1)).save(any(Contact.class));
verify(mailService, times(1)).sendMail(any(Mail.class));
verify(applicationEventPublisher, times(1)).publishEvent(any(MailSendEvent.class));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.verby.indp.domain.common.notification.mail.Mail;
import com.verby.indp.domain.common.notification.mail.MailService;
import com.verby.indp.domain.common.event.mail.MailSendEvent;
import com.verby.indp.domain.recommendation.Recommendation;
import com.verby.indp.domain.recommendation.dto.request.RegisterRecommendationRequest;
import com.verby.indp.domain.recommendation.repository.RecommendationRepository;
Expand All @@ -25,6 +24,7 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ApplicationEventPublisher;

@ExtendWith(MockitoExtension.class)
class RecommendationServiceTest {
Expand All @@ -39,7 +39,7 @@ class RecommendationServiceTest {
private StoreRepository storeRepository;

@Mock
private MailService mailService;
private ApplicationEventPublisher applicationEventPublisher;

@Nested
@DisplayName("registerRecommendation 메소드 실행 시")
Expand All @@ -65,7 +65,7 @@ void registerRecommendation() {

// then
verify(recommendationRepository, times(1)).save(any(Recommendation.class));
verify(mailService, times(1)).sendMail(any(Mail.class));
verify(applicationEventPublisher, times(1)).publishEvent(any(MailSendEvent.class));
}

@Test
Expand Down

0 comments on commit 5897274

Please sign in to comment.