-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Ref] 메일인증 비동기처리 개선 및 안정성 테스트코드 작성
- Loading branch information
Showing
9 changed files
with
151 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../soongsil/CoffeeChat/config/S3Config.java → ...ngsil/CoffeeChat/config/aws/S3Config.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/com/soongsil/CoffeeChat/config/aws/SqsConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.soongsil.CoffeeChat.config.aws; | ||
|
||
import io.awspring.cloud.sqs.config.SqsMessageListenerContainerFactory; | ||
import io.awspring.cloud.sqs.operations.SqsTemplate; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import software.amazon.awssdk.auth.credentials.AwsCredentials; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.sqs.SqsAsyncClient; | ||
|
||
public class SqsConfig { | ||
@Value("${spring.cloud.aws.region.static}") | ||
private String region; | ||
|
||
@Value("${spring.cloud.aws.credentials.access-key}") | ||
private String accessKey; | ||
|
||
@Value("${spring.cloud.aws.credentials.secret-key}") | ||
private String secretKey; | ||
// 클라이언트 설정 | ||
@Bean | ||
public SqsAsyncClient sqsAsyncClient() { | ||
return SqsAsyncClient.builder() | ||
.credentialsProvider(() -> new AwsCredentials() { | ||
@Override | ||
public String accessKeyId() { | ||
return accessKey; | ||
} | ||
@Override | ||
public String secretAccessKey() { | ||
return secretKey; | ||
} | ||
}) | ||
.region(Region.of(region)) | ||
.build(); | ||
} | ||
// Listener Factory 설정 (Listener쪽에서만 설정하면 됨) | ||
@Bean | ||
public SqsMessageListenerContainerFactory<Object> defaultSqsListenerContainerFactory() { | ||
return SqsMessageListenerContainerFactory | ||
.builder() | ||
.sqsAsyncClient(sqsAsyncClient()) | ||
.build(); | ||
} | ||
|
||
// 메세지 발송을 위한 SQS 템플릿 설정 (Sender쪽에서만 설정하면 됨) | ||
@Bean | ||
public SqsTemplate sqsTemplate() { | ||
return SqsTemplate.newTemplate(sqsAsyncClient()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
src/test/java/com/soongsil/CoffeeChat/Concurrency/ConcurrencyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/test/java/com/soongsil/CoffeeChat/Mail/EmailServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.soongsil.CoffeeChat.Mail; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
import com.soongsil.CoffeeChat.util.email.EmailUtil; | ||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.internet.MimeMessage; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class EmailServiceTest { | ||
|
||
@Mock | ||
private JavaMailSender javaMailSender; | ||
|
||
@InjectMocks | ||
private EmailUtil emailUtil; | ||
|
||
@Mock | ||
private MimeMessage mimeMessage; | ||
|
||
@Mock | ||
private ApplicationContext applicationContext; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
// JavaMailSender의 createMimeMessage 메서드가 mock MimeMessage 객체를 반환하도록 설정 | ||
when(javaMailSender.createMimeMessage()).thenReturn(mimeMessage); | ||
|
||
// EmailUtil 프록시를 반환하도록 ApplicationContext 설정 | ||
when(applicationContext.getBean(EmailUtil.class)).thenReturn(emailUtil); | ||
} | ||
|
||
@Test | ||
void testSendAuthenticationEmailReturnsCodeAndHandlesException() throws MessagingException, InterruptedException { | ||
// given | ||
String receiver = "[email protected]"; | ||
// sendMail이 호출될 때 예외를 던지도록 설정(런타임) | ||
doThrow(new RuntimeException("Failed to send email")) | ||
.when(javaMailSender).send(any(MimeMessage.class)); | ||
// when | ||
String resultCode = null; | ||
try { | ||
resultCode = emailUtil.sendAuthenticationEmail(receiver); | ||
} catch (RuntimeException e) { | ||
// 예외가 발생해도 메서드가 종료되지 않고 정상적으로 처리되었는지 확인 | ||
System.out.println("예외 발생: " + e.getMessage()); | ||
} | ||
// then | ||
//assertNotNull(resultCode); // 반환된 코드가 null이 아닌지 확인 | ||
assertEquals(6, resultCode.length()); // 반환된 코드의 길이가 6자리인지 확인 | ||
assertTrue(resultCode.matches("\\d{6}")); // 반환된 코드가 6자리 숫자인지 확인 | ||
System.out.println("resultCode = " + resultCode); | ||
verify(javaMailSender, times(1)).send(any(MimeMessage.class)); // sendMail이 한 번만 호출되었는지 확인 | ||
} | ||
} |