diff --git a/src/main/java/coffeemeet/server/admin/service/AdminService.java b/src/main/java/coffeemeet/server/admin/service/AdminService.java index 3b8e8358..e97ebf76 100644 --- a/src/main/java/coffeemeet/server/admin/service/AdminService.java +++ b/src/main/java/coffeemeet/server/admin/service/AdminService.java @@ -5,7 +5,7 @@ import coffeemeet.server.admin.implement.AdminQuery; import coffeemeet.server.certification.implement.CertificationCommand; import coffeemeet.server.certification.implement.CertificationQuery; -import coffeemeet.server.common.implement.FCMNotificationSender; +import coffeemeet.server.common.infrastructure.FCMNotificationSender; import coffeemeet.server.inquiry.domain.Inquiry; import coffeemeet.server.inquiry.implement.InquiryCommand; import coffeemeet.server.inquiry.implement.InquiryQuery; @@ -38,7 +38,7 @@ public void login(String id, String password) { } public void approveCertification(Long certificationId) { - certificationCommand.certificated(certificationId); + certificationCommand.completeCertification(certificationId); Long userId = certificationQuery.getUserIdByCertificationId(certificationId); NotificationInfo notificationInfo = userQuery.getNotificationInfoByUserId(userId); @@ -48,7 +48,7 @@ public void approveCertification(Long certificationId) { } public void rejectCertification(Long certificationId) { - certificationCommand.deleteCertification(certificationId); + certificationCommand.deleteCertificationByUserId(certificationId); Long userId = certificationQuery.getUserIdByCertificationId(certificationId); NotificationInfo notificationInfo = userQuery.getNotificationInfoByUserId(userId); diff --git a/src/main/java/coffeemeet/server/certification/domain/Certification.java b/src/main/java/coffeemeet/server/certification/domain/Certification.java index 7bd4b07e..a7cecd9d 100644 --- a/src/main/java/coffeemeet/server/certification/domain/Certification.java +++ b/src/main/java/coffeemeet/server/certification/domain/Certification.java @@ -34,17 +34,16 @@ public class Certification extends AdvancedBaseEntity { @Column(nullable = false) private String companyName; - @Embedded + @Enumerated(EnumType.STRING) @Column(nullable = false) + private Department department; + + @Embedded private CompanyEmail companyEmail; @Column(nullable = false) private String businessCardUrl; - @Enumerated(EnumType.STRING) - @Column(nullable = false) - private Department department; - @Column(nullable = false) private boolean isCertificated; @@ -64,4 +63,12 @@ public void qualify() { isCertificated = true; } + public void update(String companyName, CompanyEmail companyEmail, String businessCardUrl, + Department department) { + this.companyName = companyName; + this.companyEmail = companyEmail; + this.businessCardUrl = businessCardUrl; + this.department = department; + } + } diff --git a/src/main/java/coffeemeet/server/certification/domain/EmailVerification.java b/src/main/java/coffeemeet/server/certification/domain/VerificationInfo.java similarity index 60% rename from src/main/java/coffeemeet/server/certification/domain/EmailVerification.java rename to src/main/java/coffeemeet/server/certification/domain/VerificationInfo.java index 25e6350f..0175de56 100644 --- a/src/main/java/coffeemeet/server/certification/domain/EmailVerification.java +++ b/src/main/java/coffeemeet/server/certification/domain/VerificationInfo.java @@ -7,20 +7,20 @@ import org.springframework.data.redis.core.RedisHash; @Getter -@RedisHash(value = "email_verification", timeToLive = 360) -public class EmailVerification { +@RedisHash(value = "email_verification", timeToLive = 300) +public class VerificationInfo { @Id private Long userId; private CompanyEmail companyEmail; - private String code; + private String verificationCode; private LocalDateTime createdAt; - public EmailVerification(@NonNull Long userId, @NonNull CompanyEmail companyEmail, - @NonNull String code) { + public VerificationInfo(@NonNull Long userId, @NonNull CompanyEmail companyEmail, + @NonNull String verificationCode) { this.userId = userId; this.companyEmail = companyEmail; - this.code = code; + this.verificationCode = verificationCode; this.createdAt = LocalDateTime.now(); } diff --git a/src/main/java/coffeemeet/server/certification/infrastructure/CertificationRepository.java b/src/main/java/coffeemeet/server/certification/domain/repository/CertificationRepository.java similarity index 88% rename from src/main/java/coffeemeet/server/certification/infrastructure/CertificationRepository.java rename to src/main/java/coffeemeet/server/certification/domain/repository/CertificationRepository.java index fc66d649..25fba069 100644 --- a/src/main/java/coffeemeet/server/certification/infrastructure/CertificationRepository.java +++ b/src/main/java/coffeemeet/server/certification/domain/repository/CertificationRepository.java @@ -1,4 +1,4 @@ -package coffeemeet.server.certification.infrastructure; +package coffeemeet.server.certification.domain.repository; import coffeemeet.server.certification.domain.Certification; import coffeemeet.server.certification.domain.CompanyEmail; @@ -14,6 +14,8 @@ public interface CertificationRepository extends JpaRepository findPendingCertifications(Pageable pageable); diff --git a/src/main/java/coffeemeet/server/certification/domain/repository/VerificationInfoRepository.java b/src/main/java/coffeemeet/server/certification/domain/repository/VerificationInfoRepository.java new file mode 100644 index 00000000..88715223 --- /dev/null +++ b/src/main/java/coffeemeet/server/certification/domain/repository/VerificationInfoRepository.java @@ -0,0 +1,8 @@ +package coffeemeet.server.certification.domain.repository; + +import coffeemeet.server.certification.domain.VerificationInfo; +import org.springframework.data.repository.CrudRepository; + +public interface VerificationInfoRepository extends CrudRepository { + +} diff --git a/src/main/java/coffeemeet/server/certification/implement/BusinessCardImageDeleter.java b/src/main/java/coffeemeet/server/certification/implement/BusinessCardImageDeleter.java new file mode 100644 index 00000000..95b727d0 --- /dev/null +++ b/src/main/java/coffeemeet/server/certification/implement/BusinessCardImageDeleter.java @@ -0,0 +1,22 @@ +package coffeemeet.server.certification.implement; + +import static coffeemeet.server.common.domain.S3KeyPrefix.BUSINESS_CARD; + +import coffeemeet.server.certification.domain.Certification; +import coffeemeet.server.common.implement.ImageDeleter; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class BusinessCardImageDeleter { + + private final CertificationQuery certificationQuery; + private final ImageDeleter imageDeleter; + + public void deleteBusinessCardImageByUserId(Long userId) { + Certification certification = certificationQuery.getCertificationByUserId(userId); + imageDeleter.deleteImage(certification.getBusinessCardUrl(), BUSINESS_CARD); + } + +} diff --git a/src/main/java/coffeemeet/server/certification/implement/BusinessCardImageUploader.java b/src/main/java/coffeemeet/server/certification/implement/BusinessCardImageUploader.java new file mode 100644 index 00000000..fcd9bffc --- /dev/null +++ b/src/main/java/coffeemeet/server/certification/implement/BusinessCardImageUploader.java @@ -0,0 +1,20 @@ +package coffeemeet.server.certification.implement; + +import static coffeemeet.server.common.domain.S3KeyPrefix.BUSINESS_CARD; + +import coffeemeet.server.common.implement.ImageUploader; +import java.io.File; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class BusinessCardImageUploader { + + private final ImageUploader imageUploader; + + public String uploadBusinessCardImage(File businessCardImage) { + return imageUploader.uploadImage(businessCardImage, BUSINESS_CARD); + } + +} diff --git a/src/main/java/coffeemeet/server/certification/implement/CertificationCommand.java b/src/main/java/coffeemeet/server/certification/implement/CertificationCommand.java index 4b948a76..639cddfb 100644 --- a/src/main/java/coffeemeet/server/certification/implement/CertificationCommand.java +++ b/src/main/java/coffeemeet/server/certification/implement/CertificationCommand.java @@ -1,14 +1,11 @@ package coffeemeet.server.certification.implement; -import static coffeemeet.server.certification.exception.CertificationErrorCode.EXISTED_COMPANY_EMAIL; - import coffeemeet.server.certification.domain.Certification; import coffeemeet.server.certification.domain.CompanyEmail; import coffeemeet.server.certification.domain.Department; -import coffeemeet.server.certification.infrastructure.CertificationRepository; -import coffeemeet.server.common.execption.InvalidInputException; +import coffeemeet.server.certification.domain.repository.CertificationRepository; import coffeemeet.server.user.domain.User; -import java.util.function.Consumer; +import coffeemeet.server.user.implement.UserQuery; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; @@ -18,13 +15,13 @@ @RequiredArgsConstructor public class CertificationCommand { - private static final String EXISTED_COMPANY_EMAIL_MESSAGE = "이미 사용 중인 회사 이메일(%s) 입니다."; - private final CertificationRepository certificationRepository; private final CertificationQuery certificationQuery; + private final UserQuery userQuery; - public void createCertification(User user, String companyName, CompanyEmail companyEmail, + public void createCertification(Long userId, String companyName, CompanyEmail companyEmail, Department department, String businessCardUrl) { + User user = userQuery.getUserById(userId); certificationRepository.save( Certification.builder() .companyName(companyName) @@ -36,25 +33,19 @@ public void createCertification(User user, String companyName, CompanyEmail comp ); } - public void hasDuplicatedCompanyEmail(CompanyEmail companyEmail) { - if (certificationRepository.existsByCompanyEmail(companyEmail)) { - throw new InvalidInputException(EXISTED_COMPANY_EMAIL, - String.format(EXISTED_COMPANY_EMAIL_MESSAGE, companyEmail.getValue())); - } + public void updateCertification(Long userId, String companyName, CompanyEmail companyEmail, + Department department, String businessCardImageUrl) { + Certification certification = certificationQuery.getCertificationByUserId(userId); + certification.update(companyName, companyEmail, businessCardImageUrl, department); } - public void certificated(Long userId) { + public void completeCertification(Long userId) { Certification certification = certificationQuery.getCertificationByUserId(userId); certification.qualify(); } - @Transactional(readOnly = true) - public void applyIfCertifiedUser(Long userId, Consumer consumer) { - certificationRepository.findByUserId(userId).ifPresent(consumer); - } - - public void deleteCertification(Long userId) { - certificationRepository.deleteById(userId); + public void deleteCertificationByUserId(Long userId) { + certificationRepository.deleteByUserId(userId); } } diff --git a/src/main/java/coffeemeet/server/certification/implement/CertificationQuery.java b/src/main/java/coffeemeet/server/certification/implement/CertificationQuery.java index 91f1bdb7..16f1ee78 100644 --- a/src/main/java/coffeemeet/server/certification/implement/CertificationQuery.java +++ b/src/main/java/coffeemeet/server/certification/implement/CertificationQuery.java @@ -3,7 +3,8 @@ import static coffeemeet.server.certification.exception.CertificationErrorCode.CERTIFICATION_NOT_FOUND; import coffeemeet.server.certification.domain.Certification; -import coffeemeet.server.certification.infrastructure.CertificationRepository; +import coffeemeet.server.certification.domain.CompanyEmail; +import coffeemeet.server.certification.domain.repository.CertificationRepository; import coffeemeet.server.common.execption.InvalidInputException; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; @@ -33,6 +34,10 @@ public String getCompanyNameByUserId(Long userId) { ).getCompanyName(); } + public boolean isExistedCompanyEmail(CompanyEmail companyEmail) { + return certificationRepository.existsByCompanyEmail(companyEmail); + } + public Long getUserIdByCertificationId(Long certificationId) { return certificationId; } diff --git a/src/main/java/coffeemeet/server/certification/implement/CompanyEmailValidator.java b/src/main/java/coffeemeet/server/certification/implement/CompanyEmailValidator.java new file mode 100644 index 00000000..d0a353dd --- /dev/null +++ b/src/main/java/coffeemeet/server/certification/implement/CompanyEmailValidator.java @@ -0,0 +1,25 @@ +package coffeemeet.server.certification.implement; + +import static coffeemeet.server.certification.exception.CertificationErrorCode.EXISTED_COMPANY_EMAIL; + +import coffeemeet.server.certification.domain.CompanyEmail; +import coffeemeet.server.common.execption.InvalidInputException; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class CompanyEmailValidator { + + private static final String EXISTED_COMPANY_EMAIL_MESSAGE = "이미 사용 중인 회사 이메일(%s) 입니다."; + + private final CertificationQuery certificationQuery; + + public void validateDuplicatedCompanyEmail(CompanyEmail companyEmail) { + if (certificationQuery.isExistedCompanyEmail(companyEmail)) { + throw new InvalidInputException(EXISTED_COMPANY_EMAIL, + String.format(EXISTED_COMPANY_EMAIL_MESSAGE, companyEmail.getValue())); + } + } + +} diff --git a/src/main/java/coffeemeet/server/certification/implement/EmailVerificationCommand.java b/src/main/java/coffeemeet/server/certification/implement/EmailVerificationCommand.java deleted file mode 100644 index 1b0361f1..00000000 --- a/src/main/java/coffeemeet/server/certification/implement/EmailVerificationCommand.java +++ /dev/null @@ -1,20 +0,0 @@ -package coffeemeet.server.certification.implement; - -import coffeemeet.server.certification.domain.CompanyEmail; -import coffeemeet.server.certification.domain.EmailVerification; -import coffeemeet.server.certification.infrastructure.EmailVerificationRepository; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Component; - -@Component -@RequiredArgsConstructor -public class EmailVerificationCommand { - - private final EmailVerificationRepository emailVerificationRepository; - - public void createEmailVerification(Long userId, CompanyEmail companyEmail, - String verificationCode) { - emailVerificationRepository.save(new EmailVerification(userId, companyEmail, verificationCode)); - } - -} diff --git a/src/main/java/coffeemeet/server/certification/implement/VerificationCodeGenerator.java b/src/main/java/coffeemeet/server/certification/implement/VerificationCodeGenerator.java new file mode 100644 index 00000000..596f58e6 --- /dev/null +++ b/src/main/java/coffeemeet/server/certification/implement/VerificationCodeGenerator.java @@ -0,0 +1,15 @@ +package coffeemeet.server.certification.implement; + +import java.util.random.RandomGenerator; +import org.springframework.stereotype.Component; + +@Component +public class VerificationCodeGenerator { + + private static final RandomGenerator RANDOM_GENERATOR = RandomGenerator.getDefault(); + + public String generateVerificationCode() { + return String.format("%06d", RANDOM_GENERATOR.nextInt(1000000)); + } + +} diff --git a/src/main/java/coffeemeet/server/certification/implement/VerificationCodeValidator.java b/src/main/java/coffeemeet/server/certification/implement/VerificationCodeValidator.java new file mode 100644 index 00000000..bfefeca0 --- /dev/null +++ b/src/main/java/coffeemeet/server/certification/implement/VerificationCodeValidator.java @@ -0,0 +1,20 @@ +package coffeemeet.server.certification.implement; + +import static coffeemeet.server.certification.exception.CertificationErrorCode.INVALID_VERIFICATION_CODE; + +import coffeemeet.server.common.execption.InvalidInputException; +import org.springframework.stereotype.Component; + +@Component +public class VerificationCodeValidator { + + private static final String WRONG_VERIFICATION_CODE_MESSAGE = "잘못된 인증코드(%s)를 입력했습니다."; + + public void validateVerificationCode(String verificationCode, String userInputCode) { + if (!userInputCode.equals(verificationCode)) { + throw new InvalidInputException(INVALID_VERIFICATION_CODE, + String.format(WRONG_VERIFICATION_CODE_MESSAGE, verificationCode)); + } + } + +} diff --git a/src/main/java/coffeemeet/server/certification/implement/VerificationInfoCommand.java b/src/main/java/coffeemeet/server/certification/implement/VerificationInfoCommand.java new file mode 100644 index 00000000..bfd98091 --- /dev/null +++ b/src/main/java/coffeemeet/server/certification/implement/VerificationInfoCommand.java @@ -0,0 +1,20 @@ +package coffeemeet.server.certification.implement; + +import coffeemeet.server.certification.domain.CompanyEmail; +import coffeemeet.server.certification.domain.VerificationInfo; +import coffeemeet.server.certification.domain.repository.VerificationInfoRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class VerificationInfoCommand { + + private final VerificationInfoRepository verificationInfoRepository; + + public void createVerificationInfo(Long userId, CompanyEmail companyEmail, + String verificationCode) { + verificationInfoRepository.save(new VerificationInfo(userId, companyEmail, verificationCode)); + } + +} diff --git a/src/main/java/coffeemeet/server/certification/implement/EmailVerificationQuery.java b/src/main/java/coffeemeet/server/certification/implement/VerificationInfoQuery.java similarity index 61% rename from src/main/java/coffeemeet/server/certification/implement/EmailVerificationQuery.java rename to src/main/java/coffeemeet/server/certification/implement/VerificationInfoQuery.java index e168395a..9ace1a63 100644 --- a/src/main/java/coffeemeet/server/certification/implement/EmailVerificationQuery.java +++ b/src/main/java/coffeemeet/server/certification/implement/VerificationInfoQuery.java @@ -2,26 +2,26 @@ import static coffeemeet.server.certification.exception.CertificationErrorCode.VERIFICATION_CODE_NOT_FOUND; -import coffeemeet.server.certification.domain.EmailVerification; -import coffeemeet.server.certification.infrastructure.EmailVerificationRepository; +import coffeemeet.server.certification.domain.VerificationInfo; +import coffeemeet.server.certification.domain.repository.VerificationInfoRepository; import coffeemeet.server.common.execption.InvalidInputException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; @Component @RequiredArgsConstructor -public class EmailVerificationQuery { +public class VerificationInfoQuery { private static final String VERIFICATION_CODE_NOT_FOUND_MESSAGE = "인증코드 기간이 만료되었거나 해당 유저(%s)가 인증코드를 요청한 기록이 없습니다."; - private final EmailVerificationRepository emailVerificationRepository; + private final VerificationInfoRepository verificationInfoRepository; - public String getCodeById(Long userId) { - EmailVerification emailVerification = emailVerificationRepository.findById(userId) + public String getVerificationCodeById(Long userId) { + VerificationInfo verificationInfo = verificationInfoRepository.findById(userId) .orElseThrow(() -> new InvalidInputException( VERIFICATION_CODE_NOT_FOUND, String.format(VERIFICATION_CODE_NOT_FOUND_MESSAGE, userId))); - return emailVerification.getCode(); + return verificationInfo.getVerificationCode(); } } diff --git a/src/main/java/coffeemeet/server/certification/implement/VerificationMailSender.java b/src/main/java/coffeemeet/server/certification/implement/VerificationMailSender.java new file mode 100644 index 00000000..a0e278fb --- /dev/null +++ b/src/main/java/coffeemeet/server/certification/implement/VerificationMailSender.java @@ -0,0 +1,22 @@ +package coffeemeet.server.certification.implement; + +import coffeemeet.server.certification.domain.CompanyEmail; +import coffeemeet.server.common.infrastructure.EmailSender; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class VerificationMailSender { + + private static final String VERIFICATION_MAIL_SUBJECT = "[coffee-meet] 커피밋 사용을 위해 이메일 인증을 완료해주세요"; + private static final String VERIFICATION_MAIL_BODY = "인증코드: %s"; + + private final EmailSender emailSender; + + public void sendVerificationMail(CompanyEmail companyEmail, String verificationCode) { + emailSender.sendEmail(companyEmail.getValue(), VERIFICATION_MAIL_SUBJECT, + String.format(VERIFICATION_MAIL_BODY, verificationCode)); + } + +} diff --git a/src/main/java/coffeemeet/server/certification/infrastructure/EmailVerificationRepository.java b/src/main/java/coffeemeet/server/certification/infrastructure/EmailVerificationRepository.java deleted file mode 100644 index b236bf43..00000000 --- a/src/main/java/coffeemeet/server/certification/infrastructure/EmailVerificationRepository.java +++ /dev/null @@ -1,8 +0,0 @@ -package coffeemeet.server.certification.infrastructure; - -import coffeemeet.server.certification.domain.EmailVerification; -import org.springframework.data.repository.CrudRepository; - -public interface EmailVerificationRepository extends CrudRepository { - -} diff --git a/src/main/java/coffeemeet/server/certification/service/CertificationService.java b/src/main/java/coffeemeet/server/certification/service/CertificationService.java index 19f34f80..12fe9612 100644 --- a/src/main/java/coffeemeet/server/certification/service/CertificationService.java +++ b/src/main/java/coffeemeet/server/certification/service/CertificationService.java @@ -1,25 +1,20 @@ package coffeemeet.server.certification.service; -import static coffeemeet.server.certification.exception.CertificationErrorCode.INVALID_VERIFICATION_CODE; -import static coffeemeet.server.common.domain.KeyType.BUSINESS_CARD; - import coffeemeet.server.certification.domain.Certification; import coffeemeet.server.certification.domain.CompanyEmail; import coffeemeet.server.certification.domain.Department; +import coffeemeet.server.certification.implement.BusinessCardImageDeleter; +import coffeemeet.server.certification.implement.BusinessCardImageUploader; import coffeemeet.server.certification.implement.CertificationCommand; import coffeemeet.server.certification.implement.CertificationQuery; -import coffeemeet.server.certification.implement.EmailVerificationCommand; -import coffeemeet.server.certification.implement.EmailVerificationQuery; -import coffeemeet.server.certification.service.dto.PendingCertification; +import coffeemeet.server.certification.implement.CompanyEmailValidator; +import coffeemeet.server.certification.implement.VerificationCodeGenerator; +import coffeemeet.server.certification.implement.VerificationCodeValidator; +import coffeemeet.server.certification.implement.VerificationInfoCommand; +import coffeemeet.server.certification.implement.VerificationInfoQuery; +import coffeemeet.server.certification.implement.VerificationMailSender; import coffeemeet.server.certification.service.dto.PendingCertificationPageDto; -import coffeemeet.server.common.execption.InvalidInputException; -import coffeemeet.server.common.implement.EmailSender; -import coffeemeet.server.common.implement.MediaManager; -import coffeemeet.server.common.util.FileUtils; -import coffeemeet.server.user.domain.User; -import coffeemeet.server.user.implement.UserQuery; import java.io.File; -import java.util.random.RandomGenerator; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -29,81 +24,61 @@ @RequiredArgsConstructor public class CertificationService { - private static final String WRONG_VERIFICATION_CODE_MESSAGE = "사용자(%s)가 잘못된 인증코드(%s)를 입력했습니다."; - private static final RandomGenerator RANDOM_GENERATOR = RandomGenerator.getDefault(); - - private final MediaManager mediaManager; - private final EmailSender emailSender; - private final UserQuery userQuery; + private final BusinessCardImageUploader businessCardImageUploader; + private final BusinessCardImageDeleter businessCardImageDeleter; private final CertificationCommand certificationCommand; private final CertificationQuery certificationQuery; - private final EmailVerificationCommand emailVerificationCommand; - private final EmailVerificationQuery emailVerificationQuery; + private final CompanyEmailValidator companyEmailValidator; + private final VerificationCodeGenerator verificationCodeGenerator; + private final VerificationCodeValidator verificationCodeValidator; + private final VerificationInfoQuery verificationInfoQuery; + private final VerificationInfoCommand verificationInfoCommand; + private final VerificationMailSender verificationMailSender; public void registerCertification(Long userId, String companyName, String email, String departmentName, File businessCardImage) { - processCertification(userId, companyName, email, departmentName, businessCardImage, false); + CompanyEmail companyEmail = new CompanyEmail(email); + Department department = Department.valueOf(departmentName); + + String businessCardImageUrl = businessCardImageUploader.uploadBusinessCardImage( + businessCardImage); + + certificationCommand.createCertification(userId, companyName, companyEmail, department, + businessCardImageUrl); } public void updateCertification(Long userId, String companyName, String email, String departmentName, File businessCardImage) { - processCertification(userId, companyName, email, departmentName, businessCardImage, true); - } - - private void processCertification(Long userId, String companyName, String email, - String departmentName, File businessCardImage, boolean isUpdate) { - String key = mediaManager.generateKey(BUSINESS_CARD); - uploadBusinessCard(userId, key, businessCardImage); - CompanyEmail companyEmail = new CompanyEmail(email); - String businessCardUrl = mediaManager.getUrl(key); Department department = Department.valueOf(departmentName); - User user = userQuery.getUserById(userId); - if (isUpdate) { - certificationCommand.deleteCertification(userId); - } - certificationCommand.createCertification(user, companyName, companyEmail, department, - businessCardUrl); - } - - private void uploadBusinessCard(Long userId, String key, File businessCardUrl) { - certificationCommand.applyIfCertifiedUser(userId, certification -> { - String oldKey = mediaManager.extractKey(certification.getBusinessCardUrl(), BUSINESS_CARD); - mediaManager.delete(oldKey); - }); + businessCardImageDeleter.deleteBusinessCardImageByUserId(userId); + String businessCardImageUrl = businessCardImageUploader.uploadBusinessCardImage( + businessCardImage); - mediaManager.upload(key, businessCardUrl); - FileUtils.delete(businessCardUrl); + certificationCommand.updateCertification(userId, companyName, companyEmail, department, + businessCardImageUrl); } public void sendVerificationMail(Long userId, String email) { CompanyEmail companyEmail = new CompanyEmail(email); - certificationCommand.hasDuplicatedCompanyEmail(companyEmail); + companyEmailValidator.validateDuplicatedCompanyEmail(companyEmail); - String verificationCode = generateVerificationCode(); - emailSender.sendVerificationCode(companyEmail, verificationCode); - emailVerificationCommand.createEmailVerification(userId, companyEmail, verificationCode); - } + String verificationCode = verificationCodeGenerator.generateVerificationCode(); + verificationMailSender.sendVerificationMail(companyEmail, verificationCode); - private String generateVerificationCode() { - return String.format("%06d", RANDOM_GENERATOR.nextInt(1000000)); + verificationInfoCommand.createVerificationInfo(userId, companyEmail, verificationCode); } - public void compareCode(Long userId, String verificationCode) { - String correctCode = emailVerificationQuery.getCodeById(userId); - if (!correctCode.equals(verificationCode)) { - throw new InvalidInputException(INVALID_VERIFICATION_CODE, - String.format(WRONG_VERIFICATION_CODE_MESSAGE, userId, verificationCode)); - } + public void compareCode(Long userId, String userInputCode) { + String verificationCode = verificationInfoQuery.getVerificationCodeById(userId); + verificationCodeValidator.validateVerificationCode(verificationCode, userInputCode); } public PendingCertificationPageDto getUncertifiedUserRequests(Pageable pageable) { - Page pendingCertification = - certificationQuery.getPendingCertification(pageable); - Page pendingCertificationPage = pendingCertification.map( - PendingCertification::from); - return PendingCertificationPageDto.from(pendingCertificationPage); + Page pendingCertifications = certificationQuery.getPendingCertification( + pageable); + return PendingCertificationPageDto.from(pendingCertifications); } } diff --git a/src/main/java/coffeemeet/server/certification/service/dto/PendingCertificationPageDto.java b/src/main/java/coffeemeet/server/certification/service/dto/PendingCertificationPageDto.java index 88c1c29f..00062fab 100644 --- a/src/main/java/coffeemeet/server/certification/service/dto/PendingCertificationPageDto.java +++ b/src/main/java/coffeemeet/server/certification/service/dto/PendingCertificationPageDto.java @@ -1,13 +1,16 @@ package coffeemeet.server.certification.service.dto; +import coffeemeet.server.certification.domain.Certification; import org.springframework.data.domain.Page; public record PendingCertificationPageDto( Page page ) { - public static PendingCertificationPageDto from(Page page) { - return new PendingCertificationPageDto(page); + public static PendingCertificationPageDto from( + Page certificationPage + ) { + return new PendingCertificationPageDto(certificationPage.map(PendingCertification::from)); } } diff --git a/src/main/java/coffeemeet/server/chatting/current/service/ChattingMessageService.java b/src/main/java/coffeemeet/server/chatting/current/service/ChattingMessageService.java index 4464815d..68282c42 100644 --- a/src/main/java/coffeemeet/server/chatting/current/service/ChattingMessageService.java +++ b/src/main/java/coffeemeet/server/chatting/current/service/ChattingMessageService.java @@ -7,7 +7,7 @@ import coffeemeet.server.chatting.current.implement.ChattingSessionCommand; import coffeemeet.server.chatting.current.implement.ChattingSessionQuery; import coffeemeet.server.chatting.current.service.dto.ChattingDto; -import coffeemeet.server.common.implement.FCMNotificationSender; +import coffeemeet.server.common.infrastructure.FCMNotificationSender; import coffeemeet.server.user.domain.NotificationInfo; import coffeemeet.server.user.domain.User; import coffeemeet.server.user.domain.UserStatus; diff --git a/src/main/java/coffeemeet/server/chatting/current/service/ChattingRoomService.java b/src/main/java/coffeemeet/server/chatting/current/service/ChattingRoomService.java index ce6dc56f..294ce091 100644 --- a/src/main/java/coffeemeet/server/chatting/current/service/ChattingRoomService.java +++ b/src/main/java/coffeemeet/server/chatting/current/service/ChattingRoomService.java @@ -14,7 +14,7 @@ import coffeemeet.server.chatting.history.implement.ChattingMessageHistoryCommand; import coffeemeet.server.chatting.history.implement.ChattingRoomHistoryCommand; import coffeemeet.server.chatting.history.implement.UserChattingHistoryCommand; -import coffeemeet.server.common.implement.FCMNotificationSender; +import coffeemeet.server.common.infrastructure.FCMNotificationSender; import coffeemeet.server.user.domain.NotificationInfo; import coffeemeet.server.user.domain.User; import coffeemeet.server.user.implement.UserQuery; diff --git a/src/main/java/coffeemeet/server/common/domain/ObjectStorage.java b/src/main/java/coffeemeet/server/common/domain/ObjectStorage.java new file mode 100644 index 00000000..98b3681c --- /dev/null +++ b/src/main/java/coffeemeet/server/common/domain/ObjectStorage.java @@ -0,0 +1,17 @@ +package coffeemeet.server.common.domain; + +import java.io.File; + +public interface ObjectStorage { + + void upload(String key, File file); + + void delete(String key); + + String getUrl(String key); + + String generateKey(S3KeyPrefix s3KeyPrefix); + + String extractKey(String url, S3KeyPrefix s3KeyPrefix); + +} diff --git a/src/main/java/coffeemeet/server/common/domain/KeyType.java b/src/main/java/coffeemeet/server/common/domain/S3KeyPrefix.java similarity index 78% rename from src/main/java/coffeemeet/server/common/domain/KeyType.java rename to src/main/java/coffeemeet/server/common/domain/S3KeyPrefix.java index 686e0514..a1c8dfc2 100644 --- a/src/main/java/coffeemeet/server/common/domain/KeyType.java +++ b/src/main/java/coffeemeet/server/common/domain/S3KeyPrefix.java @@ -3,7 +3,7 @@ import lombok.Getter; @Getter -public enum KeyType { +public enum S3KeyPrefix { BUSINESS_CARD("business-card"), PROFILE_IMAGE("profile-image"), @@ -11,7 +11,7 @@ public enum KeyType { private final String value; - KeyType(String value) { + S3KeyPrefix(String value) { this.value = value; } diff --git a/src/main/java/coffeemeet/server/common/implement/EmailSender.java b/src/main/java/coffeemeet/server/common/implement/EmailSender.java deleted file mode 100644 index 68286c53..00000000 --- a/src/main/java/coffeemeet/server/common/implement/EmailSender.java +++ /dev/null @@ -1,37 +0,0 @@ -package coffeemeet.server.common.implement; - -import coffeemeet.server.certification.domain.CompanyEmail; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.mail.SimpleMailMessage; -import org.springframework.mail.javamail.JavaMailSender; -import org.springframework.stereotype.Component; - -@Component -public class EmailSender { - - private static final String VERIFICATION_CODE = "인증코드: %s"; - private static final String SUBJECT_MESSAGE = "[coffee-meet] 커피밋 사용을 위해 이메일 인증을 완료해주세요"; - - private final JavaMailSender javaMailSender; - private final String sender; - - public EmailSender(JavaMailSender javaMailSender, - @Value("${spring.mail.username}") String sender) { - this.javaMailSender = javaMailSender; - this.sender = sender; - } - - public void sendVerificationCode(CompanyEmail companyMail, String verificationCode) { - SimpleMailMessage mailMessage = new SimpleMailMessage(); - mailMessage.setFrom(sender); - mailMessage.setTo(companyMail.getValue()); - - mailMessage.setSubject(SUBJECT_MESSAGE); - - String text = String.format(VERIFICATION_CODE, verificationCode); - mailMessage.setText(text); - - javaMailSender.send(mailMessage); - } - -} diff --git a/src/main/java/coffeemeet/server/common/implement/ImageDeleter.java b/src/main/java/coffeemeet/server/common/implement/ImageDeleter.java new file mode 100644 index 00000000..2f8152a4 --- /dev/null +++ b/src/main/java/coffeemeet/server/common/implement/ImageDeleter.java @@ -0,0 +1,19 @@ +package coffeemeet.server.common.implement; + +import coffeemeet.server.common.domain.ObjectStorage; +import coffeemeet.server.common.domain.S3KeyPrefix; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class ImageDeleter { + + private final ObjectStorage objectStorage; + + public void deleteImage(String imageUrl, S3KeyPrefix s3KeyPrefix) { + String key = objectStorage.extractKey(imageUrl, s3KeyPrefix); + objectStorage.delete(key); + } + +} diff --git a/src/main/java/coffeemeet/server/common/implement/ImageUploader.java b/src/main/java/coffeemeet/server/common/implement/ImageUploader.java new file mode 100644 index 00000000..ad082a16 --- /dev/null +++ b/src/main/java/coffeemeet/server/common/implement/ImageUploader.java @@ -0,0 +1,23 @@ +package coffeemeet.server.common.implement; + +import coffeemeet.server.common.domain.ObjectStorage; +import coffeemeet.server.common.domain.S3KeyPrefix; +import coffeemeet.server.common.util.FileUtils; +import java.io.File; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class ImageUploader { + + private final ObjectStorage objectStorage; + + public String uploadImage(File image, S3KeyPrefix s3KeyPrefix) { + String key = objectStorage.generateKey(s3KeyPrefix); + objectStorage.upload(key, image); + FileUtils.deleteTempFile(image); + return objectStorage.getUrl(key); + } + +} diff --git a/src/main/java/coffeemeet/server/common/implement/MediaManager.java b/src/main/java/coffeemeet/server/common/implement/MediaManager.java deleted file mode 100644 index e189139a..00000000 --- a/src/main/java/coffeemeet/server/common/implement/MediaManager.java +++ /dev/null @@ -1,18 +0,0 @@ -package coffeemeet.server.common.implement; - -import coffeemeet.server.common.domain.KeyType; -import java.io.File; - -public interface MediaManager { - - void upload(String key, File file); - - void delete(String key); - - String getUrl(String key); - - String generateKey(KeyType keyType); - - String extractKey(String s3Url, KeyType keyType); - -} diff --git a/src/main/java/coffeemeet/server/common/infrastructure/EmailSender.java b/src/main/java/coffeemeet/server/common/infrastructure/EmailSender.java new file mode 100644 index 00000000..0aad34de --- /dev/null +++ b/src/main/java/coffeemeet/server/common/infrastructure/EmailSender.java @@ -0,0 +1,32 @@ +package coffeemeet.server.common.infrastructure; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.stereotype.Component; + +@Component +public class EmailSender { + + private final JavaMailSender javaMailSender; + private final String sender; + + public EmailSender(JavaMailSender javaMailSender, + @Value("${spring.mail.username}") String sender) { + this.javaMailSender = javaMailSender; + this.sender = sender; + } + + public void sendEmail(String email, String subject, String body) { + SimpleMailMessage mailMessage = new SimpleMailMessage(); + + mailMessage.setFrom(sender); + mailMessage.setTo(email); + + mailMessage.setSubject(subject); + mailMessage.setText(body); + + javaMailSender.send(mailMessage); // TODO: 2023/12/19 에러 핸들링 및 비동기처리 + } + +} diff --git a/src/main/java/coffeemeet/server/common/implement/FCMNotificationSender.java b/src/main/java/coffeemeet/server/common/infrastructure/FCMNotificationSender.java similarity index 98% rename from src/main/java/coffeemeet/server/common/implement/FCMNotificationSender.java rename to src/main/java/coffeemeet/server/common/infrastructure/FCMNotificationSender.java index caba71b6..e28f70ac 100644 --- a/src/main/java/coffeemeet/server/common/implement/FCMNotificationSender.java +++ b/src/main/java/coffeemeet/server/common/infrastructure/FCMNotificationSender.java @@ -1,4 +1,4 @@ -package coffeemeet.server.common.implement; +package coffeemeet.server.common.infrastructure; import static coffeemeet.server.common.execption.GlobalErrorCode.INVALID_FCM_TOKEN; import static coffeemeet.server.common.execption.GlobalErrorCode.PUSH_NOTIFICATION_SEND_FAILURE; diff --git a/src/main/java/coffeemeet/server/common/implement/S3MediaManager.java b/src/main/java/coffeemeet/server/common/infrastructure/S3ObjectStorage.java similarity index 72% rename from src/main/java/coffeemeet/server/common/implement/S3MediaManager.java rename to src/main/java/coffeemeet/server/common/infrastructure/S3ObjectStorage.java index 9bf8ecba..2fd98edd 100644 --- a/src/main/java/coffeemeet/server/common/implement/S3MediaManager.java +++ b/src/main/java/coffeemeet/server/common/infrastructure/S3ObjectStorage.java @@ -1,6 +1,7 @@ -package coffeemeet.server.common.implement; +package coffeemeet.server.common.infrastructure; -import coffeemeet.server.common.domain.KeyType; +import coffeemeet.server.common.domain.ObjectStorage; +import coffeemeet.server.common.domain.S3KeyPrefix; import com.amazonaws.AmazonServiceException; import com.amazonaws.services.s3.AmazonS3; import java.io.File; @@ -12,12 +13,12 @@ @Slf4j @Component -public class S3MediaManager implements MediaManager { +public class S3ObjectStorage implements ObjectStorage { private final AmazonS3 amazonS3; private final String bucketName; - public S3MediaManager( + public S3ObjectStorage( AmazonS3 amazonS3, @Value("${cloud.aws.s3.bucket}") String bucketName ) { @@ -49,14 +50,14 @@ public String getUrl(String key) { } @Override - public String generateKey(KeyType keyType) { - return String.format("%s-%s-%s", keyType.getValue(), LocalDateTime.now(), + public String generateKey(S3KeyPrefix s3KeyPrefix) { + return String.format("%s-%s-%s", s3KeyPrefix.getValue(), LocalDateTime.now(), UUID.randomUUID()); } @Override - public String extractKey(String s3Url, KeyType keyType) { - int startIndex = s3Url.indexOf(keyType.getValue()); + public String extractKey(String s3Url, S3KeyPrefix s3KeyPrefix) { + int startIndex = s3Url.indexOf(s3KeyPrefix.getValue()); if (startIndex == -1) { return ""; } diff --git a/src/main/java/coffeemeet/server/common/util/FileUtils.java b/src/main/java/coffeemeet/server/common/util/FileUtils.java index c0753eea..1dba9324 100644 --- a/src/main/java/coffeemeet/server/common/util/FileUtils.java +++ b/src/main/java/coffeemeet/server/common/util/FileUtils.java @@ -23,7 +23,7 @@ public static File convertMultipartFileToFile(MultipartFile multipartFile) { } } - public static void delete(File file) { + public static void deleteTempFile(File file) { try { Files.delete(file.toPath()); } catch (IOException e) { diff --git a/src/main/java/coffeemeet/server/matching/service/MatchingService.java b/src/main/java/coffeemeet/server/matching/service/MatchingService.java index 725e62e1..f2548877 100644 --- a/src/main/java/coffeemeet/server/matching/service/MatchingService.java +++ b/src/main/java/coffeemeet/server/matching/service/MatchingService.java @@ -8,7 +8,7 @@ import coffeemeet.server.chatting.current.domain.ChattingRoom; import coffeemeet.server.chatting.current.implement.ChattingRoomCommand; import coffeemeet.server.common.execption.BadRequestException; -import coffeemeet.server.common.implement.FCMNotificationSender; +import coffeemeet.server.common.infrastructure.FCMNotificationSender; import coffeemeet.server.matching.implement.MatchingQueueCommand; import coffeemeet.server.matching.implement.MatchingQueueQuery; import coffeemeet.server.user.domain.NotificationInfo; diff --git a/src/main/java/coffeemeet/server/report/service/dto/ReportSummary.java b/src/main/java/coffeemeet/server/report/service/dto/ReportSummary.java index 9b1b8f86..a9978c70 100644 --- a/src/main/java/coffeemeet/server/report/service/dto/ReportSummary.java +++ b/src/main/java/coffeemeet/server/report/service/dto/ReportSummary.java @@ -14,14 +14,14 @@ public record ReportSummary( LocalDateTime createdAt ) { - public static ReportSummary of(User targeted, ChattingRoom chattingRoom) { - return new ReportSummary( - targeted.getProfile().getNickname(), - chattingRoom.getName(), - targeted.getId(), - chattingRoom.getId(), - targeted.getCreatedAt() - ); - } + public static ReportSummary of(User targeted, ChattingRoom chattingRoom) { + return new ReportSummary( + targeted.getProfile().getNickname(), + chattingRoom.getName(), + targeted.getId(), + chattingRoom.getId(), + targeted.getCreatedAt() + ); + } } diff --git a/src/main/java/coffeemeet/server/user/service/UserService.java b/src/main/java/coffeemeet/server/user/service/UserService.java index 9e49e450..c9338caf 100644 --- a/src/main/java/coffeemeet/server/user/service/UserService.java +++ b/src/main/java/coffeemeet/server/user/service/UserService.java @@ -1,6 +1,6 @@ package coffeemeet.server.user.service; -import static coffeemeet.server.common.domain.KeyType.PROFILE_IMAGE; +import static coffeemeet.server.common.domain.S3KeyPrefix.PROFILE_IMAGE; import static coffeemeet.server.common.execption.GlobalErrorCode.BAD_REQUEST_ERROR; import static coffeemeet.server.oauth.utils.constant.OAuthConstant.DEFAULT_IMAGE_URL; @@ -8,8 +8,8 @@ import coffeemeet.server.auth.domain.AuthTokensGenerator; import coffeemeet.server.certification.domain.Certification; import coffeemeet.server.certification.implement.CertificationQuery; +import coffeemeet.server.common.domain.ObjectStorage; import coffeemeet.server.common.execption.BadRequestException; -import coffeemeet.server.common.implement.MediaManager; import coffeemeet.server.matching.implement.MatchingQueueCommand; import coffeemeet.server.oauth.domain.OAuthMemberDetail; import coffeemeet.server.oauth.implement.client.OAuthMemberClientComposite; @@ -41,7 +41,7 @@ public class UserService { private static final String INVALID_REQUEST_MESSAGE = "사용자 상태에 맞지 않는 요청입니다."; - private final MediaManager mediaManager; + private final ObjectStorage objectStorage; private final OAuthMemberClientComposite oAuthMemberClientComposite; private final CertificationQuery certificationQuery; @@ -99,9 +99,9 @@ public void updateProfileImage(Long userId, File file) { User user = userQuery.getUserById(userId); deleteCurrentProfileImage(user.getOauthInfo().getProfileImageUrl()); - String key = mediaManager.generateKey(PROFILE_IMAGE); - mediaManager.upload(key, file); - user.updateProfileImageUrl(mediaManager.getUrl(key)); + String key = objectStorage.generateKey(PROFILE_IMAGE); + objectStorage.upload(key, file); + user.updateProfileImageUrl(objectStorage.getUrl(key)); userCommand.updateUser(user); } @@ -172,12 +172,12 @@ private UserStatusDto handleReportedUser(User user) { private void deleteCurrentProfileImage(String profileImageUrl) { if (!profileImageUrl.equals(DEFAULT_IMAGE_URL)) { - String currentKey = mediaManager.extractKey(profileImageUrl, + String currentKey = objectStorage.extractKey(profileImageUrl, PROFILE_IMAGE); if (currentKey.isBlank()) { return; } - mediaManager.delete(currentKey); + objectStorage.delete(currentKey); } } diff --git a/src/test/java/coffeemeet/server/admin/presentation/AdminControllerTest.java b/src/test/java/coffeemeet/server/admin/presentation/AdminControllerTest.java index 540a49af..653a2c65 100644 --- a/src/test/java/coffeemeet/server/admin/presentation/AdminControllerTest.java +++ b/src/test/java/coffeemeet/server/admin/presentation/AdminControllerTest.java @@ -3,8 +3,9 @@ import static coffeemeet.server.common.fixture.AdminFixture.adminLoginHTTPRequest; import static coffeemeet.server.common.fixture.AdminFixture.reportApprovalHTTPRequest; import static coffeemeet.server.common.fixture.AdminFixture.reportRejectionHTTPRequest; -import static coffeemeet.server.common.fixture.CertificationFixture.pageable; +import static coffeemeet.server.common.fixture.CertificationFixture.certificationPageable; import static coffeemeet.server.common.fixture.CertificationFixture.pendingCertificationPageDto; +import static coffeemeet.server.common.fixture.ReportFixture.reportListDto; import static com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper.document; import static com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper.resourceDetails; import static org.mockito.ArgumentMatchers.any; @@ -237,7 +238,7 @@ void findAllReportsTest() throws Exception { // given Long lastReportId = 0L; int pageSize = 10; - ReportListDto reportListDto = ReportFixture.reportListDto(); + ReportListDto reportListDto = reportListDto(); AdminCustomPage result = new AdminCustomPage<>(reportListDto.contents(), reportListDto.hasNext()); @@ -454,7 +455,7 @@ void viewInquiryTest() throws Exception { @DisplayName("회사 인증 대기중인 목록을 조회할 수 있다.") void getPendingCertificationsTest() throws Exception { // given - Pageable pageable = pageable(); + Pageable pageable = certificationPageable(); PendingCertificationPageDto pendingCertificationPageDto = pendingCertificationPageDto( pageable.getPageSize()); Page page = pendingCertificationPageDto.page(); diff --git a/src/test/java/coffeemeet/server/admin/service/AdminServiceTest.java b/src/test/java/coffeemeet/server/admin/service/AdminServiceTest.java index c4e3b81d..d75e5db4 100644 --- a/src/test/java/coffeemeet/server/admin/service/AdminServiceTest.java +++ b/src/test/java/coffeemeet/server/admin/service/AdminServiceTest.java @@ -15,7 +15,7 @@ import coffeemeet.server.certification.implement.CertificationCommand; import coffeemeet.server.certification.implement.CertificationQuery; import coffeemeet.server.common.fixture.InquiryFixture; -import coffeemeet.server.common.implement.FCMNotificationSender; +import coffeemeet.server.common.infrastructure.FCMNotificationSender; import coffeemeet.server.inquiry.domain.Inquiry; import coffeemeet.server.inquiry.implement.InquiryCommand; import coffeemeet.server.inquiry.implement.InquiryQuery; @@ -92,7 +92,7 @@ void approveCertificationTest() { adminService.approveCertification(certificationId); // then - then(certificationCommand).should(only()).certificated(certificationId); + then(certificationCommand).should(only()).completeCertification(certificationId); then(userQuery).should(only()).getNotificationInfoByUserId(userId); then(fcmNotificationSender).should(only()).sendNotification(any(), any()); } @@ -109,7 +109,7 @@ void rejectCertificationTest() { adminService.rejectCertification(certificationId); // then - then(certificationCommand).should(only()).deleteCertification(certificationId); + then(certificationCommand).should(only()).deleteCertificationByUserId(certificationId); then(certificationQuery).should(only()).getUserIdByCertificationId(certificationId); then(userQuery).should(only()).getNotificationInfoByUserId(certificationId); then(fcmNotificationSender).should(only()).sendNotification(any(), any()); diff --git a/src/test/java/coffeemeet/server/certification/domain/CertificationTest.java b/src/test/java/coffeemeet/server/certification/domain/CertificationTest.java new file mode 100644 index 00000000..c1c7acb6 --- /dev/null +++ b/src/test/java/coffeemeet/server/certification/domain/CertificationTest.java @@ -0,0 +1,35 @@ +package coffeemeet.server.certification.domain; + +import static coffeemeet.server.common.fixture.CertificationFixture.businessCardUrl; +import static coffeemeet.server.common.fixture.CertificationFixture.certification; +import static coffeemeet.server.common.fixture.CertificationFixture.companyEmail; +import static coffeemeet.server.common.fixture.CertificationFixture.companyName; +import static coffeemeet.server.common.fixture.CertificationFixture.department; +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class CertificationTest { + + @Test + @DisplayName("Certification을 업데이트 할 수 있다.") + void updateTest() { + // given + Certification certification = certification(); + String newCompanyName = companyName(); + CompanyEmail newCompanyEmail = companyEmail(); + String newBusinessCardUrl = businessCardUrl(); + Department newDepartment = department(); + + // when + certification.update(newCompanyName, newCompanyEmail, newBusinessCardUrl, newDepartment); + + // then + assertThat(certification).extracting(Certification::getCompanyName, + Certification::getCompanyEmail, Certification::getBusinessCardUrl, + Certification::getDepartment) + .containsExactly(newCompanyName, newCompanyEmail, newBusinessCardUrl, newDepartment); + } + +} diff --git a/src/test/java/coffeemeet/server/certification/infrastructure/CertificationRepositoryTest.java b/src/test/java/coffeemeet/server/certification/domain/repository/CertificationRepositoryTest.java similarity index 80% rename from src/test/java/coffeemeet/server/certification/infrastructure/CertificationRepositoryTest.java rename to src/test/java/coffeemeet/server/certification/domain/repository/CertificationRepositoryTest.java index 18774332..6c01ce69 100644 --- a/src/test/java/coffeemeet/server/certification/infrastructure/CertificationRepositoryTest.java +++ b/src/test/java/coffeemeet/server/certification/domain/repository/CertificationRepositoryTest.java @@ -1,9 +1,9 @@ -package coffeemeet.server.certification.infrastructure; +package coffeemeet.server.certification.domain.repository; import static coffeemeet.server.common.fixture.CertificationFixture.certification; -import static coffeemeet.server.common.fixture.CertificationFixture.pageable; +import static coffeemeet.server.common.fixture.CertificationFixture.certificationPageable; import static coffeemeet.server.common.fixture.UserFixture.user; -import static coffeemeet.server.common.fixture.UserFixture.users; +import static coffeemeet.server.common.fixture.UserFixture.usersWithNullId; import static org.assertj.core.api.Assertions.assertThat; import coffeemeet.server.certification.domain.Certification; @@ -12,7 +12,7 @@ import coffeemeet.server.user.domain.User; import coffeemeet.server.user.infrastructure.UserRepository; import java.util.List; -import org.junit.jupiter.api.AfterEach; +import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -35,12 +35,6 @@ void setUp() { user = userRepository.save(user()); } - @AfterEach - void tearDown() { - certificationRepository.deleteAll(); - userRepository.deleteAll(); - } - @Test @DisplayName("유저 아이디로 회사 인증 정보를 조회할 수 있다.") void findByUserIdTest() { @@ -63,23 +57,37 @@ void existsByCompanyEmailTest() { certificationRepository.existsByCompanyEmail(certification.getCompanyEmail())).isTrue(); } + @Test + @DisplayName("유저 아이디로 인증정보를 삭제할 수 있다") + void deleteByUserIdTest() { + // given + Certification certification = certification(user); + certificationRepository.save(certification); + + // when + certificationRepository.deleteByUserId(user.getId()); + + // then + Optional result = certificationRepository.findByUserId(user.getId()); + assertThat(result).isEmpty(); + } + @Test @DisplayName("아직 인증이 안된 회사 인증 요청을 페이지 조회할 수 있다.") - void findByIsCertificatedFalse() { + void findPendingCertificationsTest() { // given - List users = userRepository.saveAll(users()); + List users = userRepository.saveAll(usersWithNullId()); List certifications = certificationRepository.saveAll( users.stream() .map(CertificationFixture::certification) .toList() ); - long certificatedCount = certifications.stream() .filter(Certification::isCertificated) .count(); - Pageable pageable = pageable(); + Pageable pageable = certificationPageable(); // when Page foundCertification = certificationRepository.findPendingCertifications( diff --git a/src/test/java/coffeemeet/server/certification/implement/BusinessCardImageDeleterTest.java b/src/test/java/coffeemeet/server/certification/implement/BusinessCardImageDeleterTest.java new file mode 100644 index 00000000..99ba509f --- /dev/null +++ b/src/test/java/coffeemeet/server/certification/implement/BusinessCardImageDeleterTest.java @@ -0,0 +1,45 @@ +package coffeemeet.server.certification.implement; + +import static coffeemeet.server.common.domain.S3KeyPrefix.BUSINESS_CARD; +import static coffeemeet.server.common.fixture.CertificationFixture.certification; +import static coffeemeet.server.common.fixture.UserFixture.user; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.only; + +import coffeemeet.server.certification.domain.Certification; +import coffeemeet.server.common.implement.ImageDeleter; +import coffeemeet.server.user.domain.User; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class BusinessCardImageDeleterTest { + + @InjectMocks + private BusinessCardImageDeleter businessCardImageDeleter; + @Mock + private CertificationQuery certificationQuery; + @Mock + private ImageDeleter imageDeleter; + + @Test + void deleteBusinessCardImageByUserIdTest() { + // given + User user = user(); + Certification certification = certification(user); + Long userId = user.getId(); + + given(certificationQuery.getCertificationByUserId(userId)).willReturn(certification); + + // when + businessCardImageDeleter.deleteBusinessCardImageByUserId(userId); + + // then + then(imageDeleter).should(only()) + .deleteImage(certification.getBusinessCardUrl(), BUSINESS_CARD); + } +} diff --git a/src/test/java/coffeemeet/server/certification/implement/BusinessCardImageUploaderTest.java b/src/test/java/coffeemeet/server/certification/implement/BusinessCardImageUploaderTest.java new file mode 100644 index 00000000..471f6fcc --- /dev/null +++ b/src/test/java/coffeemeet/server/certification/implement/BusinessCardImageUploaderTest.java @@ -0,0 +1,36 @@ +package coffeemeet.server.certification.implement; + +import static coffeemeet.server.common.domain.S3KeyPrefix.BUSINESS_CARD; +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.only; + +import coffeemeet.server.common.implement.ImageUploader; +import java.io.File; +import org.instancio.Instancio; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class BusinessCardImageUploaderTest { + + @InjectMocks + private BusinessCardImageUploader businessCardImageUploader; + @Mock + private ImageUploader imageUploader; + + @Test + void uploadBusinessCardImage() { + // given + File businessCardImage = Instancio.create(File.class); + + // when + businessCardImageUploader.uploadBusinessCardImage(businessCardImage); + + // then + then(imageUploader).should(only()).uploadImage(businessCardImage, BUSINESS_CARD); + } + +} diff --git a/src/test/java/coffeemeet/server/certification/implement/CertificationCommandTest.java b/src/test/java/coffeemeet/server/certification/implement/CertificationCommandTest.java index b5232dc5..e4168d4a 100644 --- a/src/test/java/coffeemeet/server/certification/implement/CertificationCommandTest.java +++ b/src/test/java/coffeemeet/server/certification/implement/CertificationCommandTest.java @@ -5,9 +5,9 @@ import static coffeemeet.server.common.fixture.CertificationFixture.companyEmail; import static coffeemeet.server.common.fixture.CertificationFixture.companyName; import static coffeemeet.server.common.fixture.CertificationFixture.department; +import static coffeemeet.server.common.fixture.CertificationFixture.pendingCertification; import static coffeemeet.server.common.fixture.UserFixture.user; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; @@ -16,11 +16,9 @@ import coffeemeet.server.certification.domain.Certification; import coffeemeet.server.certification.domain.CompanyEmail; import coffeemeet.server.certification.domain.Department; -import coffeemeet.server.certification.infrastructure.CertificationRepository; -import coffeemeet.server.common.execption.InvalidInputException; +import coffeemeet.server.certification.domain.repository.CertificationRepository; import coffeemeet.server.user.domain.User; -import java.util.Optional; -import java.util.function.Consumer; +import coffeemeet.server.user.implement.UserQuery; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -31,61 +29,34 @@ @ExtendWith(MockitoExtension.class) class CertificationCommandTest { - @Mock - private Consumer consumer; @InjectMocks private CertificationCommand certificationCommand; @Mock private CertificationRepository certificationRepository; @Mock private CertificationQuery certificationQuery; + @Mock + private UserQuery userQuery; @Test @DisplayName("새로운 Certification 객체를 저장할 수 있다.") void createCertificationTest() { // given + User user = user(); String companyName = companyName(); CompanyEmail companyEmail = companyEmail(); String businessCardUrl = businessCardUrl(); Department department = department(); - User user = user(); - - // when - certificationCommand.createCertification(user, companyName, companyEmail, department, - businessCardUrl); - - // then - then(certificationRepository).should(only()).save(any(Certification.class)); - } - - @Test - @DisplayName("중복된 회사 이메일이 있을 경우 예외가 발생한다.") - void checkDuplicatedCompanyEmailTest() { - // given - CompanyEmail companyEmail = companyEmail(); - given(certificationRepository.existsByCompanyEmail(companyEmail)).willReturn(true); - - // when, then - assertThatThrownBy( - () -> certificationCommand.hasDuplicatedCompanyEmail(companyEmail)).isInstanceOf( - InvalidInputException.class); - } - - @Test - @DisplayName("만약 인증이 완료된 사용자면 Consumer를 실행할 수 있다.") - void applyIfCertifiedUserTest() { - // given - User user = user(); Long userId = user.getId(); - Certification certification = certification(user()); - given(certificationRepository.findByUserId(userId)).willReturn(Optional.of(certification)); + given(userQuery.getUserById(userId)).willReturn(user); // when - certificationCommand.applyIfCertifiedUser(userId, consumer); + certificationCommand.createCertification(userId, companyName, companyEmail, department, + businessCardUrl); // then - then(consumer).should(only()).accept(certification); + then(certificationRepository).should(only()).save(any(Certification.class)); } @Test @@ -94,10 +65,11 @@ void certificatedTest() { // given Certification certification = certification(); Long userId = certification.getUser().getId(); + given(certificationQuery.getCertificationByUserId(userId)).willReturn(certification); // when - certificationCommand.certificated(userId); + certificationCommand.completeCertification(userId); // then assertThat(certification.isCertificated()).isTrue(); @@ -110,10 +82,27 @@ void deleteUserCertification() { Long userId = 1L; // when - certificationCommand.deleteCertification(userId); + certificationCommand.deleteCertificationByUserId(userId); + + // then + then(certificationRepository).should(only()).deleteByUserId(userId); + } + + @Test + @DisplayName("인증 완료 시킬 수 있다.") + void completeCertificationTest() { + // given + User user = user(); + Long userId = user.getId(); + Certification certification = pendingCertification(user); + + given(certificationQuery.getCertificationByUserId(userId)).willReturn(certification); + + // when + certificationCommand.completeCertification(certification.getId()); // then - then(certificationRepository).should(only()).deleteById(userId); + assertThat(certification.isCertificated()).isTrue(); } } diff --git a/src/test/java/coffeemeet/server/certification/implement/CertificationQueryTest.java b/src/test/java/coffeemeet/server/certification/implement/CertificationQueryTest.java index 062a8ca7..d2902580 100644 --- a/src/test/java/coffeemeet/server/certification/implement/CertificationQueryTest.java +++ b/src/test/java/coffeemeet/server/certification/implement/CertificationQueryTest.java @@ -6,7 +6,7 @@ import static org.mockito.BDDMockito.given; import coffeemeet.server.certification.domain.Certification; -import coffeemeet.server.certification.infrastructure.CertificationRepository; +import coffeemeet.server.certification.domain.repository.CertificationRepository; import coffeemeet.server.user.domain.User; import java.util.Collections; import java.util.Optional; diff --git a/src/test/java/coffeemeet/server/certification/implement/CompanyEmailValidatorTest.java b/src/test/java/coffeemeet/server/certification/implement/CompanyEmailValidatorTest.java new file mode 100644 index 00000000..a6b4cf0d --- /dev/null +++ b/src/test/java/coffeemeet/server/certification/implement/CompanyEmailValidatorTest.java @@ -0,0 +1,46 @@ +package coffeemeet.server.certification.implement; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.when; + +import coffeemeet.server.certification.domain.CompanyEmail; +import coffeemeet.server.common.execption.InvalidInputException; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class CompanyEmailValidatorTest { + + @InjectMocks + private CompanyEmailValidator companyEmailValidator; + @Mock + private CertificationQuery certificationQuery; + + @Test + @DisplayName("회사 이메일이 중복되면 예외가 발생한다.") + void validateDuplicatedCompanyEmailTest_InvalidInputException() { + // given + CompanyEmail companyEmail = new CompanyEmail("test@example.com"); + when(certificationQuery.isExistedCompanyEmail(companyEmail)).thenReturn(true); + + // when, then + assertThrows(InvalidInputException.class, () -> + companyEmailValidator.validateDuplicatedCompanyEmail(companyEmail)); + } + + @Test + @DisplayName("회사 이메일이 중복되지 않으면 예외가 발생하지 않는다.") + void validateDuplicatedCompanyEmailTest() { + // given + CompanyEmail companyEmail = new CompanyEmail("unique@example.com"); + when(certificationQuery.isExistedCompanyEmail(companyEmail)).thenReturn(false); + + // when, then + companyEmailValidator.validateDuplicatedCompanyEmail(companyEmail); + } + +} diff --git a/src/test/java/coffeemeet/server/certification/implement/EmailVerificationCommandTest.java b/src/test/java/coffeemeet/server/certification/implement/EmailVerificationCommandTest.java index 2f8d9f3b..e69de29b 100644 --- a/src/test/java/coffeemeet/server/certification/implement/EmailVerificationCommandTest.java +++ b/src/test/java/coffeemeet/server/certification/implement/EmailVerificationCommandTest.java @@ -1,42 +0,0 @@ -package coffeemeet.server.certification.implement; - -import static coffeemeet.server.common.fixture.CertificationFixture.emailVerification; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.BDDMockito.given; -import static org.mockito.BDDMockito.then; -import static org.mockito.Mockito.only; - -import coffeemeet.server.certification.domain.EmailVerification; -import coffeemeet.server.certification.infrastructure.EmailVerificationRepository; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -@ExtendWith(MockitoExtension.class) -class EmailVerificationCommandTest { - - @InjectMocks - private EmailVerificationCommand emailVerificationCommand; - @Mock - private EmailVerificationRepository emailVerificationRepository; - - @Test - @DisplayName("새로운 EmailVerification 객체를 저장할 수 있다.") - void createEmailVerificationTest() { - // given - EmailVerification emailVerification = emailVerification(); - given(emailVerificationRepository.save(any(EmailVerification.class))).willReturn( - emailVerification); - - // when - emailVerificationCommand.createEmailVerification(emailVerification.getUserId(), - emailVerification.getCompanyEmail(), emailVerification.getCode()); - - // then - then(emailVerificationRepository).should(only()).save(any(EmailVerification.class)); - } - -} diff --git a/src/test/java/coffeemeet/server/certification/implement/EmailVerificationQueryTest.java b/src/test/java/coffeemeet/server/certification/implement/EmailVerificationQueryTest.java index 823411b7..e69de29b 100644 --- a/src/test/java/coffeemeet/server/certification/implement/EmailVerificationQueryTest.java +++ b/src/test/java/coffeemeet/server/certification/implement/EmailVerificationQueryTest.java @@ -1,41 +0,0 @@ -package coffeemeet.server.certification.implement; - -import static coffeemeet.server.common.fixture.CertificationFixture.emailVerification; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.BDDMockito.given; - -import coffeemeet.server.certification.domain.EmailVerification; -import coffeemeet.server.certification.infrastructure.EmailVerificationRepository; -import java.util.Optional; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -@ExtendWith(MockitoExtension.class) -class EmailVerificationQueryTest { - - @InjectMocks - private EmailVerificationQuery emailVerificationQuery; - @Mock - private EmailVerificationRepository emailVerificationRepository; - - @Test - @DisplayName("유저 아이디로 EmailVerification를 조회할 수 있다.") - void getCodeByIdTest() { - // given - EmailVerification emailVerification = emailVerification(); - - given(emailVerificationRepository.findById(emailVerification.getUserId())).willReturn( - Optional.of(emailVerification)); - - // when - String code = emailVerificationQuery.getCodeById(emailVerification.getUserId()); - - // then - assertThat(code).isEqualTo(emailVerification.getCode()); - } - -} diff --git a/src/test/java/coffeemeet/server/certification/implement/VerificationCodeGeneratorTest.java b/src/test/java/coffeemeet/server/certification/implement/VerificationCodeGeneratorTest.java new file mode 100644 index 00000000..692b9486 --- /dev/null +++ b/src/test/java/coffeemeet/server/certification/implement/VerificationCodeGeneratorTest.java @@ -0,0 +1,22 @@ +package coffeemeet.server.certification.implement; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class VerificationCodeGeneratorTest { + + private final VerificationCodeGenerator verificationCodeGenerator = new VerificationCodeGenerator(); + + @Test + @DisplayName("6자리의 랜덤 인증 코드를 생성할 수 있다.") + void generateVerificationCodeTest() { + // when + String code = verificationCodeGenerator.generateVerificationCode(); + + // then + assertThat(code).matches("[0-9]{6}"); + } + +} diff --git a/src/test/java/coffeemeet/server/certification/implement/VerificationCodeValidatorTest.java b/src/test/java/coffeemeet/server/certification/implement/VerificationCodeValidatorTest.java new file mode 100644 index 00000000..d3754513 --- /dev/null +++ b/src/test/java/coffeemeet/server/certification/implement/VerificationCodeValidatorTest.java @@ -0,0 +1,40 @@ +package coffeemeet.server.certification.implement; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import coffeemeet.server.common.execption.InvalidInputException; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class VerificationCodeValidatorTest { + + private final VerificationCodeValidator verificationCodeValidator = new VerificationCodeValidator(); + + @Test + @DisplayName("일치하는 인증 코드가 입력되면 예외가 발생하지 않는다") + void validateVerificationCodeTest() { + // given + String verificationCode = "123456"; + String userInputCode = "123456"; + + // when, then + assertThatCode( + () -> verificationCodeValidator.validateVerificationCode(verificationCode, userInputCode) + ).doesNotThrowAnyException(); + } + + @Test + @DisplayName("일치하지 않는 인증 코드가 입력되면 예외가 발생한다") + void validateVerificationCode_WithIncorrectCode_ShouldThrowException() { + // given + String verificationCode = "123456"; + String userInputCode = "654321"; + + // when, then + assertThatThrownBy( + () -> verificationCodeValidator.validateVerificationCode(verificationCode, userInputCode)) + .isInstanceOf(InvalidInputException.class); + } + +} diff --git a/src/test/java/coffeemeet/server/certification/implement/VerificationInfoCommandTest.java b/src/test/java/coffeemeet/server/certification/implement/VerificationInfoCommandTest.java new file mode 100644 index 00000000..f566b3ad --- /dev/null +++ b/src/test/java/coffeemeet/server/certification/implement/VerificationInfoCommandTest.java @@ -0,0 +1,42 @@ +package coffeemeet.server.certification.implement; + +import static coffeemeet.server.common.fixture.CertificationFixture.emailVerification; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.only; + +import coffeemeet.server.certification.domain.VerificationInfo; +import coffeemeet.server.certification.domain.repository.VerificationInfoRepository; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class VerificationInfoCommandTest { + + @InjectMocks + private VerificationInfoCommand verificationInfoCommand; + @Mock + private VerificationInfoRepository verificationInfoRepository; + + @Test + @DisplayName("새로운 VerificationInfo 객체를 저장할 수 있다.") + void createEmailVerificationTest() { + // given + VerificationInfo verificationInfo = emailVerification(); + given(verificationInfoRepository.save(any(VerificationInfo.class))).willReturn( + verificationInfo); + + // when + verificationInfoCommand.createVerificationInfo(verificationInfo.getUserId(), + verificationInfo.getCompanyEmail(), verificationInfo.getVerificationCode()); + + // then + then(verificationInfoRepository).should(only()).save(any(VerificationInfo.class)); + } + +} diff --git a/src/test/java/coffeemeet/server/certification/implement/VerificationInfoQueryTest.java b/src/test/java/coffeemeet/server/certification/implement/VerificationInfoQueryTest.java new file mode 100644 index 00000000..cb70dfd8 --- /dev/null +++ b/src/test/java/coffeemeet/server/certification/implement/VerificationInfoQueryTest.java @@ -0,0 +1,41 @@ +package coffeemeet.server.certification.implement; + +import static coffeemeet.server.common.fixture.CertificationFixture.emailVerification; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; + +import coffeemeet.server.certification.domain.VerificationInfo; +import coffeemeet.server.certification.domain.repository.VerificationInfoRepository; +import java.util.Optional; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class VerificationInfoQueryTest { + + @InjectMocks + private VerificationInfoQuery verificationInfoQuery; + @Mock + private VerificationInfoRepository verificationInfoRepository; + + @Test + @DisplayName("유저 아이디로 VerificationInfo를 조회할 수 있다.") + void getCodeByIdTest() { + // given + VerificationInfo verificationInfo = emailVerification(); + + given(verificationInfoRepository.findById(verificationInfo.getUserId())).willReturn( + Optional.of(verificationInfo)); + + // when + String code = verificationInfoQuery.getVerificationCodeById(verificationInfo.getUserId()); + + // then + assertThat(code).isEqualTo(verificationInfo.getVerificationCode()); + } + +} diff --git a/src/test/java/coffeemeet/server/certification/implement/VerificationMailSenderTest.java b/src/test/java/coffeemeet/server/certification/implement/VerificationMailSenderTest.java new file mode 100644 index 00000000..73af896f --- /dev/null +++ b/src/test/java/coffeemeet/server/certification/implement/VerificationMailSenderTest.java @@ -0,0 +1,41 @@ +package coffeemeet.server.certification.implement; + +import static coffeemeet.server.common.fixture.CertificationFixture.companyEmail; +import static coffeemeet.server.common.fixture.CertificationFixture.verificationCode; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.only; + +import coffeemeet.server.certification.domain.CompanyEmail; +import coffeemeet.server.common.infrastructure.EmailSender; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class VerificationMailSenderTest { + + @InjectMocks + private VerificationMailSender verificationMailSender; + + @Mock + private EmailSender emailSender; + + + @Test + @DisplayName("인증 이메일 전송할 수 있다.") + void sendVerificationMailTest() { + // given + CompanyEmail companyEmail = companyEmail(); + String verificationCode = verificationCode(); + + // when + verificationMailSender.sendVerificationMail(companyEmail, verificationCode); + + // then + then(emailSender).should(only()).sendEmail(anyString(), anyString(), anyString()); + } +} diff --git a/src/test/java/coffeemeet/server/certification/service/CertificationServiceTest.java b/src/test/java/coffeemeet/server/certification/service/CertificationServiceTest.java index ae6a8931..e6e0a428 100644 --- a/src/test/java/coffeemeet/server/certification/service/CertificationServiceTest.java +++ b/src/test/java/coffeemeet/server/certification/service/CertificationServiceTest.java @@ -1,48 +1,42 @@ package coffeemeet.server.certification.service; import static coffeemeet.server.common.fixture.CertificationFixture.businessCardUrl; -import static coffeemeet.server.common.fixture.CertificationFixture.certifications; +import static coffeemeet.server.common.fixture.CertificationFixture.certificationPageable; import static coffeemeet.server.common.fixture.CertificationFixture.companyName; import static coffeemeet.server.common.fixture.CertificationFixture.department; import static coffeemeet.server.common.fixture.CertificationFixture.email; +import static coffeemeet.server.common.fixture.CertificationFixture.pendingCertificationPage; import static coffeemeet.server.common.fixture.CertificationFixture.verificationCode; -import static coffeemeet.server.common.fixture.UserFixture.user; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatCode; -import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyLong; -import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.only; import coffeemeet.server.certification.domain.Certification; +import coffeemeet.server.certification.domain.CompanyEmail; +import coffeemeet.server.certification.domain.Department; +import coffeemeet.server.certification.implement.BusinessCardImageDeleter; +import coffeemeet.server.certification.implement.BusinessCardImageUploader; import coffeemeet.server.certification.implement.CertificationCommand; import coffeemeet.server.certification.implement.CertificationQuery; -import coffeemeet.server.certification.implement.EmailVerificationCommand; -import coffeemeet.server.certification.implement.EmailVerificationQuery; +import coffeemeet.server.certification.implement.CompanyEmailValidator; +import coffeemeet.server.certification.implement.VerificationCodeGenerator; +import coffeemeet.server.certification.implement.VerificationCodeValidator; +import coffeemeet.server.certification.implement.VerificationInfoCommand; +import coffeemeet.server.certification.implement.VerificationInfoQuery; +import coffeemeet.server.certification.implement.VerificationMailSender; import coffeemeet.server.certification.service.dto.PendingCertificationPageDto; -import coffeemeet.server.common.execption.InvalidInputException; -import coffeemeet.server.common.implement.EmailSender; -import coffeemeet.server.common.implement.MediaManager; -import coffeemeet.server.common.util.FileUtils; -import coffeemeet.server.user.domain.User; -import coffeemeet.server.user.implement.UserQuery; import java.io.File; -import java.util.List; import org.instancio.Instancio; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.MockedStatic; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; @ExtendWith(MockitoExtension.class) @@ -50,152 +44,131 @@ class CertificationServiceTest { @InjectMocks private CertificationService certificationService; - @Mock - private MediaManager mediaManager; - + private BusinessCardImageUploader businessCardImageUploader; @Mock - private EmailSender emailSender; - - @Mock - private UserQuery userQuery; - + private BusinessCardImageDeleter businessCardImageDeleter; @Mock private CertificationCommand certificationCommand; - @Mock private CertificationQuery certificationQuery; - @Mock - private EmailVerificationCommand emailVerificationCommand; - + private CompanyEmailValidator companyEmailValidator; + @Mock + private VerificationCodeGenerator verificationCodeGenerator; + @Mock + private VerificationCodeValidator verificationCodeValidator; + @Mock + private VerificationInfoQuery verificationInfoQuery; @Mock - private EmailVerificationQuery emailVerificationQuery; + private VerificationInfoCommand verificationInfoCommand; + @Mock + private VerificationMailSender verificationMailSender; @Test - @DisplayName("회사 정보를 등록할 수 있다.") + @DisplayName("인증 정보를 등록할 수 있다.") void registerCertificationTest() { // given - User user = user(); - Long userId = user.getId(); + Long userId = 1L; String companyName = companyName(); String email = email(); String departmentName = department().name(); - File file = mock(); - String businessCardUrl = businessCardUrl(); - - MockedStatic fileUtils = mockStatic(FileUtils.class); - fileUtils.when(() -> FileUtils.delete(file)).then(invocation -> null); + File businessCardImage = Instancio.create(File.class); + String expectedImageUrl = businessCardUrl(); - given(mediaManager.generateKey(any())).willReturn("someKey"); - given(mediaManager.getUrl(any())).willReturn(businessCardUrl); - given(userQuery.getUserById(userId)).willReturn(user); + given(businessCardImageUploader.uploadBusinessCardImage(any(File.class))) + .willReturn(expectedImageUrl); // when - certificationService.registerCertification(userId, companyName, email, departmentName, file); + certificationService.registerCertification(userId, companyName, email, departmentName, + businessCardImage); // then - then(mediaManager).should().generateKey(any()); - then(mediaManager).should().upload(any(), any()); - then(certificationCommand).should().createCertification(any(), any(), any(), any(), any()); - - fileUtils.close(); + then(certificationCommand).should(only()).createCertification(eq(userId), eq(companyName), + any(CompanyEmail.class), any(Department.class), eq(expectedImageUrl)); } @Test - @DisplayName("회사 정보를 수정할 수 있다.") + @DisplayName("인증 정보를 수정할 수 있다.") void updateCertificationTest() { // given - User user = user(); - Long userId = user.getId(); + long userId = 1L; String companyName = companyName(); String email = email(); String departmentName = department().name(); - File file = mock(); - String businessCardUrl = businessCardUrl(); + File businessCardImage = Instancio.create(File.class); + String expectedImageUrl = businessCardUrl(); - MockedStatic fileUtils = mockStatic(FileUtils.class); - fileUtils.when(() -> FileUtils.delete(file)).then(invocation -> null); - - given(mediaManager.generateKey(any())).willReturn("someKey"); - given(mediaManager.getUrl(any())).willReturn(businessCardUrl); - given(userQuery.getUserById(userId)).willReturn(user); + given(businessCardImageUploader.uploadBusinessCardImage(any(File.class))).willReturn( + expectedImageUrl); // when - certificationService.updateCertification(userId, companyName, email, departmentName, file); - - // then - - then(mediaManager).should().upload(any(), any()); - then(certificationCommand).should().deleteCertification(any()); - then(certificationCommand).should() - .createCertification(any(), any(), any(), any(), any()); + certificationService.updateCertification(userId, companyName, email, departmentName, + businessCardImage); - fileUtils.close(); + //then + then(businessCardImageDeleter).should(only()).deleteBusinessCardImageByUserId(userId); + then(certificationCommand).should(only()).updateCertification(eq(userId), eq(companyName), + any(CompanyEmail.class), any(Department.class), eq(expectedImageUrl)); } @Test @DisplayName("회사 인증 메일을 전송할 수 있다.") void sendVerificationMailTest() { // given + Long userId = 1L; String email = email(); - User user = user(); - Long userId = user.getId(); + String verificationCode = verificationCode(); + + given(verificationCodeGenerator.generateVerificationCode()).willReturn(verificationCode); // when certificationService.sendVerificationMail(userId, email); // then - then(certificationCommand).should(only()).hasDuplicatedCompanyEmail(any()); - then(emailSender).should(only()).sendVerificationCode(any(), anyString()); - then(emailVerificationCommand).should(only()) - .createEmailVerification(anyLong(), any(), anyString()); + then(companyEmailValidator).should(only()) + .validateDuplicatedCompanyEmail(any(CompanyEmail.class)); + then(verificationMailSender).should(only()) + .sendVerificationMail(any(CompanyEmail.class), eq(verificationCode)); + then(verificationInfoCommand).should(only()) + .createVerificationInfo(eq(userId), any(CompanyEmail.class), eq(verificationCode)); } @Test @DisplayName("인증 코드를 비교할 수 있다.") void compareCodeTest() { // given - Long userId = Instancio.create(Long.class); - String verificationCode = verificationCode(); - given(emailVerificationQuery.getCodeById(userId)).willReturn(verificationCode); + Long userId = 1L; + String userInputCode = "123456"; + String actualVerificationCode = "123456"; - // when, then - assertThatCode(() -> certificationService.compareCode(userId, verificationCode)) - .doesNotThrowAnyException(); - } + given(verificationInfoQuery.getVerificationCodeById(userId)).willReturn(actualVerificationCode); - @Test - @DisplayName("인증 코드가 일치하지 않다면 예외를 던진다.") - void compareCodeFailTest() { - // given - Long userId = Instancio.create(Long.class); - String correctCode = "correctCode"; - String verificationCode = verificationCode(); - given(emailVerificationQuery.getCodeById(userId)).willReturn(correctCode); + // when + certificationService.compareCode(userId, userInputCode); - // when, then - assertThatThrownBy(() -> certificationService.compareCode(userId, verificationCode)) - .isInstanceOf(InvalidInputException.class); + // then + then(verificationCodeValidator).should(only()) + .validateVerificationCode(eq(actualVerificationCode), eq(userInputCode)); } @Test @DisplayName("미인증 사용자 요청을 페이지로 가져올 수 있다.") void getUncertifiedUserRequestsTest() { // given - Pageable pageable = mock(Pageable.class); - List certificationList = certifications(); - Page pendingCertificationPage = new PageImpl<>(certificationList); + Pageable pageable = certificationPageable(); + Page certificationPage = pendingCertificationPage( + pageable.getPageSize()); - given(certificationQuery.getPendingCertification(pageable)).willReturn( - pendingCertificationPage); + given(certificationQuery.getPendingCertification(any(Pageable.class))).willReturn( + certificationPage); // when PendingCertificationPageDto result = certificationService.getUncertifiedUserRequests(pageable); // then assertThat(result).isNotNull(); - assertThat(result.page().getContent()).hasSize(certificationList.size()); + assertThat(result.page()).hasSize(certificationPage.getSize()); } } diff --git a/src/test/java/coffeemeet/server/chatting/concurrency/ChattingMessageServiceConcurrencyTest.java b/src/test/java/coffeemeet/server/chatting/concurrency/ChattingMessageServiceConcurrencyTest.java index 50791bdb..51a2e7a8 100644 --- a/src/test/java/coffeemeet/server/chatting/concurrency/ChattingMessageServiceConcurrencyTest.java +++ b/src/test/java/coffeemeet/server/chatting/concurrency/ChattingMessageServiceConcurrencyTest.java @@ -11,7 +11,7 @@ import coffeemeet.server.chatting.current.service.ChattingMessageService; import coffeemeet.server.common.fixture.ChattingFixture; import coffeemeet.server.common.fixture.UserFixture; -import coffeemeet.server.common.implement.FCMNotificationSender; +import coffeemeet.server.common.infrastructure.FCMNotificationSender; import coffeemeet.server.user.domain.User; import coffeemeet.server.user.infrastructure.UserRepository; import java.util.List; diff --git a/src/test/java/coffeemeet/server/chatting/current/service/ChattingMessageServiceTest.java b/src/test/java/coffeemeet/server/chatting/current/service/ChattingMessageServiceTest.java index 52d528d8..59e2d100 100644 --- a/src/test/java/coffeemeet/server/chatting/current/service/ChattingMessageServiceTest.java +++ b/src/test/java/coffeemeet/server/chatting/current/service/ChattingMessageServiceTest.java @@ -4,7 +4,7 @@ import static coffeemeet.server.common.fixture.ChattingFixture.chattingRoom; import static coffeemeet.server.common.fixture.UserFixture.fourUsers; import static coffeemeet.server.common.fixture.UserFixture.user; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anySet; import static org.mockito.BDDMockito.given; @@ -20,7 +20,7 @@ import coffeemeet.server.chatting.current.implement.ChattingSessionCommand; import coffeemeet.server.chatting.current.implement.ChattingSessionQuery; import coffeemeet.server.chatting.current.service.dto.ChattingDto; -import coffeemeet.server.common.implement.FCMNotificationSender; +import coffeemeet.server.common.infrastructure.FCMNotificationSender; import coffeemeet.server.user.domain.User; import coffeemeet.server.user.implement.UserCommand; import coffeemeet.server.user.implement.UserQuery; @@ -76,8 +76,7 @@ void chattingTest() { given(chattingRoomQuery.getChattingRoomById(chattingRoom.getId())).willReturn(chattingRoom); given(userQuery.getUsersByRoom(chattingRoom)).willReturn(users); given(userQuery.getUserById(user.getId())).willReturn(user); - willDoNothing().given(fcmNotificationSender) - .sendMultiNotifications(anySet(), any()); + willDoNothing().given(fcmNotificationSender).sendMultiNotifications(anySet(), any()); given(chattingMessageCommand.createChattingMessage(content, chattingRoom, user)).willReturn( chattingMessage); diff --git a/src/test/java/coffeemeet/server/chatting/current/service/ChattingRoomServiceTest.java b/src/test/java/coffeemeet/server/chatting/current/service/ChattingRoomServiceTest.java index 59845235..64898bd0 100644 --- a/src/test/java/coffeemeet/server/chatting/current/service/ChattingRoomServiceTest.java +++ b/src/test/java/coffeemeet/server/chatting/current/service/ChattingRoomServiceTest.java @@ -21,7 +21,7 @@ import coffeemeet.server.chatting.history.implement.UserChattingHistoryCommand; import coffeemeet.server.common.fixture.ChattingFixture; import coffeemeet.server.common.fixture.UserFixture; -import coffeemeet.server.common.implement.FCMNotificationSender; +import coffeemeet.server.common.infrastructure.FCMNotificationSender; import coffeemeet.server.user.domain.User; import coffeemeet.server.user.implement.UserQuery; import java.util.List; diff --git a/src/test/java/coffeemeet/server/common/fixture/CertificationFixture.java b/src/test/java/coffeemeet/server/common/fixture/CertificationFixture.java index 03dcea6f..8397bc08 100644 --- a/src/test/java/coffeemeet/server/common/fixture/CertificationFixture.java +++ b/src/test/java/coffeemeet/server/common/fixture/CertificationFixture.java @@ -6,10 +6,9 @@ import coffeemeet.server.certification.domain.Certification; import coffeemeet.server.certification.domain.CompanyEmail; import coffeemeet.server.certification.domain.Department; -import coffeemeet.server.certification.domain.EmailVerification; +import coffeemeet.server.certification.domain.VerificationInfo; import coffeemeet.server.certification.presentation.dto.EmailHTTP; import coffeemeet.server.certification.presentation.dto.VerificationCodeHTTP; -import coffeemeet.server.certification.service.dto.PendingCertification; import coffeemeet.server.certification.service.dto.PendingCertificationPageDto; import coffeemeet.server.user.domain.User; import java.util.List; @@ -17,6 +16,7 @@ import org.instancio.internal.generator.domain.internet.EmailGenerator; import org.instancio.internal.generator.lang.IntegerGenerator; import org.instancio.internal.generator.net.URLGenerator; +import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; @@ -43,7 +43,7 @@ public static Certification certification(User user) { public static Certification certificatedCertification(User user, String companyName) { return Instancio.of(Certification.class) .generate(field(Certification::getBusinessCardUrl), gen -> gen.net().url().asString()) - .set(field(Certification::getCompanyEmail), new CompanyEmail(new EmailGenerator().get())) + .set(field(Certification::getCompanyEmail), new CompanyEmail(email())) .set(field(Certification::getCompanyName), companyName) .set(field(Certification::getId), user.getId()) .set(field(Certification::getUser), user) @@ -51,6 +51,16 @@ public static Certification certificatedCertification(User user, String companyN .create(); } + public static Certification pendingCertification(User user) { + return Instancio.of(Certification.class) + .generate(field(Certification::getBusinessCardUrl), gen -> gen.net().url().asString()) + .set(field(Certification::getCompanyEmail), new CompanyEmail(email())) + .set(field(Certification::getId), user.getId()) + .set(field(Certification::getUser), user) + .set(field(Certification::isCertificated), false) + .create(); + } + public static List certificatedCertifications(List users, String companyName) { return users.stream() @@ -58,18 +68,14 @@ public static List certificatedCertifications(List users, .toList(); } - public static List certifications() { - return users().stream().map(CertificationFixture::certification).toList(); - } - - public static EmailVerification emailVerification() { - return Instancio.of(EmailVerification.class).set(field(EmailVerification::getCompanyEmail), + public static VerificationInfo emailVerification() { + return Instancio.of(VerificationInfo.class).set(field(VerificationInfo::getCompanyEmail), new CompanyEmail(new EmailGenerator().get())).create(); } - public static EmailVerification emailVerification(Long userId) { - return Instancio.of(EmailVerification.class).set(field(EmailVerification::getUserId), userId) - .set(field(EmailVerification::getCompanyEmail), + public static VerificationInfo emailVerification(Long userId) { + return Instancio.of(VerificationInfo.class).set(field(VerificationInfo::getUserId), userId) + .set(field(VerificationInfo::getCompanyEmail), new CompanyEmail(new EmailGenerator().get())).create(); } @@ -113,21 +119,25 @@ public static VerificationCodeHTTP.Request verificationCodeDtoRequest() { String.format("%06d", new IntegerGenerator().range(0, 999999).get())).create(); } - public static Pageable pageable() { + public static Pageable certificationPageable() { int page = 0; int size = new IntegerGenerator().range(1, 100).get(); return PageRequest.of(page, size, Sort.by("updatedAt").ascending()); } - public static PendingCertificationPageDto pendingCertificationPageDto(int size) { - return PendingCertificationPageDto.from(new PageImpl<>(pendingCertifications(size))); + public static Page pendingCertificationPage(int size) { + return new PageImpl<>(certificationsNotCertificated(size)); + } + + private static List certificationsNotCertificated(int size) { + List users = users(size); + return users.stream() + .map(CertificationFixture::pendingCertification) + .toList(); } - private static List pendingCertifications(int size) { - return Instancio.ofList(PendingCertification.class).size(size) - .generate(field(PendingCertification::businessCardUrl), - gen -> gen.net().url().asString()) - .generate(field(PendingCertification::companyEmail), gen -> gen.net().email()).create(); + public static PendingCertificationPageDto pendingCertificationPageDto(int size) { + return PendingCertificationPageDto.from(pendingCertificationPage(size)); } } diff --git a/src/test/java/coffeemeet/server/common/fixture/UserFixture.java b/src/test/java/coffeemeet/server/common/fixture/UserFixture.java index 2f77d6a4..803a23ed 100644 --- a/src/test/java/coffeemeet/server/common/fixture/UserFixture.java +++ b/src/test/java/coffeemeet/server/common/fixture/UserFixture.java @@ -36,6 +36,16 @@ public static User user() { .create(); } + public static User user(Long userId) { + return Instancio.of(User.class).set(field(User::getProfile), profile()) + .set(field(User::isRegistered), true) + .set(field(User::getId), userId) + .ignore(field(User::isDeleted)) + .ignore(field(User::isBlacklisted)) + .ignore(field(User::getChattingRoom)) + .create(); + } + public static User user(UserStatus userStatus) { return Instancio.of(User.class).set(field(User::getProfile), profile()) .set(field(User::getUserStatus), userStatus) @@ -64,6 +74,25 @@ public static List users() { .create(); } + public static List usersWithNullId() { + return Instancio.ofList(User.class) + .set(field(User::getId), null) + .ignore(field(User::isDeleted)) + .ignore(field(User::isBlacklisted)) + .ignore(field(User::getChattingRoom)) + .create(); + } + + public static List users(int size) { + return Instancio.ofList(User.class) + .size(size) + .generate(field(User::getId), gen -> gen.longSeq().start(1L)) + .ignore(field(User::isDeleted)) + .ignore(field(User::isBlacklisted)) + .ignore(field(User::getChattingRoom)) + .create(); + } + public static List fourUsers() { return Instancio.ofList(User.class).size(4) .generate(field(User::getId), gen -> gen.longSeq().start(1L)) diff --git a/src/test/java/coffeemeet/server/common/fixture/entity/ReportFixture.java b/src/test/java/coffeemeet/server/common/fixture/entity/ReportFixture.java new file mode 100644 index 00000000..e69de29b diff --git a/src/test/java/coffeemeet/server/common/implement/EmailSenderTest.java b/src/test/java/coffeemeet/server/common/implement/EmailSenderTest.java index cc8b9fd9..9df89765 100644 --- a/src/test/java/coffeemeet/server/common/implement/EmailSenderTest.java +++ b/src/test/java/coffeemeet/server/common/implement/EmailSenderTest.java @@ -1,15 +1,14 @@ package coffeemeet.server.common.implement; -import static coffeemeet.server.common.fixture.CertificationFixture.companyEmail; import static coffeemeet.server.common.fixture.CertificationFixture.email; -import static coffeemeet.server.common.fixture.CertificationFixture.verificationCode; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertAll; import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.only; -import coffeemeet.server.certification.domain.CompanyEmail; +import coffeemeet.server.common.infrastructure.EmailSender; import java.util.Objects; +import org.instancio.Instancio; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -41,14 +40,15 @@ void setUp() { } @Test - @DisplayName("인증코드가 포함된 메일을 보낼 수 있다.") + @DisplayName("메일을 보낼 수 있다.") void sendVerificationCodeTest() { // given - CompanyEmail companyEmail = companyEmail(); - String verificationCode = verificationCode(); + String email = email(); + String subject = Instancio.create(String.class); + String body = Instancio.create(String.class); // when - emailSender.sendVerificationCode(companyEmail, verificationCode); + emailSender.sendEmail(email, subject, body); // then then(javaMailSender).should(only()).send(simpleMailMessage.capture()); @@ -56,9 +56,8 @@ void sendVerificationCodeTest() { SimpleMailMessage sentMailMessage = simpleMailMessage.getValue(); assertAll( () -> assertThat(sentMailMessage.getFrom()).isEqualTo(sender), - () -> assertThat(Objects.requireNonNull(sentMailMessage.getTo())[0]).isEqualTo( - companyEmail.getValue()), - () -> assertThat(sentMailMessage.getText()).contains(verificationCode) + () -> assertThat(Objects.requireNonNull(sentMailMessage.getTo())[0]).isEqualTo(email), + () -> assertThat(sentMailMessage.getText()).contains(body) ); } } diff --git a/src/test/java/coffeemeet/server/common/implement/FCMNotificationSenderTest.java b/src/test/java/coffeemeet/server/common/implement/FCMNotificationSenderTest.java index 5434da42..f5cf7f25 100644 --- a/src/test/java/coffeemeet/server/common/implement/FCMNotificationSenderTest.java +++ b/src/test/java/coffeemeet/server/common/implement/FCMNotificationSenderTest.java @@ -8,6 +8,7 @@ import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.only; +import coffeemeet.server.common.infrastructure.FCMNotificationSender; import coffeemeet.server.user.domain.NotificationInfo; import com.google.firebase.messaging.FirebaseMessaging; import com.google.firebase.messaging.FirebaseMessagingException; diff --git a/src/test/java/coffeemeet/server/common/implement/S3MediaManagerTest.java b/src/test/java/coffeemeet/server/common/implement/S3ObjectStorageTest.java similarity index 76% rename from src/test/java/coffeemeet/server/common/implement/S3MediaManagerTest.java rename to src/test/java/coffeemeet/server/common/implement/S3ObjectStorageTest.java index 2f6dee27..958188a7 100644 --- a/src/test/java/coffeemeet/server/common/implement/S3MediaManagerTest.java +++ b/src/test/java/coffeemeet/server/common/implement/S3ObjectStorageTest.java @@ -7,7 +7,8 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.only; -import coffeemeet.server.common.domain.KeyType; +import coffeemeet.server.common.domain.S3KeyPrefix; +import coffeemeet.server.common.infrastructure.S3ObjectStorage; import com.amazonaws.services.s3.AmazonS3; import java.io.File; import java.net.URL; @@ -21,9 +22,9 @@ import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) -class S3MediaManagerTest { +class S3ObjectStorageTest { - private S3MediaManager s3MediaManager; + private S3ObjectStorage s3MediaManager; @Mock private AmazonS3 amazonS3; @@ -32,13 +33,13 @@ class S3MediaManagerTest { private String validKey; - private KeyType keyType; + private S3KeyPrefix s3KeyPrefix; @BeforeEach void setUp() { - keyType = Instancio.create(KeyType.class); - validKey = keyType.getValue() + "-" + Instancio.create(String.class); - s3MediaManager = new S3MediaManager(amazonS3, bucketName); + s3KeyPrefix = Instancio.create(S3KeyPrefix.class); + validKey = s3KeyPrefix.getValue() + "-" + Instancio.create(String.class); + s3MediaManager = new S3ObjectStorage(amazonS3, bucketName); } @Test @@ -86,13 +87,13 @@ void getUrlTest() { @DisplayName("s3 키를 생성할 수 있다") void generateKey() { // given - KeyType keyType = Instancio.create(KeyType.class); + S3KeyPrefix s3KeyPrefix = Instancio.create(S3KeyPrefix.class); // when - String key = s3MediaManager.generateKey(keyType); + String key = s3MediaManager.generateKey(s3KeyPrefix); // then - assertThat(key).contains(keyType.getValue()); + assertThat(key).contains(s3KeyPrefix.getValue()); } @Test @@ -100,7 +101,7 @@ void generateKey() { void extractKey() { // given, when String extractedKey = s3MediaManager.extractKey( - new URLGenerator().get().toExternalForm() + validKey, keyType); + new URLGenerator().get().toExternalForm() + validKey, s3KeyPrefix); // then assertThat(extractedKey).isEqualTo(validKey); diff --git a/src/test/java/coffeemeet/server/matching/service/MatchingServiceTest.java b/src/test/java/coffeemeet/server/matching/service/MatchingServiceTest.java index bffe700b..a1fdf74d 100644 --- a/src/test/java/coffeemeet/server/matching/service/MatchingServiceTest.java +++ b/src/test/java/coffeemeet/server/matching/service/MatchingServiceTest.java @@ -17,7 +17,7 @@ import coffeemeet.server.chatting.current.domain.ChattingRoom; import coffeemeet.server.chatting.current.implement.ChattingRoomCommand; import coffeemeet.server.common.execption.BadRequestException; -import coffeemeet.server.common.implement.FCMNotificationSender; +import coffeemeet.server.common.infrastructure.FCMNotificationSender; import coffeemeet.server.matching.implement.MatchingQueueCommand; import coffeemeet.server.matching.implement.MatchingQueueQuery; import coffeemeet.server.user.domain.NotificationInfo; @@ -73,8 +73,8 @@ void startTest() { User requestedUser = users.get(0); Certification requestedUsersCertification = certifications.get(0); ChattingRoom chattingRoom = chattingRoom(); - Set notificationInfos = users.stream().map(User::getNotificationInfo).collect( - Collectors.toSet()); + Set notificationInfos = users.stream().map(User::getNotificationInfo) + .collect(Collectors.toSet()); given(certificationQuery.getCertificationByUserId(requestedUser.getId())).willReturn( requestedUsersCertification); @@ -83,8 +83,7 @@ void startTest() { given(matchingQueueQuery.dequeueMatchingGroupSize(companyName, users.size())).willReturn( fourUsers().stream().map(User::getId).collect(Collectors.toSet())); given(chattingRoomCommand.createChattingRoom()).willReturn(chattingRoom); - given(userQuery.getNotificationInfosByIdSet(userIds)).willReturn( - notificationInfos); + given(userQuery.getNotificationInfosByIdSet(userIds)).willReturn(notificationInfos); // when matchingService.startMatching(requestedUser.getId()); @@ -95,8 +94,8 @@ void startTest() { then(userCommand).should().setToMatching(requestedUser.getId()); then(userCommand).should().assignUsersToChattingRoom(userIds, chattingRoom); then(fcmNotificationSender).should() - .sendMultiNotificationsWithData(notificationInfos, "두근두근 커피밋 채팅을 시작하세요!", - "chattingRoomId", String.valueOf(chattingRoom.getId())); + .sendMultiNotificationsWithData(notificationInfos, "두근두근 커피밋 채팅을 시작하세요!", "chattingRoomId", + String.valueOf(chattingRoom.getId())); } @Test @@ -118,7 +117,7 @@ void cancelMatchingTest() { } @Test - @DisplayName("매칭을 취소할 수 있다.") + @DisplayName("MATCHING 상태가 아닐 때 매칭을 취소하면 예외가 발생 한다.") void cancelMatchingTest_BadRequestException() { // given User user = userExcludingStatus(MATCHING); diff --git a/src/test/java/coffeemeet/server/user/service/UserServiceTest.java b/src/test/java/coffeemeet/server/user/service/UserServiceTest.java index 34cb5d5e..62dc07f6 100644 --- a/src/test/java/coffeemeet/server/user/service/UserServiceTest.java +++ b/src/test/java/coffeemeet/server/user/service/UserServiceTest.java @@ -1,6 +1,6 @@ package coffeemeet.server.user.service; -import static coffeemeet.server.common.domain.KeyType.PROFILE_IMAGE; +import static coffeemeet.server.common.domain.S3KeyPrefix.PROFILE_IMAGE; import static coffeemeet.server.common.fixture.AuthFixture.authTokens; import static coffeemeet.server.common.fixture.CertificationFixture.certification; import static coffeemeet.server.common.fixture.ChattingFixture.chattingRoom; @@ -30,8 +30,8 @@ import coffeemeet.server.auth.domain.AuthTokensGenerator; import coffeemeet.server.certification.domain.Certification; import coffeemeet.server.certification.implement.CertificationQuery; +import coffeemeet.server.common.domain.ObjectStorage; import coffeemeet.server.common.fixture.UserFixture; -import coffeemeet.server.common.implement.MediaManager; import coffeemeet.server.matching.implement.MatchingQueueCommand; import coffeemeet.server.oauth.domain.OAuthMemberDetail; import coffeemeet.server.oauth.implement.client.OAuthMemberClientComposite; @@ -67,7 +67,7 @@ class UserServiceTest { private UserService userService; @Mock - private MediaManager mediaManager; + private ObjectStorage objectStorage; @Mock private OAuthMemberClientComposite oAuthMemberClientComposite; @@ -226,9 +226,9 @@ void updateProfileImage() throws IOException { File file = File.createTempFile("temp", "png"); given(userQuery.getUserById(anyLong())).willReturn(user); - given(mediaManager.generateKey(PROFILE_IMAGE)).willReturn("key"); - given(mediaManager.getUrl(anyString())).willReturn("newImageUrl"); - given(mediaManager.extractKey(any(), eq(PROFILE_IMAGE))).willReturn(""); + given(objectStorage.generateKey(PROFILE_IMAGE)).willReturn("key"); + given(objectStorage.getUrl(anyString())).willReturn("newImageUrl"); + given(objectStorage.extractKey(any(), eq(PROFILE_IMAGE))).willReturn(""); // when userService.updateProfileImage(user.getId(), file);