Refactoring + migrate mail package to server.
This commit is contained in:
@@ -1,46 +0,0 @@
|
||||
package dev.rheinsw.shared.mail;
|
||||
|
||||
import dev.rheinsw.shared.mail.dto.MailRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* @author Thatsaphorn Atchariyaphap
|
||||
* @since 22.04.25
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class MailServiceClient {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MailServiceClient.class);
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
private static final String MAIL_ENDPOINT = "http://gateway/api/mail";
|
||||
|
||||
@Async
|
||||
public void sendMail(String email, String subject, String userMessage) {
|
||||
MailRequest request = new MailRequest(email, subject, userMessage);
|
||||
postEmail(request);
|
||||
}
|
||||
|
||||
private void postEmail(MailRequest request) {
|
||||
try {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
HttpEntity<MailRequest> entity = new HttpEntity<>(request, headers);
|
||||
|
||||
restTemplate.postForEntity(MAIL_ENDPOINT + "/send", entity, String.class);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to send email to {}: {}", request.getTo(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package dev.rheinsw.shared.mail.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author Thatsaphorn Atchariyaphap
|
||||
* @since 22.04.25
|
||||
*/
|
||||
@Data
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class MailRequest {
|
||||
private String to;
|
||||
private String subject;
|
||||
private String message;
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
package dev.rheinsw.shared.mail;
|
||||
|
||||
import dev.rheinsw.shared.mail.dto.MailRequest;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class MailServiceClientTest {
|
||||
|
||||
@Mock
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@InjectMocks
|
||||
private MailServiceClient mailServiceClient;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<HttpEntity<MailRequest>> httpEntityCaptor;
|
||||
|
||||
private AutoCloseable closeable;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
closeable = MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendMail_shouldSendCorrectRequest() {
|
||||
// Arrange
|
||||
String email = "user@example.com";
|
||||
String subject = "Test Subject";
|
||||
String message = "This is a test message.";
|
||||
|
||||
// Act
|
||||
mailServiceClient.sendMail(email, subject, message);
|
||||
|
||||
// Assert
|
||||
verify(restTemplate).postForEntity(
|
||||
eq("http://gateway/api/mail/send"),
|
||||
httpEntityCaptor.capture(),
|
||||
eq(String.class)
|
||||
);
|
||||
|
||||
MailRequest captured = httpEntityCaptor.getValue().getBody(); // extract the MailRequest
|
||||
assert captured != null;
|
||||
assert captured.getTo().equals(email);
|
||||
assert captured.getSubject().equals(subject);
|
||||
assert captured.getMessage().equals(message);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendMail_shouldHandleExceptionDuringPost() {
|
||||
// Arrange
|
||||
String email = "user@example.com";
|
||||
String subject = "Test Subject";
|
||||
String message = "This is a test message.";
|
||||
|
||||
doThrow(new RuntimeException("Simulated error")).when(restTemplate).postForEntity(
|
||||
anyString(),
|
||||
any(),
|
||||
eq(String.class)
|
||||
);
|
||||
|
||||
// Act & Assert
|
||||
assertDoesNotThrow(() -> mailServiceClient.sendMail(email, subject, message),
|
||||
"sendMail should handle exception internally and not throw it");
|
||||
}
|
||||
|
||||
|
||||
@AfterEach
|
||||
void tearDown() throws Exception {
|
||||
closeable.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user